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