brintos

brintos / llvm-project-archived public Read only

0
0
Text · 88.5 KiB · df75999 Raw
2297 lines · cpp
1//===- LazyValueInfo.cpp - Value constraint analysis ------------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// This file defines the interface for lazy computation of value constraint10// information.11//12//===----------------------------------------------------------------------===//13 14#include "llvm/Analysis/LazyValueInfo.h"15#include "llvm/ADT/DenseSet.h"16#include "llvm/ADT/STLExtras.h"17#include "llvm/Analysis/AssumptionCache.h"18#include "llvm/Analysis/ConstantFolding.h"19#include "llvm/Analysis/InstructionSimplify.h"20#include "llvm/Analysis/Passes.h"21#include "llvm/Analysis/TargetLibraryInfo.h"22#include "llvm/Analysis/ValueLattice.h"23#include "llvm/Analysis/ValueTracking.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/InstrTypes.h"31#include "llvm/IR/Instructions.h"32#include "llvm/IR/IntrinsicInst.h"33#include "llvm/IR/Intrinsics.h"34#include "llvm/IR/LLVMContext.h"35#include "llvm/IR/Module.h"36#include "llvm/IR/PatternMatch.h"37#include "llvm/IR/ValueHandle.h"38#include "llvm/InitializePasses.h"39#include "llvm/Support/Debug.h"40#include "llvm/Support/FormattedStream.h"41#include "llvm/Support/KnownBits.h"42#include "llvm/Support/raw_ostream.h"43#include <optional>44using namespace llvm;45using namespace PatternMatch;46 47#define DEBUG_TYPE "lazy-value-info"48 49// This is the number of worklist items we will process to try to discover an50// answer for a given value.51static const unsigned MaxProcessedPerValue = 500;52 53char LazyValueInfoWrapperPass::ID = 0;54LazyValueInfoWrapperPass::LazyValueInfoWrapperPass() : FunctionPass(ID) {}55INITIALIZE_PASS_BEGIN(LazyValueInfoWrapperPass, "lazy-value-info",56                "Lazy Value Information Analysis", false, true)57INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)58INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)59INITIALIZE_PASS_END(LazyValueInfoWrapperPass, "lazy-value-info",60                "Lazy Value Information Analysis", false, true)61 62static cl::opt<bool> PerPredRanges(63    "lvi-per-pred-ranges", cl::Hidden, cl::init(false),64    cl::desc("Enable tracking of ranges for a value in a block for"65             "each block predecessor (default = false)"));66 67namespace llvm {68FunctionPass *createLazyValueInfoPass() {69  return new LazyValueInfoWrapperPass();70}71} // namespace llvm72 73AnalysisKey LazyValueAnalysis::Key;74 75/// Returns true if this lattice value represents at most one possible value.76/// This is as precise as any lattice value can get while still representing77/// reachable code.78static bool hasSingleValue(const ValueLatticeElement &Val) {79  if (Val.isConstantRange() &&80      Val.getConstantRange().isSingleElement())81    // Integer constants are single element ranges82    return true;83  if (Val.isConstant())84    // Non integer constants85    return true;86  return false;87}88 89//===----------------------------------------------------------------------===//90//                          LazyValueInfoCache Decl91//===----------------------------------------------------------------------===//92 93namespace {94  /// A callback value handle updates the cache when values are erased.95  class LazyValueInfoCache;96  struct LVIValueHandle final : public CallbackVH {97    LazyValueInfoCache *Parent;98 99    LVIValueHandle(Value *V, LazyValueInfoCache *P = nullptr)100      : CallbackVH(V), Parent(P) { }101 102    void deleted() override;103    void allUsesReplacedWith(Value *V) override {104      deleted();105    }106  };107} // end anonymous namespace108 109namespace {110using NonNullPointerSet = SmallDenseSet<AssertingVH<Value>, 2>;111using BBLatticeElementMap =112    SmallDenseMap<PoisoningVH<BasicBlock>, ValueLatticeElement, 4>;113using PredecessorValueLatticeMap =114    SmallDenseMap<AssertingVH<Value>, BBLatticeElementMap, 2>;115 116/// This is the cache kept by LazyValueInfo which117/// maintains information about queries across the clients' queries.118class LazyValueInfoCache {119  /// This is all of the cached information for one basic block. It contains120  /// the per-value lattice elements, as well as a separate set for121  /// overdefined values to reduce memory usage. Additionally pointers122  /// dereferenced in the block are cached for nullability queries.123  struct BlockCacheEntry {124    SmallDenseMap<AssertingVH<Value>, ValueLatticeElement, 4> LatticeElements;125    SmallDenseSet<AssertingVH<Value>, 4> OverDefined;126    // std::nullopt indicates that the nonnull pointers for this basic block127    // block have not been computed yet.128    std::optional<NonNullPointerSet> NonNullPointers;129    // This is an extension of the above LatticeElements, caching, for each130    // Value, a ValueLatticeElement, for each predecessor of the BB tracked by131    // this entry.132    std::optional<PredecessorValueLatticeMap> PredecessorLatticeElements;133  };134 135  /// Cached information per basic block.136  DenseMap<PoisoningVH<BasicBlock>, std::unique_ptr<BlockCacheEntry>>137      BlockCache;138  /// Set of value handles used to erase values from the cache on deletion.139  DenseSet<LVIValueHandle, DenseMapInfo<Value *>> ValueHandles;140 141  const BlockCacheEntry *getBlockEntry(BasicBlock *BB) const {142    auto It = BlockCache.find_as(BB);143    if (It == BlockCache.end())144      return nullptr;145    return It->second.get();146  }147 148  BlockCacheEntry *getOrCreateBlockEntry(BasicBlock *BB) {149    auto It = BlockCache.find_as(BB);150    if (It == BlockCache.end()) {151      std::unique_ptr<BlockCacheEntry> BCE =152          std::make_unique<BlockCacheEntry>();153      if (PerPredRanges)154        BCE->PredecessorLatticeElements =155            std::make_optional<PredecessorValueLatticeMap>();156      It = BlockCache.insert({BB, std::move(BCE)}).first;157    }158 159    return It->second.get();160  }161 162  void addValueHandle(Value *Val) {163    auto HandleIt = ValueHandles.find_as(Val);164    if (HandleIt == ValueHandles.end())165      ValueHandles.insert({Val, this});166  }167 168public:169  void insertResult(Value *Val, BasicBlock *BB,170                    const ValueLatticeElement &Result) {171    BlockCacheEntry *Entry = getOrCreateBlockEntry(BB);172 173    // Insert over-defined values into their own cache to reduce memory174    // overhead.175    if (Result.isOverdefined())176      Entry->OverDefined.insert(Val);177    else178      Entry->LatticeElements.insert({Val, Result});179 180    addValueHandle(Val);181  }182 183  void insertPredecessorResults(Value *Val, BasicBlock *BB,184                                BBLatticeElementMap &PredLatticeElements) {185    BlockCacheEntry *Entry = getOrCreateBlockEntry(BB);186 187    Entry->PredecessorLatticeElements->insert({Val, PredLatticeElements});188 189    addValueHandle(Val);190  }191 192  std::optional<BBLatticeElementMap>193  getCachedPredecessorInfo(Value *V, BasicBlock *BB) const {194    const BlockCacheEntry *Entry = getBlockEntry(BB);195    if (!Entry)196      return std::nullopt;197 198    auto LatticeIt = Entry->PredecessorLatticeElements->find_as(V);199    if (LatticeIt == Entry->PredecessorLatticeElements->end())200      return std::nullopt;201 202    return LatticeIt->second;203  }204 205  std::optional<ValueLatticeElement> getCachedValueInfo(Value *V,206                                                        BasicBlock *BB) const {207    const BlockCacheEntry *Entry = getBlockEntry(BB);208    if (!Entry)209      return std::nullopt;210 211    if (Entry->OverDefined.count(V))212      return ValueLatticeElement::getOverdefined();213 214    auto LatticeIt = Entry->LatticeElements.find_as(V);215    if (LatticeIt == Entry->LatticeElements.end())216      return std::nullopt;217 218    return LatticeIt->second;219  }220 221  bool222  isNonNullAtEndOfBlock(Value *V, BasicBlock *BB,223                        function_ref<NonNullPointerSet(BasicBlock *)> InitFn) {224    BlockCacheEntry *Entry = getOrCreateBlockEntry(BB);225    if (!Entry->NonNullPointers) {226      Entry->NonNullPointers = InitFn(BB);227      for (Value *V : *Entry->NonNullPointers)228        addValueHandle(V);229    }230 231    return Entry->NonNullPointers->count(V);232  }233 234  /// clear - Empty the cache.235  void clear() {236    BlockCache.clear();237    ValueHandles.clear();238  }239 240  /// Inform the cache that a given value has been deleted.241  void eraseValue(Value *V);242 243  /// This is part of the update interface to inform the cache244  /// that a block has been deleted.245  void eraseBlock(BasicBlock *BB);246 247  /// Updates the cache to remove any influence an overdefined value in248  /// OldSucc might have (unless also overdefined in NewSucc).  This just249  /// flushes elements from the cache and does not add any.250  void threadEdgeImpl(BasicBlock *OldSucc, BasicBlock *NewSucc);251};252} // namespace253 254void LazyValueInfoCache::eraseValue(Value *V) {255  for (auto &Pair : BlockCache) {256    Pair.second->LatticeElements.erase(V);257    Pair.second->OverDefined.erase(V);258    if (Pair.second->NonNullPointers)259      Pair.second->NonNullPointers->erase(V);260    if (PerPredRanges)261      Pair.second->PredecessorLatticeElements->erase(V);262  }263 264  auto HandleIt = ValueHandles.find_as(V);265  if (HandleIt != ValueHandles.end())266    ValueHandles.erase(HandleIt);267}268 269void LVIValueHandle::deleted() {270  // This erasure deallocates *this, so it MUST happen after we're done271  // using any and all members of *this.272  Parent->eraseValue(*this);273}274 275void LazyValueInfoCache::eraseBlock(BasicBlock *BB) {276  // Clear all when a BB is removed.277  if (PerPredRanges)278    for (auto &Pair : BlockCache)279      Pair.second->PredecessorLatticeElements->clear();280  BlockCache.erase(BB);281}282 283void LazyValueInfoCache::threadEdgeImpl(BasicBlock *OldSucc,284                                        BasicBlock *NewSucc) {285  // When an edge in the graph has been threaded, values that we could not286  // determine a value for before (i.e. were marked overdefined) may be287  // possible to solve now. We do NOT try to proactively update these values.288  // Instead, we clear their entries from the cache, and allow lazy updating to289  // recompute them when needed.290 291  // The updating process is fairly simple: we need to drop cached info292  // for all values that were marked overdefined in OldSucc, and for those same293  // values in any successor of OldSucc (except NewSucc) in which they were294  // also marked overdefined.295  std::vector<BasicBlock*> worklist;296  worklist.push_back(OldSucc);297 298  const BlockCacheEntry *Entry = getBlockEntry(OldSucc);299  if (!Entry || Entry->OverDefined.empty())300    return; // Nothing to process here.301  SmallVector<Value *, 4> ValsToClear(Entry->OverDefined.begin(),302                                      Entry->OverDefined.end());303 304  // Use a worklist to perform a depth-first search of OldSucc's successors.305  // NOTE: We do not need a visited list since any blocks we have already306  // visited will have had their overdefined markers cleared already, and we307  // thus won't loop to their successors.308  while (!worklist.empty()) {309    BasicBlock *ToUpdate = worklist.back();310    worklist.pop_back();311 312    // Skip blocks only accessible through NewSucc.313    if (ToUpdate == NewSucc) continue;314 315    // If a value was marked overdefined in OldSucc, and is here too...316    auto OI = BlockCache.find_as(ToUpdate);317    if (OI == BlockCache.end() || OI->second->OverDefined.empty())318      continue;319    auto &ValueSet = OI->second->OverDefined;320 321    bool changed = false;322    for (Value *V : ValsToClear) {323      if (!ValueSet.erase(V))324        continue;325 326      // If we removed anything, then we potentially need to update327      // blocks successors too.328      changed = true;329    }330 331    if (!changed) continue;332 333    llvm::append_range(worklist, successors(ToUpdate));334  }335}336 337namespace llvm {338namespace {339/// An assembly annotator class to print LazyValueCache information in340/// comments.341class LazyValueInfoAnnotatedWriter : public AssemblyAnnotationWriter {342  LazyValueInfoImpl *LVIImpl;343  // While analyzing which blocks we can solve values for, we need the dominator344  // information.345  DominatorTree &DT;346 347public:348  LazyValueInfoAnnotatedWriter(LazyValueInfoImpl *L, DominatorTree &DTree)349      : LVIImpl(L), DT(DTree) {}350 351  void emitBasicBlockStartAnnot(const BasicBlock *BB,352                                formatted_raw_ostream &OS) override;353 354  void emitInstructionAnnot(const Instruction *I,355                            formatted_raw_ostream &OS) override;356};357} // namespace358// The actual implementation of the lazy analysis and update.359class LazyValueInfoImpl {360 361  /// Cached results from previous queries362  LazyValueInfoCache TheCache;363 364  /// This stack holds the state of the value solver during a query.365  /// It basically emulates the callstack of the naive366  /// recursive value lookup process.367  SmallVector<std::pair<BasicBlock*, Value*>, 8> BlockValueStack;368 369  /// Keeps track of which block-value pairs are in BlockValueStack.370  DenseSet<std::pair<BasicBlock*, Value*> > BlockValueSet;371 372  /// Push BV onto BlockValueStack unless it's already in there.373  /// Returns true on success.374  bool pushBlockValue(const std::pair<BasicBlock *, Value *> &BV) {375    if (!BlockValueSet.insert(BV).second)376      return false;  // It's already in the stack.377 378    LLVM_DEBUG(dbgs() << "PUSH: " << *BV.second << " in "379                      << BV.first->getName() << "\n");380    BlockValueStack.push_back(BV);381    return true;382  }383 384  AssumptionCache *AC;  ///< A pointer to the cache of @llvm.assume calls.385  const DataLayout &DL; ///< A mandatory DataLayout386 387  /// Declaration of the llvm.experimental.guard() intrinsic,388  /// if it exists in the module.389  Function *GuardDecl;390 391  std::optional<ValueLatticeElement> getBlockValue(Value *Val, BasicBlock *BB,392                                                   Instruction *CxtI);393  std::optional<ValueLatticeElement> getEdgeValue(Value *V, BasicBlock *F,394                                                  BasicBlock *T,395                                                  Instruction *CxtI = nullptr);396 397  // These methods process one work item and may add more. A false value398  // returned means that the work item was not completely processed and must399  // be revisited after going through the new items.400  bool solveBlockValue(Value *Val, BasicBlock *BB);401  std::optional<ValueLatticeElement> solveBlockValueImpl(Value *Val,402                                                         BasicBlock *BB);403  std::optional<ValueLatticeElement> solveBlockValueNonLocal(Value *Val,404                                                             BasicBlock *BB);405  std::optional<ValueLatticeElement> solveBlockValuePHINode(PHINode *PN,406                                                            BasicBlock *BB);407  std::optional<ValueLatticeElement> solveBlockValueSelect(SelectInst *S,408                                                           BasicBlock *BB);409  std::optional<ConstantRange> getRangeFor(Value *V, Instruction *CxtI,410                                           BasicBlock *BB);411  std::optional<ValueLatticeElement> solveBlockValueBinaryOpImpl(412      Instruction *I, BasicBlock *BB,413      std::function<ConstantRange(const ConstantRange &, const ConstantRange &)>414          OpFn);415  std::optional<ValueLatticeElement>416  solveBlockValueBinaryOp(BinaryOperator *BBI, BasicBlock *BB);417  std::optional<ValueLatticeElement> solveBlockValueCast(CastInst *CI,418                                                         BasicBlock *BB);419  std::optional<ValueLatticeElement>420  solveBlockValueOverflowIntrinsic(WithOverflowInst *WO, BasicBlock *BB);421  std::optional<ValueLatticeElement> solveBlockValueIntrinsic(IntrinsicInst *II,422                                                              BasicBlock *BB);423  std::optional<ValueLatticeElement>424  solveBlockValueInsertElement(InsertElementInst *IEI, BasicBlock *BB);425  std::optional<ValueLatticeElement>426  solveBlockValueExtractValue(ExtractValueInst *EVI, BasicBlock *BB);427  bool isNonNullAtEndOfBlock(Value *Val, BasicBlock *BB);428  void intersectAssumeOrGuardBlockValueConstantRange(Value *Val,429                                                     ValueLatticeElement &BBLV,430                                                     Instruction *BBI);431 432  void solve();433 434  // For the following methods, if UseBlockValue is true, the function may435  // push additional values to the worklist and return nullopt. If436  // UseBlockValue is false, it will never return nullopt.437 438  std::optional<ValueLatticeElement>439  getValueFromSimpleICmpCondition(CmpInst::Predicate Pred, Value *RHS,440                                  const APInt &Offset, Instruction *CxtI,441                                  bool UseBlockValue);442 443  std::optional<ValueLatticeElement>444  getValueFromICmpCondition(Value *Val, ICmpInst *ICI, bool isTrueDest,445                            bool UseBlockValue);446  ValueLatticeElement getValueFromTrunc(Value *Val, TruncInst *Trunc,447                                        bool IsTrueDest);448 449  std::optional<ValueLatticeElement>450  getValueFromCondition(Value *Val, Value *Cond, bool IsTrueDest,451                        bool UseBlockValue, unsigned Depth = 0);452 453  std::optional<ValueLatticeElement> getEdgeValueLocal(Value *Val,454                                                       BasicBlock *BBFrom,455                                                       BasicBlock *BBTo,456                                                       bool UseBlockValue);457 458public:459  /// This is the query interface to determine the lattice value for the460  /// specified Value* at the context instruction (if specified) or at the461  /// start of the block.462  ValueLatticeElement getValueInBlock(Value *V, BasicBlock *BB,463                                      Instruction *CxtI = nullptr);464 465  /// This is the query interface to determine the lattice value for the466  /// specified Value* at the specified instruction using only information467  /// from assumes/guards and range metadata. Unlike getValueInBlock(), no468  /// recursive query is performed.469  ValueLatticeElement getValueAt(Value *V, Instruction *CxtI);470 471  /// This is the query interface to determine the lattice472  /// value for the specified Value* that is true on the specified edge.473  ValueLatticeElement getValueOnEdge(Value *V, BasicBlock *FromBB,474                                     BasicBlock *ToBB,475                                     Instruction *CxtI = nullptr);476 477  ValueLatticeElement getValueAtUse(const Use &U);478 479  /// Complete flush all previously computed values480  void clear() {481    TheCache.clear();482  }483 484  /// Printing the LazyValueInfo Analysis.485  void printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS) {486    LazyValueInfoAnnotatedWriter Writer(this, DTree);487    F.print(OS, &Writer);488  }489 490  /// This is part of the update interface to remove information related to this491  /// value from the cache.492  void forgetValue(Value *V) { TheCache.eraseValue(V); }493 494  /// This is part of the update interface to inform the cache495  /// that a block has been deleted.496  void eraseBlock(BasicBlock *BB) {497    TheCache.eraseBlock(BB);498  }499 500  /// This is the update interface to inform the cache that an edge from501  /// PredBB to OldSucc has been threaded to be from PredBB to NewSucc.502  void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);503 504  LazyValueInfoImpl(AssumptionCache *AC, const DataLayout &DL,505                    Function *GuardDecl)506      : AC(AC), DL(DL), GuardDecl(GuardDecl) {}507};508} // namespace llvm509 510void LazyValueInfoImpl::solve() {511  SmallVector<std::pair<BasicBlock *, Value *>, 8> StartingStack =512      BlockValueStack;513 514  unsigned processedCount = 0;515  while (!BlockValueStack.empty()) {516    processedCount++;517    // Abort if we have to process too many values to get a result for this one.518    // Because of the design of the overdefined cache currently being per-block519    // to avoid naming-related issues (IE it wants to try to give different520    // results for the same name in different blocks), overdefined results don't521    // get cached globally, which in turn means we will often try to rediscover522    // the same overdefined result again and again.  Once something like523    // PredicateInfo is used in LVI or CVP, we should be able to make the524    // overdefined cache global, and remove this throttle.525    if (processedCount > MaxProcessedPerValue) {526      LLVM_DEBUG(527          dbgs() << "Giving up on stack because we are getting too deep\n");528      // Fill in the original values529      while (!StartingStack.empty()) {530        std::pair<BasicBlock *, Value *> &e = StartingStack.back();531        TheCache.insertResult(e.second, e.first,532                              ValueLatticeElement::getOverdefined());533        StartingStack.pop_back();534      }535      BlockValueSet.clear();536      BlockValueStack.clear();537      return;538    }539    std::pair<BasicBlock *, Value *> e = BlockValueStack.back();540    assert(BlockValueSet.count(e) && "Stack value should be in BlockValueSet!");541    unsigned StackSize = BlockValueStack.size();542    (void) StackSize;543 544    if (solveBlockValue(e.second, e.first)) {545      // The work item was completely processed.546      assert(BlockValueStack.size() == StackSize &&547             BlockValueStack.back() == e && "Nothing should have been pushed!");548#ifndef NDEBUG549      std::optional<ValueLatticeElement> BBLV =550          TheCache.getCachedValueInfo(e.second, e.first);551      assert(BBLV && "Result should be in cache!");552      LLVM_DEBUG(553          dbgs() << "POP " << *e.second << " in " << e.first->getName() << " = "554                 << *BBLV << "\n");555#endif556 557      BlockValueStack.pop_back();558      BlockValueSet.erase(e);559    } else {560      // More work needs to be done before revisiting.561      assert(BlockValueStack.size() == StackSize + 1 &&562             "Exactly one element should have been pushed!");563    }564  }565}566 567std::optional<ValueLatticeElement>568LazyValueInfoImpl::getBlockValue(Value *Val, BasicBlock *BB,569                                 Instruction *CxtI) {570  // If already a constant, there is nothing to compute.571  if (Constant *VC = dyn_cast<Constant>(Val))572    return ValueLatticeElement::get(VC);573 574  if (std::optional<ValueLatticeElement> OptLatticeVal =575          TheCache.getCachedValueInfo(Val, BB)) {576    intersectAssumeOrGuardBlockValueConstantRange(Val, *OptLatticeVal, CxtI);577    return OptLatticeVal;578  }579 580  // We have hit a cycle, assume overdefined.581  if (!pushBlockValue({ BB, Val }))582    return ValueLatticeElement::getOverdefined();583 584  // Yet to be resolved.585  return std::nullopt;586}587 588static ValueLatticeElement getFromRangeMetadata(Instruction *BBI) {589  switch (BBI->getOpcode()) {590  default:591    break;592  case Instruction::Call:593  case Instruction::Invoke:594    if (std::optional<ConstantRange> Range = cast<CallBase>(BBI)->getRange())595      return ValueLatticeElement::getRange(*Range);596    [[fallthrough]];597  case Instruction::Load:598    if (MDNode *Ranges = BBI->getMetadata(LLVMContext::MD_range))599      if (isa<IntegerType>(BBI->getType())) {600        return ValueLatticeElement::getRange(601            getConstantRangeFromMetadata(*Ranges));602      }603    break;604  };605  // Nothing known - will be intersected with other facts606  return ValueLatticeElement::getOverdefined();607}608 609bool LazyValueInfoImpl::solveBlockValue(Value *Val, BasicBlock *BB) {610  assert(!isa<Constant>(Val) && "Value should not be constant");611  assert(!TheCache.getCachedValueInfo(Val, BB) &&612         "Value should not be in cache");613 614  // Hold off inserting this value into the Cache in case we have to return615  // false and come back later.616  std::optional<ValueLatticeElement> Res = solveBlockValueImpl(Val, BB);617  if (!Res)618    // Work pushed, will revisit619    return false;620 621  TheCache.insertResult(Val, BB, *Res);622  return true;623}624 625std::optional<ValueLatticeElement>626LazyValueInfoImpl::solveBlockValueImpl(Value *Val, BasicBlock *BB) {627  Instruction *BBI = dyn_cast<Instruction>(Val);628  if (!BBI || BBI->getParent() != BB)629    return solveBlockValueNonLocal(Val, BB);630 631  if (PHINode *PN = dyn_cast<PHINode>(BBI))632    return solveBlockValuePHINode(PN, BB);633 634  if (auto *SI = dyn_cast<SelectInst>(BBI))635    return solveBlockValueSelect(SI, BB);636 637  // If this value is a nonnull pointer, record it's range and bailout.  Note638  // that for all other pointer typed values, we terminate the search at the639  // definition.  We could easily extend this to look through geps, bitcasts,640  // and the like to prove non-nullness, but it's not clear that's worth it641  // compile time wise.  The context-insensitive value walk done inside642  // isKnownNonZero gets most of the profitable cases at much less expense.643  // This does mean that we have a sensitivity to where the defining644  // instruction is placed, even if it could legally be hoisted much higher.645  // That is unfortunate.646  PointerType *PT = dyn_cast<PointerType>(BBI->getType());647  if (PT && isKnownNonZero(BBI, DL))648    return ValueLatticeElement::getNot(ConstantPointerNull::get(PT));649 650  if (BBI->getType()->isIntOrIntVectorTy()) {651    if (auto *CI = dyn_cast<CastInst>(BBI))652      return solveBlockValueCast(CI, BB);653 654    if (BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI))655      return solveBlockValueBinaryOp(BO, BB);656 657    if (auto *IEI = dyn_cast<InsertElementInst>(BBI))658      return solveBlockValueInsertElement(IEI, BB);659 660    if (auto *EVI = dyn_cast<ExtractValueInst>(BBI))661      return solveBlockValueExtractValue(EVI, BB);662 663    if (auto *II = dyn_cast<IntrinsicInst>(BBI))664      return solveBlockValueIntrinsic(II, BB);665  }666 667  LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()668                    << "' - unknown inst def found.\n");669  return getFromRangeMetadata(BBI);670}671 672static void AddNonNullPointer(Value *Ptr, NonNullPointerSet &PtrSet,673                              bool IsDereferenced = true) {674  // TODO: Use NullPointerIsDefined instead.675  if (Ptr->getType()->getPointerAddressSpace() == 0)676    PtrSet.insert(IsDereferenced ? getUnderlyingObject(Ptr)677                                 : Ptr->stripInBoundsOffsets());678}679 680static void AddNonNullPointersByInstruction(681    Instruction *I, NonNullPointerSet &PtrSet) {682  if (LoadInst *L = dyn_cast<LoadInst>(I)) {683    AddNonNullPointer(L->getPointerOperand(), PtrSet);684  } else if (StoreInst *S = dyn_cast<StoreInst>(I)) {685    AddNonNullPointer(S->getPointerOperand(), PtrSet);686  } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {687    if (MI->isVolatile()) return;688 689    // FIXME: check whether it has a valuerange that excludes zero?690    ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength());691    if (!Len || Len->isZero()) return;692 693    AddNonNullPointer(MI->getRawDest(), PtrSet);694    if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))695      AddNonNullPointer(MTI->getRawSource(), PtrSet);696  } else if (auto *CB = dyn_cast<CallBase>(I)) {697    for (auto &U : CB->args()) {698      if (U->getType()->isPointerTy() &&699          CB->paramHasNonNullAttr(CB->getArgOperandNo(&U),700                                  /*AllowUndefOrPoison=*/false))701        AddNonNullPointer(U.get(), PtrSet, /*IsDereferenced=*/false);702    }703  }704}705 706bool LazyValueInfoImpl::isNonNullAtEndOfBlock(Value *Val, BasicBlock *BB) {707  if (NullPointerIsDefined(BB->getParent(),708                           Val->getType()->getPointerAddressSpace()))709    return false;710 711  Val = Val->stripInBoundsOffsets();712  return TheCache.isNonNullAtEndOfBlock(Val, BB, [](BasicBlock *BB) {713    NonNullPointerSet NonNullPointers;714    for (Instruction &I : *BB)715      AddNonNullPointersByInstruction(&I, NonNullPointers);716    return NonNullPointers;717  });718}719 720std::optional<ValueLatticeElement>721LazyValueInfoImpl::solveBlockValueNonLocal(Value *Val, BasicBlock *BB) {722  ValueLatticeElement Result;  // Start Undefined.723 724  // If this is the entry block, we must be asking about an argument.725  if (BB->isEntryBlock()) {726    assert(isa<Argument>(Val) && "Unknown live-in to the entry block");727    if (std::optional<ConstantRange> Range = cast<Argument>(Val)->getRange())728      return ValueLatticeElement::getRange(*Range);729    return ValueLatticeElement::getOverdefined();730  }731 732  // Loop over all of our predecessors, merging what we know from them into733  // result.  If we encounter an unexplored predecessor, we eagerly explore it734  // in a depth first manner.  In practice, this has the effect of discovering735  // paths we can't analyze eagerly without spending compile times analyzing736  // other paths.  This heuristic benefits from the fact that predecessors are737  // frequently arranged such that dominating ones come first and we quickly738  // find a path to function entry.  TODO: We should consider explicitly739  // canonicalizing to make this true rather than relying on this happy740  // accident.741  std::optional<BBLatticeElementMap> PredLatticeElements;742  if (PerPredRanges)743    PredLatticeElements = std::make_optional<BBLatticeElementMap>();744  for (BasicBlock *Pred : predecessors(BB)) {745    // Skip self loops.746    if (Pred == BB)747      continue;748    std::optional<ValueLatticeElement> EdgeResult = getEdgeValue(Val, Pred, BB);749    if (!EdgeResult)750      // Explore that input, then return here751      return std::nullopt;752 753    Result.mergeIn(*EdgeResult);754 755    // If we hit overdefined, exit early.  The BlockVals entry is already set756    // to overdefined.757    if (Result.isOverdefined()) {758      LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()759                        << "' - overdefined because of pred '"760                        << Pred->getName() << "' (non local).\n");761      return Result;762    }763    if (PerPredRanges)764      PredLatticeElements->insert({Pred, *EdgeResult});765  }766 767  if (PerPredRanges)768    TheCache.insertPredecessorResults(Val, BB, *PredLatticeElements);769 770  // Return the merged value, which is more precise than 'overdefined'.771  assert(!Result.isOverdefined());772  return Result;773}774 775std::optional<ValueLatticeElement>776LazyValueInfoImpl::solveBlockValuePHINode(PHINode *PN, BasicBlock *BB) {777  ValueLatticeElement Result;  // Start Undefined.778 779  // Loop over all of our predecessors, merging what we know from them into780  // result.  See the comment about the chosen traversal order in781  // solveBlockValueNonLocal; the same reasoning applies here.782  std::optional<BBLatticeElementMap> PredLatticeElements;783  if (PerPredRanges)784    PredLatticeElements = std::make_optional<BBLatticeElementMap>();785  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {786    BasicBlock *PhiBB = PN->getIncomingBlock(i);787    Value *PhiVal = PN->getIncomingValue(i);788    // Note that we can provide PN as the context value to getEdgeValue, even789    // though the results will be cached, because PN is the value being used as790    // the cache key in the caller.791    std::optional<ValueLatticeElement> EdgeResult =792        getEdgeValue(PhiVal, PhiBB, BB, PN);793    if (!EdgeResult)794      // Explore that input, then return here795      return std::nullopt;796 797    Result.mergeIn(*EdgeResult);798 799    // If we hit overdefined, exit early.  The BlockVals entry is already set800    // to overdefined.801    if (Result.isOverdefined()) {802      LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()803                        << "' - overdefined because of pred (local).\n");804 805      return Result;806    }807 808    if (PerPredRanges)809      PredLatticeElements->insert({PhiBB, *EdgeResult});810  }811 812  if (PerPredRanges)813    TheCache.insertPredecessorResults(PN, BB, *PredLatticeElements);814 815  // Return the merged value, which is more precise than 'overdefined'.816  assert(!Result.isOverdefined() && "Possible PHI in entry block?");817  return Result;818}819 820// If we can determine a constraint on the value given conditions assumed by821// the program, intersect those constraints with BBLV822void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange(823    Value *Val, ValueLatticeElement &BBLV, Instruction *BBI) {824  BBI = BBI ? BBI : dyn_cast<Instruction>(Val);825  if (!BBI)826    return;827 828  BasicBlock *BB = BBI->getParent();829  for (auto &AssumeVH : AC->assumptionsFor(Val)) {830    if (!AssumeVH)831      continue;832 833    // Only check assumes in the block of the context instruction. Other834    // assumes will have already been taken into account when the value was835    // propagated from predecessor blocks.836    auto *I = cast<CallInst>(AssumeVH);837    if (I->getParent() != BB || !isValidAssumeForContext(I, BBI))838      continue;839 840    BBLV = BBLV.intersect(*getValueFromCondition(Val, I->getArgOperand(0),841                                                 /*IsTrueDest*/ true,842                                                 /*UseBlockValue*/ false));843  }844 845  // If guards are not used in the module, don't spend time looking for them846  if (GuardDecl && !GuardDecl->use_empty() &&847      BBI->getIterator() != BB->begin()) {848    for (Instruction &I :849         make_range(std::next(BBI->getIterator().getReverse()), BB->rend())) {850      Value *Cond = nullptr;851      if (match(&I, m_Intrinsic<Intrinsic::experimental_guard>(m_Value(Cond))))852        BBLV = BBLV.intersect(*getValueFromCondition(Val, Cond,853                                                     /*IsTrueDest*/ true,854                                                     /*UseBlockValue*/ false));855    }856  }857 858  if (BBLV.isOverdefined()) {859    // Check whether we're checking at the terminator, and the pointer has860    // been dereferenced in this block.861    PointerType *PTy = dyn_cast<PointerType>(Val->getType());862    if (PTy && BB->getTerminator() == BBI &&863        isNonNullAtEndOfBlock(Val, BB))864      BBLV = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy));865  }866}867 868std::optional<ValueLatticeElement>869LazyValueInfoImpl::solveBlockValueSelect(SelectInst *SI, BasicBlock *BB) {870  // Recurse on our inputs if needed871  std::optional<ValueLatticeElement> OptTrueVal =872      getBlockValue(SI->getTrueValue(), BB, SI);873  if (!OptTrueVal)874    return std::nullopt;875  ValueLatticeElement &TrueVal = *OptTrueVal;876 877  std::optional<ValueLatticeElement> OptFalseVal =878      getBlockValue(SI->getFalseValue(), BB, SI);879  if (!OptFalseVal)880    return std::nullopt;881  ValueLatticeElement &FalseVal = *OptFalseVal;882 883  if (TrueVal.isConstantRange() || FalseVal.isConstantRange()) {884    const ConstantRange &TrueCR = TrueVal.asConstantRange(SI->getType());885    const ConstantRange &FalseCR = FalseVal.asConstantRange(SI->getType());886    Value *LHS = nullptr;887    Value *RHS = nullptr;888    SelectPatternResult SPR = matchSelectPattern(SI, LHS, RHS);889    // Is this a min specifically of our two inputs?  (Avoid the risk of890    // ValueTracking getting smarter looking back past our immediate inputs.)891    if (SelectPatternResult::isMinOrMax(SPR.Flavor) &&892        ((LHS == SI->getTrueValue() && RHS == SI->getFalseValue()) ||893         (RHS == SI->getTrueValue() && LHS == SI->getFalseValue()))) {894      ConstantRange ResultCR = [&]() {895        switch (SPR.Flavor) {896        default:897          llvm_unreachable("unexpected minmax type!");898        case SPF_SMIN:                   /// Signed minimum899          return TrueCR.smin(FalseCR);900        case SPF_UMIN:                   /// Unsigned minimum901          return TrueCR.umin(FalseCR);902        case SPF_SMAX:                   /// Signed maximum903          return TrueCR.smax(FalseCR);904        case SPF_UMAX:                   /// Unsigned maximum905          return TrueCR.umax(FalseCR);906        };907      }();908      return ValueLatticeElement::getRange(909          ResultCR, TrueVal.isConstantRangeIncludingUndef() ||910                        FalseVal.isConstantRangeIncludingUndef());911    }912 913    if (SPR.Flavor == SPF_ABS) {914      if (LHS == SI->getTrueValue())915        return ValueLatticeElement::getRange(916            TrueCR.abs(), TrueVal.isConstantRangeIncludingUndef());917      if (LHS == SI->getFalseValue())918        return ValueLatticeElement::getRange(919            FalseCR.abs(), FalseVal.isConstantRangeIncludingUndef());920    }921 922    if (SPR.Flavor == SPF_NABS) {923      ConstantRange Zero(APInt::getZero(TrueCR.getBitWidth()));924      if (LHS == SI->getTrueValue())925        return ValueLatticeElement::getRange(926            Zero.sub(TrueCR.abs()), FalseVal.isConstantRangeIncludingUndef());927      if (LHS == SI->getFalseValue())928        return ValueLatticeElement::getRange(929            Zero.sub(FalseCR.abs()), FalseVal.isConstantRangeIncludingUndef());930    }931  }932 933  // Can we constrain the facts about the true and false values by using the934  // condition itself?  This shows up with idioms like e.g. select(a > 5, a, 5).935  // TODO: We could potentially refine an overdefined true value above.936  Value *Cond = SI->getCondition();937  // If the value is undef, a different value may be chosen in938  // the select condition.939  if (isGuaranteedNotToBeUndef(Cond, AC)) {940    TrueVal =941        TrueVal.intersect(*getValueFromCondition(SI->getTrueValue(), Cond,942                                                 /*IsTrueDest*/ true,943                                                 /*UseBlockValue*/ false));944    FalseVal =945        FalseVal.intersect(*getValueFromCondition(SI->getFalseValue(), Cond,946                                                  /*IsTrueDest*/ false,947                                                  /*UseBlockValue*/ false));948  }949 950  TrueVal.mergeIn(FalseVal);951  return TrueVal;952}953 954std::optional<ConstantRange>955LazyValueInfoImpl::getRangeFor(Value *V, Instruction *CxtI, BasicBlock *BB) {956  std::optional<ValueLatticeElement> OptVal = getBlockValue(V, BB, CxtI);957  if (!OptVal)958    return std::nullopt;959  return OptVal->asConstantRange(V->getType());960}961 962std::optional<ValueLatticeElement>963LazyValueInfoImpl::solveBlockValueCast(CastInst *CI, BasicBlock *BB) {964  // Filter out casts we don't know how to reason about before attempting to965  // recurse on our operand.  This can cut a long search short if we know we're966  // not going to be able to get any useful information anways.967  switch (CI->getOpcode()) {968  case Instruction::Trunc:969  case Instruction::SExt:970  case Instruction::ZExt:971    break;972  default:973    // Unhandled instructions are overdefined.974    LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()975                      << "' - overdefined (unknown cast).\n");976    return ValueLatticeElement::getOverdefined();977  }978 979  // Figure out the range of the LHS.  If that fails, we still apply the980  // transfer rule on the full set since we may be able to locally infer981  // interesting facts.982  std::optional<ConstantRange> LHSRes = getRangeFor(CI->getOperand(0), CI, BB);983  if (!LHSRes)984    // More work to do before applying this transfer rule.985    return std::nullopt;986  const ConstantRange &LHSRange = *LHSRes;987 988  const unsigned ResultBitWidth = CI->getType()->getScalarSizeInBits();989 990  // NOTE: We're currently limited by the set of operations that ConstantRange991  // can evaluate symbolically.  Enhancing that set will allows us to analyze992  // more definitions.993  ConstantRange Res = ConstantRange::getEmpty(ResultBitWidth);994  if (auto *Trunc = dyn_cast<TruncInst>(CI))995    Res = LHSRange.truncate(ResultBitWidth, Trunc->getNoWrapKind());996  else997    Res = LHSRange.castOp(CI->getOpcode(), ResultBitWidth);998 999  return ValueLatticeElement::getRange(Res);1000}1001 1002std::optional<ValueLatticeElement>1003LazyValueInfoImpl::solveBlockValueBinaryOpImpl(1004    Instruction *I, BasicBlock *BB,1005    std::function<ConstantRange(const ConstantRange &, const ConstantRange &)>1006        OpFn) {1007  Value *LHS = I->getOperand(0);1008  Value *RHS = I->getOperand(1);1009 1010  auto ThreadBinOpOverSelect =1011      [&](Value *X, const ConstantRange &CRX, SelectInst *Y,1012          bool XIsLHS) -> std::optional<ValueLatticeElement> {1013    Value *Cond = Y->getCondition();1014    // Only handle selects with constant values.1015    Constant *TrueC = dyn_cast<Constant>(Y->getTrueValue());1016    if (!TrueC)1017      return std::nullopt;1018    Constant *FalseC = dyn_cast<Constant>(Y->getFalseValue());1019    if (!FalseC)1020      return std::nullopt;1021    if (!isGuaranteedNotToBeUndef(Cond, AC))1022      return std::nullopt;1023 1024    ConstantRange TrueX =1025        CRX.intersectWith(getValueFromCondition(X, Cond, /*CondIsTrue=*/true,1026                                                /*UseBlockValue=*/false)1027                              ->asConstantRange(X->getType()));1028    ConstantRange FalseX =1029        CRX.intersectWith(getValueFromCondition(X, Cond, /*CondIsTrue=*/false,1030                                                /*UseBlockValue=*/false)1031                              ->asConstantRange(X->getType()));1032    ConstantRange TrueY = TrueC->toConstantRange();1033    ConstantRange FalseY = FalseC->toConstantRange();1034 1035    if (XIsLHS)1036      return ValueLatticeElement::getRange(1037          OpFn(TrueX, TrueY).unionWith(OpFn(FalseX, FalseY)));1038    return ValueLatticeElement::getRange(1039        OpFn(TrueY, TrueX).unionWith(OpFn(FalseY, FalseX)));1040  };1041 1042  // Figure out the ranges of the operands.  If that fails, use a1043  // conservative range, but apply the transfer rule anyways.  This1044  // lets us pick up facts from expressions like "and i32 (call i321045  // @foo()), 32"1046  std::optional<ConstantRange> LHSRes = getRangeFor(LHS, I, BB);1047  if (!LHSRes)1048    return std::nullopt;1049 1050  // Try to thread binop over rhs select1051  if (auto *SI = dyn_cast<SelectInst>(RHS)) {1052    if (auto Res = ThreadBinOpOverSelect(LHS, *LHSRes, SI, /*XIsLHS=*/true))1053      return *Res;1054  }1055 1056  std::optional<ConstantRange> RHSRes = getRangeFor(RHS, I, BB);1057  if (!RHSRes)1058    return std::nullopt;1059 1060  // Try to thread binop over lhs select1061  if (auto *SI = dyn_cast<SelectInst>(LHS)) {1062    if (auto Res = ThreadBinOpOverSelect(RHS, *RHSRes, SI, /*XIsLHS=*/false))1063      return *Res;1064  }1065 1066  const ConstantRange &LHSRange = *LHSRes;1067  const ConstantRange &RHSRange = *RHSRes;1068 1069  std::optional<ValueLatticeElement> MergedResult =1070      ValueLatticeElement::getRange(OpFn(LHSRange, RHSRange));1071 1072  if (!PerPredRanges)1073    return MergedResult;1074 1075  std::optional<BBLatticeElementMap> PredLHS =1076      TheCache.getCachedPredecessorInfo(LHS, BB);1077  if (!PredLHS)1078    return MergedResult;1079  std::optional<BBLatticeElementMap> PredRHS =1080      TheCache.getCachedPredecessorInfo(RHS, BB);1081  if (!PredRHS)1082    return MergedResult;1083 1084  const BBLatticeElementMap &LHSPredMap = *PredLHS;1085  const BBLatticeElementMap &RHSPredMap = *PredRHS;1086 1087  BBLatticeElementMap PredLatticeElements;1088  ValueLatticeElement OverallPredResult;1089  for (auto *Pred : predecessors(BB)) {1090    auto LHSIt = LHSPredMap.find_as(Pred);1091    if (LHSIt == LHSPredMap.end())1092      return MergedResult;1093    const ValueLatticeElement &LHSFromPred = LHSIt->second;1094    std::optional<ConstantRange> LHSFromPredRes =1095        LHSFromPred.asConstantRange(LHS->getType());1096    if (!LHSFromPredRes)1097      return MergedResult;1098 1099    auto RHSIt = RHSPredMap.find_as(Pred);1100    if (RHSIt == RHSPredMap.end())1101      return MergedResult;1102    const ValueLatticeElement &RHSFromPred = RHSIt->second;1103    std::optional<ConstantRange> RHSFromPredRes =1104        RHSFromPred.asConstantRange(RHS->getType());1105    if (!RHSFromPredRes)1106      return MergedResult;1107 1108    const ConstantRange &LHSFromPredRange = *LHSFromPredRes;1109    const ConstantRange &RHSFromPredRange = *RHSFromPredRes;1110    std::optional<ValueLatticeElement> PredResult =1111        ValueLatticeElement::getRange(OpFn(LHSFromPredRange, RHSFromPredRange));1112    if (!PredResult)1113      return MergedResult;1114    if (PredResult->isOverdefined()) {1115      LLVM_DEBUG(1116          dbgs() << " pred BB '" << Pred->getName() << "' for BB '"1117                 << BB->getName()1118                 << "' overdefined. Discarding all predecessor intervals.\n");1119      return MergedResult;1120    }1121    PredLatticeElements.insert({Pred, *PredResult});1122    OverallPredResult.mergeIn(*PredResult);1123  }1124 1125  // If this point is reached, all predecessors for both LHS and RHS have1126  // constant ranges previously computed. Can cache result and use the1127  // OverallPredResult;1128  TheCache.insertPredecessorResults(I, BB, PredLatticeElements);1129 1130  LLVM_DEBUG(dbgs() << " Using predecessor intervals, evaluated " << *I1131                    << " to: " << OverallPredResult << ".\n");1132 1133  if (!MergedResult)1134    return OverallPredResult;1135 1136  LLVM_DEBUG(dbgs() << " Intersecting intervals for " << *I << ": "1137                    << OverallPredResult << " and  " << MergedResult << ".\n");1138  return MergedResult->intersect(OverallPredResult);1139}1140 1141std::optional<ValueLatticeElement>1142LazyValueInfoImpl::solveBlockValueBinaryOp(BinaryOperator *BO, BasicBlock *BB) {1143  assert(BO->getOperand(0)->getType()->isSized() &&1144         "all operands to binary operators are sized");1145  if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(BO)) {1146    unsigned NoWrapKind = OBO->getNoWrapKind();1147    return solveBlockValueBinaryOpImpl(1148        BO, BB,1149        [BO, NoWrapKind](const ConstantRange &CR1, const ConstantRange &CR2) {1150          return CR1.overflowingBinaryOp(BO->getOpcode(), CR2, NoWrapKind);1151        });1152  }1153 1154  return solveBlockValueBinaryOpImpl(1155      BO, BB, [BO](const ConstantRange &CR1, const ConstantRange &CR2) {1156        return CR1.binaryOp(BO->getOpcode(), CR2);1157      });1158}1159 1160std::optional<ValueLatticeElement>1161LazyValueInfoImpl::solveBlockValueOverflowIntrinsic(WithOverflowInst *WO,1162                                                    BasicBlock *BB) {1163  return solveBlockValueBinaryOpImpl(1164      WO, BB, [WO](const ConstantRange &CR1, const ConstantRange &CR2) {1165        return CR1.binaryOp(WO->getBinaryOp(), CR2);1166      });1167}1168 1169std::optional<ValueLatticeElement>1170LazyValueInfoImpl::solveBlockValueIntrinsic(IntrinsicInst *II, BasicBlock *BB) {1171  ValueLatticeElement MetadataVal = getFromRangeMetadata(II);1172  if (!ConstantRange::isIntrinsicSupported(II->getIntrinsicID())) {1173    LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()1174                      << "' - unknown intrinsic.\n");1175    return MetadataVal;1176  }1177 1178  SmallVector<ConstantRange, 2> OpRanges;1179  for (Value *Op : II->args()) {1180    std::optional<ConstantRange> Range = getRangeFor(Op, II, BB);1181    if (!Range)1182      return std::nullopt;1183    OpRanges.push_back(*Range);1184  }1185 1186  return ValueLatticeElement::getRange(1187             ConstantRange::intrinsic(II->getIntrinsicID(), OpRanges))1188      .intersect(MetadataVal);1189}1190 1191std::optional<ValueLatticeElement>1192LazyValueInfoImpl::solveBlockValueInsertElement(InsertElementInst *IEI,1193                                                BasicBlock *BB) {1194  std::optional<ValueLatticeElement> OptEltVal =1195      getBlockValue(IEI->getOperand(1), BB, IEI);1196  if (!OptEltVal)1197    return std::nullopt;1198  ValueLatticeElement &Res = *OptEltVal;1199 1200  std::optional<ValueLatticeElement> OptVecVal =1201      getBlockValue(IEI->getOperand(0), BB, IEI);1202  if (!OptVecVal)1203    return std::nullopt;1204 1205  // Bail out if the inserted element is a constant expression. Unlike other1206  // ValueLattice types, these are not considered an implicit splat when a1207  // vector type is used.1208  // We could call ConstantFoldInsertElementInstruction here to handle these.1209  if (OptEltVal->isConstant())1210    return ValueLatticeElement::getOverdefined();1211 1212  Res.mergeIn(*OptVecVal);1213  return Res;1214}1215 1216std::optional<ValueLatticeElement>1217LazyValueInfoImpl::solveBlockValueExtractValue(ExtractValueInst *EVI,1218                                               BasicBlock *BB) {1219  if (auto *WO = dyn_cast<WithOverflowInst>(EVI->getAggregateOperand()))1220    if (EVI->getNumIndices() == 1 && *EVI->idx_begin() == 0)1221      return solveBlockValueOverflowIntrinsic(WO, BB);1222 1223  // Handle extractvalue of insertvalue to allow further simplification1224  // based on replaced with.overflow intrinsics.1225  if (Value *V = simplifyExtractValueInst(1226          EVI->getAggregateOperand(), EVI->getIndices(),1227          EVI->getDataLayout()))1228    return getBlockValue(V, BB, EVI);1229 1230  LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()1231                    << "' - overdefined (unknown extractvalue).\n");1232  return ValueLatticeElement::getOverdefined();1233}1234 1235static bool matchICmpOperand(APInt &Offset, Value *LHS, Value *Val,1236                             ICmpInst::Predicate Pred) {1237  if (LHS == Val)1238    return true;1239 1240  // Handle range checking idiom produced by InstCombine. We will subtract the1241  // offset from the allowed range for RHS in this case.1242  const APInt *C;1243  if (match(LHS, m_AddLike(m_Specific(Val), m_APInt(C)))) {1244    Offset = *C;1245    return true;1246  }1247 1248  // Handle the symmetric case. This appears in saturation patterns like1249  // (x == 16) ? 16 : (x + 1).1250  if (match(Val, m_AddLike(m_Specific(LHS), m_APInt(C)))) {1251    Offset = -*C;1252    return true;1253  }1254 1255  // If (x | y) < C, then (x < C) && (y < C).1256  if (match(LHS, m_c_Or(m_Specific(Val), m_Value())) &&1257      (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE))1258    return true;1259 1260  // If (x & y) > C, then (x > C) && (y > C).1261  if (match(LHS, m_c_And(m_Specific(Val), m_Value())) &&1262      (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE))1263    return true;1264 1265  return false;1266}1267 1268/// Get value range for a "(Val + Offset) Pred RHS" condition.1269std::optional<ValueLatticeElement>1270LazyValueInfoImpl::getValueFromSimpleICmpCondition(CmpInst::Predicate Pred,1271                                                   Value *RHS,1272                                                   const APInt &Offset,1273                                                   Instruction *CxtI,1274                                                   bool UseBlockValue) {1275  ConstantRange RHSRange(RHS->getType()->getScalarSizeInBits(),1276                         /*isFullSet=*/true);1277  if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {1278    RHSRange = ConstantRange(CI->getValue());1279  } else if (UseBlockValue) {1280    std::optional<ValueLatticeElement> R =1281        getBlockValue(RHS, CxtI->getParent(), CxtI);1282    if (!R)1283      return std::nullopt;1284    RHSRange = R->asConstantRange(RHS->getType());1285  }1286 1287  ConstantRange TrueValues =1288      ConstantRange::makeAllowedICmpRegion(Pred, RHSRange);1289  return ValueLatticeElement::getRange(TrueValues.subtract(Offset));1290}1291 1292static std::optional<ConstantRange>1293getRangeViaSLT(CmpInst::Predicate Pred, APInt RHS,1294               function_ref<std::optional<ConstantRange>(const APInt &)> Fn) {1295  bool Invert = false;1296  if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {1297    Pred = ICmpInst::getInversePredicate(Pred);1298    Invert = true;1299  }1300  if (Pred == ICmpInst::ICMP_SLE) {1301    Pred = ICmpInst::ICMP_SLT;1302    if (RHS.isMaxSignedValue())1303      return std::nullopt; // Could also return full/empty here, if we wanted.1304    ++RHS;1305  }1306  assert(Pred == ICmpInst::ICMP_SLT && "Must be signed predicate");1307  if (auto CR = Fn(RHS))1308    return Invert ? CR->inverse() : CR;1309  return std::nullopt;1310}1311 1312/// Get value range for a "ctpop(Val) Pred RHS" condition.1313static ValueLatticeElement getValueFromICmpCtpop(ICmpInst::Predicate Pred,1314                                                 Value *RHS) {1315  unsigned BitWidth = RHS->getType()->getScalarSizeInBits();1316 1317  auto *RHSConst = dyn_cast<ConstantInt>(RHS);1318  if (!RHSConst)1319    return ValueLatticeElement::getOverdefined();1320 1321  ConstantRange ResValRange =1322      ConstantRange::makeExactICmpRegion(Pred, RHSConst->getValue());1323 1324  unsigned ResMin = ResValRange.getUnsignedMin().getLimitedValue(BitWidth);1325  unsigned ResMax = ResValRange.getUnsignedMax().getLimitedValue(BitWidth);1326 1327  APInt ValMin = APInt::getLowBitsSet(BitWidth, ResMin);1328  APInt ValMax = APInt::getHighBitsSet(BitWidth, ResMax);1329  return ValueLatticeElement::getRange(1330      ConstantRange::getNonEmpty(std::move(ValMin), ValMax + 1));1331}1332 1333std::optional<ValueLatticeElement> LazyValueInfoImpl::getValueFromICmpCondition(1334    Value *Val, ICmpInst *ICI, bool isTrueDest, bool UseBlockValue) {1335  Value *LHS = ICI->getOperand(0);1336  Value *RHS = ICI->getOperand(1);1337 1338  // Get the predicate that must hold along the considered edge.1339  CmpInst::Predicate EdgePred =1340      isTrueDest ? ICI->getPredicate() : ICI->getInversePredicate();1341 1342  if (isa<Constant>(RHS)) {1343    if (ICI->isEquality() && LHS == Val) {1344      if (EdgePred == ICmpInst::ICMP_EQ)1345        return ValueLatticeElement::get(cast<Constant>(RHS));1346      else if (!isa<UndefValue>(RHS))1347        return ValueLatticeElement::getNot(cast<Constant>(RHS));1348    }1349  }1350 1351  Type *Ty = Val->getType();1352  if (!Ty->isIntegerTy())1353    return ValueLatticeElement::getOverdefined();1354 1355  unsigned BitWidth = Ty->getScalarSizeInBits();1356  APInt Offset(BitWidth, 0);1357  if (matchICmpOperand(Offset, LHS, Val, EdgePred))1358    return getValueFromSimpleICmpCondition(EdgePred, RHS, Offset, ICI,1359                                           UseBlockValue);1360 1361  CmpInst::Predicate SwappedPred = CmpInst::getSwappedPredicate(EdgePred);1362  if (matchICmpOperand(Offset, RHS, Val, SwappedPred))1363    return getValueFromSimpleICmpCondition(SwappedPred, LHS, Offset, ICI,1364                                           UseBlockValue);1365 1366  if (match(LHS, m_Intrinsic<Intrinsic::ctpop>(m_Specific(Val))))1367    return getValueFromICmpCtpop(EdgePred, RHS);1368 1369  const APInt *Mask, *C;1370  if (match(LHS, m_And(m_Specific(Val), m_APInt(Mask))) &&1371      match(RHS, m_APInt(C))) {1372    // If (Val & Mask) == C then all the masked bits are known and we can1373    // compute a value range based on that.1374    if (EdgePred == ICmpInst::ICMP_EQ) {1375      KnownBits Known;1376      Known.Zero = ~*C & *Mask;1377      Known.One = *C & *Mask;1378      return ValueLatticeElement::getRange(1379          ConstantRange::fromKnownBits(Known, /*IsSigned*/ false));1380    }1381 1382    if (EdgePred == ICmpInst::ICMP_NE)1383      return ValueLatticeElement::getRange(1384          ConstantRange::makeMaskNotEqualRange(*Mask, *C));1385  }1386 1387  // If (X urem Modulus) >= C, then X >= C.1388  // If trunc X >= C, then X >= C.1389  // TODO: An upper bound could be computed as well.1390  if (match(LHS, m_CombineOr(m_URem(m_Specific(Val), m_Value()),1391                             m_Trunc(m_Specific(Val)))) &&1392      match(RHS, m_APInt(C))) {1393    // Use the icmp region so we don't have to deal with different predicates.1394    ConstantRange CR = ConstantRange::makeExactICmpRegion(EdgePred, *C);1395    if (!CR.isEmptySet())1396      return ValueLatticeElement::getRange(ConstantRange::getNonEmpty(1397          CR.getUnsignedMin().zext(BitWidth), APInt(BitWidth, 0)));1398  }1399 1400  // Recognize:1401  // icmp slt (ashr X, ShAmtC), C --> icmp slt X, C << ShAmtC1402  // Preconditions: (C << ShAmtC) >> ShAmtC == C1403  const APInt *ShAmtC;1404  if (CmpInst::isSigned(EdgePred) &&1405      match(LHS, m_AShr(m_Specific(Val), m_APInt(ShAmtC))) &&1406      match(RHS, m_APInt(C))) {1407    auto CR = getRangeViaSLT(1408        EdgePred, *C, [&](const APInt &RHS) -> std::optional<ConstantRange> {1409          APInt New = RHS << *ShAmtC;1410          if ((New.ashr(*ShAmtC)) != RHS)1411            return std::nullopt;1412          return ConstantRange::getNonEmpty(1413              APInt::getSignedMinValue(New.getBitWidth()), New);1414        });1415    if (CR)1416      return ValueLatticeElement::getRange(*CR);1417  }1418 1419  // a - b or ptrtoint(a) - ptrtoint(b) ==/!= 0 if a ==/!= b1420  Value *X, *Y;1421  if (ICI->isEquality() && match(Val, m_Sub(m_Value(X), m_Value(Y)))) {1422    // Peek through ptrtoints1423    match(X, m_PtrToIntSameSize(DL, m_Value(X)));1424    match(Y, m_PtrToIntSameSize(DL, m_Value(Y)));1425    if ((X == LHS && Y == RHS) || (X == RHS && Y == LHS)) {1426      Constant *NullVal = Constant::getNullValue(Val->getType());1427      if (EdgePred == ICmpInst::ICMP_EQ)1428        return ValueLatticeElement::get(NullVal);1429      return ValueLatticeElement::getNot(NullVal);1430    }1431  }1432 1433  return ValueLatticeElement::getOverdefined();1434}1435 1436ValueLatticeElement LazyValueInfoImpl::getValueFromTrunc(Value *Val,1437                                                         TruncInst *Trunc,1438                                                         bool IsTrueDest) {1439  assert(Trunc->getType()->isIntOrIntVectorTy(1));1440 1441  if (Trunc->getOperand(0) != Val)1442    return ValueLatticeElement::getOverdefined();1443 1444  Type *Ty = Val->getType();1445 1446  if (Trunc->hasNoUnsignedWrap()) {1447    if (IsTrueDest)1448      return ValueLatticeElement::get(ConstantInt::get(Ty, 1));1449    return ValueLatticeElement::get(Constant::getNullValue(Ty));1450  }1451 1452  if (IsTrueDest)1453    return ValueLatticeElement::getNot(Constant::getNullValue(Ty));1454  return ValueLatticeElement::getNot(Constant::getAllOnesValue(Ty));1455}1456 1457// Handle conditions of the form1458// extractvalue(op.with.overflow(%x, C), 1).1459static ValueLatticeElement getValueFromOverflowCondition(1460    Value *Val, WithOverflowInst *WO, bool IsTrueDest) {1461  // TODO: This only works with a constant RHS for now. We could also compute1462  // the range of the RHS, but this doesn't fit into the current structure of1463  // the edge value calculation.1464  const APInt *C;1465  if (WO->getLHS() != Val || !match(WO->getRHS(), m_APInt(C)))1466    return ValueLatticeElement::getOverdefined();1467 1468  // Calculate the possible values of %x for which no overflow occurs.1469  ConstantRange NWR = ConstantRange::makeExactNoWrapRegion(1470      WO->getBinaryOp(), *C, WO->getNoWrapKind());1471 1472  // If overflow is false, %x is constrained to NWR. If overflow is true, %x is1473  // constrained to it's inverse (all values that might cause overflow).1474  if (IsTrueDest)1475    NWR = NWR.inverse();1476  return ValueLatticeElement::getRange(NWR);1477}1478 1479std::optional<ValueLatticeElement>1480LazyValueInfoImpl::getValueFromCondition(Value *Val, Value *Cond,1481                                         bool IsTrueDest, bool UseBlockValue,1482                                         unsigned Depth) {1483  if (ICmpInst *ICI = dyn_cast<ICmpInst>(Cond))1484    return getValueFromICmpCondition(Val, ICI, IsTrueDest, UseBlockValue);1485 1486  if (auto *Trunc = dyn_cast<TruncInst>(Cond))1487    return getValueFromTrunc(Val, Trunc, IsTrueDest);1488 1489  if (auto *EVI = dyn_cast<ExtractValueInst>(Cond))1490    if (auto *WO = dyn_cast<WithOverflowInst>(EVI->getAggregateOperand()))1491      if (EVI->getNumIndices() == 1 && *EVI->idx_begin() == 1)1492        return getValueFromOverflowCondition(Val, WO, IsTrueDest);1493 1494  if (++Depth == MaxAnalysisRecursionDepth)1495    return ValueLatticeElement::getOverdefined();1496 1497  Value *N;1498  if (match(Cond, m_Not(m_Value(N))))1499    return getValueFromCondition(Val, N, !IsTrueDest, UseBlockValue, Depth);1500 1501  Value *L, *R;1502  bool IsAnd;1503  if (match(Cond, m_LogicalAnd(m_Value(L), m_Value(R))))1504    IsAnd = true;1505  else if (match(Cond, m_LogicalOr(m_Value(L), m_Value(R))))1506    IsAnd = false;1507  else1508    return ValueLatticeElement::getOverdefined();1509 1510  std::optional<ValueLatticeElement> LV =1511      getValueFromCondition(Val, L, IsTrueDest, UseBlockValue, Depth);1512  if (!LV)1513    return std::nullopt;1514  std::optional<ValueLatticeElement> RV =1515      getValueFromCondition(Val, R, IsTrueDest, UseBlockValue, Depth);1516  if (!RV)1517    return std::nullopt;1518 1519  // if (L && R) -> intersect L and R1520  // if (!(L || R)) -> intersect !L and !R1521  // if (L || R) -> union L and R1522  // if (!(L && R)) -> union !L and !R1523  if (IsTrueDest ^ IsAnd) {1524    LV->mergeIn(*RV);1525    return *LV;1526  }1527 1528  return LV->intersect(*RV);1529}1530 1531// Return true if Usr has Op as an operand, otherwise false.1532static bool usesOperand(User *Usr, Value *Op) {1533  return is_contained(Usr->operands(), Op);1534}1535 1536// Return true if the instruction type of Val is supported by1537// constantFoldUser(). Currently CastInst, BinaryOperator and FreezeInst only.1538// Call this before calling constantFoldUser() to find out if it's even worth1539// attempting to call it.1540static bool isOperationFoldable(User *Usr) {1541  return isa<CastInst>(Usr) || isa<BinaryOperator>(Usr) || isa<FreezeInst>(Usr);1542}1543 1544// Check if Usr can be simplified to an integer constant when the value of one1545// of its operands Op is an integer constant OpConstVal. If so, return it as an1546// lattice value range with a single element or otherwise return an overdefined1547// lattice value.1548static ValueLatticeElement constantFoldUser(User *Usr, Value *Op,1549                                            const APInt &OpConstVal,1550                                            const DataLayout &DL) {1551  assert(isOperationFoldable(Usr) && "Precondition");1552  Constant* OpConst = Constant::getIntegerValue(Op->getType(), OpConstVal);1553  // Check if Usr can be simplified to a constant.1554  if (auto *CI = dyn_cast<CastInst>(Usr)) {1555    assert(CI->getOperand(0) == Op && "Operand 0 isn't Op");1556    if (auto *C = dyn_cast_or_null<ConstantInt>(1557            simplifyCastInst(CI->getOpcode(), OpConst,1558                             CI->getDestTy(), DL))) {1559      return ValueLatticeElement::getRange(ConstantRange(C->getValue()));1560    }1561  } else if (auto *BO = dyn_cast<BinaryOperator>(Usr)) {1562    bool Op0Match = BO->getOperand(0) == Op;1563    bool Op1Match = BO->getOperand(1) == Op;1564    assert((Op0Match || Op1Match) &&1565           "Operand 0 nor Operand 1 isn't a match");1566    Value *LHS = Op0Match ? OpConst : BO->getOperand(0);1567    Value *RHS = Op1Match ? OpConst : BO->getOperand(1);1568    if (auto *C = dyn_cast_or_null<ConstantInt>(1569            simplifyBinOp(BO->getOpcode(), LHS, RHS, DL))) {1570      return ValueLatticeElement::getRange(ConstantRange(C->getValue()));1571    }1572  } else if (isa<FreezeInst>(Usr)) {1573    assert(cast<FreezeInst>(Usr)->getOperand(0) == Op && "Operand 0 isn't Op");1574    return ValueLatticeElement::getRange(ConstantRange(OpConstVal));1575  }1576  return ValueLatticeElement::getOverdefined();1577}1578 1579/// Compute the value of Val on the edge BBFrom -> BBTo.1580std::optional<ValueLatticeElement>1581LazyValueInfoImpl::getEdgeValueLocal(Value *Val, BasicBlock *BBFrom,1582                                     BasicBlock *BBTo, bool UseBlockValue) {1583  // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we1584  // know that v != 0.1585  if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {1586    // If this is a conditional branch and only one successor goes to BBTo, then1587    // we may be able to infer something from the condition.1588    if (BI->isConditional() &&1589        BI->getSuccessor(0) != BI->getSuccessor(1)) {1590      bool isTrueDest = BI->getSuccessor(0) == BBTo;1591      assert(BI->getSuccessor(!isTrueDest) == BBTo &&1592             "BBTo isn't a successor of BBFrom");1593      Value *Condition = BI->getCondition();1594 1595      // If V is the condition of the branch itself, then we know exactly what1596      // it is.1597      // NB: The condition on a `br` can't be a vector type.1598      if (Condition == Val)1599        return ValueLatticeElement::get(ConstantInt::get(1600                              Type::getInt1Ty(Val->getContext()), isTrueDest));1601 1602      // If the condition of the branch is an equality comparison, we may be1603      // able to infer the value.1604      std::optional<ValueLatticeElement> Result =1605          getValueFromCondition(Val, Condition, isTrueDest, UseBlockValue);1606      if (!Result)1607        return std::nullopt;1608 1609      if (!Result->isOverdefined())1610        return Result;1611 1612      if (User *Usr = dyn_cast<User>(Val)) {1613        assert(Result->isOverdefined() && "Result isn't overdefined");1614        // Check with isOperationFoldable() first to avoid linearly iterating1615        // over the operands unnecessarily which can be expensive for1616        // instructions with many operands.1617        if (isa<IntegerType>(Usr->getType()) && isOperationFoldable(Usr)) {1618          const DataLayout &DL = BBTo->getDataLayout();1619          if (usesOperand(Usr, Condition)) {1620            // If Val has Condition as an operand and Val can be folded into a1621            // constant with either Condition == true or Condition == false,1622            // propagate the constant.1623            // eg.1624            //   ; %Val is true on the edge to %then.1625            //   %Val = and i1 %Condition, true.1626            //   br %Condition, label %then, label %else1627            APInt ConditionVal(1, isTrueDest ? 1 : 0);1628            Result = constantFoldUser(Usr, Condition, ConditionVal, DL);1629          } else if (isa<TruncInst, ZExtInst, SExtInst>(Usr)) {1630            ValueLatticeElement OpLatticeVal =1631                *getValueFromCondition(Usr->getOperand(0), Condition,1632                                       isTrueDest, /*UseBlockValue*/ false);1633 1634            if (OpLatticeVal.isConstantRange()) {1635              const unsigned ResultBitWidth =1636                  Usr->getType()->getScalarSizeInBits();1637              if (auto *Trunc = dyn_cast<TruncInst>(Usr))1638                return ValueLatticeElement::getRange(1639                    OpLatticeVal.getConstantRange().truncate(1640                        ResultBitWidth, Trunc->getNoWrapKind()));1641 1642              return ValueLatticeElement::getRange(1643                  OpLatticeVal.getConstantRange().castOp(1644                      cast<CastInst>(Usr)->getOpcode(), ResultBitWidth));1645            }1646            if (OpLatticeVal.isConstant()) {1647              Constant *C = OpLatticeVal.getConstant();1648              if (auto *CastC = ConstantFoldCastOperand(1649                      cast<CastInst>(Usr)->getOpcode(), C, Usr->getType(), DL))1650                return ValueLatticeElement::get(CastC);1651            }1652            return ValueLatticeElement::getOverdefined();1653          } else {1654            // If one of Val's operand has an inferred value, we may be able to1655            // infer the value of Val.1656            // eg.1657            //    ; %Val is 94 on the edge to %then.1658            //    %Val = add i8 %Op, 11659            //    %Condition = icmp eq i8 %Op, 931660            //    br i1 %Condition, label %then, label %else1661            for (unsigned i = 0; i < Usr->getNumOperands(); ++i) {1662              Value *Op = Usr->getOperand(i);1663              ValueLatticeElement OpLatticeVal = *getValueFromCondition(1664                  Op, Condition, isTrueDest, /*UseBlockValue*/ false);1665              if (std::optional<APInt> OpConst =1666                      OpLatticeVal.asConstantInteger()) {1667                Result = constantFoldUser(Usr, Op, *OpConst, DL);1668                break;1669              }1670            }1671          }1672        }1673      }1674      if (!Result->isOverdefined())1675        return Result;1676    }1677  }1678 1679  // If the edge was formed by a switch on the value, then we may know exactly1680  // what it is.1681  if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {1682    Value *Condition = SI->getCondition();1683    if (!isa<IntegerType>(Val->getType()))1684      return ValueLatticeElement::getOverdefined();1685    bool ValUsesConditionAndMayBeFoldable = false;1686    if (Condition != Val) {1687      // Check if Val has Condition as an operand.1688      if (User *Usr = dyn_cast<User>(Val))1689        ValUsesConditionAndMayBeFoldable = isOperationFoldable(Usr) &&1690            usesOperand(Usr, Condition);1691      if (!ValUsesConditionAndMayBeFoldable)1692        return ValueLatticeElement::getOverdefined();1693    }1694    assert((Condition == Val || ValUsesConditionAndMayBeFoldable) &&1695           "Condition != Val nor Val doesn't use Condition");1696 1697    bool DefaultCase = SI->getDefaultDest() == BBTo;1698    unsigned BitWidth = Val->getType()->getIntegerBitWidth();1699    ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/);1700 1701    for (auto Case : SI->cases()) {1702      APInt CaseValue = Case.getCaseValue()->getValue();1703      ConstantRange EdgeVal(CaseValue);1704      if (ValUsesConditionAndMayBeFoldable) {1705        User *Usr = cast<User>(Val);1706        const DataLayout &DL = BBTo->getDataLayout();1707        ValueLatticeElement EdgeLatticeVal =1708            constantFoldUser(Usr, Condition, CaseValue, DL);1709        if (EdgeLatticeVal.isOverdefined())1710          return ValueLatticeElement::getOverdefined();1711        EdgeVal = EdgeLatticeVal.getConstantRange();1712      }1713      if (DefaultCase) {1714        // It is possible that the default destination is the destination of1715        // some cases. We cannot perform difference for those cases.1716        // We know Condition != CaseValue in BBTo.  In some cases we can use1717        // this to infer Val == f(Condition) is != f(CaseValue).  For now, we1718        // only do this when f is identity (i.e. Val == Condition), but we1719        // should be able to do this for any injective f.1720        if (Case.getCaseSuccessor() != BBTo && Condition == Val)1721          EdgesVals = EdgesVals.difference(EdgeVal);1722      } else if (Case.getCaseSuccessor() == BBTo)1723        EdgesVals = EdgesVals.unionWith(EdgeVal);1724    }1725    return ValueLatticeElement::getRange(std::move(EdgesVals));1726  }1727  return ValueLatticeElement::getOverdefined();1728}1729 1730/// Compute the value of Val on the edge BBFrom -> BBTo or the value at1731/// the basic block if the edge does not constrain Val.1732std::optional<ValueLatticeElement>1733LazyValueInfoImpl::getEdgeValue(Value *Val, BasicBlock *BBFrom,1734                                BasicBlock *BBTo, Instruction *CxtI) {1735  // If already a constant, there is nothing to compute.1736  if (Constant *VC = dyn_cast<Constant>(Val))1737    return ValueLatticeElement::get(VC);1738 1739  std::optional<ValueLatticeElement> LocalResult =1740      getEdgeValueLocal(Val, BBFrom, BBTo, /*UseBlockValue*/ true);1741  if (!LocalResult)1742    return std::nullopt;1743 1744  if (hasSingleValue(*LocalResult))1745    // Can't get any more precise here1746    return LocalResult;1747 1748  std::optional<ValueLatticeElement> OptInBlock =1749      getBlockValue(Val, BBFrom, BBFrom->getTerminator());1750  if (!OptInBlock)1751    return std::nullopt;1752  ValueLatticeElement &InBlock = *OptInBlock;1753 1754  // We can use the context instruction (generically the ultimate instruction1755  // the calling pass is trying to simplify) here, even though the result of1756  // this function is generally cached when called from the solve* functions1757  // (and that cached result might be used with queries using a different1758  // context instruction), because when this function is called from the solve*1759  // functions, the context instruction is not provided. When called from1760  // LazyValueInfoImpl::getValueOnEdge, the context instruction is provided,1761  // but then the result is not cached.1762  intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock, CxtI);1763 1764  return LocalResult->intersect(InBlock);1765}1766 1767ValueLatticeElement LazyValueInfoImpl::getValueInBlock(Value *V, BasicBlock *BB,1768                                                       Instruction *CxtI) {1769  LLVM_DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"1770                    << BB->getName() << "'\n");1771 1772  assert(BlockValueStack.empty() && BlockValueSet.empty());1773  std::optional<ValueLatticeElement> OptResult = getBlockValue(V, BB, CxtI);1774  if (!OptResult) {1775    solve();1776    OptResult = getBlockValue(V, BB, CxtI);1777    assert(OptResult && "Value not available after solving");1778  }1779 1780  LLVM_DEBUG(dbgs() << "  Result = " << *OptResult << "\n");1781  return *OptResult;1782}1783 1784ValueLatticeElement LazyValueInfoImpl::getValueAt(Value *V, Instruction *CxtI) {1785  LLVM_DEBUG(dbgs() << "LVI Getting value " << *V << " at '" << CxtI->getName()1786                    << "'\n");1787 1788  if (auto *C = dyn_cast<Constant>(V))1789    return ValueLatticeElement::get(C);1790 1791  ValueLatticeElement Result = ValueLatticeElement::getOverdefined();1792  if (auto *I = dyn_cast<Instruction>(V))1793    Result = getFromRangeMetadata(I);1794  intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI);1795 1796  LLVM_DEBUG(dbgs() << "  Result = " << Result << "\n");1797  return Result;1798}1799 1800ValueLatticeElement LazyValueInfoImpl::1801getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB,1802               Instruction *CxtI) {1803  LLVM_DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"1804                    << FromBB->getName() << "' to '" << ToBB->getName()1805                    << "'\n");1806 1807  std::optional<ValueLatticeElement> Result =1808      getEdgeValue(V, FromBB, ToBB, CxtI);1809  while (!Result) {1810    // As the worklist only explicitly tracks block values (but not edge values)1811    // we may have to call solve() multiple times, as the edge value calculation1812    // may request additional block values.1813    solve();1814    Result = getEdgeValue(V, FromBB, ToBB, CxtI);1815  }1816 1817  LLVM_DEBUG(dbgs() << "  Result = " << *Result << "\n");1818  return *Result;1819}1820 1821ValueLatticeElement LazyValueInfoImpl::getValueAtUse(const Use &U) {1822  Value *V = U.get();1823  auto *CxtI = cast<Instruction>(U.getUser());1824  ValueLatticeElement VL = getValueInBlock(V, CxtI->getParent(), CxtI);1825 1826  // Check whether the only (possibly transitive) use of the value is in a1827  // position where V can be constrained by a select or branch condition.1828  const Use *CurrU = &U;1829  // TODO: Increase limit?1830  const unsigned MaxUsesToInspect = 3;1831  for (unsigned I = 0; I < MaxUsesToInspect; ++I) {1832    std::optional<ValueLatticeElement> CondVal;1833    auto *CurrI = cast<Instruction>(CurrU->getUser());1834    if (auto *SI = dyn_cast<SelectInst>(CurrI)) {1835      // If the value is undef, a different value may be chosen in1836      // the select condition and at use.1837      if (!isGuaranteedNotToBeUndef(SI->getCondition(), AC))1838        break;1839      if (CurrU->getOperandNo() == 1)1840        CondVal =1841            *getValueFromCondition(V, SI->getCondition(), /*IsTrueDest*/ true,1842                                   /*UseBlockValue*/ false);1843      else if (CurrU->getOperandNo() == 2)1844        CondVal =1845            *getValueFromCondition(V, SI->getCondition(), /*IsTrueDest*/ false,1846                                   /*UseBlockValue*/ false);1847    } else if (auto *PHI = dyn_cast<PHINode>(CurrI)) {1848      // TODO: Use non-local query?1849      CondVal = *getEdgeValueLocal(V, PHI->getIncomingBlock(*CurrU),1850                                   PHI->getParent(), /*UseBlockValue*/ false);1851    }1852    if (CondVal)1853      VL = VL.intersect(*CondVal);1854 1855    // Only follow one-use chain, to allow direct intersection of conditions.1856    // If there are multiple uses, we would have to intersect with the union of1857    // all conditions at different uses.1858    // Stop walking if we hit a non-speculatable instruction. Even if the1859    // result is only used under a specific condition, executing the1860    // instruction itself may cause side effects or UB already.1861    // This also disallows looking through phi nodes: If the phi node is part1862    // of a cycle, we might end up reasoning about values from different cycle1863    // iterations (PR60629).1864    if (!CurrI->hasOneUse() ||1865        !isSafeToSpeculativelyExecuteWithVariableReplaced(1866            CurrI, /*IgnoreUBImplyingAttrs=*/false))1867      break;1868    CurrU = &*CurrI->use_begin();1869  }1870  return VL;1871}1872 1873void LazyValueInfoImpl::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,1874                                   BasicBlock *NewSucc) {1875  TheCache.threadEdgeImpl(OldSucc, NewSucc);1876}1877 1878//===----------------------------------------------------------------------===//1879//                            LazyValueInfo Impl1880//===----------------------------------------------------------------------===//1881 1882bool LazyValueInfoWrapperPass::runOnFunction(Function &F) {1883  Info.AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);1884 1885  if (auto *Impl = Info.getImpl())1886    Impl->clear();1887 1888  // Fully lazy.1889  return false;1890}1891 1892void LazyValueInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {1893  AU.setPreservesAll();1894  AU.addRequired<AssumptionCacheTracker>();1895  AU.addRequired<TargetLibraryInfoWrapperPass>();1896}1897 1898LazyValueInfo &LazyValueInfoWrapperPass::getLVI() { return Info; }1899 1900/// This lazily constructs the LazyValueInfoImpl.1901LazyValueInfoImpl &LazyValueInfo::getOrCreateImpl(const Module *M) {1902  if (!PImpl) {1903    assert(M && "getCache() called with a null Module");1904    const DataLayout &DL = M->getDataLayout();1905    Function *GuardDecl =1906        Intrinsic::getDeclarationIfExists(M, Intrinsic::experimental_guard);1907    PImpl = new LazyValueInfoImpl(AC, DL, GuardDecl);1908  }1909  return *PImpl;1910}1911 1912LazyValueInfoImpl *LazyValueInfo::getImpl() { return PImpl; }1913 1914LazyValueInfo::~LazyValueInfo() { releaseMemory(); }1915 1916void LazyValueInfo::releaseMemory() {1917  // If the cache was allocated, free it.1918  if (auto *Impl = getImpl()) {1919    delete &*Impl;1920    PImpl = nullptr;1921  }1922}1923 1924bool LazyValueInfo::invalidate(Function &F, const PreservedAnalyses &PA,1925                               FunctionAnalysisManager::Invalidator &Inv) {1926  // We need to invalidate if we have either failed to preserve this analyses1927  // result directly or if any of its dependencies have been invalidated.1928  auto PAC = PA.getChecker<LazyValueAnalysis>();1929  if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()))1930    return true;1931 1932  return false;1933}1934 1935void LazyValueInfoWrapperPass::releaseMemory() { Info.releaseMemory(); }1936 1937LazyValueInfo LazyValueAnalysis::run(Function &F,1938                                     FunctionAnalysisManager &FAM) {1939  auto &AC = FAM.getResult<AssumptionAnalysis>(F);1940 1941  return LazyValueInfo(&AC, &F.getDataLayout());1942}1943 1944/// Returns true if we can statically tell that this value will never be a1945/// "useful" constant.  In practice, this means we've got something like an1946/// alloca or a malloc call for which a comparison against a constant can1947/// only be guarding dead code.  Note that we are potentially giving up some1948/// precision in dead code (a constant result) in favour of avoiding a1949/// expensive search for a easily answered common query.1950static bool isKnownNonConstant(Value *V) {1951  V = V->stripPointerCasts();1952  // The return val of alloc cannot be a Constant.1953  if (isa<AllocaInst>(V))1954    return true;1955  return false;1956}1957 1958Constant *LazyValueInfo::getConstant(Value *V, Instruction *CxtI) {1959  // Bail out early if V is known not to be a Constant.1960  if (isKnownNonConstant(V))1961    return nullptr;1962 1963  BasicBlock *BB = CxtI->getParent();1964  ValueLatticeElement Result =1965      getOrCreateImpl(BB->getModule()).getValueInBlock(V, BB, CxtI);1966 1967  if (Result.isConstant())1968    return Result.getConstant();1969  if (Result.isConstantRange()) {1970    const ConstantRange &CR = Result.getConstantRange();1971    if (const APInt *SingleVal = CR.getSingleElement())1972      return ConstantInt::get(V->getType(), *SingleVal);1973  }1974  return nullptr;1975}1976 1977ConstantRange LazyValueInfo::getConstantRange(Value *V, Instruction *CxtI,1978                                              bool UndefAllowed) {1979  BasicBlock *BB = CxtI->getParent();1980  ValueLatticeElement Result =1981      getOrCreateImpl(BB->getModule()).getValueInBlock(V, BB, CxtI);1982  return Result.asConstantRange(V->getType(), UndefAllowed);1983}1984 1985ConstantRange LazyValueInfo::getConstantRangeAtUse(const Use &U,1986                                                   bool UndefAllowed) {1987  auto *Inst = cast<Instruction>(U.getUser());1988  ValueLatticeElement Result =1989      getOrCreateImpl(Inst->getModule()).getValueAtUse(U);1990  return Result.asConstantRange(U->getType(), UndefAllowed);1991}1992 1993/// Determine whether the specified value is known to be a1994/// constant on the specified edge. Return null if not.1995Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB,1996                                           BasicBlock *ToBB,1997                                           Instruction *CxtI) {1998  Module *M = FromBB->getModule();1999  ValueLatticeElement Result =2000      getOrCreateImpl(M).getValueOnEdge(V, FromBB, ToBB, CxtI);2001 2002  if (Result.isConstant())2003    return Result.getConstant();2004  if (Result.isConstantRange()) {2005    const ConstantRange &CR = Result.getConstantRange();2006    if (const APInt *SingleVal = CR.getSingleElement())2007      return ConstantInt::get(V->getType(), *SingleVal);2008  }2009  return nullptr;2010}2011 2012ConstantRange LazyValueInfo::getConstantRangeOnEdge(Value *V,2013                                                    BasicBlock *FromBB,2014                                                    BasicBlock *ToBB,2015                                                    Instruction *CxtI) {2016  Module *M = FromBB->getModule();2017  ValueLatticeElement Result =2018      getOrCreateImpl(M).getValueOnEdge(V, FromBB, ToBB, CxtI);2019  // TODO: Should undef be allowed here?2020  return Result.asConstantRange(V->getType(), /*UndefAllowed*/ true);2021}2022 2023static Constant *getPredicateResult(CmpInst::Predicate Pred, Constant *C,2024                                    const ValueLatticeElement &Val,2025                                    const DataLayout &DL) {2026  // If we know the value is a constant, evaluate the conditional.2027  if (Val.isConstant())2028    return ConstantFoldCompareInstOperands(Pred, Val.getConstant(), C, DL);2029 2030  Type *ResTy = CmpInst::makeCmpResultType(C->getType());2031  if (Val.isConstantRange()) {2032    const ConstantRange &CR = Val.getConstantRange();2033    ConstantRange RHS = C->toConstantRange();2034    if (CR.icmp(Pred, RHS))2035      return ConstantInt::getTrue(ResTy);2036    if (CR.icmp(CmpInst::getInversePredicate(Pred), RHS))2037      return ConstantInt::getFalse(ResTy);2038    return nullptr;2039  }2040 2041  if (Val.isNotConstant()) {2042    // If this is an equality comparison, we can try to fold it knowing that2043    // "V != C1".2044    if (Pred == ICmpInst::ICMP_EQ) {2045      // !C1 == C -> false iff C1 == C.2046      Constant *Res = ConstantFoldCompareInstOperands(2047          ICmpInst::ICMP_NE, Val.getNotConstant(), C, DL);2048      if (Res && Res->isNullValue())2049        return ConstantInt::getFalse(ResTy);2050    } else if (Pred == ICmpInst::ICMP_NE) {2051      // !C1 != C -> true iff C1 == C.2052      Constant *Res = ConstantFoldCompareInstOperands(2053          ICmpInst::ICMP_NE, Val.getNotConstant(), C, DL);2054      if (Res && Res->isNullValue())2055        return ConstantInt::getTrue(ResTy);2056    }2057    return nullptr;2058  }2059 2060  return nullptr;2061}2062 2063/// Determine whether the specified value comparison with a constant is known to2064/// be true or false on the specified CFG edge. Pred is a CmpInst predicate.2065Constant *LazyValueInfo::getPredicateOnEdge(CmpInst::Predicate Pred, Value *V,2066                                            Constant *C, BasicBlock *FromBB,2067                                            BasicBlock *ToBB,2068                                            Instruction *CxtI) {2069  Module *M = FromBB->getModule();2070  ValueLatticeElement Result =2071      getOrCreateImpl(M).getValueOnEdge(V, FromBB, ToBB, CxtI);2072 2073  return getPredicateResult(Pred, C, Result, M->getDataLayout());2074}2075 2076Constant *LazyValueInfo::getPredicateAt(CmpInst::Predicate Pred, Value *V,2077                                        Constant *C, Instruction *CxtI,2078                                        bool UseBlockValue) {2079  // Is or is not NonNull are common predicates being queried. If2080  // isKnownNonZero can tell us the result of the predicate, we can2081  // return it quickly. But this is only a fastpath, and falling2082  // through would still be correct.2083  Module *M = CxtI->getModule();2084  const DataLayout &DL = M->getDataLayout();2085  if (V->getType()->isPointerTy() && C->isNullValue() &&2086      isKnownNonZero(V->stripPointerCastsSameRepresentation(), DL)) {2087    Type *ResTy = CmpInst::makeCmpResultType(C->getType());2088    if (Pred == ICmpInst::ICMP_EQ)2089      return ConstantInt::getFalse(ResTy);2090    else if (Pred == ICmpInst::ICMP_NE)2091      return ConstantInt::getTrue(ResTy);2092  }2093 2094  auto &Impl = getOrCreateImpl(M);2095  ValueLatticeElement Result =2096      UseBlockValue ? Impl.getValueInBlock(V, CxtI->getParent(), CxtI)2097                    : Impl.getValueAt(V, CxtI);2098  Constant *Ret = getPredicateResult(Pred, C, Result, DL);2099  if (Ret)2100    return Ret;2101 2102  // Note: The following bit of code is somewhat distinct from the rest of LVI;2103  // LVI as a whole tries to compute a lattice value which is conservatively2104  // correct at a given location.  In this case, we have a predicate which we2105  // weren't able to prove about the merged result, and we're pushing that2106  // predicate back along each incoming edge to see if we can prove it2107  // separately for each input.  As a motivating example, consider:2108  // bb1:2109  //   %v1 = ... ; constantrange<1, 5>2110  //   br label %merge2111  // bb2:2112  //   %v2 = ... ; constantrange<10, 20>2113  //   br label %merge2114  // merge:2115  //   %phi = phi [%v1, %v2] ; constantrange<1,20>2116  //   %pred = icmp eq i32 %phi, 82117  // We can't tell from the lattice value for '%phi' that '%pred' is false2118  // along each path, but by checking the predicate over each input separately,2119  // we can.2120  // We limit the search to one step backwards from the current BB and value.2121  // We could consider extending this to search further backwards through the2122  // CFG and/or value graph, but there are non-obvious compile time vs quality2123  // tradeoffs.2124  BasicBlock *BB = CxtI->getParent();2125 2126  // Function entry or an unreachable block.  Bail to avoid confusing2127  // analysis below.2128  pred_iterator PI = pred_begin(BB), PE = pred_end(BB);2129  if (PI == PE)2130    return nullptr;2131 2132  // If V is a PHI node in the same block as the context, we need to ask2133  // questions about the predicate as applied to the incoming value along2134  // each edge. This is useful for eliminating cases where the predicate is2135  // known along all incoming edges.2136  if (auto *PHI = dyn_cast<PHINode>(V))2137    if (PHI->getParent() == BB) {2138      Constant *Baseline = nullptr;2139      for (unsigned i = 0, e = PHI->getNumIncomingValues(); i < e; i++) {2140        Value *Incoming = PHI->getIncomingValue(i);2141        BasicBlock *PredBB = PHI->getIncomingBlock(i);2142        // Note that PredBB may be BB itself.2143        Constant *Result =2144            getPredicateOnEdge(Pred, Incoming, C, PredBB, BB, CxtI);2145 2146        // Keep going as long as we've seen a consistent known result for2147        // all inputs.2148        Baseline = (i == 0) ? Result /* First iteration */2149                            : (Baseline == Result ? Baseline2150                                                  : nullptr); /* All others */2151        if (!Baseline)2152          break;2153      }2154      if (Baseline)2155        return Baseline;2156    }2157 2158  // For a comparison where the V is outside this block, it's possible2159  // that we've branched on it before. Look to see if the value is known2160  // on all incoming edges.2161  if (!isa<Instruction>(V) || cast<Instruction>(V)->getParent() != BB) {2162    // For predecessor edge, determine if the comparison is true or false2163    // on that edge. If they're all true or all false, we can conclude2164    // the value of the comparison in this block.2165    Constant *Baseline = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);2166    if (Baseline) {2167      // Check that all remaining incoming values match the first one.2168      while (++PI != PE) {2169        Constant *Ret = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);2170        if (Ret != Baseline)2171          break;2172      }2173      // If we terminated early, then one of the values didn't match.2174      if (PI == PE) {2175        return Baseline;2176      }2177    }2178  }2179 2180  return nullptr;2181}2182 2183Constant *LazyValueInfo::getPredicateAt(CmpInst::Predicate Pred, Value *LHS,2184                                        Value *RHS, Instruction *CxtI,2185                                        bool UseBlockValue) {2186  if (auto *C = dyn_cast<Constant>(RHS))2187    return getPredicateAt(Pred, LHS, C, CxtI, UseBlockValue);2188  if (auto *C = dyn_cast<Constant>(LHS))2189    return getPredicateAt(CmpInst::getSwappedPredicate(Pred), RHS, C, CxtI,2190                          UseBlockValue);2191 2192  // Got two non-Constant values. Try to determine the comparison results based2193  // on the block values of the two operands, e.g. because they have2194  // non-overlapping ranges.2195  if (UseBlockValue) {2196    Module *M = CxtI->getModule();2197    ValueLatticeElement L =2198        getOrCreateImpl(M).getValueInBlock(LHS, CxtI->getParent(), CxtI);2199    if (L.isOverdefined())2200      return nullptr;2201 2202    ValueLatticeElement R =2203        getOrCreateImpl(M).getValueInBlock(RHS, CxtI->getParent(), CxtI);2204    Type *Ty = CmpInst::makeCmpResultType(LHS->getType());2205    return L.getCompare(Pred, Ty, R, M->getDataLayout());2206  }2207  return nullptr;2208}2209 2210void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,2211                               BasicBlock *NewSucc) {2212  if (auto *Impl = getImpl())2213    Impl->threadEdge(PredBB, OldSucc, NewSucc);2214}2215 2216void LazyValueInfo::forgetValue(Value *V) {2217  if (auto *Impl = getImpl())2218    Impl->forgetValue(V);2219}2220 2221void LazyValueInfo::eraseBlock(BasicBlock *BB) {2222  if (auto *Impl = getImpl())2223    Impl->eraseBlock(BB);2224}2225 2226void LazyValueInfo::clear() {2227  if (auto *Impl = getImpl())2228    Impl->clear();2229}2230 2231void LazyValueInfo::printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS) {2232  if (auto *Impl = getImpl())2233    Impl->printLVI(F, DTree, OS);2234}2235 2236// Print the LVI for the function arguments at the start of each basic block.2237void LazyValueInfoAnnotatedWriter::emitBasicBlockStartAnnot(2238    const BasicBlock *BB, formatted_raw_ostream &OS) {2239  // Find if there are latticevalues defined for arguments of the function.2240  auto *F = BB->getParent();2241  for (const auto &Arg : F->args()) {2242    ValueLatticeElement Result = LVIImpl->getValueInBlock(2243        const_cast<Argument *>(&Arg), const_cast<BasicBlock *>(BB));2244    if (Result.isUnknown())2245      continue;2246    OS << "; LatticeVal for: '" << Arg << "' is: " << Result << "\n";2247  }2248}2249 2250// This function prints the LVI analysis for the instruction I at the beginning2251// of various basic blocks. It relies on calculated values that are stored in2252// the LazyValueInfoCache, and in the absence of cached values, recalculate the2253// LazyValueInfo for `I`, and print that info.2254void LazyValueInfoAnnotatedWriter::emitInstructionAnnot(2255    const Instruction *I, formatted_raw_ostream &OS) {2256 2257  auto *ParentBB = I->getParent();2258  SmallPtrSet<const BasicBlock*, 16> BlocksContainingLVI;2259  // We can generate (solve) LVI values only for blocks that are dominated by2260  // the I's parent. However, to avoid generating LVI for all dominating blocks,2261  // that contain redundant/uninteresting information, we print LVI for2262  // blocks that may use this LVI information (such as immediate successor2263  // blocks, and blocks that contain uses of `I`).2264  auto printResult = [&](const BasicBlock *BB) {2265    if (!BlocksContainingLVI.insert(BB).second)2266      return;2267    ValueLatticeElement Result = LVIImpl->getValueInBlock(2268        const_cast<Instruction *>(I), const_cast<BasicBlock *>(BB));2269      OS << "; LatticeVal for: '" << *I << "' in BB: '";2270      BB->printAsOperand(OS, false);2271      OS << "' is: " << Result << "\n";2272  };2273 2274  printResult(ParentBB);2275  // Print the LVI analysis results for the immediate successor blocks, that2276  // are dominated by `ParentBB`.2277  for (const auto *BBSucc : successors(ParentBB))2278    if (DT.dominates(ParentBB, BBSucc))2279      printResult(BBSucc);2280 2281  // Print LVI in blocks where `I` is used.2282  for (const auto *U : I->users())2283    if (auto *UseI = dyn_cast<Instruction>(U))2284      if (!isa<PHINode>(UseI) || DT.dominates(ParentBB, UseI->getParent()))2285        printResult(UseI->getParent());2286 2287}2288 2289PreservedAnalyses LazyValueInfoPrinterPass::run(Function &F,2290                                                FunctionAnalysisManager &AM) {2291  OS << "LVI for function '" << F.getName() << "':\n";2292  auto &LVI = AM.getResult<LazyValueAnalysis>(F);2293  auto &DTree = AM.getResult<DominatorTreeAnalysis>(F);2294  LVI.printLVI(F, DTree, OS);2295  return PreservedAnalyses::all();2296}2297