1 //===- LazyValueInfo.cpp - Value constraint analysis ------------*- C++ -*-===//
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/AssumptionCache.h"
19 #include "llvm/Analysis/ConstantFolding.h"
20 #include "llvm/Analysis/TargetLibraryInfo.h"
21 #include "llvm/Analysis/ValueTracking.h"
22 #include "llvm/IR/CFG.h"
23 #include "llvm/IR/ConstantRange.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/Dominators.h"
27 #include "llvm/IR/Instructions.h"
28 #include "llvm/IR/IntrinsicInst.h"
29 #include "llvm/IR/LLVMContext.h"
30 #include "llvm/IR/PatternMatch.h"
31 #include "llvm/IR/ValueHandle.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include <map>
35 #include <stack>
36 using namespace llvm;
37 using namespace PatternMatch;
38 
39 #define DEBUG_TYPE "lazy-value-info"
40 
41 char LazyValueInfo::ID = 0;
42 INITIALIZE_PASS_BEGIN(LazyValueInfo, "lazy-value-info",
43                 "Lazy Value Information Analysis", false, true)
44 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
45 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
46 INITIALIZE_PASS_END(LazyValueInfo, "lazy-value-info",
47                 "Lazy Value Information Analysis", false, true)
48 
49 namespace llvm {
50   FunctionPass *createLazyValueInfoPass() { return new LazyValueInfo(); }
51 }
52 
53 
54 //===----------------------------------------------------------------------===//
55 //                               LVILatticeVal
56 //===----------------------------------------------------------------------===//
57 
58 /// This is the information tracked by LazyValueInfo for each value.
59 ///
60 /// FIXME: This is basically just for bringup, this can be made a lot more rich
61 /// in the future.
62 ///
63 namespace {
64 class LVILatticeVal {
65   enum LatticeValueTy {
66     /// This Value has no known value yet.  As a result, this implies the
67     /// producing instruction is dead.  Caution: We use this as the starting
68     /// state in our local meet rules.  In this usage, it's taken to mean
69     /// "nothing known yet".
70     undefined,
71 
72     /// This Value has a specific constant value.  (For integers, constantrange
73     /// is used instead.)
74     constant,
75 
76     /// This Value is known to not have the specified value.  (For integers,
77     /// constantrange is used instead.)
78     notconstant,
79 
80     /// The Value falls within this range. (Used only for integer typed values.)
81     constantrange,
82 
83     /// We can not precisely model the dynamic values this value might take.
84     overdefined
85   };
86 
87   /// Val: This stores the current lattice value along with the Constant* for
88   /// the constant if this is a 'constant' or 'notconstant' value.
89   LatticeValueTy Tag;
90   Constant *Val;
91   ConstantRange Range;
92 
93 public:
94   LVILatticeVal() : Tag(undefined), Val(nullptr), Range(1, true) {}
95 
96   static LVILatticeVal get(Constant *C) {
97     LVILatticeVal Res;
98     if (!isa<UndefValue>(C))
99       Res.markConstant(C);
100     return Res;
101   }
102   static LVILatticeVal getNot(Constant *C) {
103     LVILatticeVal Res;
104     if (!isa<UndefValue>(C))
105       Res.markNotConstant(C);
106     return Res;
107   }
108   static LVILatticeVal getRange(ConstantRange CR) {
109     LVILatticeVal Res;
110     Res.markConstantRange(std::move(CR));
111     return Res;
112   }
113   static LVILatticeVal getOverdefined() {
114     LVILatticeVal Res;
115     Res.markOverdefined();
116     return Res;
117   }
118 
119   bool isUndefined() const     { return Tag == undefined; }
120   bool isConstant() const      { return Tag == constant; }
121   bool isNotConstant() const   { return Tag == notconstant; }
122   bool isConstantRange() const { return Tag == constantrange; }
123   bool isOverdefined() const   { return Tag == overdefined; }
124 
125   Constant *getConstant() const {
126     assert(isConstant() && "Cannot get the constant of a non-constant!");
127     return Val;
128   }
129 
130   Constant *getNotConstant() const {
131     assert(isNotConstant() && "Cannot get the constant of a non-notconstant!");
132     return Val;
133   }
134 
135   ConstantRange getConstantRange() const {
136     assert(isConstantRange() &&
137            "Cannot get the constant-range of a non-constant-range!");
138     return Range;
139   }
140 
141   /// Return true if this is a change in status.
142   bool markOverdefined() {
143     if (isOverdefined())
144       return false;
145     Tag = overdefined;
146     return true;
147   }
148 
149   /// Return true if this is a change in status.
150   bool markConstant(Constant *V) {
151     assert(V && "Marking constant with NULL");
152     if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
153       return markConstantRange(ConstantRange(CI->getValue()));
154     if (isa<UndefValue>(V))
155       return false;
156 
157     assert((!isConstant() || getConstant() == V) &&
158            "Marking constant with different value");
159     assert(isUndefined());
160     Tag = constant;
161     Val = V;
162     return true;
163   }
164 
165   /// Return true if this is a change in status.
166   bool markNotConstant(Constant *V) {
167     assert(V && "Marking constant with NULL");
168     if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
169       return markConstantRange(ConstantRange(CI->getValue()+1, CI->getValue()));
170     if (isa<UndefValue>(V))
171       return false;
172 
173     assert((!isConstant() || getConstant() != V) &&
174            "Marking constant !constant with same value");
175     assert((!isNotConstant() || getNotConstant() == V) &&
176            "Marking !constant with different value");
177     assert(isUndefined() || isConstant());
178     Tag = notconstant;
179     Val = V;
180     return true;
181   }
182 
183   /// Return true if this is a change in status.
184   bool markConstantRange(ConstantRange NewR) {
185     if (isConstantRange()) {
186       if (NewR.isEmptySet())
187         return markOverdefined();
188 
189       bool changed = Range != NewR;
190       Range = std::move(NewR);
191       return changed;
192     }
193 
194     assert(isUndefined());
195     if (NewR.isEmptySet())
196       return markOverdefined();
197 
198     Tag = constantrange;
199     Range = std::move(NewR);
200     return true;
201   }
202 
203   /// Merge the specified lattice value into this one, updating this
204   /// one and returning true if anything changed.
205   bool mergeIn(const LVILatticeVal &RHS, const DataLayout &DL) {
206     if (RHS.isUndefined() || isOverdefined()) return false;
207     if (RHS.isOverdefined()) return markOverdefined();
208 
209     if (isUndefined()) {
210       Tag = RHS.Tag;
211       Val = RHS.Val;
212       Range = RHS.Range;
213       return true;
214     }
215 
216     if (isConstant()) {
217       if (RHS.isConstant()) {
218         if (Val == RHS.Val)
219           return false;
220         return markOverdefined();
221       }
222 
223       if (RHS.isNotConstant()) {
224         if (Val == RHS.Val)
225           return markOverdefined();
226 
227         // Unless we can prove that the two Constants are different, we must
228         // move to overdefined.
229         if (ConstantInt *Res =
230                 dyn_cast<ConstantInt>(ConstantFoldCompareInstOperands(
231                     CmpInst::ICMP_NE, getConstant(), RHS.getNotConstant(), DL)))
232           if (Res->isOne())
233             return markNotConstant(RHS.getNotConstant());
234 
235         return markOverdefined();
236       }
237 
238       return markOverdefined();
239     }
240 
241     if (isNotConstant()) {
242       if (RHS.isConstant()) {
243         if (Val == RHS.Val)
244           return markOverdefined();
245 
246         // Unless we can prove that the two Constants are different, we must
247         // move to overdefined.
248         if (ConstantInt *Res =
249                 dyn_cast<ConstantInt>(ConstantFoldCompareInstOperands(
250                     CmpInst::ICMP_NE, getNotConstant(), RHS.getConstant(), DL)))
251           if (Res->isOne())
252             return false;
253 
254         return markOverdefined();
255       }
256 
257       if (RHS.isNotConstant()) {
258         if (Val == RHS.Val)
259           return false;
260         return markOverdefined();
261       }
262 
263       return markOverdefined();
264     }
265 
266     assert(isConstantRange() && "New LVILattice type?");
267     if (!RHS.isConstantRange())
268       return markOverdefined();
269 
270     ConstantRange NewR = Range.unionWith(RHS.getConstantRange());
271     if (NewR.isFullSet())
272       return markOverdefined();
273     return markConstantRange(NewR);
274   }
275 };
276 
277 } // end anonymous namespace.
278 
279 namespace llvm {
280 raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val)
281     LLVM_ATTRIBUTE_USED;
282 raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) {
283   if (Val.isUndefined())
284     return OS << "undefined";
285   if (Val.isOverdefined())
286     return OS << "overdefined";
287 
288   if (Val.isNotConstant())
289     return OS << "notconstant<" << *Val.getNotConstant() << '>';
290   if (Val.isConstantRange())
291     return OS << "constantrange<" << Val.getConstantRange().getLower() << ", "
292               << Val.getConstantRange().getUpper() << '>';
293   return OS << "constant<" << *Val.getConstant() << '>';
294 }
295 }
296 
297 /// Returns true if this lattice value represents at most one possible value.
298 /// This is as precise as any lattice value can get while still representing
299 /// reachable code.
300 static bool hasSingleValue(LVILatticeVal Val) {
301   if (Val.isConstantRange() &&
302       Val.getConstantRange().isSingleElement())
303     // Integer constants are single element ranges
304     return true;
305   if (Val.isConstant())
306     // Non integer constants
307     return true;
308   return false;
309 }
310 
311 /// Combine two sets of facts about the same value into a single set of
312 /// facts.  Note that this method is not suitable for merging facts along
313 /// different paths in a CFG; that's what the mergeIn function is for.  This
314 /// is for merging facts gathered about the same value at the same location
315 /// through two independent means.
316 /// Notes:
317 /// * This method does not promise to return the most precise possible lattice
318 ///   value implied by A and B.  It is allowed to return any lattice element
319 ///   which is at least as strong as *either* A or B (unless our facts
320 ///   conflict, see below).
321 /// * Due to unreachable code, the intersection of two lattice values could be
322 ///   contradictory.  If this happens, we return some valid lattice value so as
323 ///   not confuse the rest of LVI.  Ideally, we'd always return Undefined, but
324 ///   we do not make this guarantee.  TODO: This would be a useful enhancement.
325 static LVILatticeVal intersect(LVILatticeVal A, LVILatticeVal B) {
326   // Undefined is the strongest state.  It means the value is known to be along
327   // an unreachable path.
328   if (A.isUndefined())
329     return A;
330   if (B.isUndefined())
331     return B;
332 
333   // If we gave up for one, but got a useable fact from the other, use it.
334   if (A.isOverdefined())
335     return B;
336   if (B.isOverdefined())
337     return A;
338 
339   // Can't get any more precise than constants.
340   if (hasSingleValue(A))
341     return A;
342   if (hasSingleValue(B))
343     return B;
344 
345   // Could be either constant range or not constant here.
346   if (!A.isConstantRange() || !B.isConstantRange()) {
347     // TODO: Arbitrary choice, could be improved
348     return A;
349   }
350 
351   // Intersect two constant ranges
352   ConstantRange Range =
353     A.getConstantRange().intersectWith(B.getConstantRange());
354   // Note: An empty range is implicitly converted to overdefined internally.
355   // TODO: We could instead use Undefined here since we've proven a conflict
356   // and thus know this path must be unreachable.
357   return LVILatticeVal::getRange(std::move(Range));
358 }
359 
360 //===----------------------------------------------------------------------===//
361 //                          LazyValueInfoCache Decl
362 //===----------------------------------------------------------------------===//
363 
364 namespace {
365   /// A callback value handle updates the cache when values are erased.
366   class LazyValueInfoCache;
367   struct LVIValueHandle final : public CallbackVH {
368     LazyValueInfoCache *Parent;
369 
370     LVIValueHandle(Value *V, LazyValueInfoCache *P)
371       : CallbackVH(V), Parent(P) { }
372 
373     void deleted() override;
374     void allUsesReplacedWith(Value *V) override {
375       deleted();
376     }
377   };
378 }
379 
380 namespace {
381   /// This is the cache kept by LazyValueInfo which
382   /// maintains information about queries across the clients' queries.
383   class LazyValueInfoCache {
384     /// This is all of the cached block information for exactly one Value*.
385     /// The entries are sorted by the BasicBlock* of the
386     /// entries, allowing us to do a lookup with a binary search.
387     /// Over-defined lattice values are recorded in OverDefinedCache to reduce
388     /// memory overhead.
389     typedef SmallDenseMap<AssertingVH<BasicBlock>, LVILatticeVal, 4>
390         ValueCacheEntryTy;
391 
392     /// This is all of the cached information for all values,
393     /// mapped from Value* to key information.
394     std::map<LVIValueHandle, ValueCacheEntryTy> ValueCache;
395 
396     /// This tracks, on a per-block basis, the set of values that are
397     /// over-defined at the end of that block.
398     typedef DenseMap<AssertingVH<BasicBlock>, SmallPtrSet<Value *, 4>>
399         OverDefinedCacheTy;
400     OverDefinedCacheTy OverDefinedCache;
401 
402     /// Keep track of all blocks that we have ever seen, so we
403     /// don't spend time removing unused blocks from our caches.
404     DenseSet<AssertingVH<BasicBlock> > SeenBlocks;
405 
406     /// This stack holds the state of the value solver during a query.
407     /// It basically emulates the callstack of the naive
408     /// recursive value lookup process.
409     std::stack<std::pair<BasicBlock*, Value*> > BlockValueStack;
410 
411     /// Keeps track of which block-value pairs are in BlockValueStack.
412     DenseSet<std::pair<BasicBlock*, Value*> > BlockValueSet;
413 
414     /// Push BV onto BlockValueStack unless it's already in there.
415     /// Returns true on success.
416     bool pushBlockValue(const std::pair<BasicBlock *, Value *> &BV) {
417       if (!BlockValueSet.insert(BV).second)
418         return false;  // It's already in the stack.
419 
420       DEBUG(dbgs() << "PUSH: " << *BV.second << " in " << BV.first->getName()
421                    << "\n");
422       BlockValueStack.push(BV);
423       return true;
424     }
425 
426     AssumptionCache *AC;  ///< A pointer to the cache of @llvm.assume calls.
427     const DataLayout &DL; ///< A mandatory DataLayout
428     DominatorTree *DT;    ///< An optional DT pointer.
429 
430     friend struct LVIValueHandle;
431 
432     void insertResult(Value *Val, BasicBlock *BB, const LVILatticeVal &Result) {
433       SeenBlocks.insert(BB);
434 
435       // Insert over-defined values into their own cache to reduce memory
436       // overhead.
437       if (Result.isOverdefined())
438         OverDefinedCache[BB].insert(Val);
439       else
440         lookup(Val)[BB] = Result;
441     }
442 
443     LVILatticeVal getBlockValue(Value *Val, BasicBlock *BB);
444     bool getEdgeValue(Value *V, BasicBlock *F, BasicBlock *T,
445                       LVILatticeVal &Result,
446                       Instruction *CxtI = nullptr);
447     bool hasBlockValue(Value *Val, BasicBlock *BB);
448 
449     // These methods process one work item and may add more. A false value
450     // returned means that the work item was not completely processed and must
451     // be revisited after going through the new items.
452     bool solveBlockValue(Value *Val, BasicBlock *BB);
453     bool solveBlockValueNonLocal(LVILatticeVal &BBLV,
454                                  Value *Val, BasicBlock *BB);
455     bool solveBlockValuePHINode(LVILatticeVal &BBLV,
456                                 PHINode *PN, BasicBlock *BB);
457     bool solveBlockValueSelect(LVILatticeVal &BBLV,
458                                SelectInst *S, BasicBlock *BB);
459     bool solveBlockValueBinaryOp(LVILatticeVal &BBLV,
460                               Instruction *BBI, BasicBlock *BB);
461     bool solveBlockValueCast(LVILatticeVal &BBLV,
462                              Instruction *BBI, BasicBlock *BB);
463    void intersectAssumeBlockValueConstantRange(Value *Val, LVILatticeVal &BBLV,
464                                             Instruction *BBI);
465 
466     void solve();
467 
468     ValueCacheEntryTy &lookup(Value *V) {
469       return ValueCache[LVIValueHandle(V, this)];
470     }
471 
472     bool isOverdefined(Value *V, BasicBlock *BB) const {
473       auto ODI = OverDefinedCache.find(BB);
474 
475       if (ODI == OverDefinedCache.end())
476         return false;
477 
478       return ODI->second.count(V);
479     }
480 
481     bool hasCachedValueInfo(Value *V, BasicBlock *BB) {
482       if (isOverdefined(V, BB))
483         return true;
484 
485       LVIValueHandle ValHandle(V, this);
486       auto I = ValueCache.find(ValHandle);
487       if (I == ValueCache.end())
488         return false;
489 
490       return I->second.count(BB);
491     }
492 
493     LVILatticeVal getCachedValueInfo(Value *V, BasicBlock *BB) {
494       if (isOverdefined(V, BB))
495         return LVILatticeVal::getOverdefined();
496 
497       return lookup(V)[BB];
498     }
499 
500   public:
501     /// This is the query interface to determine the lattice
502     /// value for the specified Value* at the end of the specified block.
503     LVILatticeVal getValueInBlock(Value *V, BasicBlock *BB,
504                                   Instruction *CxtI = nullptr);
505 
506     /// This is the query interface to determine the lattice
507     /// value for the specified Value* at the specified instruction (generally
508     /// from an assume intrinsic).
509     LVILatticeVal getValueAt(Value *V, Instruction *CxtI);
510 
511     /// This is the query interface to determine the lattice
512     /// value for the specified Value* that is true on the specified edge.
513     LVILatticeVal getValueOnEdge(Value *V, BasicBlock *FromBB,BasicBlock *ToBB,
514                                  Instruction *CxtI = nullptr);
515 
516     /// This is the update interface to inform the cache that an edge from
517     /// PredBB to OldSucc has been threaded to be from PredBB to NewSucc.
518     void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
519 
520     /// This is part of the update interface to inform the cache
521     /// that a block has been deleted.
522     void eraseBlock(BasicBlock *BB);
523 
524     /// clear - Empty the cache.
525     void clear() {
526       SeenBlocks.clear();
527       ValueCache.clear();
528       OverDefinedCache.clear();
529     }
530 
531     LazyValueInfoCache(AssumptionCache *AC, const DataLayout &DL,
532                        DominatorTree *DT = nullptr)
533         : AC(AC), DL(DL), DT(DT) {}
534   };
535 } // end anonymous namespace
536 
537 void LVIValueHandle::deleted() {
538   SmallVector<AssertingVH<BasicBlock>, 4> ToErase;
539   for (auto &I : Parent->OverDefinedCache) {
540     SmallPtrSetImpl<Value *> &ValueSet = I.second;
541     if (ValueSet.count(getValPtr()))
542       ValueSet.erase(getValPtr());
543     if (ValueSet.empty())
544       ToErase.push_back(I.first);
545   }
546   for (auto &BB : ToErase)
547     Parent->OverDefinedCache.erase(BB);
548 
549   // This erasure deallocates *this, so it MUST happen after we're done
550   // using any and all members of *this.
551   Parent->ValueCache.erase(*this);
552 }
553 
554 void LazyValueInfoCache::eraseBlock(BasicBlock *BB) {
555   // Shortcut if we have never seen this block.
556   DenseSet<AssertingVH<BasicBlock> >::iterator I = SeenBlocks.find(BB);
557   if (I == SeenBlocks.end())
558     return;
559   SeenBlocks.erase(I);
560 
561   auto ODI = OverDefinedCache.find(BB);
562   if (ODI != OverDefinedCache.end())
563     OverDefinedCache.erase(ODI);
564 
565   for (auto I = ValueCache.begin(), E = ValueCache.end(); I != E; ++I)
566     I->second.erase(BB);
567 }
568 
569 void LazyValueInfoCache::solve() {
570   while (!BlockValueStack.empty()) {
571     std::pair<BasicBlock*, Value*> &e = BlockValueStack.top();
572     assert(BlockValueSet.count(e) && "Stack value should be in BlockValueSet!");
573 
574     if (solveBlockValue(e.second, e.first)) {
575       // The work item was completely processed.
576       assert(BlockValueStack.top() == e && "Nothing should have been pushed!");
577       assert(hasCachedValueInfo(e.second, e.first) &&
578              "Result should be in cache!");
579 
580       DEBUG(dbgs() << "POP " << *e.second << " in " << e.first->getName()
581                    << " = " << getCachedValueInfo(e.second, e.first) << "\n");
582 
583       BlockValueStack.pop();
584       BlockValueSet.erase(e);
585     } else {
586       // More work needs to be done before revisiting.
587       assert(BlockValueStack.top() != e && "Stack should have been pushed!");
588     }
589   }
590 }
591 
592 bool LazyValueInfoCache::hasBlockValue(Value *Val, BasicBlock *BB) {
593   // If already a constant, there is nothing to compute.
594   if (isa<Constant>(Val))
595     return true;
596 
597   return hasCachedValueInfo(Val, BB);
598 }
599 
600 LVILatticeVal LazyValueInfoCache::getBlockValue(Value *Val, BasicBlock *BB) {
601   // If already a constant, there is nothing to compute.
602   if (Constant *VC = dyn_cast<Constant>(Val))
603     return LVILatticeVal::get(VC);
604 
605   SeenBlocks.insert(BB);
606   return getCachedValueInfo(Val, BB);
607 }
608 
609 static LVILatticeVal getFromRangeMetadata(Instruction *BBI) {
610   switch (BBI->getOpcode()) {
611   default: break;
612   case Instruction::Load:
613   case Instruction::Call:
614   case Instruction::Invoke:
615     if (MDNode *Ranges = BBI->getMetadata(LLVMContext::MD_range))
616       if (isa<IntegerType>(BBI->getType())) {
617         return LVILatticeVal::getRange(getConstantRangeFromMetadata(*Ranges));
618       }
619     break;
620   };
621   // Nothing known - will be intersected with other facts
622   return LVILatticeVal::getOverdefined();
623 }
624 
625 bool LazyValueInfoCache::solveBlockValue(Value *Val, BasicBlock *BB) {
626   if (isa<Constant>(Val))
627     return true;
628 
629   if (hasCachedValueInfo(Val, BB)) {
630     // If we have a cached value, use that.
631     DEBUG(dbgs() << "  reuse BB '" << BB->getName()
632                  << "' val=" << getCachedValueInfo(Val, BB) << '\n');
633 
634     // Since we're reusing a cached value, we don't need to update the
635     // OverDefinedCache. The cache will have been properly updated whenever the
636     // cached value was inserted.
637     return true;
638   }
639 
640   // Hold off inserting this value into the Cache in case we have to return
641   // false and come back later.
642   LVILatticeVal Res;
643 
644   Instruction *BBI = dyn_cast<Instruction>(Val);
645   if (!BBI || BBI->getParent() != BB) {
646     if (!solveBlockValueNonLocal(Res, Val, BB))
647       return false;
648    insertResult(Val, BB, Res);
649    return true;
650   }
651 
652   if (PHINode *PN = dyn_cast<PHINode>(BBI)) {
653     if (!solveBlockValuePHINode(Res, PN, BB))
654       return false;
655     insertResult(Val, BB, Res);
656     return true;
657   }
658 
659   if (auto *SI = dyn_cast<SelectInst>(BBI)) {
660     if (!solveBlockValueSelect(Res, SI, BB))
661       return false;
662     insertResult(Val, BB, Res);
663     return true;
664   }
665 
666   // If this value is a nonnull pointer, record it's range and bailout.  Note
667   // that for all other pointer typed values, we terminate the search at the
668   // definition.  We could easily extend this to look through geps, bitcasts,
669   // and the like to prove non-nullness, but it's not clear that's worth it
670   // compile time wise.  The context-insensative value walk done inside
671   // isKnownNonNull gets most of the profitable cases at much less expense.
672   // This does mean that we have a sensativity to where the defining
673   // instruction is placed, even if it could legally be hoisted much higher.
674   // That is unfortunate.
675   PointerType *PT = dyn_cast<PointerType>(BBI->getType());
676   if (PT && isKnownNonNull(BBI)) {
677     Res = LVILatticeVal::getNot(ConstantPointerNull::get(PT));
678     insertResult(Val, BB, Res);
679     return true;
680   }
681   if (BBI->getType()->isIntegerTy()) {
682     if (isa<CastInst>(BBI)) {
683       if (!solveBlockValueCast(Res, BBI, BB))
684         return false;
685       insertResult(Val, BB, Res);
686       return true;
687     }
688     BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI);
689     if (BO && isa<ConstantInt>(BO->getOperand(1))) {
690       if (!solveBlockValueBinaryOp(Res, BBI, BB))
691         return false;
692       insertResult(Val, BB, Res);
693       return true;
694     }
695   }
696 
697   DEBUG(dbgs() << " compute BB '" << BB->getName()
698                  << "' - unknown inst def found.\n");
699   Res = getFromRangeMetadata(BBI);
700   insertResult(Val, BB, Res);
701   return true;
702 }
703 
704 static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr) {
705   if (LoadInst *L = dyn_cast<LoadInst>(I)) {
706     return L->getPointerAddressSpace() == 0 &&
707            GetUnderlyingObject(L->getPointerOperand(),
708                                L->getModule()->getDataLayout()) == Ptr;
709   }
710   if (StoreInst *S = dyn_cast<StoreInst>(I)) {
711     return S->getPointerAddressSpace() == 0 &&
712            GetUnderlyingObject(S->getPointerOperand(),
713                                S->getModule()->getDataLayout()) == Ptr;
714   }
715   if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
716     if (MI->isVolatile()) return false;
717 
718     // FIXME: check whether it has a valuerange that excludes zero?
719     ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength());
720     if (!Len || Len->isZero()) return false;
721 
722     if (MI->getDestAddressSpace() == 0)
723       if (GetUnderlyingObject(MI->getRawDest(),
724                               MI->getModule()->getDataLayout()) == Ptr)
725         return true;
726     if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))
727       if (MTI->getSourceAddressSpace() == 0)
728         if (GetUnderlyingObject(MTI->getRawSource(),
729                                 MTI->getModule()->getDataLayout()) == Ptr)
730           return true;
731   }
732   return false;
733 }
734 
735 /// Return true if the allocation associated with Val is ever dereferenced
736 /// within the given basic block.  This establishes the fact Val is not null,
737 /// but does not imply that the memory at Val is dereferenceable.  (Val may
738 /// point off the end of the dereferenceable part of the object.)
739 static bool isObjectDereferencedInBlock(Value *Val, BasicBlock *BB) {
740   assert(Val->getType()->isPointerTy());
741 
742   const DataLayout &DL = BB->getModule()->getDataLayout();
743   Value *UnderlyingVal = GetUnderlyingObject(Val, DL);
744   // If 'GetUnderlyingObject' didn't converge, skip it. It won't converge
745   // inside InstructionDereferencesPointer either.
746   if (UnderlyingVal == GetUnderlyingObject(UnderlyingVal, DL, 1))
747     for (Instruction &I : *BB)
748       if (InstructionDereferencesPointer(&I, UnderlyingVal))
749         return true;
750   return false;
751 }
752 
753 bool LazyValueInfoCache::solveBlockValueNonLocal(LVILatticeVal &BBLV,
754                                                  Value *Val, BasicBlock *BB) {
755   LVILatticeVal Result;  // Start Undefined.
756 
757   // If this is the entry block, we must be asking about an argument.  The
758   // value is overdefined.
759   if (BB == &BB->getParent()->getEntryBlock()) {
760     assert(isa<Argument>(Val) && "Unknown live-in to the entry block");
761     // Bofore giving up, see if we can prove the pointer non-null local to
762     // this particular block.
763     if (Val->getType()->isPointerTy() &&
764         (isKnownNonNull(Val) || isObjectDereferencedInBlock(Val, BB))) {
765       PointerType *PTy = cast<PointerType>(Val->getType());
766       Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
767     } else {
768       Result.markOverdefined();
769     }
770     BBLV = Result;
771     return true;
772   }
773 
774   // Loop over all of our predecessors, merging what we know from them into
775   // result.
776   bool EdgesMissing = false;
777   for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
778     LVILatticeVal EdgeResult;
779     EdgesMissing |= !getEdgeValue(Val, *PI, BB, EdgeResult);
780     if (EdgesMissing)
781       continue;
782 
783     Result.mergeIn(EdgeResult, DL);
784 
785     // If we hit overdefined, exit early.  The BlockVals entry is already set
786     // to overdefined.
787     if (Result.isOverdefined()) {
788       DEBUG(dbgs() << " compute BB '" << BB->getName()
789             << "' - overdefined because of pred (non local).\n");
790       // Bofore giving up, see if we can prove the pointer non-null local to
791       // this particular block.
792       if (Val->getType()->isPointerTy() &&
793           isObjectDereferencedInBlock(Val, BB)) {
794         PointerType *PTy = cast<PointerType>(Val->getType());
795         Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
796       }
797 
798       BBLV = Result;
799       return true;
800     }
801   }
802   if (EdgesMissing)
803     return false;
804 
805   // Return the merged value, which is more precise than 'overdefined'.
806   assert(!Result.isOverdefined());
807   BBLV = Result;
808   return true;
809 }
810 
811 bool LazyValueInfoCache::solveBlockValuePHINode(LVILatticeVal &BBLV,
812                                                 PHINode *PN, BasicBlock *BB) {
813   LVILatticeVal Result;  // Start Undefined.
814 
815   // Loop over all of our predecessors, merging what we know from them into
816   // result.
817   bool EdgesMissing = false;
818   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
819     BasicBlock *PhiBB = PN->getIncomingBlock(i);
820     Value *PhiVal = PN->getIncomingValue(i);
821     LVILatticeVal EdgeResult;
822     // Note that we can provide PN as the context value to getEdgeValue, even
823     // though the results will be cached, because PN is the value being used as
824     // the cache key in the caller.
825     EdgesMissing |= !getEdgeValue(PhiVal, PhiBB, BB, EdgeResult, PN);
826     if (EdgesMissing)
827       continue;
828 
829     Result.mergeIn(EdgeResult, DL);
830 
831     // If we hit overdefined, exit early.  The BlockVals entry is already set
832     // to overdefined.
833     if (Result.isOverdefined()) {
834       DEBUG(dbgs() << " compute BB '" << BB->getName()
835             << "' - overdefined because of pred (local).\n");
836 
837       BBLV = Result;
838       return true;
839     }
840   }
841   if (EdgesMissing)
842     return false;
843 
844   // Return the merged value, which is more precise than 'overdefined'.
845   assert(!Result.isOverdefined() && "Possible PHI in entry block?");
846   BBLV = Result;
847   return true;
848 }
849 
850 static bool getValueFromFromCondition(Value *Val, ICmpInst *ICI,
851                                       LVILatticeVal &Result,
852                                       bool isTrueDest = true);
853 
854 // If we can determine a constraint on the value given conditions assumed by
855 // the program, intersect those constraints with BBLV
856 void LazyValueInfoCache::intersectAssumeBlockValueConstantRange(Value *Val,
857                                                             LVILatticeVal &BBLV,
858                                                             Instruction *BBI) {
859   BBI = BBI ? BBI : dyn_cast<Instruction>(Val);
860   if (!BBI)
861     return;
862 
863   for (auto &AssumeVH : AC->assumptions()) {
864     if (!AssumeVH)
865       continue;
866     auto *I = cast<CallInst>(AssumeVH);
867     if (!isValidAssumeForContext(I, BBI, DT))
868       continue;
869 
870     Value *C = I->getArgOperand(0);
871     if (ICmpInst *ICI = dyn_cast<ICmpInst>(C)) {
872       LVILatticeVal Result;
873       if (getValueFromFromCondition(Val, ICI, Result))
874         BBLV = intersect(BBLV, Result);
875     }
876   }
877 }
878 
879 bool LazyValueInfoCache::solveBlockValueSelect(LVILatticeVal &BBLV,
880                                                SelectInst *SI, BasicBlock *BB) {
881 
882   // Recurse on our inputs if needed
883   if (!hasBlockValue(SI->getTrueValue(), BB)) {
884     if (pushBlockValue(std::make_pair(BB, SI->getTrueValue())))
885       return false;
886     BBLV.markOverdefined();
887     return true;
888   }
889   LVILatticeVal TrueVal = getBlockValue(SI->getTrueValue(), BB);
890   // If we hit overdefined, don't ask more queries.  We want to avoid poisoning
891   // extra slots in the table if we can.
892   if (TrueVal.isOverdefined()) {
893     BBLV.markOverdefined();
894     return true;
895   }
896 
897   if (!hasBlockValue(SI->getFalseValue(), BB)) {
898     if (pushBlockValue(std::make_pair(BB, SI->getFalseValue())))
899       return false;
900     BBLV.markOverdefined();
901     return true;
902   }
903   LVILatticeVal FalseVal = getBlockValue(SI->getFalseValue(), BB);
904   // If we hit overdefined, don't ask more queries.  We want to avoid poisoning
905   // extra slots in the table if we can.
906   if (FalseVal.isOverdefined()) {
907     BBLV.markOverdefined();
908     return true;
909   }
910 
911   if (TrueVal.isConstantRange() && FalseVal.isConstantRange()) {
912     ConstantRange TrueCR = TrueVal.getConstantRange();
913     ConstantRange FalseCR = FalseVal.getConstantRange();
914     Value *LHS = nullptr;
915     Value *RHS = nullptr;
916     SelectPatternResult SPR = matchSelectPattern(SI, LHS, RHS);
917     // Is this a min specifically of our two inputs?  (Avoid the risk of
918     // ValueTracking getting smarter looking back past our immediate inputs.)
919     if (SelectPatternResult::isMinOrMax(SPR.Flavor) &&
920         LHS == SI->getTrueValue() && RHS == SI->getFalseValue()) {
921       switch (SPR.Flavor) {
922       default:
923         llvm_unreachable("unexpected minmax type!");
924       case SPF_SMIN:                   /// Signed minimum
925         BBLV.markConstantRange(TrueCR.smin(FalseCR));
926         return true;
927       case SPF_UMIN:                   /// Unsigned minimum
928         BBLV.markConstantRange(TrueCR.umin(FalseCR));
929         return true;
930       case SPF_SMAX:                   /// Signed maximum
931         BBLV.markConstantRange(TrueCR.smax(FalseCR));
932         return true;
933       case SPF_UMAX:                   /// Unsigned maximum
934         BBLV.markConstantRange(TrueCR.umax(FalseCR));
935         return true;
936       };
937     }
938 
939     // TODO: ABS, NABS from the SelectPatternResult
940   }
941 
942   // Can we constrain the facts about the true and false values by using the
943   // condition itself?  This shows up with idioms like e.g. select(a > 5, a, 5).
944   // TODO: We could potentially refine an overdefined true value above.
945   if (auto *ICI = dyn_cast<ICmpInst>(SI->getCondition())) {
946     LVILatticeVal TrueValTaken, FalseValTaken;
947     if (!getValueFromFromCondition(SI->getTrueValue(), ICI,
948                                    TrueValTaken, true))
949       TrueValTaken.markOverdefined();
950     if (!getValueFromFromCondition(SI->getFalseValue(), ICI,
951                                    FalseValTaken, false))
952       FalseValTaken.markOverdefined();
953 
954     TrueVal = intersect(TrueVal, TrueValTaken);
955     FalseVal = intersect(FalseVal, FalseValTaken);
956 
957 
958     // Handle clamp idioms such as:
959     //   %24 = constantrange<0, 17>
960     //   %39 = icmp eq i32 %24, 0
961     //   %40 = add i32 %24, -1
962     //   %siv.next = select i1 %39, i32 16, i32 %40
963     //   %siv.next = constantrange<0, 17> not <-1, 17>
964     // In general, this can handle any clamp idiom which tests the edge
965     // condition via an equality or inequality.
966     ICmpInst::Predicate Pred = ICI->getPredicate();
967     Value *A = ICI->getOperand(0);
968     if (ConstantInt *CIBase = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
969       auto addConstants = [](ConstantInt *A, ConstantInt *B) {
970         assert(A->getType() == B->getType());
971         return ConstantInt::get(A->getType(), A->getValue() + B->getValue());
972       };
973       // See if either input is A + C2, subject to the constraint from the
974       // condition that A != C when that input is used.  We can assume that
975       // that input doesn't include C + C2.
976       ConstantInt *CIAdded;
977       switch (Pred) {
978       default: break;
979       case ICmpInst::ICMP_EQ:
980         if (match(SI->getFalseValue(), m_Add(m_Specific(A),
981                                              m_ConstantInt(CIAdded)))) {
982           auto ResNot = addConstants(CIBase, CIAdded);
983           FalseVal = intersect(FalseVal,
984                                LVILatticeVal::getNot(ResNot));
985         }
986         break;
987       case ICmpInst::ICMP_NE:
988         if (match(SI->getTrueValue(), m_Add(m_Specific(A),
989                                             m_ConstantInt(CIAdded)))) {
990           auto ResNot = addConstants(CIBase, CIAdded);
991           TrueVal = intersect(TrueVal,
992                               LVILatticeVal::getNot(ResNot));
993         }
994         break;
995       };
996     }
997   }
998 
999   LVILatticeVal Result;  // Start Undefined.
1000   Result.mergeIn(TrueVal, DL);
1001   Result.mergeIn(FalseVal, DL);
1002   BBLV = Result;
1003   return true;
1004 }
1005 
1006 bool LazyValueInfoCache::solveBlockValueCast(LVILatticeVal &BBLV,
1007                                              Instruction *BBI,
1008                                              BasicBlock *BB) {
1009   if (!BBI->getOperand(0)->getType()->isSized()) {
1010     // Without knowing how wide the input is, we can't analyze it in any useful
1011     // way.
1012     BBLV.markOverdefined();
1013     return true;
1014   }
1015 
1016   // Filter out casts we don't know how to reason about before attempting to
1017   // recurse on our operand.  This can cut a long search short if we know we're
1018   // not going to be able to get any useful information anways.
1019   switch (BBI->getOpcode()) {
1020   case Instruction::Trunc:
1021   case Instruction::SExt:
1022   case Instruction::ZExt:
1023   case Instruction::BitCast:
1024     break;
1025   default:
1026     // Unhandled instructions are overdefined.
1027     DEBUG(dbgs() << " compute BB '" << BB->getName()
1028                  << "' - overdefined (unknown cast).\n");
1029     BBLV.markOverdefined();
1030     return true;
1031   }
1032 
1033 
1034   // Figure out the range of the LHS.  If that fails, we still apply the
1035   // transfer rule on the full set since we may be able to locally infer
1036   // interesting facts.
1037   if (!hasBlockValue(BBI->getOperand(0), BB))
1038     if (pushBlockValue(std::make_pair(BB, BBI->getOperand(0))))
1039       // More work to do before applying this transfer rule.
1040       return false;
1041 
1042   const unsigned OperandBitWidth =
1043     DL.getTypeSizeInBits(BBI->getOperand(0)->getType());
1044   ConstantRange LHSRange = ConstantRange(OperandBitWidth);
1045   if (hasBlockValue(BBI->getOperand(0), BB)) {
1046     LVILatticeVal LHSVal = getBlockValue(BBI->getOperand(0), BB);
1047     intersectAssumeBlockValueConstantRange(BBI->getOperand(0), LHSVal, BBI);
1048     if (LHSVal.isConstantRange())
1049       LHSRange = LHSVal.getConstantRange();
1050   }
1051 
1052   const unsigned ResultBitWidth =
1053     cast<IntegerType>(BBI->getType())->getBitWidth();
1054 
1055   // NOTE: We're currently limited by the set of operations that ConstantRange
1056   // can evaluate symbolically.  Enhancing that set will allows us to analyze
1057   // more definitions.
1058   LVILatticeVal Result;
1059   switch (BBI->getOpcode()) {
1060   case Instruction::Trunc:
1061     Result.markConstantRange(LHSRange.truncate(ResultBitWidth));
1062     break;
1063   case Instruction::SExt:
1064     Result.markConstantRange(LHSRange.signExtend(ResultBitWidth));
1065     break;
1066   case Instruction::ZExt:
1067     Result.markConstantRange(LHSRange.zeroExtend(ResultBitWidth));
1068     break;
1069   case Instruction::BitCast:
1070     Result.markConstantRange(LHSRange);
1071     break;
1072   default:
1073     // Should be dead if the code above is correct
1074     llvm_unreachable("inconsistent with above");
1075     break;
1076   }
1077 
1078   BBLV = Result;
1079   return true;
1080 }
1081 
1082 bool LazyValueInfoCache::solveBlockValueBinaryOp(LVILatticeVal &BBLV,
1083                                                  Instruction *BBI,
1084                                                  BasicBlock *BB) {
1085 
1086   assert(BBI->getOperand(0)->getType()->isSized() &&
1087          "all operands to binary operators are sized");
1088 
1089   // Filter out operators we don't know how to reason about before attempting to
1090   // recurse on our operand(s).  This can cut a long search short if we know
1091   // we're not going to be able to get any useful information anways.
1092   switch (BBI->getOpcode()) {
1093   case Instruction::Add:
1094   case Instruction::Sub:
1095   case Instruction::Mul:
1096   case Instruction::UDiv:
1097   case Instruction::Shl:
1098   case Instruction::LShr:
1099   case Instruction::And:
1100   case Instruction::Or:
1101     // continue into the code below
1102     break;
1103   default:
1104     // Unhandled instructions are overdefined.
1105     DEBUG(dbgs() << " compute BB '" << BB->getName()
1106                  << "' - overdefined (unknown binary operator).\n");
1107     BBLV.markOverdefined();
1108     return true;
1109   };
1110 
1111   // Figure out the range of the LHS.  If that fails, use a conservative range,
1112   // but apply the transfer rule anyways.  This lets us pick up facts from
1113   // expressions like "and i32 (call i32 @foo()), 32"
1114   if (!hasBlockValue(BBI->getOperand(0), BB))
1115     if (pushBlockValue(std::make_pair(BB, BBI->getOperand(0))))
1116       // More work to do before applying this transfer rule.
1117       return false;
1118 
1119   const unsigned OperandBitWidth =
1120     DL.getTypeSizeInBits(BBI->getOperand(0)->getType());
1121   ConstantRange LHSRange = ConstantRange(OperandBitWidth);
1122   if (hasBlockValue(BBI->getOperand(0), BB)) {
1123     LVILatticeVal LHSVal = getBlockValue(BBI->getOperand(0), BB);
1124     intersectAssumeBlockValueConstantRange(BBI->getOperand(0), LHSVal, BBI);
1125     if (LHSVal.isConstantRange())
1126       LHSRange = LHSVal.getConstantRange();
1127   }
1128 
1129   ConstantInt *RHS = cast<ConstantInt>(BBI->getOperand(1));
1130   ConstantRange RHSRange = ConstantRange(RHS->getValue());
1131 
1132   // NOTE: We're currently limited by the set of operations that ConstantRange
1133   // can evaluate symbolically.  Enhancing that set will allows us to analyze
1134   // more definitions.
1135   LVILatticeVal Result;
1136   switch (BBI->getOpcode()) {
1137   case Instruction::Add:
1138     Result.markConstantRange(LHSRange.add(RHSRange));
1139     break;
1140   case Instruction::Sub:
1141     Result.markConstantRange(LHSRange.sub(RHSRange));
1142     break;
1143   case Instruction::Mul:
1144     Result.markConstantRange(LHSRange.multiply(RHSRange));
1145     break;
1146   case Instruction::UDiv:
1147     Result.markConstantRange(LHSRange.udiv(RHSRange));
1148     break;
1149   case Instruction::Shl:
1150     Result.markConstantRange(LHSRange.shl(RHSRange));
1151     break;
1152   case Instruction::LShr:
1153     Result.markConstantRange(LHSRange.lshr(RHSRange));
1154     break;
1155   case Instruction::And:
1156     Result.markConstantRange(LHSRange.binaryAnd(RHSRange));
1157     break;
1158   case Instruction::Or:
1159     Result.markConstantRange(LHSRange.binaryOr(RHSRange));
1160     break;
1161   default:
1162     // Should be dead if the code above is correct
1163     llvm_unreachable("inconsistent with above");
1164     break;
1165   }
1166 
1167   BBLV = Result;
1168   return true;
1169 }
1170 
1171 bool getValueFromFromCondition(Value *Val, ICmpInst *ICI,
1172                                LVILatticeVal &Result, bool isTrueDest) {
1173   assert(ICI && "precondition");
1174   if (isa<Constant>(ICI->getOperand(1))) {
1175     if (ICI->isEquality() && ICI->getOperand(0) == Val) {
1176       // We know that V has the RHS constant if this is a true SETEQ or
1177       // false SETNE.
1178       if (isTrueDest == (ICI->getPredicate() == ICmpInst::ICMP_EQ))
1179         Result = LVILatticeVal::get(cast<Constant>(ICI->getOperand(1)));
1180       else
1181         Result = LVILatticeVal::getNot(cast<Constant>(ICI->getOperand(1)));
1182       return true;
1183     }
1184 
1185     // Recognize the range checking idiom that InstCombine produces.
1186     // (X-C1) u< C2 --> [C1, C1+C2)
1187     ConstantInt *NegOffset = nullptr;
1188     if (ICI->getPredicate() == ICmpInst::ICMP_ULT)
1189       match(ICI->getOperand(0), m_Add(m_Specific(Val),
1190                                       m_ConstantInt(NegOffset)));
1191 
1192     ConstantInt *CI = dyn_cast<ConstantInt>(ICI->getOperand(1));
1193     if (CI && (ICI->getOperand(0) == Val || NegOffset)) {
1194       // Calculate the range of values that are allowed by the comparison
1195       ConstantRange CmpRange(CI->getValue());
1196       ConstantRange TrueValues =
1197           ConstantRange::makeAllowedICmpRegion(ICI->getPredicate(), CmpRange);
1198 
1199       if (NegOffset) // Apply the offset from above.
1200         TrueValues = TrueValues.subtract(NegOffset->getValue());
1201 
1202       // If we're interested in the false dest, invert the condition.
1203       if (!isTrueDest) TrueValues = TrueValues.inverse();
1204 
1205       Result = LVILatticeVal::getRange(std::move(TrueValues));
1206       return true;
1207     }
1208   }
1209 
1210   return false;
1211 }
1212 
1213 /// \brief Compute the value of Val on the edge BBFrom -> BBTo. Returns false if
1214 /// Val is not constrained on the edge.  Result is unspecified if return value
1215 /// is false.
1216 static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom,
1217                               BasicBlock *BBTo, LVILatticeVal &Result) {
1218   // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
1219   // know that v != 0.
1220   if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {
1221     // If this is a conditional branch and only one successor goes to BBTo, then
1222     // we may be able to infer something from the condition.
1223     if (BI->isConditional() &&
1224         BI->getSuccessor(0) != BI->getSuccessor(1)) {
1225       bool isTrueDest = BI->getSuccessor(0) == BBTo;
1226       assert(BI->getSuccessor(!isTrueDest) == BBTo &&
1227              "BBTo isn't a successor of BBFrom");
1228 
1229       // If V is the condition of the branch itself, then we know exactly what
1230       // it is.
1231       if (BI->getCondition() == Val) {
1232         Result = LVILatticeVal::get(ConstantInt::get(
1233                               Type::getInt1Ty(Val->getContext()), isTrueDest));
1234         return true;
1235       }
1236 
1237       // If the condition of the branch is an equality comparison, we may be
1238       // able to infer the value.
1239       if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition()))
1240         if (getValueFromFromCondition(Val, ICI, Result, isTrueDest))
1241           return true;
1242     }
1243   }
1244 
1245   // If the edge was formed by a switch on the value, then we may know exactly
1246   // what it is.
1247   if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {
1248     if (SI->getCondition() != Val)
1249       return false;
1250 
1251     bool DefaultCase = SI->getDefaultDest() == BBTo;
1252     unsigned BitWidth = Val->getType()->getIntegerBitWidth();
1253     ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/);
1254 
1255     for (SwitchInst::CaseIt i : SI->cases()) {
1256       ConstantRange EdgeVal(i.getCaseValue()->getValue());
1257       if (DefaultCase) {
1258         // It is possible that the default destination is the destination of
1259         // some cases. There is no need to perform difference for those cases.
1260         if (i.getCaseSuccessor() != BBTo)
1261           EdgesVals = EdgesVals.difference(EdgeVal);
1262       } else if (i.getCaseSuccessor() == BBTo)
1263         EdgesVals = EdgesVals.unionWith(EdgeVal);
1264     }
1265     Result = LVILatticeVal::getRange(std::move(EdgesVals));
1266     return true;
1267   }
1268   return false;
1269 }
1270 
1271 /// \brief Compute the value of Val on the edge BBFrom -> BBTo or the value at
1272 /// the basic block if the edge does not constrain Val.
1273 bool LazyValueInfoCache::getEdgeValue(Value *Val, BasicBlock *BBFrom,
1274                                       BasicBlock *BBTo, LVILatticeVal &Result,
1275                                       Instruction *CxtI) {
1276   // If already a constant, there is nothing to compute.
1277   if (Constant *VC = dyn_cast<Constant>(Val)) {
1278     Result = LVILatticeVal::get(VC);
1279     return true;
1280   }
1281 
1282   LVILatticeVal LocalResult;
1283   if (!getEdgeValueLocal(Val, BBFrom, BBTo, LocalResult))
1284     // If we couldn't constrain the value on the edge, LocalResult doesn't
1285     // provide any information.
1286     LocalResult.markOverdefined();
1287 
1288   if (hasSingleValue(LocalResult)) {
1289     // Can't get any more precise here
1290     Result = LocalResult;
1291     return true;
1292   }
1293 
1294   if (!hasBlockValue(Val, BBFrom)) {
1295     if (pushBlockValue(std::make_pair(BBFrom, Val)))
1296       return false;
1297     // No new information.
1298     Result = LocalResult;
1299     return true;
1300   }
1301 
1302   // Try to intersect ranges of the BB and the constraint on the edge.
1303   LVILatticeVal InBlock = getBlockValue(Val, BBFrom);
1304   intersectAssumeBlockValueConstantRange(Val, InBlock, BBFrom->getTerminator());
1305   // We can use the context instruction (generically the ultimate instruction
1306   // the calling pass is trying to simplify) here, even though the result of
1307   // this function is generally cached when called from the solve* functions
1308   // (and that cached result might be used with queries using a different
1309   // context instruction), because when this function is called from the solve*
1310   // functions, the context instruction is not provided. When called from
1311   // LazyValueInfoCache::getValueOnEdge, the context instruction is provided,
1312   // but then the result is not cached.
1313   intersectAssumeBlockValueConstantRange(Val, InBlock, CxtI);
1314 
1315   Result = intersect(LocalResult, InBlock);
1316   return true;
1317 }
1318 
1319 LVILatticeVal LazyValueInfoCache::getValueInBlock(Value *V, BasicBlock *BB,
1320                                                   Instruction *CxtI) {
1321   DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
1322         << BB->getName() << "'\n");
1323 
1324   assert(BlockValueStack.empty() && BlockValueSet.empty());
1325   if (!hasBlockValue(V, BB)) {
1326     pushBlockValue(std::make_pair(BB, V));
1327     solve();
1328   }
1329   LVILatticeVal Result = getBlockValue(V, BB);
1330   intersectAssumeBlockValueConstantRange(V, Result, CxtI);
1331 
1332   DEBUG(dbgs() << "  Result = " << Result << "\n");
1333   return Result;
1334 }
1335 
1336 LVILatticeVal LazyValueInfoCache::getValueAt(Value *V, Instruction *CxtI) {
1337   DEBUG(dbgs() << "LVI Getting value " << *V << " at '"
1338         << CxtI->getName() << "'\n");
1339 
1340   if (auto *C = dyn_cast<Constant>(V))
1341     return LVILatticeVal::get(C);
1342 
1343   LVILatticeVal Result = LVILatticeVal::getOverdefined();
1344   if (auto *I = dyn_cast<Instruction>(V))
1345     Result = getFromRangeMetadata(I);
1346   intersectAssumeBlockValueConstantRange(V, Result, CxtI);
1347 
1348   DEBUG(dbgs() << "  Result = " << Result << "\n");
1349   return Result;
1350 }
1351 
1352 LVILatticeVal LazyValueInfoCache::
1353 getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB,
1354                Instruction *CxtI) {
1355   DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
1356         << FromBB->getName() << "' to '" << ToBB->getName() << "'\n");
1357 
1358   LVILatticeVal Result;
1359   if (!getEdgeValue(V, FromBB, ToBB, Result, CxtI)) {
1360     solve();
1361     bool WasFastQuery = getEdgeValue(V, FromBB, ToBB, Result, CxtI);
1362     (void)WasFastQuery;
1363     assert(WasFastQuery && "More work to do after problem solved?");
1364   }
1365 
1366   DEBUG(dbgs() << "  Result = " << Result << "\n");
1367   return Result;
1368 }
1369 
1370 void LazyValueInfoCache::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
1371                                     BasicBlock *NewSucc) {
1372   // When an edge in the graph has been threaded, values that we could not
1373   // determine a value for before (i.e. were marked overdefined) may be
1374   // possible to solve now. We do NOT try to proactively update these values.
1375   // Instead, we clear their entries from the cache, and allow lazy updating to
1376   // recompute them when needed.
1377 
1378   // The updating process is fairly simple: we need to drop cached info
1379   // for all values that were marked overdefined in OldSucc, and for those same
1380   // values in any successor of OldSucc (except NewSucc) in which they were
1381   // also marked overdefined.
1382   std::vector<BasicBlock*> worklist;
1383   worklist.push_back(OldSucc);
1384 
1385   auto I = OverDefinedCache.find(OldSucc);
1386   if (I == OverDefinedCache.end())
1387     return; // Nothing to process here.
1388   SmallVector<Value *, 4> ValsToClear(I->second.begin(), I->second.end());
1389 
1390   // Use a worklist to perform a depth-first search of OldSucc's successors.
1391   // NOTE: We do not need a visited list since any blocks we have already
1392   // visited will have had their overdefined markers cleared already, and we
1393   // thus won't loop to their successors.
1394   while (!worklist.empty()) {
1395     BasicBlock *ToUpdate = worklist.back();
1396     worklist.pop_back();
1397 
1398     // Skip blocks only accessible through NewSucc.
1399     if (ToUpdate == NewSucc) continue;
1400 
1401     bool changed = false;
1402     for (Value *V : ValsToClear) {
1403       // If a value was marked overdefined in OldSucc, and is here too...
1404       auto OI = OverDefinedCache.find(ToUpdate);
1405       if (OI == OverDefinedCache.end())
1406         continue;
1407       SmallPtrSetImpl<Value *> &ValueSet = OI->second;
1408       if (!ValueSet.count(V))
1409         continue;
1410 
1411       ValueSet.erase(V);
1412       if (ValueSet.empty())
1413         OverDefinedCache.erase(OI);
1414 
1415       // If we removed anything, then we potentially need to update
1416       // blocks successors too.
1417       changed = true;
1418     }
1419 
1420     if (!changed) continue;
1421 
1422     worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate));
1423   }
1424 }
1425 
1426 //===----------------------------------------------------------------------===//
1427 //                            LazyValueInfo Impl
1428 //===----------------------------------------------------------------------===//
1429 
1430 /// This lazily constructs the LazyValueInfoCache.
1431 static LazyValueInfoCache &getCache(void *&PImpl, AssumptionCache *AC,
1432                                     const DataLayout *DL,
1433                                     DominatorTree *DT = nullptr) {
1434   if (!PImpl) {
1435     assert(DL && "getCache() called with a null DataLayout");
1436     PImpl = new LazyValueInfoCache(AC, *DL, DT);
1437   }
1438   return *static_cast<LazyValueInfoCache*>(PImpl);
1439 }
1440 
1441 bool LazyValueInfo::runOnFunction(Function &F) {
1442   AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
1443   const DataLayout &DL = F.getParent()->getDataLayout();
1444 
1445   DominatorTreeWrapperPass *DTWP =
1446       getAnalysisIfAvailable<DominatorTreeWrapperPass>();
1447   DT = DTWP ? &DTWP->getDomTree() : nullptr;
1448 
1449   TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
1450 
1451   if (PImpl)
1452     getCache(PImpl, AC, &DL, DT).clear();
1453 
1454   // Fully lazy.
1455   return false;
1456 }
1457 
1458 void LazyValueInfo::getAnalysisUsage(AnalysisUsage &AU) const {
1459   AU.setPreservesAll();
1460   AU.addRequired<AssumptionCacheTracker>();
1461   AU.addRequired<TargetLibraryInfoWrapperPass>();
1462 }
1463 
1464 void LazyValueInfo::releaseMemory() {
1465   // If the cache was allocated, free it.
1466   if (PImpl) {
1467     delete &getCache(PImpl, AC, nullptr);
1468     PImpl = nullptr;
1469   }
1470 }
1471 
1472 Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB,
1473                                      Instruction *CxtI) {
1474   const DataLayout &DL = BB->getModule()->getDataLayout();
1475   LVILatticeVal Result =
1476       getCache(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
1477 
1478   if (Result.isConstant())
1479     return Result.getConstant();
1480   if (Result.isConstantRange()) {
1481     ConstantRange CR = Result.getConstantRange();
1482     if (const APInt *SingleVal = CR.getSingleElement())
1483       return ConstantInt::get(V->getContext(), *SingleVal);
1484   }
1485   return nullptr;
1486 }
1487 
1488 ConstantRange LazyValueInfo::getConstantRange(Value *V, BasicBlock *BB,
1489 					      Instruction *CxtI) {
1490   assert(V->getType()->isIntegerTy());
1491   unsigned Width = V->getType()->getIntegerBitWidth();
1492   const DataLayout &DL = BB->getModule()->getDataLayout();
1493   LVILatticeVal Result =
1494       getCache(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
1495   assert(!Result.isConstant());
1496   if (Result.isUndefined())
1497     return ConstantRange(Width, /*isFullSet=*/false);
1498   if (Result.isConstantRange())
1499     return Result.getConstantRange();
1500   return ConstantRange(Width, /*isFullSet=*/true);
1501 }
1502 
1503 /// Determine whether the specified value is known to be a
1504 /// constant on the specified edge. Return null if not.
1505 Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB,
1506                                            BasicBlock *ToBB,
1507                                            Instruction *CxtI) {
1508   const DataLayout &DL = FromBB->getModule()->getDataLayout();
1509   LVILatticeVal Result =
1510       getCache(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
1511 
1512   if (Result.isConstant())
1513     return Result.getConstant();
1514   if (Result.isConstantRange()) {
1515     ConstantRange CR = Result.getConstantRange();
1516     if (const APInt *SingleVal = CR.getSingleElement())
1517       return ConstantInt::get(V->getContext(), *SingleVal);
1518   }
1519   return nullptr;
1520 }
1521 
1522 static LazyValueInfo::Tristate getPredicateResult(unsigned Pred, Constant *C,
1523                                                   LVILatticeVal &Result,
1524                                                   const DataLayout &DL,
1525                                                   TargetLibraryInfo *TLI) {
1526 
1527   // If we know the value is a constant, evaluate the conditional.
1528   Constant *Res = nullptr;
1529   if (Result.isConstant()) {
1530     Res = ConstantFoldCompareInstOperands(Pred, Result.getConstant(), C, DL,
1531                                           TLI);
1532     if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res))
1533       return ResCI->isZero() ? LazyValueInfo::False : LazyValueInfo::True;
1534     return LazyValueInfo::Unknown;
1535   }
1536 
1537   if (Result.isConstantRange()) {
1538     ConstantInt *CI = dyn_cast<ConstantInt>(C);
1539     if (!CI) return LazyValueInfo::Unknown;
1540 
1541     ConstantRange CR = Result.getConstantRange();
1542     if (Pred == ICmpInst::ICMP_EQ) {
1543       if (!CR.contains(CI->getValue()))
1544         return LazyValueInfo::False;
1545 
1546       if (CR.isSingleElement() && CR.contains(CI->getValue()))
1547         return LazyValueInfo::True;
1548     } else if (Pred == ICmpInst::ICMP_NE) {
1549       if (!CR.contains(CI->getValue()))
1550         return LazyValueInfo::True;
1551 
1552       if (CR.isSingleElement() && CR.contains(CI->getValue()))
1553         return LazyValueInfo::False;
1554     }
1555 
1556     // Handle more complex predicates.
1557     ConstantRange TrueValues =
1558         ICmpInst::makeConstantRange((ICmpInst::Predicate)Pred, CI->getValue());
1559     if (TrueValues.contains(CR))
1560       return LazyValueInfo::True;
1561     if (TrueValues.inverse().contains(CR))
1562       return LazyValueInfo::False;
1563     return LazyValueInfo::Unknown;
1564   }
1565 
1566   if (Result.isNotConstant()) {
1567     // If this is an equality comparison, we can try to fold it knowing that
1568     // "V != C1".
1569     if (Pred == ICmpInst::ICMP_EQ) {
1570       // !C1 == C -> false iff C1 == C.
1571       Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
1572                                             Result.getNotConstant(), C, DL,
1573                                             TLI);
1574       if (Res->isNullValue())
1575         return LazyValueInfo::False;
1576     } else if (Pred == ICmpInst::ICMP_NE) {
1577       // !C1 != C -> true iff C1 == C.
1578       Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
1579                                             Result.getNotConstant(), C, DL,
1580                                             TLI);
1581       if (Res->isNullValue())
1582         return LazyValueInfo::True;
1583     }
1584     return LazyValueInfo::Unknown;
1585   }
1586 
1587   return LazyValueInfo::Unknown;
1588 }
1589 
1590 /// Determine whether the specified value comparison with a constant is known to
1591 /// be true or false on the specified CFG edge. Pred is a CmpInst predicate.
1592 LazyValueInfo::Tristate
1593 LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C,
1594                                   BasicBlock *FromBB, BasicBlock *ToBB,
1595                                   Instruction *CxtI) {
1596   const DataLayout &DL = FromBB->getModule()->getDataLayout();
1597   LVILatticeVal Result =
1598       getCache(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
1599 
1600   return getPredicateResult(Pred, C, Result, DL, TLI);
1601 }
1602 
1603 LazyValueInfo::Tristate
1604 LazyValueInfo::getPredicateAt(unsigned Pred, Value *V, Constant *C,
1605                               Instruction *CxtI) {
1606   const DataLayout &DL = CxtI->getModule()->getDataLayout();
1607   LVILatticeVal Result = getCache(PImpl, AC, &DL, DT).getValueAt(V, CxtI);
1608   Tristate Ret = getPredicateResult(Pred, C, Result, DL, TLI);
1609   if (Ret != Unknown)
1610     return Ret;
1611 
1612   // Note: The following bit of code is somewhat distinct from the rest of LVI;
1613   // LVI as a whole tries to compute a lattice value which is conservatively
1614   // correct at a given location.  In this case, we have a predicate which we
1615   // weren't able to prove about the merged result, and we're pushing that
1616   // predicate back along each incoming edge to see if we can prove it
1617   // separately for each input.  As a motivating example, consider:
1618   // bb1:
1619   //   %v1 = ... ; constantrange<1, 5>
1620   //   br label %merge
1621   // bb2:
1622   //   %v2 = ... ; constantrange<10, 20>
1623   //   br label %merge
1624   // merge:
1625   //   %phi = phi [%v1, %v2] ; constantrange<1,20>
1626   //   %pred = icmp eq i32 %phi, 8
1627   // We can't tell from the lattice value for '%phi' that '%pred' is false
1628   // along each path, but by checking the predicate over each input separately,
1629   // we can.
1630   // We limit the search to one step backwards from the current BB and value.
1631   // We could consider extending this to search further backwards through the
1632   // CFG and/or value graph, but there are non-obvious compile time vs quality
1633   // tradeoffs.
1634   if (CxtI) {
1635     BasicBlock *BB = CxtI->getParent();
1636 
1637     // Function entry or an unreachable block.  Bail to avoid confusing
1638     // analysis below.
1639     pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1640     if (PI == PE)
1641       return Unknown;
1642 
1643     // If V is a PHI node in the same block as the context, we need to ask
1644     // questions about the predicate as applied to the incoming value along
1645     // each edge. This is useful for eliminating cases where the predicate is
1646     // known along all incoming edges.
1647     if (auto *PHI = dyn_cast<PHINode>(V))
1648       if (PHI->getParent() == BB) {
1649         Tristate Baseline = Unknown;
1650         for (unsigned i = 0, e = PHI->getNumIncomingValues(); i < e; i++) {
1651           Value *Incoming = PHI->getIncomingValue(i);
1652           BasicBlock *PredBB = PHI->getIncomingBlock(i);
1653           // Note that PredBB may be BB itself.
1654           Tristate Result = getPredicateOnEdge(Pred, Incoming, C, PredBB, BB,
1655                                                CxtI);
1656 
1657           // Keep going as long as we've seen a consistent known result for
1658           // all inputs.
1659           Baseline = (i == 0) ? Result /* First iteration */
1660             : (Baseline == Result ? Baseline : Unknown); /* All others */
1661           if (Baseline == Unknown)
1662             break;
1663         }
1664         if (Baseline != Unknown)
1665           return Baseline;
1666       }
1667 
1668     // For a comparison where the V is outside this block, it's possible
1669     // that we've branched on it before. Look to see if the value is known
1670     // on all incoming edges.
1671     if (!isa<Instruction>(V) ||
1672         cast<Instruction>(V)->getParent() != BB) {
1673       // For predecessor edge, determine if the comparison is true or false
1674       // on that edge. If they're all true or all false, we can conclude
1675       // the value of the comparison in this block.
1676       Tristate Baseline = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1677       if (Baseline != Unknown) {
1678         // Check that all remaining incoming values match the first one.
1679         while (++PI != PE) {
1680           Tristate Ret = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1681           if (Ret != Baseline) break;
1682         }
1683         // If we terminated early, then one of the values didn't match.
1684         if (PI == PE) {
1685           return Baseline;
1686         }
1687       }
1688     }
1689   }
1690   return Unknown;
1691 }
1692 
1693 void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
1694                                BasicBlock *NewSucc) {
1695   if (PImpl) {
1696     const DataLayout &DL = PredBB->getModule()->getDataLayout();
1697     getCache(PImpl, AC, &DL, DT).threadEdge(PredBB, OldSucc, NewSucc);
1698   }
1699 }
1700 
1701 void LazyValueInfo::eraseBlock(BasicBlock *BB) {
1702   if (PImpl) {
1703     const DataLayout &DL = BB->getModule()->getDataLayout();
1704     getCache(PImpl, AC, &DL, DT).eraseBlock(BB);
1705   }
1706 }
1707