1 //===- LazyValueInfo.cpp - Value constraint analysis ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the interface for lazy computation of value constraint
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/LazyValueInfo.h"
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/Analysis/ConstantFolding.h"
19 #include "llvm/Analysis/ValueTracking.h"
20 #include "llvm/IR/CFG.h"
21 #include "llvm/IR/ConstantRange.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/IR/PatternMatch.h"
27 #include "llvm/IR/ValueHandle.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/Target/TargetLibraryInfo.h"
34 using namespace PatternMatch;
36 #define DEBUG_TYPE "lazy-value-info"
38 char LazyValueInfo::ID = 0;
39 INITIALIZE_PASS_BEGIN(LazyValueInfo, "lazy-value-info",
40 "Lazy Value Information Analysis", false, true)
41 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
42 INITIALIZE_PASS_END(LazyValueInfo, "lazy-value-info",
43 "Lazy Value Information Analysis", false, true)
46 FunctionPass *createLazyValueInfoPass() { return new LazyValueInfo(); }
50 //===----------------------------------------------------------------------===//
52 //===----------------------------------------------------------------------===//
54 /// LVILatticeVal - This is the information tracked by LazyValueInfo for each
57 /// FIXME: This is basically just for bringup, this can be made a lot more rich
63 /// undefined - This Value has no known value yet.
66 /// constant - This Value has a specific constant value.
68 /// notconstant - This Value is known to not have the specified value.
71 /// constantrange - The Value falls within this range.
74 /// overdefined - This value is not known to be constant, and we know that
79 /// Val: This stores the current lattice value along with the Constant* for
80 /// the constant if this is a 'constant' or 'notconstant' value.
86 LVILatticeVal() : Tag(undefined), Val(nullptr), Range(1, true) {}
88 static LVILatticeVal get(Constant *C) {
90 if (!isa<UndefValue>(C))
94 static LVILatticeVal getNot(Constant *C) {
96 if (!isa<UndefValue>(C))
97 Res.markNotConstant(C);
100 static LVILatticeVal getRange(ConstantRange CR) {
102 Res.markConstantRange(CR);
106 bool isUndefined() const { return Tag == undefined; }
107 bool isConstant() const { return Tag == constant; }
108 bool isNotConstant() const { return Tag == notconstant; }
109 bool isConstantRange() const { return Tag == constantrange; }
110 bool isOverdefined() const { return Tag == overdefined; }
112 Constant *getConstant() const {
113 assert(isConstant() && "Cannot get the constant of a non-constant!");
117 Constant *getNotConstant() const {
118 assert(isNotConstant() && "Cannot get the constant of a non-notconstant!");
122 ConstantRange getConstantRange() const {
123 assert(isConstantRange() &&
124 "Cannot get the constant-range of a non-constant-range!");
128 /// markOverdefined - Return true if this is a change in status.
129 bool markOverdefined() {
136 /// markConstant - Return true if this is a change in status.
137 bool markConstant(Constant *V) {
138 assert(V && "Marking constant with NULL");
139 if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
140 return markConstantRange(ConstantRange(CI->getValue()));
141 if (isa<UndefValue>(V))
144 assert((!isConstant() || getConstant() == V) &&
145 "Marking constant with different value");
146 assert(isUndefined());
152 /// markNotConstant - Return true if this is a change in status.
153 bool markNotConstant(Constant *V) {
154 assert(V && "Marking constant with NULL");
155 if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
156 return markConstantRange(ConstantRange(CI->getValue()+1, CI->getValue()));
157 if (isa<UndefValue>(V))
160 assert((!isConstant() || getConstant() != V) &&
161 "Marking constant !constant with same value");
162 assert((!isNotConstant() || getNotConstant() == V) &&
163 "Marking !constant with different value");
164 assert(isUndefined() || isConstant());
170 /// markConstantRange - Return true if this is a change in status.
171 bool markConstantRange(const ConstantRange NewR) {
172 if (isConstantRange()) {
173 if (NewR.isEmptySet())
174 return markOverdefined();
176 bool changed = Range != NewR;
181 assert(isUndefined());
182 if (NewR.isEmptySet())
183 return markOverdefined();
190 /// mergeIn - Merge the specified lattice value into this one, updating this
191 /// one and returning true if anything changed.
192 bool mergeIn(const LVILatticeVal &RHS) {
193 if (RHS.isUndefined() || isOverdefined()) return false;
194 if (RHS.isOverdefined()) return markOverdefined();
204 if (RHS.isConstant()) {
207 return markOverdefined();
210 if (RHS.isNotConstant()) {
212 return markOverdefined();
214 // Unless we can prove that the two Constants are different, we must
215 // move to overdefined.
216 // FIXME: use DataLayout/TargetLibraryInfo for smarter constant folding.
217 if (ConstantInt *Res = dyn_cast<ConstantInt>(
218 ConstantFoldCompareInstOperands(CmpInst::ICMP_NE,
220 RHS.getNotConstant())))
222 return markNotConstant(RHS.getNotConstant());
224 return markOverdefined();
227 // RHS is a ConstantRange, LHS is a non-integer Constant.
229 // FIXME: consider the case where RHS is a range [1, 0) and LHS is
230 // a function. The correct result is to pick up RHS.
232 return markOverdefined();
235 if (isNotConstant()) {
236 if (RHS.isConstant()) {
238 return markOverdefined();
240 // Unless we can prove that the two Constants are different, we must
241 // move to overdefined.
242 // FIXME: use DataLayout/TargetLibraryInfo for smarter constant folding.
243 if (ConstantInt *Res = dyn_cast<ConstantInt>(
244 ConstantFoldCompareInstOperands(CmpInst::ICMP_NE,
250 return markOverdefined();
253 if (RHS.isNotConstant()) {
256 return markOverdefined();
259 return markOverdefined();
262 assert(isConstantRange() && "New LVILattice type?");
263 if (!RHS.isConstantRange())
264 return markOverdefined();
266 ConstantRange NewR = Range.unionWith(RHS.getConstantRange());
267 if (NewR.isFullSet())
268 return markOverdefined();
269 return markConstantRange(NewR);
273 } // end anonymous namespace.
276 raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val)
278 raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) {
279 if (Val.isUndefined())
280 return OS << "undefined";
281 if (Val.isOverdefined())
282 return OS << "overdefined";
284 if (Val.isNotConstant())
285 return OS << "notconstant<" << *Val.getNotConstant() << '>';
286 else if (Val.isConstantRange())
287 return OS << "constantrange<" << Val.getConstantRange().getLower() << ", "
288 << Val.getConstantRange().getUpper() << '>';
289 return OS << "constant<" << *Val.getConstant() << '>';
293 //===----------------------------------------------------------------------===//
294 // LazyValueInfoCache Decl
295 //===----------------------------------------------------------------------===//
298 /// LVIValueHandle - A callback value handle updates the cache when
299 /// values are erased.
300 class LazyValueInfoCache;
301 struct LVIValueHandle : public CallbackVH {
302 LazyValueInfoCache *Parent;
304 LVIValueHandle(Value *V, LazyValueInfoCache *P)
305 : CallbackVH(V), Parent(P) { }
307 void deleted() override;
308 void allUsesReplacedWith(Value *V) override {
315 /// LazyValueInfoCache - This is the cache kept by LazyValueInfo which
316 /// maintains information about queries across the clients' queries.
317 class LazyValueInfoCache {
318 /// ValueCacheEntryTy - This is all of the cached block information for
319 /// exactly one Value*. The entries are sorted by the BasicBlock* of the
320 /// entries, allowing us to do a lookup with a binary search.
321 typedef std::map<AssertingVH<BasicBlock>, LVILatticeVal> ValueCacheEntryTy;
323 /// ValueCache - This is all of the cached information for all values,
324 /// mapped from Value* to key information.
325 std::map<LVIValueHandle, ValueCacheEntryTy> ValueCache;
327 /// OverDefinedCache - This tracks, on a per-block basis, the set of
328 /// values that are over-defined at the end of that block. This is required
329 /// for cache updating.
330 typedef std::pair<AssertingVH<BasicBlock>, Value*> OverDefinedPairTy;
331 DenseSet<OverDefinedPairTy> OverDefinedCache;
333 /// SeenBlocks - Keep track of all blocks that we have ever seen, so we
334 /// don't spend time removing unused blocks from our caches.
335 DenseSet<AssertingVH<BasicBlock> > SeenBlocks;
337 /// BlockValueStack - This stack holds the state of the value solver
338 /// during a query. It basically emulates the callstack of the naive
339 /// recursive value lookup process.
340 std::stack<std::pair<BasicBlock*, Value*> > BlockValueStack;
342 friend struct LVIValueHandle;
344 /// OverDefinedCacheUpdater - A helper object that ensures that the
345 /// OverDefinedCache is updated whenever solveBlockValue returns.
346 struct OverDefinedCacheUpdater {
347 LazyValueInfoCache *Parent;
352 OverDefinedCacheUpdater(Value *V, BasicBlock *B, LVILatticeVal &LV,
353 LazyValueInfoCache *P)
354 : Parent(P), Val(V), BB(B), BBLV(LV) { }
356 bool markResult(bool changed) {
357 if (changed && BBLV.isOverdefined())
358 Parent->OverDefinedCache.insert(std::make_pair(BB, Val));
365 LVILatticeVal getBlockValue(Value *Val, BasicBlock *BB);
366 bool getEdgeValue(Value *V, BasicBlock *F, BasicBlock *T,
367 LVILatticeVal &Result);
368 bool hasBlockValue(Value *Val, BasicBlock *BB);
370 // These methods process one work item and may add more. A false value
371 // returned means that the work item was not completely processed and must
372 // be revisited after going through the new items.
373 bool solveBlockValue(Value *Val, BasicBlock *BB);
374 bool solveBlockValueNonLocal(LVILatticeVal &BBLV,
375 Value *Val, BasicBlock *BB);
376 bool solveBlockValuePHINode(LVILatticeVal &BBLV,
377 PHINode *PN, BasicBlock *BB);
378 bool solveBlockValueConstantRange(LVILatticeVal &BBLV,
379 Instruction *BBI, BasicBlock *BB);
383 ValueCacheEntryTy &lookup(Value *V) {
384 return ValueCache[LVIValueHandle(V, this)];
388 /// getValueInBlock - This is the query interface to determine the lattice
389 /// value for the specified Value* at the end of the specified block.
390 LVILatticeVal getValueInBlock(Value *V, BasicBlock *BB);
392 /// getValueOnEdge - This is the query interface to determine the lattice
393 /// value for the specified Value* that is true on the specified edge.
394 LVILatticeVal getValueOnEdge(Value *V, BasicBlock *FromBB,BasicBlock *ToBB);
396 /// threadEdge - This is the update interface to inform the cache that an
397 /// edge from PredBB to OldSucc has been threaded to be from PredBB to
399 void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
401 /// eraseBlock - This is part of the update interface to inform the cache
402 /// that a block has been deleted.
403 void eraseBlock(BasicBlock *BB);
405 /// clear - Empty the cache.
409 OverDefinedCache.clear();
412 } // end anonymous namespace
414 void LVIValueHandle::deleted() {
415 typedef std::pair<AssertingVH<BasicBlock>, Value*> OverDefinedPairTy;
417 SmallVector<OverDefinedPairTy, 4> ToErase;
418 for (DenseSet<OverDefinedPairTy>::iterator
419 I = Parent->OverDefinedCache.begin(),
420 E = Parent->OverDefinedCache.end();
422 if (I->second == getValPtr())
423 ToErase.push_back(*I);
426 for (SmallVectorImpl<OverDefinedPairTy>::iterator I = ToErase.begin(),
427 E = ToErase.end(); I != E; ++I)
428 Parent->OverDefinedCache.erase(*I);
430 // This erasure deallocates *this, so it MUST happen after we're done
431 // using any and all members of *this.
432 Parent->ValueCache.erase(*this);
435 void LazyValueInfoCache::eraseBlock(BasicBlock *BB) {
436 // Shortcut if we have never seen this block.
437 DenseSet<AssertingVH<BasicBlock> >::iterator I = SeenBlocks.find(BB);
438 if (I == SeenBlocks.end())
442 SmallVector<OverDefinedPairTy, 4> ToErase;
443 for (DenseSet<OverDefinedPairTy>::iterator I = OverDefinedCache.begin(),
444 E = OverDefinedCache.end(); I != E; ++I) {
446 ToErase.push_back(*I);
449 for (SmallVectorImpl<OverDefinedPairTy>::iterator I = ToErase.begin(),
450 E = ToErase.end(); I != E; ++I)
451 OverDefinedCache.erase(*I);
453 for (std::map<LVIValueHandle, ValueCacheEntryTy>::iterator
454 I = ValueCache.begin(), E = ValueCache.end(); I != E; ++I)
458 void LazyValueInfoCache::solve() {
459 while (!BlockValueStack.empty()) {
460 std::pair<BasicBlock*, Value*> &e = BlockValueStack.top();
461 if (solveBlockValue(e.second, e.first)) {
462 assert(BlockValueStack.top() == e);
463 BlockValueStack.pop();
468 bool LazyValueInfoCache::hasBlockValue(Value *Val, BasicBlock *BB) {
469 // If already a constant, there is nothing to compute.
470 if (isa<Constant>(Val))
473 LVIValueHandle ValHandle(Val, this);
474 std::map<LVIValueHandle, ValueCacheEntryTy>::iterator I =
475 ValueCache.find(ValHandle);
476 if (I == ValueCache.end()) return false;
477 return I->second.count(BB);
480 LVILatticeVal LazyValueInfoCache::getBlockValue(Value *Val, BasicBlock *BB) {
481 // If already a constant, there is nothing to compute.
482 if (Constant *VC = dyn_cast<Constant>(Val))
483 return LVILatticeVal::get(VC);
485 SeenBlocks.insert(BB);
486 return lookup(Val)[BB];
489 bool LazyValueInfoCache::solveBlockValue(Value *Val, BasicBlock *BB) {
490 if (isa<Constant>(Val))
493 ValueCacheEntryTy &Cache = lookup(Val);
494 SeenBlocks.insert(BB);
495 LVILatticeVal &BBLV = Cache[BB];
497 // OverDefinedCacheUpdater is a helper object that will update
498 // the OverDefinedCache for us when this method exits. Make sure to
499 // call markResult on it as we exist, passing a bool to indicate if the
500 // cache needs updating, i.e. if we have solve a new value or not.
501 OverDefinedCacheUpdater ODCacheUpdater(Val, BB, BBLV, this);
503 // If we've already computed this block's value, return it.
504 if (!BBLV.isUndefined()) {
505 DEBUG(dbgs() << " reuse BB '" << BB->getName() << "' val=" << BBLV <<'\n');
507 // Since we're reusing a cached value here, we don't need to update the
508 // OverDefinedCahce. The cache will have been properly updated
509 // whenever the cached value was inserted.
510 ODCacheUpdater.markResult(false);
514 // Otherwise, this is the first time we're seeing this block. Reset the
515 // lattice value to overdefined, so that cycles will terminate and be
516 // conservatively correct.
517 BBLV.markOverdefined();
519 Instruction *BBI = dyn_cast<Instruction>(Val);
520 if (!BBI || BBI->getParent() != BB) {
521 return ODCacheUpdater.markResult(solveBlockValueNonLocal(BBLV, Val, BB));
524 if (PHINode *PN = dyn_cast<PHINode>(BBI)) {
525 return ODCacheUpdater.markResult(solveBlockValuePHINode(BBLV, PN, BB));
528 if (AllocaInst *AI = dyn_cast<AllocaInst>(BBI)) {
529 BBLV = LVILatticeVal::getNot(ConstantPointerNull::get(AI->getType()));
530 return ODCacheUpdater.markResult(true);
533 // We can only analyze the definitions of certain classes of instructions
534 // (integral binops and casts at the moment), so bail if this isn't one.
535 LVILatticeVal Result;
536 if ((!isa<BinaryOperator>(BBI) && !isa<CastInst>(BBI)) ||
537 !BBI->getType()->isIntegerTy()) {
538 DEBUG(dbgs() << " compute BB '" << BB->getName()
539 << "' - overdefined because inst def found.\n");
540 BBLV.markOverdefined();
541 return ODCacheUpdater.markResult(true);
544 // FIXME: We're currently limited to binops with a constant RHS. This should
546 BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI);
547 if (BO && !isa<ConstantInt>(BO->getOperand(1))) {
548 DEBUG(dbgs() << " compute BB '" << BB->getName()
549 << "' - overdefined because inst def found.\n");
551 BBLV.markOverdefined();
552 return ODCacheUpdater.markResult(true);
555 return ODCacheUpdater.markResult(solveBlockValueConstantRange(BBLV, BBI, BB));
558 static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr) {
559 if (LoadInst *L = dyn_cast<LoadInst>(I)) {
560 return L->getPointerAddressSpace() == 0 &&
561 GetUnderlyingObject(L->getPointerOperand()) == Ptr;
563 if (StoreInst *S = dyn_cast<StoreInst>(I)) {
564 return S->getPointerAddressSpace() == 0 &&
565 GetUnderlyingObject(S->getPointerOperand()) == Ptr;
567 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
568 if (MI->isVolatile()) return false;
570 // FIXME: check whether it has a valuerange that excludes zero?
571 ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength());
572 if (!Len || Len->isZero()) return false;
574 if (MI->getDestAddressSpace() == 0)
575 if (GetUnderlyingObject(MI->getRawDest()) == Ptr)
577 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))
578 if (MTI->getSourceAddressSpace() == 0)
579 if (GetUnderlyingObject(MTI->getRawSource()) == Ptr)
585 bool LazyValueInfoCache::solveBlockValueNonLocal(LVILatticeVal &BBLV,
586 Value *Val, BasicBlock *BB) {
587 LVILatticeVal Result; // Start Undefined.
589 // If this is a pointer, and there's a load from that pointer in this BB,
590 // then we know that the pointer can't be NULL.
591 bool NotNull = false;
592 if (Val->getType()->isPointerTy()) {
593 if (isKnownNonNull(Val)) {
596 Value *UnderlyingVal = GetUnderlyingObject(Val);
597 // If 'GetUnderlyingObject' didn't converge, skip it. It won't converge
598 // inside InstructionDereferencesPointer either.
599 if (UnderlyingVal == GetUnderlyingObject(UnderlyingVal, nullptr, 1)) {
600 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
602 if (InstructionDereferencesPointer(BI, UnderlyingVal)) {
611 // If this is the entry block, we must be asking about an argument. The
612 // value is overdefined.
613 if (BB == &BB->getParent()->getEntryBlock()) {
614 assert(isa<Argument>(Val) && "Unknown live-in to the entry block");
616 PointerType *PTy = cast<PointerType>(Val->getType());
617 Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
619 Result.markOverdefined();
625 // Loop over all of our predecessors, merging what we know from them into
627 bool EdgesMissing = false;
628 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
629 LVILatticeVal EdgeResult;
630 EdgesMissing |= !getEdgeValue(Val, *PI, BB, EdgeResult);
634 Result.mergeIn(EdgeResult);
636 // If we hit overdefined, exit early. The BlockVals entry is already set
638 if (Result.isOverdefined()) {
639 DEBUG(dbgs() << " compute BB '" << BB->getName()
640 << "' - overdefined because of pred.\n");
641 // If we previously determined that this is a pointer that can't be null
642 // then return that rather than giving up entirely.
644 PointerType *PTy = cast<PointerType>(Val->getType());
645 Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
655 // Return the merged value, which is more precise than 'overdefined'.
656 assert(!Result.isOverdefined());
661 bool LazyValueInfoCache::solveBlockValuePHINode(LVILatticeVal &BBLV,
662 PHINode *PN, BasicBlock *BB) {
663 LVILatticeVal Result; // Start Undefined.
665 // Loop over all of our predecessors, merging what we know from them into
667 bool EdgesMissing = false;
668 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
669 BasicBlock *PhiBB = PN->getIncomingBlock(i);
670 Value *PhiVal = PN->getIncomingValue(i);
671 LVILatticeVal EdgeResult;
672 EdgesMissing |= !getEdgeValue(PhiVal, PhiBB, BB, EdgeResult);
676 Result.mergeIn(EdgeResult);
678 // If we hit overdefined, exit early. The BlockVals entry is already set
680 if (Result.isOverdefined()) {
681 DEBUG(dbgs() << " compute BB '" << BB->getName()
682 << "' - overdefined because of pred.\n");
691 // Return the merged value, which is more precise than 'overdefined'.
692 assert(!Result.isOverdefined() && "Possible PHI in entry block?");
697 bool LazyValueInfoCache::solveBlockValueConstantRange(LVILatticeVal &BBLV,
700 // Figure out the range of the LHS. If that fails, bail.
701 if (!hasBlockValue(BBI->getOperand(0), BB)) {
702 BlockValueStack.push(std::make_pair(BB, BBI->getOperand(0)));
706 LVILatticeVal LHSVal = getBlockValue(BBI->getOperand(0), BB);
707 if (!LHSVal.isConstantRange()) {
708 BBLV.markOverdefined();
712 ConstantRange LHSRange = LHSVal.getConstantRange();
713 ConstantRange RHSRange(1);
714 IntegerType *ResultTy = cast<IntegerType>(BBI->getType());
715 if (isa<BinaryOperator>(BBI)) {
716 if (ConstantInt *RHS = dyn_cast<ConstantInt>(BBI->getOperand(1))) {
717 RHSRange = ConstantRange(RHS->getValue());
719 BBLV.markOverdefined();
724 // NOTE: We're currently limited by the set of operations that ConstantRange
725 // can evaluate symbolically. Enhancing that set will allows us to analyze
727 LVILatticeVal Result;
728 switch (BBI->getOpcode()) {
729 case Instruction::Add:
730 Result.markConstantRange(LHSRange.add(RHSRange));
732 case Instruction::Sub:
733 Result.markConstantRange(LHSRange.sub(RHSRange));
735 case Instruction::Mul:
736 Result.markConstantRange(LHSRange.multiply(RHSRange));
738 case Instruction::UDiv:
739 Result.markConstantRange(LHSRange.udiv(RHSRange));
741 case Instruction::Shl:
742 Result.markConstantRange(LHSRange.shl(RHSRange));
744 case Instruction::LShr:
745 Result.markConstantRange(LHSRange.lshr(RHSRange));
747 case Instruction::Trunc:
748 Result.markConstantRange(LHSRange.truncate(ResultTy->getBitWidth()));
750 case Instruction::SExt:
751 Result.markConstantRange(LHSRange.signExtend(ResultTy->getBitWidth()));
753 case Instruction::ZExt:
754 Result.markConstantRange(LHSRange.zeroExtend(ResultTy->getBitWidth()));
756 case Instruction::BitCast:
757 Result.markConstantRange(LHSRange);
759 case Instruction::And:
760 Result.markConstantRange(LHSRange.binaryAnd(RHSRange));
762 case Instruction::Or:
763 Result.markConstantRange(LHSRange.binaryOr(RHSRange));
766 // Unhandled instructions are overdefined.
768 DEBUG(dbgs() << " compute BB '" << BB->getName()
769 << "' - overdefined because inst def found.\n");
770 Result.markOverdefined();
778 /// \brief Compute the value of Val on the edge BBFrom -> BBTo. Returns false if
779 /// Val is not constrained on the edge.
780 static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom,
781 BasicBlock *BBTo, LVILatticeVal &Result) {
782 // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
784 if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {
785 // If this is a conditional branch and only one successor goes to BBTo, then
786 // we maybe able to infer something from the condition.
787 if (BI->isConditional() &&
788 BI->getSuccessor(0) != BI->getSuccessor(1)) {
789 bool isTrueDest = BI->getSuccessor(0) == BBTo;
790 assert(BI->getSuccessor(!isTrueDest) == BBTo &&
791 "BBTo isn't a successor of BBFrom");
793 // If V is the condition of the branch itself, then we know exactly what
795 if (BI->getCondition() == Val) {
796 Result = LVILatticeVal::get(ConstantInt::get(
797 Type::getInt1Ty(Val->getContext()), isTrueDest));
801 // If the condition of the branch is an equality comparison, we may be
802 // able to infer the value.
803 ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition());
804 if (ICI && isa<Constant>(ICI->getOperand(1))) {
805 if (ICI->isEquality() && ICI->getOperand(0) == Val) {
806 // We know that V has the RHS constant if this is a true SETEQ or
808 if (isTrueDest == (ICI->getPredicate() == ICmpInst::ICMP_EQ))
809 Result = LVILatticeVal::get(cast<Constant>(ICI->getOperand(1)));
811 Result = LVILatticeVal::getNot(cast<Constant>(ICI->getOperand(1)));
815 // Recognize the range checking idiom that InstCombine produces.
816 // (X-C1) u< C2 --> [C1, C1+C2)
817 ConstantInt *NegOffset = nullptr;
818 if (ICI->getPredicate() == ICmpInst::ICMP_ULT)
819 match(ICI->getOperand(0), m_Add(m_Specific(Val),
820 m_ConstantInt(NegOffset)));
822 ConstantInt *CI = dyn_cast<ConstantInt>(ICI->getOperand(1));
823 if (CI && (ICI->getOperand(0) == Val || NegOffset)) {
824 // Calculate the range of values that would satisfy the comparison.
825 ConstantRange CmpRange(CI->getValue());
826 ConstantRange TrueValues =
827 ConstantRange::makeICmpRegion(ICI->getPredicate(), CmpRange);
829 if (NegOffset) // Apply the offset from above.
830 TrueValues = TrueValues.subtract(NegOffset->getValue());
832 // If we're interested in the false dest, invert the condition.
833 if (!isTrueDest) TrueValues = TrueValues.inverse();
835 Result = LVILatticeVal::getRange(TrueValues);
842 // If the edge was formed by a switch on the value, then we may know exactly
844 if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {
845 if (SI->getCondition() != Val)
848 bool DefaultCase = SI->getDefaultDest() == BBTo;
849 unsigned BitWidth = Val->getType()->getIntegerBitWidth();
850 ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/);
852 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
854 ConstantRange EdgeVal(i.getCaseValue()->getValue());
856 // It is possible that the default destination is the destination of
857 // some cases. There is no need to perform difference for those cases.
858 if (i.getCaseSuccessor() != BBTo)
859 EdgesVals = EdgesVals.difference(EdgeVal);
860 } else if (i.getCaseSuccessor() == BBTo)
861 EdgesVals = EdgesVals.unionWith(EdgeVal);
863 Result = LVILatticeVal::getRange(EdgesVals);
869 /// \brief Compute the value of Val on the edge BBFrom -> BBTo, or the value at
870 /// the basic block if the edge does not constraint Val.
871 bool LazyValueInfoCache::getEdgeValue(Value *Val, BasicBlock *BBFrom,
872 BasicBlock *BBTo, LVILatticeVal &Result) {
873 // If already a constant, there is nothing to compute.
874 if (Constant *VC = dyn_cast<Constant>(Val)) {
875 Result = LVILatticeVal::get(VC);
879 if (getEdgeValueLocal(Val, BBFrom, BBTo, Result)) {
880 if (!Result.isConstantRange() ||
881 Result.getConstantRange().getSingleElement())
884 // FIXME: this check should be moved to the beginning of the function when
885 // LVI better supports recursive values. Even for the single value case, we
886 // can intersect to detect dead code (an empty range).
887 if (!hasBlockValue(Val, BBFrom)) {
888 BlockValueStack.push(std::make_pair(BBFrom, Val));
892 // Try to intersect ranges of the BB and the constraint on the edge.
893 LVILatticeVal InBlock = getBlockValue(Val, BBFrom);
894 if (!InBlock.isConstantRange())
897 ConstantRange Range =
898 Result.getConstantRange().intersectWith(InBlock.getConstantRange());
899 Result = LVILatticeVal::getRange(Range);
903 if (!hasBlockValue(Val, BBFrom)) {
904 BlockValueStack.push(std::make_pair(BBFrom, Val));
908 // if we couldn't compute the value on the edge, use the value from the BB
909 Result = getBlockValue(Val, BBFrom);
913 LVILatticeVal LazyValueInfoCache::getValueInBlock(Value *V, BasicBlock *BB) {
914 DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
915 << BB->getName() << "'\n");
917 BlockValueStack.push(std::make_pair(BB, V));
919 LVILatticeVal Result = getBlockValue(V, BB);
921 DEBUG(dbgs() << " Result = " << Result << "\n");
925 LVILatticeVal LazyValueInfoCache::
926 getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB) {
927 DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
928 << FromBB->getName() << "' to '" << ToBB->getName() << "'\n");
930 LVILatticeVal Result;
931 if (!getEdgeValue(V, FromBB, ToBB, Result)) {
933 bool WasFastQuery = getEdgeValue(V, FromBB, ToBB, Result);
935 assert(WasFastQuery && "More work to do after problem solved?");
938 DEBUG(dbgs() << " Result = " << Result << "\n");
942 void LazyValueInfoCache::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
943 BasicBlock *NewSucc) {
944 // When an edge in the graph has been threaded, values that we could not
945 // determine a value for before (i.e. were marked overdefined) may be possible
946 // to solve now. We do NOT try to proactively update these values. Instead,
947 // we clear their entries from the cache, and allow lazy updating to recompute
950 // The updating process is fairly simple: we need to dropped cached info
951 // for all values that were marked overdefined in OldSucc, and for those same
952 // values in any successor of OldSucc (except NewSucc) in which they were
953 // also marked overdefined.
954 std::vector<BasicBlock*> worklist;
955 worklist.push_back(OldSucc);
957 DenseSet<Value*> ClearSet;
958 for (DenseSet<OverDefinedPairTy>::iterator I = OverDefinedCache.begin(),
959 E = OverDefinedCache.end(); I != E; ++I) {
960 if (I->first == OldSucc)
961 ClearSet.insert(I->second);
964 // Use a worklist to perform a depth-first search of OldSucc's successors.
965 // NOTE: We do not need a visited list since any blocks we have already
966 // visited will have had their overdefined markers cleared already, and we
967 // thus won't loop to their successors.
968 while (!worklist.empty()) {
969 BasicBlock *ToUpdate = worklist.back();
972 // Skip blocks only accessible through NewSucc.
973 if (ToUpdate == NewSucc) continue;
975 bool changed = false;
976 for (DenseSet<Value*>::iterator I = ClearSet.begin(), E = ClearSet.end();
978 // If a value was marked overdefined in OldSucc, and is here too...
979 DenseSet<OverDefinedPairTy>::iterator OI =
980 OverDefinedCache.find(std::make_pair(ToUpdate, *I));
981 if (OI == OverDefinedCache.end()) continue;
983 // Remove it from the caches.
984 ValueCacheEntryTy &Entry = ValueCache[LVIValueHandle(*I, this)];
985 ValueCacheEntryTy::iterator CI = Entry.find(ToUpdate);
987 assert(CI != Entry.end() && "Couldn't find entry to update?");
989 OverDefinedCache.erase(OI);
991 // If we removed anything, then we potentially need to update
992 // blocks successors too.
996 if (!changed) continue;
998 worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate));
1002 //===----------------------------------------------------------------------===//
1003 // LazyValueInfo Impl
1004 //===----------------------------------------------------------------------===//
1006 /// getCache - This lazily constructs the LazyValueInfoCache.
1007 static LazyValueInfoCache &getCache(void *&PImpl) {
1009 PImpl = new LazyValueInfoCache();
1010 return *static_cast<LazyValueInfoCache*>(PImpl);
1013 bool LazyValueInfo::runOnFunction(Function &F) {
1015 getCache(PImpl).clear();
1017 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
1018 DL = DLP ? &DLP->getDataLayout() : nullptr;
1019 TLI = &getAnalysis<TargetLibraryInfo>();
1025 void LazyValueInfo::getAnalysisUsage(AnalysisUsage &AU) const {
1026 AU.setPreservesAll();
1027 AU.addRequired<TargetLibraryInfo>();
1030 void LazyValueInfo::releaseMemory() {
1031 // If the cache was allocated, free it.
1033 delete &getCache(PImpl);
1038 Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB) {
1039 LVILatticeVal Result = getCache(PImpl).getValueInBlock(V, BB);
1041 if (Result.isConstant())
1042 return Result.getConstant();
1043 if (Result.isConstantRange()) {
1044 ConstantRange CR = Result.getConstantRange();
1045 if (const APInt *SingleVal = CR.getSingleElement())
1046 return ConstantInt::get(V->getContext(), *SingleVal);
1051 /// getConstantOnEdge - Determine whether the specified value is known to be a
1052 /// constant on the specified edge. Return null if not.
1053 Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB,
1055 LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB);
1057 if (Result.isConstant())
1058 return Result.getConstant();
1059 if (Result.isConstantRange()) {
1060 ConstantRange CR = Result.getConstantRange();
1061 if (const APInt *SingleVal = CR.getSingleElement())
1062 return ConstantInt::get(V->getContext(), *SingleVal);
1067 /// getPredicateOnEdge - Determine whether the specified value comparison
1068 /// with a constant is known to be true or false on the specified CFG edge.
1069 /// Pred is a CmpInst predicate.
1070 LazyValueInfo::Tristate
1071 LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C,
1072 BasicBlock *FromBB, BasicBlock *ToBB) {
1073 LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB);
1075 // If we know the value is a constant, evaluate the conditional.
1076 Constant *Res = nullptr;
1077 if (Result.isConstant()) {
1078 Res = ConstantFoldCompareInstOperands(Pred, Result.getConstant(), C, DL,
1080 if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res))
1081 return ResCI->isZero() ? False : True;
1085 if (Result.isConstantRange()) {
1086 ConstantInt *CI = dyn_cast<ConstantInt>(C);
1087 if (!CI) return Unknown;
1089 ConstantRange CR = Result.getConstantRange();
1090 if (Pred == ICmpInst::ICMP_EQ) {
1091 if (!CR.contains(CI->getValue()))
1094 if (CR.isSingleElement() && CR.contains(CI->getValue()))
1096 } else if (Pred == ICmpInst::ICMP_NE) {
1097 if (!CR.contains(CI->getValue()))
1100 if (CR.isSingleElement() && CR.contains(CI->getValue()))
1104 // Handle more complex predicates.
1105 ConstantRange TrueValues =
1106 ICmpInst::makeConstantRange((ICmpInst::Predicate)Pred, CI->getValue());
1107 if (TrueValues.contains(CR))
1109 if (TrueValues.inverse().contains(CR))
1114 if (Result.isNotConstant()) {
1115 // If this is an equality comparison, we can try to fold it knowing that
1117 if (Pred == ICmpInst::ICMP_EQ) {
1118 // !C1 == C -> false iff C1 == C.
1119 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
1120 Result.getNotConstant(), C, DL,
1122 if (Res->isNullValue())
1124 } else if (Pred == ICmpInst::ICMP_NE) {
1125 // !C1 != C -> true iff C1 == C.
1126 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
1127 Result.getNotConstant(), C, DL,
1129 if (Res->isNullValue())
1138 void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
1139 BasicBlock *NewSucc) {
1140 if (PImpl) getCache(PImpl).threadEdge(PredBB, OldSucc, NewSucc);
1143 void LazyValueInfo::eraseBlock(BasicBlock *BB) {
1144 if (PImpl) getCache(PImpl).eraseBlock(BB);