[C++11] Add predecessors(BasicBlock *) / successors(BasicBlock *) iterator ranges.
[oota-llvm.git] / lib / Analysis / LazyValueInfo.cpp
1 //===- LazyValueInfo.cpp - Value constraint analysis ----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the interface for lazy computation of value constraint
11 // information.
12 //
13 //===----------------------------------------------------------------------===//
14
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"
31 #include <map>
32 #include <stack>
33 using namespace llvm;
34 using namespace PatternMatch;
35
36 #define DEBUG_TYPE "lazy-value-info"
37
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)
44
45 namespace llvm {
46   FunctionPass *createLazyValueInfoPass() { return new LazyValueInfo(); }
47 }
48
49
50 //===----------------------------------------------------------------------===//
51 //                               LVILatticeVal
52 //===----------------------------------------------------------------------===//
53
54 /// LVILatticeVal - This is the information tracked by LazyValueInfo for each
55 /// value.
56 ///
57 /// FIXME: This is basically just for bringup, this can be made a lot more rich
58 /// in the future.
59 ///
60 namespace {
61 class LVILatticeVal {
62   enum LatticeValueTy {
63     /// undefined - This Value has no known value yet.
64     undefined,
65     
66     /// constant - This Value has a specific constant value.
67     constant,
68     /// notconstant - This Value is known to not have the specified value.
69     notconstant,
70
71     /// constantrange - The Value falls within this range.
72     constantrange,
73
74     /// overdefined - This value is not known to be constant, and we know that
75     /// it has a value.
76     overdefined
77   };
78   
79   /// Val: This stores the current lattice value along with the Constant* for
80   /// the constant if this is a 'constant' or 'notconstant' value.
81   LatticeValueTy Tag;
82   Constant *Val;
83   ConstantRange Range;
84   
85 public:
86   LVILatticeVal() : Tag(undefined), Val(nullptr), Range(1, true) {}
87
88   static LVILatticeVal get(Constant *C) {
89     LVILatticeVal Res;
90     if (!isa<UndefValue>(C))
91       Res.markConstant(C);
92     return Res;
93   }
94   static LVILatticeVal getNot(Constant *C) {
95     LVILatticeVal Res;
96     if (!isa<UndefValue>(C))
97       Res.markNotConstant(C);
98     return Res;
99   }
100   static LVILatticeVal getRange(ConstantRange CR) {
101     LVILatticeVal Res;
102     Res.markConstantRange(CR);
103     return Res;
104   }
105   
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; }
111   
112   Constant *getConstant() const {
113     assert(isConstant() && "Cannot get the constant of a non-constant!");
114     return Val;
115   }
116   
117   Constant *getNotConstant() const {
118     assert(isNotConstant() && "Cannot get the constant of a non-notconstant!");
119     return Val;
120   }
121   
122   ConstantRange getConstantRange() const {
123     assert(isConstantRange() &&
124            "Cannot get the constant-range of a non-constant-range!");
125     return Range;
126   }
127   
128   /// markOverdefined - Return true if this is a change in status.
129   bool markOverdefined() {
130     if (isOverdefined())
131       return false;
132     Tag = overdefined;
133     return true;
134   }
135
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))
142       return false;
143
144     assert((!isConstant() || getConstant() == V) &&
145            "Marking constant with different value");
146     assert(isUndefined());
147     Tag = constant;
148     Val = V;
149     return true;
150   }
151   
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))
158       return false;
159
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());
165     Tag = notconstant;
166     Val = V;
167     return true;
168   }
169   
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();
175       
176       bool changed = Range != NewR;
177       Range = NewR;
178       return changed;
179     }
180     
181     assert(isUndefined());
182     if (NewR.isEmptySet())
183       return markOverdefined();
184     
185     Tag = constantrange;
186     Range = NewR;
187     return true;
188   }
189   
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();
195
196     if (isUndefined()) {
197       Tag = RHS.Tag;
198       Val = RHS.Val;
199       Range = RHS.Range;
200       return true;
201     }
202
203     if (isConstant()) {
204       if (RHS.isConstant()) {
205         if (Val == RHS.Val)
206           return false;
207         return markOverdefined();
208       }
209
210       if (RHS.isNotConstant()) {
211         if (Val == RHS.Val)
212           return markOverdefined();
213
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,
219                                                 getConstant(),
220                                                 RHS.getNotConstant())))
221           if (Res->isOne())
222             return markNotConstant(RHS.getNotConstant());
223
224         return markOverdefined();
225       }
226
227       // RHS is a ConstantRange, LHS is a non-integer Constant.
228
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.
231
232       return markOverdefined();
233     }
234
235     if (isNotConstant()) {
236       if (RHS.isConstant()) {
237         if (Val == RHS.Val)
238           return markOverdefined();
239
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,
245                                                 getNotConstant(),
246                                                 RHS.getConstant())))
247           if (Res->isOne())
248             return false;
249
250         return markOverdefined();
251       }
252
253       if (RHS.isNotConstant()) {
254         if (Val == RHS.Val)
255           return false;
256         return markOverdefined();
257       }
258
259       return markOverdefined();
260     }
261
262     assert(isConstantRange() && "New LVILattice type?");
263     if (!RHS.isConstantRange())
264       return markOverdefined();
265
266     ConstantRange NewR = Range.unionWith(RHS.getConstantRange());
267     if (NewR.isFullSet())
268       return markOverdefined();
269     return markConstantRange(NewR);
270   }
271 };
272   
273 } // end anonymous namespace.
274
275 namespace llvm {
276 raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val)
277     LLVM_ATTRIBUTE_USED;
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";
283
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() << '>';
290 }
291 }
292
293 //===----------------------------------------------------------------------===//
294 //                          LazyValueInfoCache Decl
295 //===----------------------------------------------------------------------===//
296
297 namespace {
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;
303       
304     LVIValueHandle(Value *V, LazyValueInfoCache *P)
305       : CallbackVH(V), Parent(P) { }
306
307     void deleted() override;
308     void allUsesReplacedWith(Value *V) override {
309       deleted();
310     }
311   };
312 }
313
314 namespace { 
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;
322
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;
326     
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;
332
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;
336
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;
341     
342     friend struct LVIValueHandle;
343     
344     /// OverDefinedCacheUpdater - A helper object that ensures that the
345     /// OverDefinedCache is updated whenever solveBlockValue returns.
346     struct OverDefinedCacheUpdater {
347       LazyValueInfoCache *Parent;
348       Value *Val;
349       BasicBlock *BB;
350       LVILatticeVal &BBLV;
351       
352       OverDefinedCacheUpdater(Value *V, BasicBlock *B, LVILatticeVal &LV,
353                        LazyValueInfoCache *P)
354         : Parent(P), Val(V), BB(B), BBLV(LV) { }
355       
356       bool markResult(bool changed) { 
357         if (changed && BBLV.isOverdefined())
358           Parent->OverDefinedCache.insert(std::make_pair(BB, Val));
359         return changed;
360       }
361     };
362     
363
364
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);
369
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);
380
381     void solve();
382     
383     ValueCacheEntryTy &lookup(Value *V) {
384       return ValueCache[LVIValueHandle(V, this)];
385     }
386
387   public:
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);
391
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);
395     
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
398     /// NewSucc.
399     void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
400     
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);
404     
405     /// clear - Empty the cache.
406     void clear() {
407       SeenBlocks.clear();
408       ValueCache.clear();
409       OverDefinedCache.clear();
410     }
411   };
412 } // end anonymous namespace
413
414 void LVIValueHandle::deleted() {
415   typedef std::pair<AssertingVH<BasicBlock>, Value*> OverDefinedPairTy;
416   
417   SmallVector<OverDefinedPairTy, 4> ToErase;
418   for (DenseSet<OverDefinedPairTy>::iterator 
419        I = Parent->OverDefinedCache.begin(),
420        E = Parent->OverDefinedCache.end();
421        I != E; ++I) {
422     if (I->second == getValPtr())
423       ToErase.push_back(*I);
424   }
425
426   for (SmallVectorImpl<OverDefinedPairTy>::iterator I = ToErase.begin(),
427        E = ToErase.end(); I != E; ++I)
428     Parent->OverDefinedCache.erase(*I);
429   
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);
433 }
434
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())
439     return;
440   SeenBlocks.erase(I);
441
442   SmallVector<OverDefinedPairTy, 4> ToErase;
443   for (DenseSet<OverDefinedPairTy>::iterator  I = OverDefinedCache.begin(),
444        E = OverDefinedCache.end(); I != E; ++I) {
445     if (I->first == BB)
446       ToErase.push_back(*I);
447   }
448
449   for (SmallVectorImpl<OverDefinedPairTy>::iterator I = ToErase.begin(),
450        E = ToErase.end(); I != E; ++I)
451     OverDefinedCache.erase(*I);
452
453   for (std::map<LVIValueHandle, ValueCacheEntryTy>::iterator
454        I = ValueCache.begin(), E = ValueCache.end(); I != E; ++I)
455     I->second.erase(BB);
456 }
457
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();
464     }
465   }
466 }
467
468 bool LazyValueInfoCache::hasBlockValue(Value *Val, BasicBlock *BB) {
469   // If already a constant, there is nothing to compute.
470   if (isa<Constant>(Val))
471     return true;
472
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);
478 }
479
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);
484
485   SeenBlocks.insert(BB);
486   return lookup(Val)[BB];
487 }
488
489 bool LazyValueInfoCache::solveBlockValue(Value *Val, BasicBlock *BB) {
490   if (isa<Constant>(Val))
491     return true;
492
493   ValueCacheEntryTy &Cache = lookup(Val);
494   SeenBlocks.insert(BB);
495   LVILatticeVal &BBLV = Cache[BB];
496   
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);
502
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');
506     
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);
511     return true;
512   }
513
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();
518   
519   Instruction *BBI = dyn_cast<Instruction>(Val);
520   if (!BBI || BBI->getParent() != BB) {
521     return ODCacheUpdater.markResult(solveBlockValueNonLocal(BBLV, Val, BB));
522   }
523
524   if (PHINode *PN = dyn_cast<PHINode>(BBI)) {
525     return ODCacheUpdater.markResult(solveBlockValuePHINode(BBLV, PN, BB));
526   }
527
528   if (AllocaInst *AI = dyn_cast<AllocaInst>(BBI)) {
529     BBLV = LVILatticeVal::getNot(ConstantPointerNull::get(AI->getType()));
530     return ODCacheUpdater.markResult(true);
531   }
532
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);
542   }
543
544   // FIXME: We're currently limited to binops with a constant RHS.  This should
545   // be improved.
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");
550
551     BBLV.markOverdefined();
552     return ODCacheUpdater.markResult(true);
553   }
554
555   return ODCacheUpdater.markResult(solveBlockValueConstantRange(BBLV, BBI, BB));
556 }
557
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;
562   }
563   if (StoreInst *S = dyn_cast<StoreInst>(I)) {
564     return S->getPointerAddressSpace() == 0 &&
565         GetUnderlyingObject(S->getPointerOperand()) == Ptr;
566   }
567   if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
568     if (MI->isVolatile()) return false;
569
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;
573
574     if (MI->getDestAddressSpace() == 0)
575       if (GetUnderlyingObject(MI->getRawDest()) == Ptr)
576         return true;
577     if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))
578       if (MTI->getSourceAddressSpace() == 0)
579         if (GetUnderlyingObject(MTI->getRawSource()) == Ptr)
580           return true;
581   }
582   return false;
583 }
584
585 bool LazyValueInfoCache::solveBlockValueNonLocal(LVILatticeVal &BBLV,
586                                                  Value *Val, BasicBlock *BB) {
587   LVILatticeVal Result;  // Start Undefined.
588
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)) {
594       NotNull = true;
595     } else {
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();
601              BI != BE; ++BI) {
602           if (InstructionDereferencesPointer(BI, UnderlyingVal)) {
603             NotNull = true;
604             break;
605           }
606         }
607       }
608     }
609   }
610
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");
615     if (NotNull) {
616       PointerType *PTy = cast<PointerType>(Val->getType());
617       Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
618     } else {
619       Result.markOverdefined();
620     }
621     BBLV = Result;
622     return true;
623   }
624
625   // Loop over all of our predecessors, merging what we know from them into
626   // result.
627   bool EdgesMissing = false;
628   for (BasicBlock *Pred : predecessors(BB)) {
629     LVILatticeVal EdgeResult;
630     EdgesMissing |= !getEdgeValue(Val, Pred, BB, EdgeResult);
631     if (EdgesMissing)
632       continue;
633
634     Result.mergeIn(EdgeResult);
635
636     // If we hit overdefined, exit early.  The BlockVals entry is already set
637     // to overdefined.
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.
643       if (NotNull) {
644         PointerType *PTy = cast<PointerType>(Val->getType());
645         Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
646       }
647       
648       BBLV = Result;
649       return true;
650     }
651   }
652   if (EdgesMissing)
653     return false;
654
655   // Return the merged value, which is more precise than 'overdefined'.
656   assert(!Result.isOverdefined());
657   BBLV = Result;
658   return true;
659 }
660   
661 bool LazyValueInfoCache::solveBlockValuePHINode(LVILatticeVal &BBLV,
662                                                 PHINode *PN, BasicBlock *BB) {
663   LVILatticeVal Result;  // Start Undefined.
664
665   // Loop over all of our predecessors, merging what we know from them into
666   // result.
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);
673     if (EdgesMissing)
674       continue;
675
676     Result.mergeIn(EdgeResult);
677
678     // If we hit overdefined, exit early.  The BlockVals entry is already set
679     // to overdefined.
680     if (Result.isOverdefined()) {
681       DEBUG(dbgs() << " compute BB '" << BB->getName()
682             << "' - overdefined because of pred.\n");
683       
684       BBLV = Result;
685       return true;
686     }
687   }
688   if (EdgesMissing)
689     return false;
690
691   // Return the merged value, which is more precise than 'overdefined'.
692   assert(!Result.isOverdefined() && "Possible PHI in entry block?");
693   BBLV = Result;
694   return true;
695 }
696
697 bool LazyValueInfoCache::solveBlockValueConstantRange(LVILatticeVal &BBLV,
698                                                       Instruction *BBI,
699                                                       BasicBlock *BB) {
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)));
703     return false;
704   }
705
706   LVILatticeVal LHSVal = getBlockValue(BBI->getOperand(0), BB);
707   if (!LHSVal.isConstantRange()) {
708     BBLV.markOverdefined();
709     return true;
710   }
711   
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());
718     } else {
719       BBLV.markOverdefined();
720       return true;
721     }
722   }
723
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
726   // more definitions.
727   LVILatticeVal Result;
728   switch (BBI->getOpcode()) {
729   case Instruction::Add:
730     Result.markConstantRange(LHSRange.add(RHSRange));
731     break;
732   case Instruction::Sub:
733     Result.markConstantRange(LHSRange.sub(RHSRange));
734     break;
735   case Instruction::Mul:
736     Result.markConstantRange(LHSRange.multiply(RHSRange));
737     break;
738   case Instruction::UDiv:
739     Result.markConstantRange(LHSRange.udiv(RHSRange));
740     break;
741   case Instruction::Shl:
742     Result.markConstantRange(LHSRange.shl(RHSRange));
743     break;
744   case Instruction::LShr:
745     Result.markConstantRange(LHSRange.lshr(RHSRange));
746     break;
747   case Instruction::Trunc:
748     Result.markConstantRange(LHSRange.truncate(ResultTy->getBitWidth()));
749     break;
750   case Instruction::SExt:
751     Result.markConstantRange(LHSRange.signExtend(ResultTy->getBitWidth()));
752     break;
753   case Instruction::ZExt:
754     Result.markConstantRange(LHSRange.zeroExtend(ResultTy->getBitWidth()));
755     break;
756   case Instruction::BitCast:
757     Result.markConstantRange(LHSRange);
758     break;
759   case Instruction::And:
760     Result.markConstantRange(LHSRange.binaryAnd(RHSRange));
761     break;
762   case Instruction::Or:
763     Result.markConstantRange(LHSRange.binaryOr(RHSRange));
764     break;
765   
766   // Unhandled instructions are overdefined.
767   default:
768     DEBUG(dbgs() << " compute BB '" << BB->getName()
769                  << "' - overdefined because inst def found.\n");
770     Result.markOverdefined();
771     break;
772   }
773   
774   BBLV = Result;
775   return true;
776 }
777
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
783   // know that v != 0.
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");
792       
793       // If V is the condition of the branch itself, then we know exactly what
794       // it is.
795       if (BI->getCondition() == Val) {
796         Result = LVILatticeVal::get(ConstantInt::get(
797                               Type::getInt1Ty(Val->getContext()), isTrueDest));
798         return true;
799       }
800       
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
807           // false SETNE. 
808           if (isTrueDest == (ICI->getPredicate() == ICmpInst::ICMP_EQ))
809             Result = LVILatticeVal::get(cast<Constant>(ICI->getOperand(1)));
810           else
811             Result = LVILatticeVal::getNot(cast<Constant>(ICI->getOperand(1)));
812           return true;
813         }
814
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)));
821
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);
828
829           if (NegOffset) // Apply the offset from above.
830             TrueValues = TrueValues.subtract(NegOffset->getValue());
831
832           // If we're interested in the false dest, invert the condition.
833           if (!isTrueDest) TrueValues = TrueValues.inverse();
834
835           Result = LVILatticeVal::getRange(TrueValues);
836           return true;
837         }
838       }
839     }
840   }
841
842   // If the edge was formed by a switch on the value, then we may know exactly
843   // what it is.
844   if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {
845     if (SI->getCondition() != Val)
846       return false;
847
848     bool DefaultCase = SI->getDefaultDest() == BBTo;
849     unsigned BitWidth = Val->getType()->getIntegerBitWidth();
850     ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/);
851
852     for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
853          i != e; ++i) {
854       ConstantRange EdgeVal(i.getCaseValue()->getValue());
855       if (DefaultCase) {
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);
862     }
863     Result = LVILatticeVal::getRange(EdgesVals);
864     return true;
865   }
866   return false;
867 }
868
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);
876     return true;
877   }
878
879   if (getEdgeValueLocal(Val, BBFrom, BBTo, Result)) {
880     if (!Result.isConstantRange() ||
881       Result.getConstantRange().getSingleElement())
882       return true;
883
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));
889       return false;
890     }
891
892     // Try to intersect ranges of the BB and the constraint on the edge.
893     LVILatticeVal InBlock = getBlockValue(Val, BBFrom);
894     if (!InBlock.isConstantRange())
895       return true;
896
897     ConstantRange Range =
898       Result.getConstantRange().intersectWith(InBlock.getConstantRange());
899     Result = LVILatticeVal::getRange(Range);
900     return true;
901   }
902
903   if (!hasBlockValue(Val, BBFrom)) {
904     BlockValueStack.push(std::make_pair(BBFrom, Val));
905     return false;
906   }
907
908   // if we couldn't compute the value on the edge, use the value from the BB
909   Result = getBlockValue(Val, BBFrom);
910   return true;
911 }
912
913 LVILatticeVal LazyValueInfoCache::getValueInBlock(Value *V, BasicBlock *BB) {
914   DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
915         << BB->getName() << "'\n");
916   
917   BlockValueStack.push(std::make_pair(BB, V));
918   solve();
919   LVILatticeVal Result = getBlockValue(V, BB);
920
921   DEBUG(dbgs() << "  Result = " << Result << "\n");
922   return Result;
923 }
924
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");
929   
930   LVILatticeVal Result;
931   if (!getEdgeValue(V, FromBB, ToBB, Result)) {
932     solve();
933     bool WasFastQuery = getEdgeValue(V, FromBB, ToBB, Result);
934     (void)WasFastQuery;
935     assert(WasFastQuery && "More work to do after problem solved?");
936   }
937
938   DEBUG(dbgs() << "  Result = " << Result << "\n");
939   return Result;
940 }
941
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
948   // them when needed.
949   
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);
956   
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);
962   }
963   
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();
970     worklist.pop_back();
971     
972     // Skip blocks only accessible through NewSucc.
973     if (ToUpdate == NewSucc) continue;
974     
975     bool changed = false;
976     for (DenseSet<Value*>::iterator I = ClearSet.begin(), E = ClearSet.end();
977          I != E; ++I) {
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;
982
983       // Remove it from the caches.
984       ValueCacheEntryTy &Entry = ValueCache[LVIValueHandle(*I, this)];
985       ValueCacheEntryTy::iterator CI = Entry.find(ToUpdate);
986
987       assert(CI != Entry.end() && "Couldn't find entry to update?");
988       Entry.erase(CI);
989       OverDefinedCache.erase(OI);
990
991       // If we removed anything, then we potentially need to update 
992       // blocks successors too.
993       changed = true;
994     }
995
996     if (!changed) continue;
997     
998     worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate));
999   }
1000 }
1001
1002 //===----------------------------------------------------------------------===//
1003 //                            LazyValueInfo Impl
1004 //===----------------------------------------------------------------------===//
1005
1006 /// getCache - This lazily constructs the LazyValueInfoCache.
1007 static LazyValueInfoCache &getCache(void *&PImpl) {
1008   if (!PImpl)
1009     PImpl = new LazyValueInfoCache();
1010   return *static_cast<LazyValueInfoCache*>(PImpl);
1011 }
1012
1013 bool LazyValueInfo::runOnFunction(Function &F) {
1014   if (PImpl)
1015     getCache(PImpl).clear();
1016
1017   DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
1018   DL = DLP ? &DLP->getDataLayout() : nullptr;
1019   TLI = &getAnalysis<TargetLibraryInfo>();
1020
1021   // Fully lazy.
1022   return false;
1023 }
1024
1025 void LazyValueInfo::getAnalysisUsage(AnalysisUsage &AU) const {
1026   AU.setPreservesAll();
1027   AU.addRequired<TargetLibraryInfo>();
1028 }
1029
1030 void LazyValueInfo::releaseMemory() {
1031   // If the cache was allocated, free it.
1032   if (PImpl) {
1033     delete &getCache(PImpl);
1034     PImpl = nullptr;
1035   }
1036 }
1037
1038 Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB) {
1039   LVILatticeVal Result = getCache(PImpl).getValueInBlock(V, BB);
1040   
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);
1047   }
1048   return nullptr;
1049 }
1050
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,
1054                                            BasicBlock *ToBB) {
1055   LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB);
1056   
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);
1063   }
1064   return nullptr;
1065 }
1066
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);
1074   
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,
1079                                           TLI);
1080     if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res))
1081       return ResCI->isZero() ? False : True;
1082     return Unknown;
1083   }
1084   
1085   if (Result.isConstantRange()) {
1086     ConstantInt *CI = dyn_cast<ConstantInt>(C);
1087     if (!CI) return Unknown;
1088     
1089     ConstantRange CR = Result.getConstantRange();
1090     if (Pred == ICmpInst::ICMP_EQ) {
1091       if (!CR.contains(CI->getValue()))
1092         return False;
1093       
1094       if (CR.isSingleElement() && CR.contains(CI->getValue()))
1095         return True;
1096     } else if (Pred == ICmpInst::ICMP_NE) {
1097       if (!CR.contains(CI->getValue()))
1098         return True;
1099       
1100       if (CR.isSingleElement() && CR.contains(CI->getValue()))
1101         return False;
1102     }
1103     
1104     // Handle more complex predicates.
1105     ConstantRange TrueValues =
1106         ICmpInst::makeConstantRange((ICmpInst::Predicate)Pred, CI->getValue());
1107     if (TrueValues.contains(CR))
1108       return True;
1109     if (TrueValues.inverse().contains(CR))
1110       return False;
1111     return Unknown;
1112   }
1113   
1114   if (Result.isNotConstant()) {
1115     // If this is an equality comparison, we can try to fold it knowing that
1116     // "V != C1".
1117     if (Pred == ICmpInst::ICMP_EQ) {
1118       // !C1 == C -> false iff C1 == C.
1119       Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
1120                                             Result.getNotConstant(), C, DL,
1121                                             TLI);
1122       if (Res->isNullValue())
1123         return False;
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,
1128                                             TLI);
1129       if (Res->isNullValue())
1130         return True;
1131     }
1132     return Unknown;
1133   }
1134   
1135   return Unknown;
1136 }
1137
1138 void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
1139                                BasicBlock *NewSucc) {
1140   if (PImpl) getCache(PImpl).threadEdge(PredBB, OldSucc, NewSucc);
1141 }
1142
1143 void LazyValueInfo::eraseBlock(BasicBlock *BB) {
1144   if (PImpl) getCache(PImpl).eraseBlock(BB);
1145 }