LCOV - code coverage report
Current view: top level - lib/Analysis - LazyValueInfo.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 537 570 94.2 %
Date: 2018-02-18 03:11:45 Functions: 71 79 89.9 %
Legend: Lines: hit not hit

          Line data    Source code
       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       28952 : INITIALIZE_PASS_BEGIN(LazyValueInfoWrapperPass, "lazy-value-info",
      51             :                 "Lazy Value Information Analysis", false, true)
      52       28952 : INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
      53       28952 : INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
      54      217322 : INITIALIZE_PASS_END(LazyValueInfoWrapperPass, "lazy-value-info",
      55             :                 "Lazy Value Information Analysis", false, true)
      56             : 
      57             : namespace llvm {
      58           0 :   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     1196139 :   if (Val.isConstantRange() &&
      68             :       Val.getConstantRange().isSingleElement())
      69             :     // Integer constants are single element ranges
      70             :     return true;
      71     1143154 :   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      667348 : 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      667348 :   if (A.isUndefined())
      96             :     return A;
      97      667348 :   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      667348 :   if (A.isOverdefined())
     102             :     return B;
     103       23384 :   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       17349 :   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       17330 :     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        8665 :   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      326900 :       : CallbackVH(V), Parent(P) { }
     142             : 
     143             :     void deleted() override;
     144         133 :     void allUsesReplacedWith(Value *V) override {
     145             :       deleted();
     146         133 :     }
     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      400883 :   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      326900 :     struct ValueCacheEntryTy {
     160      490350 :       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      778911 :     void insertResult(Value *Val, BasicBlock *BB,
     181             :                       const ValueLatticeElement &Result) {
     182      778911 :       SeenBlocks.insert(BB);
     183             : 
     184             :       // Insert over-defined values into their own cache to reduce memory
     185             :       // overhead.
     186      778911 :       if (Result.isOverdefined())
     187      892120 :         OverDefinedCache[BB].insert(Val);
     188             :       else {
     189      332851 :         auto It = ValueCache.find_as(Val);
     190      332851 :         if (It == ValueCache.end()) {
     191      163450 :           ValueCache[Val] = make_unique<ValueCacheEntryTy>(Val, this);
     192      163450 :           It = ValueCache.find_as(Val);
     193             :           assert(It != ValueCache.end() && "Val was just added to the map!");
     194             :         }
     195      665702 :         It->second->BlockVals[BB] = Result;
     196             :       }
     197      778911 :     }
     198             : 
     199     3790303 :     bool isOverdefined(Value *V, BasicBlock *BB) const {
     200     7580606 :       auto ODI = OverDefinedCache.find(BB);
     201             : 
     202     3790303 :       if (ODI == OverDefinedCache.end())
     203             :         return false;
     204             : 
     205     2658704 :       return ODI->second.count(V);
     206             :     }
     207             : 
     208     2683716 :     bool hasCachedValueInfo(Value *V, BasicBlock *BB) const {
     209     2683716 :       if (isOverdefined(V, BB))
     210             :         return true;
     211             : 
     212     2256554 :       auto I = ValueCache.find_as(V);
     213     2256554 :       if (I == ValueCache.end())
     214             :         return false;
     215             : 
     216     1723786 :       return I->second->BlockVals.count(BB);
     217             :     }
     218             : 
     219     1106587 :     ValueLatticeElement getCachedValueInfo(Value *V, BasicBlock *BB) const {
     220     1106587 :       if (isOverdefined(V, BB))
     221             :         return ValueLatticeElement::getOverdefined();
     222             : 
     223      524797 :       auto I = ValueCache.find_as(V);
     224      524797 :       if (I == ValueCache.end())
     225             :         return ValueLatticeElement();
     226     1049594 :       auto BBI = I->second->BlockVals.find(BB);
     227      524797 :       if (BBI == I->second->BlockVals.end())
     228             :         return ValueLatticeElement();
     229      524797 :       return BBI->second;
     230             :     }
     231             : 
     232             :     /// clear - Empty the cache.
     233           0 :     void clear() {
     234             :       SeenBlocks.clear();
     235           0 :       ValueCache.clear();
     236           0 :       OverDefinedCache.clear();
     237           0 :     }
     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         171 : void LazyValueInfoCache::eraseValue(Value *V) {
     256        6902 :   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        3280 :     ValueSet.erase(V);
     262        3280 :     if (ValueSet.empty())
     263             :       OverDefinedCache.erase(Iter);
     264             :   }
     265             : 
     266         171 :   ValueCache.erase(V);
     267         171 : }
     268             : 
     269          38 : 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         171 :   Parent->eraseValue(*this);
     273          38 : }
     274             : 
     275       27434 : void LazyValueInfoCache::eraseBlock(BasicBlock *BB) {
     276             :   // Shortcut if we have never seen this block.
     277       27434 :   DenseSet<PoisoningVH<BasicBlock> >::iterator I = SeenBlocks.find(BB);
     278       27434 :   if (I == SeenBlocks.end())
     279       23645 :     return;
     280             :   SeenBlocks.erase(I);
     281             : 
     282        7578 :   auto ODI = OverDefinedCache.find(BB);
     283        3789 :   if (ODI != OverDefinedCache.end())
     284             :     OverDefinedCache.erase(ODI);
     285             : 
     286       30600 :   for (auto &I : ValueCache)
     287       46044 :     I.second->BlockVals.erase(BB);
     288             : }
     289             : 
     290        1415 : 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        1415 :   worklist.push_back(OldSucc);
     304             : 
     305        2830 :   auto I = OverDefinedCache.find(OldSucc);
     306        1415 :   if (I == OverDefinedCache.end())
     307             :     return; // Nothing to process here.
     308          42 :   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         244 :   while (!worklist.empty()) {
     315         202 :     BasicBlock *ToUpdate = worklist.back();
     316             :     worklist.pop_back();
     317             : 
     318             :     // Skip blocks only accessible through NewSucc.
     319         318 :     if (ToUpdate == NewSucc) continue;
     320             : 
     321             :     // If a value was marked overdefined in OldSucc, and is here too...
     322         158 :     auto OI = OverDefinedCache.find(ToUpdate);
     323         158 :     if (OI == OverDefinedCache.end())
     324          52 :       continue;
     325             :     SmallPtrSetImpl<Value *> &ValueSet = OI->second;
     326             : 
     327             :     bool changed = false;
     328         542 :     for (Value *V : ValsToClear) {
     329         280 :       if (!ValueSet.erase(V))
     330         143 :         continue;
     331             : 
     332             :       // If we removed anything, then we potentially need to update
     333             :       // blocks successors too.
     334             :       changed = true;
     335             : 
     336         137 :       if (ValueSet.empty()) {
     337             :         OverDefinedCache.erase(OI);
     338             :         break;
     339             :       }
     340             :     }
     341             : 
     342          44 :     if (!changed) continue;
     343             : 
     344         172 :     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           4 : 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           4 :       : 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      114538 :   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      792287 :     bool pushBlockValue(const std::pair<BasicBlock *, Value *> &BV) {
     392      792287 :       if (!BlockValueSet.insert(BV).second)
     393             :         return false;  // It's already in the stack.
     394             : 
     395             :       DEBUG(dbgs() << "PUSH: " << *BV.second << " in " << BV.first->getName()
     396             :                    << "\n");
     397      778911 :       BlockValueStack.push_back(BV);
     398      778911 :       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           0 :       TheCache.clear();
     453             :     }
     454             : 
     455             :     /// Printing the LazyValueInfo Analysis.
     456           4 :     void printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS) {
     457             :         LazyValueInfoAnnotatedWriter Writer(this, DTree);
     458           4 :         F.print(OS, &Writer);
     459           4 :     }
     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       27434 :       TheCache.eraseBlock(BB);
     465             :     }
     466             : 
     467             :     /// Disables use of the DominatorTree within LVI.
     468             :     void disableDT() {
     469      104263 :       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       71653 :       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      114538 :         : AC(AC), DL(DL), DT(DT), DisabledDT(nullptr) {}
     491             :   };
     492             : } // end anonymous namespace
     493             : 
     494             : 
     495      464228 : void LazyValueInfoImpl::solve() {
     496             :   SmallVector<std::pair<BasicBlock *, Value *>, 8> StartingStack(
     497             :       BlockValueStack.begin(), BlockValueStack.end());
     498             : 
     499             :   unsigned processedCount = 0;
     500     2651416 :   while (!BlockValueStack.empty()) {
     501     1093594 :     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     1093594 :     if (processedCount > MaxProcessedPerValue) {
     511             :       DEBUG(dbgs() << "Giving up on stack because we are getting too deep\n");
     512             :       // Fill in the original values
     513           0 :       while (!StartingStack.empty()) {
     514             :         std::pair<BasicBlock *, Value *> &e = StartingStack.back();
     515           0 :         TheCache.insertResult(e.second, e.first,
     516           0 :                               ValueLatticeElement::getOverdefined());
     517             :         StartingStack.pop_back();
     518             :       }
     519             :       BlockValueSet.clear();
     520             :       BlockValueStack.clear();
     521           0 :       return;
     522             :     }
     523     1093594 :     std::pair<BasicBlock *, Value *> e = BlockValueStack.back();
     524             :     assert(BlockValueSet.count(e) && "Stack value should be in BlockValueSet!");
     525             : 
     526     1093594 :     if (solveBlockValue(e.second, e.first)) {
     527             :       // The work item was completely processed.
     528             :       assert(BlockValueStack.back() == e && "Nothing should have been pushed!");
     529             :       assert(TheCache.hasCachedValueInfo(e.second, e.first) &&
     530             :              "Result should be in cache!");
     531             : 
     532             :       DEBUG(dbgs() << "POP " << *e.second << " in " << e.first->getName()
     533             :                    << " = " << TheCache.getCachedValueInfo(e.second, e.first) << "\n");
     534             : 
     535             :       BlockValueStack.pop_back();
     536             :       BlockValueSet.erase(e);
     537             :     } else {
     538             :       // More work needs to be done before revisiting.
     539             :       assert(BlockValueStack.back() != e && "Stack should have been pushed!");
     540             :     }
     541             :   }
     542             : }
     543             : 
     544             : bool LazyValueInfoImpl::hasBlockValue(Value *Val, BasicBlock *BB) {
     545             :   // If already a constant, there is nothing to compute.
     546     1590920 :   if (isa<Constant>(Val))
     547             :     return true;
     548             : 
     549     1590122 :   return TheCache.hasCachedValueInfo(Val, BB);
     550             : }
     551             : 
     552     1107372 : ValueLatticeElement LazyValueInfoImpl::getBlockValue(Value *Val,
     553             :                                                      BasicBlock *BB) {
     554             :   // If already a constant, there is nothing to compute.
     555             :   if (Constant *VC = dyn_cast<Constant>(Val))
     556             :     return ValueLatticeElement::get(VC);
     557             : 
     558     1106587 :   return TheCache.getCachedValueInfo(Val, BB);
     559             : }
     560             : 
     561      374556 : static ValueLatticeElement getFromRangeMetadata(Instruction *BBI) {
     562             :   switch (BBI->getOpcode()) {
     563             :   default: break;
     564             :   case Instruction::Load:
     565             :   case Instruction::Call:
     566             :   case Instruction::Invoke:
     567      125046 :     if (MDNode *Ranges = BBI->getMetadata(LLVMContext::MD_range))
     568       13130 :       if (isa<IntegerType>(BBI->getType())) {
     569             :         return ValueLatticeElement::getRange(
     570       13130 :             getConstantRangeFromMetadata(*Ranges));
     571             :       }
     572             :     break;
     573             :   };
     574             :   // Nothing known - will be intersected with other facts
     575             :   return ValueLatticeElement::getOverdefined();
     576             : }
     577             : 
     578     1093594 : bool LazyValueInfoImpl::solveBlockValue(Value *Val, BasicBlock *BB) {
     579     1093594 :   if (isa<Constant>(Val))
     580             :     return true;
     581             : 
     582     1093594 :   if (TheCache.hasCachedValueInfo(Val, BB)) {
     583             :     // If we have a cached value, use that.
     584             :     DEBUG(dbgs() << "  reuse BB '" << BB->getName()
     585             :                  << "' val=" << TheCache.getCachedValueInfo(Val, BB) << '\n');
     586             : 
     587             :     // Since we're reusing a cached value, we don't need to update the
     588             :     // OverDefinedCache. The cache will have been properly updated whenever the
     589             :     // cached value was inserted.
     590             :     return true;
     591             :   }
     592             : 
     593             :   // Hold off inserting this value into the Cache in case we have to return
     594             :   // false and come back later.
     595             :   ValueLatticeElement Res;
     596     1093594 :   if (!solveBlockValueImpl(Res, Val, BB))
     597             :     // Work pushed, will revisit
     598             :     return false;
     599             : 
     600      778911 :   TheCache.insertResult(Val, BB, Res);
     601      778911 :   return true;
     602             : }
     603             : 
     604     1093594 : bool LazyValueInfoImpl::solveBlockValueImpl(ValueLatticeElement &Res,
     605             :                                             Value *Val, BasicBlock *BB) {
     606             : 
     607             :   Instruction *BBI = dyn_cast<Instruction>(Val);
     608     1021123 :   if (!BBI || BBI->getParent() != BB)
     609      609411 :     return solveBlockValueNonLocal(Res, Val, BB);
     610             : 
     611             :   if (PHINode *PN = dyn_cast<PHINode>(BBI))
     612       91165 :     return solveBlockValuePHINode(Res, PN, BB);
     613             : 
     614             :   if (auto *SI = dyn_cast<SelectInst>(BBI))
     615        1654 :     return solveBlockValueSelect(Res, SI, BB);
     616             : 
     617             :   // If this value is a nonnull pointer, record it's range and bailout.  Note
     618             :   // that for all other pointer typed values, we terminate the search at the
     619             :   // definition.  We could easily extend this to look through geps, bitcasts,
     620             :   // and the like to prove non-nullness, but it's not clear that's worth it
     621             :   // compile time wise.  The context-insensitive value walk done inside
     622             :   // isKnownNonZero gets most of the profitable cases at much less expense.
     623             :   // This does mean that we have a sensativity to where the defining
     624             :   // instruction is placed, even if it could legally be hoisted much higher.
     625             :   // That is unfortunate.
     626             :   PointerType *PT = dyn_cast<PointerType>(BBI->getType());
     627      237672 :   if (PT && isKnownNonZero(BBI, DL)) {
     628      343779 :     Res = ValueLatticeElement::getNot(ConstantPointerNull::get(PT));
     629      114593 :     return true;
     630             :   }
     631      276771 :   if (BBI->getType()->isIntegerTy()) {
     632             :     if (auto *CI = dyn_cast<CastInst>(BBI))
     633        3811 :       return solveBlockValueCast(Res, CI, BB);
     634             : 
     635             :     BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI);
     636       25383 :     if (BO && isa<ConstantInt>(BO->getOperand(1)))
     637       20872 :       return solveBlockValueBinaryOp(Res, BO, BB);
     638             :   }
     639             : 
     640             :   DEBUG(dbgs() << " compute BB '" << BB->getName()
     641             :                  << "' - unknown inst def found.\n");
     642      504176 :   Res = getFromRangeMetadata(BBI);
     643      252088 :   return true;
     644             : }
     645             : 
     646     1469716 : static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr) {
     647             :   if (LoadInst *L = dyn_cast<LoadInst>(I)) {
     648      635376 :     return L->getPointerAddressSpace() == 0 &&
     649      952497 :            GetUnderlyingObject(L->getPointerOperand(),
     650             :                                L->getModule()->getDataLayout()) == Ptr;
     651             :   }
     652             :   if (StoreInst *S = dyn_cast<StoreInst>(I)) {
     653      690729 :     return S->getPointerAddressSpace() == 0 &&
     654     1034886 :            GetUnderlyingObject(S->getPointerOperand(),
     655             :                                S->getModule()->getDataLayout()) == Ptr;
     656             :   }
     657             :   if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
     658        1294 :     if (MI->isVolatile()) return false;
     659             : 
     660             :     // FIXME: check whether it has a valuerange that excludes zero?
     661             :     ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength());
     662         893 :     if (!Len || Len->isZero()) return false;
     663             : 
     664         893 :     if (MI->getDestAddressSpace() == 0)
     665        2673 :       if (GetUnderlyingObject(MI->getRawDest(),
     666             :                               MI->getModule()->getDataLayout()) == Ptr)
     667             :         return true;
     668             :     if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))
     669         436 :       if (MTI->getSourceAddressSpace() == 0)
     670        1302 :         if (GetUnderlyingObject(MTI->getRawSource(),
     671             :                                 MTI->getModule()->getDataLayout()) == Ptr)
     672             :           return true;
     673             :   }
     674             :   return false;
     675             : }
     676             : 
     677             : /// Return true if the allocation associated with Val is ever dereferenced
     678             : /// within the given basic block.  This establishes the fact Val is not null,
     679             : /// but does not imply that the memory at Val is dereferenceable.  (Val may
     680             : /// point off the end of the dereferenceable part of the object.)
     681      113995 : static bool isObjectDereferencedInBlock(Value *Val, BasicBlock *BB) {
     682             :   assert(Val->getType()->isPointerTy());
     683             : 
     684      113995 :   const DataLayout &DL = BB->getModule()->getDataLayout();
     685      113995 :   Value *UnderlyingVal = GetUnderlyingObject(Val, DL);
     686             :   // If 'GetUnderlyingObject' didn't converge, skip it. It won't converge
     687             :   // inside InstructionDereferencesPointer either.
     688      113995 :   if (UnderlyingVal == GetUnderlyingObject(UnderlyingVal, DL, 1))
     689     1565758 :     for (Instruction &I : *BB)
     690     1469716 :       if (InstructionDereferencesPointer(&I, UnderlyingVal))
     691             :         return true;
     692             :   return false;
     693             : }
     694             : 
     695      609411 : bool LazyValueInfoImpl::solveBlockValueNonLocal(ValueLatticeElement &BBLV,
     696             :                                                  Value *Val, BasicBlock *BB) {
     697             :   ValueLatticeElement Result;  // Start Undefined.
     698             : 
     699             :   // If this is the entry block, we must be asking about an argument.  The
     700             :   // value is overdefined.
     701      609411 :   if (BB == &BB->getParent()->getEntryBlock()) {
     702             :     assert(isa<Argument>(Val) && "Unknown live-in to the entry block");
     703             :     // Before giving up, see if we can prove the pointer non-null local to
     704             :     // this particular block.
     705       50496 :     if (Val->getType()->isPointerTy() &&
     706       38609 :         (isKnownNonZero(Val, DL) || isObjectDereferencedInBlock(Val, BB))) {
     707             :       PointerType *PTy = cast<PointerType>(Val->getType());
     708       36585 :       Result = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy));
     709             :     } else {
     710       28306 :       Result = ValueLatticeElement::getOverdefined();
     711             :     }
     712       26348 :     BBLV = Result;
     713       26348 :     return true;
     714             :   }
     715             : 
     716             :   // Loop over all of our predecessors, merging what we know from them into
     717             :   // result.  If we encounter an unexplored predecessor, we eagerly explore it
     718             :   // in a depth first manner.  In practice, this has the effect of discovering
     719             :   // paths we can't analyze eagerly without spending compile times analyzing
     720             :   // other paths.  This heuristic benefits from the fact that predecessors are
     721             :   // frequently arranged such that dominating ones come first and we quickly
     722             :   // find a path to function entry.  TODO: We should consider explicitly
     723             :   // canonicalizing to make this true rather than relying on this happy
     724             :   // accident.  
     725     1418049 :   for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
     726             :     ValueLatticeElement EdgeResult;
     727      666585 :     if (!getEdgeValue(Val, *PI, BB, EdgeResult))
     728             :       // Explore that input, then return here
     729             :       return false;
     730             : 
     731      397980 :     Result.mergeIn(EdgeResult, DL);
     732             : 
     733             :     // If we hit overdefined, exit early.  The BlockVals entry is already set
     734             :     // to overdefined.
     735      397980 :     if (Result.isOverdefined()) {
     736             :       DEBUG(dbgs() << " compute BB '" << BB->getName()
     737             :             << "' - overdefined because of pred (non local).\n");
     738             :       // Before giving up, see if we can prove the pointer non-null local to
     739             :       // this particular block.
     740      245591 :       if (Val->getType()->isPointerTy() &&
     741       99534 :           isObjectDereferencedInBlock(Val, BB)) {
     742             :         PointerType *PTy = cast<PointerType>(Val->getType());
     743       46335 :         Result = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy));
     744             :       }
     745             : 
     746      146057 :       BBLV = Result;
     747      146057 :       return true;
     748             :     }
     749             :   }
     750             : 
     751             :   // Return the merged value, which is more precise than 'overdefined'.
     752             :   assert(!Result.isOverdefined());
     753      168401 :   BBLV = Result;
     754      168401 :   return true;
     755             : }
     756             : 
     757       91165 : bool LazyValueInfoImpl::solveBlockValuePHINode(ValueLatticeElement &BBLV,
     758             :                                                PHINode *PN, BasicBlock *BB) {
     759             :   ValueLatticeElement Result;  // Start Undefined.
     760             : 
     761             :   // Loop over all of our predecessors, merging what we know from them into
     762             :   // result.  See the comment about the chosen traversal order in
     763             :   // solveBlockValueNonLocal; the same reasoning applies here.
     764      129229 :   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
     765             :     BasicBlock *PhiBB = PN->getIncomingBlock(i);
     766             :     Value *PhiVal = PN->getIncomingValue(i);
     767             :     ValueLatticeElement EdgeResult;
     768             :     // Note that we can provide PN as the context value to getEdgeValue, even
     769             :     // though the results will be cached, because PN is the value being used as
     770             :     // the cache key in the caller.
     771      105225 :     if (!getEdgeValue(PhiVal, PhiBB, BB, EdgeResult, PN))
     772             :       // Explore that input, then return here
     773             :       return false;
     774             : 
     775       71138 :     Result.mergeIn(EdgeResult, DL);
     776             : 
     777             :     // If we hit overdefined, exit early.  The BlockVals entry is already set
     778             :     // to overdefined.
     779       71138 :     if (Result.isOverdefined()) {
     780             :       DEBUG(dbgs() << " compute BB '" << BB->getName()
     781             :             << "' - overdefined because of pred (local).\n");
     782             : 
     783       52106 :       BBLV = Result;
     784       52106 :       return true;
     785             :     }
     786             :   }
     787             : 
     788             :   // Return the merged value, which is more precise than 'overdefined'.
     789             :   assert(!Result.isOverdefined() && "Possible PHI in entry block?");
     790        4972 :   BBLV = Result;
     791        4972 :   return true;
     792             : }
     793             : 
     794             : static ValueLatticeElement getValueFromCondition(Value *Val, Value *Cond,
     795             :                                                  bool isTrueDest = true);
     796             : 
     797             : // If we can determine a constraint on the value given conditions assumed by
     798             : // the program, intersect those constraints with BBLV
     799     1927052 : void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange(
     800             :         Value *Val, ValueLatticeElement &BBLV, Instruction *BBI) {
     801     1927052 :   BBI = BBI ? BBI : dyn_cast<Instruction>(Val);
     802     1899705 :   if (!BBI)
     803             :     return;
     804             : 
     805     3799538 :   for (auto &AssumeVH : AC->assumptionsFor(Val)) {
     806          64 :     if (!AssumeVH)
     807             :       continue;
     808             :     auto *I = cast<CallInst>(AssumeVH);
     809          64 :     if (!isValidAssumeForContext(I, BBI, DT))
     810             :       continue;
     811             : 
     812          78 :     BBLV = intersect(BBLV, getValueFromCondition(Val, I->getArgOperand(0)));
     813             :   }
     814             : 
     815             :   // If guards are not used in the module, don't spend time looking for them
     816     1899705 :   auto *GuardDecl = BBI->getModule()->getFunction(
     817     1899705 :           Intrinsic::getName(Intrinsic::experimental_guard));
     818     1899705 :   if (!GuardDecl || GuardDecl->use_empty())
     819             :     return;
     820             : 
     821         394 :   for (Instruction &I : make_range(BBI->getIterator().getReverse(),
     822        1467 :                                    BBI->getParent()->rend())) {
     823        1073 :     Value *Cond = nullptr;
     824        1073 :     if (match(&I, m_Intrinsic<Intrinsic::experimental_guard>(m_Value(Cond))))
     825         156 :       BBLV = intersect(BBLV, getValueFromCondition(Val, Cond));
     826             :   }
     827             : }
     828             : 
     829        1654 : bool LazyValueInfoImpl::solveBlockValueSelect(ValueLatticeElement &BBLV,
     830             :                                               SelectInst *SI, BasicBlock *BB) {
     831             : 
     832             :   // Recurse on our inputs if needed
     833        1090 :   if (!hasBlockValue(SI->getTrueValue(), BB)) {
     834         493 :     if (pushBlockValue(std::make_pair(BB, SI->getTrueValue())))
     835             :       return false;
     836           2 :     BBLV = ValueLatticeElement::getOverdefined();
     837           1 :     return true;
     838             :   }
     839        1161 :   ValueLatticeElement TrueVal = getBlockValue(SI->getTrueValue(), BB);
     840             :   // If we hit overdefined, don't ask more queries.  We want to avoid poisoning
     841             :   // extra slots in the table if we can.
     842        1161 :   if (TrueVal.isOverdefined()) {
     843         670 :     BBLV = ValueLatticeElement::getOverdefined();
     844         335 :     return true;
     845             :   }
     846             : 
     847         652 :   if (!hasBlockValue(SI->getFalseValue(), BB)) {
     848         306 :     if (pushBlockValue(std::make_pair(BB, SI->getFalseValue())))
     849             :       return false;
     850           0 :     BBLV = ValueLatticeElement::getOverdefined();
     851           0 :     return true;
     852             :   }
     853         520 :   ValueLatticeElement FalseVal = getBlockValue(SI->getFalseValue(), BB);
     854             :   // If we hit overdefined, don't ask more queries.  We want to avoid poisoning
     855             :   // extra slots in the table if we can.
     856         520 :   if (FalseVal.isOverdefined()) {
     857         386 :     BBLV = ValueLatticeElement::getOverdefined();
     858         193 :     return true;
     859             :   }
     860             : 
     861         327 :   if (TrueVal.isConstantRange() && FalseVal.isConstantRange()) {
     862             :     const ConstantRange &TrueCR = TrueVal.getConstantRange();
     863             :     const ConstantRange &FalseCR = FalseVal.getConstantRange();
     864         272 :     Value *LHS = nullptr;
     865         272 :     Value *RHS = nullptr;
     866         272 :     SelectPatternResult SPR = matchSelectPattern(SI, LHS, RHS);
     867             :     // Is this a min specifically of our two inputs?  (Avoid the risk of
     868             :     // ValueTracking getting smarter looking back past our immediate inputs.)
     869         359 :     if (SelectPatternResult::isMinOrMax(SPR.Flavor) &&
     870         174 :         LHS == SI->getTrueValue() && RHS == SI->getFalseValue()) {
     871          87 :       ConstantRange ResultCR = [&]() {
     872          87 :         switch (SPR.Flavor) {
     873           0 :         default:
     874           0 :           llvm_unreachable("unexpected minmax type!");
     875           3 :         case SPF_SMIN:                   /// Signed minimum
     876          87 :           return TrueCR.smin(FalseCR);
     877          79 :         case SPF_UMIN:                   /// Unsigned minimum
     878          79 :           return TrueCR.umin(FalseCR);
     879           1 :         case SPF_SMAX:                   /// Signed maximum
     880           1 :           return TrueCR.smax(FalseCR);
     881           4 :         case SPF_UMAX:                   /// Unsigned maximum
     882           4 :           return TrueCR.umax(FalseCR);
     883             :         };
     884         174 :       }();
     885         174 :       BBLV = ValueLatticeElement::getRange(ResultCR);
     886             :       return true;
     887             :     }
     888             : 
     889             :     // TODO: ABS, NABS from the SelectPatternResult
     890             :   }
     891             : 
     892             :   // Can we constrain the facts about the true and false values by using the
     893             :   // condition itself?  This shows up with idioms like e.g. select(a > 5, a, 5).
     894             :   // TODO: We could potentially refine an overdefined true value above.
     895             :   Value *Cond = SI->getCondition();
     896         480 :   TrueVal = intersect(TrueVal,
     897         480 :                       getValueFromCondition(SI->getTrueValue(), Cond, true));
     898         480 :   FalseVal = intersect(FalseVal,
     899         480 :                        getValueFromCondition(SI->getFalseValue(), Cond, false));
     900             : 
     901             :   // Handle clamp idioms such as:
     902             :   //   %24 = constantrange<0, 17>
     903             :   //   %39 = icmp eq i32 %24, 0
     904             :   //   %40 = add i32 %24, -1
     905             :   //   %siv.next = select i1 %39, i32 16, i32 %40
     906             :   //   %siv.next = constantrange<0, 17> not <-1, 17>
     907             :   // In general, this can handle any clamp idiom which tests the edge
     908             :   // condition via an equality or inequality.
     909             :   if (auto *ICI = dyn_cast<ICmpInst>(Cond)) {
     910             :     ICmpInst::Predicate Pred = ICI->getPredicate();
     911             :     Value *A = ICI->getOperand(0);
     912             :     if (ConstantInt *CIBase = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
     913           5 :       auto addConstants = [](ConstantInt *A, ConstantInt *B) {
     914             :         assert(A->getType() == B->getType());
     915          15 :         return ConstantInt::get(A->getType(), A->getValue() + B->getValue());
     916             :       };
     917             :       // See if either input is A + C2, subject to the constraint from the
     918             :       // condition that A != C when that input is used.  We can assume that
     919             :       // that input doesn't include C + C2.
     920             :       ConstantInt *CIAdded;
     921         155 :       switch (Pred) {
     922             :       default: break;
     923             :       case ICmpInst::ICMP_EQ:
     924         270 :         if (match(SI->getFalseValue(), m_Add(m_Specific(A),
     925             :                                              m_ConstantInt(CIAdded)))) {
     926           5 :           auto ResNot = addConstants(CIBase, CIAdded);
     927          10 :           FalseVal = intersect(FalseVal,
     928           5 :                                ValueLatticeElement::getNot(ResNot));
     929             :         }
     930             :         break;
     931             :       case ICmpInst::ICMP_NE:
     932          10 :         if (match(SI->getTrueValue(), m_Add(m_Specific(A),
     933             :                                             m_ConstantInt(CIAdded)))) {
     934           0 :           auto ResNot = addConstants(CIBase, CIAdded);
     935           0 :           TrueVal = intersect(TrueVal,
     936           0 :                               ValueLatticeElement::getNot(ResNot));
     937             :         }
     938             :         break;
     939             :       };
     940             :     }
     941             :   }
     942             : 
     943             :   ValueLatticeElement Result;  // Start Undefined.
     944         240 :   Result.mergeIn(TrueVal, DL);
     945         240 :   Result.mergeIn(FalseVal, DL);
     946         240 :   BBLV = Result;
     947             :   return true;
     948             : }
     949             : 
     950        3811 : bool LazyValueInfoImpl::solveBlockValueCast(ValueLatticeElement &BBLV,
     951             :                                             CastInst *CI,
     952             :                                             BasicBlock *BB) {
     953        3811 :   if (!CI->getOperand(0)->getType()->isSized()) {
     954             :     // Without knowing how wide the input is, we can't analyze it in any useful
     955             :     // way.
     956           0 :     BBLV = ValueLatticeElement::getOverdefined();
     957           0 :     return true;
     958             :   }
     959             : 
     960             :   // Filter out casts we don't know how to reason about before attempting to
     961             :   // recurse on our operand.  This can cut a long search short if we know we're
     962             :   // not going to be able to get any useful information anways.
     963             :   switch (CI->getOpcode()) {
     964             :   case Instruction::Trunc:
     965             :   case Instruction::SExt:
     966             :   case Instruction::ZExt:
     967             :   case Instruction::BitCast:
     968             :     break;
     969             :   default:
     970             :     // Unhandled instructions are overdefined.
     971             :     DEBUG(dbgs() << " compute BB '" << BB->getName()
     972             :                  << "' - overdefined (unknown cast).\n");
     973        1694 :     BBLV = ValueLatticeElement::getOverdefined();
     974         847 :     return true;
     975             :   }
     976             : 
     977             :   // Figure out the range of the LHS.  If that fails, we still apply the
     978             :   // transfer rule on the full set since we may be able to locally infer
     979             :   // interesting facts.
     980        2964 :   if (!hasBlockValue(CI->getOperand(0), BB))
     981        1470 :     if (pushBlockValue(std::make_pair(BB, CI->getOperand(0))))
     982             :       // More work to do before applying this transfer rule.
     983             :       return false;
     984             : 
     985             :   const unsigned OperandBitWidth =
     986        2988 :     DL.getTypeSizeInBits(CI->getOperand(0)->getType());
     987        2988 :   ConstantRange LHSRange = ConstantRange(OperandBitWidth);
     988        1494 :   if (hasBlockValue(CI->getOperand(0), BB)) {
     989        1494 :     ValueLatticeElement LHSVal = getBlockValue(CI->getOperand(0), BB);
     990        1494 :     intersectAssumeOrGuardBlockValueConstantRange(CI->getOperand(0), LHSVal,
     991             :                                                   CI);
     992        1494 :     if (LHSVal.isConstantRange())
     993             :       LHSRange = LHSVal.getConstantRange();
     994             :   }
     995             : 
     996             :   const unsigned ResultBitWidth = CI->getType()->getIntegerBitWidth();
     997             : 
     998             :   // NOTE: We're currently limited by the set of operations that ConstantRange
     999             :   // can evaluate symbolically.  Enhancing that set will allows us to analyze
    1000             :   // more definitions.
    1001        2988 :   BBLV = ValueLatticeElement::getRange(LHSRange.castOp(CI->getOpcode(),
    1002             :                                                        ResultBitWidth));
    1003             :   return true;
    1004             : }
    1005             : 
    1006       20872 : bool LazyValueInfoImpl::solveBlockValueBinaryOp(ValueLatticeElement &BBLV,
    1007             :                                                 BinaryOperator *BO,
    1008             :                                                 BasicBlock *BB) {
    1009             : 
    1010             :   assert(BO->getOperand(0)->getType()->isSized() &&
    1011             :          "all operands to binary operators are sized");
    1012             : 
    1013             :   // Filter out operators we don't know how to reason about before attempting to
    1014             :   // recurse on our operand(s).  This can cut a long search short if we know
    1015             :   // we're not going to be able to get any useful information anyways.
    1016             :   switch (BO->getOpcode()) {
    1017             :   case Instruction::Add:
    1018             :   case Instruction::Sub:
    1019             :   case Instruction::Mul:
    1020             :   case Instruction::UDiv:
    1021             :   case Instruction::Shl:
    1022             :   case Instruction::LShr:
    1023             :   case Instruction::AShr:
    1024             :   case Instruction::And:
    1025             :   case Instruction::Or:
    1026             :     // continue into the code below
    1027             :     break;
    1028             :   default:
    1029             :     // Unhandled instructions are overdefined.
    1030             :     DEBUG(dbgs() << " compute BB '" << BB->getName()
    1031             :                  << "' - overdefined (unknown binary operator).\n");
    1032        1384 :     BBLV = ValueLatticeElement::getOverdefined();
    1033         692 :     return true;
    1034             :   };
    1035             : 
    1036             :   // Figure out the range of the LHS.  If that fails, use a conservative range,
    1037             :   // but apply the transfer rule anyways.  This lets us pick up facts from
    1038             :   // expressions like "and i32 (call i32 @foo()), 32"
    1039       20167 :   if (!hasBlockValue(BO->getOperand(0), BB))
    1040        9731 :     if (pushBlockValue(std::make_pair(BB, BO->getOperand(0))))
    1041             :       // More work to do before applying this transfer rule.
    1042             :       return false;
    1043             : 
    1044             :   const unsigned OperandBitWidth =
    1045       20914 :     DL.getTypeSizeInBits(BO->getOperand(0)->getType());
    1046       20914 :   ConstantRange LHSRange = ConstantRange(OperandBitWidth);
    1047       10444 :   if (hasBlockValue(BO->getOperand(0), BB)) {
    1048       10449 :     ValueLatticeElement LHSVal = getBlockValue(BO->getOperand(0), BB);
    1049       10449 :     intersectAssumeOrGuardBlockValueConstantRange(BO->getOperand(0), LHSVal,
    1050             :                                                   BO);
    1051       10449 :     if (LHSVal.isConstantRange())
    1052             :       LHSRange = LHSVal.getConstantRange();
    1053             :   }
    1054             : 
    1055             :   ConstantInt *RHS = cast<ConstantInt>(BO->getOperand(1));
    1056       31371 :   ConstantRange RHSRange = ConstantRange(RHS->getValue());
    1057             : 
    1058             :   // NOTE: We're currently limited by the set of operations that ConstantRange
    1059             :   // can evaluate symbolically.  Enhancing that set will allows us to analyze
    1060             :   // more definitions.
    1061             :   Instruction::BinaryOps BinOp = BO->getOpcode();
    1062       20914 :   BBLV = ValueLatticeElement::getRange(LHSRange.binaryOp(BinOp, RHSRange));
    1063             :   return true;
    1064             : }
    1065             : 
    1066      381165 : static ValueLatticeElement getValueFromICmpCondition(Value *Val, ICmpInst *ICI,
    1067             :                                                      bool isTrueDest) {
    1068             :   Value *LHS = ICI->getOperand(0);
    1069             :   Value *RHS = ICI->getOperand(1);
    1070             :   CmpInst::Predicate Predicate = ICI->getPredicate();
    1071             : 
    1072      381165 :   if (isa<Constant>(RHS)) {
    1073      261804 :     if (ICI->isEquality() && LHS == Val) {
    1074             :       // We know that V has the RHS constant if this is a true SETEQ or
    1075             :       // false SETNE.
    1076       16714 :       if (isTrueDest == (Predicate == ICmpInst::ICMP_EQ))
    1077             :         return ValueLatticeElement::get(cast<Constant>(RHS));
    1078             :       else
    1079             :         return ValueLatticeElement::getNot(cast<Constant>(RHS));
    1080             :     }
    1081             :   }
    1082             : 
    1083      364451 :   if (!Val->getType()->isIntegerTy())
    1084             :     return ValueLatticeElement::getOverdefined();
    1085             : 
    1086             :   // Use ConstantRange::makeAllowedICmpRegion in order to determine the possible
    1087             :   // range of Val guaranteed by the condition. Recognize comparisons in the from
    1088             :   // of:
    1089             :   //  icmp <pred> Val, ...
    1090             :   //  icmp <pred> (add Val, Offset), ...
    1091             :   // The latter is the range checking idiom that InstCombine produces. Subtract
    1092             :   // the offset from the allowed range for RHS in this case.
    1093             : 
    1094             :   // Val or (add Val, Offset) can be on either hand of the comparison
    1095      246460 :   if (LHS != Val && !match(LHS, m_Add(m_Specific(Val), m_ConstantInt()))) {
    1096             :     std::swap(LHS, RHS);
    1097      114617 :     Predicate = CmpInst::getSwappedPredicate(Predicate);
    1098             :   }
    1099             : 
    1100      131541 :   ConstantInt *Offset = nullptr;
    1101      131541 :   if (LHS != Val)
    1102      224040 :     match(LHS, m_Add(m_Specific(Val), m_ConstantInt(Offset)));
    1103             : 
    1104      131541 :   if (LHS == Val || Offset) {
    1105             :     // Calculate the range of values that are allowed by the comparison
    1106             :     ConstantRange RHSRange(RHS->getType()->getIntegerBitWidth(),
    1107       39816 :                            /*isFullSet=*/true);
    1108             :     if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS))
    1109       22706 :       RHSRange = ConstantRange(CI->getValue());
    1110             :     else if (Instruction *I = dyn_cast<Instruction>(RHS))
    1111        5060 :       if (auto *Ranges = I->getMetadata(LLVMContext::MD_range))
    1112           4 :         RHSRange = getConstantRangeFromMetadata(*Ranges);
    1113             : 
    1114             :     // If we're interested in the false dest, invert the condition
    1115             :     CmpInst::Predicate Pred =
    1116       19908 :             isTrueDest ? Predicate : CmpInst::getInversePredicate(Predicate);
    1117             :     ConstantRange TrueValues =
    1118       39816 :             ConstantRange::makeAllowedICmpRegion(Pred, RHSRange);
    1119             : 
    1120       19908 :     if (Offset) // Apply the offset from above.
    1121         387 :       TrueValues = TrueValues.subtract(Offset->getValue());
    1122             : 
    1123       19908 :     return ValueLatticeElement::getRange(std::move(TrueValues));
    1124             :   }
    1125             : 
    1126             :   return ValueLatticeElement::getOverdefined();
    1127             : }
    1128             : 
    1129             : static ValueLatticeElement
    1130             : getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest,
    1131             :                       DenseMap<Value*, ValueLatticeElement> &Visited);
    1132             : 
    1133             : static ValueLatticeElement
    1134      431950 : getValueFromConditionImpl(Value *Val, Value *Cond, bool isTrueDest,
    1135             :                           DenseMap<Value*, ValueLatticeElement> &Visited) {
    1136             :   if (ICmpInst *ICI = dyn_cast<ICmpInst>(Cond))
    1137      381165 :     return getValueFromICmpCondition(Val, ICI, isTrueDest);
    1138             : 
    1139             :   // Handle conditions in the form of (cond1 && cond2), we know that on the
    1140             :   // true dest path both of the conditions hold. Similarly for conditions of
    1141             :   // the form (cond1 || cond2), we know that on the false dest path neither
    1142             :   // condition holds.
    1143             :   BinaryOperator *BO = dyn_cast<BinaryOperator>(Cond);
    1144        6166 :   if (!BO || (isTrueDest && BO->getOpcode() != BinaryOperator::And) ||
    1145        2491 :              (!isTrueDest && BO->getOpcode() != BinaryOperator::Or))
    1146             :     return ValueLatticeElement::getOverdefined();
    1147             : 
    1148        2424 :   auto RHS = getValueFromCondition(Val, BO->getOperand(0), isTrueDest, Visited);
    1149        1212 :   auto LHS = getValueFromCondition(Val, BO->getOperand(1), isTrueDest, Visited);
    1150        1212 :   return intersect(RHS, LHS);
    1151             : }
    1152             : 
    1153             : static ValueLatticeElement
    1154      432206 : getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest,
    1155             :                       DenseMap<Value*, ValueLatticeElement> &Visited) {
    1156      432206 :   auto I = Visited.find(Cond);
    1157      432206 :   if (I != Visited.end())
    1158         256 :     return I->second;
    1159             : 
    1160      431950 :   auto Result = getValueFromConditionImpl(Val, Cond, isTrueDest, Visited);
    1161      431950 :   Visited[Cond] = Result;
    1162             :   return Result;
    1163             : }
    1164             : 
    1165      429782 : ValueLatticeElement getValueFromCondition(Value *Val, Value *Cond,
    1166             :                                           bool isTrueDest) {
    1167             :   assert(Cond && "precondition");
    1168             :   DenseMap<Value*, ValueLatticeElement> Visited;
    1169      859564 :   return getValueFromCondition(Val, Cond, isTrueDest, Visited);
    1170             : }
    1171             : 
    1172             : // Return true if Usr has Op as an operand, otherwise false.
    1173       19102 : static bool usesOperand(User *Usr, Value *Op) {
    1174       19102 :   return find(Usr->operands(), Op) != Usr->op_end();
    1175             : }
    1176             : 
    1177             : // Return true if the instruction type of Val is supported by
    1178             : // constantFoldUser(). Currently CastInst and BinaryOperator only.  Call this
    1179             : // before calling constantFoldUser() to find out if it's even worth attempting
    1180             : // to call it.
    1181             : static bool isOperationFoldable(User *Usr) {
    1182             :   return isa<CastInst>(Usr) || isa<BinaryOperator>(Usr);
    1183             : }
    1184             : 
    1185             : // Check if Usr can be simplified to an integer constant when the value of one
    1186             : // of its operands Op is an integer constant OpConstVal. If so, return it as an
    1187             : // lattice value range with a single element or otherwise return an overdefined
    1188             : // lattice value.
    1189         416 : static ValueLatticeElement constantFoldUser(User *Usr, Value *Op,
    1190             :                                             const APInt &OpConstVal,
    1191             :                                             const DataLayout &DL) {
    1192             :   assert(isOperationFoldable(Usr) && "Precondition");
    1193         416 :   Constant* OpConst = Constant::getIntegerValue(Op->getType(), OpConstVal);
    1194             :   // Check if Usr can be simplified to a constant.
    1195             :   if (auto *CI = dyn_cast<CastInst>(Usr)) {
    1196             :     assert(CI->getOperand(0) == Op && "Operand 0 isn't Op");
    1197         204 :     if (auto *C = dyn_cast_or_null<ConstantInt>(
    1198             :             SimplifyCastInst(CI->getOpcode(), OpConst,
    1199             :                              CI->getDestTy(), DL))) {
    1200         408 :       return ValueLatticeElement::getRange(ConstantRange(C->getValue()));
    1201             :     }
    1202             :   } else if (auto *BO = dyn_cast<BinaryOperator>(Usr)) {
    1203             :     bool Op0Match = BO->getOperand(0) == Op;
    1204             :     bool Op1Match = BO->getOperand(1) == Op;
    1205             :     assert((Op0Match || Op1Match) &&
    1206             :            "Operand 0 nor Operand 1 isn't a match");
    1207         212 :     Value *LHS = Op0Match ? OpConst : BO->getOperand(0);
    1208         212 :     Value *RHS = Op1Match ? OpConst : BO->getOperand(1);
    1209         236 :     if (auto *C = dyn_cast_or_null<ConstantInt>(
    1210             :             SimplifyBinOp(BO->getOpcode(), LHS, RHS, DL))) {
    1211         280 :       return ValueLatticeElement::getRange(ConstantRange(C->getValue()));
    1212             :     }
    1213             :   }
    1214             :   return ValueLatticeElement::getOverdefined();
    1215             : }
    1216             : 
    1217             : /// \brief Compute the value of Val on the edge BBFrom -> BBTo. Returns false if
    1218             : /// Val is not constrained on the edge.  Result is unspecified if return value
    1219             : /// is false.
    1220     1128019 : static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom,
    1221             :                               BasicBlock *BBTo, ValueLatticeElement &Result) {
    1222             :   // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
    1223             :   // know that v != 0.
    1224             :   if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {
    1225             :     // If this is a conditional branch and only one successor goes to BBTo, then
    1226             :     // we may be able to infer something from the condition.
    1227     1314943 :     if (BI->isConditional() &&
    1228             :         BI->getSuccessor(0) != BI->getSuccessor(1)) {
    1229      394239 :       bool isTrueDest = BI->getSuccessor(0) == BBTo;
    1230             :       assert(BI->getSuccessor(!isTrueDest) == BBTo &&
    1231             :              "BBTo isn't a successor of BBFrom");
    1232             :       Value *Condition = BI->getCondition();
    1233             : 
    1234             :       // If V is the condition of the branch itself, then we know exactly what
    1235             :       // it is.
    1236      394239 :       if (Condition == Val) {
    1237        4686 :         Result = ValueLatticeElement::get(ConstantInt::get(
    1238             :                               Type::getInt1Ty(Val->getContext()), isTrueDest));
    1239        1562 :         return true;
    1240             :       }
    1241             : 
    1242             :       // If the condition of the branch is an equality comparison, we may be
    1243             :       // able to infer the value.
    1244      785354 :       Result = getValueFromCondition(Val, Condition, isTrueDest);
    1245      392677 :       if (!Result.isOverdefined())
    1246             :         return true;
    1247             : 
    1248             :       if (User *Usr = dyn_cast<User>(Val)) {
    1249             :         assert(Result.isOverdefined() && "Result isn't overdefined");
    1250             :         // Check with isOperationFoldable() first to avoid linearly iterating
    1251             :         // over the operands unnecessarily which can be expensive for
    1252             :         // instructions with many operands.
    1253      312342 :         if (isa<IntegerType>(Usr->getType()) && isOperationFoldable(Usr)) {
    1254       18987 :           const DataLayout &DL = BBTo->getModule()->getDataLayout();
    1255       18987 :           if (usesOperand(Usr, Condition)) {
    1256             :             // If Val has Condition as an operand and Val can be folded into a
    1257             :             // constant with either Condition == true or Condition == false,
    1258             :             // propagate the constant.
    1259             :             // eg.
    1260             :             //   ; %Val is true on the edge to %then.
    1261             :             //   %Val = and i1 %Condition, true.
    1262             :             //   br %Condition, label %then, label %else
    1263           2 :             APInt ConditionVal(1, isTrueDest ? 1 : 0);
    1264           4 :             Result = constantFoldUser(Usr, Condition, ConditionVal, DL);
    1265             :           } else {
    1266             :             // If one of Val's operand has an inferred value, we may be able to
    1267             :             // infer the value of Val.
    1268             :             // eg.
    1269             :             //    ; %Val is 94 on the edge to %then.
    1270             :             //    %Val = add i8 %Op, 1
    1271             :             //    %Condition = icmp eq i8 %Op, 93
    1272             :             //    br i1 %Condition, label %then, label %else
    1273       91911 :             for (unsigned i = 0; i < Usr->getNumOperands(); ++i) {
    1274             :               Value *Op = Usr->getOperand(i);
    1275             :               ValueLatticeElement OpLatticeVal =
    1276       36547 :                   getValueFromCondition(Op, Condition, isTrueDest);
    1277       36547 :               if (Optional<APInt> OpConst = OpLatticeVal.asConstantInteger()) {
    1278         168 :                 Result = constantFoldUser(Usr, Op, OpConst.getValue(), DL);
    1279             :                 break;
    1280             :               }
    1281             :             }
    1282             :           }
    1283             :         }
    1284             :       }
    1285      357783 :       if (!Result.isOverdefined())
    1286             :         return true;
    1287             :     }
    1288             :   }
    1289             : 
    1290             :   // If the edge was formed by a switch on the value, then we may know exactly
    1291             :   // what it is.
    1292             :   if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {
    1293             :     Value *Condition = SI->getCondition();
    1294        6574 :     if (!isa<IntegerType>(Val->getType()))
    1295             :       return false;
    1296             :     bool ValUsesConditionAndMayBeFoldable = false;
    1297        1121 :     if (Condition != Val) {
    1298             :       // Check if Val has Condition as an operand.
    1299             :       if (User *Usr = dyn_cast<User>(Val))
    1300         115 :         ValUsesConditionAndMayBeFoldable = isOperationFoldable(Usr) &&
    1301         115 :             usesOperand(Usr, Condition);
    1302         784 :       if (!ValUsesConditionAndMayBeFoldable)
    1303             :         return false;
    1304             :     }
    1305             :     assert((Condition == Val || ValUsesConditionAndMayBeFoldable) &&
    1306             :            "Condition != Val nor Val doesn't use Condition");
    1307             : 
    1308         277 :     bool DefaultCase = SI->getDefaultDest() == BBTo;
    1309             :     unsigned BitWidth = Val->getType()->getIntegerBitWidth();
    1310         554 :     ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/);
    1311             : 
    1312        1653 :     for (auto Case : SI->cases()) {
    1313             :       APInt CaseValue = Case.getCaseValue()->getValue();
    1314        4128 :       ConstantRange EdgeVal(CaseValue);
    1315        1376 :       if (ValUsesConditionAndMayBeFoldable) {
    1316             :         User *Usr = cast<User>(Val);
    1317         330 :         const DataLayout &DL = BBTo->getModule()->getDataLayout();
    1318             :         ValueLatticeElement EdgeLatticeVal =
    1319         330 :             constantFoldUser(Usr, Condition, CaseValue, DL);
    1320         330 :         if (EdgeLatticeVal.isOverdefined())
    1321             :           return false;
    1322             :         EdgeVal = EdgeLatticeVal.getConstantRange();
    1323             :       }
    1324        1376 :       if (DefaultCase) {
    1325             :         // It is possible that the default destination is the destination of
    1326             :         // some cases. We cannot perform difference for those cases.
    1327             :         // We know Condition != CaseValue in BBTo.  In some cases we can use
    1328             :         // this to infer Val == f(Condition) is != f(CaseValue).  For now, we
    1329             :         // only do this when f is identity (i.e. Val == Condition), but we
    1330             :         // should be able to do this for any injective f.
    1331         862 :         if (Case.getCaseSuccessor() != BBTo && Condition == Val)
    1332         556 :           EdgesVals = EdgesVals.difference(EdgeVal);
    1333         514 :       } else if (Case.getCaseSuccessor() == BBTo)
    1334         142 :         EdgesVals = EdgesVals.unionWith(EdgeVal);
    1335             :     }
    1336         554 :     Result = ValueLatticeElement::getRange(std::move(EdgesVals));
    1337         277 :     return true;
    1338             :   }
    1339             :   return false;
    1340             : }
    1341             : 
    1342             : /// \brief Compute the value of Val on the edge BBFrom -> BBTo or the value at
    1343             : /// the basic block if the edge does not constrain Val.
    1344     1139790 : bool LazyValueInfoImpl::getEdgeValue(Value *Val, BasicBlock *BBFrom,
    1345             :                                      BasicBlock *BBTo,
    1346             :                                      ValueLatticeElement &Result,
    1347             :                                      Instruction *CxtI) {
    1348             :   // If already a constant, there is nothing to compute.
    1349             :   if (Constant *VC = dyn_cast<Constant>(Val)) {
    1350       23542 :     Result = ValueLatticeElement::get(VC);
    1351       11771 :     return true;
    1352             :   }
    1353             : 
    1354             :   ValueLatticeElement LocalResult;
    1355     1128019 :   if (!getEdgeValueLocal(Val, BBFrom, BBTo, LocalResult))
    1356             :     // If we couldn't constrain the value on the edge, LocalResult doesn't
    1357             :     // provide any information.
    1358     2182544 :     LocalResult = ValueLatticeElement::getOverdefined();
    1359             : 
    1360             :   if (hasSingleValue(LocalResult)) {
    1361             :     // Can't get any more precise here
    1362        2849 :     Result = LocalResult;
    1363        2849 :     return true;
    1364             :   }
    1365             : 
    1366     1125170 :   if (!hasBlockValue(Val, BBFrom)) {
    1367      459597 :     if (pushBlockValue(std::make_pair(BBFrom, Val)))
    1368             :       return false;
    1369             :     // No new information.
    1370       13367 :     Result = LocalResult;
    1371       13367 :     return true;
    1372             :   }
    1373             : 
    1374             :   // Try to intersect ranges of the BB and the constraint on the edge.
    1375      665573 :   ValueLatticeElement InBlock = getBlockValue(Val, BBFrom);
    1376      665573 :   intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock,
    1377             :                                                 BBFrom->getTerminator());
    1378             :   // We can use the context instruction (generically the ultimate instruction
    1379             :   // the calling pass is trying to simplify) here, even though the result of
    1380             :   // this function is generally cached when called from the solve* functions
    1381             :   // (and that cached result might be used with queries using a different
    1382             :   // context instruction), because when this function is called from the solve*
    1383             :   // functions, the context instruction is not provided. When called from
    1384             :   // LazyValueInfoImpl::getValueOnEdge, the context instruction is provided,
    1385             :   // but then the result is not cached.
    1386      665573 :   intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock, CxtI);
    1387             : 
    1388     1331146 :   Result = intersect(LocalResult, InBlock);
    1389             :   return true;
    1390             : }
    1391             : 
    1392      428175 : ValueLatticeElement LazyValueInfoImpl::getValueInBlock(Value *V, BasicBlock *BB,
    1393             :                                                        Instruction *CxtI) {
    1394             :   DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
    1395             :         << BB->getName() << "'\n");
    1396             : 
    1397             :   assert(BlockValueStack.empty() && BlockValueSet.empty());
    1398      428141 :   if (!hasBlockValue(V, BB)) {
    1399      320690 :     pushBlockValue(std::make_pair(BB, V));
    1400      320690 :     solve();
    1401             :   }
    1402      428175 :   ValueLatticeElement Result = getBlockValue(V, BB);
    1403      428175 :   intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI);
    1404             : 
    1405             :   DEBUG(dbgs() << "  Result = " << Result << "\n");
    1406      428175 :   return Result;
    1407             : }
    1408             : 
    1409      155826 : ValueLatticeElement LazyValueInfoImpl::getValueAt(Value *V, Instruction *CxtI) {
    1410             :   DEBUG(dbgs() << "LVI Getting value " << *V << " at '"
    1411             :         << CxtI->getName() << "'\n");
    1412             : 
    1413             :   if (auto *C = dyn_cast<Constant>(V))
    1414             :     return ValueLatticeElement::get(C);
    1415             : 
    1416             :   ValueLatticeElement Result = ValueLatticeElement::getOverdefined();
    1417             :   if (auto *I = dyn_cast<Instruction>(V))
    1418      244936 :     Result = getFromRangeMetadata(I);
    1419      155788 :   intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI);
    1420             : 
    1421             :   DEBUG(dbgs() << "  Result = " << Result << "\n");
    1422             :   return Result;
    1423             : }
    1424             : 
    1425      224442 : ValueLatticeElement LazyValueInfoImpl::
    1426             : getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB,
    1427             :                Instruction *CxtI) {
    1428             :   DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
    1429             :         << FromBB->getName() << "' to '" << ToBB->getName() << "'\n");
    1430             : 
    1431             :   ValueLatticeElement Result;
    1432      224442 :   if (!getEdgeValue(V, FromBB, ToBB, Result, CxtI)) {
    1433      143538 :     solve();
    1434      143538 :     bool WasFastQuery = getEdgeValue(V, FromBB, ToBB, Result, CxtI);
    1435             :     (void)WasFastQuery;
    1436             :     assert(WasFastQuery && "More work to do after problem solved?");
    1437             :   }
    1438             : 
    1439             :   DEBUG(dbgs() << "  Result = " << Result << "\n");
    1440      224442 :   return Result;
    1441             : }
    1442             : 
    1443             : void LazyValueInfoImpl::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
    1444             :                                    BasicBlock *NewSucc) {
    1445        1415 :   TheCache.threadEdgeImpl(OldSucc, NewSucc);
    1446             : }
    1447             : 
    1448             : //===----------------------------------------------------------------------===//
    1449             : //                            LazyValueInfo Impl
    1450             : //===----------------------------------------------------------------------===//
    1451             : 
    1452             : /// This lazily constructs the LazyValueInfoImpl.
    1453     1070370 : static LazyValueInfoImpl &getImpl(void *&PImpl, AssumptionCache *AC,
    1454             :                                   const DataLayout *DL,
    1455             :                                   DominatorTree *DT = nullptr) {
    1456     1070370 :   if (!PImpl) {
    1457             :     assert(DL && "getCache() called with a null DataLayout");
    1458      114538 :     PImpl = new LazyValueInfoImpl(AC, *DL, DT);
    1459             :   }
    1460     1070370 :   return *static_cast<LazyValueInfoImpl*>(PImpl);
    1461             : }
    1462             : 
    1463       68193 : bool LazyValueInfoWrapperPass::runOnFunction(Function &F) {
    1464       68193 :   Info.AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
    1465       68193 :   const DataLayout &DL = F.getParent()->getDataLayout();
    1466             : 
    1467             :   DominatorTreeWrapperPass *DTWP =
    1468       68193 :       getAnalysisIfAvailable<DominatorTreeWrapperPass>();
    1469      136386 :   Info.DT = DTWP ? &DTWP->getDomTree() : nullptr;
    1470      136386 :   Info.TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
    1471             : 
    1472       68193 :   if (Info.PImpl)
    1473           0 :     getImpl(Info.PImpl, Info.AC, &DL, Info.DT).clear();
    1474             : 
    1475             :   // Fully lazy.
    1476       68193 :   return false;
    1477             : }
    1478             : 
    1479        2693 : void LazyValueInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
    1480             :   AU.setPreservesAll();
    1481             :   AU.addRequired<AssumptionCacheTracker>();
    1482             :   AU.addRequired<TargetLibraryInfoWrapperPass>();
    1483        2693 : }
    1484             : 
    1485      135255 : LazyValueInfo &LazyValueInfoWrapperPass::getLVI() { return Info; }
    1486             : 
    1487        3383 : LazyValueInfo::~LazyValueInfo() { releaseMemory(); }
    1488             : 
    1489       71576 : void LazyValueInfo::releaseMemory() {
    1490             :   // If the cache was allocated, free it.
    1491       71576 :   if (PImpl) {
    1492       57269 :     delete &getImpl(PImpl, AC, nullptr);
    1493       57269 :     PImpl = nullptr;
    1494             :   }
    1495       71576 : }
    1496             : 
    1497         180 : bool LazyValueInfo::invalidate(Function &F, const PreservedAnalyses &PA,
    1498             :                                FunctionAnalysisManager::Invalidator &Inv) {
    1499             :   // We need to invalidate if we have either failed to preserve this analyses
    1500             :   // result directly or if any of its dependencies have been invalidated.
    1501             :   auto PAC = PA.getChecker<LazyValueAnalysis>();
    1502         206 :   if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
    1503          50 :       (DT && Inv.invalidate<DominatorTreeAnalysis>(F, PA)))
    1504             :     return true;
    1505             : 
    1506             :   return false;
    1507             : }
    1508             : 
    1509       68193 : void LazyValueInfoWrapperPass::releaseMemory() { Info.releaseMemory(); }
    1510             : 
    1511         230 : LazyValueInfo LazyValueAnalysis::run(Function &F,
    1512             :                                      FunctionAnalysisManager &FAM) {
    1513             :   auto &AC = FAM.getResult<AssumptionAnalysis>(F);
    1514             :   auto &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
    1515             :   auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(F);
    1516             : 
    1517         460 :   return LazyValueInfo(&AC, &F.getParent()->getDataLayout(), &TLI, DT);
    1518             : }
    1519             : 
    1520             : /// Returns true if we can statically tell that this value will never be a
    1521             : /// "useful" constant.  In practice, this means we've got something like an
    1522             : /// alloca or a malloc call for which a comparison against a constant can
    1523             : /// only be guarding dead code.  Note that we are potentially giving up some
    1524             : /// precision in dead code (a constant result) in favour of avoiding a
    1525             : /// expensive search for a easily answered common query.
    1526             : static bool isKnownNonConstant(Value *V) {
    1527             :   V = V->stripPointerCasts();
    1528             :   // The return val of alloc cannot be a Constant.
    1529             :   if (isa<AllocaInst>(V))
    1530             :     return true;
    1531             :   return false;
    1532             : }
    1533             : 
    1534      459380 : Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB,
    1535             :                                      Instruction *CxtI) {
    1536             :   // Bail out early if V is known not to be a Constant.
    1537             :   if (isKnownNonConstant(V))
    1538             :     return nullptr;
    1539             : 
    1540      427746 :   const DataLayout &DL = BB->getModule()->getDataLayout();
    1541             :   ValueLatticeElement Result =
    1542      427746 :       getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
    1543             : 
    1544      427746 :   if (Result.isConstant())
    1545           1 :     return Result.getConstant();
    1546      427745 :   if (Result.isConstantRange()) {
    1547             :     const ConstantRange &CR = Result.getConstantRange();
    1548       10250 :     if (const APInt *SingleVal = CR.getSingleElement())
    1549          58 :       return ConstantInt::get(V->getContext(), *SingleVal);
    1550             :   }
    1551             :   return nullptr;
    1552             : }
    1553             : 
    1554         318 : ConstantRange LazyValueInfo::getConstantRange(Value *V, BasicBlock *BB,
    1555             :                                               Instruction *CxtI) {
    1556             :   assert(V->getType()->isIntegerTy());
    1557             :   unsigned Width = V->getType()->getIntegerBitWidth();
    1558         318 :   const DataLayout &DL = BB->getModule()->getDataLayout();
    1559             :   ValueLatticeElement Result =
    1560         318 :       getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
    1561         318 :   if (Result.isUndefined())
    1562           0 :     return ConstantRange(Width, /*isFullSet=*/false);
    1563         318 :   if (Result.isConstantRange())
    1564          64 :     return Result.getConstantRange();
    1565             :   // We represent ConstantInt constants as constant ranges but other kinds
    1566             :   // of integer constants, i.e. ConstantExpr will be tagged as constants
    1567             :   assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) &&
    1568             :          "ConstantInt value must be represented as constantrange");
    1569         254 :   return ConstantRange(Width, /*isFullSet=*/true);
    1570             : }
    1571             : 
    1572             : /// Determine whether the specified value is known to be a
    1573             : /// constant on the specified edge. Return null if not.
    1574      165772 : Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB,
    1575             :                                            BasicBlock *ToBB,
    1576             :                                            Instruction *CxtI) {
    1577      165772 :   const DataLayout &DL = FromBB->getModule()->getDataLayout();
    1578             :   ValueLatticeElement Result =
    1579      165772 :       getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
    1580             : 
    1581      165772 :   if (Result.isConstant())
    1582          59 :     return Result.getConstant();
    1583      165713 :   if (Result.isConstantRange()) {
    1584             :     const ConstantRange &CR = Result.getConstantRange();
    1585       14204 :     if (const APInt *SingleVal = CR.getSingleElement())
    1586        1507 :       return ConstantInt::get(V->getContext(), *SingleVal);
    1587             :   }
    1588             :   return nullptr;
    1589             : }
    1590             : 
    1591        1960 : ConstantRange LazyValueInfo::getConstantRangeOnEdge(Value *V,
    1592             :                                                     BasicBlock *FromBB,
    1593             :                                                     BasicBlock *ToBB,
    1594             :                                                     Instruction *CxtI) {
    1595             :   unsigned Width = V->getType()->getIntegerBitWidth();
    1596        1960 :   const DataLayout &DL = FromBB->getModule()->getDataLayout();
    1597             :   ValueLatticeElement Result =
    1598        1960 :       getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
    1599             : 
    1600        1960 :   if (Result.isUndefined())
    1601           0 :     return ConstantRange(Width, /*isFullSet=*/false);
    1602        1960 :   if (Result.isConstantRange())
    1603        1435 :     return Result.getConstantRange();
    1604             :   // We represent ConstantInt constants as constant ranges but other kinds
    1605             :   // of integer constants, i.e. ConstantExpr will be tagged as constants
    1606             :   assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) &&
    1607             :          "ConstantInt value must be represented as constantrange");
    1608         525 :   return ConstantRange(Width, /*isFullSet=*/true);
    1609             : }
    1610             : 
    1611             : static LazyValueInfo::Tristate
    1612      212536 : getPredicateResult(unsigned Pred, Constant *C, const ValueLatticeElement &Val,
    1613             :                    const DataLayout &DL, TargetLibraryInfo *TLI) {
    1614             :   // If we know the value is a constant, evaluate the conditional.
    1615             :   Constant *Res = nullptr;
    1616      212536 :   if (Val.isConstant()) {
    1617        1087 :     Res = ConstantFoldCompareInstOperands(Pred, Val.getConstant(), C, DL, TLI);
    1618             :     if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res))
    1619        1085 :       return ResCI->isZero() ? LazyValueInfo::False : LazyValueInfo::True;
    1620             :     return LazyValueInfo::Unknown;
    1621             :   }
    1622             : 
    1623      211449 :   if (Val.isConstantRange()) {
    1624             :     ConstantInt *CI = dyn_cast<ConstantInt>(C);
    1625             :     if (!CI) return LazyValueInfo::Unknown;
    1626             : 
    1627             :     const ConstantRange &CR = Val.getConstantRange();
    1628       26504 :     if (Pred == ICmpInst::ICMP_EQ) {
    1629        9138 :       if (!CR.contains(CI->getValue()))
    1630             :         return LazyValueInfo::False;
    1631             : 
    1632        7463 :       if (CR.isSingleElement())
    1633             :         return LazyValueInfo::True;
    1634       17366 :     } else if (Pred == ICmpInst::ICMP_NE) {
    1635        8510 :       if (!CR.contains(CI->getValue()))
    1636             :         return LazyValueInfo::True;
    1637             : 
    1638        8385 :       if (CR.isSingleElement())
    1639             :         return LazyValueInfo::False;
    1640             :     } else {
    1641             :       // Handle more complex predicates.
    1642             :       ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(
    1643       15595 :           (ICmpInst::Predicate)Pred, CI->getValue());
    1644        8856 :       if (TrueValues.contains(CR))
    1645        2117 :         return LazyValueInfo::True;
    1646        6968 :       if (TrueValues.inverse().contains(CR))
    1647             :         return LazyValueInfo::False;
    1648             :     }
    1649             :     return LazyValueInfo::Unknown;
    1650             :   }
    1651             : 
    1652      184937 :   if (Val.isNotConstant()) {
    1653             :     // If this is an equality comparison, we can try to fold it knowing that
    1654             :     // "V != C1".
    1655        2642 :     if (Pred == ICmpInst::ICMP_EQ) {
    1656             :       // !C1 == C -> false iff C1 == C.
    1657        2312 :       Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
    1658             :                                             Val.getNotConstant(), C, DL,
    1659             :                                             TLI);
    1660        2312 :       if (Res->isNullValue())
    1661             :         return LazyValueInfo::False;
    1662         330 :     } else if (Pred == ICmpInst::ICMP_NE) {
    1663             :       // !C1 != C -> true iff C1 == C.
    1664         252 :       Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
    1665             :                                             Val.getNotConstant(), C, DL,
    1666             :                                             TLI);
    1667         252 :       if (Res->isNullValue())
    1668             :         return LazyValueInfo::True;
    1669             :     }
    1670             :     return LazyValueInfo::Unknown;
    1671             :   }
    1672             : 
    1673             :   return LazyValueInfo::Unknown;
    1674             : }
    1675             : 
    1676             : /// Determine whether the specified value comparison with a constant is known to
    1677             : /// be true or false on the specified CFG edge. Pred is a CmpInst predicate.
    1678             : LazyValueInfo::Tristate
    1679       56710 : LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C,
    1680             :                                   BasicBlock *FromBB, BasicBlock *ToBB,
    1681             :                                   Instruction *CxtI) {
    1682       56710 :   const DataLayout &DL = FromBB->getModule()->getDataLayout();
    1683             :   ValueLatticeElement Result =
    1684       56710 :       getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
    1685             : 
    1686      113420 :   return getPredicateResult(Pred, C, Result, DL, TLI);
    1687             : }
    1688             : 
    1689             : LazyValueInfo::Tristate
    1690      196079 : LazyValueInfo::getPredicateAt(unsigned Pred, Value *V, Constant *C,
    1691             :                               Instruction *CxtI) {
    1692             :   // Is or is not NonNull are common predicates being queried. If
    1693             :   // isKnownNonZero can tell us the result of the predicate, we can
    1694             :   // return it quickly. But this is only a fastpath, and falling
    1695             :   // through would still be correct.
    1696      196079 :   const DataLayout &DL = CxtI->getModule()->getDataLayout();
    1697      341809 :   if (V->getType()->isPointerTy() && C->isNullValue() &&
    1698      145730 :       isKnownNonZero(V->stripPointerCasts(), DL)) {
    1699       40253 :     if (Pred == ICmpInst::ICMP_EQ)
    1700             :       return LazyValueInfo::False;
    1701           1 :     else if (Pred == ICmpInst::ICMP_NE)
    1702             :       return LazyValueInfo::True;
    1703             :   }
    1704      155826 :   ValueLatticeElement Result = getImpl(PImpl, AC, &DL, DT).getValueAt(V, CxtI);
    1705      155826 :   Tristate Ret = getPredicateResult(Pred, C, Result, DL, TLI);
    1706      155826 :   if (Ret != Unknown)
    1707             :     return Ret;
    1708             : 
    1709             :   // Note: The following bit of code is somewhat distinct from the rest of LVI;
    1710             :   // LVI as a whole tries to compute a lattice value which is conservatively
    1711             :   // correct at a given location.  In this case, we have a predicate which we
    1712             :   // weren't able to prove about the merged result, and we're pushing that
    1713             :   // predicate back along each incoming edge to see if we can prove it
    1714             :   // separately for each input.  As a motivating example, consider:
    1715             :   // bb1:
    1716             :   //   %v1 = ... ; constantrange<1, 5>
    1717             :   //   br label %merge
    1718             :   // bb2:
    1719             :   //   %v2 = ... ; constantrange<10, 20>
    1720             :   //   br label %merge
    1721             :   // merge:
    1722             :   //   %phi = phi [%v1, %v2] ; constantrange<1,20>
    1723             :   //   %pred = icmp eq i32 %phi, 8
    1724             :   // We can't tell from the lattice value for '%phi' that '%pred' is false
    1725             :   // along each path, but by checking the predicate over each input separately,
    1726             :   // we can.
    1727             :   // We limit the search to one step backwards from the current BB and value.
    1728             :   // We could consider extending this to search further backwards through the
    1729             :   // CFG and/or value graph, but there are non-obvious compile time vs quality
    1730             :   // tradeoffs.
    1731      155764 :   if (CxtI) {
    1732             :     BasicBlock *BB = CxtI->getParent();
    1733             : 
    1734             :     // Function entry or an unreachable block.  Bail to avoid confusing
    1735             :     // analysis below.
    1736      155764 :     pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
    1737      155764 :     if (PI == PE)
    1738       36819 :       return Unknown;
    1739             : 
    1740             :     // If V is a PHI node in the same block as the context, we need to ask
    1741             :     // questions about the predicate as applied to the incoming value along
    1742             :     // each edge. This is useful for eliminating cases where the predicate is
    1743             :     // known along all incoming edges.
    1744             :     if (auto *PHI = dyn_cast<PHINode>(V))
    1745       12253 :       if (PHI->getParent() == BB) {
    1746             :         Tristate Baseline = Unknown;
    1747       15760 :         for (unsigned i = 0, e = PHI->getNumIncomingValues(); i < e; i++) {
    1748             :           Value *Incoming = PHI->getIncomingValue(i);
    1749             :           BasicBlock *PredBB = PHI->getIncomingBlock(i);
    1750             :           // Note that PredBB may be BB itself.
    1751             :           Tristate Result = getPredicateOnEdge(Pred, Incoming, C, PredBB, BB,
    1752       12279 :                                                CxtI);
    1753             : 
    1754             :           // Keep going as long as we've seen a consistent known result for
    1755             :           // all inputs.
    1756       12279 :           Baseline = (i == 0) ? Result /* First iteration */
    1757        3301 :             : (Baseline == Result ? Baseline : Unknown); /* All others */
    1758        9099 :           if (Baseline == Unknown)
    1759             :             break;
    1760             :         }
    1761        8978 :         if (Baseline != Unknown)
    1762             :           return Baseline;
    1763             :       }
    1764             : 
    1765             :     // For a comparison where the V is outside this block, it's possible
    1766             :     // that we've branched on it before. Look to see if the value is known
    1767             :     // on all incoming edges.
    1768      120752 :     if (!isa<Instruction>(V) ||
    1769             :         cast<Instruction>(V)->getParent() != BB) {
    1770             :       // For predecessor edge, determine if the comparison is true or false
    1771             :       // on that edge. If they're all true or all false, we can conclude
    1772             :       // the value of the comparison in this block.
    1773       30541 :       Tristate Baseline = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
    1774       30541 :       if (Baseline != Unknown) {
    1775             :         // Check that all remaining incoming values match the first one.
    1776        2694 :         while (++PI != PE) {
    1777         887 :           Tristate Ret = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
    1778         887 :           if (Ret != Baseline) break;
    1779             :         }
    1780             :         // If we terminated early, then one of the values didn't match.
    1781        2347 :         if (PI == PE) {
    1782             :           return Baseline;
    1783             :         }
    1784             :       }
    1785             :     }
    1786             :   }
    1787             :   return Unknown;
    1788             : }
    1789             : 
    1790        1418 : void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
    1791             :                                BasicBlock *NewSucc) {
    1792        1418 :   if (PImpl) {
    1793        1415 :     const DataLayout &DL = PredBB->getModule()->getDataLayout();
    1794        1415 :     getImpl(PImpl, AC, &DL, DT).threadEdge(PredBB, OldSucc, NewSucc);
    1795             :   }
    1796        1418 : }
    1797             : 
    1798       35831 : void LazyValueInfo::eraseBlock(BasicBlock *BB) {
    1799       35831 :   if (PImpl) {
    1800       27434 :     const DataLayout &DL = BB->getModule()->getDataLayout();
    1801       27434 :     getImpl(PImpl, AC, &DL, DT).eraseBlock(BB);
    1802             :   }
    1803       35831 : }
    1804             : 
    1805             : 
    1806           4 : void LazyValueInfo::printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS) {
    1807           4 :   if (PImpl) {
    1808           4 :     getImpl(PImpl, AC, DL, DT).printLVI(F, DTree, OS);
    1809             :   }
    1810           4 : }
    1811             : 
    1812      104971 : void LazyValueInfo::disableDT() {
    1813      104971 :   if (PImpl)
    1814      104263 :     getImpl(PImpl, AC, DL, DT).disableDT();
    1815      104971 : }
    1816             : 
    1817      139529 : void LazyValueInfo::enableDT() {
    1818      139529 :   if (PImpl)
    1819       71653 :     getImpl(PImpl, AC, DL, DT).enableDT();
    1820      139529 : }
    1821             : 
    1822             : // Print the LVI for the function arguments at the start of each basic block.
    1823          16 : void LazyValueInfoAnnotatedWriter::emitBasicBlockStartAnnot(
    1824             :     const BasicBlock *BB, formatted_raw_ostream &OS) {
    1825             :   // Find if there are latticevalues defined for arguments of the function.
    1826             :   auto *F = BB->getParent();
    1827          53 :   for (auto &Arg : F->args()) {
    1828          37 :     ValueLatticeElement Result = LVIImpl->getValueInBlock(
    1829          37 :         const_cast<Argument *>(&Arg), const_cast<BasicBlock *>(BB));
    1830          37 :     if (Result.isUndefined())
    1831             :       continue;
    1832          74 :     OS << "; LatticeVal for: '" << Arg << "' is: " << Result << "\n";
    1833             :   }
    1834          16 : }
    1835             : 
    1836             : // This function prints the LVI analysis for the instruction I at the beginning
    1837             : // of various basic blocks. It relies on calculated values that are stored in
    1838             : // the LazyValueInfoCache, and in the absence of cached values, recalculte the
    1839             : // LazyValueInfo for `I`, and print that info.
    1840          43 : void LazyValueInfoAnnotatedWriter::emitInstructionAnnot(
    1841             :     const Instruction *I, formatted_raw_ostream &OS) {
    1842             : 
    1843          43 :   auto *ParentBB = I->getParent();
    1844             :   SmallPtrSet<const BasicBlock*, 16> BlocksContainingLVI;
    1845             :   // We can generate (solve) LVI values only for blocks that are dominated by
    1846             :   // the I's parent. However, to avoid generating LVI for all dominating blocks,
    1847             :   // that contain redundant/uninteresting information, we print LVI for
    1848             :   // blocks that may use this LVI information (such as immediate successor
    1849             :   // blocks, and blocks that contain uses of `I`).
    1850         106 :   auto printResult = [&](const BasicBlock *BB) {
    1851         106 :     if (!BlocksContainingLVI.insert(BB).second)
    1852          32 :       return;
    1853          74 :     ValueLatticeElement Result = LVIImpl->getValueInBlock(
    1854         148 :         const_cast<Instruction *>(I), const_cast<BasicBlock *>(BB));
    1855         370 :       OS << "; LatticeVal for: '" << *I << "' in BB: '";
    1856         148 :       BB->printAsOperand(OS, false);
    1857          74 :       OS << "' is: " << Result << "\n";
    1858          43 :   };
    1859             : 
    1860          43 :   printResult(ParentBB);
    1861             :   // Print the LVI analysis results for the immediate successor blocks, that
    1862             :   // are dominated by `ParentBB`.
    1863          43 :   for (auto *BBSucc : successors(ParentBB))
    1864          74 :     if (DT.dominates(ParentBB, BBSucc))
    1865          39 :       printResult(BBSucc);
    1866             : 
    1867             :   // Print LVI in blocks where `I` is used.
    1868          43 :   for (auto *U : I->users())
    1869             :     if (auto *UseI = dyn_cast<Instruction>(U))
    1870          26 :       if (!isa<PHINode>(UseI) || DT.dominates(ParentBB, UseI->getParent()))
    1871          24 :         printResult(UseI->getParent());
    1872             : 
    1873          43 : }
    1874             : 
    1875             : namespace {
    1876             : // Printer class for LazyValueInfo results.
    1877           0 : class LazyValueInfoPrinter : public FunctionPass {
    1878             : public:
    1879             :   static char ID; // Pass identification, replacement for typeid
    1880           0 :   LazyValueInfoPrinter() : FunctionPass(ID) {
    1881           0 :     initializeLazyValueInfoPrinterPass(*PassRegistry::getPassRegistry());
    1882             :   }
    1883             : 
    1884           0 :   void getAnalysisUsage(AnalysisUsage &AU) const override {
    1885             :     AU.setPreservesAll();
    1886             :     AU.addRequired<LazyValueInfoWrapperPass>();
    1887             :     AU.addRequired<DominatorTreeWrapperPass>();
    1888           0 :   }
    1889             : 
    1890             :   // Get the mandatory dominator tree analysis and pass this in to the
    1891             :   // LVIPrinter. We cannot rely on the LVI's DT, since it's optional.
    1892           0 :   bool runOnFunction(Function &F) override {
    1893           0 :     dbgs() << "LVI for function '" << F.getName() << "':\n";
    1894           0 :     auto &LVI = getAnalysis<LazyValueInfoWrapperPass>().getLVI();
    1895           0 :     auto &DTree = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
    1896           0 :     LVI.printLVI(F, DTree, dbgs());
    1897           0 :     return false;
    1898             :   }
    1899             : };
    1900             : }
    1901             : 
    1902             : char LazyValueInfoPrinter::ID = 0;
    1903        9687 : INITIALIZE_PASS_BEGIN(LazyValueInfoPrinter, "print-lazy-value-info",
    1904             :                 "Lazy Value Info Printer Pass", false, false)
    1905        9687 : INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass)
    1906       38748 : INITIALIZE_PASS_END(LazyValueInfoPrinter, "print-lazy-value-info",
    1907             :                 "Lazy Value Info Printer Pass", false, false)

Generated by: LCOV version 1.13