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/Analysis/ConstantFolding.h"
17 #include "llvm/Analysis/InstructionSimplify.h"
18 #include "llvm/Analysis/ValueLattice.h"
19 #include "llvm/IR/InstVisitor.h"
20 #include "llvm/Support/Casting.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include <cassert>
25 #include <utility>
26 #include <vector>
27 
28 using namespace llvm;
29 
30 #define DEBUG_TYPE "sccp"
31 
32 // The maximum number of range extensions allowed for operations requiring
33 // widening.
34 static const unsigned MaxNumRangeExtensions = 10;
35 
36 /// Returns MergeOptions with MaxWidenSteps set to MaxNumRangeExtensions.
37 static ValueLatticeElement::MergeOptions getMaxWidenStepsOpts() {
38   return ValueLatticeElement::MergeOptions().setMaxWidenSteps(
39       MaxNumRangeExtensions);
40 }
41 
42 namespace {
43 
44 // Helper to check if \p LV is either a constant or a constant
45 // range with a single element. This should cover exactly the same cases as the
46 // old ValueLatticeElement::isConstant() and is intended to be used in the
47 // transition to ValueLatticeElement.
48 bool isConstant(const ValueLatticeElement &LV) {
49   return LV.isConstant() ||
50          (LV.isConstantRange() && LV.getConstantRange().isSingleElement());
51 }
52 
53 // Helper to check if \p LV is either overdefined or a constant range with more
54 // than a single element. This should cover exactly the same cases as the old
55 // ValueLatticeElement::isOverdefined() and is intended to be used in the
56 // transition to ValueLatticeElement.
57 bool isOverdefined(const ValueLatticeElement &LV) {
58   return !LV.isUnknownOrUndef() && !isConstant(LV);
59 }
60 
61 } // namespace
62 
63 namespace llvm {
64 
65 /// Helper class for SCCPSolver. This implements the instruction visitor and
66 /// holds all the state.
67 class SCCPInstVisitor : public InstVisitor<SCCPInstVisitor> {
68   const DataLayout &DL;
69   std::function<const TargetLibraryInfo &(Function &)> GetTLI;
70   SmallPtrSet<BasicBlock *, 8> BBExecutable; // The BBs that are executable.
71   DenseMap<Value *, ValueLatticeElement>
72       ValueState; // The state each value is in.
73 
74   /// StructValueState - This maintains ValueState for values that have
75   /// StructType, for example for formal arguments, calls, insertelement, etc.
76   DenseMap<std::pair<Value *, unsigned>, ValueLatticeElement> StructValueState;
77 
78   /// GlobalValue - If we are tracking any values for the contents of a global
79   /// variable, we keep a mapping from the constant accessor to the element of
80   /// the global, to the currently known value.  If the value becomes
81   /// overdefined, it's entry is simply removed from this map.
82   DenseMap<GlobalVariable *, ValueLatticeElement> TrackedGlobals;
83 
84   /// TrackedRetVals - If we are tracking arguments into and the return
85   /// value out of a function, it will have an entry in this map, indicating
86   /// what the known return value for the function is.
87   MapVector<Function *, ValueLatticeElement> TrackedRetVals;
88 
89   /// TrackedMultipleRetVals - Same as TrackedRetVals, but used for functions
90   /// that return multiple values.
91   MapVector<std::pair<Function *, unsigned>, ValueLatticeElement>
92       TrackedMultipleRetVals;
93 
94   /// MRVFunctionsTracked - Each function in TrackedMultipleRetVals is
95   /// represented here for efficient lookup.
96   SmallPtrSet<Function *, 16> MRVFunctionsTracked;
97 
98   /// A list of functions whose return cannot be modified.
99   SmallPtrSet<Function *, 16> MustPreserveReturnsInFunctions;
100 
101   /// TrackingIncomingArguments - This is the set of functions for whose
102   /// arguments we make optimistic assumptions about and try to prove as
103   /// constants.
104   SmallPtrSet<Function *, 16> TrackingIncomingArguments;
105 
106   /// The reason for two worklists is that overdefined is the lowest state
107   /// on the lattice, and moving things to overdefined as fast as possible
108   /// makes SCCP converge much faster.
109   ///
110   /// By having a separate worklist, we accomplish this because everything
111   /// possibly overdefined will become overdefined at the soonest possible
112   /// point.
113   SmallVector<Value *, 64> OverdefinedInstWorkList;
114   SmallVector<Value *, 64> InstWorkList;
115 
116   // The BasicBlock work list
117   SmallVector<BasicBlock *, 64> BBWorkList;
118 
119   /// KnownFeasibleEdges - Entries in this set are edges which have already had
120   /// PHI nodes retriggered.
121   using Edge = std::pair<BasicBlock *, BasicBlock *>;
122   DenseSet<Edge> KnownFeasibleEdges;
123 
124   DenseMap<Function *, AnalysisResultsForFn> AnalysisResults;
125   DenseMap<Value *, SmallPtrSet<User *, 2>> AdditionalUsers;
126 
127   LLVMContext &Ctx;
128 
129 private:
130   ConstantInt *getConstantInt(const ValueLatticeElement &IV) const {
131     return dyn_cast_or_null<ConstantInt>(getConstant(IV));
132   }
133 
134   // pushToWorkList - Helper for markConstant/markOverdefined
135   void pushToWorkList(ValueLatticeElement &IV, Value *V);
136 
137   // Helper to push \p V to the worklist, after updating it to \p IV. Also
138   // prints a debug message with the updated value.
139   void pushToWorkListMsg(ValueLatticeElement &IV, Value *V);
140 
141   // markConstant - Make a value be marked as "constant".  If the value
142   // is not already a constant, add it to the instruction work list so that
143   // the users of the instruction are updated later.
144   bool markConstant(ValueLatticeElement &IV, Value *V, Constant *C,
145                     bool MayIncludeUndef = false);
146 
147   bool markConstant(Value *V, Constant *C) {
148     assert(!V->getType()->isStructTy() && "structs should use mergeInValue");
149     return markConstant(ValueState[V], V, C);
150   }
151 
152   // markOverdefined - Make a value be marked as "overdefined". If the
153   // value is not already overdefined, add it to the overdefined instruction
154   // work list so that the users of the instruction are updated later.
155   bool markOverdefined(ValueLatticeElement &IV, Value *V);
156 
157   /// Merge \p MergeWithV into \p IV and push \p V to the worklist, if \p IV
158   /// changes.
159   bool mergeInValue(ValueLatticeElement &IV, Value *V,
160                     ValueLatticeElement MergeWithV,
161                     ValueLatticeElement::MergeOptions Opts = {
162                         /*MayIncludeUndef=*/false, /*CheckWiden=*/false});
163 
164   bool mergeInValue(Value *V, ValueLatticeElement MergeWithV,
165                     ValueLatticeElement::MergeOptions Opts = {
166                         /*MayIncludeUndef=*/false, /*CheckWiden=*/false}) {
167     assert(!V->getType()->isStructTy() &&
168            "non-structs should use markConstant");
169     return mergeInValue(ValueState[V], V, MergeWithV, Opts);
170   }
171 
172   /// getValueState - Return the ValueLatticeElement object that corresponds to
173   /// the value.  This function handles the case when the value hasn't been seen
174   /// yet by properly seeding constants etc.
175   ValueLatticeElement &getValueState(Value *V) {
176     assert(!V->getType()->isStructTy() && "Should use getStructValueState");
177 
178     auto I = ValueState.insert(std::make_pair(V, ValueLatticeElement()));
179     ValueLatticeElement &LV = I.first->second;
180 
181     if (!I.second)
182       return LV; // Common case, already in the map.
183 
184     if (auto *C = dyn_cast<Constant>(V))
185       LV.markConstant(C); // Constants are constant
186 
187     // All others are unknown by default.
188     return LV;
189   }
190 
191   /// getStructValueState - Return the ValueLatticeElement object that
192   /// corresponds to the value/field pair.  This function handles the case when
193   /// the value hasn't been seen yet by properly seeding constants etc.
194   ValueLatticeElement &getStructValueState(Value *V, unsigned i) {
195     assert(V->getType()->isStructTy() && "Should use getValueState");
196     assert(i < cast<StructType>(V->getType())->getNumElements() &&
197            "Invalid element #");
198 
199     auto I = StructValueState.insert(
200         std::make_pair(std::make_pair(V, i), ValueLatticeElement()));
201     ValueLatticeElement &LV = I.first->second;
202 
203     if (!I.second)
204       return LV; // Common case, already in the map.
205 
206     if (auto *C = dyn_cast<Constant>(V)) {
207       Constant *Elt = C->getAggregateElement(i);
208 
209       if (!Elt)
210         LV.markOverdefined(); // Unknown sort of constant.
211       else if (isa<UndefValue>(Elt))
212         ; // Undef values remain unknown.
213       else
214         LV.markConstant(Elt); // Constants are constant.
215     }
216 
217     // All others are underdefined by default.
218     return LV;
219   }
220 
221   /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
222   /// work list if it is not already executable.
223   bool markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest);
224 
225   // getFeasibleSuccessors - Return a vector of booleans to indicate which
226   // successors are reachable from a given terminator instruction.
227   void getFeasibleSuccessors(Instruction &TI, SmallVectorImpl<bool> &Succs);
228 
229   // OperandChangedState - This method is invoked on all of the users of an
230   // instruction that was just changed state somehow.  Based on this
231   // information, we need to update the specified user of this instruction.
232   void operandChangedState(Instruction *I) {
233     if (BBExecutable.count(I->getParent())) // Inst is executable?
234       visit(*I);
235   }
236 
237   // Add U as additional user of V.
238   void addAdditionalUser(Value *V, User *U) {
239     auto Iter = AdditionalUsers.insert({V, {}});
240     Iter.first->second.insert(U);
241   }
242 
243   // Mark I's users as changed, including AdditionalUsers.
244   void markUsersAsChanged(Value *I) {
245     // Functions include their arguments in the use-list. Changed function
246     // values mean that the result of the function changed. We only need to
247     // update the call sites with the new function result and do not have to
248     // propagate the call arguments.
249     if (isa<Function>(I)) {
250       for (User *U : I->users()) {
251         if (auto *CB = dyn_cast<CallBase>(U))
252           handleCallResult(*CB);
253       }
254     } else {
255       for (User *U : I->users())
256         if (auto *UI = dyn_cast<Instruction>(U))
257           operandChangedState(UI);
258     }
259 
260     auto Iter = AdditionalUsers.find(I);
261     if (Iter != AdditionalUsers.end()) {
262       // Copy additional users before notifying them of changes, because new
263       // users may be added, potentially invalidating the iterator.
264       SmallVector<Instruction *, 2> ToNotify;
265       for (User *U : Iter->second)
266         if (auto *UI = dyn_cast<Instruction>(U))
267           ToNotify.push_back(UI);
268       for (Instruction *UI : ToNotify)
269         operandChangedState(UI);
270     }
271   }
272   void handleCallOverdefined(CallBase &CB);
273   void handleCallResult(CallBase &CB);
274   void handleCallArguments(CallBase &CB);
275 
276 private:
277   friend class InstVisitor<SCCPInstVisitor>;
278 
279   // visit implementations - Something changed in this instruction.  Either an
280   // operand made a transition, or the instruction is newly executable.  Change
281   // the value type of I to reflect these changes if appropriate.
282   void visitPHINode(PHINode &I);
283 
284   // Terminators
285 
286   void visitReturnInst(ReturnInst &I);
287   void visitTerminator(Instruction &TI);
288 
289   void visitCastInst(CastInst &I);
290   void visitSelectInst(SelectInst &I);
291   void visitUnaryOperator(Instruction &I);
292   void visitBinaryOperator(Instruction &I);
293   void visitCmpInst(CmpInst &I);
294   void visitExtractValueInst(ExtractValueInst &EVI);
295   void visitInsertValueInst(InsertValueInst &IVI);
296 
297   void visitCatchSwitchInst(CatchSwitchInst &CPI) {
298     markOverdefined(&CPI);
299     visitTerminator(CPI);
300   }
301 
302   // Instructions that cannot be folded away.
303 
304   void visitStoreInst(StoreInst &I);
305   void visitLoadInst(LoadInst &I);
306   void visitGetElementPtrInst(GetElementPtrInst &I);
307 
308   void visitInvokeInst(InvokeInst &II) {
309     visitCallBase(II);
310     visitTerminator(II);
311   }
312 
313   void visitCallBrInst(CallBrInst &CBI) {
314     visitCallBase(CBI);
315     visitTerminator(CBI);
316   }
317 
318   void visitCallBase(CallBase &CB);
319   void visitResumeInst(ResumeInst &I) { /*returns void*/
320   }
321   void visitUnreachableInst(UnreachableInst &I) { /*returns void*/
322   }
323   void visitFenceInst(FenceInst &I) { /*returns void*/
324   }
325 
326   void visitInstruction(Instruction &I);
327 
328 public:
329   void addAnalysis(Function &F, AnalysisResultsForFn A) {
330     AnalysisResults.insert({&F, std::move(A)});
331   }
332 
333   void visitCallInst(CallInst &I) { visitCallBase(I); }
334 
335   bool markBlockExecutable(BasicBlock *BB);
336 
337   const PredicateBase *getPredicateInfoFor(Instruction *I) {
338     auto A = AnalysisResults.find(I->getParent()->getParent());
339     if (A == AnalysisResults.end())
340       return nullptr;
341     return A->second.PredInfo->getPredicateInfoFor(I);
342   }
343 
344   DomTreeUpdater getDTU(Function &F) {
345     auto A = AnalysisResults.find(&F);
346     assert(A != AnalysisResults.end() && "Need analysis results for function.");
347     return {A->second.DT, A->second.PDT, DomTreeUpdater::UpdateStrategy::Lazy};
348   }
349 
350   SCCPInstVisitor(const DataLayout &DL,
351                   std::function<const TargetLibraryInfo &(Function &)> GetTLI,
352                   LLVMContext &Ctx)
353       : DL(DL), GetTLI(GetTLI), Ctx(Ctx) {}
354 
355   void trackValueOfGlobalVariable(GlobalVariable *GV) {
356     // We only track the contents of scalar globals.
357     if (GV->getValueType()->isSingleValueType()) {
358       ValueLatticeElement &IV = TrackedGlobals[GV];
359       if (!isa<UndefValue>(GV->getInitializer()))
360         IV.markConstant(GV->getInitializer());
361     }
362   }
363 
364   void addTrackedFunction(Function *F) {
365     // Add an entry, F -> undef.
366     if (auto *STy = dyn_cast<StructType>(F->getReturnType())) {
367       MRVFunctionsTracked.insert(F);
368       for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
369         TrackedMultipleRetVals.insert(
370             std::make_pair(std::make_pair(F, i), ValueLatticeElement()));
371     } else if (!F->getReturnType()->isVoidTy())
372       TrackedRetVals.insert(std::make_pair(F, ValueLatticeElement()));
373   }
374 
375   void addToMustPreserveReturnsInFunctions(Function *F) {
376     MustPreserveReturnsInFunctions.insert(F);
377   }
378 
379   bool mustPreserveReturn(Function *F) {
380     return MustPreserveReturnsInFunctions.count(F);
381   }
382 
383   void addArgumentTrackedFunction(Function *F) {
384     TrackingIncomingArguments.insert(F);
385   }
386 
387   bool isArgumentTrackedFunction(Function *F) {
388     return TrackingIncomingArguments.count(F);
389   }
390 
391   void solve();
392 
393   bool resolvedUndefsIn(Function &F);
394 
395   bool isBlockExecutable(BasicBlock *BB) const {
396     return BBExecutable.count(BB);
397   }
398 
399   bool isEdgeFeasible(BasicBlock *From, BasicBlock *To) const;
400 
401   std::vector<ValueLatticeElement> getStructLatticeValueFor(Value *V) const {
402     std::vector<ValueLatticeElement> StructValues;
403     auto *STy = dyn_cast<StructType>(V->getType());
404     assert(STy && "getStructLatticeValueFor() can be called only on structs");
405     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
406       auto I = StructValueState.find(std::make_pair(V, i));
407       assert(I != StructValueState.end() && "Value not in valuemap!");
408       StructValues.push_back(I->second);
409     }
410     return StructValues;
411   }
412 
413   void removeLatticeValueFor(Value *V) { ValueState.erase(V); }
414 
415   const ValueLatticeElement &getLatticeValueFor(Value *V) const {
416     assert(!V->getType()->isStructTy() &&
417            "Should use getStructLatticeValueFor");
418     DenseMap<Value *, ValueLatticeElement>::const_iterator I =
419         ValueState.find(V);
420     assert(I != ValueState.end() &&
421            "V not found in ValueState nor Paramstate map!");
422     return I->second;
423   }
424 
425   const MapVector<Function *, ValueLatticeElement> &getTrackedRetVals() {
426     return TrackedRetVals;
427   }
428 
429   const DenseMap<GlobalVariable *, ValueLatticeElement> &getTrackedGlobals() {
430     return TrackedGlobals;
431   }
432 
433   const SmallPtrSet<Function *, 16> getMRVFunctionsTracked() {
434     return MRVFunctionsTracked;
435   }
436 
437   void markOverdefined(Value *V) {
438     if (auto *STy = dyn_cast<StructType>(V->getType()))
439       for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
440         markOverdefined(getStructValueState(V, i), V);
441     else
442       markOverdefined(ValueState[V], V);
443   }
444 
445   bool isStructLatticeConstant(Function *F, StructType *STy);
446 
447   Constant *getConstant(const ValueLatticeElement &LV) const;
448 
449   SmallPtrSetImpl<Function *> &getArgumentTrackedFunctions() {
450     return TrackingIncomingArguments;
451   }
452 
453   void markArgInFuncSpecialization(Function *F, Argument *A, Constant *C);
454 
455   void markFunctionUnreachable(Function *F) {
456     for (auto &BB : *F)
457       BBExecutable.erase(&BB);
458   }
459 };
460 
461 } // namespace llvm
462 
463 bool SCCPInstVisitor::markBlockExecutable(BasicBlock *BB) {
464   if (!BBExecutable.insert(BB).second)
465     return false;
466   LLVM_DEBUG(dbgs() << "Marking Block Executable: " << BB->getName() << '\n');
467   BBWorkList.push_back(BB); // Add the block to the work list!
468   return true;
469 }
470 
471 void SCCPInstVisitor::pushToWorkList(ValueLatticeElement &IV, Value *V) {
472   if (IV.isOverdefined())
473     return OverdefinedInstWorkList.push_back(V);
474   InstWorkList.push_back(V);
475 }
476 
477 void SCCPInstVisitor::pushToWorkListMsg(ValueLatticeElement &IV, Value *V) {
478   LLVM_DEBUG(dbgs() << "updated " << IV << ": " << *V << '\n');
479   pushToWorkList(IV, V);
480 }
481 
482 bool SCCPInstVisitor::markConstant(ValueLatticeElement &IV, Value *V,
483                                    Constant *C, bool MayIncludeUndef) {
484   if (!IV.markConstant(C, MayIncludeUndef))
485     return false;
486   LLVM_DEBUG(dbgs() << "markConstant: " << *C << ": " << *V << '\n');
487   pushToWorkList(IV, V);
488   return true;
489 }
490 
491 bool SCCPInstVisitor::markOverdefined(ValueLatticeElement &IV, Value *V) {
492   if (!IV.markOverdefined())
493     return false;
494 
495   LLVM_DEBUG(dbgs() << "markOverdefined: ";
496              if (auto *F = dyn_cast<Function>(V)) dbgs()
497              << "Function '" << F->getName() << "'\n";
498              else dbgs() << *V << '\n');
499   // Only instructions go on the work list
500   pushToWorkList(IV, V);
501   return true;
502 }
503 
504 bool SCCPInstVisitor::isStructLatticeConstant(Function *F, StructType *STy) {
505   for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
506     const auto &It = TrackedMultipleRetVals.find(std::make_pair(F, i));
507     assert(It != TrackedMultipleRetVals.end());
508     ValueLatticeElement LV = It->second;
509     if (!isConstant(LV))
510       return false;
511   }
512   return true;
513 }
514 
515 Constant *SCCPInstVisitor::getConstant(const ValueLatticeElement &LV) const {
516   if (LV.isConstant())
517     return LV.getConstant();
518 
519   if (LV.isConstantRange()) {
520     const auto &CR = LV.getConstantRange();
521     if (CR.getSingleElement())
522       return ConstantInt::get(Ctx, *CR.getSingleElement());
523   }
524   return nullptr;
525 }
526 
527 void SCCPInstVisitor::markArgInFuncSpecialization(Function *F, Argument *A,
528                                                   Constant *C) {
529   assert(F->arg_size() == A->getParent()->arg_size() &&
530          "Functions should have the same number of arguments");
531 
532   // Mark the argument constant in the new function.
533   markConstant(A, C);
534 
535   // For the remaining arguments in the new function, copy the lattice state
536   // over from the old function.
537   for (Argument *OldArg = F->arg_begin(), *NewArg = A->getParent()->arg_begin(),
538                 *End = F->arg_end();
539        OldArg != End; ++OldArg, ++NewArg) {
540 
541     LLVM_DEBUG(dbgs() << "SCCP: Marking argument "
542                       << NewArg->getNameOrAsOperand() << "\n");
543 
544     if (NewArg != A && ValueState.count(OldArg)) {
545       // Note: This previously looked like this:
546       // ValueState[NewArg] = ValueState[OldArg];
547       // This is incorrect because the DenseMap class may resize the underlying
548       // memory when inserting `NewArg`, which will invalidate the reference to
549       // `OldArg`. Instead, we make sure `NewArg` exists before setting it.
550       auto &NewValue = ValueState[NewArg];
551       NewValue = ValueState[OldArg];
552       pushToWorkList(NewValue, NewArg);
553     }
554   }
555 }
556 
557 void SCCPInstVisitor::visitInstruction(Instruction &I) {
558   // All the instructions we don't do any special handling for just
559   // go to overdefined.
560   LLVM_DEBUG(dbgs() << "SCCP: Don't know how to handle: " << I << '\n');
561   markOverdefined(&I);
562 }
563 
564 bool SCCPInstVisitor::mergeInValue(ValueLatticeElement &IV, Value *V,
565                                    ValueLatticeElement MergeWithV,
566                                    ValueLatticeElement::MergeOptions Opts) {
567   if (IV.mergeIn(MergeWithV, Opts)) {
568     pushToWorkList(IV, V);
569     LLVM_DEBUG(dbgs() << "Merged " << MergeWithV << " into " << *V << " : "
570                       << IV << "\n");
571     return true;
572   }
573   return false;
574 }
575 
576 bool SCCPInstVisitor::markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
577   if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
578     return false; // This edge is already known to be executable!
579 
580   if (!markBlockExecutable(Dest)) {
581     // If the destination is already executable, we just made an *edge*
582     // feasible that wasn't before.  Revisit the PHI nodes in the block
583     // because they have potentially new operands.
584     LLVM_DEBUG(dbgs() << "Marking Edge Executable: " << Source->getName()
585                       << " -> " << Dest->getName() << '\n');
586 
587     for (PHINode &PN : Dest->phis())
588       visitPHINode(PN);
589   }
590   return true;
591 }
592 
593 // getFeasibleSuccessors - Return a vector of booleans to indicate which
594 // successors are reachable from a given terminator instruction.
595 void SCCPInstVisitor::getFeasibleSuccessors(Instruction &TI,
596                                             SmallVectorImpl<bool> &Succs) {
597   Succs.resize(TI.getNumSuccessors());
598   if (auto *BI = dyn_cast<BranchInst>(&TI)) {
599     if (BI->isUnconditional()) {
600       Succs[0] = true;
601       return;
602     }
603 
604     ValueLatticeElement BCValue = getValueState(BI->getCondition());
605     ConstantInt *CI = getConstantInt(BCValue);
606     if (!CI) {
607       // Overdefined condition variables, and branches on unfoldable constant
608       // conditions, mean the branch could go either way.
609       if (!BCValue.isUnknownOrUndef())
610         Succs[0] = Succs[1] = true;
611       return;
612     }
613 
614     // Constant condition variables mean the branch can only go a single way.
615     Succs[CI->isZero()] = true;
616     return;
617   }
618 
619   // Unwinding instructions successors are always executable.
620   if (TI.isExceptionalTerminator()) {
621     Succs.assign(TI.getNumSuccessors(), true);
622     return;
623   }
624 
625   if (auto *SI = dyn_cast<SwitchInst>(&TI)) {
626     if (!SI->getNumCases()) {
627       Succs[0] = true;
628       return;
629     }
630     const ValueLatticeElement &SCValue = getValueState(SI->getCondition());
631     if (ConstantInt *CI = getConstantInt(SCValue)) {
632       Succs[SI->findCaseValue(CI)->getSuccessorIndex()] = true;
633       return;
634     }
635 
636     // TODO: Switch on undef is UB. Stop passing false once the rest of LLVM
637     // is ready.
638     if (SCValue.isConstantRange(/*UndefAllowed=*/false)) {
639       const ConstantRange &Range = SCValue.getConstantRange();
640       for (const auto &Case : SI->cases()) {
641         const APInt &CaseValue = Case.getCaseValue()->getValue();
642         if (Range.contains(CaseValue))
643           Succs[Case.getSuccessorIndex()] = true;
644       }
645 
646       // TODO: Determine whether default case is reachable.
647       Succs[SI->case_default()->getSuccessorIndex()] = true;
648       return;
649     }
650 
651     // Overdefined or unknown condition? All destinations are executable!
652     if (!SCValue.isUnknownOrUndef())
653       Succs.assign(TI.getNumSuccessors(), true);
654     return;
655   }
656 
657   // In case of indirect branch and its address is a blockaddress, we mark
658   // the target as executable.
659   if (auto *IBR = dyn_cast<IndirectBrInst>(&TI)) {
660     // Casts are folded by visitCastInst.
661     ValueLatticeElement IBRValue = getValueState(IBR->getAddress());
662     BlockAddress *Addr = dyn_cast_or_null<BlockAddress>(getConstant(IBRValue));
663     if (!Addr) { // Overdefined or unknown condition?
664       // All destinations are executable!
665       if (!IBRValue.isUnknownOrUndef())
666         Succs.assign(TI.getNumSuccessors(), true);
667       return;
668     }
669 
670     BasicBlock *T = Addr->getBasicBlock();
671     assert(Addr->getFunction() == T->getParent() &&
672            "Block address of a different function ?");
673     for (unsigned i = 0; i < IBR->getNumSuccessors(); ++i) {
674       // This is the target.
675       if (IBR->getDestination(i) == T) {
676         Succs[i] = true;
677         return;
678       }
679     }
680 
681     // If we didn't find our destination in the IBR successor list, then we
682     // have undefined behavior. Its ok to assume no successor is executable.
683     return;
684   }
685 
686   // In case of callbr, we pessimistically assume that all successors are
687   // feasible.
688   if (isa<CallBrInst>(&TI)) {
689     Succs.assign(TI.getNumSuccessors(), true);
690     return;
691   }
692 
693   LLVM_DEBUG(dbgs() << "Unknown terminator instruction: " << TI << '\n');
694   llvm_unreachable("SCCP: Don't know how to handle this terminator!");
695 }
696 
697 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
698 // block to the 'To' basic block is currently feasible.
699 bool SCCPInstVisitor::isEdgeFeasible(BasicBlock *From, BasicBlock *To) const {
700   // Check if we've called markEdgeExecutable on the edge yet. (We could
701   // be more aggressive and try to consider edges which haven't been marked
702   // yet, but there isn't any need.)
703   return KnownFeasibleEdges.count(Edge(From, To));
704 }
705 
706 // visit Implementations - Something changed in this instruction, either an
707 // operand made a transition, or the instruction is newly executable.  Change
708 // the value type of I to reflect these changes if appropriate.  This method
709 // makes sure to do the following actions:
710 //
711 // 1. If a phi node merges two constants in, and has conflicting value coming
712 //    from different branches, or if the PHI node merges in an overdefined
713 //    value, then the PHI node becomes overdefined.
714 // 2. If a phi node merges only constants in, and they all agree on value, the
715 //    PHI node becomes a constant value equal to that.
716 // 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
717 // 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
718 // 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
719 // 6. If a conditional branch has a value that is constant, make the selected
720 //    destination executable
721 // 7. If a conditional branch has a value that is overdefined, make all
722 //    successors executable.
723 void SCCPInstVisitor::visitPHINode(PHINode &PN) {
724   // If this PN returns a struct, just mark the result overdefined.
725   // TODO: We could do a lot better than this if code actually uses this.
726   if (PN.getType()->isStructTy())
727     return (void)markOverdefined(&PN);
728 
729   if (getValueState(&PN).isOverdefined())
730     return; // Quick exit
731 
732   // Super-extra-high-degree PHI nodes are unlikely to ever be marked constant,
733   // and slow us down a lot.  Just mark them overdefined.
734   if (PN.getNumIncomingValues() > 64)
735     return (void)markOverdefined(&PN);
736 
737   unsigned NumActiveIncoming = 0;
738 
739   // Look at all of the executable operands of the PHI node.  If any of them
740   // are overdefined, the PHI becomes overdefined as well.  If they are all
741   // constant, and they agree with each other, the PHI becomes the identical
742   // constant.  If they are constant and don't agree, the PHI is a constant
743   // range. If there are no executable operands, the PHI remains unknown.
744   ValueLatticeElement PhiState = getValueState(&PN);
745   for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
746     if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent()))
747       continue;
748 
749     ValueLatticeElement IV = getValueState(PN.getIncomingValue(i));
750     PhiState.mergeIn(IV);
751     NumActiveIncoming++;
752     if (PhiState.isOverdefined())
753       break;
754   }
755 
756   // We allow up to 1 range extension per active incoming value and one
757   // additional extension. Note that we manually adjust the number of range
758   // extensions to match the number of active incoming values. This helps to
759   // limit multiple extensions caused by the same incoming value, if other
760   // incoming values are equal.
761   mergeInValue(&PN, PhiState,
762                ValueLatticeElement::MergeOptions().setMaxWidenSteps(
763                    NumActiveIncoming + 1));
764   ValueLatticeElement &PhiStateRef = getValueState(&PN);
765   PhiStateRef.setNumRangeExtensions(
766       std::max(NumActiveIncoming, PhiStateRef.getNumRangeExtensions()));
767 }
768 
769 void SCCPInstVisitor::visitReturnInst(ReturnInst &I) {
770   if (I.getNumOperands() == 0)
771     return; // ret void
772 
773   Function *F = I.getParent()->getParent();
774   Value *ResultOp = I.getOperand(0);
775 
776   // If we are tracking the return value of this function, merge it in.
777   if (!TrackedRetVals.empty() && !ResultOp->getType()->isStructTy()) {
778     auto TFRVI = TrackedRetVals.find(F);
779     if (TFRVI != TrackedRetVals.end()) {
780       mergeInValue(TFRVI->second, F, getValueState(ResultOp));
781       return;
782     }
783   }
784 
785   // Handle functions that return multiple values.
786   if (!TrackedMultipleRetVals.empty()) {
787     if (auto *STy = dyn_cast<StructType>(ResultOp->getType()))
788       if (MRVFunctionsTracked.count(F))
789         for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
790           mergeInValue(TrackedMultipleRetVals[std::make_pair(F, i)], F,
791                        getStructValueState(ResultOp, i));
792   }
793 }
794 
795 void SCCPInstVisitor::visitTerminator(Instruction &TI) {
796   SmallVector<bool, 16> SuccFeasible;
797   getFeasibleSuccessors(TI, SuccFeasible);
798 
799   BasicBlock *BB = TI.getParent();
800 
801   // Mark all feasible successors executable.
802   for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
803     if (SuccFeasible[i])
804       markEdgeExecutable(BB, TI.getSuccessor(i));
805 }
806 
807 void SCCPInstVisitor::visitCastInst(CastInst &I) {
808   // ResolvedUndefsIn might mark I as overdefined. Bail out, even if we would
809   // discover a concrete value later.
810   if (ValueState[&I].isOverdefined())
811     return;
812 
813   ValueLatticeElement OpSt = getValueState(I.getOperand(0));
814   if (OpSt.isUnknownOrUndef())
815     return;
816 
817   if (Constant *OpC = getConstant(OpSt)) {
818     // Fold the constant as we build.
819     Constant *C = ConstantFoldCastOperand(I.getOpcode(), OpC, I.getType(), DL);
820     if (isa<UndefValue>(C))
821       return;
822     // Propagate constant value
823     markConstant(&I, C);
824   } else if (I.getDestTy()->isIntegerTy()) {
825     auto &LV = getValueState(&I);
826     ConstantRange OpRange =
827         OpSt.isConstantRange()
828             ? OpSt.getConstantRange()
829             : ConstantRange::getFull(
830                   I.getOperand(0)->getType()->getScalarSizeInBits());
831 
832     Type *DestTy = I.getDestTy();
833     // Vectors where all elements have the same known constant range are treated
834     // as a single constant range in the lattice. When bitcasting such vectors,
835     // there is a mis-match between the width of the lattice value (single
836     // constant range) and the original operands (vector). Go to overdefined in
837     // that case.
838     if (I.getOpcode() == Instruction::BitCast &&
839         I.getOperand(0)->getType()->isVectorTy() &&
840         OpRange.getBitWidth() < DL.getTypeSizeInBits(DestTy))
841       return (void)markOverdefined(&I);
842 
843     ConstantRange Res =
844         OpRange.castOp(I.getOpcode(), DL.getTypeSizeInBits(DestTy));
845     mergeInValue(LV, &I, ValueLatticeElement::getRange(Res));
846   } else
847     markOverdefined(&I);
848 }
849 
850 void SCCPInstVisitor::visitExtractValueInst(ExtractValueInst &EVI) {
851   // If this returns a struct, mark all elements over defined, we don't track
852   // structs in structs.
853   if (EVI.getType()->isStructTy())
854     return (void)markOverdefined(&EVI);
855 
856   // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
857   // discover a concrete value later.
858   if (ValueState[&EVI].isOverdefined())
859     return (void)markOverdefined(&EVI);
860 
861   // If this is extracting from more than one level of struct, we don't know.
862   if (EVI.getNumIndices() != 1)
863     return (void)markOverdefined(&EVI);
864 
865   Value *AggVal = EVI.getAggregateOperand();
866   if (AggVal->getType()->isStructTy()) {
867     unsigned i = *EVI.idx_begin();
868     ValueLatticeElement EltVal = getStructValueState(AggVal, i);
869     mergeInValue(getValueState(&EVI), &EVI, EltVal);
870   } else {
871     // Otherwise, must be extracting from an array.
872     return (void)markOverdefined(&EVI);
873   }
874 }
875 
876 void SCCPInstVisitor::visitInsertValueInst(InsertValueInst &IVI) {
877   auto *STy = dyn_cast<StructType>(IVI.getType());
878   if (!STy)
879     return (void)markOverdefined(&IVI);
880 
881   // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
882   // discover a concrete value later.
883   if (isOverdefined(ValueState[&IVI]))
884     return (void)markOverdefined(&IVI);
885 
886   // If this has more than one index, we can't handle it, drive all results to
887   // undef.
888   if (IVI.getNumIndices() != 1)
889     return (void)markOverdefined(&IVI);
890 
891   Value *Aggr = IVI.getAggregateOperand();
892   unsigned Idx = *IVI.idx_begin();
893 
894   // Compute the result based on what we're inserting.
895   for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
896     // This passes through all values that aren't the inserted element.
897     if (i != Idx) {
898       ValueLatticeElement EltVal = getStructValueState(Aggr, i);
899       mergeInValue(getStructValueState(&IVI, i), &IVI, EltVal);
900       continue;
901     }
902 
903     Value *Val = IVI.getInsertedValueOperand();
904     if (Val->getType()->isStructTy())
905       // We don't track structs in structs.
906       markOverdefined(getStructValueState(&IVI, i), &IVI);
907     else {
908       ValueLatticeElement InVal = getValueState(Val);
909       mergeInValue(getStructValueState(&IVI, i), &IVI, InVal);
910     }
911   }
912 }
913 
914 void SCCPInstVisitor::visitSelectInst(SelectInst &I) {
915   // If this select returns a struct, just mark the result overdefined.
916   // TODO: We could do a lot better than this if code actually uses this.
917   if (I.getType()->isStructTy())
918     return (void)markOverdefined(&I);
919 
920   // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
921   // discover a concrete value later.
922   if (ValueState[&I].isOverdefined())
923     return (void)markOverdefined(&I);
924 
925   ValueLatticeElement CondValue = getValueState(I.getCondition());
926   if (CondValue.isUnknownOrUndef())
927     return;
928 
929   if (ConstantInt *CondCB = getConstantInt(CondValue)) {
930     Value *OpVal = CondCB->isZero() ? I.getFalseValue() : I.getTrueValue();
931     mergeInValue(&I, getValueState(OpVal));
932     return;
933   }
934 
935   // Otherwise, the condition is overdefined or a constant we can't evaluate.
936   // See if we can produce something better than overdefined based on the T/F
937   // value.
938   ValueLatticeElement TVal = getValueState(I.getTrueValue());
939   ValueLatticeElement FVal = getValueState(I.getFalseValue());
940 
941   bool Changed = ValueState[&I].mergeIn(TVal);
942   Changed |= ValueState[&I].mergeIn(FVal);
943   if (Changed)
944     pushToWorkListMsg(ValueState[&I], &I);
945 }
946 
947 // Handle Unary Operators.
948 void SCCPInstVisitor::visitUnaryOperator(Instruction &I) {
949   ValueLatticeElement V0State = getValueState(I.getOperand(0));
950 
951   ValueLatticeElement &IV = ValueState[&I];
952   // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
953   // discover a concrete value later.
954   if (isOverdefined(IV))
955     return (void)markOverdefined(&I);
956 
957   if (isConstant(V0State)) {
958     Constant *C = ConstantExpr::get(I.getOpcode(), getConstant(V0State));
959 
960     // op Y -> undef.
961     if (isa<UndefValue>(C))
962       return;
963     return (void)markConstant(IV, &I, C);
964   }
965 
966   // If something is undef, wait for it to resolve.
967   if (!isOverdefined(V0State))
968     return;
969 
970   markOverdefined(&I);
971 }
972 
973 // Handle Binary Operators.
974 void SCCPInstVisitor::visitBinaryOperator(Instruction &I) {
975   ValueLatticeElement V1State = getValueState(I.getOperand(0));
976   ValueLatticeElement V2State = getValueState(I.getOperand(1));
977 
978   ValueLatticeElement &IV = ValueState[&I];
979   if (IV.isOverdefined())
980     return;
981 
982   // If something is undef, wait for it to resolve.
983   if (V1State.isUnknownOrUndef() || V2State.isUnknownOrUndef())
984     return;
985 
986   if (V1State.isOverdefined() && V2State.isOverdefined())
987     return (void)markOverdefined(&I);
988 
989   // If either of the operands is a constant, try to fold it to a constant.
990   // TODO: Use information from notconstant better.
991   if ((V1State.isConstant() || V2State.isConstant())) {
992     Value *V1 = isConstant(V1State) ? getConstant(V1State) : I.getOperand(0);
993     Value *V2 = isConstant(V2State) ? getConstant(V2State) : I.getOperand(1);
994     Value *R = SimplifyBinOp(I.getOpcode(), V1, V2, SimplifyQuery(DL));
995     auto *C = dyn_cast_or_null<Constant>(R);
996     if (C) {
997       // X op Y -> undef.
998       if (isa<UndefValue>(C))
999         return;
1000       // Conservatively assume that the result may be based on operands that may
1001       // be undef. Note that we use mergeInValue to combine the constant with
1002       // the existing lattice value for I, as different constants might be found
1003       // after one of the operands go to overdefined, e.g. due to one operand
1004       // being a special floating value.
1005       ValueLatticeElement NewV;
1006       NewV.markConstant(C, /*MayIncludeUndef=*/true);
1007       return (void)mergeInValue(&I, NewV);
1008     }
1009   }
1010 
1011   // Only use ranges for binary operators on integers.
1012   if (!I.getType()->isIntegerTy())
1013     return markOverdefined(&I);
1014 
1015   // Try to simplify to a constant range.
1016   ConstantRange A = ConstantRange::getFull(I.getType()->getScalarSizeInBits());
1017   ConstantRange B = ConstantRange::getFull(I.getType()->getScalarSizeInBits());
1018   if (V1State.isConstantRange())
1019     A = V1State.getConstantRange();
1020   if (V2State.isConstantRange())
1021     B = V2State.getConstantRange();
1022 
1023   ConstantRange R = A.binaryOp(cast<BinaryOperator>(&I)->getOpcode(), B);
1024   mergeInValue(&I, ValueLatticeElement::getRange(R));
1025 
1026   // TODO: Currently we do not exploit special values that produce something
1027   // better than overdefined with an overdefined operand for vector or floating
1028   // point types, like and <4 x i32> overdefined, zeroinitializer.
1029 }
1030 
1031 // Handle ICmpInst instruction.
1032 void SCCPInstVisitor::visitCmpInst(CmpInst &I) {
1033   // Do not cache this lookup, getValueState calls later in the function might
1034   // invalidate the reference.
1035   if (isOverdefined(ValueState[&I]))
1036     return (void)markOverdefined(&I);
1037 
1038   Value *Op1 = I.getOperand(0);
1039   Value *Op2 = I.getOperand(1);
1040 
1041   // For parameters, use ParamState which includes constant range info if
1042   // available.
1043   auto V1State = getValueState(Op1);
1044   auto V2State = getValueState(Op2);
1045 
1046   Constant *C = V1State.getCompare(I.getPredicate(), I.getType(), V2State);
1047   if (C) {
1048     if (isa<UndefValue>(C))
1049       return;
1050     ValueLatticeElement CV;
1051     CV.markConstant(C);
1052     mergeInValue(&I, CV);
1053     return;
1054   }
1055 
1056   // If operands are still unknown, wait for it to resolve.
1057   if ((V1State.isUnknownOrUndef() || V2State.isUnknownOrUndef()) &&
1058       !isConstant(ValueState[&I]))
1059     return;
1060 
1061   markOverdefined(&I);
1062 }
1063 
1064 // Handle getelementptr instructions.  If all operands are constants then we
1065 // can turn this into a getelementptr ConstantExpr.
1066 void SCCPInstVisitor::visitGetElementPtrInst(GetElementPtrInst &I) {
1067   if (isOverdefined(ValueState[&I]))
1068     return (void)markOverdefined(&I);
1069 
1070   SmallVector<Constant *, 8> Operands;
1071   Operands.reserve(I.getNumOperands());
1072 
1073   for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
1074     ValueLatticeElement State = getValueState(I.getOperand(i));
1075     if (State.isUnknownOrUndef())
1076       return; // Operands are not resolved yet.
1077 
1078     if (isOverdefined(State))
1079       return (void)markOverdefined(&I);
1080 
1081     if (Constant *C = getConstant(State)) {
1082       Operands.push_back(C);
1083       continue;
1084     }
1085 
1086     return (void)markOverdefined(&I);
1087   }
1088 
1089   Constant *Ptr = Operands[0];
1090   auto Indices = makeArrayRef(Operands.begin() + 1, Operands.end());
1091   Constant *C =
1092       ConstantExpr::getGetElementPtr(I.getSourceElementType(), Ptr, Indices);
1093   if (isa<UndefValue>(C))
1094     return;
1095   markConstant(&I, C);
1096 }
1097 
1098 void SCCPInstVisitor::visitStoreInst(StoreInst &SI) {
1099   // If this store is of a struct, ignore it.
1100   if (SI.getOperand(0)->getType()->isStructTy())
1101     return;
1102 
1103   if (TrackedGlobals.empty() || !isa<GlobalVariable>(SI.getOperand(1)))
1104     return;
1105 
1106   GlobalVariable *GV = cast<GlobalVariable>(SI.getOperand(1));
1107   auto I = TrackedGlobals.find(GV);
1108   if (I == TrackedGlobals.end())
1109     return;
1110 
1111   // Get the value we are storing into the global, then merge it.
1112   mergeInValue(I->second, GV, getValueState(SI.getOperand(0)),
1113                ValueLatticeElement::MergeOptions().setCheckWiden(false));
1114   if (I->second.isOverdefined())
1115     TrackedGlobals.erase(I); // No need to keep tracking this!
1116 }
1117 
1118 static ValueLatticeElement getValueFromMetadata(const Instruction *I) {
1119   if (MDNode *Ranges = I->getMetadata(LLVMContext::MD_range))
1120     if (I->getType()->isIntegerTy())
1121       return ValueLatticeElement::getRange(
1122           getConstantRangeFromMetadata(*Ranges));
1123   if (I->hasMetadata(LLVMContext::MD_nonnull))
1124     return ValueLatticeElement::getNot(
1125         ConstantPointerNull::get(cast<PointerType>(I->getType())));
1126   return ValueLatticeElement::getOverdefined();
1127 }
1128 
1129 // Handle load instructions.  If the operand is a constant pointer to a constant
1130 // global, we can replace the load with the loaded constant value!
1131 void SCCPInstVisitor::visitLoadInst(LoadInst &I) {
1132   // If this load is of a struct or the load is volatile, just mark the result
1133   // as overdefined.
1134   if (I.getType()->isStructTy() || I.isVolatile())
1135     return (void)markOverdefined(&I);
1136 
1137   // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1138   // discover a concrete value later.
1139   if (ValueState[&I].isOverdefined())
1140     return (void)markOverdefined(&I);
1141 
1142   ValueLatticeElement PtrVal = getValueState(I.getOperand(0));
1143   if (PtrVal.isUnknownOrUndef())
1144     return; // The pointer is not resolved yet!
1145 
1146   ValueLatticeElement &IV = ValueState[&I];
1147 
1148   if (isConstant(PtrVal)) {
1149     Constant *Ptr = getConstant(PtrVal);
1150 
1151     // load null is undefined.
1152     if (isa<ConstantPointerNull>(Ptr)) {
1153       if (NullPointerIsDefined(I.getFunction(), I.getPointerAddressSpace()))
1154         return (void)markOverdefined(IV, &I);
1155       else
1156         return;
1157     }
1158 
1159     // Transform load (constant global) into the value loaded.
1160     if (auto *GV = dyn_cast<GlobalVariable>(Ptr)) {
1161       if (!TrackedGlobals.empty()) {
1162         // If we are tracking this global, merge in the known value for it.
1163         auto It = TrackedGlobals.find(GV);
1164         if (It != TrackedGlobals.end()) {
1165           mergeInValue(IV, &I, It->second, getMaxWidenStepsOpts());
1166           return;
1167         }
1168       }
1169     }
1170 
1171     // Transform load from a constant into a constant if possible.
1172     if (Constant *C = ConstantFoldLoadFromConstPtr(Ptr, I.getType(), DL)) {
1173       if (isa<UndefValue>(C))
1174         return;
1175       return (void)markConstant(IV, &I, C);
1176     }
1177   }
1178 
1179   // Fall back to metadata.
1180   mergeInValue(&I, getValueFromMetadata(&I));
1181 }
1182 
1183 void SCCPInstVisitor::visitCallBase(CallBase &CB) {
1184   handleCallResult(CB);
1185   handleCallArguments(CB);
1186 }
1187 
1188 void SCCPInstVisitor::handleCallOverdefined(CallBase &CB) {
1189   Function *F = CB.getCalledFunction();
1190 
1191   // Void return and not tracking callee, just bail.
1192   if (CB.getType()->isVoidTy())
1193     return;
1194 
1195   // Always mark struct return as overdefined.
1196   if (CB.getType()->isStructTy())
1197     return (void)markOverdefined(&CB);
1198 
1199   // Otherwise, if we have a single return value case, and if the function is
1200   // a declaration, maybe we can constant fold it.
1201   if (F && F->isDeclaration() && canConstantFoldCallTo(&CB, F)) {
1202     SmallVector<Constant *, 8> Operands;
1203     for (const Use &A : CB.args()) {
1204       if (A.get()->getType()->isStructTy())
1205         return markOverdefined(&CB); // Can't handle struct args.
1206       ValueLatticeElement State = getValueState(A);
1207 
1208       if (State.isUnknownOrUndef())
1209         return; // Operands are not resolved yet.
1210       if (isOverdefined(State))
1211         return (void)markOverdefined(&CB);
1212       assert(isConstant(State) && "Unknown state!");
1213       Operands.push_back(getConstant(State));
1214     }
1215 
1216     if (isOverdefined(getValueState(&CB)))
1217       return (void)markOverdefined(&CB);
1218 
1219     // If we can constant fold this, mark the result of the call as a
1220     // constant.
1221     if (Constant *C = ConstantFoldCall(&CB, F, Operands, &GetTLI(*F))) {
1222       // call -> undef.
1223       if (isa<UndefValue>(C))
1224         return;
1225       return (void)markConstant(&CB, C);
1226     }
1227   }
1228 
1229   // Fall back to metadata.
1230   mergeInValue(&CB, getValueFromMetadata(&CB));
1231 }
1232 
1233 void SCCPInstVisitor::handleCallArguments(CallBase &CB) {
1234   Function *F = CB.getCalledFunction();
1235   // If this is a local function that doesn't have its address taken, mark its
1236   // entry block executable and merge in the actual arguments to the call into
1237   // the formal arguments of the function.
1238   if (!TrackingIncomingArguments.empty() &&
1239       TrackingIncomingArguments.count(F)) {
1240     markBlockExecutable(&F->front());
1241 
1242     // Propagate information from this call site into the callee.
1243     auto CAI = CB.arg_begin();
1244     for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E;
1245          ++AI, ++CAI) {
1246       // If this argument is byval, and if the function is not readonly, there
1247       // will be an implicit copy formed of the input aggregate.
1248       if (AI->hasByValAttr() && !F->onlyReadsMemory()) {
1249         markOverdefined(&*AI);
1250         continue;
1251       }
1252 
1253       if (auto *STy = dyn_cast<StructType>(AI->getType())) {
1254         for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1255           ValueLatticeElement CallArg = getStructValueState(*CAI, i);
1256           mergeInValue(getStructValueState(&*AI, i), &*AI, CallArg,
1257                        getMaxWidenStepsOpts());
1258         }
1259       } else
1260         mergeInValue(&*AI, getValueState(*CAI), getMaxWidenStepsOpts());
1261     }
1262   }
1263 }
1264 
1265 void SCCPInstVisitor::handleCallResult(CallBase &CB) {
1266   Function *F = CB.getCalledFunction();
1267 
1268   if (auto *II = dyn_cast<IntrinsicInst>(&CB)) {
1269     if (II->getIntrinsicID() == Intrinsic::ssa_copy) {
1270       if (ValueState[&CB].isOverdefined())
1271         return;
1272 
1273       Value *CopyOf = CB.getOperand(0);
1274       ValueLatticeElement CopyOfVal = getValueState(CopyOf);
1275       const auto *PI = getPredicateInfoFor(&CB);
1276       assert(PI && "Missing predicate info for ssa.copy");
1277 
1278       const Optional<PredicateConstraint> &Constraint = PI->getConstraint();
1279       if (!Constraint) {
1280         mergeInValue(ValueState[&CB], &CB, CopyOfVal);
1281         return;
1282       }
1283 
1284       CmpInst::Predicate Pred = Constraint->Predicate;
1285       Value *OtherOp = Constraint->OtherOp;
1286 
1287       // Wait until OtherOp is resolved.
1288       if (getValueState(OtherOp).isUnknown()) {
1289         addAdditionalUser(OtherOp, &CB);
1290         return;
1291       }
1292 
1293       // TODO: Actually filp MayIncludeUndef for the created range to false,
1294       // once most places in the optimizer respect the branches on
1295       // undef/poison are UB rule. The reason why the new range cannot be
1296       // undef is as follows below:
1297       // The new range is based on a branch condition. That guarantees that
1298       // neither of the compare operands can be undef in the branch targets,
1299       // unless we have conditions that are always true/false (e.g. icmp ule
1300       // i32, %a, i32_max). For the latter overdefined/empty range will be
1301       // inferred, but the branch will get folded accordingly anyways.
1302       bool MayIncludeUndef = !isa<PredicateAssume>(PI);
1303 
1304       ValueLatticeElement CondVal = getValueState(OtherOp);
1305       ValueLatticeElement &IV = ValueState[&CB];
1306       if (CondVal.isConstantRange() || CopyOfVal.isConstantRange()) {
1307         auto ImposedCR =
1308             ConstantRange::getFull(DL.getTypeSizeInBits(CopyOf->getType()));
1309 
1310         // Get the range imposed by the condition.
1311         if (CondVal.isConstantRange())
1312           ImposedCR = ConstantRange::makeAllowedICmpRegion(
1313               Pred, CondVal.getConstantRange());
1314 
1315         // Combine range info for the original value with the new range from the
1316         // condition.
1317         auto CopyOfCR = CopyOfVal.isConstantRange()
1318                             ? CopyOfVal.getConstantRange()
1319                             : ConstantRange::getFull(
1320                                   DL.getTypeSizeInBits(CopyOf->getType()));
1321         auto NewCR = ImposedCR.intersectWith(CopyOfCR);
1322         // If the existing information is != x, do not use the information from
1323         // a chained predicate, as the != x information is more likely to be
1324         // helpful in practice.
1325         if (!CopyOfCR.contains(NewCR) && CopyOfCR.getSingleMissingElement())
1326           NewCR = CopyOfCR;
1327 
1328         addAdditionalUser(OtherOp, &CB);
1329         mergeInValue(IV, &CB,
1330                      ValueLatticeElement::getRange(NewCR, MayIncludeUndef));
1331         return;
1332       } else if (Pred == CmpInst::ICMP_EQ && CondVal.isConstant()) {
1333         // For non-integer values or integer constant expressions, only
1334         // propagate equal constants.
1335         addAdditionalUser(OtherOp, &CB);
1336         mergeInValue(IV, &CB, CondVal);
1337         return;
1338       } else if (Pred == CmpInst::ICMP_NE && CondVal.isConstant() &&
1339                  !MayIncludeUndef) {
1340         // Propagate inequalities.
1341         addAdditionalUser(OtherOp, &CB);
1342         mergeInValue(IV, &CB,
1343                      ValueLatticeElement::getNot(CondVal.getConstant()));
1344         return;
1345       }
1346 
1347       return (void)mergeInValue(IV, &CB, CopyOfVal);
1348     }
1349 
1350     if (ConstantRange::isIntrinsicSupported(II->getIntrinsicID())) {
1351       // Compute result range for intrinsics supported by ConstantRange.
1352       // Do this even if we don't know a range for all operands, as we may
1353       // still know something about the result range, e.g. of abs(x).
1354       SmallVector<ConstantRange, 2> OpRanges;
1355       for (Value *Op : II->args()) {
1356         const ValueLatticeElement &State = getValueState(Op);
1357         if (State.isConstantRange())
1358           OpRanges.push_back(State.getConstantRange());
1359         else
1360           OpRanges.push_back(
1361               ConstantRange::getFull(Op->getType()->getScalarSizeInBits()));
1362       }
1363 
1364       ConstantRange Result =
1365           ConstantRange::intrinsic(II->getIntrinsicID(), OpRanges);
1366       return (void)mergeInValue(II, ValueLatticeElement::getRange(Result));
1367     }
1368   }
1369 
1370   // The common case is that we aren't tracking the callee, either because we
1371   // are not doing interprocedural analysis or the callee is indirect, or is
1372   // external.  Handle these cases first.
1373   if (!F || F->isDeclaration())
1374     return handleCallOverdefined(CB);
1375 
1376   // If this is a single/zero retval case, see if we're tracking the function.
1377   if (auto *STy = dyn_cast<StructType>(F->getReturnType())) {
1378     if (!MRVFunctionsTracked.count(F))
1379       return handleCallOverdefined(CB); // Not tracking this callee.
1380 
1381     // If we are tracking this callee, propagate the result of the function
1382     // into this call site.
1383     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
1384       mergeInValue(getStructValueState(&CB, i), &CB,
1385                    TrackedMultipleRetVals[std::make_pair(F, i)],
1386                    getMaxWidenStepsOpts());
1387   } else {
1388     auto TFRVI = TrackedRetVals.find(F);
1389     if (TFRVI == TrackedRetVals.end())
1390       return handleCallOverdefined(CB); // Not tracking this callee.
1391 
1392     // If so, propagate the return value of the callee into this call result.
1393     mergeInValue(&CB, TFRVI->second, getMaxWidenStepsOpts());
1394   }
1395 }
1396 
1397 void SCCPInstVisitor::solve() {
1398   // Process the work lists until they are empty!
1399   while (!BBWorkList.empty() || !InstWorkList.empty() ||
1400          !OverdefinedInstWorkList.empty()) {
1401     // Process the overdefined instruction's work list first, which drives other
1402     // things to overdefined more quickly.
1403     while (!OverdefinedInstWorkList.empty()) {
1404       Value *I = OverdefinedInstWorkList.pop_back_val();
1405 
1406       LLVM_DEBUG(dbgs() << "\nPopped off OI-WL: " << *I << '\n');
1407 
1408       // "I" got into the work list because it either made the transition from
1409       // bottom to constant, or to overdefined.
1410       //
1411       // Anything on this worklist that is overdefined need not be visited
1412       // since all of its users will have already been marked as overdefined
1413       // Update all of the users of this instruction's value.
1414       //
1415       markUsersAsChanged(I);
1416     }
1417 
1418     // Process the instruction work list.
1419     while (!InstWorkList.empty()) {
1420       Value *I = InstWorkList.pop_back_val();
1421 
1422       LLVM_DEBUG(dbgs() << "\nPopped off I-WL: " << *I << '\n');
1423 
1424       // "I" got into the work list because it made the transition from undef to
1425       // constant.
1426       //
1427       // Anything on this worklist that is overdefined need not be visited
1428       // since all of its users will have already been marked as overdefined.
1429       // Update all of the users of this instruction's value.
1430       //
1431       if (I->getType()->isStructTy() || !getValueState(I).isOverdefined())
1432         markUsersAsChanged(I);
1433     }
1434 
1435     // Process the basic block work list.
1436     while (!BBWorkList.empty()) {
1437       BasicBlock *BB = BBWorkList.pop_back_val();
1438 
1439       LLVM_DEBUG(dbgs() << "\nPopped off BBWL: " << *BB << '\n');
1440 
1441       // Notify all instructions in this basic block that they are newly
1442       // executable.
1443       visit(BB);
1444     }
1445   }
1446 }
1447 
1448 /// resolvedUndefsIn - While solving the dataflow for a function, we assume
1449 /// that branches on undef values cannot reach any of their successors.
1450 /// However, this is not a safe assumption.  After we solve dataflow, this
1451 /// method should be use to handle this.  If this returns true, the solver
1452 /// should be rerun.
1453 ///
1454 /// This method handles this by finding an unresolved branch and marking it one
1455 /// of the edges from the block as being feasible, even though the condition
1456 /// doesn't say it would otherwise be.  This allows SCCP to find the rest of the
1457 /// CFG and only slightly pessimizes the analysis results (by marking one,
1458 /// potentially infeasible, edge feasible).  This cannot usefully modify the
1459 /// constraints on the condition of the branch, as that would impact other users
1460 /// of the value.
1461 ///
1462 /// This scan also checks for values that use undefs. It conservatively marks
1463 /// them as overdefined.
1464 bool SCCPInstVisitor::resolvedUndefsIn(Function &F) {
1465   bool MadeChange = false;
1466   for (BasicBlock &BB : F) {
1467     if (!BBExecutable.count(&BB))
1468       continue;
1469 
1470     for (Instruction &I : BB) {
1471       // Look for instructions which produce undef values.
1472       if (I.getType()->isVoidTy())
1473         continue;
1474 
1475       if (auto *STy = dyn_cast<StructType>(I.getType())) {
1476         // Only a few things that can be structs matter for undef.
1477 
1478         // Tracked calls must never be marked overdefined in resolvedUndefsIn.
1479         if (auto *CB = dyn_cast<CallBase>(&I))
1480           if (Function *F = CB->getCalledFunction())
1481             if (MRVFunctionsTracked.count(F))
1482               continue;
1483 
1484         // extractvalue and insertvalue don't need to be marked; they are
1485         // tracked as precisely as their operands.
1486         if (isa<ExtractValueInst>(I) || isa<InsertValueInst>(I))
1487           continue;
1488         // Send the results of everything else to overdefined.  We could be
1489         // more precise than this but it isn't worth bothering.
1490         for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1491           ValueLatticeElement &LV = getStructValueState(&I, i);
1492           if (LV.isUnknownOrUndef()) {
1493             markOverdefined(LV, &I);
1494             MadeChange = true;
1495           }
1496         }
1497         continue;
1498       }
1499 
1500       ValueLatticeElement &LV = getValueState(&I);
1501       if (!LV.isUnknownOrUndef())
1502         continue;
1503 
1504       // There are two reasons a call can have an undef result
1505       // 1. It could be tracked.
1506       // 2. It could be constant-foldable.
1507       // Because of the way we solve return values, tracked calls must
1508       // never be marked overdefined in resolvedUndefsIn.
1509       if (auto *CB = dyn_cast<CallBase>(&I))
1510         if (Function *F = CB->getCalledFunction())
1511           if (TrackedRetVals.count(F))
1512             continue;
1513 
1514       if (isa<LoadInst>(I)) {
1515         // A load here means one of two things: a load of undef from a global,
1516         // a load from an unknown pointer.  Either way, having it return undef
1517         // is okay.
1518         continue;
1519       }
1520 
1521       markOverdefined(&I);
1522       MadeChange = true;
1523     }
1524 
1525     // Check to see if we have a branch or switch on an undefined value.  If so
1526     // we force the branch to go one way or the other to make the successor
1527     // values live.  It doesn't really matter which way we force it.
1528     Instruction *TI = BB.getTerminator();
1529     if (auto *BI = dyn_cast<BranchInst>(TI)) {
1530       if (!BI->isConditional())
1531         continue;
1532       if (!getValueState(BI->getCondition()).isUnknownOrUndef())
1533         continue;
1534 
1535       // If the input to SCCP is actually branch on undef, fix the undef to
1536       // false.
1537       if (isa<UndefValue>(BI->getCondition())) {
1538         BI->setCondition(ConstantInt::getFalse(BI->getContext()));
1539         markEdgeExecutable(&BB, TI->getSuccessor(1));
1540         MadeChange = true;
1541         continue;
1542       }
1543 
1544       // Otherwise, it is a branch on a symbolic value which is currently
1545       // considered to be undef.  Make sure some edge is executable, so a
1546       // branch on "undef" always flows somewhere.
1547       // FIXME: Distinguish between dead code and an LLVM "undef" value.
1548       BasicBlock *DefaultSuccessor = TI->getSuccessor(1);
1549       if (markEdgeExecutable(&BB, DefaultSuccessor))
1550         MadeChange = true;
1551 
1552       continue;
1553     }
1554 
1555     if (auto *IBR = dyn_cast<IndirectBrInst>(TI)) {
1556       // Indirect branch with no successor ?. Its ok to assume it branches
1557       // to no target.
1558       if (IBR->getNumSuccessors() < 1)
1559         continue;
1560 
1561       if (!getValueState(IBR->getAddress()).isUnknownOrUndef())
1562         continue;
1563 
1564       // If the input to SCCP is actually branch on undef, fix the undef to
1565       // the first successor of the indirect branch.
1566       if (isa<UndefValue>(IBR->getAddress())) {
1567         IBR->setAddress(BlockAddress::get(IBR->getSuccessor(0)));
1568         markEdgeExecutable(&BB, IBR->getSuccessor(0));
1569         MadeChange = true;
1570         continue;
1571       }
1572 
1573       // Otherwise, it is a branch on a symbolic value which is currently
1574       // considered to be undef.  Make sure some edge is executable, so a
1575       // branch on "undef" always flows somewhere.
1576       // FIXME: IndirectBr on "undef" doesn't actually need to go anywhere:
1577       // we can assume the branch has undefined behavior instead.
1578       BasicBlock *DefaultSuccessor = IBR->getSuccessor(0);
1579       if (markEdgeExecutable(&BB, DefaultSuccessor))
1580         MadeChange = true;
1581 
1582       continue;
1583     }
1584 
1585     if (auto *SI = dyn_cast<SwitchInst>(TI)) {
1586       if (!SI->getNumCases() ||
1587           !getValueState(SI->getCondition()).isUnknownOrUndef())
1588         continue;
1589 
1590       // If the input to SCCP is actually switch on undef, fix the undef to
1591       // the first constant.
1592       if (isa<UndefValue>(SI->getCondition())) {
1593         SI->setCondition(SI->case_begin()->getCaseValue());
1594         markEdgeExecutable(&BB, SI->case_begin()->getCaseSuccessor());
1595         MadeChange = true;
1596         continue;
1597       }
1598 
1599       // Otherwise, it is a branch on a symbolic value which is currently
1600       // considered to be undef.  Make sure some edge is executable, so a
1601       // branch on "undef" always flows somewhere.
1602       // FIXME: Distinguish between dead code and an LLVM "undef" value.
1603       BasicBlock *DefaultSuccessor = SI->case_begin()->getCaseSuccessor();
1604       if (markEdgeExecutable(&BB, DefaultSuccessor))
1605         MadeChange = true;
1606 
1607       continue;
1608     }
1609   }
1610 
1611   return MadeChange;
1612 }
1613 
1614 //===----------------------------------------------------------------------===//
1615 //
1616 // SCCPSolver implementations
1617 //
1618 SCCPSolver::SCCPSolver(
1619     const DataLayout &DL,
1620     std::function<const TargetLibraryInfo &(Function &)> GetTLI,
1621     LLVMContext &Ctx)
1622     : Visitor(new SCCPInstVisitor(DL, std::move(GetTLI), Ctx)) {}
1623 
1624 SCCPSolver::~SCCPSolver() = default;
1625 
1626 void SCCPSolver::addAnalysis(Function &F, AnalysisResultsForFn A) {
1627   return Visitor->addAnalysis(F, std::move(A));
1628 }
1629 
1630 bool SCCPSolver::markBlockExecutable(BasicBlock *BB) {
1631   return Visitor->markBlockExecutable(BB);
1632 }
1633 
1634 const PredicateBase *SCCPSolver::getPredicateInfoFor(Instruction *I) {
1635   return Visitor->getPredicateInfoFor(I);
1636 }
1637 
1638 DomTreeUpdater SCCPSolver::getDTU(Function &F) { return Visitor->getDTU(F); }
1639 
1640 void SCCPSolver::trackValueOfGlobalVariable(GlobalVariable *GV) {
1641   Visitor->trackValueOfGlobalVariable(GV);
1642 }
1643 
1644 void SCCPSolver::addTrackedFunction(Function *F) {
1645   Visitor->addTrackedFunction(F);
1646 }
1647 
1648 void SCCPSolver::addToMustPreserveReturnsInFunctions(Function *F) {
1649   Visitor->addToMustPreserveReturnsInFunctions(F);
1650 }
1651 
1652 bool SCCPSolver::mustPreserveReturn(Function *F) {
1653   return Visitor->mustPreserveReturn(F);
1654 }
1655 
1656 void SCCPSolver::addArgumentTrackedFunction(Function *F) {
1657   Visitor->addArgumentTrackedFunction(F);
1658 }
1659 
1660 bool SCCPSolver::isArgumentTrackedFunction(Function *F) {
1661   return Visitor->isArgumentTrackedFunction(F);
1662 }
1663 
1664 void SCCPSolver::solve() { Visitor->solve(); }
1665 
1666 bool SCCPSolver::resolvedUndefsIn(Function &F) {
1667   return Visitor->resolvedUndefsIn(F);
1668 }
1669 
1670 bool SCCPSolver::isBlockExecutable(BasicBlock *BB) const {
1671   return Visitor->isBlockExecutable(BB);
1672 }
1673 
1674 bool SCCPSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To) const {
1675   return Visitor->isEdgeFeasible(From, To);
1676 }
1677 
1678 std::vector<ValueLatticeElement>
1679 SCCPSolver::getStructLatticeValueFor(Value *V) const {
1680   return Visitor->getStructLatticeValueFor(V);
1681 }
1682 
1683 void SCCPSolver::removeLatticeValueFor(Value *V) {
1684   return Visitor->removeLatticeValueFor(V);
1685 }
1686 
1687 const ValueLatticeElement &SCCPSolver::getLatticeValueFor(Value *V) const {
1688   return Visitor->getLatticeValueFor(V);
1689 }
1690 
1691 const MapVector<Function *, ValueLatticeElement> &
1692 SCCPSolver::getTrackedRetVals() {
1693   return Visitor->getTrackedRetVals();
1694 }
1695 
1696 const DenseMap<GlobalVariable *, ValueLatticeElement> &
1697 SCCPSolver::getTrackedGlobals() {
1698   return Visitor->getTrackedGlobals();
1699 }
1700 
1701 const SmallPtrSet<Function *, 16> SCCPSolver::getMRVFunctionsTracked() {
1702   return Visitor->getMRVFunctionsTracked();
1703 }
1704 
1705 void SCCPSolver::markOverdefined(Value *V) { Visitor->markOverdefined(V); }
1706 
1707 bool SCCPSolver::isStructLatticeConstant(Function *F, StructType *STy) {
1708   return Visitor->isStructLatticeConstant(F, STy);
1709 }
1710 
1711 Constant *SCCPSolver::getConstant(const ValueLatticeElement &LV) const {
1712   return Visitor->getConstant(LV);
1713 }
1714 
1715 SmallPtrSetImpl<Function *> &SCCPSolver::getArgumentTrackedFunctions() {
1716   return Visitor->getArgumentTrackedFunctions();
1717 }
1718 
1719 void SCCPSolver::markArgInFuncSpecialization(Function *F, Argument *A,
1720                                              Constant *C) {
1721   Visitor->markArgInFuncSpecialization(F, A, C);
1722 }
1723 
1724 void SCCPSolver::markFunctionUnreachable(Function *F) {
1725   Visitor->markFunctionUnreachable(F);
1726 }
1727 
1728 void SCCPSolver::visit(Instruction *I) { Visitor->visit(I); }
1729 
1730 void SCCPSolver::visitCall(CallInst &I) { Visitor->visitCall(I); }
1731