1 //===- LazyValueInfo.cpp - Value constraint analysis ------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the interface for lazy computation of value constraint
10 // information.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Analysis/LazyValueInfo.h"
15 #include "llvm/ADT/DenseSet.h"
16 #include "llvm/ADT/Optional.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] = std::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   Optional<ConstantRange> getRangeForOperand(unsigned Op, Instruction *I,
424                                              BasicBlock *BB);
425   bool solveBlockValueBinaryOpImpl(
426       ValueLatticeElement &BBLV, Instruction *I, BasicBlock *BB,
427       std::function<ConstantRange(const ConstantRange &,
428                                   const ConstantRange &)> OpFn);
429   bool solveBlockValueBinaryOp(ValueLatticeElement &BBLV, BinaryOperator *BBI,
430                                BasicBlock *BB);
431   bool solveBlockValueCast(ValueLatticeElement &BBLV, CastInst *CI,
432                            BasicBlock *BB);
433   bool solveBlockValueOverflowIntrinsic(
434       ValueLatticeElement &BBLV, WithOverflowInst *WO, BasicBlock *BB);
435   bool solveBlockValueSaturatingIntrinsic(ValueLatticeElement &BBLV,
436                                           SaturatingInst *SI, BasicBlock *BB);
437   bool solveBlockValueIntrinsic(ValueLatticeElement &BBLV, IntrinsicInst *II,
438                                 BasicBlock *BB);
439   bool solveBlockValueExtractValue(ValueLatticeElement &BBLV,
440                                    ExtractValueInst *EVI, BasicBlock *BB);
441   void intersectAssumeOrGuardBlockValueConstantRange(Value *Val,
442                                                      ValueLatticeElement &BBLV,
443                                                      Instruction *BBI);
444 
445   void solve();
446 
447   public:
448     /// This is the query interface to determine the lattice
449     /// value for the specified Value* at the end of the specified block.
450     ValueLatticeElement getValueInBlock(Value *V, BasicBlock *BB,
451                                         Instruction *CxtI = nullptr);
452 
453     /// This is the query interface to determine the lattice
454     /// value for the specified Value* at the specified instruction (generally
455     /// from an assume intrinsic).
456     ValueLatticeElement getValueAt(Value *V, Instruction *CxtI);
457 
458     /// This is the query interface to determine the lattice
459     /// value for the specified Value* that is true on the specified edge.
460     ValueLatticeElement getValueOnEdge(Value *V, BasicBlock *FromBB,
461                                        BasicBlock *ToBB,
462                                    Instruction *CxtI = nullptr);
463 
464     /// Complete flush all previously computed values
465     void clear() {
466       TheCache.clear();
467     }
468 
469     /// Printing the LazyValueInfo Analysis.
470     void printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS) {
471         LazyValueInfoAnnotatedWriter Writer(this, DTree);
472         F.print(OS, &Writer);
473     }
474 
475     /// This is part of the update interface to inform the cache
476     /// that a block has been deleted.
477     void eraseBlock(BasicBlock *BB) {
478       TheCache.eraseBlock(BB);
479     }
480 
481     /// Disables use of the DominatorTree within LVI.
482     void disableDT() {
483       if (DT) {
484         assert(!DisabledDT && "Both DT and DisabledDT are not nullptr!");
485         std::swap(DT, DisabledDT);
486       }
487     }
488 
489     /// Enables use of the DominatorTree within LVI. Does nothing if the class
490     /// instance was initialized without a DT pointer.
491     void enableDT() {
492       if (DisabledDT) {
493         assert(!DT && "Both DT and DisabledDT are not nullptr!");
494         std::swap(DT, DisabledDT);
495       }
496     }
497 
498     /// This is the update interface to inform the cache that an edge from
499     /// PredBB to OldSucc has been threaded to be from PredBB to NewSucc.
500     void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
501 
502     LazyValueInfoImpl(AssumptionCache *AC, const DataLayout &DL,
503                        DominatorTree *DT = nullptr)
504         : AC(AC), DL(DL), DT(DT), DisabledDT(nullptr) {}
505   };
506 } // end anonymous namespace
507 
508 
509 void LazyValueInfoImpl::solve() {
510   SmallVector<std::pair<BasicBlock *, Value *>, 8> StartingStack(
511       BlockValueStack.begin(), BlockValueStack.end());
512 
513   unsigned processedCount = 0;
514   while (!BlockValueStack.empty()) {
515     processedCount++;
516     // Abort if we have to process too many values to get a result for this one.
517     // Because of the design of the overdefined cache currently being per-block
518     // to avoid naming-related issues (IE it wants to try to give different
519     // results for the same name in different blocks), overdefined results don't
520     // get cached globally, which in turn means we will often try to rediscover
521     // the same overdefined result again and again.  Once something like
522     // PredicateInfo is used in LVI or CVP, we should be able to make the
523     // overdefined cache global, and remove this throttle.
524     if (processedCount > MaxProcessedPerValue) {
525       LLVM_DEBUG(
526           dbgs() << "Giving up on stack because we are getting too deep\n");
527       // Fill in the original values
528       while (!StartingStack.empty()) {
529         std::pair<BasicBlock *, Value *> &e = StartingStack.back();
530         TheCache.insertResult(e.second, e.first,
531                               ValueLatticeElement::getOverdefined());
532         StartingStack.pop_back();
533       }
534       BlockValueSet.clear();
535       BlockValueStack.clear();
536       return;
537     }
538     std::pair<BasicBlock *, Value *> e = BlockValueStack.back();
539     assert(BlockValueSet.count(e) && "Stack value should be in BlockValueSet!");
540 
541     if (solveBlockValue(e.second, e.first)) {
542       // The work item was completely processed.
543       assert(BlockValueStack.back() == e && "Nothing should have been pushed!");
544       assert(TheCache.hasCachedValueInfo(e.second, e.first) &&
545              "Result should be in cache!");
546 
547       LLVM_DEBUG(
548           dbgs() << "POP " << *e.second << " in " << e.first->getName() << " = "
549                  << TheCache.getCachedValueInfo(e.second, e.first) << "\n");
550 
551       BlockValueStack.pop_back();
552       BlockValueSet.erase(e);
553     } else {
554       // More work needs to be done before revisiting.
555       assert(BlockValueStack.back() != e && "Stack should have been pushed!");
556     }
557   }
558 }
559 
560 bool LazyValueInfoImpl::hasBlockValue(Value *Val, BasicBlock *BB) {
561   // If already a constant, there is nothing to compute.
562   if (isa<Constant>(Val))
563     return true;
564 
565   return TheCache.hasCachedValueInfo(Val, BB);
566 }
567 
568 ValueLatticeElement LazyValueInfoImpl::getBlockValue(Value *Val,
569                                                      BasicBlock *BB) {
570   // If already a constant, there is nothing to compute.
571   if (Constant *VC = dyn_cast<Constant>(Val))
572     return ValueLatticeElement::get(VC);
573 
574   return TheCache.getCachedValueInfo(Val, BB);
575 }
576 
577 static ValueLatticeElement getFromRangeMetadata(Instruction *BBI) {
578   switch (BBI->getOpcode()) {
579   default: break;
580   case Instruction::Load:
581   case Instruction::Call:
582   case Instruction::Invoke:
583     if (MDNode *Ranges = BBI->getMetadata(LLVMContext::MD_range))
584       if (isa<IntegerType>(BBI->getType())) {
585         return ValueLatticeElement::getRange(
586             getConstantRangeFromMetadata(*Ranges));
587       }
588     break;
589   };
590   // Nothing known - will be intersected with other facts
591   return ValueLatticeElement::getOverdefined();
592 }
593 
594 bool LazyValueInfoImpl::solveBlockValue(Value *Val, BasicBlock *BB) {
595   if (isa<Constant>(Val))
596     return true;
597 
598   if (TheCache.hasCachedValueInfo(Val, BB)) {
599     // If we have a cached value, use that.
600     LLVM_DEBUG(dbgs() << "  reuse BB '" << BB->getName() << "' val="
601                       << TheCache.getCachedValueInfo(Val, BB) << '\n');
602 
603     // Since we're reusing a cached value, we don't need to update the
604     // OverDefinedCache. The cache will have been properly updated whenever the
605     // cached value was inserted.
606     return true;
607   }
608 
609   // Hold off inserting this value into the Cache in case we have to return
610   // false and come back later.
611   ValueLatticeElement Res;
612   if (!solveBlockValueImpl(Res, Val, BB))
613     // Work pushed, will revisit
614     return false;
615 
616   TheCache.insertResult(Val, BB, Res);
617   return true;
618 }
619 
620 bool LazyValueInfoImpl::solveBlockValueImpl(ValueLatticeElement &Res,
621                                             Value *Val, BasicBlock *BB) {
622 
623   Instruction *BBI = dyn_cast<Instruction>(Val);
624   if (!BBI || BBI->getParent() != BB)
625     return solveBlockValueNonLocal(Res, Val, BB);
626 
627   if (PHINode *PN = dyn_cast<PHINode>(BBI))
628     return solveBlockValuePHINode(Res, PN, BB);
629 
630   if (auto *SI = dyn_cast<SelectInst>(BBI))
631     return solveBlockValueSelect(Res, SI, BB);
632 
633   // If this value is a nonnull pointer, record it's range and bailout.  Note
634   // that for all other pointer typed values, we terminate the search at the
635   // definition.  We could easily extend this to look through geps, bitcasts,
636   // and the like to prove non-nullness, but it's not clear that's worth it
637   // compile time wise.  The context-insensitive value walk done inside
638   // isKnownNonZero gets most of the profitable cases at much less expense.
639   // This does mean that we have a sensitivity to where the defining
640   // instruction is placed, even if it could legally be hoisted much higher.
641   // That is unfortunate.
642   PointerType *PT = dyn_cast<PointerType>(BBI->getType());
643   if (PT && isKnownNonZero(BBI, DL)) {
644     Res = ValueLatticeElement::getNot(ConstantPointerNull::get(PT));
645     return true;
646   }
647   if (BBI->getType()->isIntegerTy()) {
648     if (auto *CI = dyn_cast<CastInst>(BBI))
649       return solveBlockValueCast(Res, CI, BB);
650 
651     if (BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI))
652       return solveBlockValueBinaryOp(Res, BO, BB);
653 
654     if (auto *EVI = dyn_cast<ExtractValueInst>(BBI))
655       return solveBlockValueExtractValue(Res, EVI, BB);
656 
657     if (auto *II = dyn_cast<IntrinsicInst>(BBI))
658       return solveBlockValueIntrinsic(Res, II, BB);
659   }
660 
661   LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
662                     << "' - unknown inst def found.\n");
663   Res = getFromRangeMetadata(BBI);
664   return true;
665 }
666 
667 static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr) {
668   if (LoadInst *L = dyn_cast<LoadInst>(I)) {
669     return L->getPointerAddressSpace() == 0 &&
670            GetUnderlyingObject(L->getPointerOperand(),
671                                L->getModule()->getDataLayout()) == Ptr;
672   }
673   if (StoreInst *S = dyn_cast<StoreInst>(I)) {
674     return S->getPointerAddressSpace() == 0 &&
675            GetUnderlyingObject(S->getPointerOperand(),
676                                S->getModule()->getDataLayout()) == Ptr;
677   }
678   if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
679     if (MI->isVolatile()) return false;
680 
681     // FIXME: check whether it has a valuerange that excludes zero?
682     ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength());
683     if (!Len || Len->isZero()) return false;
684 
685     if (MI->getDestAddressSpace() == 0)
686       if (GetUnderlyingObject(MI->getRawDest(),
687                               MI->getModule()->getDataLayout()) == Ptr)
688         return true;
689     if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))
690       if (MTI->getSourceAddressSpace() == 0)
691         if (GetUnderlyingObject(MTI->getRawSource(),
692                                 MTI->getModule()->getDataLayout()) == Ptr)
693           return true;
694   }
695   return false;
696 }
697 
698 /// Return true if the allocation associated with Val is ever dereferenced
699 /// within the given basic block.  This establishes the fact Val is not null,
700 /// but does not imply that the memory at Val is dereferenceable.  (Val may
701 /// point off the end of the dereferenceable part of the object.)
702 static bool isObjectDereferencedInBlock(Value *Val, BasicBlock *BB) {
703   assert(Val->getType()->isPointerTy());
704 
705   const DataLayout &DL = BB->getModule()->getDataLayout();
706   Value *UnderlyingVal = GetUnderlyingObject(Val, DL);
707   // If 'GetUnderlyingObject' didn't converge, skip it. It won't converge
708   // inside InstructionDereferencesPointer either.
709   if (UnderlyingVal == GetUnderlyingObject(UnderlyingVal, DL, 1))
710     for (Instruction &I : *BB)
711       if (InstructionDereferencesPointer(&I, UnderlyingVal))
712         return true;
713   return false;
714 }
715 
716 bool LazyValueInfoImpl::solveBlockValueNonLocal(ValueLatticeElement &BBLV,
717                                                  Value *Val, BasicBlock *BB) {
718   ValueLatticeElement Result;  // Start Undefined.
719 
720   // If this is the entry block, we must be asking about an argument.  The
721   // value is overdefined.
722   if (BB == &BB->getParent()->getEntryBlock()) {
723     assert(isa<Argument>(Val) && "Unknown live-in to the entry block");
724     // Before giving up, see if we can prove the pointer non-null local to
725     // this particular block.
726     PointerType *PTy = dyn_cast<PointerType>(Val->getType());
727     if (PTy &&
728         (isKnownNonZero(Val, DL) ||
729           (isObjectDereferencedInBlock(Val, BB) &&
730            !NullPointerIsDefined(BB->getParent(), PTy->getAddressSpace())))) {
731       Result = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy));
732     } else {
733       Result = ValueLatticeElement::getOverdefined();
734     }
735     BBLV = Result;
736     return true;
737   }
738 
739   // Loop over all of our predecessors, merging what we know from them into
740   // result.  If we encounter an unexplored predecessor, we eagerly explore it
741   // in a depth first manner.  In practice, this has the effect of discovering
742   // paths we can't analyze eagerly without spending compile times analyzing
743   // other paths.  This heuristic benefits from the fact that predecessors are
744   // frequently arranged such that dominating ones come first and we quickly
745   // find a path to function entry.  TODO: We should consider explicitly
746   // canonicalizing to make this true rather than relying on this happy
747   // accident.
748   for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
749     ValueLatticeElement EdgeResult;
750     if (!getEdgeValue(Val, *PI, BB, EdgeResult))
751       // Explore that input, then return here
752       return false;
753 
754     Result.mergeIn(EdgeResult, DL);
755 
756     // If we hit overdefined, exit early.  The BlockVals entry is already set
757     // to overdefined.
758     if (Result.isOverdefined()) {
759       LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
760                         << "' - overdefined because of pred (non local).\n");
761       // Before giving up, see if we can prove the pointer non-null local to
762       // this particular block.
763       PointerType *PTy = dyn_cast<PointerType>(Val->getType());
764       if (PTy && isObjectDereferencedInBlock(Val, BB) &&
765           !NullPointerIsDefined(BB->getParent(), PTy->getAddressSpace())) {
766         Result = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy));
767       }
768 
769       BBLV = Result;
770       return true;
771     }
772   }
773 
774   // Return the merged value, which is more precise than 'overdefined'.
775   assert(!Result.isOverdefined());
776   BBLV = Result;
777   return true;
778 }
779 
780 bool LazyValueInfoImpl::solveBlockValuePHINode(ValueLatticeElement &BBLV,
781                                                PHINode *PN, BasicBlock *BB) {
782   ValueLatticeElement Result;  // Start Undefined.
783 
784   // Loop over all of our predecessors, merging what we know from them into
785   // result.  See the comment about the chosen traversal order in
786   // solveBlockValueNonLocal; the same reasoning applies here.
787   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
788     BasicBlock *PhiBB = PN->getIncomingBlock(i);
789     Value *PhiVal = PN->getIncomingValue(i);
790     ValueLatticeElement EdgeResult;
791     // Note that we can provide PN as the context value to getEdgeValue, even
792     // though the results will be cached, because PN is the value being used as
793     // the cache key in the caller.
794     if (!getEdgeValue(PhiVal, PhiBB, BB, EdgeResult, PN))
795       // Explore that input, then return here
796       return false;
797 
798     Result.mergeIn(EdgeResult, DL);
799 
800     // If we hit overdefined, exit early.  The BlockVals entry is already set
801     // to overdefined.
802     if (Result.isOverdefined()) {
803       LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
804                         << "' - overdefined because of pred (local).\n");
805 
806       BBLV = Result;
807       return true;
808     }
809   }
810 
811   // Return the merged value, which is more precise than 'overdefined'.
812   assert(!Result.isOverdefined() && "Possible PHI in entry block?");
813   BBLV = Result;
814   return true;
815 }
816 
817 static ValueLatticeElement getValueFromCondition(Value *Val, Value *Cond,
818                                                  bool isTrueDest = true);
819 
820 // If we can determine a constraint on the value given conditions assumed by
821 // the program, intersect those constraints with BBLV
822 void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange(
823         Value *Val, ValueLatticeElement &BBLV, Instruction *BBI) {
824   BBI = BBI ? BBI : dyn_cast<Instruction>(Val);
825   if (!BBI)
826     return;
827 
828   for (auto &AssumeVH : AC->assumptionsFor(Val)) {
829     if (!AssumeVH)
830       continue;
831     auto *I = cast<CallInst>(AssumeVH);
832     if (!isValidAssumeForContext(I, BBI, DT))
833       continue;
834 
835     BBLV = intersect(BBLV, getValueFromCondition(Val, I->getArgOperand(0)));
836   }
837 
838   // If guards are not used in the module, don't spend time looking for them
839   auto *GuardDecl = BBI->getModule()->getFunction(
840           Intrinsic::getName(Intrinsic::experimental_guard));
841   if (!GuardDecl || GuardDecl->use_empty())
842     return;
843 
844   if (BBI->getIterator() == BBI->getParent()->begin())
845     return;
846   for (Instruction &I : make_range(std::next(BBI->getIterator().getReverse()),
847                                    BBI->getParent()->rend())) {
848     Value *Cond = nullptr;
849     if (match(&I, m_Intrinsic<Intrinsic::experimental_guard>(m_Value(Cond))))
850       BBLV = intersect(BBLV, getValueFromCondition(Val, Cond));
851   }
852 }
853 
854 bool LazyValueInfoImpl::solveBlockValueSelect(ValueLatticeElement &BBLV,
855                                               SelectInst *SI, BasicBlock *BB) {
856 
857   // Recurse on our inputs if needed
858   if (!hasBlockValue(SI->getTrueValue(), BB)) {
859     if (pushBlockValue(std::make_pair(BB, SI->getTrueValue())))
860       return false;
861     BBLV = ValueLatticeElement::getOverdefined();
862     return true;
863   }
864   ValueLatticeElement TrueVal = getBlockValue(SI->getTrueValue(), BB);
865   // If we hit overdefined, don't ask more queries.  We want to avoid poisoning
866   // extra slots in the table if we can.
867   if (TrueVal.isOverdefined()) {
868     BBLV = ValueLatticeElement::getOverdefined();
869     return true;
870   }
871 
872   if (!hasBlockValue(SI->getFalseValue(), BB)) {
873     if (pushBlockValue(std::make_pair(BB, SI->getFalseValue())))
874       return false;
875     BBLV = ValueLatticeElement::getOverdefined();
876     return true;
877   }
878   ValueLatticeElement FalseVal = getBlockValue(SI->getFalseValue(), BB);
879   // If we hit overdefined, don't ask more queries.  We want to avoid poisoning
880   // extra slots in the table if we can.
881   if (FalseVal.isOverdefined()) {
882     BBLV = ValueLatticeElement::getOverdefined();
883     return true;
884   }
885 
886   if (TrueVal.isConstantRange() && FalseVal.isConstantRange()) {
887     const ConstantRange &TrueCR = TrueVal.getConstantRange();
888     const ConstantRange &FalseCR = FalseVal.getConstantRange();
889     Value *LHS = nullptr;
890     Value *RHS = nullptr;
891     SelectPatternResult SPR = matchSelectPattern(SI, LHS, RHS);
892     // Is this a min specifically of our two inputs?  (Avoid the risk of
893     // ValueTracking getting smarter looking back past our immediate inputs.)
894     if (SelectPatternResult::isMinOrMax(SPR.Flavor) &&
895         LHS == SI->getTrueValue() && RHS == SI->getFalseValue()) {
896       ConstantRange ResultCR = [&]() {
897         switch (SPR.Flavor) {
898         default:
899           llvm_unreachable("unexpected minmax type!");
900         case SPF_SMIN:                   /// Signed minimum
901           return TrueCR.smin(FalseCR);
902         case SPF_UMIN:                   /// Unsigned minimum
903           return TrueCR.umin(FalseCR);
904         case SPF_SMAX:                   /// Signed maximum
905           return TrueCR.smax(FalseCR);
906         case SPF_UMAX:                   /// Unsigned maximum
907           return TrueCR.umax(FalseCR);
908         };
909       }();
910       BBLV = ValueLatticeElement::getRange(ResultCR);
911       return true;
912     }
913 
914     if (SPR.Flavor == SPF_ABS) {
915       if (LHS == SI->getTrueValue()) {
916         BBLV = ValueLatticeElement::getRange(TrueCR.abs());
917         return true;
918       }
919       if (LHS == SI->getFalseValue()) {
920         BBLV = ValueLatticeElement::getRange(FalseCR.abs());
921         return true;
922       }
923     }
924 
925     if (SPR.Flavor == SPF_NABS) {
926       ConstantRange Zero(APInt::getNullValue(TrueCR.getBitWidth()));
927       if (LHS == SI->getTrueValue()) {
928         BBLV = ValueLatticeElement::getRange(Zero.sub(TrueCR.abs()));
929         return true;
930       }
931       if (LHS == SI->getFalseValue()) {
932         BBLV = ValueLatticeElement::getRange(Zero.sub(FalseCR.abs()));
933         return true;
934       }
935     }
936   }
937 
938   // Can we constrain the facts about the true and false values by using the
939   // condition itself?  This shows up with idioms like e.g. select(a > 5, a, 5).
940   // TODO: We could potentially refine an overdefined true value above.
941   Value *Cond = SI->getCondition();
942   TrueVal = intersect(TrueVal,
943                       getValueFromCondition(SI->getTrueValue(), Cond, true));
944   FalseVal = intersect(FalseVal,
945                        getValueFromCondition(SI->getFalseValue(), Cond, false));
946 
947   // Handle clamp idioms such as:
948   //   %24 = constantrange<0, 17>
949   //   %39 = icmp eq i32 %24, 0
950   //   %40 = add i32 %24, -1
951   //   %siv.next = select i1 %39, i32 16, i32 %40
952   //   %siv.next = constantrange<0, 17> not <-1, 17>
953   // In general, this can handle any clamp idiom which tests the edge
954   // condition via an equality or inequality.
955   if (auto *ICI = dyn_cast<ICmpInst>(Cond)) {
956     ICmpInst::Predicate Pred = ICI->getPredicate();
957     Value *A = ICI->getOperand(0);
958     if (ConstantInt *CIBase = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
959       auto addConstants = [](ConstantInt *A, ConstantInt *B) {
960         assert(A->getType() == B->getType());
961         return ConstantInt::get(A->getType(), A->getValue() + B->getValue());
962       };
963       // See if either input is A + C2, subject to the constraint from the
964       // condition that A != C when that input is used.  We can assume that
965       // that input doesn't include C + C2.
966       ConstantInt *CIAdded;
967       switch (Pred) {
968       default: break;
969       case ICmpInst::ICMP_EQ:
970         if (match(SI->getFalseValue(), m_Add(m_Specific(A),
971                                              m_ConstantInt(CIAdded)))) {
972           auto ResNot = addConstants(CIBase, CIAdded);
973           FalseVal = intersect(FalseVal,
974                                ValueLatticeElement::getNot(ResNot));
975         }
976         break;
977       case ICmpInst::ICMP_NE:
978         if (match(SI->getTrueValue(), m_Add(m_Specific(A),
979                                             m_ConstantInt(CIAdded)))) {
980           auto ResNot = addConstants(CIBase, CIAdded);
981           TrueVal = intersect(TrueVal,
982                               ValueLatticeElement::getNot(ResNot));
983         }
984         break;
985       };
986     }
987   }
988 
989   ValueLatticeElement Result;  // Start Undefined.
990   Result.mergeIn(TrueVal, DL);
991   Result.mergeIn(FalseVal, DL);
992   BBLV = Result;
993   return true;
994 }
995 
996 Optional<ConstantRange> LazyValueInfoImpl::getRangeForOperand(unsigned Op,
997                                                               Instruction *I,
998                                                               BasicBlock *BB) {
999   if (!hasBlockValue(I->getOperand(Op), BB))
1000     if (pushBlockValue(std::make_pair(BB, I->getOperand(Op))))
1001       return None;
1002 
1003   const unsigned OperandBitWidth =
1004     DL.getTypeSizeInBits(I->getOperand(Op)->getType());
1005   ConstantRange Range = ConstantRange::getFull(OperandBitWidth);
1006   if (hasBlockValue(I->getOperand(Op), BB)) {
1007     ValueLatticeElement Val = getBlockValue(I->getOperand(Op), BB);
1008     intersectAssumeOrGuardBlockValueConstantRange(I->getOperand(Op), Val, I);
1009     if (Val.isConstantRange())
1010       Range = Val.getConstantRange();
1011   }
1012   return Range;
1013 }
1014 
1015 bool LazyValueInfoImpl::solveBlockValueCast(ValueLatticeElement &BBLV,
1016                                             CastInst *CI,
1017                                             BasicBlock *BB) {
1018   if (!CI->getOperand(0)->getType()->isSized()) {
1019     // Without knowing how wide the input is, we can't analyze it in any useful
1020     // way.
1021     BBLV = ValueLatticeElement::getOverdefined();
1022     return true;
1023   }
1024 
1025   // Filter out casts we don't know how to reason about before attempting to
1026   // recurse on our operand.  This can cut a long search short if we know we're
1027   // not going to be able to get any useful information anways.
1028   switch (CI->getOpcode()) {
1029   case Instruction::Trunc:
1030   case Instruction::SExt:
1031   case Instruction::ZExt:
1032   case Instruction::BitCast:
1033     break;
1034   default:
1035     // Unhandled instructions are overdefined.
1036     LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
1037                       << "' - overdefined (unknown cast).\n");
1038     BBLV = ValueLatticeElement::getOverdefined();
1039     return true;
1040   }
1041 
1042   // Figure out the range of the LHS.  If that fails, we still apply the
1043   // transfer rule on the full set since we may be able to locally infer
1044   // interesting facts.
1045   Optional<ConstantRange> LHSRes = getRangeForOperand(0, CI, BB);
1046   if (!LHSRes.hasValue())
1047     // More work to do before applying this transfer rule.
1048     return false;
1049   ConstantRange LHSRange = LHSRes.getValue();
1050 
1051   const unsigned ResultBitWidth = CI->getType()->getIntegerBitWidth();
1052 
1053   // NOTE: We're currently limited by the set of operations that ConstantRange
1054   // can evaluate symbolically.  Enhancing that set will allows us to analyze
1055   // more definitions.
1056   BBLV = ValueLatticeElement::getRange(LHSRange.castOp(CI->getOpcode(),
1057                                                        ResultBitWidth));
1058   return true;
1059 }
1060 
1061 bool LazyValueInfoImpl::solveBlockValueBinaryOpImpl(
1062     ValueLatticeElement &BBLV, Instruction *I, BasicBlock *BB,
1063     std::function<ConstantRange(const ConstantRange &,
1064                                 const ConstantRange &)> OpFn) {
1065   // Figure out the ranges of the operands.  If that fails, use a
1066   // conservative range, but apply the transfer rule anyways.  This
1067   // lets us pick up facts from expressions like "and i32 (call i32
1068   // @foo()), 32"
1069   Optional<ConstantRange> LHSRes = getRangeForOperand(0, I, BB);
1070   Optional<ConstantRange> RHSRes = getRangeForOperand(1, I, BB);
1071   if (!LHSRes.hasValue() || !RHSRes.hasValue())
1072     // More work to do before applying this transfer rule.
1073     return false;
1074 
1075   ConstantRange LHSRange = LHSRes.getValue();
1076   ConstantRange RHSRange = RHSRes.getValue();
1077   BBLV = ValueLatticeElement::getRange(OpFn(LHSRange, RHSRange));
1078   return true;
1079 }
1080 
1081 bool LazyValueInfoImpl::solveBlockValueBinaryOp(ValueLatticeElement &BBLV,
1082                                                 BinaryOperator *BO,
1083                                                 BasicBlock *BB) {
1084 
1085   assert(BO->getOperand(0)->getType()->isSized() &&
1086          "all operands to binary operators are sized");
1087   if (BO->getOpcode() == Instruction::Xor) {
1088     // Xor is the only operation not supported by ConstantRange::binaryOp().
1089     LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
1090                       << "' - overdefined (unknown binary operator).\n");
1091     BBLV = ValueLatticeElement::getOverdefined();
1092     return true;
1093   }
1094 
1095   if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(BO)) {
1096     unsigned NoWrapKind = 0;
1097     if (OBO->hasNoUnsignedWrap())
1098       NoWrapKind |= OverflowingBinaryOperator::NoUnsignedWrap;
1099     if (OBO->hasNoSignedWrap())
1100       NoWrapKind |= OverflowingBinaryOperator::NoSignedWrap;
1101 
1102     return solveBlockValueBinaryOpImpl(
1103         BBLV, BO, BB,
1104         [BO, NoWrapKind](const ConstantRange &CR1, const ConstantRange &CR2) {
1105           return CR1.overflowingBinaryOp(BO->getOpcode(), CR2, NoWrapKind);
1106         });
1107   }
1108 
1109   return solveBlockValueBinaryOpImpl(
1110       BBLV, BO, BB, [BO](const ConstantRange &CR1, const ConstantRange &CR2) {
1111         return CR1.binaryOp(BO->getOpcode(), CR2);
1112       });
1113 }
1114 
1115 bool LazyValueInfoImpl::solveBlockValueOverflowIntrinsic(
1116     ValueLatticeElement &BBLV, WithOverflowInst *WO, BasicBlock *BB) {
1117   return solveBlockValueBinaryOpImpl(BBLV, WO, BB,
1118       [WO](const ConstantRange &CR1, const ConstantRange &CR2) {
1119         return CR1.binaryOp(WO->getBinaryOp(), CR2);
1120       });
1121 }
1122 
1123 bool LazyValueInfoImpl::solveBlockValueSaturatingIntrinsic(
1124     ValueLatticeElement &BBLV, SaturatingInst *SI, BasicBlock *BB) {
1125   switch (SI->getIntrinsicID()) {
1126   case Intrinsic::uadd_sat:
1127     return solveBlockValueBinaryOpImpl(
1128         BBLV, SI, BB, [](const ConstantRange &CR1, const ConstantRange &CR2) {
1129           return CR1.uadd_sat(CR2);
1130         });
1131   case Intrinsic::usub_sat:
1132     return solveBlockValueBinaryOpImpl(
1133         BBLV, SI, BB, [](const ConstantRange &CR1, const ConstantRange &CR2) {
1134           return CR1.usub_sat(CR2);
1135         });
1136   case Intrinsic::sadd_sat:
1137     return solveBlockValueBinaryOpImpl(
1138         BBLV, SI, BB, [](const ConstantRange &CR1, const ConstantRange &CR2) {
1139           return CR1.sadd_sat(CR2);
1140         });
1141   case Intrinsic::ssub_sat:
1142     return solveBlockValueBinaryOpImpl(
1143         BBLV, SI, BB, [](const ConstantRange &CR1, const ConstantRange &CR2) {
1144           return CR1.ssub_sat(CR2);
1145         });
1146   default:
1147     llvm_unreachable("All llvm.sat intrinsic are handled.");
1148   }
1149 }
1150 
1151 bool LazyValueInfoImpl::solveBlockValueIntrinsic(ValueLatticeElement &BBLV,
1152                                                  IntrinsicInst *II,
1153                                                  BasicBlock *BB) {
1154   if (auto *SI = dyn_cast<SaturatingInst>(II))
1155     return solveBlockValueSaturatingIntrinsic(BBLV, SI, BB);
1156 
1157   LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
1158                     << "' - overdefined (unknown intrinsic).\n");
1159   BBLV = ValueLatticeElement::getOverdefined();
1160   return true;
1161 }
1162 
1163 bool LazyValueInfoImpl::solveBlockValueExtractValue(
1164     ValueLatticeElement &BBLV, ExtractValueInst *EVI, BasicBlock *BB) {
1165   if (auto *WO = dyn_cast<WithOverflowInst>(EVI->getAggregateOperand()))
1166     if (EVI->getNumIndices() == 1 && *EVI->idx_begin() == 0)
1167       return solveBlockValueOverflowIntrinsic(BBLV, WO, BB);
1168 
1169   // Handle extractvalue of insertvalue to allow further simplification
1170   // based on replaced with.overflow intrinsics.
1171   if (Value *V = SimplifyExtractValueInst(
1172           EVI->getAggregateOperand(), EVI->getIndices(),
1173           EVI->getModule()->getDataLayout())) {
1174     if (!hasBlockValue(V, BB)) {
1175       if (pushBlockValue({ BB, V }))
1176         return false;
1177       BBLV = ValueLatticeElement::getOverdefined();
1178       return true;
1179     }
1180     BBLV = getBlockValue(V, BB);
1181     return true;
1182   }
1183 
1184   LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
1185                     << "' - overdefined (unknown extractvalue).\n");
1186   BBLV = ValueLatticeElement::getOverdefined();
1187   return true;
1188 }
1189 
1190 static ValueLatticeElement getValueFromICmpCondition(Value *Val, ICmpInst *ICI,
1191                                                      bool isTrueDest) {
1192   Value *LHS = ICI->getOperand(0);
1193   Value *RHS = ICI->getOperand(1);
1194   CmpInst::Predicate Predicate = ICI->getPredicate();
1195 
1196   if (isa<Constant>(RHS)) {
1197     if (ICI->isEquality() && LHS == Val) {
1198       // We know that V has the RHS constant if this is a true SETEQ or
1199       // false SETNE.
1200       if (isTrueDest == (Predicate == ICmpInst::ICMP_EQ))
1201         return ValueLatticeElement::get(cast<Constant>(RHS));
1202       else
1203         return ValueLatticeElement::getNot(cast<Constant>(RHS));
1204     }
1205   }
1206 
1207   if (!Val->getType()->isIntegerTy())
1208     return ValueLatticeElement::getOverdefined();
1209 
1210   // Use ConstantRange::makeAllowedICmpRegion in order to determine the possible
1211   // range of Val guaranteed by the condition. Recognize comparisons in the from
1212   // of:
1213   //  icmp <pred> Val, ...
1214   //  icmp <pred> (add Val, Offset), ...
1215   // The latter is the range checking idiom that InstCombine produces. Subtract
1216   // the offset from the allowed range for RHS in this case.
1217 
1218   // Val or (add Val, Offset) can be on either hand of the comparison
1219   if (LHS != Val && !match(LHS, m_Add(m_Specific(Val), m_ConstantInt()))) {
1220     std::swap(LHS, RHS);
1221     Predicate = CmpInst::getSwappedPredicate(Predicate);
1222   }
1223 
1224   ConstantInt *Offset = nullptr;
1225   if (LHS != Val)
1226     match(LHS, m_Add(m_Specific(Val), m_ConstantInt(Offset)));
1227 
1228   if (LHS == Val || Offset) {
1229     // Calculate the range of values that are allowed by the comparison
1230     ConstantRange RHSRange(RHS->getType()->getIntegerBitWidth(),
1231                            /*isFullSet=*/true);
1232     if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS))
1233       RHSRange = ConstantRange(CI->getValue());
1234     else if (Instruction *I = dyn_cast<Instruction>(RHS))
1235       if (auto *Ranges = I->getMetadata(LLVMContext::MD_range))
1236         RHSRange = getConstantRangeFromMetadata(*Ranges);
1237 
1238     // If we're interested in the false dest, invert the condition
1239     CmpInst::Predicate Pred =
1240             isTrueDest ? Predicate : CmpInst::getInversePredicate(Predicate);
1241     ConstantRange TrueValues =
1242             ConstantRange::makeAllowedICmpRegion(Pred, RHSRange);
1243 
1244     if (Offset) // Apply the offset from above.
1245       TrueValues = TrueValues.subtract(Offset->getValue());
1246 
1247     return ValueLatticeElement::getRange(std::move(TrueValues));
1248   }
1249 
1250   return ValueLatticeElement::getOverdefined();
1251 }
1252 
1253 // Handle conditions of the form
1254 // extractvalue(op.with.overflow(%x, C), 1).
1255 static ValueLatticeElement getValueFromOverflowCondition(
1256     Value *Val, WithOverflowInst *WO, bool IsTrueDest) {
1257   // TODO: This only works with a constant RHS for now. We could also compute
1258   // the range of the RHS, but this doesn't fit into the current structure of
1259   // the edge value calculation.
1260   const APInt *C;
1261   if (WO->getLHS() != Val || !match(WO->getRHS(), m_APInt(C)))
1262     return ValueLatticeElement::getOverdefined();
1263 
1264   // Calculate the possible values of %x for which no overflow occurs.
1265   ConstantRange NWR = ConstantRange::makeExactNoWrapRegion(
1266       WO->getBinaryOp(), *C, WO->getNoWrapKind());
1267 
1268   // If overflow is false, %x is constrained to NWR. If overflow is true, %x is
1269   // constrained to it's inverse (all values that might cause overflow).
1270   if (IsTrueDest)
1271     NWR = NWR.inverse();
1272   return ValueLatticeElement::getRange(NWR);
1273 }
1274 
1275 static ValueLatticeElement
1276 getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest,
1277                       DenseMap<Value*, ValueLatticeElement> &Visited);
1278 
1279 static ValueLatticeElement
1280 getValueFromConditionImpl(Value *Val, Value *Cond, bool isTrueDest,
1281                           DenseMap<Value*, ValueLatticeElement> &Visited) {
1282   if (ICmpInst *ICI = dyn_cast<ICmpInst>(Cond))
1283     return getValueFromICmpCondition(Val, ICI, isTrueDest);
1284 
1285   if (auto *EVI = dyn_cast<ExtractValueInst>(Cond))
1286     if (auto *WO = dyn_cast<WithOverflowInst>(EVI->getAggregateOperand()))
1287       if (EVI->getNumIndices() == 1 && *EVI->idx_begin() == 1)
1288         return getValueFromOverflowCondition(Val, WO, isTrueDest);
1289 
1290   // Handle conditions in the form of (cond1 && cond2), we know that on the
1291   // true dest path both of the conditions hold. Similarly for conditions of
1292   // the form (cond1 || cond2), we know that on the false dest path neither
1293   // condition holds.
1294   BinaryOperator *BO = dyn_cast<BinaryOperator>(Cond);
1295   if (!BO || (isTrueDest && BO->getOpcode() != BinaryOperator::And) ||
1296              (!isTrueDest && BO->getOpcode() != BinaryOperator::Or))
1297     return ValueLatticeElement::getOverdefined();
1298 
1299   // Prevent infinite recursion if Cond references itself as in this example:
1300   //  Cond: "%tmp4 = and i1 %tmp4, undef"
1301   //    BL: "%tmp4 = and i1 %tmp4, undef"
1302   //    BR: "i1 undef"
1303   Value *BL = BO->getOperand(0);
1304   Value *BR = BO->getOperand(1);
1305   if (BL == Cond || BR == Cond)
1306     return ValueLatticeElement::getOverdefined();
1307 
1308   return intersect(getValueFromCondition(Val, BL, isTrueDest, Visited),
1309                    getValueFromCondition(Val, BR, isTrueDest, Visited));
1310 }
1311 
1312 static ValueLatticeElement
1313 getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest,
1314                       DenseMap<Value*, ValueLatticeElement> &Visited) {
1315   auto I = Visited.find(Cond);
1316   if (I != Visited.end())
1317     return I->second;
1318 
1319   auto Result = getValueFromConditionImpl(Val, Cond, isTrueDest, Visited);
1320   Visited[Cond] = Result;
1321   return Result;
1322 }
1323 
1324 ValueLatticeElement getValueFromCondition(Value *Val, Value *Cond,
1325                                           bool isTrueDest) {
1326   assert(Cond && "precondition");
1327   DenseMap<Value*, ValueLatticeElement> Visited;
1328   return getValueFromCondition(Val, Cond, isTrueDest, Visited);
1329 }
1330 
1331 // Return true if Usr has Op as an operand, otherwise false.
1332 static bool usesOperand(User *Usr, Value *Op) {
1333   return find(Usr->operands(), Op) != Usr->op_end();
1334 }
1335 
1336 // Return true if the instruction type of Val is supported by
1337 // constantFoldUser(). Currently CastInst and BinaryOperator only.  Call this
1338 // before calling constantFoldUser() to find out if it's even worth attempting
1339 // to call it.
1340 static bool isOperationFoldable(User *Usr) {
1341   return isa<CastInst>(Usr) || isa<BinaryOperator>(Usr);
1342 }
1343 
1344 // Check if Usr can be simplified to an integer constant when the value of one
1345 // of its operands Op is an integer constant OpConstVal. If so, return it as an
1346 // lattice value range with a single element or otherwise return an overdefined
1347 // lattice value.
1348 static ValueLatticeElement constantFoldUser(User *Usr, Value *Op,
1349                                             const APInt &OpConstVal,
1350                                             const DataLayout &DL) {
1351   assert(isOperationFoldable(Usr) && "Precondition");
1352   Constant* OpConst = Constant::getIntegerValue(Op->getType(), OpConstVal);
1353   // Check if Usr can be simplified to a constant.
1354   if (auto *CI = dyn_cast<CastInst>(Usr)) {
1355     assert(CI->getOperand(0) == Op && "Operand 0 isn't Op");
1356     if (auto *C = dyn_cast_or_null<ConstantInt>(
1357             SimplifyCastInst(CI->getOpcode(), OpConst,
1358                              CI->getDestTy(), DL))) {
1359       return ValueLatticeElement::getRange(ConstantRange(C->getValue()));
1360     }
1361   } else if (auto *BO = dyn_cast<BinaryOperator>(Usr)) {
1362     bool Op0Match = BO->getOperand(0) == Op;
1363     bool Op1Match = BO->getOperand(1) == Op;
1364     assert((Op0Match || Op1Match) &&
1365            "Operand 0 nor Operand 1 isn't a match");
1366     Value *LHS = Op0Match ? OpConst : BO->getOperand(0);
1367     Value *RHS = Op1Match ? OpConst : BO->getOperand(1);
1368     if (auto *C = dyn_cast_or_null<ConstantInt>(
1369             SimplifyBinOp(BO->getOpcode(), LHS, RHS, DL))) {
1370       return ValueLatticeElement::getRange(ConstantRange(C->getValue()));
1371     }
1372   }
1373   return ValueLatticeElement::getOverdefined();
1374 }
1375 
1376 /// Compute the value of Val on the edge BBFrom -> BBTo. Returns false if
1377 /// Val is not constrained on the edge.  Result is unspecified if return value
1378 /// is false.
1379 static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom,
1380                               BasicBlock *BBTo, ValueLatticeElement &Result) {
1381   // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
1382   // know that v != 0.
1383   if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {
1384     // If this is a conditional branch and only one successor goes to BBTo, then
1385     // we may be able to infer something from the condition.
1386     if (BI->isConditional() &&
1387         BI->getSuccessor(0) != BI->getSuccessor(1)) {
1388       bool isTrueDest = BI->getSuccessor(0) == BBTo;
1389       assert(BI->getSuccessor(!isTrueDest) == BBTo &&
1390              "BBTo isn't a successor of BBFrom");
1391       Value *Condition = BI->getCondition();
1392 
1393       // If V is the condition of the branch itself, then we know exactly what
1394       // it is.
1395       if (Condition == Val) {
1396         Result = ValueLatticeElement::get(ConstantInt::get(
1397                               Type::getInt1Ty(Val->getContext()), isTrueDest));
1398         return true;
1399       }
1400 
1401       // If the condition of the branch is an equality comparison, we may be
1402       // able to infer the value.
1403       Result = getValueFromCondition(Val, Condition, isTrueDest);
1404       if (!Result.isOverdefined())
1405         return true;
1406 
1407       if (User *Usr = dyn_cast<User>(Val)) {
1408         assert(Result.isOverdefined() && "Result isn't overdefined");
1409         // Check with isOperationFoldable() first to avoid linearly iterating
1410         // over the operands unnecessarily which can be expensive for
1411         // instructions with many operands.
1412         if (isa<IntegerType>(Usr->getType()) && isOperationFoldable(Usr)) {
1413           const DataLayout &DL = BBTo->getModule()->getDataLayout();
1414           if (usesOperand(Usr, Condition)) {
1415             // If Val has Condition as an operand and Val can be folded into a
1416             // constant with either Condition == true or Condition == false,
1417             // propagate the constant.
1418             // eg.
1419             //   ; %Val is true on the edge to %then.
1420             //   %Val = and i1 %Condition, true.
1421             //   br %Condition, label %then, label %else
1422             APInt ConditionVal(1, isTrueDest ? 1 : 0);
1423             Result = constantFoldUser(Usr, Condition, ConditionVal, DL);
1424           } else {
1425             // If one of Val's operand has an inferred value, we may be able to
1426             // infer the value of Val.
1427             // eg.
1428             //    ; %Val is 94 on the edge to %then.
1429             //    %Val = add i8 %Op, 1
1430             //    %Condition = icmp eq i8 %Op, 93
1431             //    br i1 %Condition, label %then, label %else
1432             for (unsigned i = 0; i < Usr->getNumOperands(); ++i) {
1433               Value *Op = Usr->getOperand(i);
1434               ValueLatticeElement OpLatticeVal =
1435                   getValueFromCondition(Op, Condition, isTrueDest);
1436               if (Optional<APInt> OpConst = OpLatticeVal.asConstantInteger()) {
1437                 Result = constantFoldUser(Usr, Op, OpConst.getValue(), DL);
1438                 break;
1439               }
1440             }
1441           }
1442         }
1443       }
1444       if (!Result.isOverdefined())
1445         return true;
1446     }
1447   }
1448 
1449   // If the edge was formed by a switch on the value, then we may know exactly
1450   // what it is.
1451   if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {
1452     Value *Condition = SI->getCondition();
1453     if (!isa<IntegerType>(Val->getType()))
1454       return false;
1455     bool ValUsesConditionAndMayBeFoldable = false;
1456     if (Condition != Val) {
1457       // Check if Val has Condition as an operand.
1458       if (User *Usr = dyn_cast<User>(Val))
1459         ValUsesConditionAndMayBeFoldable = isOperationFoldable(Usr) &&
1460             usesOperand(Usr, Condition);
1461       if (!ValUsesConditionAndMayBeFoldable)
1462         return false;
1463     }
1464     assert((Condition == Val || ValUsesConditionAndMayBeFoldable) &&
1465            "Condition != Val nor Val doesn't use Condition");
1466 
1467     bool DefaultCase = SI->getDefaultDest() == BBTo;
1468     unsigned BitWidth = Val->getType()->getIntegerBitWidth();
1469     ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/);
1470 
1471     for (auto Case : SI->cases()) {
1472       APInt CaseValue = Case.getCaseValue()->getValue();
1473       ConstantRange EdgeVal(CaseValue);
1474       if (ValUsesConditionAndMayBeFoldable) {
1475         User *Usr = cast<User>(Val);
1476         const DataLayout &DL = BBTo->getModule()->getDataLayout();
1477         ValueLatticeElement EdgeLatticeVal =
1478             constantFoldUser(Usr, Condition, CaseValue, DL);
1479         if (EdgeLatticeVal.isOverdefined())
1480           return false;
1481         EdgeVal = EdgeLatticeVal.getConstantRange();
1482       }
1483       if (DefaultCase) {
1484         // It is possible that the default destination is the destination of
1485         // some cases. We cannot perform difference for those cases.
1486         // We know Condition != CaseValue in BBTo.  In some cases we can use
1487         // this to infer Val == f(Condition) is != f(CaseValue).  For now, we
1488         // only do this when f is identity (i.e. Val == Condition), but we
1489         // should be able to do this for any injective f.
1490         if (Case.getCaseSuccessor() != BBTo && Condition == Val)
1491           EdgesVals = EdgesVals.difference(EdgeVal);
1492       } else if (Case.getCaseSuccessor() == BBTo)
1493         EdgesVals = EdgesVals.unionWith(EdgeVal);
1494     }
1495     Result = ValueLatticeElement::getRange(std::move(EdgesVals));
1496     return true;
1497   }
1498   return false;
1499 }
1500 
1501 /// Compute the value of Val on the edge BBFrom -> BBTo or the value at
1502 /// the basic block if the edge does not constrain Val.
1503 bool LazyValueInfoImpl::getEdgeValue(Value *Val, BasicBlock *BBFrom,
1504                                      BasicBlock *BBTo,
1505                                      ValueLatticeElement &Result,
1506                                      Instruction *CxtI) {
1507   // If already a constant, there is nothing to compute.
1508   if (Constant *VC = dyn_cast<Constant>(Val)) {
1509     Result = ValueLatticeElement::get(VC);
1510     return true;
1511   }
1512 
1513   ValueLatticeElement LocalResult;
1514   if (!getEdgeValueLocal(Val, BBFrom, BBTo, LocalResult))
1515     // If we couldn't constrain the value on the edge, LocalResult doesn't
1516     // provide any information.
1517     LocalResult = ValueLatticeElement::getOverdefined();
1518 
1519   if (hasSingleValue(LocalResult)) {
1520     // Can't get any more precise here
1521     Result = LocalResult;
1522     return true;
1523   }
1524 
1525   if (!hasBlockValue(Val, BBFrom)) {
1526     if (pushBlockValue(std::make_pair(BBFrom, Val)))
1527       return false;
1528     // No new information.
1529     Result = LocalResult;
1530     return true;
1531   }
1532 
1533   // Try to intersect ranges of the BB and the constraint on the edge.
1534   ValueLatticeElement InBlock = getBlockValue(Val, BBFrom);
1535   intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock,
1536                                                 BBFrom->getTerminator());
1537   // We can use the context instruction (generically the ultimate instruction
1538   // the calling pass is trying to simplify) here, even though the result of
1539   // this function is generally cached when called from the solve* functions
1540   // (and that cached result might be used with queries using a different
1541   // context instruction), because when this function is called from the solve*
1542   // functions, the context instruction is not provided. When called from
1543   // LazyValueInfoImpl::getValueOnEdge, the context instruction is provided,
1544   // but then the result is not cached.
1545   intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock, CxtI);
1546 
1547   Result = intersect(LocalResult, InBlock);
1548   return true;
1549 }
1550 
1551 ValueLatticeElement LazyValueInfoImpl::getValueInBlock(Value *V, BasicBlock *BB,
1552                                                        Instruction *CxtI) {
1553   LLVM_DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
1554                     << BB->getName() << "'\n");
1555 
1556   assert(BlockValueStack.empty() && BlockValueSet.empty());
1557   if (!hasBlockValue(V, BB)) {
1558     pushBlockValue(std::make_pair(BB, V));
1559     solve();
1560   }
1561   ValueLatticeElement Result = getBlockValue(V, BB);
1562   intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI);
1563 
1564   LLVM_DEBUG(dbgs() << "  Result = " << Result << "\n");
1565   return Result;
1566 }
1567 
1568 ValueLatticeElement LazyValueInfoImpl::getValueAt(Value *V, Instruction *CxtI) {
1569   LLVM_DEBUG(dbgs() << "LVI Getting value " << *V << " at '" << CxtI->getName()
1570                     << "'\n");
1571 
1572   if (auto *C = dyn_cast<Constant>(V))
1573     return ValueLatticeElement::get(C);
1574 
1575   ValueLatticeElement Result = ValueLatticeElement::getOverdefined();
1576   if (auto *I = dyn_cast<Instruction>(V))
1577     Result = getFromRangeMetadata(I);
1578   intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI);
1579 
1580   LLVM_DEBUG(dbgs() << "  Result = " << Result << "\n");
1581   return Result;
1582 }
1583 
1584 ValueLatticeElement LazyValueInfoImpl::
1585 getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB,
1586                Instruction *CxtI) {
1587   LLVM_DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
1588                     << FromBB->getName() << "' to '" << ToBB->getName()
1589                     << "'\n");
1590 
1591   ValueLatticeElement Result;
1592   if (!getEdgeValue(V, FromBB, ToBB, Result, CxtI)) {
1593     solve();
1594     bool WasFastQuery = getEdgeValue(V, FromBB, ToBB, Result, CxtI);
1595     (void)WasFastQuery;
1596     assert(WasFastQuery && "More work to do after problem solved?");
1597   }
1598 
1599   LLVM_DEBUG(dbgs() << "  Result = " << Result << "\n");
1600   return Result;
1601 }
1602 
1603 void LazyValueInfoImpl::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
1604                                    BasicBlock *NewSucc) {
1605   TheCache.threadEdgeImpl(OldSucc, NewSucc);
1606 }
1607 
1608 //===----------------------------------------------------------------------===//
1609 //                            LazyValueInfo Impl
1610 //===----------------------------------------------------------------------===//
1611 
1612 /// This lazily constructs the LazyValueInfoImpl.
1613 static LazyValueInfoImpl &getImpl(void *&PImpl, AssumptionCache *AC,
1614                                   const DataLayout *DL,
1615                                   DominatorTree *DT = nullptr) {
1616   if (!PImpl) {
1617     assert(DL && "getCache() called with a null DataLayout");
1618     PImpl = new LazyValueInfoImpl(AC, *DL, DT);
1619   }
1620   return *static_cast<LazyValueInfoImpl*>(PImpl);
1621 }
1622 
1623 bool LazyValueInfoWrapperPass::runOnFunction(Function &F) {
1624   Info.AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
1625   const DataLayout &DL = F.getParent()->getDataLayout();
1626 
1627   DominatorTreeWrapperPass *DTWP =
1628       getAnalysisIfAvailable<DominatorTreeWrapperPass>();
1629   Info.DT = DTWP ? &DTWP->getDomTree() : nullptr;
1630   Info.TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
1631 
1632   if (Info.PImpl)
1633     getImpl(Info.PImpl, Info.AC, &DL, Info.DT).clear();
1634 
1635   // Fully lazy.
1636   return false;
1637 }
1638 
1639 void LazyValueInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
1640   AU.setPreservesAll();
1641   AU.addRequired<AssumptionCacheTracker>();
1642   AU.addRequired<TargetLibraryInfoWrapperPass>();
1643 }
1644 
1645 LazyValueInfo &LazyValueInfoWrapperPass::getLVI() { return Info; }
1646 
1647 LazyValueInfo::~LazyValueInfo() { releaseMemory(); }
1648 
1649 void LazyValueInfo::releaseMemory() {
1650   // If the cache was allocated, free it.
1651   if (PImpl) {
1652     delete &getImpl(PImpl, AC, nullptr);
1653     PImpl = nullptr;
1654   }
1655 }
1656 
1657 bool LazyValueInfo::invalidate(Function &F, const PreservedAnalyses &PA,
1658                                FunctionAnalysisManager::Invalidator &Inv) {
1659   // We need to invalidate if we have either failed to preserve this analyses
1660   // result directly or if any of its dependencies have been invalidated.
1661   auto PAC = PA.getChecker<LazyValueAnalysis>();
1662   if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
1663       (DT && Inv.invalidate<DominatorTreeAnalysis>(F, PA)))
1664     return true;
1665 
1666   return false;
1667 }
1668 
1669 void LazyValueInfoWrapperPass::releaseMemory() { Info.releaseMemory(); }
1670 
1671 LazyValueInfo LazyValueAnalysis::run(Function &F,
1672                                      FunctionAnalysisManager &FAM) {
1673   auto &AC = FAM.getResult<AssumptionAnalysis>(F);
1674   auto &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
1675   auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(F);
1676 
1677   return LazyValueInfo(&AC, &F.getParent()->getDataLayout(), &TLI, DT);
1678 }
1679 
1680 /// Returns true if we can statically tell that this value will never be a
1681 /// "useful" constant.  In practice, this means we've got something like an
1682 /// alloca or a malloc call for which a comparison against a constant can
1683 /// only be guarding dead code.  Note that we are potentially giving up some
1684 /// precision in dead code (a constant result) in favour of avoiding a
1685 /// expensive search for a easily answered common query.
1686 static bool isKnownNonConstant(Value *V) {
1687   V = V->stripPointerCasts();
1688   // The return val of alloc cannot be a Constant.
1689   if (isa<AllocaInst>(V))
1690     return true;
1691   return false;
1692 }
1693 
1694 Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB,
1695                                      Instruction *CxtI) {
1696   // Bail out early if V is known not to be a Constant.
1697   if (isKnownNonConstant(V))
1698     return nullptr;
1699 
1700   const DataLayout &DL = BB->getModule()->getDataLayout();
1701   ValueLatticeElement Result =
1702       getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
1703 
1704   if (Result.isConstant())
1705     return Result.getConstant();
1706   if (Result.isConstantRange()) {
1707     const ConstantRange &CR = Result.getConstantRange();
1708     if (const APInt *SingleVal = CR.getSingleElement())
1709       return ConstantInt::get(V->getContext(), *SingleVal);
1710   }
1711   return nullptr;
1712 }
1713 
1714 ConstantRange LazyValueInfo::getConstantRange(Value *V, BasicBlock *BB,
1715                                               Instruction *CxtI) {
1716   assert(V->getType()->isIntegerTy());
1717   unsigned Width = V->getType()->getIntegerBitWidth();
1718   const DataLayout &DL = BB->getModule()->getDataLayout();
1719   ValueLatticeElement Result =
1720       getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
1721   if (Result.isUndefined())
1722     return ConstantRange::getEmpty(Width);
1723   if (Result.isConstantRange())
1724     return Result.getConstantRange();
1725   // We represent ConstantInt constants as constant ranges but other kinds
1726   // of integer constants, i.e. ConstantExpr will be tagged as constants
1727   assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) &&
1728          "ConstantInt value must be represented as constantrange");
1729   return ConstantRange::getFull(Width);
1730 }
1731 
1732 /// Determine whether the specified value is known to be a
1733 /// constant on the specified edge. Return null if not.
1734 Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB,
1735                                            BasicBlock *ToBB,
1736                                            Instruction *CxtI) {
1737   const DataLayout &DL = FromBB->getModule()->getDataLayout();
1738   ValueLatticeElement Result =
1739       getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
1740 
1741   if (Result.isConstant())
1742     return Result.getConstant();
1743   if (Result.isConstantRange()) {
1744     const ConstantRange &CR = Result.getConstantRange();
1745     if (const APInt *SingleVal = CR.getSingleElement())
1746       return ConstantInt::get(V->getContext(), *SingleVal);
1747   }
1748   return nullptr;
1749 }
1750 
1751 ConstantRange LazyValueInfo::getConstantRangeOnEdge(Value *V,
1752                                                     BasicBlock *FromBB,
1753                                                     BasicBlock *ToBB,
1754                                                     Instruction *CxtI) {
1755   unsigned Width = V->getType()->getIntegerBitWidth();
1756   const DataLayout &DL = FromBB->getModule()->getDataLayout();
1757   ValueLatticeElement Result =
1758       getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
1759 
1760   if (Result.isUndefined())
1761     return ConstantRange::getEmpty(Width);
1762   if (Result.isConstantRange())
1763     return Result.getConstantRange();
1764   // We represent ConstantInt constants as constant ranges but other kinds
1765   // of integer constants, i.e. ConstantExpr will be tagged as constants
1766   assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) &&
1767          "ConstantInt value must be represented as constantrange");
1768   return ConstantRange::getFull(Width);
1769 }
1770 
1771 static LazyValueInfo::Tristate
1772 getPredicateResult(unsigned Pred, Constant *C, const ValueLatticeElement &Val,
1773                    const DataLayout &DL, TargetLibraryInfo *TLI) {
1774   // If we know the value is a constant, evaluate the conditional.
1775   Constant *Res = nullptr;
1776   if (Val.isConstant()) {
1777     Res = ConstantFoldCompareInstOperands(Pred, Val.getConstant(), C, DL, TLI);
1778     if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res))
1779       return ResCI->isZero() ? LazyValueInfo::False : LazyValueInfo::True;
1780     return LazyValueInfo::Unknown;
1781   }
1782 
1783   if (Val.isConstantRange()) {
1784     ConstantInt *CI = dyn_cast<ConstantInt>(C);
1785     if (!CI) return LazyValueInfo::Unknown;
1786 
1787     const ConstantRange &CR = Val.getConstantRange();
1788     if (Pred == ICmpInst::ICMP_EQ) {
1789       if (!CR.contains(CI->getValue()))
1790         return LazyValueInfo::False;
1791 
1792       if (CR.isSingleElement())
1793         return LazyValueInfo::True;
1794     } else if (Pred == ICmpInst::ICMP_NE) {
1795       if (!CR.contains(CI->getValue()))
1796         return LazyValueInfo::True;
1797 
1798       if (CR.isSingleElement())
1799         return LazyValueInfo::False;
1800     } else {
1801       // Handle more complex predicates.
1802       ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(
1803           (ICmpInst::Predicate)Pred, CI->getValue());
1804       if (TrueValues.contains(CR))
1805         return LazyValueInfo::True;
1806       if (TrueValues.inverse().contains(CR))
1807         return LazyValueInfo::False;
1808     }
1809     return LazyValueInfo::Unknown;
1810   }
1811 
1812   if (Val.isNotConstant()) {
1813     // If this is an equality comparison, we can try to fold it knowing that
1814     // "V != C1".
1815     if (Pred == ICmpInst::ICMP_EQ) {
1816       // !C1 == C -> false iff C1 == C.
1817       Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
1818                                             Val.getNotConstant(), C, DL,
1819                                             TLI);
1820       if (Res->isNullValue())
1821         return LazyValueInfo::False;
1822     } else if (Pred == ICmpInst::ICMP_NE) {
1823       // !C1 != C -> true iff C1 == C.
1824       Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
1825                                             Val.getNotConstant(), C, DL,
1826                                             TLI);
1827       if (Res->isNullValue())
1828         return LazyValueInfo::True;
1829     }
1830     return LazyValueInfo::Unknown;
1831   }
1832 
1833   return LazyValueInfo::Unknown;
1834 }
1835 
1836 /// Determine whether the specified value comparison with a constant is known to
1837 /// be true or false on the specified CFG edge. Pred is a CmpInst predicate.
1838 LazyValueInfo::Tristate
1839 LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C,
1840                                   BasicBlock *FromBB, BasicBlock *ToBB,
1841                                   Instruction *CxtI) {
1842   const DataLayout &DL = FromBB->getModule()->getDataLayout();
1843   ValueLatticeElement Result =
1844       getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
1845 
1846   return getPredicateResult(Pred, C, Result, DL, TLI);
1847 }
1848 
1849 LazyValueInfo::Tristate
1850 LazyValueInfo::getPredicateAt(unsigned Pred, Value *V, Constant *C,
1851                               Instruction *CxtI) {
1852   // Is or is not NonNull are common predicates being queried. If
1853   // isKnownNonZero can tell us the result of the predicate, we can
1854   // return it quickly. But this is only a fastpath, and falling
1855   // through would still be correct.
1856   const DataLayout &DL = CxtI->getModule()->getDataLayout();
1857   if (V->getType()->isPointerTy() && C->isNullValue() &&
1858       isKnownNonZero(V->stripPointerCastsSameRepresentation(), DL)) {
1859     if (Pred == ICmpInst::ICMP_EQ)
1860       return LazyValueInfo::False;
1861     else if (Pred == ICmpInst::ICMP_NE)
1862       return LazyValueInfo::True;
1863   }
1864   ValueLatticeElement Result = getImpl(PImpl, AC, &DL, DT).getValueAt(V, CxtI);
1865   Tristate Ret = getPredicateResult(Pred, C, Result, DL, TLI);
1866   if (Ret != Unknown)
1867     return Ret;
1868 
1869   // Note: The following bit of code is somewhat distinct from the rest of LVI;
1870   // LVI as a whole tries to compute a lattice value which is conservatively
1871   // correct at a given location.  In this case, we have a predicate which we
1872   // weren't able to prove about the merged result, and we're pushing that
1873   // predicate back along each incoming edge to see if we can prove it
1874   // separately for each input.  As a motivating example, consider:
1875   // bb1:
1876   //   %v1 = ... ; constantrange<1, 5>
1877   //   br label %merge
1878   // bb2:
1879   //   %v2 = ... ; constantrange<10, 20>
1880   //   br label %merge
1881   // merge:
1882   //   %phi = phi [%v1, %v2] ; constantrange<1,20>
1883   //   %pred = icmp eq i32 %phi, 8
1884   // We can't tell from the lattice value for '%phi' that '%pred' is false
1885   // along each path, but by checking the predicate over each input separately,
1886   // we can.
1887   // We limit the search to one step backwards from the current BB and value.
1888   // We could consider extending this to search further backwards through the
1889   // CFG and/or value graph, but there are non-obvious compile time vs quality
1890   // tradeoffs.
1891   if (CxtI) {
1892     BasicBlock *BB = CxtI->getParent();
1893 
1894     // Function entry or an unreachable block.  Bail to avoid confusing
1895     // analysis below.
1896     pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1897     if (PI == PE)
1898       return Unknown;
1899 
1900     // If V is a PHI node in the same block as the context, we need to ask
1901     // questions about the predicate as applied to the incoming value along
1902     // each edge. This is useful for eliminating cases where the predicate is
1903     // known along all incoming edges.
1904     if (auto *PHI = dyn_cast<PHINode>(V))
1905       if (PHI->getParent() == BB) {
1906         Tristate Baseline = Unknown;
1907         for (unsigned i = 0, e = PHI->getNumIncomingValues(); i < e; i++) {
1908           Value *Incoming = PHI->getIncomingValue(i);
1909           BasicBlock *PredBB = PHI->getIncomingBlock(i);
1910           // Note that PredBB may be BB itself.
1911           Tristate Result = getPredicateOnEdge(Pred, Incoming, C, PredBB, BB,
1912                                                CxtI);
1913 
1914           // Keep going as long as we've seen a consistent known result for
1915           // all inputs.
1916           Baseline = (i == 0) ? Result /* First iteration */
1917             : (Baseline == Result ? Baseline : Unknown); /* All others */
1918           if (Baseline == Unknown)
1919             break;
1920         }
1921         if (Baseline != Unknown)
1922           return Baseline;
1923       }
1924 
1925     // For a comparison where the V is outside this block, it's possible
1926     // that we've branched on it before. Look to see if the value is known
1927     // on all incoming edges.
1928     if (!isa<Instruction>(V) ||
1929         cast<Instruction>(V)->getParent() != BB) {
1930       // For predecessor edge, determine if the comparison is true or false
1931       // on that edge. If they're all true or all false, we can conclude
1932       // the value of the comparison in this block.
1933       Tristate Baseline = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1934       if (Baseline != Unknown) {
1935         // Check that all remaining incoming values match the first one.
1936         while (++PI != PE) {
1937           Tristate Ret = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1938           if (Ret != Baseline) break;
1939         }
1940         // If we terminated early, then one of the values didn't match.
1941         if (PI == PE) {
1942           return Baseline;
1943         }
1944       }
1945     }
1946   }
1947   return Unknown;
1948 }
1949 
1950 void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
1951                                BasicBlock *NewSucc) {
1952   if (PImpl) {
1953     const DataLayout &DL = PredBB->getModule()->getDataLayout();
1954     getImpl(PImpl, AC, &DL, DT).threadEdge(PredBB, OldSucc, NewSucc);
1955   }
1956 }
1957 
1958 void LazyValueInfo::eraseBlock(BasicBlock *BB) {
1959   if (PImpl) {
1960     const DataLayout &DL = BB->getModule()->getDataLayout();
1961     getImpl(PImpl, AC, &DL, DT).eraseBlock(BB);
1962   }
1963 }
1964 
1965 
1966 void LazyValueInfo::printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS) {
1967   if (PImpl) {
1968     getImpl(PImpl, AC, DL, DT).printLVI(F, DTree, OS);
1969   }
1970 }
1971 
1972 void LazyValueInfo::disableDT() {
1973   if (PImpl)
1974     getImpl(PImpl, AC, DL, DT).disableDT();
1975 }
1976 
1977 void LazyValueInfo::enableDT() {
1978   if (PImpl)
1979     getImpl(PImpl, AC, DL, DT).enableDT();
1980 }
1981 
1982 // Print the LVI for the function arguments at the start of each basic block.
1983 void LazyValueInfoAnnotatedWriter::emitBasicBlockStartAnnot(
1984     const BasicBlock *BB, formatted_raw_ostream &OS) {
1985   // Find if there are latticevalues defined for arguments of the function.
1986   auto *F = BB->getParent();
1987   for (auto &Arg : F->args()) {
1988     ValueLatticeElement Result = LVIImpl->getValueInBlock(
1989         const_cast<Argument *>(&Arg), const_cast<BasicBlock *>(BB));
1990     if (Result.isUndefined())
1991       continue;
1992     OS << "; LatticeVal for: '" << Arg << "' is: " << Result << "\n";
1993   }
1994 }
1995 
1996 // This function prints the LVI analysis for the instruction I at the beginning
1997 // of various basic blocks. It relies on calculated values that are stored in
1998 // the LazyValueInfoCache, and in the absence of cached values, recalculate the
1999 // LazyValueInfo for `I`, and print that info.
2000 void LazyValueInfoAnnotatedWriter::emitInstructionAnnot(
2001     const Instruction *I, formatted_raw_ostream &OS) {
2002 
2003   auto *ParentBB = I->getParent();
2004   SmallPtrSet<const BasicBlock*, 16> BlocksContainingLVI;
2005   // We can generate (solve) LVI values only for blocks that are dominated by
2006   // the I's parent. However, to avoid generating LVI for all dominating blocks,
2007   // that contain redundant/uninteresting information, we print LVI for
2008   // blocks that may use this LVI information (such as immediate successor
2009   // blocks, and blocks that contain uses of `I`).
2010   auto printResult = [&](const BasicBlock *BB) {
2011     if (!BlocksContainingLVI.insert(BB).second)
2012       return;
2013     ValueLatticeElement Result = LVIImpl->getValueInBlock(
2014         const_cast<Instruction *>(I), const_cast<BasicBlock *>(BB));
2015       OS << "; LatticeVal for: '" << *I << "' in BB: '";
2016       BB->printAsOperand(OS, false);
2017       OS << "' is: " << Result << "\n";
2018   };
2019 
2020   printResult(ParentBB);
2021   // Print the LVI analysis results for the immediate successor blocks, that
2022   // are dominated by `ParentBB`.
2023   for (auto *BBSucc : successors(ParentBB))
2024     if (DT.dominates(ParentBB, BBSucc))
2025       printResult(BBSucc);
2026 
2027   // Print LVI in blocks where `I` is used.
2028   for (auto *U : I->users())
2029     if (auto *UseI = dyn_cast<Instruction>(U))
2030       if (!isa<PHINode>(UseI) || DT.dominates(ParentBB, UseI->getParent()))
2031         printResult(UseI->getParent());
2032 
2033 }
2034 
2035 namespace {
2036 // Printer class for LazyValueInfo results.
2037 class LazyValueInfoPrinter : public FunctionPass {
2038 public:
2039   static char ID; // Pass identification, replacement for typeid
2040   LazyValueInfoPrinter() : FunctionPass(ID) {
2041     initializeLazyValueInfoPrinterPass(*PassRegistry::getPassRegistry());
2042   }
2043 
2044   void getAnalysisUsage(AnalysisUsage &AU) const override {
2045     AU.setPreservesAll();
2046     AU.addRequired<LazyValueInfoWrapperPass>();
2047     AU.addRequired<DominatorTreeWrapperPass>();
2048   }
2049 
2050   // Get the mandatory dominator tree analysis and pass this in to the
2051   // LVIPrinter. We cannot rely on the LVI's DT, since it's optional.
2052   bool runOnFunction(Function &F) override {
2053     dbgs() << "LVI for function '" << F.getName() << "':\n";
2054     auto &LVI = getAnalysis<LazyValueInfoWrapperPass>().getLVI();
2055     auto &DTree = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
2056     LVI.printLVI(F, DTree, dbgs());
2057     return false;
2058   }
2059 };
2060 }
2061 
2062 char LazyValueInfoPrinter::ID = 0;
2063 INITIALIZE_PASS_BEGIN(LazyValueInfoPrinter, "print-lazy-value-info",
2064                 "Lazy Value Info Printer Pass", false, false)
2065 INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass)
2066 INITIALIZE_PASS_END(LazyValueInfoPrinter, "print-lazy-value-info",
2067                 "Lazy Value Info Printer Pass", false, false)
2068