1 //===- LazyValueInfo.cpp - Value constraint analysis ------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the interface for lazy computation of value constraint 10 // information. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Analysis/LazyValueInfo.h" 15 #include "llvm/ADT/DenseSet.h" 16 #include "llvm/ADT/STLExtras.h" 17 #include "llvm/Analysis/AssumptionCache.h" 18 #include "llvm/Analysis/ConstantFolding.h" 19 #include "llvm/Analysis/InstructionSimplify.h" 20 #include "llvm/Analysis/TargetLibraryInfo.h" 21 #include "llvm/Analysis/ValueLattice.h" 22 #include "llvm/Analysis/ValueTracking.h" 23 #include "llvm/IR/AssemblyAnnotationWriter.h" 24 #include "llvm/IR/CFG.h" 25 #include "llvm/IR/ConstantRange.h" 26 #include "llvm/IR/Constants.h" 27 #include "llvm/IR/DataLayout.h" 28 #include "llvm/IR/Dominators.h" 29 #include "llvm/IR/Instructions.h" 30 #include "llvm/IR/IntrinsicInst.h" 31 #include "llvm/IR/Intrinsics.h" 32 #include "llvm/IR/LLVMContext.h" 33 #include "llvm/IR/PatternMatch.h" 34 #include "llvm/IR/ValueHandle.h" 35 #include "llvm/InitializePasses.h" 36 #include "llvm/Support/Debug.h" 37 #include "llvm/Support/FormattedStream.h" 38 #include "llvm/Support/KnownBits.h" 39 #include "llvm/Support/raw_ostream.h" 40 #include <optional> 41 using namespace llvm; 42 using namespace PatternMatch; 43 44 #define DEBUG_TYPE "lazy-value-info" 45 46 // This is the number of worklist items we will process to try to discover an 47 // answer for a given value. 48 static const unsigned MaxProcessedPerValue = 500; 49 50 char LazyValueInfoWrapperPass::ID = 0; 51 LazyValueInfoWrapperPass::LazyValueInfoWrapperPass() : FunctionPass(ID) { 52 initializeLazyValueInfoWrapperPassPass(*PassRegistry::getPassRegistry()); 53 } 54 INITIALIZE_PASS_BEGIN(LazyValueInfoWrapperPass, "lazy-value-info", 55 "Lazy Value Information Analysis", false, true) 56 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 57 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 58 INITIALIZE_PASS_END(LazyValueInfoWrapperPass, "lazy-value-info", 59 "Lazy Value Information Analysis", false, true) 60 61 namespace llvm { 62 FunctionPass *createLazyValueInfoPass() { return new LazyValueInfoWrapperPass(); } 63 } 64 65 AnalysisKey LazyValueAnalysis::Key; 66 67 /// Returns true if this lattice value represents at most one possible value. 68 /// This is as precise as any lattice value can get while still representing 69 /// reachable code. 70 static bool hasSingleValue(const ValueLatticeElement &Val) { 71 if (Val.isConstantRange() && 72 Val.getConstantRange().isSingleElement()) 73 // Integer constants are single element ranges 74 return true; 75 if (Val.isConstant()) 76 // Non integer constants 77 return true; 78 return false; 79 } 80 81 /// Combine two sets of facts about the same value into a single set of 82 /// facts. Note that this method is not suitable for merging facts along 83 /// different paths in a CFG; that's what the mergeIn function is for. This 84 /// is for merging facts gathered about the same value at the same location 85 /// through two independent means. 86 /// Notes: 87 /// * This method does not promise to return the most precise possible lattice 88 /// value implied by A and B. It is allowed to return any lattice element 89 /// which is at least as strong as *either* A or B (unless our facts 90 /// conflict, see below). 91 /// * Due to unreachable code, the intersection of two lattice values could be 92 /// contradictory. If this happens, we return some valid lattice value so as 93 /// not confuse the rest of LVI. Ideally, we'd always return Undefined, but 94 /// we do not make this guarantee. TODO: This would be a useful enhancement. 95 static ValueLatticeElement intersect(const ValueLatticeElement &A, 96 const ValueLatticeElement &B) { 97 // Undefined is the strongest state. It means the value is known to be along 98 // an unreachable path. 99 if (A.isUnknown()) 100 return A; 101 if (B.isUnknown()) 102 return B; 103 104 // If we gave up for one, but got a useable fact from the other, use it. 105 if (A.isOverdefined()) 106 return B; 107 if (B.isOverdefined()) 108 return A; 109 110 // Can't get any more precise than constants. 111 if (hasSingleValue(A)) 112 return A; 113 if (hasSingleValue(B)) 114 return B; 115 116 // Could be either constant range or not constant here. 117 if (!A.isConstantRange() || !B.isConstantRange()) { 118 // TODO: Arbitrary choice, could be improved 119 return A; 120 } 121 122 // Intersect two constant ranges 123 ConstantRange Range = 124 A.getConstantRange().intersectWith(B.getConstantRange()); 125 // Note: An empty range is implicitly converted to unknown or undef depending 126 // on MayIncludeUndef internally. 127 return ValueLatticeElement::getRange( 128 std::move(Range), /*MayIncludeUndef=*/A.isConstantRangeIncludingUndef() || 129 B.isConstantRangeIncludingUndef()); 130 } 131 132 //===----------------------------------------------------------------------===// 133 // LazyValueInfoCache Decl 134 //===----------------------------------------------------------------------===// 135 136 namespace { 137 /// A callback value handle updates the cache when values are erased. 138 class LazyValueInfoCache; 139 struct LVIValueHandle final : public CallbackVH { 140 LazyValueInfoCache *Parent; 141 142 LVIValueHandle(Value *V, LazyValueInfoCache *P = nullptr) 143 : CallbackVH(V), Parent(P) { } 144 145 void deleted() override; 146 void allUsesReplacedWith(Value *V) override { 147 deleted(); 148 } 149 }; 150 } // end anonymous namespace 151 152 namespace { 153 using NonNullPointerSet = SmallDenseSet<AssertingVH<Value>, 2>; 154 155 /// This is the cache kept by LazyValueInfo which 156 /// maintains information about queries across the clients' queries. 157 class LazyValueInfoCache { 158 /// This is all of the cached information for one basic block. It contains 159 /// the per-value lattice elements, as well as a separate set for 160 /// overdefined values to reduce memory usage. Additionally pointers 161 /// dereferenced in the block are cached for nullability queries. 162 struct BlockCacheEntry { 163 SmallDenseMap<AssertingVH<Value>, ValueLatticeElement, 4> LatticeElements; 164 SmallDenseSet<AssertingVH<Value>, 4> OverDefined; 165 // std::nullopt indicates that the nonnull pointers for this basic block 166 // block have not been computed yet. 167 std::optional<NonNullPointerSet> NonNullPointers; 168 }; 169 170 /// Cached information per basic block. 171 DenseMap<PoisoningVH<BasicBlock>, std::unique_ptr<BlockCacheEntry>> 172 BlockCache; 173 /// Set of value handles used to erase values from the cache on deletion. 174 DenseSet<LVIValueHandle, DenseMapInfo<Value *>> ValueHandles; 175 176 const BlockCacheEntry *getBlockEntry(BasicBlock *BB) const { 177 auto It = BlockCache.find_as(BB); 178 if (It == BlockCache.end()) 179 return nullptr; 180 return It->second.get(); 181 } 182 183 BlockCacheEntry *getOrCreateBlockEntry(BasicBlock *BB) { 184 auto It = BlockCache.find_as(BB); 185 if (It == BlockCache.end()) 186 It = BlockCache.insert({ BB, std::make_unique<BlockCacheEntry>() }) 187 .first; 188 189 return It->second.get(); 190 } 191 192 void addValueHandle(Value *Val) { 193 auto HandleIt = ValueHandles.find_as(Val); 194 if (HandleIt == ValueHandles.end()) 195 ValueHandles.insert({ Val, this }); 196 } 197 198 public: 199 void insertResult(Value *Val, BasicBlock *BB, 200 const ValueLatticeElement &Result) { 201 BlockCacheEntry *Entry = getOrCreateBlockEntry(BB); 202 203 // Insert over-defined values into their own cache to reduce memory 204 // overhead. 205 if (Result.isOverdefined()) 206 Entry->OverDefined.insert(Val); 207 else 208 Entry->LatticeElements.insert({ Val, Result }); 209 210 addValueHandle(Val); 211 } 212 213 std::optional<ValueLatticeElement> 214 getCachedValueInfo(Value *V, BasicBlock *BB) const { 215 const BlockCacheEntry *Entry = getBlockEntry(BB); 216 if (!Entry) 217 return std::nullopt; 218 219 if (Entry->OverDefined.count(V)) 220 return ValueLatticeElement::getOverdefined(); 221 222 auto LatticeIt = Entry->LatticeElements.find_as(V); 223 if (LatticeIt == Entry->LatticeElements.end()) 224 return std::nullopt; 225 226 return LatticeIt->second; 227 } 228 229 bool isNonNullAtEndOfBlock( 230 Value *V, BasicBlock *BB, 231 function_ref<NonNullPointerSet(BasicBlock *)> InitFn) { 232 BlockCacheEntry *Entry = getOrCreateBlockEntry(BB); 233 if (!Entry->NonNullPointers) { 234 Entry->NonNullPointers = InitFn(BB); 235 for (Value *V : *Entry->NonNullPointers) 236 addValueHandle(V); 237 } 238 239 return Entry->NonNullPointers->count(V); 240 } 241 242 /// clear - Empty the cache. 243 void clear() { 244 BlockCache.clear(); 245 ValueHandles.clear(); 246 } 247 248 /// Inform the cache that a given value has been deleted. 249 void eraseValue(Value *V); 250 251 /// This is part of the update interface to inform the cache 252 /// that a block has been deleted. 253 void eraseBlock(BasicBlock *BB); 254 255 /// Updates the cache to remove any influence an overdefined value in 256 /// OldSucc might have (unless also overdefined in NewSucc). This just 257 /// flushes elements from the cache and does not add any. 258 void threadEdgeImpl(BasicBlock *OldSucc,BasicBlock *NewSucc); 259 }; 260 } 261 262 void LazyValueInfoCache::eraseValue(Value *V) { 263 for (auto &Pair : BlockCache) { 264 Pair.second->LatticeElements.erase(V); 265 Pair.second->OverDefined.erase(V); 266 if (Pair.second->NonNullPointers) 267 Pair.second->NonNullPointers->erase(V); 268 } 269 270 auto HandleIt = ValueHandles.find_as(V); 271 if (HandleIt != ValueHandles.end()) 272 ValueHandles.erase(HandleIt); 273 } 274 275 void LVIValueHandle::deleted() { 276 // This erasure deallocates *this, so it MUST happen after we're done 277 // using any and all members of *this. 278 Parent->eraseValue(*this); 279 } 280 281 void LazyValueInfoCache::eraseBlock(BasicBlock *BB) { 282 BlockCache.erase(BB); 283 } 284 285 void LazyValueInfoCache::threadEdgeImpl(BasicBlock *OldSucc, 286 BasicBlock *NewSucc) { 287 // When an edge in the graph has been threaded, values that we could not 288 // determine a value for before (i.e. were marked overdefined) may be 289 // possible to solve now. We do NOT try to proactively update these values. 290 // Instead, we clear their entries from the cache, and allow lazy updating to 291 // recompute them when needed. 292 293 // The updating process is fairly simple: we need to drop cached info 294 // for all values that were marked overdefined in OldSucc, and for those same 295 // values in any successor of OldSucc (except NewSucc) in which they were 296 // also marked overdefined. 297 std::vector<BasicBlock*> worklist; 298 worklist.push_back(OldSucc); 299 300 const BlockCacheEntry *Entry = getBlockEntry(OldSucc); 301 if (!Entry || Entry->OverDefined.empty()) 302 return; // Nothing to process here. 303 SmallVector<Value *, 4> ValsToClear(Entry->OverDefined.begin(), 304 Entry->OverDefined.end()); 305 306 // Use a worklist to perform a depth-first search of OldSucc's successors. 307 // NOTE: We do not need a visited list since any blocks we have already 308 // visited will have had their overdefined markers cleared already, and we 309 // thus won't loop to their successors. 310 while (!worklist.empty()) { 311 BasicBlock *ToUpdate = worklist.back(); 312 worklist.pop_back(); 313 314 // Skip blocks only accessible through NewSucc. 315 if (ToUpdate == NewSucc) continue; 316 317 // If a value was marked overdefined in OldSucc, and is here too... 318 auto OI = BlockCache.find_as(ToUpdate); 319 if (OI == BlockCache.end() || OI->second->OverDefined.empty()) 320 continue; 321 auto &ValueSet = OI->second->OverDefined; 322 323 bool changed = false; 324 for (Value *V : ValsToClear) { 325 if (!ValueSet.erase(V)) 326 continue; 327 328 // If we removed anything, then we potentially need to update 329 // blocks successors too. 330 changed = true; 331 } 332 333 if (!changed) continue; 334 335 llvm::append_range(worklist, successors(ToUpdate)); 336 } 337 } 338 339 340 namespace { 341 /// An assembly annotator class to print LazyValueCache information in 342 /// comments. 343 class LazyValueInfoImpl; 344 class LazyValueInfoAnnotatedWriter : public AssemblyAnnotationWriter { 345 LazyValueInfoImpl *LVIImpl; 346 // While analyzing which blocks we can solve values for, we need the dominator 347 // information. 348 DominatorTree &DT; 349 350 public: 351 LazyValueInfoAnnotatedWriter(LazyValueInfoImpl *L, DominatorTree &DTree) 352 : LVIImpl(L), DT(DTree) {} 353 354 void emitBasicBlockStartAnnot(const BasicBlock *BB, 355 formatted_raw_ostream &OS) override; 356 357 void emitInstructionAnnot(const Instruction *I, 358 formatted_raw_ostream &OS) override; 359 }; 360 } 361 namespace { 362 // The actual implementation of the lazy analysis and update. Note that the 363 // inheritance from LazyValueInfoCache is intended to be temporary while 364 // splitting the code and then transitioning to a has-a relationship. 365 class LazyValueInfoImpl { 366 367 /// Cached results from previous queries 368 LazyValueInfoCache TheCache; 369 370 /// This stack holds the state of the value solver during a query. 371 /// It basically emulates the callstack of the naive 372 /// recursive value lookup process. 373 SmallVector<std::pair<BasicBlock*, Value*>, 8> BlockValueStack; 374 375 /// Keeps track of which block-value pairs are in BlockValueStack. 376 DenseSet<std::pair<BasicBlock*, Value*> > BlockValueSet; 377 378 /// Push BV onto BlockValueStack unless it's already in there. 379 /// Returns true on success. 380 bool pushBlockValue(const std::pair<BasicBlock *, Value *> &BV) { 381 if (!BlockValueSet.insert(BV).second) 382 return false; // It's already in the stack. 383 384 LLVM_DEBUG(dbgs() << "PUSH: " << *BV.second << " in " 385 << BV.first->getName() << "\n"); 386 BlockValueStack.push_back(BV); 387 return true; 388 } 389 390 AssumptionCache *AC; ///< A pointer to the cache of @llvm.assume calls. 391 const DataLayout &DL; ///< A mandatory DataLayout 392 393 /// Declaration of the llvm.experimental.guard() intrinsic, 394 /// if it exists in the module. 395 Function *GuardDecl; 396 397 std::optional<ValueLatticeElement> getBlockValue(Value *Val, BasicBlock *BB, 398 Instruction *CxtI); 399 std::optional<ValueLatticeElement> getEdgeValue(Value *V, BasicBlock *F, 400 BasicBlock *T, 401 Instruction *CxtI = nullptr); 402 403 // These methods process one work item and may add more. A false value 404 // returned means that the work item was not completely processed and must 405 // be revisited after going through the new items. 406 bool solveBlockValue(Value *Val, BasicBlock *BB); 407 std::optional<ValueLatticeElement> solveBlockValueImpl(Value *Val, 408 BasicBlock *BB); 409 std::optional<ValueLatticeElement> solveBlockValueNonLocal(Value *Val, 410 BasicBlock *BB); 411 std::optional<ValueLatticeElement> solveBlockValuePHINode(PHINode *PN, 412 BasicBlock *BB); 413 std::optional<ValueLatticeElement> solveBlockValueSelect(SelectInst *S, 414 BasicBlock *BB); 415 std::optional<ConstantRange> getRangeFor(Value *V, Instruction *CxtI, 416 BasicBlock *BB); 417 std::optional<ValueLatticeElement> solveBlockValueBinaryOpImpl( 418 Instruction *I, BasicBlock *BB, 419 std::function<ConstantRange(const ConstantRange &, const ConstantRange &)> 420 OpFn); 421 std::optional<ValueLatticeElement> 422 solveBlockValueBinaryOp(BinaryOperator *BBI, BasicBlock *BB); 423 std::optional<ValueLatticeElement> solveBlockValueCast(CastInst *CI, 424 BasicBlock *BB); 425 std::optional<ValueLatticeElement> 426 solveBlockValueOverflowIntrinsic(WithOverflowInst *WO, BasicBlock *BB); 427 std::optional<ValueLatticeElement> solveBlockValueIntrinsic(IntrinsicInst *II, 428 BasicBlock *BB); 429 std::optional<ValueLatticeElement> 430 solveBlockValueExtractValue(ExtractValueInst *EVI, BasicBlock *BB); 431 bool isNonNullAtEndOfBlock(Value *Val, BasicBlock *BB); 432 void intersectAssumeOrGuardBlockValueConstantRange(Value *Val, 433 ValueLatticeElement &BBLV, 434 Instruction *BBI); 435 436 void solve(); 437 438 public: 439 /// This is the query interface to determine the lattice value for the 440 /// specified Value* at the context instruction (if specified) or at the 441 /// start of the block. 442 ValueLatticeElement getValueInBlock(Value *V, BasicBlock *BB, 443 Instruction *CxtI = nullptr); 444 445 /// This is the query interface to determine the lattice value for the 446 /// specified Value* at the specified instruction using only information 447 /// from assumes/guards and range metadata. Unlike getValueInBlock(), no 448 /// recursive query is performed. 449 ValueLatticeElement getValueAt(Value *V, Instruction *CxtI); 450 451 /// This is the query interface to determine the lattice 452 /// value for the specified Value* that is true on the specified edge. 453 ValueLatticeElement getValueOnEdge(Value *V, BasicBlock *FromBB, 454 BasicBlock *ToBB, 455 Instruction *CxtI = nullptr); 456 457 /// Complete flush all previously computed values 458 void clear() { 459 TheCache.clear(); 460 } 461 462 /// Printing the LazyValueInfo Analysis. 463 void printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS) { 464 LazyValueInfoAnnotatedWriter Writer(this, DTree); 465 F.print(OS, &Writer); 466 } 467 468 /// This is part of the update interface to remove information related to this 469 /// value from the cache. 470 void forgetValue(Value *V) { TheCache.eraseValue(V); } 471 472 /// This is part of the update interface to inform the cache 473 /// that a block has been deleted. 474 void eraseBlock(BasicBlock *BB) { 475 TheCache.eraseBlock(BB); 476 } 477 478 /// This is the update interface to inform the cache that an edge from 479 /// PredBB to OldSucc has been threaded to be from PredBB to NewSucc. 480 void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc); 481 482 LazyValueInfoImpl(AssumptionCache *AC, const DataLayout &DL, 483 Function *GuardDecl) 484 : AC(AC), DL(DL), GuardDecl(GuardDecl) {} 485 }; 486 } // end anonymous namespace 487 488 489 void LazyValueInfoImpl::solve() { 490 SmallVector<std::pair<BasicBlock *, Value *>, 8> StartingStack( 491 BlockValueStack.begin(), BlockValueStack.end()); 492 493 unsigned processedCount = 0; 494 while (!BlockValueStack.empty()) { 495 processedCount++; 496 // Abort if we have to process too many values to get a result for this one. 497 // Because of the design of the overdefined cache currently being per-block 498 // to avoid naming-related issues (IE it wants to try to give different 499 // results for the same name in different blocks), overdefined results don't 500 // get cached globally, which in turn means we will often try to rediscover 501 // the same overdefined result again and again. Once something like 502 // PredicateInfo is used in LVI or CVP, we should be able to make the 503 // overdefined cache global, and remove this throttle. 504 if (processedCount > MaxProcessedPerValue) { 505 LLVM_DEBUG( 506 dbgs() << "Giving up on stack because we are getting too deep\n"); 507 // Fill in the original values 508 while (!StartingStack.empty()) { 509 std::pair<BasicBlock *, Value *> &e = StartingStack.back(); 510 TheCache.insertResult(e.second, e.first, 511 ValueLatticeElement::getOverdefined()); 512 StartingStack.pop_back(); 513 } 514 BlockValueSet.clear(); 515 BlockValueStack.clear(); 516 return; 517 } 518 std::pair<BasicBlock *, Value *> e = BlockValueStack.back(); 519 assert(BlockValueSet.count(e) && "Stack value should be in BlockValueSet!"); 520 521 if (solveBlockValue(e.second, e.first)) { 522 // The work item was completely processed. 523 assert(BlockValueStack.back() == e && "Nothing should have been pushed!"); 524 #ifndef NDEBUG 525 std::optional<ValueLatticeElement> BBLV = 526 TheCache.getCachedValueInfo(e.second, e.first); 527 assert(BBLV && "Result should be in cache!"); 528 LLVM_DEBUG( 529 dbgs() << "POP " << *e.second << " in " << e.first->getName() << " = " 530 << *BBLV << "\n"); 531 #endif 532 533 BlockValueStack.pop_back(); 534 BlockValueSet.erase(e); 535 } else { 536 // More work needs to be done before revisiting. 537 assert(BlockValueStack.back() != e && "Stack should have been pushed!"); 538 } 539 } 540 } 541 542 std::optional<ValueLatticeElement> 543 LazyValueInfoImpl::getBlockValue(Value *Val, BasicBlock *BB, 544 Instruction *CxtI) { 545 // If already a constant, there is nothing to compute. 546 if (Constant *VC = dyn_cast<Constant>(Val)) 547 return ValueLatticeElement::get(VC); 548 549 if (std::optional<ValueLatticeElement> OptLatticeVal = 550 TheCache.getCachedValueInfo(Val, BB)) { 551 intersectAssumeOrGuardBlockValueConstantRange(Val, *OptLatticeVal, CxtI); 552 return OptLatticeVal; 553 } 554 555 // We have hit a cycle, assume overdefined. 556 if (!pushBlockValue({ BB, Val })) 557 return ValueLatticeElement::getOverdefined(); 558 559 // Yet to be resolved. 560 return std::nullopt; 561 } 562 563 static ValueLatticeElement getFromRangeMetadata(Instruction *BBI) { 564 switch (BBI->getOpcode()) { 565 default: break; 566 case Instruction::Load: 567 case Instruction::Call: 568 case Instruction::Invoke: 569 if (MDNode *Ranges = BBI->getMetadata(LLVMContext::MD_range)) 570 if (isa<IntegerType>(BBI->getType())) { 571 return ValueLatticeElement::getRange( 572 getConstantRangeFromMetadata(*Ranges)); 573 } 574 break; 575 }; 576 // Nothing known - will be intersected with other facts 577 return ValueLatticeElement::getOverdefined(); 578 } 579 580 bool LazyValueInfoImpl::solveBlockValue(Value *Val, BasicBlock *BB) { 581 assert(!isa<Constant>(Val) && "Value should not be constant"); 582 assert(!TheCache.getCachedValueInfo(Val, BB) && 583 "Value should not be in cache"); 584 585 // Hold off inserting this value into the Cache in case we have to return 586 // false and come back later. 587 std::optional<ValueLatticeElement> Res = solveBlockValueImpl(Val, BB); 588 if (!Res) 589 // Work pushed, will revisit 590 return false; 591 592 TheCache.insertResult(Val, BB, *Res); 593 return true; 594 } 595 596 std::optional<ValueLatticeElement> 597 LazyValueInfoImpl::solveBlockValueImpl(Value *Val, BasicBlock *BB) { 598 Instruction *BBI = dyn_cast<Instruction>(Val); 599 if (!BBI || BBI->getParent() != BB) 600 return solveBlockValueNonLocal(Val, BB); 601 602 if (PHINode *PN = dyn_cast<PHINode>(BBI)) 603 return solveBlockValuePHINode(PN, BB); 604 605 if (auto *SI = dyn_cast<SelectInst>(BBI)) 606 return solveBlockValueSelect(SI, BB); 607 608 // If this value is a nonnull pointer, record it's range and bailout. Note 609 // that for all other pointer typed values, we terminate the search at the 610 // definition. We could easily extend this to look through geps, bitcasts, 611 // and the like to prove non-nullness, but it's not clear that's worth it 612 // compile time wise. The context-insensitive value walk done inside 613 // isKnownNonZero gets most of the profitable cases at much less expense. 614 // This does mean that we have a sensitivity to where the defining 615 // instruction is placed, even if it could legally be hoisted much higher. 616 // That is unfortunate. 617 PointerType *PT = dyn_cast<PointerType>(BBI->getType()); 618 if (PT && isKnownNonZero(BBI, DL)) 619 return ValueLatticeElement::getNot(ConstantPointerNull::get(PT)); 620 621 if (BBI->getType()->isIntegerTy()) { 622 if (auto *CI = dyn_cast<CastInst>(BBI)) 623 return solveBlockValueCast(CI, BB); 624 625 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI)) 626 return solveBlockValueBinaryOp(BO, BB); 627 628 if (auto *EVI = dyn_cast<ExtractValueInst>(BBI)) 629 return solveBlockValueExtractValue(EVI, BB); 630 631 if (auto *II = dyn_cast<IntrinsicInst>(BBI)) 632 return solveBlockValueIntrinsic(II, BB); 633 } 634 635 LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName() 636 << "' - unknown inst def found.\n"); 637 return getFromRangeMetadata(BBI); 638 } 639 640 static void AddNonNullPointer(Value *Ptr, NonNullPointerSet &PtrSet) { 641 // TODO: Use NullPointerIsDefined instead. 642 if (Ptr->getType()->getPointerAddressSpace() == 0) 643 PtrSet.insert(getUnderlyingObject(Ptr)); 644 } 645 646 static void AddNonNullPointersByInstruction( 647 Instruction *I, NonNullPointerSet &PtrSet) { 648 if (LoadInst *L = dyn_cast<LoadInst>(I)) { 649 AddNonNullPointer(L->getPointerOperand(), PtrSet); 650 } else if (StoreInst *S = dyn_cast<StoreInst>(I)) { 651 AddNonNullPointer(S->getPointerOperand(), PtrSet); 652 } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) { 653 if (MI->isVolatile()) return; 654 655 // FIXME: check whether it has a valuerange that excludes zero? 656 ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength()); 657 if (!Len || Len->isZero()) return; 658 659 AddNonNullPointer(MI->getRawDest(), PtrSet); 660 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) 661 AddNonNullPointer(MTI->getRawSource(), PtrSet); 662 } 663 } 664 665 bool LazyValueInfoImpl::isNonNullAtEndOfBlock(Value *Val, BasicBlock *BB) { 666 if (NullPointerIsDefined(BB->getParent(), 667 Val->getType()->getPointerAddressSpace())) 668 return false; 669 670 Val = Val->stripInBoundsOffsets(); 671 return TheCache.isNonNullAtEndOfBlock(Val, BB, [](BasicBlock *BB) { 672 NonNullPointerSet NonNullPointers; 673 for (Instruction &I : *BB) 674 AddNonNullPointersByInstruction(&I, NonNullPointers); 675 return NonNullPointers; 676 }); 677 } 678 679 std::optional<ValueLatticeElement> 680 LazyValueInfoImpl::solveBlockValueNonLocal(Value *Val, BasicBlock *BB) { 681 ValueLatticeElement Result; // Start Undefined. 682 683 // If this is the entry block, we must be asking about an argument. The 684 // value is overdefined. 685 if (BB->isEntryBlock()) { 686 assert(isa<Argument>(Val) && "Unknown live-in to the entry block"); 687 return ValueLatticeElement::getOverdefined(); 688 } 689 690 // Loop over all of our predecessors, merging what we know from them into 691 // result. If we encounter an unexplored predecessor, we eagerly explore it 692 // in a depth first manner. In practice, this has the effect of discovering 693 // paths we can't analyze eagerly without spending compile times analyzing 694 // other paths. This heuristic benefits from the fact that predecessors are 695 // frequently arranged such that dominating ones come first and we quickly 696 // find a path to function entry. TODO: We should consider explicitly 697 // canonicalizing to make this true rather than relying on this happy 698 // accident. 699 for (BasicBlock *Pred : predecessors(BB)) { 700 std::optional<ValueLatticeElement> EdgeResult = getEdgeValue(Val, Pred, BB); 701 if (!EdgeResult) 702 // Explore that input, then return here 703 return std::nullopt; 704 705 Result.mergeIn(*EdgeResult); 706 707 // If we hit overdefined, exit early. The BlockVals entry is already set 708 // to overdefined. 709 if (Result.isOverdefined()) { 710 LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName() 711 << "' - overdefined because of pred '" 712 << Pred->getName() << "' (non local).\n"); 713 return Result; 714 } 715 } 716 717 // Return the merged value, which is more precise than 'overdefined'. 718 assert(!Result.isOverdefined()); 719 return Result; 720 } 721 722 std::optional<ValueLatticeElement> 723 LazyValueInfoImpl::solveBlockValuePHINode(PHINode *PN, BasicBlock *BB) { 724 ValueLatticeElement Result; // Start Undefined. 725 726 // Loop over all of our predecessors, merging what we know from them into 727 // result. See the comment about the chosen traversal order in 728 // solveBlockValueNonLocal; the same reasoning applies here. 729 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 730 BasicBlock *PhiBB = PN->getIncomingBlock(i); 731 Value *PhiVal = PN->getIncomingValue(i); 732 // Note that we can provide PN as the context value to getEdgeValue, even 733 // though the results will be cached, because PN is the value being used as 734 // the cache key in the caller. 735 std::optional<ValueLatticeElement> EdgeResult = 736 getEdgeValue(PhiVal, PhiBB, BB, PN); 737 if (!EdgeResult) 738 // Explore that input, then return here 739 return std::nullopt; 740 741 Result.mergeIn(*EdgeResult); 742 743 // If we hit overdefined, exit early. The BlockVals entry is already set 744 // to overdefined. 745 if (Result.isOverdefined()) { 746 LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName() 747 << "' - overdefined because of pred (local).\n"); 748 749 return Result; 750 } 751 } 752 753 // Return the merged value, which is more precise than 'overdefined'. 754 assert(!Result.isOverdefined() && "Possible PHI in entry block?"); 755 return Result; 756 } 757 758 static ValueLatticeElement getValueFromCondition(Value *Val, Value *Cond, 759 bool isTrueDest = true); 760 761 // If we can determine a constraint on the value given conditions assumed by 762 // the program, intersect those constraints with BBLV 763 void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange( 764 Value *Val, ValueLatticeElement &BBLV, Instruction *BBI) { 765 BBI = BBI ? BBI : dyn_cast<Instruction>(Val); 766 if (!BBI) 767 return; 768 769 BasicBlock *BB = BBI->getParent(); 770 for (auto &AssumeVH : AC->assumptionsFor(Val)) { 771 if (!AssumeVH) 772 continue; 773 774 // Only check assumes in the block of the context instruction. Other 775 // assumes will have already been taken into account when the value was 776 // propagated from predecessor blocks. 777 auto *I = cast<CallInst>(AssumeVH); 778 if (I->getParent() != BB || !isValidAssumeForContext(I, BBI)) 779 continue; 780 781 BBLV = intersect(BBLV, getValueFromCondition(Val, I->getArgOperand(0))); 782 } 783 784 // If guards are not used in the module, don't spend time looking for them 785 if (GuardDecl && !GuardDecl->use_empty() && 786 BBI->getIterator() != BB->begin()) { 787 for (Instruction &I : make_range(std::next(BBI->getIterator().getReverse()), 788 BB->rend())) { 789 Value *Cond = nullptr; 790 if (match(&I, m_Intrinsic<Intrinsic::experimental_guard>(m_Value(Cond)))) 791 BBLV = intersect(BBLV, getValueFromCondition(Val, Cond)); 792 } 793 } 794 795 if (BBLV.isOverdefined()) { 796 // Check whether we're checking at the terminator, and the pointer has 797 // been dereferenced in this block. 798 PointerType *PTy = dyn_cast<PointerType>(Val->getType()); 799 if (PTy && BB->getTerminator() == BBI && 800 isNonNullAtEndOfBlock(Val, BB)) 801 BBLV = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy)); 802 } 803 } 804 805 static ConstantRange getConstantRangeOrFull(const ValueLatticeElement &Val, 806 Type *Ty, const DataLayout &DL) { 807 if (Val.isConstantRange(/*UndefAllowed*/ false)) 808 return Val.getConstantRange(); 809 return ConstantRange::getFull(DL.getTypeSizeInBits(Ty)); 810 } 811 812 std::optional<ValueLatticeElement> 813 LazyValueInfoImpl::solveBlockValueSelect(SelectInst *SI, BasicBlock *BB) { 814 // Recurse on our inputs if needed 815 std::optional<ValueLatticeElement> OptTrueVal = 816 getBlockValue(SI->getTrueValue(), BB, SI); 817 if (!OptTrueVal) 818 return std::nullopt; 819 ValueLatticeElement &TrueVal = *OptTrueVal; 820 821 std::optional<ValueLatticeElement> OptFalseVal = 822 getBlockValue(SI->getFalseValue(), BB, SI); 823 if (!OptFalseVal) 824 return std::nullopt; 825 ValueLatticeElement &FalseVal = *OptFalseVal; 826 827 if (TrueVal.isConstantRange() || FalseVal.isConstantRange()) { 828 const ConstantRange &TrueCR = 829 getConstantRangeOrFull(TrueVal, SI->getType(), DL); 830 const ConstantRange &FalseCR = 831 getConstantRangeOrFull(FalseVal, SI->getType(), DL); 832 Value *LHS = nullptr; 833 Value *RHS = nullptr; 834 SelectPatternResult SPR = matchSelectPattern(SI, LHS, RHS); 835 // Is this a min specifically of our two inputs? (Avoid the risk of 836 // ValueTracking getting smarter looking back past our immediate inputs.) 837 if (SelectPatternResult::isMinOrMax(SPR.Flavor) && 838 ((LHS == SI->getTrueValue() && RHS == SI->getFalseValue()) || 839 (RHS == SI->getTrueValue() && LHS == SI->getFalseValue()))) { 840 ConstantRange ResultCR = [&]() { 841 switch (SPR.Flavor) { 842 default: 843 llvm_unreachable("unexpected minmax type!"); 844 case SPF_SMIN: /// Signed minimum 845 return TrueCR.smin(FalseCR); 846 case SPF_UMIN: /// Unsigned minimum 847 return TrueCR.umin(FalseCR); 848 case SPF_SMAX: /// Signed maximum 849 return TrueCR.smax(FalseCR); 850 case SPF_UMAX: /// Unsigned maximum 851 return TrueCR.umax(FalseCR); 852 }; 853 }(); 854 return ValueLatticeElement::getRange( 855 ResultCR, TrueVal.isConstantRangeIncludingUndef() || 856 FalseVal.isConstantRangeIncludingUndef()); 857 } 858 859 if (SPR.Flavor == SPF_ABS) { 860 if (LHS == SI->getTrueValue()) 861 return ValueLatticeElement::getRange( 862 TrueCR.abs(), TrueVal.isConstantRangeIncludingUndef()); 863 if (LHS == SI->getFalseValue()) 864 return ValueLatticeElement::getRange( 865 FalseCR.abs(), FalseVal.isConstantRangeIncludingUndef()); 866 } 867 868 if (SPR.Flavor == SPF_NABS) { 869 ConstantRange Zero(APInt::getZero(TrueCR.getBitWidth())); 870 if (LHS == SI->getTrueValue()) 871 return ValueLatticeElement::getRange( 872 Zero.sub(TrueCR.abs()), FalseVal.isConstantRangeIncludingUndef()); 873 if (LHS == SI->getFalseValue()) 874 return ValueLatticeElement::getRange( 875 Zero.sub(FalseCR.abs()), FalseVal.isConstantRangeIncludingUndef()); 876 } 877 } 878 879 // Can we constrain the facts about the true and false values by using the 880 // condition itself? This shows up with idioms like e.g. select(a > 5, a, 5). 881 // TODO: We could potentially refine an overdefined true value above. 882 Value *Cond = SI->getCondition(); 883 // If the value is undef, a different value may be chosen in 884 // the select condition. 885 if (isGuaranteedNotToBeUndefOrPoison(Cond, AC)) { 886 TrueVal = intersect(TrueVal, 887 getValueFromCondition(SI->getTrueValue(), Cond, true)); 888 FalseVal = intersect( 889 FalseVal, getValueFromCondition(SI->getFalseValue(), Cond, false)); 890 } 891 892 ValueLatticeElement Result = TrueVal; 893 Result.mergeIn(FalseVal); 894 return Result; 895 } 896 897 std::optional<ConstantRange> 898 LazyValueInfoImpl::getRangeFor(Value *V, Instruction *CxtI, BasicBlock *BB) { 899 std::optional<ValueLatticeElement> OptVal = getBlockValue(V, BB, CxtI); 900 if (!OptVal) 901 return std::nullopt; 902 return getConstantRangeOrFull(*OptVal, V->getType(), DL); 903 } 904 905 std::optional<ValueLatticeElement> 906 LazyValueInfoImpl::solveBlockValueCast(CastInst *CI, BasicBlock *BB) { 907 // Without knowing how wide the input is, we can't analyze it in any useful 908 // way. 909 if (!CI->getOperand(0)->getType()->isSized()) 910 return ValueLatticeElement::getOverdefined(); 911 912 // Filter out casts we don't know how to reason about before attempting to 913 // recurse on our operand. This can cut a long search short if we know we're 914 // not going to be able to get any useful information anways. 915 switch (CI->getOpcode()) { 916 case Instruction::Trunc: 917 case Instruction::SExt: 918 case Instruction::ZExt: 919 case Instruction::BitCast: 920 break; 921 default: 922 // Unhandled instructions are overdefined. 923 LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName() 924 << "' - overdefined (unknown cast).\n"); 925 return ValueLatticeElement::getOverdefined(); 926 } 927 928 // Figure out the range of the LHS. If that fails, we still apply the 929 // transfer rule on the full set since we may be able to locally infer 930 // interesting facts. 931 std::optional<ConstantRange> LHSRes = getRangeFor(CI->getOperand(0), CI, BB); 932 if (!LHSRes) 933 // More work to do before applying this transfer rule. 934 return std::nullopt; 935 const ConstantRange &LHSRange = *LHSRes; 936 937 const unsigned ResultBitWidth = CI->getType()->getIntegerBitWidth(); 938 939 // NOTE: We're currently limited by the set of operations that ConstantRange 940 // can evaluate symbolically. Enhancing that set will allows us to analyze 941 // more definitions. 942 return ValueLatticeElement::getRange(LHSRange.castOp(CI->getOpcode(), 943 ResultBitWidth)); 944 } 945 946 std::optional<ValueLatticeElement> 947 LazyValueInfoImpl::solveBlockValueBinaryOpImpl( 948 Instruction *I, BasicBlock *BB, 949 std::function<ConstantRange(const ConstantRange &, const ConstantRange &)> 950 OpFn) { 951 // Figure out the ranges of the operands. If that fails, use a 952 // conservative range, but apply the transfer rule anyways. This 953 // lets us pick up facts from expressions like "and i32 (call i32 954 // @foo()), 32" 955 std::optional<ConstantRange> LHSRes = getRangeFor(I->getOperand(0), I, BB); 956 std::optional<ConstantRange> RHSRes = getRangeFor(I->getOperand(1), I, BB); 957 if (!LHSRes || !RHSRes) 958 // More work to do before applying this transfer rule. 959 return std::nullopt; 960 961 const ConstantRange &LHSRange = *LHSRes; 962 const ConstantRange &RHSRange = *RHSRes; 963 return ValueLatticeElement::getRange(OpFn(LHSRange, RHSRange)); 964 } 965 966 std::optional<ValueLatticeElement> 967 LazyValueInfoImpl::solveBlockValueBinaryOp(BinaryOperator *BO, BasicBlock *BB) { 968 assert(BO->getOperand(0)->getType()->isSized() && 969 "all operands to binary operators are sized"); 970 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(BO)) { 971 unsigned NoWrapKind = 0; 972 if (OBO->hasNoUnsignedWrap()) 973 NoWrapKind |= OverflowingBinaryOperator::NoUnsignedWrap; 974 if (OBO->hasNoSignedWrap()) 975 NoWrapKind |= OverflowingBinaryOperator::NoSignedWrap; 976 977 return solveBlockValueBinaryOpImpl( 978 BO, BB, 979 [BO, NoWrapKind](const ConstantRange &CR1, const ConstantRange &CR2) { 980 return CR1.overflowingBinaryOp(BO->getOpcode(), CR2, NoWrapKind); 981 }); 982 } 983 984 return solveBlockValueBinaryOpImpl( 985 BO, BB, [BO](const ConstantRange &CR1, const ConstantRange &CR2) { 986 return CR1.binaryOp(BO->getOpcode(), CR2); 987 }); 988 } 989 990 std::optional<ValueLatticeElement> 991 LazyValueInfoImpl::solveBlockValueOverflowIntrinsic(WithOverflowInst *WO, 992 BasicBlock *BB) { 993 return solveBlockValueBinaryOpImpl( 994 WO, BB, [WO](const ConstantRange &CR1, const ConstantRange &CR2) { 995 return CR1.binaryOp(WO->getBinaryOp(), CR2); 996 }); 997 } 998 999 std::optional<ValueLatticeElement> 1000 LazyValueInfoImpl::solveBlockValueIntrinsic(IntrinsicInst *II, BasicBlock *BB) { 1001 ValueLatticeElement MetadataVal = getFromRangeMetadata(II); 1002 if (!ConstantRange::isIntrinsicSupported(II->getIntrinsicID())) { 1003 LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName() 1004 << "' - unknown intrinsic.\n"); 1005 return MetadataVal; 1006 } 1007 1008 SmallVector<ConstantRange, 2> OpRanges; 1009 for (Value *Op : II->args()) { 1010 std::optional<ConstantRange> Range = getRangeFor(Op, II, BB); 1011 if (!Range) 1012 return std::nullopt; 1013 OpRanges.push_back(*Range); 1014 } 1015 1016 return intersect(ValueLatticeElement::getRange(ConstantRange::intrinsic( 1017 II->getIntrinsicID(), OpRanges)), 1018 MetadataVal); 1019 } 1020 1021 std::optional<ValueLatticeElement> 1022 LazyValueInfoImpl::solveBlockValueExtractValue(ExtractValueInst *EVI, 1023 BasicBlock *BB) { 1024 if (auto *WO = dyn_cast<WithOverflowInst>(EVI->getAggregateOperand())) 1025 if (EVI->getNumIndices() == 1 && *EVI->idx_begin() == 0) 1026 return solveBlockValueOverflowIntrinsic(WO, BB); 1027 1028 // Handle extractvalue of insertvalue to allow further simplification 1029 // based on replaced with.overflow intrinsics. 1030 if (Value *V = simplifyExtractValueInst( 1031 EVI->getAggregateOperand(), EVI->getIndices(), 1032 EVI->getModule()->getDataLayout())) 1033 return getBlockValue(V, BB, EVI); 1034 1035 LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName() 1036 << "' - overdefined (unknown extractvalue).\n"); 1037 return ValueLatticeElement::getOverdefined(); 1038 } 1039 1040 static bool matchICmpOperand(APInt &Offset, Value *LHS, Value *Val, 1041 ICmpInst::Predicate Pred) { 1042 if (LHS == Val) 1043 return true; 1044 1045 // Handle range checking idiom produced by InstCombine. We will subtract the 1046 // offset from the allowed range for RHS in this case. 1047 const APInt *C; 1048 if (match(LHS, m_Add(m_Specific(Val), m_APInt(C)))) { 1049 Offset = *C; 1050 return true; 1051 } 1052 1053 // Handle the symmetric case. This appears in saturation patterns like 1054 // (x == 16) ? 16 : (x + 1). 1055 if (match(Val, m_Add(m_Specific(LHS), m_APInt(C)))) { 1056 Offset = -*C; 1057 return true; 1058 } 1059 1060 // If (x | y) < C, then (x < C) && (y < C). 1061 if (match(LHS, m_c_Or(m_Specific(Val), m_Value())) && 1062 (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)) 1063 return true; 1064 1065 // If (x & y) > C, then (x > C) && (y > C). 1066 if (match(LHS, m_c_And(m_Specific(Val), m_Value())) && 1067 (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)) 1068 return true; 1069 1070 return false; 1071 } 1072 1073 /// Get value range for a "(Val + Offset) Pred RHS" condition. 1074 static ValueLatticeElement getValueFromSimpleICmpCondition( 1075 CmpInst::Predicate Pred, Value *RHS, const APInt &Offset) { 1076 ConstantRange RHSRange(RHS->getType()->getIntegerBitWidth(), 1077 /*isFullSet=*/true); 1078 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) 1079 RHSRange = ConstantRange(CI->getValue()); 1080 else if (Instruction *I = dyn_cast<Instruction>(RHS)) 1081 if (auto *Ranges = I->getMetadata(LLVMContext::MD_range)) 1082 RHSRange = getConstantRangeFromMetadata(*Ranges); 1083 1084 ConstantRange TrueValues = 1085 ConstantRange::makeAllowedICmpRegion(Pred, RHSRange); 1086 return ValueLatticeElement::getRange(TrueValues.subtract(Offset)); 1087 } 1088 1089 static ValueLatticeElement getValueFromICmpCondition(Value *Val, ICmpInst *ICI, 1090 bool isTrueDest) { 1091 Value *LHS = ICI->getOperand(0); 1092 Value *RHS = ICI->getOperand(1); 1093 1094 // Get the predicate that must hold along the considered edge. 1095 CmpInst::Predicate EdgePred = 1096 isTrueDest ? ICI->getPredicate() : ICI->getInversePredicate(); 1097 1098 if (isa<Constant>(RHS)) { 1099 if (ICI->isEquality() && LHS == Val) { 1100 if (EdgePred == ICmpInst::ICMP_EQ) 1101 return ValueLatticeElement::get(cast<Constant>(RHS)); 1102 else if (!isa<UndefValue>(RHS)) 1103 return ValueLatticeElement::getNot(cast<Constant>(RHS)); 1104 } 1105 } 1106 1107 Type *Ty = Val->getType(); 1108 if (!Ty->isIntegerTy()) 1109 return ValueLatticeElement::getOverdefined(); 1110 1111 unsigned BitWidth = Ty->getScalarSizeInBits(); 1112 APInt Offset(BitWidth, 0); 1113 if (matchICmpOperand(Offset, LHS, Val, EdgePred)) 1114 return getValueFromSimpleICmpCondition(EdgePred, RHS, Offset); 1115 1116 CmpInst::Predicate SwappedPred = CmpInst::getSwappedPredicate(EdgePred); 1117 if (matchICmpOperand(Offset, RHS, Val, SwappedPred)) 1118 return getValueFromSimpleICmpCondition(SwappedPred, LHS, Offset); 1119 1120 const APInt *Mask, *C; 1121 if (match(LHS, m_And(m_Specific(Val), m_APInt(Mask))) && 1122 match(RHS, m_APInt(C))) { 1123 // If (Val & Mask) == C then all the masked bits are known and we can 1124 // compute a value range based on that. 1125 if (EdgePred == ICmpInst::ICMP_EQ) { 1126 KnownBits Known; 1127 Known.Zero = ~*C & *Mask; 1128 Known.One = *C & *Mask; 1129 return ValueLatticeElement::getRange( 1130 ConstantRange::fromKnownBits(Known, /*IsSigned*/ false)); 1131 } 1132 // If (Val & Mask) != 0 then the value must be larger than the lowest set 1133 // bit of Mask. 1134 if (EdgePred == ICmpInst::ICMP_NE && !Mask->isZero() && C->isZero()) { 1135 return ValueLatticeElement::getRange(ConstantRange::getNonEmpty( 1136 APInt::getOneBitSet(BitWidth, Mask->countr_zero()), 1137 APInt::getZero(BitWidth))); 1138 } 1139 } 1140 1141 // If (X urem Modulus) >= C, then X >= C. 1142 // If trunc X >= C, then X >= C. 1143 // TODO: An upper bound could be computed as well. 1144 if (match(LHS, m_CombineOr(m_URem(m_Specific(Val), m_Value()), 1145 m_Trunc(m_Specific(Val)))) && 1146 match(RHS, m_APInt(C))) { 1147 // Use the icmp region so we don't have to deal with different predicates. 1148 ConstantRange CR = ConstantRange::makeExactICmpRegion(EdgePred, *C); 1149 if (!CR.isEmptySet()) 1150 return ValueLatticeElement::getRange(ConstantRange::getNonEmpty( 1151 CR.getUnsignedMin().zext(BitWidth), APInt(BitWidth, 0))); 1152 } 1153 1154 return ValueLatticeElement::getOverdefined(); 1155 } 1156 1157 // Handle conditions of the form 1158 // extractvalue(op.with.overflow(%x, C), 1). 1159 static ValueLatticeElement getValueFromOverflowCondition( 1160 Value *Val, WithOverflowInst *WO, bool IsTrueDest) { 1161 // TODO: This only works with a constant RHS for now. We could also compute 1162 // the range of the RHS, but this doesn't fit into the current structure of 1163 // the edge value calculation. 1164 const APInt *C; 1165 if (WO->getLHS() != Val || !match(WO->getRHS(), m_APInt(C))) 1166 return ValueLatticeElement::getOverdefined(); 1167 1168 // Calculate the possible values of %x for which no overflow occurs. 1169 ConstantRange NWR = ConstantRange::makeExactNoWrapRegion( 1170 WO->getBinaryOp(), *C, WO->getNoWrapKind()); 1171 1172 // If overflow is false, %x is constrained to NWR. If overflow is true, %x is 1173 // constrained to it's inverse (all values that might cause overflow). 1174 if (IsTrueDest) 1175 NWR = NWR.inverse(); 1176 return ValueLatticeElement::getRange(NWR); 1177 } 1178 1179 // Tracks a Value * condition and whether we're interested in it or its inverse 1180 typedef PointerIntPair<Value *, 1, bool> CondValue; 1181 1182 static std::optional<ValueLatticeElement> getValueFromConditionImpl( 1183 Value *Val, CondValue CondVal, bool isRevisit, 1184 SmallDenseMap<CondValue, ValueLatticeElement> &Visited, 1185 SmallVectorImpl<CondValue> &Worklist) { 1186 1187 Value *Cond = CondVal.getPointer(); 1188 bool isTrueDest = CondVal.getInt(); 1189 if (!isRevisit) { 1190 if (ICmpInst *ICI = dyn_cast<ICmpInst>(Cond)) 1191 return getValueFromICmpCondition(Val, ICI, isTrueDest); 1192 1193 if (auto *EVI = dyn_cast<ExtractValueInst>(Cond)) 1194 if (auto *WO = dyn_cast<WithOverflowInst>(EVI->getAggregateOperand())) 1195 if (EVI->getNumIndices() == 1 && *EVI->idx_begin() == 1) 1196 return getValueFromOverflowCondition(Val, WO, isTrueDest); 1197 } 1198 1199 Value *N; 1200 if (match(Cond, m_Not(m_Value(N)))) { 1201 CondValue NKey(N, !isTrueDest); 1202 auto NV = Visited.find(NKey); 1203 if (NV == Visited.end()) { 1204 Worklist.push_back(NKey); 1205 return std::nullopt; 1206 } 1207 return NV->second; 1208 } 1209 1210 Value *L, *R; 1211 bool IsAnd; 1212 if (match(Cond, m_LogicalAnd(m_Value(L), m_Value(R)))) 1213 IsAnd = true; 1214 else if (match(Cond, m_LogicalOr(m_Value(L), m_Value(R)))) 1215 IsAnd = false; 1216 else 1217 return ValueLatticeElement::getOverdefined(); 1218 1219 auto LV = Visited.find(CondValue(L, isTrueDest)); 1220 auto RV = Visited.find(CondValue(R, isTrueDest)); 1221 1222 // if (L && R) -> intersect L and R 1223 // if (!(L || R)) -> intersect !L and !R 1224 // if (L || R) -> union L and R 1225 // if (!(L && R)) -> union !L and !R 1226 if ((isTrueDest ^ IsAnd) && (LV != Visited.end())) { 1227 ValueLatticeElement V = LV->second; 1228 if (V.isOverdefined()) 1229 return V; 1230 if (RV != Visited.end()) { 1231 V.mergeIn(RV->second); 1232 return V; 1233 } 1234 } 1235 1236 if (LV == Visited.end() || RV == Visited.end()) { 1237 assert(!isRevisit); 1238 if (LV == Visited.end()) 1239 Worklist.push_back(CondValue(L, isTrueDest)); 1240 if (RV == Visited.end()) 1241 Worklist.push_back(CondValue(R, isTrueDest)); 1242 return std::nullopt; 1243 } 1244 1245 return intersect(LV->second, RV->second); 1246 } 1247 1248 ValueLatticeElement getValueFromCondition(Value *Val, Value *Cond, 1249 bool isTrueDest) { 1250 assert(Cond && "precondition"); 1251 SmallDenseMap<CondValue, ValueLatticeElement> Visited; 1252 SmallVector<CondValue> Worklist; 1253 1254 CondValue CondKey(Cond, isTrueDest); 1255 Worklist.push_back(CondKey); 1256 do { 1257 CondValue CurrentCond = Worklist.back(); 1258 // Insert an Overdefined placeholder into the set to prevent 1259 // infinite recursion if there exists IRs that use not 1260 // dominated by its def as in this example: 1261 // "%tmp3 = or i1 undef, %tmp4" 1262 // "%tmp4 = or i1 undef, %tmp3" 1263 auto Iter = 1264 Visited.try_emplace(CurrentCond, ValueLatticeElement::getOverdefined()); 1265 bool isRevisit = !Iter.second; 1266 std::optional<ValueLatticeElement> Result = getValueFromConditionImpl( 1267 Val, CurrentCond, isRevisit, Visited, Worklist); 1268 if (Result) { 1269 Visited[CurrentCond] = *Result; 1270 Worklist.pop_back(); 1271 } 1272 } while (!Worklist.empty()); 1273 1274 auto Result = Visited.find(CondKey); 1275 assert(Result != Visited.end()); 1276 return Result->second; 1277 } 1278 1279 // Return true if Usr has Op as an operand, otherwise false. 1280 static bool usesOperand(User *Usr, Value *Op) { 1281 return is_contained(Usr->operands(), Op); 1282 } 1283 1284 // Return true if the instruction type of Val is supported by 1285 // constantFoldUser(). Currently CastInst, BinaryOperator and FreezeInst only. 1286 // Call this before calling constantFoldUser() to find out if it's even worth 1287 // attempting to call it. 1288 static bool isOperationFoldable(User *Usr) { 1289 return isa<CastInst>(Usr) || isa<BinaryOperator>(Usr) || isa<FreezeInst>(Usr); 1290 } 1291 1292 // Check if Usr can be simplified to an integer constant when the value of one 1293 // of its operands Op is an integer constant OpConstVal. If so, return it as an 1294 // lattice value range with a single element or otherwise return an overdefined 1295 // lattice value. 1296 static ValueLatticeElement constantFoldUser(User *Usr, Value *Op, 1297 const APInt &OpConstVal, 1298 const DataLayout &DL) { 1299 assert(isOperationFoldable(Usr) && "Precondition"); 1300 Constant* OpConst = Constant::getIntegerValue(Op->getType(), OpConstVal); 1301 // Check if Usr can be simplified to a constant. 1302 if (auto *CI = dyn_cast<CastInst>(Usr)) { 1303 assert(CI->getOperand(0) == Op && "Operand 0 isn't Op"); 1304 if (auto *C = dyn_cast_or_null<ConstantInt>( 1305 simplifyCastInst(CI->getOpcode(), OpConst, 1306 CI->getDestTy(), DL))) { 1307 return ValueLatticeElement::getRange(ConstantRange(C->getValue())); 1308 } 1309 } else if (auto *BO = dyn_cast<BinaryOperator>(Usr)) { 1310 bool Op0Match = BO->getOperand(0) == Op; 1311 bool Op1Match = BO->getOperand(1) == Op; 1312 assert((Op0Match || Op1Match) && 1313 "Operand 0 nor Operand 1 isn't a match"); 1314 Value *LHS = Op0Match ? OpConst : BO->getOperand(0); 1315 Value *RHS = Op1Match ? OpConst : BO->getOperand(1); 1316 if (auto *C = dyn_cast_or_null<ConstantInt>( 1317 simplifyBinOp(BO->getOpcode(), LHS, RHS, DL))) { 1318 return ValueLatticeElement::getRange(ConstantRange(C->getValue())); 1319 } 1320 } else if (isa<FreezeInst>(Usr)) { 1321 assert(cast<FreezeInst>(Usr)->getOperand(0) == Op && "Operand 0 isn't Op"); 1322 return ValueLatticeElement::getRange(ConstantRange(OpConstVal)); 1323 } 1324 return ValueLatticeElement::getOverdefined(); 1325 } 1326 1327 /// Compute the value of Val on the edge BBFrom -> BBTo. Returns false if 1328 /// Val is not constrained on the edge. Result is unspecified if return value 1329 /// is false. 1330 static std::optional<ValueLatticeElement> getEdgeValueLocal(Value *Val, 1331 BasicBlock *BBFrom, 1332 BasicBlock *BBTo) { 1333 // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we 1334 // know that v != 0. 1335 if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) { 1336 // If this is a conditional branch and only one successor goes to BBTo, then 1337 // we may be able to infer something from the condition. 1338 if (BI->isConditional() && 1339 BI->getSuccessor(0) != BI->getSuccessor(1)) { 1340 bool isTrueDest = BI->getSuccessor(0) == BBTo; 1341 assert(BI->getSuccessor(!isTrueDest) == BBTo && 1342 "BBTo isn't a successor of BBFrom"); 1343 Value *Condition = BI->getCondition(); 1344 1345 // If V is the condition of the branch itself, then we know exactly what 1346 // it is. 1347 if (Condition == Val) 1348 return ValueLatticeElement::get(ConstantInt::get( 1349 Type::getInt1Ty(Val->getContext()), isTrueDest)); 1350 1351 // If the condition of the branch is an equality comparison, we may be 1352 // able to infer the value. 1353 ValueLatticeElement Result = getValueFromCondition(Val, Condition, 1354 isTrueDest); 1355 if (!Result.isOverdefined()) 1356 return Result; 1357 1358 if (User *Usr = dyn_cast<User>(Val)) { 1359 assert(Result.isOverdefined() && "Result isn't overdefined"); 1360 // Check with isOperationFoldable() first to avoid linearly iterating 1361 // over the operands unnecessarily which can be expensive for 1362 // instructions with many operands. 1363 if (isa<IntegerType>(Usr->getType()) && isOperationFoldable(Usr)) { 1364 const DataLayout &DL = BBTo->getModule()->getDataLayout(); 1365 if (usesOperand(Usr, Condition)) { 1366 // If Val has Condition as an operand and Val can be folded into a 1367 // constant with either Condition == true or Condition == false, 1368 // propagate the constant. 1369 // eg. 1370 // ; %Val is true on the edge to %then. 1371 // %Val = and i1 %Condition, true. 1372 // br %Condition, label %then, label %else 1373 APInt ConditionVal(1, isTrueDest ? 1 : 0); 1374 Result = constantFoldUser(Usr, Condition, ConditionVal, DL); 1375 } else { 1376 // If one of Val's operand has an inferred value, we may be able to 1377 // infer the value of Val. 1378 // eg. 1379 // ; %Val is 94 on the edge to %then. 1380 // %Val = add i8 %Op, 1 1381 // %Condition = icmp eq i8 %Op, 93 1382 // br i1 %Condition, label %then, label %else 1383 for (unsigned i = 0; i < Usr->getNumOperands(); ++i) { 1384 Value *Op = Usr->getOperand(i); 1385 ValueLatticeElement OpLatticeVal = 1386 getValueFromCondition(Op, Condition, isTrueDest); 1387 if (std::optional<APInt> OpConst = 1388 OpLatticeVal.asConstantInteger()) { 1389 Result = constantFoldUser(Usr, Op, *OpConst, DL); 1390 break; 1391 } 1392 } 1393 } 1394 } 1395 } 1396 if (!Result.isOverdefined()) 1397 return Result; 1398 } 1399 } 1400 1401 // If the edge was formed by a switch on the value, then we may know exactly 1402 // what it is. 1403 if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) { 1404 Value *Condition = SI->getCondition(); 1405 if (!isa<IntegerType>(Val->getType())) 1406 return std::nullopt; 1407 bool ValUsesConditionAndMayBeFoldable = false; 1408 if (Condition != Val) { 1409 // Check if Val has Condition as an operand. 1410 if (User *Usr = dyn_cast<User>(Val)) 1411 ValUsesConditionAndMayBeFoldable = isOperationFoldable(Usr) && 1412 usesOperand(Usr, Condition); 1413 if (!ValUsesConditionAndMayBeFoldable) 1414 return std::nullopt; 1415 } 1416 assert((Condition == Val || ValUsesConditionAndMayBeFoldable) && 1417 "Condition != Val nor Val doesn't use Condition"); 1418 1419 bool DefaultCase = SI->getDefaultDest() == BBTo; 1420 unsigned BitWidth = Val->getType()->getIntegerBitWidth(); 1421 ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/); 1422 1423 for (auto Case : SI->cases()) { 1424 APInt CaseValue = Case.getCaseValue()->getValue(); 1425 ConstantRange EdgeVal(CaseValue); 1426 if (ValUsesConditionAndMayBeFoldable) { 1427 User *Usr = cast<User>(Val); 1428 const DataLayout &DL = BBTo->getModule()->getDataLayout(); 1429 ValueLatticeElement EdgeLatticeVal = 1430 constantFoldUser(Usr, Condition, CaseValue, DL); 1431 if (EdgeLatticeVal.isOverdefined()) 1432 return std::nullopt; 1433 EdgeVal = EdgeLatticeVal.getConstantRange(); 1434 } 1435 if (DefaultCase) { 1436 // It is possible that the default destination is the destination of 1437 // some cases. We cannot perform difference for those cases. 1438 // We know Condition != CaseValue in BBTo. In some cases we can use 1439 // this to infer Val == f(Condition) is != f(CaseValue). For now, we 1440 // only do this when f is identity (i.e. Val == Condition), but we 1441 // should be able to do this for any injective f. 1442 if (Case.getCaseSuccessor() != BBTo && Condition == Val) 1443 EdgesVals = EdgesVals.difference(EdgeVal); 1444 } else if (Case.getCaseSuccessor() == BBTo) 1445 EdgesVals = EdgesVals.unionWith(EdgeVal); 1446 } 1447 return ValueLatticeElement::getRange(std::move(EdgesVals)); 1448 } 1449 return std::nullopt; 1450 } 1451 1452 /// Compute the value of Val on the edge BBFrom -> BBTo or the value at 1453 /// the basic block if the edge does not constrain Val. 1454 std::optional<ValueLatticeElement> 1455 LazyValueInfoImpl::getEdgeValue(Value *Val, BasicBlock *BBFrom, 1456 BasicBlock *BBTo, Instruction *CxtI) { 1457 // If already a constant, there is nothing to compute. 1458 if (Constant *VC = dyn_cast<Constant>(Val)) 1459 return ValueLatticeElement::get(VC); 1460 1461 ValueLatticeElement LocalResult = 1462 getEdgeValueLocal(Val, BBFrom, BBTo) 1463 .value_or(ValueLatticeElement::getOverdefined()); 1464 if (hasSingleValue(LocalResult)) 1465 // Can't get any more precise here 1466 return LocalResult; 1467 1468 std::optional<ValueLatticeElement> OptInBlock = 1469 getBlockValue(Val, BBFrom, BBFrom->getTerminator()); 1470 if (!OptInBlock) 1471 return std::nullopt; 1472 ValueLatticeElement &InBlock = *OptInBlock; 1473 1474 // We can use the context instruction (generically the ultimate instruction 1475 // the calling pass is trying to simplify) here, even though the result of 1476 // this function is generally cached when called from the solve* functions 1477 // (and that cached result might be used with queries using a different 1478 // context instruction), because when this function is called from the solve* 1479 // functions, the context instruction is not provided. When called from 1480 // LazyValueInfoImpl::getValueOnEdge, the context instruction is provided, 1481 // but then the result is not cached. 1482 intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock, CxtI); 1483 1484 return intersect(LocalResult, InBlock); 1485 } 1486 1487 ValueLatticeElement LazyValueInfoImpl::getValueInBlock(Value *V, BasicBlock *BB, 1488 Instruction *CxtI) { 1489 LLVM_DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '" 1490 << BB->getName() << "'\n"); 1491 1492 assert(BlockValueStack.empty() && BlockValueSet.empty()); 1493 std::optional<ValueLatticeElement> OptResult = getBlockValue(V, BB, CxtI); 1494 if (!OptResult) { 1495 solve(); 1496 OptResult = getBlockValue(V, BB, CxtI); 1497 assert(OptResult && "Value not available after solving"); 1498 } 1499 1500 ValueLatticeElement Result = *OptResult; 1501 LLVM_DEBUG(dbgs() << " Result = " << Result << "\n"); 1502 return Result; 1503 } 1504 1505 ValueLatticeElement LazyValueInfoImpl::getValueAt(Value *V, Instruction *CxtI) { 1506 LLVM_DEBUG(dbgs() << "LVI Getting value " << *V << " at '" << CxtI->getName() 1507 << "'\n"); 1508 1509 if (auto *C = dyn_cast<Constant>(V)) 1510 return ValueLatticeElement::get(C); 1511 1512 ValueLatticeElement Result = ValueLatticeElement::getOverdefined(); 1513 if (auto *I = dyn_cast<Instruction>(V)) 1514 Result = getFromRangeMetadata(I); 1515 intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI); 1516 1517 LLVM_DEBUG(dbgs() << " Result = " << Result << "\n"); 1518 return Result; 1519 } 1520 1521 ValueLatticeElement LazyValueInfoImpl:: 1522 getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB, 1523 Instruction *CxtI) { 1524 LLVM_DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '" 1525 << FromBB->getName() << "' to '" << ToBB->getName() 1526 << "'\n"); 1527 1528 std::optional<ValueLatticeElement> Result = 1529 getEdgeValue(V, FromBB, ToBB, CxtI); 1530 if (!Result) { 1531 solve(); 1532 Result = getEdgeValue(V, FromBB, ToBB, CxtI); 1533 assert(Result && "More work to do after problem solved?"); 1534 } 1535 1536 LLVM_DEBUG(dbgs() << " Result = " << *Result << "\n"); 1537 return *Result; 1538 } 1539 1540 void LazyValueInfoImpl::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, 1541 BasicBlock *NewSucc) { 1542 TheCache.threadEdgeImpl(OldSucc, NewSucc); 1543 } 1544 1545 //===----------------------------------------------------------------------===// 1546 // LazyValueInfo Impl 1547 //===----------------------------------------------------------------------===// 1548 1549 /// This lazily constructs the LazyValueInfoImpl. 1550 static LazyValueInfoImpl &getImpl(void *&PImpl, AssumptionCache *AC, 1551 const Module *M) { 1552 if (!PImpl) { 1553 assert(M && "getCache() called with a null Module"); 1554 const DataLayout &DL = M->getDataLayout(); 1555 Function *GuardDecl = M->getFunction( 1556 Intrinsic::getName(Intrinsic::experimental_guard)); 1557 PImpl = new LazyValueInfoImpl(AC, DL, GuardDecl); 1558 } 1559 return *static_cast<LazyValueInfoImpl*>(PImpl); 1560 } 1561 1562 bool LazyValueInfoWrapperPass::runOnFunction(Function &F) { 1563 Info.AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); 1564 Info.TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F); 1565 1566 if (Info.PImpl) 1567 getImpl(Info.PImpl, Info.AC, F.getParent()).clear(); 1568 1569 // Fully lazy. 1570 return false; 1571 } 1572 1573 void LazyValueInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { 1574 AU.setPreservesAll(); 1575 AU.addRequired<AssumptionCacheTracker>(); 1576 AU.addRequired<TargetLibraryInfoWrapperPass>(); 1577 } 1578 1579 LazyValueInfo &LazyValueInfoWrapperPass::getLVI() { return Info; } 1580 1581 LazyValueInfo::~LazyValueInfo() { releaseMemory(); } 1582 1583 void LazyValueInfo::releaseMemory() { 1584 // If the cache was allocated, free it. 1585 if (PImpl) { 1586 delete &getImpl(PImpl, AC, nullptr); 1587 PImpl = nullptr; 1588 } 1589 } 1590 1591 bool LazyValueInfo::invalidate(Function &F, const PreservedAnalyses &PA, 1592 FunctionAnalysisManager::Invalidator &Inv) { 1593 // We need to invalidate if we have either failed to preserve this analyses 1594 // result directly or if any of its dependencies have been invalidated. 1595 auto PAC = PA.getChecker<LazyValueAnalysis>(); 1596 if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>())) 1597 return true; 1598 1599 return false; 1600 } 1601 1602 void LazyValueInfoWrapperPass::releaseMemory() { Info.releaseMemory(); } 1603 1604 LazyValueInfo LazyValueAnalysis::run(Function &F, 1605 FunctionAnalysisManager &FAM) { 1606 auto &AC = FAM.getResult<AssumptionAnalysis>(F); 1607 auto &TLI = FAM.getResult<TargetLibraryAnalysis>(F); 1608 1609 return LazyValueInfo(&AC, &F.getParent()->getDataLayout(), &TLI); 1610 } 1611 1612 /// Returns true if we can statically tell that this value will never be a 1613 /// "useful" constant. In practice, this means we've got something like an 1614 /// alloca or a malloc call for which a comparison against a constant can 1615 /// only be guarding dead code. Note that we are potentially giving up some 1616 /// precision in dead code (a constant result) in favour of avoiding a 1617 /// expensive search for a easily answered common query. 1618 static bool isKnownNonConstant(Value *V) { 1619 V = V->stripPointerCasts(); 1620 // The return val of alloc cannot be a Constant. 1621 if (isa<AllocaInst>(V)) 1622 return true; 1623 return false; 1624 } 1625 1626 Constant *LazyValueInfo::getConstant(Value *V, Instruction *CxtI) { 1627 // Bail out early if V is known not to be a Constant. 1628 if (isKnownNonConstant(V)) 1629 return nullptr; 1630 1631 BasicBlock *BB = CxtI->getParent(); 1632 ValueLatticeElement Result = 1633 getImpl(PImpl, AC, BB->getModule()).getValueInBlock(V, BB, CxtI); 1634 1635 if (Result.isConstant()) 1636 return Result.getConstant(); 1637 if (Result.isConstantRange()) { 1638 const ConstantRange &CR = Result.getConstantRange(); 1639 if (const APInt *SingleVal = CR.getSingleElement()) 1640 return ConstantInt::get(V->getContext(), *SingleVal); 1641 } 1642 return nullptr; 1643 } 1644 1645 ConstantRange LazyValueInfo::getConstantRange(Value *V, Instruction *CxtI, 1646 bool UndefAllowed) { 1647 assert(V->getType()->isIntegerTy()); 1648 unsigned Width = V->getType()->getIntegerBitWidth(); 1649 BasicBlock *BB = CxtI->getParent(); 1650 ValueLatticeElement Result = 1651 getImpl(PImpl, AC, BB->getModule()).getValueInBlock(V, BB, CxtI); 1652 if (Result.isUnknown()) 1653 return ConstantRange::getEmpty(Width); 1654 if (Result.isConstantRange(UndefAllowed)) 1655 return Result.getConstantRange(UndefAllowed); 1656 // We represent ConstantInt constants as constant ranges but other kinds 1657 // of integer constants, i.e. ConstantExpr will be tagged as constants 1658 assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) && 1659 "ConstantInt value must be represented as constantrange"); 1660 return ConstantRange::getFull(Width); 1661 } 1662 1663 ConstantRange LazyValueInfo::getConstantRangeAtUse(const Use &U, 1664 bool UndefAllowed) { 1665 Value *V = U.get(); 1666 ConstantRange CR = 1667 getConstantRange(V, cast<Instruction>(U.getUser()), UndefAllowed); 1668 1669 // Check whether the only (possibly transitive) use of the value is in a 1670 // position where V can be constrained by a select or branch condition. 1671 const Use *CurrU = &U; 1672 // TODO: Increase limit? 1673 const unsigned MaxUsesToInspect = 3; 1674 for (unsigned I = 0; I < MaxUsesToInspect; ++I) { 1675 std::optional<ValueLatticeElement> CondVal; 1676 auto *CurrI = cast<Instruction>(CurrU->getUser()); 1677 if (auto *SI = dyn_cast<SelectInst>(CurrI)) { 1678 // If the value is undef, a different value may be chosen in 1679 // the select condition and at use. 1680 if (!isGuaranteedNotToBeUndefOrPoison(SI->getCondition(), AC)) 1681 break; 1682 if (CurrU->getOperandNo() == 1) 1683 CondVal = getValueFromCondition(V, SI->getCondition(), true); 1684 else if (CurrU->getOperandNo() == 2) 1685 CondVal = getValueFromCondition(V, SI->getCondition(), false); 1686 } else if (auto *PHI = dyn_cast<PHINode>(CurrI)) { 1687 // TODO: Use non-local query? 1688 CondVal = 1689 getEdgeValueLocal(V, PHI->getIncomingBlock(*CurrU), PHI->getParent()); 1690 } 1691 if (CondVal && CondVal->isConstantRange()) 1692 CR = CR.intersectWith(CondVal->getConstantRange()); 1693 1694 // Only follow one-use chain, to allow direct intersection of conditions. 1695 // If there are multiple uses, we would have to intersect with the union of 1696 // all conditions at different uses. 1697 // Stop walking if we hit a non-speculatable instruction. Even if the 1698 // result is only used under a specific condition, executing the 1699 // instruction itself may cause side effects or UB already. 1700 // This also disallows looking through phi nodes: If the phi node is part 1701 // of a cycle, we might end up reasoning about values from different cycle 1702 // iterations (PR60629). 1703 if (!CurrI->hasOneUse() || !isSafeToSpeculativelyExecute(CurrI)) 1704 break; 1705 CurrU = &*CurrI->use_begin(); 1706 } 1707 return CR; 1708 } 1709 1710 /// Determine whether the specified value is known to be a 1711 /// constant on the specified edge. Return null if not. 1712 Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB, 1713 BasicBlock *ToBB, 1714 Instruction *CxtI) { 1715 Module *M = FromBB->getModule(); 1716 ValueLatticeElement Result = 1717 getImpl(PImpl, AC, M).getValueOnEdge(V, FromBB, ToBB, CxtI); 1718 1719 if (Result.isConstant()) 1720 return Result.getConstant(); 1721 if (Result.isConstantRange()) { 1722 const ConstantRange &CR = Result.getConstantRange(); 1723 if (const APInt *SingleVal = CR.getSingleElement()) 1724 return ConstantInt::get(V->getContext(), *SingleVal); 1725 } 1726 return nullptr; 1727 } 1728 1729 ConstantRange LazyValueInfo::getConstantRangeOnEdge(Value *V, 1730 BasicBlock *FromBB, 1731 BasicBlock *ToBB, 1732 Instruction *CxtI) { 1733 unsigned Width = V->getType()->getIntegerBitWidth(); 1734 Module *M = FromBB->getModule(); 1735 ValueLatticeElement Result = 1736 getImpl(PImpl, AC, M).getValueOnEdge(V, FromBB, ToBB, CxtI); 1737 1738 if (Result.isUnknown()) 1739 return ConstantRange::getEmpty(Width); 1740 if (Result.isConstantRange()) 1741 return Result.getConstantRange(); 1742 // We represent ConstantInt constants as constant ranges but other kinds 1743 // of integer constants, i.e. ConstantExpr will be tagged as constants 1744 assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) && 1745 "ConstantInt value must be represented as constantrange"); 1746 return ConstantRange::getFull(Width); 1747 } 1748 1749 static LazyValueInfo::Tristate 1750 getPredicateResult(unsigned Pred, Constant *C, const ValueLatticeElement &Val, 1751 const DataLayout &DL, TargetLibraryInfo *TLI) { 1752 // If we know the value is a constant, evaluate the conditional. 1753 Constant *Res = nullptr; 1754 if (Val.isConstant()) { 1755 Res = ConstantFoldCompareInstOperands(Pred, Val.getConstant(), C, DL, TLI); 1756 if (ConstantInt *ResCI = dyn_cast_or_null<ConstantInt>(Res)) 1757 return ResCI->isZero() ? LazyValueInfo::False : LazyValueInfo::True; 1758 return LazyValueInfo::Unknown; 1759 } 1760 1761 if (Val.isConstantRange()) { 1762 ConstantInt *CI = dyn_cast<ConstantInt>(C); 1763 if (!CI) return LazyValueInfo::Unknown; 1764 1765 const ConstantRange &CR = Val.getConstantRange(); 1766 if (Pred == ICmpInst::ICMP_EQ) { 1767 if (!CR.contains(CI->getValue())) 1768 return LazyValueInfo::False; 1769 1770 if (CR.isSingleElement()) 1771 return LazyValueInfo::True; 1772 } else if (Pred == ICmpInst::ICMP_NE) { 1773 if (!CR.contains(CI->getValue())) 1774 return LazyValueInfo::True; 1775 1776 if (CR.isSingleElement()) 1777 return LazyValueInfo::False; 1778 } else { 1779 // Handle more complex predicates. 1780 ConstantRange TrueValues = ConstantRange::makeExactICmpRegion( 1781 (ICmpInst::Predicate)Pred, CI->getValue()); 1782 if (TrueValues.contains(CR)) 1783 return LazyValueInfo::True; 1784 if (TrueValues.inverse().contains(CR)) 1785 return LazyValueInfo::False; 1786 } 1787 return LazyValueInfo::Unknown; 1788 } 1789 1790 if (Val.isNotConstant()) { 1791 // If this is an equality comparison, we can try to fold it knowing that 1792 // "V != C1". 1793 if (Pred == ICmpInst::ICMP_EQ) { 1794 // !C1 == C -> false iff C1 == C. 1795 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE, 1796 Val.getNotConstant(), C, DL, 1797 TLI); 1798 if (Res && Res->isNullValue()) 1799 return LazyValueInfo::False; 1800 } else if (Pred == ICmpInst::ICMP_NE) { 1801 // !C1 != C -> true iff C1 == C. 1802 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE, 1803 Val.getNotConstant(), C, DL, 1804 TLI); 1805 if (Res && Res->isNullValue()) 1806 return LazyValueInfo::True; 1807 } 1808 return LazyValueInfo::Unknown; 1809 } 1810 1811 return LazyValueInfo::Unknown; 1812 } 1813 1814 /// Determine whether the specified value comparison with a constant is known to 1815 /// be true or false on the specified CFG edge. Pred is a CmpInst predicate. 1816 LazyValueInfo::Tristate 1817 LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C, 1818 BasicBlock *FromBB, BasicBlock *ToBB, 1819 Instruction *CxtI) { 1820 Module *M = FromBB->getModule(); 1821 ValueLatticeElement Result = 1822 getImpl(PImpl, AC, M).getValueOnEdge(V, FromBB, ToBB, CxtI); 1823 1824 return getPredicateResult(Pred, C, Result, M->getDataLayout(), TLI); 1825 } 1826 1827 LazyValueInfo::Tristate 1828 LazyValueInfo::getPredicateAt(unsigned Pred, Value *V, Constant *C, 1829 Instruction *CxtI, bool UseBlockValue) { 1830 // Is or is not NonNull are common predicates being queried. If 1831 // isKnownNonZero can tell us the result of the predicate, we can 1832 // return it quickly. But this is only a fastpath, and falling 1833 // through would still be correct. 1834 Module *M = CxtI->getModule(); 1835 const DataLayout &DL = M->getDataLayout(); 1836 if (V->getType()->isPointerTy() && C->isNullValue() && 1837 isKnownNonZero(V->stripPointerCastsSameRepresentation(), DL)) { 1838 if (Pred == ICmpInst::ICMP_EQ) 1839 return LazyValueInfo::False; 1840 else if (Pred == ICmpInst::ICMP_NE) 1841 return LazyValueInfo::True; 1842 } 1843 1844 ValueLatticeElement Result = UseBlockValue 1845 ? getImpl(PImpl, AC, M).getValueInBlock(V, CxtI->getParent(), CxtI) 1846 : getImpl(PImpl, AC, M).getValueAt(V, CxtI); 1847 Tristate Ret = getPredicateResult(Pred, C, Result, DL, TLI); 1848 if (Ret != Unknown) 1849 return Ret; 1850 1851 // Note: The following bit of code is somewhat distinct from the rest of LVI; 1852 // LVI as a whole tries to compute a lattice value which is conservatively 1853 // correct at a given location. In this case, we have a predicate which we 1854 // weren't able to prove about the merged result, and we're pushing that 1855 // predicate back along each incoming edge to see if we can prove it 1856 // separately for each input. As a motivating example, consider: 1857 // bb1: 1858 // %v1 = ... ; constantrange<1, 5> 1859 // br label %merge 1860 // bb2: 1861 // %v2 = ... ; constantrange<10, 20> 1862 // br label %merge 1863 // merge: 1864 // %phi = phi [%v1, %v2] ; constantrange<1,20> 1865 // %pred = icmp eq i32 %phi, 8 1866 // We can't tell from the lattice value for '%phi' that '%pred' is false 1867 // along each path, but by checking the predicate over each input separately, 1868 // we can. 1869 // We limit the search to one step backwards from the current BB and value. 1870 // We could consider extending this to search further backwards through the 1871 // CFG and/or value graph, but there are non-obvious compile time vs quality 1872 // tradeoffs. 1873 BasicBlock *BB = CxtI->getParent(); 1874 1875 // Function entry or an unreachable block. Bail to avoid confusing 1876 // analysis below. 1877 pred_iterator PI = pred_begin(BB), PE = pred_end(BB); 1878 if (PI == PE) 1879 return Unknown; 1880 1881 // If V is a PHI node in the same block as the context, we need to ask 1882 // questions about the predicate as applied to the incoming value along 1883 // each edge. This is useful for eliminating cases where the predicate is 1884 // known along all incoming edges. 1885 if (auto *PHI = dyn_cast<PHINode>(V)) 1886 if (PHI->getParent() == BB) { 1887 Tristate Baseline = Unknown; 1888 for (unsigned i = 0, e = PHI->getNumIncomingValues(); i < e; i++) { 1889 Value *Incoming = PHI->getIncomingValue(i); 1890 BasicBlock *PredBB = PHI->getIncomingBlock(i); 1891 // Note that PredBB may be BB itself. 1892 Tristate Result = 1893 getPredicateOnEdge(Pred, Incoming, C, PredBB, BB, CxtI); 1894 1895 // Keep going as long as we've seen a consistent known result for 1896 // all inputs. 1897 Baseline = (i == 0) ? Result /* First iteration */ 1898 : (Baseline == Result ? Baseline 1899 : Unknown); /* All others */ 1900 if (Baseline == Unknown) 1901 break; 1902 } 1903 if (Baseline != Unknown) 1904 return Baseline; 1905 } 1906 1907 // For a comparison where the V is outside this block, it's possible 1908 // that we've branched on it before. Look to see if the value is known 1909 // on all incoming edges. 1910 if (!isa<Instruction>(V) || cast<Instruction>(V)->getParent() != BB) { 1911 // For predecessor edge, determine if the comparison is true or false 1912 // on that edge. If they're all true or all false, we can conclude 1913 // the value of the comparison in this block. 1914 Tristate Baseline = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI); 1915 if (Baseline != Unknown) { 1916 // Check that all remaining incoming values match the first one. 1917 while (++PI != PE) { 1918 Tristate Ret = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI); 1919 if (Ret != Baseline) 1920 break; 1921 } 1922 // If we terminated early, then one of the values didn't match. 1923 if (PI == PE) { 1924 return Baseline; 1925 } 1926 } 1927 } 1928 1929 return Unknown; 1930 } 1931 1932 LazyValueInfo::Tristate LazyValueInfo::getPredicateAt(unsigned P, Value *LHS, 1933 Value *RHS, 1934 Instruction *CxtI, 1935 bool UseBlockValue) { 1936 CmpInst::Predicate Pred = (CmpInst::Predicate)P; 1937 1938 if (auto *C = dyn_cast<Constant>(RHS)) 1939 return getPredicateAt(P, LHS, C, CxtI, UseBlockValue); 1940 if (auto *C = dyn_cast<Constant>(LHS)) 1941 return getPredicateAt(CmpInst::getSwappedPredicate(Pred), RHS, C, CxtI, 1942 UseBlockValue); 1943 1944 // Got two non-Constant values. Try to determine the comparison results based 1945 // on the block values of the two operands, e.g. because they have 1946 // non-overlapping ranges. 1947 if (UseBlockValue) { 1948 Module *M = CxtI->getModule(); 1949 ValueLatticeElement L = 1950 getImpl(PImpl, AC, M).getValueInBlock(LHS, CxtI->getParent(), CxtI); 1951 if (L.isOverdefined()) 1952 return LazyValueInfo::Unknown; 1953 1954 ValueLatticeElement R = 1955 getImpl(PImpl, AC, M).getValueInBlock(RHS, CxtI->getParent(), CxtI); 1956 Type *Ty = CmpInst::makeCmpResultType(LHS->getType()); 1957 if (Constant *Res = L.getCompare((CmpInst::Predicate)P, Ty, R, 1958 M->getDataLayout())) { 1959 if (Res->isNullValue()) 1960 return LazyValueInfo::False; 1961 if (Res->isOneValue()) 1962 return LazyValueInfo::True; 1963 } 1964 } 1965 return LazyValueInfo::Unknown; 1966 } 1967 1968 void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, 1969 BasicBlock *NewSucc) { 1970 if (PImpl) { 1971 getImpl(PImpl, AC, PredBB->getModule()) 1972 .threadEdge(PredBB, OldSucc, NewSucc); 1973 } 1974 } 1975 1976 void LazyValueInfo::forgetValue(Value *V) { 1977 if (PImpl) 1978 getImpl(PImpl, AC, nullptr).forgetValue(V); 1979 } 1980 1981 void LazyValueInfo::eraseBlock(BasicBlock *BB) { 1982 if (PImpl) { 1983 getImpl(PImpl, AC, BB->getModule()).eraseBlock(BB); 1984 } 1985 } 1986 1987 void LazyValueInfo::clear(const Module *M) { 1988 if (PImpl) { 1989 getImpl(PImpl, AC, M).clear(); 1990 } 1991 } 1992 1993 void LazyValueInfo::printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS) { 1994 if (PImpl) { 1995 getImpl(PImpl, AC, F.getParent()).printLVI(F, DTree, OS); 1996 } 1997 } 1998 1999 // Print the LVI for the function arguments at the start of each basic block. 2000 void LazyValueInfoAnnotatedWriter::emitBasicBlockStartAnnot( 2001 const BasicBlock *BB, formatted_raw_ostream &OS) { 2002 // Find if there are latticevalues defined for arguments of the function. 2003 auto *F = BB->getParent(); 2004 for (const auto &Arg : F->args()) { 2005 ValueLatticeElement Result = LVIImpl->getValueInBlock( 2006 const_cast<Argument *>(&Arg), const_cast<BasicBlock *>(BB)); 2007 if (Result.isUnknown()) 2008 continue; 2009 OS << "; LatticeVal for: '" << Arg << "' is: " << Result << "\n"; 2010 } 2011 } 2012 2013 // This function prints the LVI analysis for the instruction I at the beginning 2014 // of various basic blocks. It relies on calculated values that are stored in 2015 // the LazyValueInfoCache, and in the absence of cached values, recalculate the 2016 // LazyValueInfo for `I`, and print that info. 2017 void LazyValueInfoAnnotatedWriter::emitInstructionAnnot( 2018 const Instruction *I, formatted_raw_ostream &OS) { 2019 2020 auto *ParentBB = I->getParent(); 2021 SmallPtrSet<const BasicBlock*, 16> BlocksContainingLVI; 2022 // We can generate (solve) LVI values only for blocks that are dominated by 2023 // the I's parent. However, to avoid generating LVI for all dominating blocks, 2024 // that contain redundant/uninteresting information, we print LVI for 2025 // blocks that may use this LVI information (such as immediate successor 2026 // blocks, and blocks that contain uses of `I`). 2027 auto printResult = [&](const BasicBlock *BB) { 2028 if (!BlocksContainingLVI.insert(BB).second) 2029 return; 2030 ValueLatticeElement Result = LVIImpl->getValueInBlock( 2031 const_cast<Instruction *>(I), const_cast<BasicBlock *>(BB)); 2032 OS << "; LatticeVal for: '" << *I << "' in BB: '"; 2033 BB->printAsOperand(OS, false); 2034 OS << "' is: " << Result << "\n"; 2035 }; 2036 2037 printResult(ParentBB); 2038 // Print the LVI analysis results for the immediate successor blocks, that 2039 // are dominated by `ParentBB`. 2040 for (const auto *BBSucc : successors(ParentBB)) 2041 if (DT.dominates(ParentBB, BBSucc)) 2042 printResult(BBSucc); 2043 2044 // Print LVI in blocks where `I` is used. 2045 for (const auto *U : I->users()) 2046 if (auto *UseI = dyn_cast<Instruction>(U)) 2047 if (!isa<PHINode>(UseI) || DT.dominates(ParentBB, UseI->getParent())) 2048 printResult(UseI->getParent()); 2049 2050 } 2051 2052 namespace { 2053 // Printer class for LazyValueInfo results. 2054 class LazyValueInfoPrinter : public FunctionPass { 2055 public: 2056 static char ID; // Pass identification, replacement for typeid 2057 LazyValueInfoPrinter() : FunctionPass(ID) { 2058 initializeLazyValueInfoPrinterPass(*PassRegistry::getPassRegistry()); 2059 } 2060 2061 void getAnalysisUsage(AnalysisUsage &AU) const override { 2062 AU.setPreservesAll(); 2063 AU.addRequired<LazyValueInfoWrapperPass>(); 2064 AU.addRequired<DominatorTreeWrapperPass>(); 2065 } 2066 2067 // Get the mandatory dominator tree analysis and pass this in to the 2068 // LVIPrinter. We cannot rely on the LVI's DT, since it's optional. 2069 bool runOnFunction(Function &F) override { 2070 dbgs() << "LVI for function '" << F.getName() << "':\n"; 2071 auto &LVI = getAnalysis<LazyValueInfoWrapperPass>().getLVI(); 2072 auto &DTree = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 2073 LVI.printLVI(F, DTree, dbgs()); 2074 return false; 2075 } 2076 }; 2077 } 2078 2079 char LazyValueInfoPrinter::ID = 0; 2080 INITIALIZE_PASS_BEGIN(LazyValueInfoPrinter, "print-lazy-value-info", 2081 "Lazy Value Info Printer Pass", false, false) 2082 INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass) 2083 INITIALIZE_PASS_END(LazyValueInfoPrinter, "print-lazy-value-info", 2084 "Lazy Value Info Printer Pass", false, false) 2085