xref: /llvm-project/llvm/lib/Transforms/Utils/SCCPSolver.cpp (revision b569ec6de6a0c57d6c4b675df7d7e3e28a9f4904)
1 //===- SCCPSolver.cpp - SCCP Utility --------------------------- *- 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 // \file
10 // This file implements the Sparse Conditional Constant Propagation (SCCP)
11 // utility.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/Transforms/Utils/SCCPSolver.h"
16 #include "llvm/ADT/SetVector.h"
17 #include "llvm/Analysis/ConstantFolding.h"
18 #include "llvm/Analysis/InstructionSimplify.h"
19 #include "llvm/Analysis/ValueLattice.h"
20 #include "llvm/Analysis/ValueLatticeUtils.h"
21 #include "llvm/Analysis/ValueTracking.h"
22 #include "llvm/IR/InstVisitor.h"
23 #include "llvm/Support/Casting.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Transforms/Utils/Local.h"
28 #include <cassert>
29 #include <utility>
30 #include <vector>
31 
32 using namespace llvm;
33 
34 #define DEBUG_TYPE "sccp"
35 
36 // The maximum number of range extensions allowed for operations requiring
37 // widening.
38 static const unsigned MaxNumRangeExtensions = 10;
39 
40 /// Returns MergeOptions with MaxWidenSteps set to MaxNumRangeExtensions.
41 static ValueLatticeElement::MergeOptions getMaxWidenStepsOpts() {
42   return ValueLatticeElement::MergeOptions().setMaxWidenSteps(
43       MaxNumRangeExtensions);
44 }
45 
46 namespace llvm {
47 
48 bool SCCPSolver::isConstant(const ValueLatticeElement &LV) {
49   return LV.isConstant() ||
50          (LV.isConstantRange() && LV.getConstantRange().isSingleElement());
51 }
52 
53 bool SCCPSolver::isOverdefined(const ValueLatticeElement &LV) {
54   return !LV.isUnknownOrUndef() && !SCCPSolver::isConstant(LV);
55 }
56 
57 bool SCCPSolver::tryToReplaceWithConstant(Value *V) {
58   Constant *Const = getConstantOrNull(V);
59   if (!Const)
60     return false;
61   // Replacing `musttail` instructions with constant breaks `musttail` invariant
62   // unless the call itself can be removed.
63   // Calls with "clang.arc.attachedcall" implicitly use the return value and
64   // those uses cannot be updated with a constant.
65   CallBase *CB = dyn_cast<CallBase>(V);
66   if (CB && ((CB->isMustTailCall() && !wouldInstructionBeTriviallyDead(CB)) ||
67              CB->getOperandBundle(LLVMContext::OB_clang_arc_attachedcall))) {
68     Function *F = CB->getCalledFunction();
69 
70     // Don't zap returns of the callee
71     if (F)
72       addToMustPreserveReturnsInFunctions(F);
73 
74     LLVM_DEBUG(dbgs() << "  Can\'t treat the result of call " << *CB
75                       << " as a constant\n");
76     return false;
77   }
78 
79   LLVM_DEBUG(dbgs() << "  Constant: " << *Const << " = " << *V << '\n');
80 
81   // Replaces all of the uses of a variable with uses of the constant.
82   V->replaceAllUsesWith(Const);
83   return true;
84 }
85 
86 /// Try to use \p Inst's value range from \p Solver to infer the NUW flag.
87 static bool refineInstruction(SCCPSolver &Solver,
88                               const SmallPtrSetImpl<Value *> &InsertedValues,
89                               Instruction &Inst) {
90   bool Changed = false;
91   auto GetRange = [&Solver, &InsertedValues](Value *Op) {
92     if (auto *Const = dyn_cast<Constant>(Op))
93       return Const->toConstantRange();
94     if (InsertedValues.contains(Op)) {
95       unsigned Bitwidth = Op->getType()->getScalarSizeInBits();
96       return ConstantRange::getFull(Bitwidth);
97     }
98     return Solver.getLatticeValueFor(Op).asConstantRange(
99         Op->getType(), /*UndefAllowed=*/false);
100   };
101 
102   if (isa<OverflowingBinaryOperator>(Inst)) {
103     if (Inst.hasNoSignedWrap() && Inst.hasNoUnsignedWrap())
104       return false;
105 
106     auto RangeA = GetRange(Inst.getOperand(0));
107     auto RangeB = GetRange(Inst.getOperand(1));
108     if (!Inst.hasNoUnsignedWrap()) {
109       auto NUWRange = ConstantRange::makeGuaranteedNoWrapRegion(
110           Instruction::BinaryOps(Inst.getOpcode()), RangeB,
111           OverflowingBinaryOperator::NoUnsignedWrap);
112       if (NUWRange.contains(RangeA)) {
113         Inst.setHasNoUnsignedWrap();
114         Changed = true;
115       }
116     }
117     if (!Inst.hasNoSignedWrap()) {
118       auto NSWRange = ConstantRange::makeGuaranteedNoWrapRegion(
119           Instruction::BinaryOps(Inst.getOpcode()), RangeB,
120           OverflowingBinaryOperator::NoSignedWrap);
121       if (NSWRange.contains(RangeA)) {
122         Inst.setHasNoSignedWrap();
123         Changed = true;
124       }
125     }
126   } else if (isa<PossiblyNonNegInst>(Inst) && !Inst.hasNonNeg()) {
127     auto Range = GetRange(Inst.getOperand(0));
128     if (Range.isAllNonNegative()) {
129       Inst.setNonNeg();
130       Changed = true;
131     }
132   } else if (TruncInst *TI = dyn_cast<TruncInst>(&Inst)) {
133     if (TI->hasNoSignedWrap() && TI->hasNoUnsignedWrap())
134       return false;
135 
136     auto Range = GetRange(Inst.getOperand(0));
137     uint64_t DestWidth = TI->getDestTy()->getScalarSizeInBits();
138     if (!TI->hasNoUnsignedWrap()) {
139       if (Range.getActiveBits() <= DestWidth) {
140         TI->setHasNoUnsignedWrap(true);
141         Changed = true;
142       }
143     }
144     if (!TI->hasNoSignedWrap()) {
145       if (Range.getMinSignedBits() <= DestWidth) {
146         TI->setHasNoSignedWrap(true);
147         Changed = true;
148       }
149     }
150   } else if (auto *GEP = dyn_cast<GetElementPtrInst>(&Inst)) {
151     if (GEP->hasNoUnsignedWrap() || !GEP->hasNoUnsignedSignedWrap())
152       return false;
153 
154     if (all_of(GEP->indices(),
155                [&](Value *V) { return GetRange(V).isAllNonNegative(); })) {
156       GEP->setNoWrapFlags(GEP->getNoWrapFlags() |
157                           GEPNoWrapFlags::noUnsignedWrap());
158       Changed = true;
159     }
160   }
161 
162   return Changed;
163 }
164 
165 /// Try to replace signed instructions with their unsigned equivalent.
166 static bool replaceSignedInst(SCCPSolver &Solver,
167                               SmallPtrSetImpl<Value *> &InsertedValues,
168                               Instruction &Inst) {
169   // Determine if a signed value is known to be >= 0.
170   auto isNonNegative = [&Solver](Value *V) {
171     // If this value was constant-folded, it may not have a solver entry.
172     // Handle integers. Otherwise, return false.
173     if (auto *C = dyn_cast<Constant>(V)) {
174       auto *CInt = dyn_cast<ConstantInt>(C);
175       return CInt && !CInt->isNegative();
176     }
177     const ValueLatticeElement &IV = Solver.getLatticeValueFor(V);
178     return IV.isConstantRange(/*UndefAllowed=*/false) &&
179            IV.getConstantRange().isAllNonNegative();
180   };
181 
182   Instruction *NewInst = nullptr;
183   switch (Inst.getOpcode()) {
184   case Instruction::SIToFP:
185   case Instruction::SExt: {
186     // If the source value is not negative, this is a zext/uitofp.
187     Value *Op0 = Inst.getOperand(0);
188     if (InsertedValues.count(Op0) || !isNonNegative(Op0))
189       return false;
190     NewInst = CastInst::Create(Inst.getOpcode() == Instruction::SExt
191                                    ? Instruction::ZExt
192                                    : Instruction::UIToFP,
193                                Op0, Inst.getType(), "", Inst.getIterator());
194     NewInst->setNonNeg();
195     break;
196   }
197   case Instruction::AShr: {
198     // If the shifted value is not negative, this is a logical shift right.
199     Value *Op0 = Inst.getOperand(0);
200     if (InsertedValues.count(Op0) || !isNonNegative(Op0))
201       return false;
202     NewInst = BinaryOperator::CreateLShr(Op0, Inst.getOperand(1), "", Inst.getIterator());
203     NewInst->setIsExact(Inst.isExact());
204     break;
205   }
206   case Instruction::SDiv:
207   case Instruction::SRem: {
208     // If both operands are not negative, this is the same as udiv/urem.
209     Value *Op0 = Inst.getOperand(0), *Op1 = Inst.getOperand(1);
210     if (InsertedValues.count(Op0) || InsertedValues.count(Op1) ||
211         !isNonNegative(Op0) || !isNonNegative(Op1))
212       return false;
213     auto NewOpcode = Inst.getOpcode() == Instruction::SDiv ? Instruction::UDiv
214                                                            : Instruction::URem;
215     NewInst = BinaryOperator::Create(NewOpcode, Op0, Op1, "", Inst.getIterator());
216     if (Inst.getOpcode() == Instruction::SDiv)
217       NewInst->setIsExact(Inst.isExact());
218     break;
219   }
220   default:
221     return false;
222   }
223 
224   // Wire up the new instruction and update state.
225   assert(NewInst && "Expected replacement instruction");
226   NewInst->takeName(&Inst);
227   InsertedValues.insert(NewInst);
228   Inst.replaceAllUsesWith(NewInst);
229   NewInst->setDebugLoc(Inst.getDebugLoc());
230   Solver.removeLatticeValueFor(&Inst);
231   Inst.eraseFromParent();
232   return true;
233 }
234 
235 bool SCCPSolver::simplifyInstsInBlock(BasicBlock &BB,
236                                       SmallPtrSetImpl<Value *> &InsertedValues,
237                                       Statistic &InstRemovedStat,
238                                       Statistic &InstReplacedStat) {
239   bool MadeChanges = false;
240   for (Instruction &Inst : make_early_inc_range(BB)) {
241     if (Inst.getType()->isVoidTy())
242       continue;
243     if (tryToReplaceWithConstant(&Inst)) {
244       if (wouldInstructionBeTriviallyDead(&Inst))
245         Inst.eraseFromParent();
246 
247       MadeChanges = true;
248       ++InstRemovedStat;
249     } else if (replaceSignedInst(*this, InsertedValues, Inst)) {
250       MadeChanges = true;
251       ++InstReplacedStat;
252     } else if (refineInstruction(*this, InsertedValues, Inst)) {
253       MadeChanges = true;
254     }
255   }
256   return MadeChanges;
257 }
258 
259 bool SCCPSolver::removeNonFeasibleEdges(BasicBlock *BB, DomTreeUpdater &DTU,
260                                         BasicBlock *&NewUnreachableBB) const {
261   SmallPtrSet<BasicBlock *, 8> FeasibleSuccessors;
262   bool HasNonFeasibleEdges = false;
263   for (BasicBlock *Succ : successors(BB)) {
264     if (isEdgeFeasible(BB, Succ))
265       FeasibleSuccessors.insert(Succ);
266     else
267       HasNonFeasibleEdges = true;
268   }
269 
270   // All edges feasible, nothing to do.
271   if (!HasNonFeasibleEdges)
272     return false;
273 
274   // SCCP can only determine non-feasible edges for br, switch and indirectbr.
275   Instruction *TI = BB->getTerminator();
276   assert((isa<BranchInst>(TI) || isa<SwitchInst>(TI) ||
277           isa<IndirectBrInst>(TI)) &&
278          "Terminator must be a br, switch or indirectbr");
279 
280   if (FeasibleSuccessors.size() == 0) {
281     // Branch on undef/poison, replace with unreachable.
282     SmallPtrSet<BasicBlock *, 8> SeenSuccs;
283     SmallVector<DominatorTree::UpdateType, 8> Updates;
284     for (BasicBlock *Succ : successors(BB)) {
285       Succ->removePredecessor(BB);
286       if (SeenSuccs.insert(Succ).second)
287         Updates.push_back({DominatorTree::Delete, BB, Succ});
288     }
289     TI->eraseFromParent();
290     new UnreachableInst(BB->getContext(), BB);
291     DTU.applyUpdatesPermissive(Updates);
292   } else if (FeasibleSuccessors.size() == 1) {
293     // Replace with an unconditional branch to the only feasible successor.
294     BasicBlock *OnlyFeasibleSuccessor = *FeasibleSuccessors.begin();
295     SmallVector<DominatorTree::UpdateType, 8> Updates;
296     bool HaveSeenOnlyFeasibleSuccessor = false;
297     for (BasicBlock *Succ : successors(BB)) {
298       if (Succ == OnlyFeasibleSuccessor && !HaveSeenOnlyFeasibleSuccessor) {
299         // Don't remove the edge to the only feasible successor the first time
300         // we see it. We still do need to remove any multi-edges to it though.
301         HaveSeenOnlyFeasibleSuccessor = true;
302         continue;
303       }
304 
305       Succ->removePredecessor(BB);
306       Updates.push_back({DominatorTree::Delete, BB, Succ});
307     }
308 
309     Instruction *BI = BranchInst::Create(OnlyFeasibleSuccessor, BB);
310     BI->setDebugLoc(TI->getDebugLoc());
311     TI->eraseFromParent();
312     DTU.applyUpdatesPermissive(Updates);
313   } else if (FeasibleSuccessors.size() > 1) {
314     SwitchInstProfUpdateWrapper SI(*cast<SwitchInst>(TI));
315     SmallVector<DominatorTree::UpdateType, 8> Updates;
316 
317     // If the default destination is unfeasible it will never be taken. Replace
318     // it with a new block with a single Unreachable instruction.
319     BasicBlock *DefaultDest = SI->getDefaultDest();
320     if (!FeasibleSuccessors.contains(DefaultDest)) {
321       if (!NewUnreachableBB) {
322         NewUnreachableBB =
323             BasicBlock::Create(DefaultDest->getContext(), "default.unreachable",
324                                DefaultDest->getParent(), DefaultDest);
325         new UnreachableInst(DefaultDest->getContext(), NewUnreachableBB);
326       }
327 
328       DefaultDest->removePredecessor(BB);
329       SI->setDefaultDest(NewUnreachableBB);
330       Updates.push_back({DominatorTree::Delete, BB, DefaultDest});
331       Updates.push_back({DominatorTree::Insert, BB, NewUnreachableBB});
332     }
333 
334     for (auto CI = SI->case_begin(); CI != SI->case_end();) {
335       if (FeasibleSuccessors.contains(CI->getCaseSuccessor())) {
336         ++CI;
337         continue;
338       }
339 
340       BasicBlock *Succ = CI->getCaseSuccessor();
341       Succ->removePredecessor(BB);
342       Updates.push_back({DominatorTree::Delete, BB, Succ});
343       SI.removeCase(CI);
344       // Don't increment CI, as we removed a case.
345     }
346 
347     DTU.applyUpdatesPermissive(Updates);
348   } else {
349     llvm_unreachable("Must have at least one feasible successor");
350   }
351   return true;
352 }
353 
354 static void inferAttribute(Function *F, unsigned AttrIndex,
355                            const ValueLatticeElement &Val) {
356   // If there is a known constant range for the value, add range attribute.
357   if (Val.isConstantRange() && !Val.getConstantRange().isSingleElement()) {
358     // Do not add range attribute if the value may include undef.
359     if (Val.isConstantRangeIncludingUndef())
360       return;
361 
362     // Take the intersection of the existing attribute and the inferred range.
363     Attribute OldAttr = F->getAttributeAtIndex(AttrIndex, Attribute::Range);
364     ConstantRange CR = Val.getConstantRange();
365     if (OldAttr.isValid())
366       CR = CR.intersectWith(OldAttr.getRange());
367     F->addAttributeAtIndex(
368         AttrIndex, Attribute::get(F->getContext(), Attribute::Range, CR));
369     return;
370   }
371   // Infer nonnull attribute.
372   if (Val.isNotConstant() && Val.getNotConstant()->getType()->isPointerTy() &&
373       Val.getNotConstant()->isNullValue() &&
374       !F->hasAttributeAtIndex(AttrIndex, Attribute::NonNull)) {
375     F->addAttributeAtIndex(AttrIndex,
376                            Attribute::get(F->getContext(), Attribute::NonNull));
377   }
378 }
379 
380 void SCCPSolver::inferReturnAttributes() const {
381   for (const auto &[F, ReturnValue] : getTrackedRetVals())
382     inferAttribute(F, AttributeList::ReturnIndex, ReturnValue);
383 }
384 
385 void SCCPSolver::inferArgAttributes() const {
386   for (Function *F : getArgumentTrackedFunctions()) {
387     if (!isBlockExecutable(&F->front()))
388       continue;
389     for (Argument &A : F->args())
390       if (!A.getType()->isStructTy())
391         inferAttribute(F, AttributeList::FirstArgIndex + A.getArgNo(),
392                        getLatticeValueFor(&A));
393   }
394 }
395 
396 /// Helper class for SCCPSolver. This implements the instruction visitor and
397 /// holds all the state.
398 class SCCPInstVisitor : public InstVisitor<SCCPInstVisitor> {
399   const DataLayout &DL;
400   std::function<const TargetLibraryInfo &(Function &)> GetTLI;
401   SmallPtrSet<BasicBlock *, 8> BBExecutable; // The BBs that are executable.
402   DenseMap<Value *, ValueLatticeElement>
403       ValueState; // The state each value is in.
404 
405   /// StructValueState - This maintains ValueState for values that have
406   /// StructType, for example for formal arguments, calls, insertelement, etc.
407   DenseMap<std::pair<Value *, unsigned>, ValueLatticeElement> StructValueState;
408 
409   /// GlobalValue - If we are tracking any values for the contents of a global
410   /// variable, we keep a mapping from the constant accessor to the element of
411   /// the global, to the currently known value.  If the value becomes
412   /// overdefined, it's entry is simply removed from this map.
413   DenseMap<GlobalVariable *, ValueLatticeElement> TrackedGlobals;
414 
415   /// TrackedRetVals - If we are tracking arguments into and the return
416   /// value out of a function, it will have an entry in this map, indicating
417   /// what the known return value for the function is.
418   MapVector<Function *, ValueLatticeElement> TrackedRetVals;
419 
420   /// TrackedMultipleRetVals - Same as TrackedRetVals, but used for functions
421   /// that return multiple values.
422   MapVector<std::pair<Function *, unsigned>, ValueLatticeElement>
423       TrackedMultipleRetVals;
424 
425   /// The set of values whose lattice has been invalidated.
426   /// Populated by resetLatticeValueFor(), cleared after resolving undefs.
427   DenseSet<Value *> Invalidated;
428 
429   /// MRVFunctionsTracked - Each function in TrackedMultipleRetVals is
430   /// represented here for efficient lookup.
431   SmallPtrSet<Function *, 16> MRVFunctionsTracked;
432 
433   /// A list of functions whose return cannot be modified.
434   SmallPtrSet<Function *, 16> MustPreserveReturnsInFunctions;
435 
436   /// TrackingIncomingArguments - This is the set of functions for whose
437   /// arguments we make optimistic assumptions about and try to prove as
438   /// constants.
439   SmallPtrSet<Function *, 16> TrackingIncomingArguments;
440 
441   /// The reason for two worklists is that overdefined is the lowest state
442   /// on the lattice, and moving things to overdefined as fast as possible
443   /// makes SCCP converge much faster.
444   ///
445   /// By having a separate worklist, we accomplish this because everything
446   /// possibly overdefined will become overdefined at the soonest possible
447   /// point.
448   SmallVector<Value *, 64> OverdefinedInstWorkList;
449   SmallVector<Value *, 64> InstWorkList;
450 
451   // The BasicBlock work list
452   SmallVector<BasicBlock *, 64> BBWorkList;
453 
454   /// KnownFeasibleEdges - Entries in this set are edges which have already had
455   /// PHI nodes retriggered.
456   using Edge = std::pair<BasicBlock *, BasicBlock *>;
457   DenseSet<Edge> KnownFeasibleEdges;
458 
459   DenseMap<Function *, std::unique_ptr<PredicateInfo>> FnPredicateInfo;
460 
461   DenseMap<Value *, SmallSetVector<User *, 2>> AdditionalUsers;
462 
463   LLVMContext &Ctx;
464 
465 private:
466   ConstantInt *getConstantInt(const ValueLatticeElement &IV, Type *Ty) const {
467     return dyn_cast_or_null<ConstantInt>(getConstant(IV, Ty));
468   }
469 
470   // pushToWorkList - Helper for markConstant/markOverdefined
471   void pushToWorkList(ValueLatticeElement &IV, Value *V);
472 
473   // Helper to push \p V to the worklist, after updating it to \p IV. Also
474   // prints a debug message with the updated value.
475   void pushToWorkListMsg(ValueLatticeElement &IV, Value *V);
476 
477   // markConstant - Make a value be marked as "constant".  If the value
478   // is not already a constant, add it to the instruction work list so that
479   // the users of the instruction are updated later.
480   bool markConstant(ValueLatticeElement &IV, Value *V, Constant *C,
481                     bool MayIncludeUndef = false);
482 
483   bool markConstant(Value *V, Constant *C) {
484     assert(!V->getType()->isStructTy() && "structs should use mergeInValue");
485     return markConstant(ValueState[V], V, C);
486   }
487 
488   bool markNotConstant(ValueLatticeElement &IV, Value *V, Constant *C);
489 
490   bool markNotNull(ValueLatticeElement &IV, Value *V) {
491     return markNotConstant(IV, V, Constant::getNullValue(V->getType()));
492   }
493 
494   /// markConstantRange - Mark the object as constant range with \p CR. If the
495   /// object is not a constant range with the range \p CR, add it to the
496   /// instruction work list so that the users of the instruction are updated
497   /// later.
498   bool markConstantRange(ValueLatticeElement &IV, Value *V,
499                          const ConstantRange &CR);
500 
501   // markOverdefined - Make a value be marked as "overdefined". If the
502   // value is not already overdefined, add it to the overdefined instruction
503   // work list so that the users of the instruction are updated later.
504   bool markOverdefined(ValueLatticeElement &IV, Value *V);
505 
506   /// Merge \p MergeWithV into \p IV and push \p V to the worklist, if \p IV
507   /// changes.
508   bool mergeInValue(ValueLatticeElement &IV, Value *V,
509                     ValueLatticeElement MergeWithV,
510                     ValueLatticeElement::MergeOptions Opts = {
511                         /*MayIncludeUndef=*/false, /*CheckWiden=*/false});
512 
513   bool mergeInValue(Value *V, ValueLatticeElement MergeWithV,
514                     ValueLatticeElement::MergeOptions Opts = {
515                         /*MayIncludeUndef=*/false, /*CheckWiden=*/false}) {
516     assert(!V->getType()->isStructTy() &&
517            "non-structs should use markConstant");
518     return mergeInValue(ValueState[V], V, MergeWithV, Opts);
519   }
520 
521   /// getValueState - Return the ValueLatticeElement object that corresponds to
522   /// the value.  This function handles the case when the value hasn't been seen
523   /// yet by properly seeding constants etc.
524   ValueLatticeElement &getValueState(Value *V) {
525     assert(!V->getType()->isStructTy() && "Should use getStructValueState");
526 
527     auto I = ValueState.insert(std::make_pair(V, ValueLatticeElement()));
528     ValueLatticeElement &LV = I.first->second;
529 
530     if (!I.second)
531       return LV; // Common case, already in the map.
532 
533     if (auto *C = dyn_cast<Constant>(V))
534       LV.markConstant(C); // Constants are constant
535 
536     // All others are unknown by default.
537     return LV;
538   }
539 
540   /// getStructValueState - Return the ValueLatticeElement object that
541   /// corresponds to the value/field pair.  This function handles the case when
542   /// the value hasn't been seen yet by properly seeding constants etc.
543   ValueLatticeElement &getStructValueState(Value *V, unsigned i) {
544     assert(V->getType()->isStructTy() && "Should use getValueState");
545     assert(i < cast<StructType>(V->getType())->getNumElements() &&
546            "Invalid element #");
547 
548     auto I = StructValueState.insert(
549         std::make_pair(std::make_pair(V, i), ValueLatticeElement()));
550     ValueLatticeElement &LV = I.first->second;
551 
552     if (!I.second)
553       return LV; // Common case, already in the map.
554 
555     if (auto *C = dyn_cast<Constant>(V)) {
556       Constant *Elt = C->getAggregateElement(i);
557 
558       if (!Elt)
559         LV.markOverdefined(); // Unknown sort of constant.
560       else
561         LV.markConstant(Elt); // Constants are constant.
562     }
563 
564     // All others are underdefined by default.
565     return LV;
566   }
567 
568   /// Traverse the use-def chain of \p Call, marking itself and its users as
569   /// "unknown" on the way.
570   void invalidate(CallBase *Call) {
571     SmallVector<Instruction *, 64> ToInvalidate;
572     ToInvalidate.push_back(Call);
573 
574     while (!ToInvalidate.empty()) {
575       Instruction *Inst = ToInvalidate.pop_back_val();
576 
577       if (!Invalidated.insert(Inst).second)
578         continue;
579 
580       if (!BBExecutable.count(Inst->getParent()))
581         continue;
582 
583       Value *V = nullptr;
584       // For return instructions we need to invalidate the tracked returns map.
585       // Anything else has its lattice in the value map.
586       if (auto *RetInst = dyn_cast<ReturnInst>(Inst)) {
587         Function *F = RetInst->getParent()->getParent();
588         if (auto It = TrackedRetVals.find(F); It != TrackedRetVals.end()) {
589           It->second = ValueLatticeElement();
590           V = F;
591         } else if (MRVFunctionsTracked.count(F)) {
592           auto *STy = cast<StructType>(F->getReturnType());
593           for (unsigned I = 0, E = STy->getNumElements(); I != E; ++I)
594             TrackedMultipleRetVals[{F, I}] = ValueLatticeElement();
595           V = F;
596         }
597       } else if (auto *STy = dyn_cast<StructType>(Inst->getType())) {
598         for (unsigned I = 0, E = STy->getNumElements(); I != E; ++I) {
599           if (auto It = StructValueState.find({Inst, I});
600               It != StructValueState.end()) {
601             It->second = ValueLatticeElement();
602             V = Inst;
603           }
604         }
605       } else if (auto It = ValueState.find(Inst); It != ValueState.end()) {
606         It->second = ValueLatticeElement();
607         V = Inst;
608       }
609 
610       if (V) {
611         LLVM_DEBUG(dbgs() << "Invalidated lattice for " << *V << "\n");
612 
613         for (User *U : V->users())
614           if (auto *UI = dyn_cast<Instruction>(U))
615             ToInvalidate.push_back(UI);
616 
617         auto It = AdditionalUsers.find(V);
618         if (It != AdditionalUsers.end())
619           for (User *U : It->second)
620             if (auto *UI = dyn_cast<Instruction>(U))
621               ToInvalidate.push_back(UI);
622       }
623     }
624   }
625 
626   /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
627   /// work list if it is not already executable.
628   bool markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest);
629 
630   // getFeasibleSuccessors - Return a vector of booleans to indicate which
631   // successors are reachable from a given terminator instruction.
632   void getFeasibleSuccessors(Instruction &TI, SmallVectorImpl<bool> &Succs);
633 
634   // OperandChangedState - This method is invoked on all of the users of an
635   // instruction that was just changed state somehow.  Based on this
636   // information, we need to update the specified user of this instruction.
637   void operandChangedState(Instruction *I) {
638     if (BBExecutable.count(I->getParent())) // Inst is executable?
639       visit(*I);
640   }
641 
642   // Add U as additional user of V.
643   void addAdditionalUser(Value *V, User *U) { AdditionalUsers[V].insert(U); }
644 
645   // Mark I's users as changed, including AdditionalUsers.
646   void markUsersAsChanged(Value *I) {
647     // Functions include their arguments in the use-list. Changed function
648     // values mean that the result of the function changed. We only need to
649     // update the call sites with the new function result and do not have to
650     // propagate the call arguments.
651     if (isa<Function>(I)) {
652       for (User *U : I->users()) {
653         if (auto *CB = dyn_cast<CallBase>(U))
654           handleCallResult(*CB);
655       }
656     } else {
657       for (User *U : I->users())
658         if (auto *UI = dyn_cast<Instruction>(U))
659           operandChangedState(UI);
660     }
661 
662     auto Iter = AdditionalUsers.find(I);
663     if (Iter != AdditionalUsers.end()) {
664       // Copy additional users before notifying them of changes, because new
665       // users may be added, potentially invalidating the iterator.
666       SmallVector<Instruction *, 2> ToNotify;
667       for (User *U : Iter->second)
668         if (auto *UI = dyn_cast<Instruction>(U))
669           ToNotify.push_back(UI);
670       for (Instruction *UI : ToNotify)
671         operandChangedState(UI);
672     }
673   }
674   void handleCallOverdefined(CallBase &CB);
675   void handleCallResult(CallBase &CB);
676   void handleCallArguments(CallBase &CB);
677   void handleExtractOfWithOverflow(ExtractValueInst &EVI,
678                                    const WithOverflowInst *WO, unsigned Idx);
679 
680 private:
681   friend class InstVisitor<SCCPInstVisitor>;
682 
683   // visit implementations - Something changed in this instruction.  Either an
684   // operand made a transition, or the instruction is newly executable.  Change
685   // the value type of I to reflect these changes if appropriate.
686   void visitPHINode(PHINode &I);
687 
688   // Terminators
689 
690   void visitReturnInst(ReturnInst &I);
691   void visitTerminator(Instruction &TI);
692 
693   void visitCastInst(CastInst &I);
694   void visitSelectInst(SelectInst &I);
695   void visitUnaryOperator(Instruction &I);
696   void visitFreezeInst(FreezeInst &I);
697   void visitBinaryOperator(Instruction &I);
698   void visitCmpInst(CmpInst &I);
699   void visitExtractValueInst(ExtractValueInst &EVI);
700   void visitInsertValueInst(InsertValueInst &IVI);
701 
702   void visitCatchSwitchInst(CatchSwitchInst &CPI) {
703     markOverdefined(&CPI);
704     visitTerminator(CPI);
705   }
706 
707   // Instructions that cannot be folded away.
708 
709   void visitStoreInst(StoreInst &I);
710   void visitLoadInst(LoadInst &I);
711   void visitGetElementPtrInst(GetElementPtrInst &I);
712   void visitAllocaInst(AllocaInst &AI);
713 
714   void visitInvokeInst(InvokeInst &II) {
715     visitCallBase(II);
716     visitTerminator(II);
717   }
718 
719   void visitCallBrInst(CallBrInst &CBI) {
720     visitCallBase(CBI);
721     visitTerminator(CBI);
722   }
723 
724   void visitCallBase(CallBase &CB);
725   void visitResumeInst(ResumeInst &I) { /*returns void*/
726   }
727   void visitUnreachableInst(UnreachableInst &I) { /*returns void*/
728   }
729   void visitFenceInst(FenceInst &I) { /*returns void*/
730   }
731 
732   void visitInstruction(Instruction &I);
733 
734 public:
735   void addPredicateInfo(Function &F, DominatorTree &DT, AssumptionCache &AC) {
736     FnPredicateInfo.insert({&F, std::make_unique<PredicateInfo>(F, DT, AC)});
737   }
738 
739   void visitCallInst(CallInst &I) { visitCallBase(I); }
740 
741   bool markBlockExecutable(BasicBlock *BB);
742 
743   const PredicateBase *getPredicateInfoFor(Instruction *I) {
744     auto It = FnPredicateInfo.find(I->getParent()->getParent());
745     if (It == FnPredicateInfo.end())
746       return nullptr;
747     return It->second->getPredicateInfoFor(I);
748   }
749 
750   SCCPInstVisitor(const DataLayout &DL,
751                   std::function<const TargetLibraryInfo &(Function &)> GetTLI,
752                   LLVMContext &Ctx)
753       : DL(DL), GetTLI(GetTLI), Ctx(Ctx) {}
754 
755   void trackValueOfGlobalVariable(GlobalVariable *GV) {
756     // We only track the contents of scalar globals.
757     if (GV->getValueType()->isSingleValueType()) {
758       ValueLatticeElement &IV = TrackedGlobals[GV];
759       IV.markConstant(GV->getInitializer());
760     }
761   }
762 
763   void addTrackedFunction(Function *F) {
764     // Add an entry, F -> undef.
765     if (auto *STy = dyn_cast<StructType>(F->getReturnType())) {
766       MRVFunctionsTracked.insert(F);
767       for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
768         TrackedMultipleRetVals.insert(
769             std::make_pair(std::make_pair(F, i), ValueLatticeElement()));
770     } else if (!F->getReturnType()->isVoidTy())
771       TrackedRetVals.insert(std::make_pair(F, ValueLatticeElement()));
772   }
773 
774   void addToMustPreserveReturnsInFunctions(Function *F) {
775     MustPreserveReturnsInFunctions.insert(F);
776   }
777 
778   bool mustPreserveReturn(Function *F) {
779     return MustPreserveReturnsInFunctions.count(F);
780   }
781 
782   void addArgumentTrackedFunction(Function *F) {
783     TrackingIncomingArguments.insert(F);
784   }
785 
786   bool isArgumentTrackedFunction(Function *F) {
787     return TrackingIncomingArguments.count(F);
788   }
789 
790   const SmallPtrSetImpl<Function *> &getArgumentTrackedFunctions() const {
791     return TrackingIncomingArguments;
792   }
793 
794   void solve();
795 
796   bool resolvedUndef(Instruction &I);
797 
798   bool resolvedUndefsIn(Function &F);
799 
800   bool isBlockExecutable(BasicBlock *BB) const {
801     return BBExecutable.count(BB);
802   }
803 
804   bool isEdgeFeasible(BasicBlock *From, BasicBlock *To) const;
805 
806   std::vector<ValueLatticeElement> getStructLatticeValueFor(Value *V) const {
807     std::vector<ValueLatticeElement> StructValues;
808     auto *STy = dyn_cast<StructType>(V->getType());
809     assert(STy && "getStructLatticeValueFor() can be called only on structs");
810     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
811       auto I = StructValueState.find(std::make_pair(V, i));
812       assert(I != StructValueState.end() && "Value not in valuemap!");
813       StructValues.push_back(I->second);
814     }
815     return StructValues;
816   }
817 
818   void removeLatticeValueFor(Value *V) { ValueState.erase(V); }
819 
820   /// Invalidate the Lattice Value of \p Call and its users after specializing
821   /// the call. Then recompute it.
822   void resetLatticeValueFor(CallBase *Call) {
823     // Calls to void returning functions do not need invalidation.
824     Function *F = Call->getCalledFunction();
825     (void)F;
826     assert(!F->getReturnType()->isVoidTy() &&
827            (TrackedRetVals.count(F) || MRVFunctionsTracked.count(F)) &&
828            "All non void specializations should be tracked");
829     invalidate(Call);
830     handleCallResult(*Call);
831   }
832 
833   const ValueLatticeElement &getLatticeValueFor(Value *V) const {
834     assert(!V->getType()->isStructTy() &&
835            "Should use getStructLatticeValueFor");
836     DenseMap<Value *, ValueLatticeElement>::const_iterator I =
837         ValueState.find(V);
838     assert(I != ValueState.end() &&
839            "V not found in ValueState nor Paramstate map!");
840     return I->second;
841   }
842 
843   const MapVector<Function *, ValueLatticeElement> &getTrackedRetVals() {
844     return TrackedRetVals;
845   }
846 
847   const DenseMap<GlobalVariable *, ValueLatticeElement> &getTrackedGlobals() {
848     return TrackedGlobals;
849   }
850 
851   const SmallPtrSet<Function *, 16> getMRVFunctionsTracked() {
852     return MRVFunctionsTracked;
853   }
854 
855   void markOverdefined(Value *V) {
856     if (auto *STy = dyn_cast<StructType>(V->getType()))
857       for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
858         markOverdefined(getStructValueState(V, i), V);
859     else
860       markOverdefined(ValueState[V], V);
861   }
862 
863   ValueLatticeElement getArgAttributeVL(Argument *A) {
864     if (A->getType()->isIntOrIntVectorTy()) {
865       if (std::optional<ConstantRange> Range = A->getRange())
866         return ValueLatticeElement::getRange(*Range);
867     }
868     if (A->hasNonNullAttr())
869       return ValueLatticeElement::getNot(Constant::getNullValue(A->getType()));
870     // Assume nothing about the incoming arguments without attributes.
871     return ValueLatticeElement::getOverdefined();
872   }
873 
874   void trackValueOfArgument(Argument *A) {
875     if (A->getType()->isStructTy())
876       return (void)markOverdefined(A);
877     mergeInValue(A, getArgAttributeVL(A));
878   }
879 
880   bool isStructLatticeConstant(Function *F, StructType *STy);
881 
882   Constant *getConstant(const ValueLatticeElement &LV, Type *Ty) const;
883 
884   Constant *getConstantOrNull(Value *V) const;
885 
886   void setLatticeValueForSpecializationArguments(Function *F,
887                                        const SmallVectorImpl<ArgInfo> &Args);
888 
889   void markFunctionUnreachable(Function *F) {
890     for (auto &BB : *F)
891       BBExecutable.erase(&BB);
892   }
893 
894   void solveWhileResolvedUndefsIn(Module &M) {
895     bool ResolvedUndefs = true;
896     while (ResolvedUndefs) {
897       solve();
898       ResolvedUndefs = false;
899       for (Function &F : M)
900         ResolvedUndefs |= resolvedUndefsIn(F);
901     }
902   }
903 
904   void solveWhileResolvedUndefsIn(SmallVectorImpl<Function *> &WorkList) {
905     bool ResolvedUndefs = true;
906     while (ResolvedUndefs) {
907       solve();
908       ResolvedUndefs = false;
909       for (Function *F : WorkList)
910         ResolvedUndefs |= resolvedUndefsIn(*F);
911     }
912   }
913 
914   void solveWhileResolvedUndefs() {
915     bool ResolvedUndefs = true;
916     while (ResolvedUndefs) {
917       solve();
918       ResolvedUndefs = false;
919       for (Value *V : Invalidated)
920         if (auto *I = dyn_cast<Instruction>(V))
921           ResolvedUndefs |= resolvedUndef(*I);
922     }
923     Invalidated.clear();
924   }
925 };
926 
927 } // namespace llvm
928 
929 bool SCCPInstVisitor::markBlockExecutable(BasicBlock *BB) {
930   if (!BBExecutable.insert(BB).second)
931     return false;
932   LLVM_DEBUG(dbgs() << "Marking Block Executable: " << BB->getName() << '\n');
933   BBWorkList.push_back(BB); // Add the block to the work list!
934   return true;
935 }
936 
937 void SCCPInstVisitor::pushToWorkList(ValueLatticeElement &IV, Value *V) {
938   if (IV.isOverdefined()) {
939     if (OverdefinedInstWorkList.empty() || OverdefinedInstWorkList.back() != V)
940       OverdefinedInstWorkList.push_back(V);
941     return;
942   }
943   if (InstWorkList.empty() || InstWorkList.back() != V)
944     InstWorkList.push_back(V);
945 }
946 
947 void SCCPInstVisitor::pushToWorkListMsg(ValueLatticeElement &IV, Value *V) {
948   LLVM_DEBUG(dbgs() << "updated " << IV << ": " << *V << '\n');
949   pushToWorkList(IV, V);
950 }
951 
952 bool SCCPInstVisitor::markConstant(ValueLatticeElement &IV, Value *V,
953                                    Constant *C, bool MayIncludeUndef) {
954   if (!IV.markConstant(C, MayIncludeUndef))
955     return false;
956   LLVM_DEBUG(dbgs() << "markConstant: " << *C << ": " << *V << '\n');
957   pushToWorkList(IV, V);
958   return true;
959 }
960 
961 bool SCCPInstVisitor::markNotConstant(ValueLatticeElement &IV, Value *V,
962                                       Constant *C) {
963   if (!IV.markNotConstant(C))
964     return false;
965   LLVM_DEBUG(dbgs() << "markNotConstant: " << *C << ": " << *V << '\n');
966   pushToWorkList(IV, V);
967   return true;
968 }
969 
970 bool SCCPInstVisitor::markConstantRange(ValueLatticeElement &IV, Value *V,
971                                         const ConstantRange &CR) {
972   if (!IV.markConstantRange(CR))
973     return false;
974   LLVM_DEBUG(dbgs() << "markConstantRange: " << CR << ": " << *V << '\n');
975   pushToWorkList(IV, V);
976   return true;
977 }
978 
979 bool SCCPInstVisitor::markOverdefined(ValueLatticeElement &IV, Value *V) {
980   if (!IV.markOverdefined())
981     return false;
982 
983   LLVM_DEBUG(dbgs() << "markOverdefined: ";
984              if (auto *F = dyn_cast<Function>(V)) dbgs()
985              << "Function '" << F->getName() << "'\n";
986              else dbgs() << *V << '\n');
987   // Only instructions go on the work list
988   pushToWorkList(IV, V);
989   return true;
990 }
991 
992 bool SCCPInstVisitor::isStructLatticeConstant(Function *F, StructType *STy) {
993   for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
994     const auto &It = TrackedMultipleRetVals.find(std::make_pair(F, i));
995     assert(It != TrackedMultipleRetVals.end());
996     ValueLatticeElement LV = It->second;
997     if (!SCCPSolver::isConstant(LV))
998       return false;
999   }
1000   return true;
1001 }
1002 
1003 Constant *SCCPInstVisitor::getConstant(const ValueLatticeElement &LV,
1004                                        Type *Ty) const {
1005   if (LV.isConstant()) {
1006     Constant *C = LV.getConstant();
1007     assert(C->getType() == Ty && "Type mismatch");
1008     return C;
1009   }
1010 
1011   if (LV.isConstantRange()) {
1012     const auto &CR = LV.getConstantRange();
1013     if (CR.getSingleElement())
1014       return ConstantInt::get(Ty, *CR.getSingleElement());
1015   }
1016   return nullptr;
1017 }
1018 
1019 Constant *SCCPInstVisitor::getConstantOrNull(Value *V) const {
1020   Constant *Const = nullptr;
1021   if (V->getType()->isStructTy()) {
1022     std::vector<ValueLatticeElement> LVs = getStructLatticeValueFor(V);
1023     if (any_of(LVs, SCCPSolver::isOverdefined))
1024       return nullptr;
1025     std::vector<Constant *> ConstVals;
1026     auto *ST = cast<StructType>(V->getType());
1027     for (unsigned I = 0, E = ST->getNumElements(); I != E; ++I) {
1028       ValueLatticeElement LV = LVs[I];
1029       ConstVals.push_back(SCCPSolver::isConstant(LV)
1030                               ? getConstant(LV, ST->getElementType(I))
1031                               : UndefValue::get(ST->getElementType(I)));
1032     }
1033     Const = ConstantStruct::get(ST, ConstVals);
1034   } else {
1035     const ValueLatticeElement &LV = getLatticeValueFor(V);
1036     if (SCCPSolver::isOverdefined(LV))
1037       return nullptr;
1038     Const = SCCPSolver::isConstant(LV) ? getConstant(LV, V->getType())
1039                                        : UndefValue::get(V->getType());
1040   }
1041   assert(Const && "Constant is nullptr here!");
1042   return Const;
1043 }
1044 
1045 void SCCPInstVisitor::setLatticeValueForSpecializationArguments(Function *F,
1046                                         const SmallVectorImpl<ArgInfo> &Args) {
1047   assert(!Args.empty() && "Specialization without arguments");
1048   assert(F->arg_size() == Args[0].Formal->getParent()->arg_size() &&
1049          "Functions should have the same number of arguments");
1050 
1051   auto Iter = Args.begin();
1052   Function::arg_iterator NewArg = F->arg_begin();
1053   Function::arg_iterator OldArg = Args[0].Formal->getParent()->arg_begin();
1054   for (auto End = F->arg_end(); NewArg != End; ++NewArg, ++OldArg) {
1055 
1056     LLVM_DEBUG(dbgs() << "SCCP: Marking argument "
1057                       << NewArg->getNameOrAsOperand() << "\n");
1058 
1059     // Mark the argument constants in the new function
1060     // or copy the lattice state over from the old function.
1061     if (Iter != Args.end() && Iter->Formal == &*OldArg) {
1062       if (auto *STy = dyn_cast<StructType>(NewArg->getType())) {
1063         for (unsigned I = 0, E = STy->getNumElements(); I != E; ++I) {
1064           ValueLatticeElement &NewValue = StructValueState[{&*NewArg, I}];
1065           NewValue.markConstant(Iter->Actual->getAggregateElement(I));
1066         }
1067       } else {
1068         ValueState[&*NewArg].markConstant(Iter->Actual);
1069       }
1070       ++Iter;
1071     } else {
1072       if (auto *STy = dyn_cast<StructType>(NewArg->getType())) {
1073         for (unsigned I = 0, E = STy->getNumElements(); I != E; ++I) {
1074           ValueLatticeElement &NewValue = StructValueState[{&*NewArg, I}];
1075           NewValue = StructValueState[{&*OldArg, I}];
1076         }
1077       } else {
1078         ValueLatticeElement &NewValue = ValueState[&*NewArg];
1079         NewValue = ValueState[&*OldArg];
1080       }
1081     }
1082   }
1083 }
1084 
1085 void SCCPInstVisitor::visitInstruction(Instruction &I) {
1086   // All the instructions we don't do any special handling for just
1087   // go to overdefined.
1088   LLVM_DEBUG(dbgs() << "SCCP: Don't know how to handle: " << I << '\n');
1089   markOverdefined(&I);
1090 }
1091 
1092 bool SCCPInstVisitor::mergeInValue(ValueLatticeElement &IV, Value *V,
1093                                    ValueLatticeElement MergeWithV,
1094                                    ValueLatticeElement::MergeOptions Opts) {
1095   if (IV.mergeIn(MergeWithV, Opts)) {
1096     pushToWorkList(IV, V);
1097     LLVM_DEBUG(dbgs() << "Merged " << MergeWithV << " into " << *V << " : "
1098                       << IV << "\n");
1099     return true;
1100   }
1101   return false;
1102 }
1103 
1104 bool SCCPInstVisitor::markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
1105   if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
1106     return false; // This edge is already known to be executable!
1107 
1108   if (!markBlockExecutable(Dest)) {
1109     // If the destination is already executable, we just made an *edge*
1110     // feasible that wasn't before.  Revisit the PHI nodes in the block
1111     // because they have potentially new operands.
1112     LLVM_DEBUG(dbgs() << "Marking Edge Executable: " << Source->getName()
1113                       << " -> " << Dest->getName() << '\n');
1114 
1115     for (PHINode &PN : Dest->phis())
1116       visitPHINode(PN);
1117   }
1118   return true;
1119 }
1120 
1121 // getFeasibleSuccessors - Return a vector of booleans to indicate which
1122 // successors are reachable from a given terminator instruction.
1123 void SCCPInstVisitor::getFeasibleSuccessors(Instruction &TI,
1124                                             SmallVectorImpl<bool> &Succs) {
1125   Succs.resize(TI.getNumSuccessors());
1126   if (auto *BI = dyn_cast<BranchInst>(&TI)) {
1127     if (BI->isUnconditional()) {
1128       Succs[0] = true;
1129       return;
1130     }
1131 
1132     ValueLatticeElement BCValue = getValueState(BI->getCondition());
1133     ConstantInt *CI = getConstantInt(BCValue, BI->getCondition()->getType());
1134     if (!CI) {
1135       // Overdefined condition variables, and branches on unfoldable constant
1136       // conditions, mean the branch could go either way.
1137       if (!BCValue.isUnknownOrUndef())
1138         Succs[0] = Succs[1] = true;
1139       return;
1140     }
1141 
1142     // Constant condition variables mean the branch can only go a single way.
1143     Succs[CI->isZero()] = true;
1144     return;
1145   }
1146 
1147   // We cannot analyze special terminators, so consider all successors
1148   // executable.
1149   if (TI.isSpecialTerminator()) {
1150     Succs.assign(TI.getNumSuccessors(), true);
1151     return;
1152   }
1153 
1154   if (auto *SI = dyn_cast<SwitchInst>(&TI)) {
1155     if (!SI->getNumCases()) {
1156       Succs[0] = true;
1157       return;
1158     }
1159     const ValueLatticeElement &SCValue = getValueState(SI->getCondition());
1160     if (ConstantInt *CI =
1161             getConstantInt(SCValue, SI->getCondition()->getType())) {
1162       Succs[SI->findCaseValue(CI)->getSuccessorIndex()] = true;
1163       return;
1164     }
1165 
1166     // TODO: Switch on undef is UB. Stop passing false once the rest of LLVM
1167     // is ready.
1168     if (SCValue.isConstantRange(/*UndefAllowed=*/false)) {
1169       const ConstantRange &Range = SCValue.getConstantRange();
1170       unsigned ReachableCaseCount = 0;
1171       for (const auto &Case : SI->cases()) {
1172         const APInt &CaseValue = Case.getCaseValue()->getValue();
1173         if (Range.contains(CaseValue)) {
1174           Succs[Case.getSuccessorIndex()] = true;
1175           ++ReachableCaseCount;
1176         }
1177       }
1178 
1179       Succs[SI->case_default()->getSuccessorIndex()] =
1180           Range.isSizeLargerThan(ReachableCaseCount);
1181       return;
1182     }
1183 
1184     // Overdefined or unknown condition? All destinations are executable!
1185     if (!SCValue.isUnknownOrUndef())
1186       Succs.assign(TI.getNumSuccessors(), true);
1187     return;
1188   }
1189 
1190   // In case of indirect branch and its address is a blockaddress, we mark
1191   // the target as executable.
1192   if (auto *IBR = dyn_cast<IndirectBrInst>(&TI)) {
1193     // Casts are folded by visitCastInst.
1194     ValueLatticeElement IBRValue = getValueState(IBR->getAddress());
1195     BlockAddress *Addr = dyn_cast_or_null<BlockAddress>(
1196         getConstant(IBRValue, IBR->getAddress()->getType()));
1197     if (!Addr) { // Overdefined or unknown condition?
1198       // All destinations are executable!
1199       if (!IBRValue.isUnknownOrUndef())
1200         Succs.assign(TI.getNumSuccessors(), true);
1201       return;
1202     }
1203 
1204     BasicBlock *T = Addr->getBasicBlock();
1205     assert(Addr->getFunction() == T->getParent() &&
1206            "Block address of a different function ?");
1207     for (unsigned i = 0; i < IBR->getNumSuccessors(); ++i) {
1208       // This is the target.
1209       if (IBR->getDestination(i) == T) {
1210         Succs[i] = true;
1211         return;
1212       }
1213     }
1214 
1215     // If we didn't find our destination in the IBR successor list, then we
1216     // have undefined behavior. Its ok to assume no successor is executable.
1217     return;
1218   }
1219 
1220   LLVM_DEBUG(dbgs() << "Unknown terminator instruction: " << TI << '\n');
1221   llvm_unreachable("SCCP: Don't know how to handle this terminator!");
1222 }
1223 
1224 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
1225 // block to the 'To' basic block is currently feasible.
1226 bool SCCPInstVisitor::isEdgeFeasible(BasicBlock *From, BasicBlock *To) const {
1227   // Check if we've called markEdgeExecutable on the edge yet. (We could
1228   // be more aggressive and try to consider edges which haven't been marked
1229   // yet, but there isn't any need.)
1230   return KnownFeasibleEdges.count(Edge(From, To));
1231 }
1232 
1233 // visit Implementations - Something changed in this instruction, either an
1234 // operand made a transition, or the instruction is newly executable.  Change
1235 // the value type of I to reflect these changes if appropriate.  This method
1236 // makes sure to do the following actions:
1237 //
1238 // 1. If a phi node merges two constants in, and has conflicting value coming
1239 //    from different branches, or if the PHI node merges in an overdefined
1240 //    value, then the PHI node becomes overdefined.
1241 // 2. If a phi node merges only constants in, and they all agree on value, the
1242 //    PHI node becomes a constant value equal to that.
1243 // 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
1244 // 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
1245 // 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
1246 // 6. If a conditional branch has a value that is constant, make the selected
1247 //    destination executable
1248 // 7. If a conditional branch has a value that is overdefined, make all
1249 //    successors executable.
1250 void SCCPInstVisitor::visitPHINode(PHINode &PN) {
1251   // If this PN returns a struct, just mark the result overdefined.
1252   // TODO: We could do a lot better than this if code actually uses this.
1253   if (PN.getType()->isStructTy())
1254     return (void)markOverdefined(&PN);
1255 
1256   if (getValueState(&PN).isOverdefined())
1257     return; // Quick exit
1258 
1259   // Super-extra-high-degree PHI nodes are unlikely to ever be marked constant,
1260   // and slow us down a lot.  Just mark them overdefined.
1261   if (PN.getNumIncomingValues() > 64)
1262     return (void)markOverdefined(&PN);
1263 
1264   unsigned NumActiveIncoming = 0;
1265 
1266   // Look at all of the executable operands of the PHI node.  If any of them
1267   // are overdefined, the PHI becomes overdefined as well.  If they are all
1268   // constant, and they agree with each other, the PHI becomes the identical
1269   // constant.  If they are constant and don't agree, the PHI is a constant
1270   // range. If there are no executable operands, the PHI remains unknown.
1271   ValueLatticeElement PhiState = getValueState(&PN);
1272   for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
1273     if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent()))
1274       continue;
1275 
1276     ValueLatticeElement IV = getValueState(PN.getIncomingValue(i));
1277     PhiState.mergeIn(IV);
1278     NumActiveIncoming++;
1279     if (PhiState.isOverdefined())
1280       break;
1281   }
1282 
1283   // We allow up to 1 range extension per active incoming value and one
1284   // additional extension. Note that we manually adjust the number of range
1285   // extensions to match the number of active incoming values. This helps to
1286   // limit multiple extensions caused by the same incoming value, if other
1287   // incoming values are equal.
1288   mergeInValue(&PN, PhiState,
1289                ValueLatticeElement::MergeOptions().setMaxWidenSteps(
1290                    NumActiveIncoming + 1));
1291   ValueLatticeElement &PhiStateRef = getValueState(&PN);
1292   PhiStateRef.setNumRangeExtensions(
1293       std::max(NumActiveIncoming, PhiStateRef.getNumRangeExtensions()));
1294 }
1295 
1296 void SCCPInstVisitor::visitReturnInst(ReturnInst &I) {
1297   if (I.getNumOperands() == 0)
1298     return; // ret void
1299 
1300   Function *F = I.getParent()->getParent();
1301   Value *ResultOp = I.getOperand(0);
1302 
1303   // If we are tracking the return value of this function, merge it in.
1304   if (!TrackedRetVals.empty() && !ResultOp->getType()->isStructTy()) {
1305     auto TFRVI = TrackedRetVals.find(F);
1306     if (TFRVI != TrackedRetVals.end()) {
1307       mergeInValue(TFRVI->second, F, getValueState(ResultOp));
1308       return;
1309     }
1310   }
1311 
1312   // Handle functions that return multiple values.
1313   if (!TrackedMultipleRetVals.empty()) {
1314     if (auto *STy = dyn_cast<StructType>(ResultOp->getType()))
1315       if (MRVFunctionsTracked.count(F))
1316         for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
1317           mergeInValue(TrackedMultipleRetVals[std::make_pair(F, i)], F,
1318                        getStructValueState(ResultOp, i));
1319   }
1320 }
1321 
1322 void SCCPInstVisitor::visitTerminator(Instruction &TI) {
1323   SmallVector<bool, 16> SuccFeasible;
1324   getFeasibleSuccessors(TI, SuccFeasible);
1325 
1326   BasicBlock *BB = TI.getParent();
1327 
1328   // Mark all feasible successors executable.
1329   for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
1330     if (SuccFeasible[i])
1331       markEdgeExecutable(BB, TI.getSuccessor(i));
1332 }
1333 
1334 void SCCPInstVisitor::visitCastInst(CastInst &I) {
1335   // ResolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1336   // discover a concrete value later.
1337   if (ValueState[&I].isOverdefined())
1338     return;
1339 
1340   ValueLatticeElement OpSt = getValueState(I.getOperand(0));
1341   if (OpSt.isUnknownOrUndef())
1342     return;
1343 
1344   if (Constant *OpC = getConstant(OpSt, I.getOperand(0)->getType())) {
1345     // Fold the constant as we build.
1346     if (Constant *C =
1347             ConstantFoldCastOperand(I.getOpcode(), OpC, I.getType(), DL))
1348       return (void)markConstant(&I, C);
1349   }
1350 
1351   // Ignore bitcasts, as they may change the number of vector elements.
1352   if (I.getDestTy()->isIntOrIntVectorTy() &&
1353       I.getSrcTy()->isIntOrIntVectorTy() &&
1354       I.getOpcode() != Instruction::BitCast) {
1355     auto &LV = getValueState(&I);
1356     ConstantRange OpRange =
1357         OpSt.asConstantRange(I.getSrcTy(), /*UndefAllowed=*/false);
1358 
1359     Type *DestTy = I.getDestTy();
1360     ConstantRange Res =
1361         OpRange.castOp(I.getOpcode(), DestTy->getScalarSizeInBits());
1362     mergeInValue(LV, &I, ValueLatticeElement::getRange(Res));
1363   } else
1364     markOverdefined(&I);
1365 }
1366 
1367 void SCCPInstVisitor::handleExtractOfWithOverflow(ExtractValueInst &EVI,
1368                                                   const WithOverflowInst *WO,
1369                                                   unsigned Idx) {
1370   Value *LHS = WO->getLHS(), *RHS = WO->getRHS();
1371   ValueLatticeElement L = getValueState(LHS);
1372   ValueLatticeElement R = getValueState(RHS);
1373   addAdditionalUser(LHS, &EVI);
1374   addAdditionalUser(RHS, &EVI);
1375   if (L.isUnknownOrUndef() || R.isUnknownOrUndef())
1376     return; // Wait to resolve.
1377 
1378   Type *Ty = LHS->getType();
1379   ConstantRange LR = L.asConstantRange(Ty, /*UndefAllowed=*/false);
1380   ConstantRange RR = R.asConstantRange(Ty, /*UndefAllowed=*/false);
1381   if (Idx == 0) {
1382     ConstantRange Res = LR.binaryOp(WO->getBinaryOp(), RR);
1383     mergeInValue(&EVI, ValueLatticeElement::getRange(Res));
1384   } else {
1385     assert(Idx == 1 && "Index can only be 0 or 1");
1386     ConstantRange NWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
1387         WO->getBinaryOp(), RR, WO->getNoWrapKind());
1388     if (NWRegion.contains(LR))
1389       return (void)markConstant(&EVI, ConstantInt::getFalse(EVI.getType()));
1390     markOverdefined(&EVI);
1391   }
1392 }
1393 
1394 void SCCPInstVisitor::visitExtractValueInst(ExtractValueInst &EVI) {
1395   // If this returns a struct, mark all elements over defined, we don't track
1396   // structs in structs.
1397   if (EVI.getType()->isStructTy())
1398     return (void)markOverdefined(&EVI);
1399 
1400   // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1401   // discover a concrete value later.
1402   if (ValueState[&EVI].isOverdefined())
1403     return (void)markOverdefined(&EVI);
1404 
1405   // If this is extracting from more than one level of struct, we don't know.
1406   if (EVI.getNumIndices() != 1)
1407     return (void)markOverdefined(&EVI);
1408 
1409   Value *AggVal = EVI.getAggregateOperand();
1410   if (AggVal->getType()->isStructTy()) {
1411     unsigned i = *EVI.idx_begin();
1412     if (auto *WO = dyn_cast<WithOverflowInst>(AggVal))
1413       return handleExtractOfWithOverflow(EVI, WO, i);
1414     ValueLatticeElement EltVal = getStructValueState(AggVal, i);
1415     mergeInValue(getValueState(&EVI), &EVI, EltVal);
1416   } else {
1417     // Otherwise, must be extracting from an array.
1418     return (void)markOverdefined(&EVI);
1419   }
1420 }
1421 
1422 void SCCPInstVisitor::visitInsertValueInst(InsertValueInst &IVI) {
1423   auto *STy = dyn_cast<StructType>(IVI.getType());
1424   if (!STy)
1425     return (void)markOverdefined(&IVI);
1426 
1427   // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1428   // discover a concrete value later.
1429   if (ValueState[&IVI].isOverdefined())
1430     return (void)markOverdefined(&IVI);
1431 
1432   // If this has more than one index, we can't handle it, drive all results to
1433   // undef.
1434   if (IVI.getNumIndices() != 1)
1435     return (void)markOverdefined(&IVI);
1436 
1437   Value *Aggr = IVI.getAggregateOperand();
1438   unsigned Idx = *IVI.idx_begin();
1439 
1440   // Compute the result based on what we're inserting.
1441   for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1442     // This passes through all values that aren't the inserted element.
1443     if (i != Idx) {
1444       ValueLatticeElement EltVal = getStructValueState(Aggr, i);
1445       mergeInValue(getStructValueState(&IVI, i), &IVI, EltVal);
1446       continue;
1447     }
1448 
1449     Value *Val = IVI.getInsertedValueOperand();
1450     if (Val->getType()->isStructTy())
1451       // We don't track structs in structs.
1452       markOverdefined(getStructValueState(&IVI, i), &IVI);
1453     else {
1454       ValueLatticeElement InVal = getValueState(Val);
1455       mergeInValue(getStructValueState(&IVI, i), &IVI, InVal);
1456     }
1457   }
1458 }
1459 
1460 void SCCPInstVisitor::visitSelectInst(SelectInst &I) {
1461   // If this select returns a struct, just mark the result overdefined.
1462   // TODO: We could do a lot better than this if code actually uses this.
1463   if (I.getType()->isStructTy())
1464     return (void)markOverdefined(&I);
1465 
1466   // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1467   // discover a concrete value later.
1468   if (ValueState[&I].isOverdefined())
1469     return (void)markOverdefined(&I);
1470 
1471   ValueLatticeElement CondValue = getValueState(I.getCondition());
1472   if (CondValue.isUnknownOrUndef())
1473     return;
1474 
1475   if (ConstantInt *CondCB =
1476           getConstantInt(CondValue, I.getCondition()->getType())) {
1477     Value *OpVal = CondCB->isZero() ? I.getFalseValue() : I.getTrueValue();
1478     mergeInValue(&I, getValueState(OpVal));
1479     return;
1480   }
1481 
1482   // Otherwise, the condition is overdefined or a constant we can't evaluate.
1483   // See if we can produce something better than overdefined based on the T/F
1484   // value.
1485   ValueLatticeElement TVal = getValueState(I.getTrueValue());
1486   ValueLatticeElement FVal = getValueState(I.getFalseValue());
1487 
1488   bool Changed = ValueState[&I].mergeIn(TVal);
1489   Changed |= ValueState[&I].mergeIn(FVal);
1490   if (Changed)
1491     pushToWorkListMsg(ValueState[&I], &I);
1492 }
1493 
1494 // Handle Unary Operators.
1495 void SCCPInstVisitor::visitUnaryOperator(Instruction &I) {
1496   ValueLatticeElement V0State = getValueState(I.getOperand(0));
1497 
1498   ValueLatticeElement &IV = ValueState[&I];
1499   // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1500   // discover a concrete value later.
1501   if (IV.isOverdefined())
1502     return (void)markOverdefined(&I);
1503 
1504   // If something is unknown/undef, wait for it to resolve.
1505   if (V0State.isUnknownOrUndef())
1506     return;
1507 
1508   if (SCCPSolver::isConstant(V0State))
1509     if (Constant *C = ConstantFoldUnaryOpOperand(
1510             I.getOpcode(), getConstant(V0State, I.getType()), DL))
1511       return (void)markConstant(IV, &I, C);
1512 
1513   markOverdefined(&I);
1514 }
1515 
1516 void SCCPInstVisitor::visitFreezeInst(FreezeInst &I) {
1517   // If this freeze returns a struct, just mark the result overdefined.
1518   // TODO: We could do a lot better than this.
1519   if (I.getType()->isStructTy())
1520     return (void)markOverdefined(&I);
1521 
1522   ValueLatticeElement V0State = getValueState(I.getOperand(0));
1523   ValueLatticeElement &IV = ValueState[&I];
1524   // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1525   // discover a concrete value later.
1526   if (IV.isOverdefined())
1527     return (void)markOverdefined(&I);
1528 
1529   // If something is unknown/undef, wait for it to resolve.
1530   if (V0State.isUnknownOrUndef())
1531     return;
1532 
1533   if (SCCPSolver::isConstant(V0State) &&
1534       isGuaranteedNotToBeUndefOrPoison(getConstant(V0State, I.getType())))
1535     return (void)markConstant(IV, &I, getConstant(V0State, I.getType()));
1536 
1537   markOverdefined(&I);
1538 }
1539 
1540 // Handle Binary Operators.
1541 void SCCPInstVisitor::visitBinaryOperator(Instruction &I) {
1542   ValueLatticeElement V1State = getValueState(I.getOperand(0));
1543   ValueLatticeElement V2State = getValueState(I.getOperand(1));
1544 
1545   ValueLatticeElement &IV = ValueState[&I];
1546   if (IV.isOverdefined())
1547     return;
1548 
1549   // If something is undef, wait for it to resolve.
1550   if (V1State.isUnknownOrUndef() || V2State.isUnknownOrUndef())
1551     return;
1552 
1553   if (V1State.isOverdefined() && V2State.isOverdefined())
1554     return (void)markOverdefined(&I);
1555 
1556   // If either of the operands is a constant, try to fold it to a constant.
1557   // TODO: Use information from notconstant better.
1558   if ((V1State.isConstant() || V2State.isConstant())) {
1559     Value *V1 = SCCPSolver::isConstant(V1State)
1560                     ? getConstant(V1State, I.getOperand(0)->getType())
1561                     : I.getOperand(0);
1562     Value *V2 = SCCPSolver::isConstant(V2State)
1563                     ? getConstant(V2State, I.getOperand(1)->getType())
1564                     : I.getOperand(1);
1565     Value *R = simplifyBinOp(I.getOpcode(), V1, V2, SimplifyQuery(DL, &I));
1566     auto *C = dyn_cast_or_null<Constant>(R);
1567     if (C) {
1568       // Conservatively assume that the result may be based on operands that may
1569       // be undef. Note that we use mergeInValue to combine the constant with
1570       // the existing lattice value for I, as different constants might be found
1571       // after one of the operands go to overdefined, e.g. due to one operand
1572       // being a special floating value.
1573       ValueLatticeElement NewV;
1574       NewV.markConstant(C, /*MayIncludeUndef=*/true);
1575       return (void)mergeInValue(&I, NewV);
1576     }
1577   }
1578 
1579   // Only use ranges for binary operators on integers.
1580   if (!I.getType()->isIntOrIntVectorTy())
1581     return markOverdefined(&I);
1582 
1583   // Try to simplify to a constant range.
1584   ConstantRange A =
1585       V1State.asConstantRange(I.getType(), /*UndefAllowed=*/false);
1586   ConstantRange B =
1587       V2State.asConstantRange(I.getType(), /*UndefAllowed=*/false);
1588 
1589   auto *BO = cast<BinaryOperator>(&I);
1590   ConstantRange R = ConstantRange::getEmpty(I.getType()->getScalarSizeInBits());
1591   if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(BO))
1592     R = A.overflowingBinaryOp(BO->getOpcode(), B, OBO->getNoWrapKind());
1593   else
1594     R = A.binaryOp(BO->getOpcode(), B);
1595   mergeInValue(&I, ValueLatticeElement::getRange(R));
1596 
1597   // TODO: Currently we do not exploit special values that produce something
1598   // better than overdefined with an overdefined operand for vector or floating
1599   // point types, like and <4 x i32> overdefined, zeroinitializer.
1600 }
1601 
1602 // Handle ICmpInst instruction.
1603 void SCCPInstVisitor::visitCmpInst(CmpInst &I) {
1604   // Do not cache this lookup, getValueState calls later in the function might
1605   // invalidate the reference.
1606   if (ValueState[&I].isOverdefined())
1607     return (void)markOverdefined(&I);
1608 
1609   Value *Op1 = I.getOperand(0);
1610   Value *Op2 = I.getOperand(1);
1611 
1612   // For parameters, use ParamState which includes constant range info if
1613   // available.
1614   auto V1State = getValueState(Op1);
1615   auto V2State = getValueState(Op2);
1616 
1617   Constant *C = V1State.getCompare(I.getPredicate(), I.getType(), V2State, DL);
1618   if (C) {
1619     ValueLatticeElement CV;
1620     CV.markConstant(C);
1621     mergeInValue(&I, CV);
1622     return;
1623   }
1624 
1625   // If operands are still unknown, wait for it to resolve.
1626   if ((V1State.isUnknownOrUndef() || V2State.isUnknownOrUndef()) &&
1627       !SCCPSolver::isConstant(ValueState[&I]))
1628     return;
1629 
1630   markOverdefined(&I);
1631 }
1632 
1633 // Handle getelementptr instructions.  If all operands are constants then we
1634 // can turn this into a getelementptr ConstantExpr.
1635 void SCCPInstVisitor::visitGetElementPtrInst(GetElementPtrInst &I) {
1636   if (ValueState[&I].isOverdefined())
1637     return (void)markOverdefined(&I);
1638 
1639   const ValueLatticeElement &PtrState = getValueState(I.getPointerOperand());
1640   if (PtrState.isUnknownOrUndef())
1641     return;
1642 
1643   // gep inbounds/nuw of non-null is non-null.
1644   if (PtrState.isNotConstant() && PtrState.getNotConstant()->isNullValue()) {
1645     if (I.hasNoUnsignedWrap() ||
1646         (I.isInBounds() &&
1647          !NullPointerIsDefined(I.getFunction(), I.getAddressSpace())))
1648       return (void)markNotNull(ValueState[&I], &I);
1649     return (void)markOverdefined(&I);
1650   }
1651 
1652   SmallVector<Constant *, 8> Operands;
1653   Operands.reserve(I.getNumOperands());
1654 
1655   for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
1656     ValueLatticeElement State = getValueState(I.getOperand(i));
1657     if (State.isUnknownOrUndef())
1658       return; // Operands are not resolved yet.
1659 
1660     if (Constant *C = getConstant(State, I.getOperand(i)->getType())) {
1661       Operands.push_back(C);
1662       continue;
1663     }
1664 
1665     return (void)markOverdefined(&I);
1666   }
1667 
1668   if (Constant *C = ConstantFoldInstOperands(&I, Operands, DL))
1669     markConstant(&I, C);
1670   else
1671     markOverdefined(&I);
1672 }
1673 
1674 void SCCPInstVisitor::visitAllocaInst(AllocaInst &I) {
1675   if (!NullPointerIsDefined(I.getFunction(), I.getAddressSpace()))
1676     return (void)markNotNull(ValueState[&I], &I);
1677 
1678   markOverdefined(&I);
1679 }
1680 
1681 void SCCPInstVisitor::visitStoreInst(StoreInst &SI) {
1682   // If this store is of a struct, ignore it.
1683   if (SI.getOperand(0)->getType()->isStructTy())
1684     return;
1685 
1686   if (TrackedGlobals.empty() || !isa<GlobalVariable>(SI.getOperand(1)))
1687     return;
1688 
1689   GlobalVariable *GV = cast<GlobalVariable>(SI.getOperand(1));
1690   auto I = TrackedGlobals.find(GV);
1691   if (I == TrackedGlobals.end())
1692     return;
1693 
1694   // Get the value we are storing into the global, then merge it.
1695   mergeInValue(I->second, GV, getValueState(SI.getOperand(0)),
1696                ValueLatticeElement::MergeOptions().setCheckWiden(false));
1697   if (I->second.isOverdefined())
1698     TrackedGlobals.erase(I); // No need to keep tracking this!
1699 }
1700 
1701 static ValueLatticeElement getValueFromMetadata(const Instruction *I) {
1702   if (const auto *CB = dyn_cast<CallBase>(I)) {
1703     if (CB->getType()->isIntOrIntVectorTy())
1704       if (std::optional<ConstantRange> Range = CB->getRange())
1705         return ValueLatticeElement::getRange(*Range);
1706     if (CB->getType()->isPointerTy() && CB->isReturnNonNull())
1707       return ValueLatticeElement::getNot(
1708           ConstantPointerNull::get(cast<PointerType>(I->getType())));
1709   }
1710 
1711   if (I->getType()->isIntOrIntVectorTy())
1712     if (MDNode *Ranges = I->getMetadata(LLVMContext::MD_range))
1713       return ValueLatticeElement::getRange(
1714           getConstantRangeFromMetadata(*Ranges));
1715   if (I->hasMetadata(LLVMContext::MD_nonnull))
1716     return ValueLatticeElement::getNot(
1717         ConstantPointerNull::get(cast<PointerType>(I->getType())));
1718 
1719   return ValueLatticeElement::getOverdefined();
1720 }
1721 
1722 // Handle load instructions.  If the operand is a constant pointer to a constant
1723 // global, we can replace the load with the loaded constant value!
1724 void SCCPInstVisitor::visitLoadInst(LoadInst &I) {
1725   // If this load is of a struct or the load is volatile, just mark the result
1726   // as overdefined.
1727   if (I.getType()->isStructTy() || I.isVolatile())
1728     return (void)markOverdefined(&I);
1729 
1730   // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1731   // discover a concrete value later.
1732   if (ValueState[&I].isOverdefined())
1733     return (void)markOverdefined(&I);
1734 
1735   ValueLatticeElement PtrVal = getValueState(I.getOperand(0));
1736   if (PtrVal.isUnknownOrUndef())
1737     return; // The pointer is not resolved yet!
1738 
1739   ValueLatticeElement &IV = ValueState[&I];
1740 
1741   if (SCCPSolver::isConstant(PtrVal)) {
1742     Constant *Ptr = getConstant(PtrVal, I.getOperand(0)->getType());
1743 
1744     // load null is undefined.
1745     if (isa<ConstantPointerNull>(Ptr)) {
1746       if (NullPointerIsDefined(I.getFunction(), I.getPointerAddressSpace()))
1747         return (void)markOverdefined(IV, &I);
1748       else
1749         return;
1750     }
1751 
1752     // Transform load (constant global) into the value loaded.
1753     if (auto *GV = dyn_cast<GlobalVariable>(Ptr)) {
1754       if (!TrackedGlobals.empty()) {
1755         // If we are tracking this global, merge in the known value for it.
1756         auto It = TrackedGlobals.find(GV);
1757         if (It != TrackedGlobals.end()) {
1758           mergeInValue(IV, &I, It->second, getMaxWidenStepsOpts());
1759           return;
1760         }
1761       }
1762     }
1763 
1764     // Transform load from a constant into a constant if possible.
1765     if (Constant *C = ConstantFoldLoadFromConstPtr(Ptr, I.getType(), DL))
1766       return (void)markConstant(IV, &I, C);
1767   }
1768 
1769   // Fall back to metadata.
1770   mergeInValue(&I, getValueFromMetadata(&I));
1771 }
1772 
1773 void SCCPInstVisitor::visitCallBase(CallBase &CB) {
1774   handleCallResult(CB);
1775   handleCallArguments(CB);
1776 }
1777 
1778 void SCCPInstVisitor::handleCallOverdefined(CallBase &CB) {
1779   Function *F = CB.getCalledFunction();
1780 
1781   // Void return and not tracking callee, just bail.
1782   if (CB.getType()->isVoidTy())
1783     return;
1784 
1785   // Always mark struct return as overdefined.
1786   if (CB.getType()->isStructTy())
1787     return (void)markOverdefined(&CB);
1788 
1789   // Otherwise, if we have a single return value case, and if the function is
1790   // a declaration, maybe we can constant fold it.
1791   if (F && F->isDeclaration() && canConstantFoldCallTo(&CB, F)) {
1792     SmallVector<Constant *, 8> Operands;
1793     for (const Use &A : CB.args()) {
1794       if (A.get()->getType()->isStructTy())
1795         return markOverdefined(&CB); // Can't handle struct args.
1796       if (A.get()->getType()->isMetadataTy())
1797         continue;                    // Carried in CB, not allowed in Operands.
1798       ValueLatticeElement State = getValueState(A);
1799 
1800       if (State.isUnknownOrUndef())
1801         return; // Operands are not resolved yet.
1802       if (SCCPSolver::isOverdefined(State))
1803         return (void)markOverdefined(&CB);
1804       assert(SCCPSolver::isConstant(State) && "Unknown state!");
1805       Operands.push_back(getConstant(State, A->getType()));
1806     }
1807 
1808     if (SCCPSolver::isOverdefined(getValueState(&CB)))
1809       return (void)markOverdefined(&CB);
1810 
1811     // If we can constant fold this, mark the result of the call as a
1812     // constant.
1813     if (Constant *C = ConstantFoldCall(&CB, F, Operands, &GetTLI(*F)))
1814       return (void)markConstant(&CB, C);
1815   }
1816 
1817   // Fall back to metadata.
1818   mergeInValue(&CB, getValueFromMetadata(&CB));
1819 }
1820 
1821 void SCCPInstVisitor::handleCallArguments(CallBase &CB) {
1822   Function *F = CB.getCalledFunction();
1823   // If this is a local function that doesn't have its address taken, mark its
1824   // entry block executable and merge in the actual arguments to the call into
1825   // the formal arguments of the function.
1826   if (TrackingIncomingArguments.count(F)) {
1827     markBlockExecutable(&F->front());
1828 
1829     // Propagate information from this call site into the callee.
1830     auto CAI = CB.arg_begin();
1831     for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E;
1832          ++AI, ++CAI) {
1833       // If this argument is byval, and if the function is not readonly, there
1834       // will be an implicit copy formed of the input aggregate.
1835       if (AI->hasByValAttr() && !F->onlyReadsMemory()) {
1836         markOverdefined(&*AI);
1837         continue;
1838       }
1839 
1840       if (auto *STy = dyn_cast<StructType>(AI->getType())) {
1841         for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1842           ValueLatticeElement CallArg = getStructValueState(*CAI, i);
1843           mergeInValue(getStructValueState(&*AI, i), &*AI, CallArg,
1844                        getMaxWidenStepsOpts());
1845         }
1846       } else
1847         mergeInValue(&*AI,
1848                      getValueState(*CAI).intersect(getArgAttributeVL(&*AI)),
1849                      getMaxWidenStepsOpts());
1850     }
1851   }
1852 }
1853 
1854 void SCCPInstVisitor::handleCallResult(CallBase &CB) {
1855   Function *F = CB.getCalledFunction();
1856 
1857   if (auto *II = dyn_cast<IntrinsicInst>(&CB)) {
1858     if (II->getIntrinsicID() == Intrinsic::ssa_copy) {
1859       if (ValueState[&CB].isOverdefined())
1860         return;
1861 
1862       Value *CopyOf = CB.getOperand(0);
1863       ValueLatticeElement CopyOfVal = getValueState(CopyOf);
1864       const auto *PI = getPredicateInfoFor(&CB);
1865       assert(PI && "Missing predicate info for ssa.copy");
1866 
1867       const std::optional<PredicateConstraint> &Constraint =
1868           PI->getConstraint();
1869       if (!Constraint) {
1870         mergeInValue(ValueState[&CB], &CB, CopyOfVal);
1871         return;
1872       }
1873 
1874       CmpInst::Predicate Pred = Constraint->Predicate;
1875       Value *OtherOp = Constraint->OtherOp;
1876 
1877       // Wait until OtherOp is resolved.
1878       if (getValueState(OtherOp).isUnknown()) {
1879         addAdditionalUser(OtherOp, &CB);
1880         return;
1881       }
1882 
1883       ValueLatticeElement CondVal = getValueState(OtherOp);
1884       ValueLatticeElement &IV = ValueState[&CB];
1885       if (CondVal.isConstantRange() || CopyOfVal.isConstantRange()) {
1886         auto ImposedCR =
1887             ConstantRange::getFull(DL.getTypeSizeInBits(CopyOf->getType()));
1888 
1889         // Get the range imposed by the condition.
1890         if (CondVal.isConstantRange())
1891           ImposedCR = ConstantRange::makeAllowedICmpRegion(
1892               Pred, CondVal.getConstantRange());
1893 
1894         // Combine range info for the original value with the new range from the
1895         // condition.
1896         auto CopyOfCR = CopyOfVal.asConstantRange(CopyOf->getType(),
1897                                                   /*UndefAllowed=*/true);
1898         // Treat an unresolved input like a full range.
1899         if (CopyOfCR.isEmptySet())
1900           CopyOfCR = ConstantRange::getFull(CopyOfCR.getBitWidth());
1901         auto NewCR = ImposedCR.intersectWith(CopyOfCR);
1902         // If the existing information is != x, do not use the information from
1903         // a chained predicate, as the != x information is more likely to be
1904         // helpful in practice.
1905         if (!CopyOfCR.contains(NewCR) && CopyOfCR.getSingleMissingElement())
1906           NewCR = CopyOfCR;
1907 
1908         // The new range is based on a branch condition. That guarantees that
1909         // neither of the compare operands can be undef in the branch targets,
1910         // unless we have conditions that are always true/false (e.g. icmp ule
1911         // i32, %a, i32_max). For the latter overdefined/empty range will be
1912         // inferred, but the branch will get folded accordingly anyways.
1913         addAdditionalUser(OtherOp, &CB);
1914         mergeInValue(
1915             IV, &CB,
1916             ValueLatticeElement::getRange(NewCR, /*MayIncludeUndef*/ false));
1917         return;
1918       } else if (Pred == CmpInst::ICMP_EQ &&
1919                  (CondVal.isConstant() || CondVal.isNotConstant())) {
1920         // For non-integer values or integer constant expressions, only
1921         // propagate equal constants or not-constants.
1922         addAdditionalUser(OtherOp, &CB);
1923         mergeInValue(IV, &CB, CondVal);
1924         return;
1925       } else if (Pred == CmpInst::ICMP_NE && CondVal.isConstant()) {
1926         // Propagate inequalities.
1927         addAdditionalUser(OtherOp, &CB);
1928         mergeInValue(IV, &CB,
1929                      ValueLatticeElement::getNot(CondVal.getConstant()));
1930         return;
1931       }
1932 
1933       return (void)mergeInValue(IV, &CB, CopyOfVal);
1934     }
1935 
1936     if (II->getIntrinsicID() == Intrinsic::vscale) {
1937       unsigned BitWidth = CB.getType()->getScalarSizeInBits();
1938       const ConstantRange Result = getVScaleRange(II->getFunction(), BitWidth);
1939       return (void)mergeInValue(II, ValueLatticeElement::getRange(Result));
1940     }
1941 
1942     if (ConstantRange::isIntrinsicSupported(II->getIntrinsicID())) {
1943       // Compute result range for intrinsics supported by ConstantRange.
1944       // Do this even if we don't know a range for all operands, as we may
1945       // still know something about the result range, e.g. of abs(x).
1946       SmallVector<ConstantRange, 2> OpRanges;
1947       for (Value *Op : II->args()) {
1948         const ValueLatticeElement &State = getValueState(Op);
1949         if (State.isUnknownOrUndef())
1950           return;
1951         OpRanges.push_back(
1952             State.asConstantRange(Op->getType(), /*UndefAllowed=*/false));
1953       }
1954 
1955       ConstantRange Result =
1956           ConstantRange::intrinsic(II->getIntrinsicID(), OpRanges);
1957       return (void)mergeInValue(II, ValueLatticeElement::getRange(Result));
1958     }
1959   }
1960 
1961   // The common case is that we aren't tracking the callee, either because we
1962   // are not doing interprocedural analysis or the callee is indirect, or is
1963   // external.  Handle these cases first.
1964   if (!F || F->isDeclaration())
1965     return handleCallOverdefined(CB);
1966 
1967   // If this is a single/zero retval case, see if we're tracking the function.
1968   if (auto *STy = dyn_cast<StructType>(F->getReturnType())) {
1969     if (!MRVFunctionsTracked.count(F))
1970       return handleCallOverdefined(CB); // Not tracking this callee.
1971 
1972     // If we are tracking this callee, propagate the result of the function
1973     // into this call site.
1974     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
1975       mergeInValue(getStructValueState(&CB, i), &CB,
1976                    TrackedMultipleRetVals[std::make_pair(F, i)],
1977                    getMaxWidenStepsOpts());
1978   } else {
1979     auto TFRVI = TrackedRetVals.find(F);
1980     if (TFRVI == TrackedRetVals.end())
1981       return handleCallOverdefined(CB); // Not tracking this callee.
1982 
1983     // If so, propagate the return value of the callee into this call result.
1984     mergeInValue(&CB, TFRVI->second, getMaxWidenStepsOpts());
1985   }
1986 }
1987 
1988 void SCCPInstVisitor::solve() {
1989   // Process the work lists until they are empty!
1990   while (!BBWorkList.empty() || !InstWorkList.empty() ||
1991          !OverdefinedInstWorkList.empty()) {
1992     // Process the overdefined instruction's work list first, which drives other
1993     // things to overdefined more quickly.
1994     while (!OverdefinedInstWorkList.empty()) {
1995       Value *I = OverdefinedInstWorkList.pop_back_val();
1996       Invalidated.erase(I);
1997 
1998       LLVM_DEBUG(dbgs() << "\nPopped off OI-WL: " << *I << '\n');
1999 
2000       // "I" got into the work list because it either made the transition from
2001       // bottom to constant, or to overdefined.
2002       //
2003       // Anything on this worklist that is overdefined need not be visited
2004       // since all of its users will have already been marked as overdefined
2005       // Update all of the users of this instruction's value.
2006       //
2007       markUsersAsChanged(I);
2008     }
2009 
2010     // Process the instruction work list.
2011     while (!InstWorkList.empty()) {
2012       Value *I = InstWorkList.pop_back_val();
2013       Invalidated.erase(I);
2014 
2015       LLVM_DEBUG(dbgs() << "\nPopped off I-WL: " << *I << '\n');
2016 
2017       // "I" got into the work list because it made the transition from undef to
2018       // constant.
2019       //
2020       // Anything on this worklist that is overdefined need not be visited
2021       // since all of its users will have already been marked as overdefined.
2022       // Update all of the users of this instruction's value.
2023       //
2024       if (I->getType()->isStructTy() || !getValueState(I).isOverdefined())
2025         markUsersAsChanged(I);
2026     }
2027 
2028     // Process the basic block work list.
2029     while (!BBWorkList.empty()) {
2030       BasicBlock *BB = BBWorkList.pop_back_val();
2031 
2032       LLVM_DEBUG(dbgs() << "\nPopped off BBWL: " << *BB << '\n');
2033 
2034       // Notify all instructions in this basic block that they are newly
2035       // executable.
2036       visit(BB);
2037     }
2038   }
2039 }
2040 
2041 bool SCCPInstVisitor::resolvedUndef(Instruction &I) {
2042   // Look for instructions which produce undef values.
2043   if (I.getType()->isVoidTy())
2044     return false;
2045 
2046   if (auto *STy = dyn_cast<StructType>(I.getType())) {
2047     // Only a few things that can be structs matter for undef.
2048 
2049     // Tracked calls must never be marked overdefined in resolvedUndefsIn.
2050     if (auto *CB = dyn_cast<CallBase>(&I))
2051       if (Function *F = CB->getCalledFunction())
2052         if (MRVFunctionsTracked.count(F))
2053           return false;
2054 
2055     // extractvalue and insertvalue don't need to be marked; they are
2056     // tracked as precisely as their operands.
2057     if (isa<ExtractValueInst>(I) || isa<InsertValueInst>(I))
2058       return false;
2059     // Send the results of everything else to overdefined.  We could be
2060     // more precise than this but it isn't worth bothering.
2061     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
2062       ValueLatticeElement &LV = getStructValueState(&I, i);
2063       if (LV.isUnknown()) {
2064         markOverdefined(LV, &I);
2065         return true;
2066       }
2067     }
2068     return false;
2069   }
2070 
2071   ValueLatticeElement &LV = getValueState(&I);
2072   if (!LV.isUnknown())
2073     return false;
2074 
2075   // There are two reasons a call can have an undef result
2076   // 1. It could be tracked.
2077   // 2. It could be constant-foldable.
2078   // Because of the way we solve return values, tracked calls must
2079   // never be marked overdefined in resolvedUndefsIn.
2080   if (auto *CB = dyn_cast<CallBase>(&I))
2081     if (Function *F = CB->getCalledFunction())
2082       if (TrackedRetVals.count(F))
2083         return false;
2084 
2085   if (isa<LoadInst>(I)) {
2086     // A load here means one of two things: a load of undef from a global,
2087     // a load from an unknown pointer.  Either way, having it return undef
2088     // is okay.
2089     return false;
2090   }
2091 
2092   markOverdefined(&I);
2093   return true;
2094 }
2095 
2096 /// While solving the dataflow for a function, we don't compute a result for
2097 /// operations with an undef operand, to allow undef to be lowered to a
2098 /// constant later. For example, constant folding of "zext i8 undef to i16"
2099 /// would result in "i16 0", and if undef is later lowered to "i8 1", then the
2100 /// zext result would become "i16 1" and would result into an overdefined
2101 /// lattice value once merged with the previous result. Not computing the
2102 /// result of the zext (treating undef the same as unknown) allows us to handle
2103 /// a later undef->constant lowering more optimally.
2104 ///
2105 /// However, if the operand remains undef when the solver returns, we do need
2106 /// to assign some result to the instruction (otherwise we would treat it as
2107 /// unreachable). For simplicity, we mark any instructions that are still
2108 /// unknown as overdefined.
2109 bool SCCPInstVisitor::resolvedUndefsIn(Function &F) {
2110   bool MadeChange = false;
2111   for (BasicBlock &BB : F) {
2112     if (!BBExecutable.count(&BB))
2113       continue;
2114 
2115     for (Instruction &I : BB)
2116       MadeChange |= resolvedUndef(I);
2117   }
2118 
2119   LLVM_DEBUG(if (MadeChange) dbgs()
2120              << "\nResolved undefs in " << F.getName() << '\n');
2121 
2122   return MadeChange;
2123 }
2124 
2125 //===----------------------------------------------------------------------===//
2126 //
2127 // SCCPSolver implementations
2128 //
2129 SCCPSolver::SCCPSolver(
2130     const DataLayout &DL,
2131     std::function<const TargetLibraryInfo &(Function &)> GetTLI,
2132     LLVMContext &Ctx)
2133     : Visitor(new SCCPInstVisitor(DL, std::move(GetTLI), Ctx)) {}
2134 
2135 SCCPSolver::~SCCPSolver() = default;
2136 
2137 void SCCPSolver::addPredicateInfo(Function &F, DominatorTree &DT,
2138                                   AssumptionCache &AC) {
2139   Visitor->addPredicateInfo(F, DT, AC);
2140 }
2141 
2142 bool SCCPSolver::markBlockExecutable(BasicBlock *BB) {
2143   return Visitor->markBlockExecutable(BB);
2144 }
2145 
2146 const PredicateBase *SCCPSolver::getPredicateInfoFor(Instruction *I) {
2147   return Visitor->getPredicateInfoFor(I);
2148 }
2149 
2150 void SCCPSolver::trackValueOfGlobalVariable(GlobalVariable *GV) {
2151   Visitor->trackValueOfGlobalVariable(GV);
2152 }
2153 
2154 void SCCPSolver::addTrackedFunction(Function *F) {
2155   Visitor->addTrackedFunction(F);
2156 }
2157 
2158 void SCCPSolver::addToMustPreserveReturnsInFunctions(Function *F) {
2159   Visitor->addToMustPreserveReturnsInFunctions(F);
2160 }
2161 
2162 bool SCCPSolver::mustPreserveReturn(Function *F) {
2163   return Visitor->mustPreserveReturn(F);
2164 }
2165 
2166 void SCCPSolver::addArgumentTrackedFunction(Function *F) {
2167   Visitor->addArgumentTrackedFunction(F);
2168 }
2169 
2170 bool SCCPSolver::isArgumentTrackedFunction(Function *F) {
2171   return Visitor->isArgumentTrackedFunction(F);
2172 }
2173 
2174 const SmallPtrSetImpl<Function *> &
2175 SCCPSolver::getArgumentTrackedFunctions() const {
2176   return Visitor->getArgumentTrackedFunctions();
2177 }
2178 
2179 void SCCPSolver::solve() { Visitor->solve(); }
2180 
2181 bool SCCPSolver::resolvedUndefsIn(Function &F) {
2182   return Visitor->resolvedUndefsIn(F);
2183 }
2184 
2185 void SCCPSolver::solveWhileResolvedUndefsIn(Module &M) {
2186   Visitor->solveWhileResolvedUndefsIn(M);
2187 }
2188 
2189 void
2190 SCCPSolver::solveWhileResolvedUndefsIn(SmallVectorImpl<Function *> &WorkList) {
2191   Visitor->solveWhileResolvedUndefsIn(WorkList);
2192 }
2193 
2194 void SCCPSolver::solveWhileResolvedUndefs() {
2195   Visitor->solveWhileResolvedUndefs();
2196 }
2197 
2198 bool SCCPSolver::isBlockExecutable(BasicBlock *BB) const {
2199   return Visitor->isBlockExecutable(BB);
2200 }
2201 
2202 bool SCCPSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To) const {
2203   return Visitor->isEdgeFeasible(From, To);
2204 }
2205 
2206 std::vector<ValueLatticeElement>
2207 SCCPSolver::getStructLatticeValueFor(Value *V) const {
2208   return Visitor->getStructLatticeValueFor(V);
2209 }
2210 
2211 void SCCPSolver::removeLatticeValueFor(Value *V) {
2212   return Visitor->removeLatticeValueFor(V);
2213 }
2214 
2215 void SCCPSolver::resetLatticeValueFor(CallBase *Call) {
2216   Visitor->resetLatticeValueFor(Call);
2217 }
2218 
2219 const ValueLatticeElement &SCCPSolver::getLatticeValueFor(Value *V) const {
2220   return Visitor->getLatticeValueFor(V);
2221 }
2222 
2223 const MapVector<Function *, ValueLatticeElement> &
2224 SCCPSolver::getTrackedRetVals() const {
2225   return Visitor->getTrackedRetVals();
2226 }
2227 
2228 const DenseMap<GlobalVariable *, ValueLatticeElement> &
2229 SCCPSolver::getTrackedGlobals() {
2230   return Visitor->getTrackedGlobals();
2231 }
2232 
2233 const SmallPtrSet<Function *, 16> SCCPSolver::getMRVFunctionsTracked() {
2234   return Visitor->getMRVFunctionsTracked();
2235 }
2236 
2237 void SCCPSolver::markOverdefined(Value *V) { Visitor->markOverdefined(V); }
2238 
2239 void SCCPSolver::trackValueOfArgument(Argument *V) {
2240   Visitor->trackValueOfArgument(V);
2241 }
2242 
2243 bool SCCPSolver::isStructLatticeConstant(Function *F, StructType *STy) {
2244   return Visitor->isStructLatticeConstant(F, STy);
2245 }
2246 
2247 Constant *SCCPSolver::getConstant(const ValueLatticeElement &LV,
2248                                   Type *Ty) const {
2249   return Visitor->getConstant(LV, Ty);
2250 }
2251 
2252 Constant *SCCPSolver::getConstantOrNull(Value *V) const {
2253   return Visitor->getConstantOrNull(V);
2254 }
2255 
2256 void SCCPSolver::setLatticeValueForSpecializationArguments(Function *F,
2257                                    const SmallVectorImpl<ArgInfo> &Args) {
2258   Visitor->setLatticeValueForSpecializationArguments(F, Args);
2259 }
2260 
2261 void SCCPSolver::markFunctionUnreachable(Function *F) {
2262   Visitor->markFunctionUnreachable(F);
2263 }
2264 
2265 void SCCPSolver::visit(Instruction *I) { Visitor->visit(I); }
2266 
2267 void SCCPSolver::visitCall(CallInst &I) { Visitor->visitCall(I); }
2268