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