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

Generated by: LCOV version 1.13