1 //===- SCCP.cpp - Sparse Conditional Constant Propagation -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements sparse conditional constant propagation and merging:
10 //
11 // Specifically, this:
12 //   * Assumes values are constant unless proven otherwise
13 //   * Assumes BasicBlocks are dead unless proven otherwise
14 //   * Proves values to be constant, and replaces them with constants
15 //   * Proves conditional branches to be unconditional
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #include "llvm/Transforms/Scalar/SCCP.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/DenseSet.h"
23 #include "llvm/ADT/MapVector.h"
24 #include "llvm/ADT/PointerIntPair.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ADT/SmallPtrSet.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/Statistic.h"
29 #include "llvm/Analysis/ConstantFolding.h"
30 #include "llvm/Analysis/DomTreeUpdater.h"
31 #include "llvm/Analysis/GlobalsModRef.h"
32 #include "llvm/Analysis/InstructionSimplify.h"
33 #include "llvm/Analysis/TargetLibraryInfo.h"
34 #include "llvm/Analysis/ValueLattice.h"
35 #include "llvm/Analysis/ValueLatticeUtils.h"
36 #include "llvm/IR/BasicBlock.h"
37 #include "llvm/IR/Constant.h"
38 #include "llvm/IR/Constants.h"
39 #include "llvm/IR/DataLayout.h"
40 #include "llvm/IR/DerivedTypes.h"
41 #include "llvm/IR/Function.h"
42 #include "llvm/IR/GlobalVariable.h"
43 #include "llvm/IR/InstVisitor.h"
44 #include "llvm/IR/InstrTypes.h"
45 #include "llvm/IR/Instruction.h"
46 #include "llvm/IR/Instructions.h"
47 #include "llvm/IR/Module.h"
48 #include "llvm/IR/PassManager.h"
49 #include "llvm/IR/Type.h"
50 #include "llvm/IR/User.h"
51 #include "llvm/IR/Value.h"
52 #include "llvm/InitializePasses.h"
53 #include "llvm/Pass.h"
54 #include "llvm/Support/Casting.h"
55 #include "llvm/Support/Debug.h"
56 #include "llvm/Support/ErrorHandling.h"
57 #include "llvm/Support/raw_ostream.h"
58 #include "llvm/Transforms/Scalar.h"
59 #include "llvm/Transforms/Utils/Local.h"
60 #include "llvm/Transforms/Utils/PredicateInfo.h"
61 #include <cassert>
62 #include <utility>
63 #include <vector>
64 
65 using namespace llvm;
66 
67 #define DEBUG_TYPE "sccp"
68 
69 STATISTIC(NumInstRemoved, "Number of instructions removed");
70 STATISTIC(NumDeadBlocks , "Number of basic blocks unreachable");
71 STATISTIC(NumInstReplaced,
72           "Number of instructions replaced with (simpler) instruction");
73 
74 STATISTIC(IPNumInstRemoved, "Number of instructions removed by IPSCCP");
75 STATISTIC(IPNumArgsElimed ,"Number of arguments constant propagated by IPSCCP");
76 STATISTIC(IPNumGlobalConst, "Number of globals found to be constant by IPSCCP");
77 STATISTIC(
78     IPNumInstReplaced,
79     "Number of instructions replaced with (simpler) instruction by IPSCCP");
80 
81 // The maximum number of range extensions allowed for operations requiring
82 // widening.
83 static const unsigned MaxNumRangeExtensions = 10;
84 
85 /// Returns MergeOptions with MaxWidenSteps set to MaxNumRangeExtensions.
86 static ValueLatticeElement::MergeOptions getMaxWidenStepsOpts() {
87   return ValueLatticeElement::MergeOptions().setMaxWidenSteps(
88       MaxNumRangeExtensions);
89 }
90 namespace {
91 
92 // Helper to check if \p LV is either a constant or a constant
93 // range with a single element. This should cover exactly the same cases as the
94 // old ValueLatticeElement::isConstant() and is intended to be used in the
95 // transition to ValueLatticeElement.
96 bool isConstant(const ValueLatticeElement &LV) {
97   return LV.isConstant() ||
98          (LV.isConstantRange() && LV.getConstantRange().isSingleElement());
99 }
100 
101 // Helper to check if \p LV is either overdefined or a constant range with more
102 // than a single element. This should cover exactly the same cases as the old
103 // ValueLatticeElement::isOverdefined() and is intended to be used in the
104 // transition to ValueLatticeElement.
105 bool isOverdefined(const ValueLatticeElement &LV) {
106   return LV.isOverdefined() ||
107          (LV.isConstantRange() && !LV.getConstantRange().isSingleElement());
108 }
109 
110 //===----------------------------------------------------------------------===//
111 //
112 /// SCCPSolver - This class is a general purpose solver for Sparse Conditional
113 /// Constant Propagation.
114 ///
115 class SCCPSolver : public InstVisitor<SCCPSolver> {
116   const DataLayout &DL;
117   std::function<const TargetLibraryInfo &(Function &)> GetTLI;
118   SmallPtrSet<BasicBlock *, 8> BBExecutable; // The BBs that are executable.
119   DenseMap<Value *, ValueLatticeElement>
120       ValueState; // The state each value is in.
121 
122   /// StructValueState - This maintains ValueState for values that have
123   /// StructType, for example for formal arguments, calls, insertelement, etc.
124   DenseMap<std::pair<Value *, unsigned>, ValueLatticeElement> StructValueState;
125 
126   /// GlobalValue - If we are tracking any values for the contents of a global
127   /// variable, we keep a mapping from the constant accessor to the element of
128   /// the global, to the currently known value.  If the value becomes
129   /// overdefined, it's entry is simply removed from this map.
130   DenseMap<GlobalVariable *, ValueLatticeElement> TrackedGlobals;
131 
132   /// TrackedRetVals - If we are tracking arguments into and the return
133   /// value out of a function, it will have an entry in this map, indicating
134   /// what the known return value for the function is.
135   MapVector<Function *, ValueLatticeElement> TrackedRetVals;
136 
137   /// TrackedMultipleRetVals - Same as TrackedRetVals, but used for functions
138   /// that return multiple values.
139   MapVector<std::pair<Function *, unsigned>, ValueLatticeElement>
140       TrackedMultipleRetVals;
141 
142   /// MRVFunctionsTracked - Each function in TrackedMultipleRetVals is
143   /// represented here for efficient lookup.
144   SmallPtrSet<Function *, 16> MRVFunctionsTracked;
145 
146   /// MustTailFunctions - Each function here is a callee of non-removable
147   /// musttail call site.
148   SmallPtrSet<Function *, 16> MustTailCallees;
149 
150   /// TrackingIncomingArguments - This is the set of functions for whose
151   /// arguments we make optimistic assumptions about and try to prove as
152   /// constants.
153   SmallPtrSet<Function *, 16> TrackingIncomingArguments;
154 
155   /// The reason for two worklists is that overdefined is the lowest state
156   /// on the lattice, and moving things to overdefined as fast as possible
157   /// makes SCCP converge much faster.
158   ///
159   /// By having a separate worklist, we accomplish this because everything
160   /// possibly overdefined will become overdefined at the soonest possible
161   /// point.
162   SmallVector<Value *, 64> OverdefinedInstWorkList;
163   SmallVector<Value *, 64> InstWorkList;
164 
165   // The BasicBlock work list
166   SmallVector<BasicBlock *, 64>  BBWorkList;
167 
168   /// KnownFeasibleEdges - Entries in this set are edges which have already had
169   /// PHI nodes retriggered.
170   using Edge = std::pair<BasicBlock *, BasicBlock *>;
171   DenseSet<Edge> KnownFeasibleEdges;
172 
173   DenseMap<Function *, AnalysisResultsForFn> AnalysisResults;
174   DenseMap<Value *, SmallPtrSet<User *, 2>> AdditionalUsers;
175 
176   LLVMContext &Ctx;
177 
178 public:
179   void addAnalysis(Function &F, AnalysisResultsForFn A) {
180     AnalysisResults.insert({&F, std::move(A)});
181   }
182 
183   const PredicateBase *getPredicateInfoFor(Instruction *I) {
184     auto A = AnalysisResults.find(I->getParent()->getParent());
185     if (A == AnalysisResults.end())
186       return nullptr;
187     return A->second.PredInfo->getPredicateInfoFor(I);
188   }
189 
190   DomTreeUpdater getDTU(Function &F) {
191     auto A = AnalysisResults.find(&F);
192     assert(A != AnalysisResults.end() && "Need analysis results for function.");
193     return {A->second.DT, A->second.PDT, DomTreeUpdater::UpdateStrategy::Lazy};
194   }
195 
196   SCCPSolver(const DataLayout &DL,
197              std::function<const TargetLibraryInfo &(Function &)> GetTLI,
198              LLVMContext &Ctx)
199       : DL(DL), GetTLI(std::move(GetTLI)), Ctx(Ctx) {}
200 
201   /// MarkBlockExecutable - This method can be used by clients to mark all of
202   /// the blocks that are known to be intrinsically live in the processed unit.
203   ///
204   /// This returns true if the block was not considered live before.
205   bool MarkBlockExecutable(BasicBlock *BB) {
206     if (!BBExecutable.insert(BB).second)
207       return false;
208     LLVM_DEBUG(dbgs() << "Marking Block Executable: " << BB->getName() << '\n');
209     BBWorkList.push_back(BB);  // Add the block to the work list!
210     return true;
211   }
212 
213   /// TrackValueOfGlobalVariable - Clients can use this method to
214   /// inform the SCCPSolver that it should track loads and stores to the
215   /// specified global variable if it can.  This is only legal to call if
216   /// performing Interprocedural SCCP.
217   void TrackValueOfGlobalVariable(GlobalVariable *GV) {
218     // We only track the contents of scalar globals.
219     if (GV->getValueType()->isSingleValueType()) {
220       ValueLatticeElement &IV = TrackedGlobals[GV];
221       if (!isa<UndefValue>(GV->getInitializer()))
222         IV.markConstant(GV->getInitializer());
223     }
224   }
225 
226   /// AddTrackedFunction - If the SCCP solver is supposed to track calls into
227   /// and out of the specified function (which cannot have its address taken),
228   /// this method must be called.
229   void AddTrackedFunction(Function *F) {
230     // Add an entry, F -> undef.
231     if (auto *STy = dyn_cast<StructType>(F->getReturnType())) {
232       MRVFunctionsTracked.insert(F);
233       for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
234         TrackedMultipleRetVals.insert(
235             std::make_pair(std::make_pair(F, i), ValueLatticeElement()));
236     } else
237       TrackedRetVals.insert(std::make_pair(F, ValueLatticeElement()));
238   }
239 
240   /// AddMustTailCallee - If the SCCP solver finds that this function is called
241   /// from non-removable musttail call site.
242   void AddMustTailCallee(Function *F) {
243     MustTailCallees.insert(F);
244   }
245 
246   /// Returns true if the given function is called from non-removable musttail
247   /// call site.
248   bool isMustTailCallee(Function *F) {
249     return MustTailCallees.count(F);
250   }
251 
252   void AddArgumentTrackedFunction(Function *F) {
253     TrackingIncomingArguments.insert(F);
254   }
255 
256   /// Returns true if the given function is in the solver's set of
257   /// argument-tracked functions.
258   bool isArgumentTrackedFunction(Function *F) {
259     return TrackingIncomingArguments.count(F);
260   }
261 
262   /// Solve - Solve for constants and executable blocks.
263   void Solve();
264 
265   /// ResolvedUndefsIn - While solving the dataflow for a function, we assume
266   /// that branches on undef values cannot reach any of their successors.
267   /// However, this is not a safe assumption.  After we solve dataflow, this
268   /// method should be use to handle this.  If this returns true, the solver
269   /// should be rerun.
270   bool ResolvedUndefsIn(Function &F);
271 
272   bool isBlockExecutable(BasicBlock *BB) const {
273     return BBExecutable.count(BB);
274   }
275 
276   // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
277   // block to the 'To' basic block is currently feasible.
278   bool isEdgeFeasible(BasicBlock *From, BasicBlock *To);
279 
280   std::vector<ValueLatticeElement> getStructLatticeValueFor(Value *V) const {
281     std::vector<ValueLatticeElement> StructValues;
282     auto *STy = dyn_cast<StructType>(V->getType());
283     assert(STy && "getStructLatticeValueFor() can be called only on structs");
284     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
285       auto I = StructValueState.find(std::make_pair(V, i));
286       assert(I != StructValueState.end() && "Value not in valuemap!");
287       StructValues.push_back(I->second);
288     }
289     return StructValues;
290   }
291 
292   void removeLatticeValueFor(Value *V) { ValueState.erase(V); }
293 
294   const ValueLatticeElement &getLatticeValueFor(Value *V) const {
295     assert(!V->getType()->isStructTy() &&
296            "Should use getStructLatticeValueFor");
297     DenseMap<Value *, ValueLatticeElement>::const_iterator I =
298         ValueState.find(V);
299     assert(I != ValueState.end() &&
300            "V not found in ValueState nor Paramstate map!");
301     return I->second;
302   }
303 
304   /// getTrackedRetVals - Get the inferred return value map.
305   const MapVector<Function *, ValueLatticeElement> &getTrackedRetVals() {
306     return TrackedRetVals;
307   }
308 
309   /// getTrackedGlobals - Get and return the set of inferred initializers for
310   /// global variables.
311   const DenseMap<GlobalVariable *, ValueLatticeElement> &getTrackedGlobals() {
312     return TrackedGlobals;
313   }
314 
315   /// getMRVFunctionsTracked - Get the set of functions which return multiple
316   /// values tracked by the pass.
317   const SmallPtrSet<Function *, 16> getMRVFunctionsTracked() {
318     return MRVFunctionsTracked;
319   }
320 
321   /// getMustTailCallees - Get the set of functions which are called
322   /// from non-removable musttail call sites.
323   const SmallPtrSet<Function *, 16> getMustTailCallees() {
324     return MustTailCallees;
325   }
326 
327   /// markOverdefined - Mark the specified value overdefined.  This
328   /// works with both scalars and structs.
329   void markOverdefined(Value *V) {
330     if (auto *STy = dyn_cast<StructType>(V->getType()))
331       for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
332         markOverdefined(getStructValueState(V, i), V);
333     else
334       markOverdefined(ValueState[V], V);
335   }
336 
337   // isStructLatticeConstant - Return true if all the lattice values
338   // corresponding to elements of the structure are constants,
339   // false otherwise.
340   bool isStructLatticeConstant(Function *F, StructType *STy) {
341     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
342       const auto &It = TrackedMultipleRetVals.find(std::make_pair(F, i));
343       assert(It != TrackedMultipleRetVals.end());
344       ValueLatticeElement LV = It->second;
345       if (!isConstant(LV))
346         return false;
347     }
348     return true;
349   }
350 
351   /// Helper to return a Constant if \p LV is either a constant or a constant
352   /// range with a single element.
353   Constant *getConstant(const ValueLatticeElement &LV) const {
354     if (LV.isConstant())
355       return LV.getConstant();
356 
357     if (LV.isConstantRange()) {
358       auto &CR = LV.getConstantRange();
359       if (CR.getSingleElement())
360         return ConstantInt::get(Ctx, *CR.getSingleElement());
361     }
362     return nullptr;
363   }
364 
365 private:
366   ConstantInt *getConstantInt(const ValueLatticeElement &IV) const {
367     return dyn_cast_or_null<ConstantInt>(getConstant(IV));
368   }
369 
370   // pushToWorkList - Helper for markConstant/markOverdefined
371   void pushToWorkList(ValueLatticeElement &IV, Value *V) {
372     if (IV.isOverdefined())
373       return OverdefinedInstWorkList.push_back(V);
374     InstWorkList.push_back(V);
375   }
376 
377   // Helper to push \p V to the worklist, after updating it to \p IV. Also
378   // prints a debug message with the updated value.
379   void pushToWorkListMsg(ValueLatticeElement &IV, Value *V) {
380     LLVM_DEBUG(dbgs() << "updated " << IV << ": " << *V << '\n');
381     pushToWorkList(IV, V);
382   }
383 
384   // markConstant - Make a value be marked as "constant".  If the value
385   // is not already a constant, add it to the instruction work list so that
386   // the users of the instruction are updated later.
387   bool markConstant(ValueLatticeElement &IV, Value *V, Constant *C,
388                     bool MayIncludeUndef = false) {
389     if (!IV.markConstant(C, MayIncludeUndef))
390       return false;
391     LLVM_DEBUG(dbgs() << "markConstant: " << *C << ": " << *V << '\n');
392     pushToWorkList(IV, V);
393     return true;
394   }
395 
396   bool markConstant(Value *V, Constant *C) {
397     assert(!V->getType()->isStructTy() && "structs should use mergeInValue");
398     return markConstant(ValueState[V], V, C);
399   }
400 
401   // markOverdefined - Make a value be marked as "overdefined". If the
402   // value is not already overdefined, add it to the overdefined instruction
403   // work list so that the users of the instruction are updated later.
404   bool markOverdefined(ValueLatticeElement &IV, Value *V) {
405     if (!IV.markOverdefined()) return false;
406 
407     LLVM_DEBUG(dbgs() << "markOverdefined: ";
408                if (auto *F = dyn_cast<Function>(V)) dbgs()
409                << "Function '" << F->getName() << "'\n";
410                else dbgs() << *V << '\n');
411     // Only instructions go on the work list
412     pushToWorkList(IV, V);
413     return true;
414   }
415 
416   /// Merge \p MergeWithV into \p IV and push \p V to the worklist, if \p IV
417   /// changes.
418   bool mergeInValue(ValueLatticeElement &IV, Value *V,
419                     ValueLatticeElement MergeWithV,
420                     ValueLatticeElement::MergeOptions Opts = {
421                         /*MayIncludeUndef=*/false, /*CheckWiden=*/false}) {
422     if (IV.mergeIn(MergeWithV, Opts)) {
423       pushToWorkList(IV, V);
424       LLVM_DEBUG(dbgs() << "Merged " << MergeWithV << " into " << *V << " : "
425                         << IV << "\n");
426       return true;
427     }
428     return false;
429   }
430 
431   bool mergeInValue(Value *V, ValueLatticeElement MergeWithV,
432                     ValueLatticeElement::MergeOptions Opts = {
433                         /*MayIncludeUndef=*/false, /*CheckWiden=*/false}) {
434     assert(!V->getType()->isStructTy() &&
435            "non-structs should use markConstant");
436     return mergeInValue(ValueState[V], V, MergeWithV, Opts);
437   }
438 
439   /// getValueState - Return the ValueLatticeElement object that corresponds to
440   /// the value.  This function handles the case when the value hasn't been seen
441   /// yet by properly seeding constants etc.
442   ValueLatticeElement &getValueState(Value *V) {
443     assert(!V->getType()->isStructTy() && "Should use getStructValueState");
444 
445     auto I = ValueState.insert(std::make_pair(V, ValueLatticeElement()));
446     ValueLatticeElement &LV = I.first->second;
447 
448     if (!I.second)
449       return LV;  // Common case, already in the map.
450 
451     if (auto *C = dyn_cast<Constant>(V))
452       LV.markConstant(C);          // Constants are constant
453 
454     // All others are unknown by default.
455     return LV;
456   }
457 
458   /// getStructValueState - Return the ValueLatticeElement object that
459   /// corresponds to the value/field pair.  This function handles the case when
460   /// the value hasn't been seen yet by properly seeding constants etc.
461   ValueLatticeElement &getStructValueState(Value *V, unsigned i) {
462     assert(V->getType()->isStructTy() && "Should use getValueState");
463     assert(i < cast<StructType>(V->getType())->getNumElements() &&
464            "Invalid element #");
465 
466     auto I = StructValueState.insert(
467         std::make_pair(std::make_pair(V, i), ValueLatticeElement()));
468     ValueLatticeElement &LV = I.first->second;
469 
470     if (!I.second)
471       return LV;  // Common case, already in the map.
472 
473     if (auto *C = dyn_cast<Constant>(V)) {
474       Constant *Elt = C->getAggregateElement(i);
475 
476       if (!Elt)
477         LV.markOverdefined();      // Unknown sort of constant.
478       else if (isa<UndefValue>(Elt))
479         ; // Undef values remain unknown.
480       else
481         LV.markConstant(Elt);      // Constants are constant.
482     }
483 
484     // All others are underdefined by default.
485     return LV;
486   }
487 
488   /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
489   /// work list if it is not already executable.
490   bool markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
491     if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
492       return false;  // This edge is already known to be executable!
493 
494     if (!MarkBlockExecutable(Dest)) {
495       // If the destination is already executable, we just made an *edge*
496       // feasible that wasn't before.  Revisit the PHI nodes in the block
497       // because they have potentially new operands.
498       LLVM_DEBUG(dbgs() << "Marking Edge Executable: " << Source->getName()
499                         << " -> " << Dest->getName() << '\n');
500 
501       for (PHINode &PN : Dest->phis())
502         visitPHINode(PN);
503     }
504     return true;
505   }
506 
507   // getFeasibleSuccessors - Return a vector of booleans to indicate which
508   // successors are reachable from a given terminator instruction.
509   void getFeasibleSuccessors(Instruction &TI, SmallVectorImpl<bool> &Succs);
510 
511   // OperandChangedState - This method is invoked on all of the users of an
512   // instruction that was just changed state somehow.  Based on this
513   // information, we need to update the specified user of this instruction.
514   void OperandChangedState(Instruction *I) {
515     if (BBExecutable.count(I->getParent()))   // Inst is executable?
516       visit(*I);
517   }
518 
519   // Add U as additional user of V.
520   void addAdditionalUser(Value *V, User *U) {
521     auto Iter = AdditionalUsers.insert({V, {}});
522     Iter.first->second.insert(U);
523   }
524 
525   // Mark I's users as changed, including AdditionalUsers.
526   void markUsersAsChanged(Value *I) {
527     // Functions include their arguments in the use-list. Changed function
528     // values mean that the result of the function changed. We only need to
529     // update the call sites with the new function result and do not have to
530     // propagate the call arguments.
531     if (isa<Function>(I)) {
532       for (User *U : I->users()) {
533         if (auto *CB = dyn_cast<CallBase>(U))
534           handleCallResult(*CB);
535       }
536     } else {
537       for (User *U : I->users())
538         if (auto *UI = dyn_cast<Instruction>(U))
539           OperandChangedState(UI);
540     }
541 
542     auto Iter = AdditionalUsers.find(I);
543     if (Iter != AdditionalUsers.end()) {
544       for (User *U : Iter->second)
545         if (auto *UI = dyn_cast<Instruction>(U))
546           OperandChangedState(UI);
547     }
548   }
549   void handleCallOverdefined(CallBase &CB);
550   void handleCallResult(CallBase &CB);
551   void handleCallArguments(CallBase &CB);
552 
553 private:
554   friend class InstVisitor<SCCPSolver>;
555 
556   // visit implementations - Something changed in this instruction.  Either an
557   // operand made a transition, or the instruction is newly executable.  Change
558   // the value type of I to reflect these changes if appropriate.
559   void visitPHINode(PHINode &I);
560 
561   // Terminators
562 
563   void visitReturnInst(ReturnInst &I);
564   void visitTerminator(Instruction &TI);
565 
566   void visitCastInst(CastInst &I);
567   void visitSelectInst(SelectInst &I);
568   void visitUnaryOperator(Instruction &I);
569   void visitBinaryOperator(Instruction &I);
570   void visitCmpInst(CmpInst &I);
571   void visitExtractValueInst(ExtractValueInst &EVI);
572   void visitInsertValueInst(InsertValueInst &IVI);
573 
574   void visitCatchSwitchInst(CatchSwitchInst &CPI) {
575     markOverdefined(&CPI);
576     visitTerminator(CPI);
577   }
578 
579   // Instructions that cannot be folded away.
580 
581   void visitStoreInst     (StoreInst &I);
582   void visitLoadInst      (LoadInst &I);
583   void visitGetElementPtrInst(GetElementPtrInst &I);
584 
585   void visitCallInst      (CallInst &I) {
586     visitCallBase(I);
587   }
588 
589   void visitInvokeInst    (InvokeInst &II) {
590     visitCallBase(II);
591     visitTerminator(II);
592   }
593 
594   void visitCallBrInst    (CallBrInst &CBI) {
595     visitCallBase(CBI);
596     visitTerminator(CBI);
597   }
598 
599   void visitCallBase      (CallBase &CB);
600   void visitResumeInst    (ResumeInst &I) { /*returns void*/ }
601   void visitUnreachableInst(UnreachableInst &I) { /*returns void*/ }
602   void visitFenceInst     (FenceInst &I) { /*returns void*/ }
603 
604   void visitInstruction(Instruction &I) {
605     // All the instructions we don't do any special handling for just
606     // go to overdefined.
607     LLVM_DEBUG(dbgs() << "SCCP: Don't know how to handle: " << I << '\n');
608     markOverdefined(&I);
609   }
610 };
611 
612 } // end anonymous namespace
613 
614 // getFeasibleSuccessors - Return a vector of booleans to indicate which
615 // successors are reachable from a given terminator instruction.
616 void SCCPSolver::getFeasibleSuccessors(Instruction &TI,
617                                        SmallVectorImpl<bool> &Succs) {
618   Succs.resize(TI.getNumSuccessors());
619   if (auto *BI = dyn_cast<BranchInst>(&TI)) {
620     if (BI->isUnconditional()) {
621       Succs[0] = true;
622       return;
623     }
624 
625     ValueLatticeElement BCValue = getValueState(BI->getCondition());
626     ConstantInt *CI = getConstantInt(BCValue);
627     if (!CI) {
628       // Overdefined condition variables, and branches on unfoldable constant
629       // conditions, mean the branch could go either way.
630       if (!BCValue.isUnknownOrUndef())
631         Succs[0] = Succs[1] = true;
632       return;
633     }
634 
635     // Constant condition variables mean the branch can only go a single way.
636     Succs[CI->isZero()] = true;
637     return;
638   }
639 
640   // Unwinding instructions successors are always executable.
641   if (TI.isExceptionalTerminator()) {
642     Succs.assign(TI.getNumSuccessors(), true);
643     return;
644   }
645 
646   if (auto *SI = dyn_cast<SwitchInst>(&TI)) {
647     if (!SI->getNumCases()) {
648       Succs[0] = true;
649       return;
650     }
651     ValueLatticeElement SCValue = getValueState(SI->getCondition());
652     ConstantInt *CI = getConstantInt(SCValue);
653 
654     if (!CI) {   // Overdefined or unknown condition?
655       // All destinations are executable!
656       if (!SCValue.isUnknownOrUndef())
657         Succs.assign(TI.getNumSuccessors(), true);
658       return;
659     }
660 
661     Succs[SI->findCaseValue(CI)->getSuccessorIndex()] = true;
662     return;
663   }
664 
665   // In case of indirect branch and its address is a blockaddress, we mark
666   // the target as executable.
667   if (auto *IBR = dyn_cast<IndirectBrInst>(&TI)) {
668     // Casts are folded by visitCastInst.
669     ValueLatticeElement IBRValue = getValueState(IBR->getAddress());
670     BlockAddress *Addr = dyn_cast_or_null<BlockAddress>(getConstant(IBRValue));
671     if (!Addr) {   // Overdefined or unknown condition?
672       // All destinations are executable!
673       if (!IBRValue.isUnknownOrUndef())
674         Succs.assign(TI.getNumSuccessors(), true);
675       return;
676     }
677 
678     BasicBlock* T = Addr->getBasicBlock();
679     assert(Addr->getFunction() == T->getParent() &&
680            "Block address of a different function ?");
681     for (unsigned i = 0; i < IBR->getNumSuccessors(); ++i) {
682       // This is the target.
683       if (IBR->getDestination(i) == T) {
684         Succs[i] = true;
685         return;
686       }
687     }
688 
689     // If we didn't find our destination in the IBR successor list, then we
690     // have undefined behavior. Its ok to assume no successor is executable.
691     return;
692   }
693 
694   // In case of callbr, we pessimistically assume that all successors are
695   // feasible.
696   if (isa<CallBrInst>(&TI)) {
697     Succs.assign(TI.getNumSuccessors(), true);
698     return;
699   }
700 
701   LLVM_DEBUG(dbgs() << "Unknown terminator instruction: " << TI << '\n');
702   llvm_unreachable("SCCP: Don't know how to handle this terminator!");
703 }
704 
705 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
706 // block to the 'To' basic block is currently feasible.
707 bool SCCPSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
708   // Check if we've called markEdgeExecutable on the edge yet. (We could
709   // be more aggressive and try to consider edges which haven't been marked
710   // yet, but there isn't any need.)
711   return KnownFeasibleEdges.count(Edge(From, To));
712 }
713 
714 // visit Implementations - Something changed in this instruction, either an
715 // operand made a transition, or the instruction is newly executable.  Change
716 // the value type of I to reflect these changes if appropriate.  This method
717 // makes sure to do the following actions:
718 //
719 // 1. If a phi node merges two constants in, and has conflicting value coming
720 //    from different branches, or if the PHI node merges in an overdefined
721 //    value, then the PHI node becomes overdefined.
722 // 2. If a phi node merges only constants in, and they all agree on value, the
723 //    PHI node becomes a constant value equal to that.
724 // 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
725 // 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
726 // 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
727 // 6. If a conditional branch has a value that is constant, make the selected
728 //    destination executable
729 // 7. If a conditional branch has a value that is overdefined, make all
730 //    successors executable.
731 void SCCPSolver::visitPHINode(PHINode &PN) {
732   // If this PN returns a struct, just mark the result overdefined.
733   // TODO: We could do a lot better than this if code actually uses this.
734   if (PN.getType()->isStructTy())
735     return (void)markOverdefined(&PN);
736 
737   if (getValueState(&PN).isOverdefined())
738     return; // Quick exit
739 
740   // Super-extra-high-degree PHI nodes are unlikely to ever be marked constant,
741   // and slow us down a lot.  Just mark them overdefined.
742   if (PN.getNumIncomingValues() > 64)
743     return (void)markOverdefined(&PN);
744 
745   unsigned NumActiveIncoming = 0;
746 
747   // Look at all of the executable operands of the PHI node.  If any of them
748   // are overdefined, the PHI becomes overdefined as well.  If they are all
749   // constant, and they agree with each other, the PHI becomes the identical
750   // constant.  If they are constant and don't agree, the PHI is a constant
751   // range. If there are no executable operands, the PHI remains unknown.
752   ValueLatticeElement PhiState = getValueState(&PN);
753   for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
754     if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent()))
755       continue;
756 
757     ValueLatticeElement IV = getValueState(PN.getIncomingValue(i));
758     PhiState.mergeIn(IV);
759     NumActiveIncoming++;
760     if (PhiState.isOverdefined())
761       break;
762   }
763 
764   // We allow up to 1 range extension per active incoming value and one
765   // additional extension. Note that we manually adjust the number of range
766   // extensions to match the number of active incoming values. This helps to
767   // limit multiple extensions caused by the same incoming value, if other
768   // incoming values are equal.
769   mergeInValue(&PN, PhiState,
770                ValueLatticeElement::MergeOptions().setMaxWidenSteps(
771                    NumActiveIncoming + 1));
772   ValueLatticeElement &PhiStateRef = getValueState(&PN);
773   PhiStateRef.setNumRangeExtensions(
774       std::max(NumActiveIncoming, PhiStateRef.getNumRangeExtensions()));
775 }
776 
777 void SCCPSolver::visitReturnInst(ReturnInst &I) {
778   if (I.getNumOperands() == 0) return;  // ret void
779 
780   Function *F = I.getParent()->getParent();
781   Value *ResultOp = I.getOperand(0);
782 
783   // If we are tracking the return value of this function, merge it in.
784   if (!TrackedRetVals.empty() && !ResultOp->getType()->isStructTy()) {
785     auto TFRVI = TrackedRetVals.find(F);
786     if (TFRVI != TrackedRetVals.end()) {
787       mergeInValue(TFRVI->second, F, getValueState(ResultOp));
788       return;
789     }
790   }
791 
792   // Handle functions that return multiple values.
793   if (!TrackedMultipleRetVals.empty()) {
794     if (auto *STy = dyn_cast<StructType>(ResultOp->getType()))
795       if (MRVFunctionsTracked.count(F))
796         for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
797           mergeInValue(TrackedMultipleRetVals[std::make_pair(F, i)], F,
798                        getStructValueState(ResultOp, i));
799   }
800 }
801 
802 void SCCPSolver::visitTerminator(Instruction &TI) {
803   SmallVector<bool, 16> SuccFeasible;
804   getFeasibleSuccessors(TI, SuccFeasible);
805 
806   BasicBlock *BB = TI.getParent();
807 
808   // Mark all feasible successors executable.
809   for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
810     if (SuccFeasible[i])
811       markEdgeExecutable(BB, TI.getSuccessor(i));
812 }
813 
814 void SCCPSolver::visitCastInst(CastInst &I) {
815   // ResolvedUndefsIn might mark I as overdefined. Bail out, even if we would
816   // discover a concrete value later.
817   if (ValueState[&I].isOverdefined())
818     return;
819 
820   ValueLatticeElement OpSt = getValueState(I.getOperand(0));
821   if (Constant *OpC = getConstant(OpSt)) {
822     // Fold the constant as we build.
823     Constant *C = ConstantFoldCastOperand(I.getOpcode(), OpC, I.getType(), DL);
824     if (isa<UndefValue>(C))
825       return;
826     // Propagate constant value
827     markConstant(&I, C);
828   } else if (OpSt.isConstantRange() && I.getDestTy()->isIntegerTy()) {
829     auto &LV = getValueState(&I);
830     ConstantRange OpRange = OpSt.getConstantRange();
831     Type *DestTy = I.getDestTy();
832     ConstantRange Res =
833         OpRange.castOp(I.getOpcode(), DL.getTypeSizeInBits(DestTy));
834     mergeInValue(LV, &I, ValueLatticeElement::getRange(Res));
835   } else if (!OpSt.isUnknownOrUndef())
836     markOverdefined(&I);
837 }
838 
839 void SCCPSolver::visitExtractValueInst(ExtractValueInst &EVI) {
840   // If this returns a struct, mark all elements over defined, we don't track
841   // structs in structs.
842   if (EVI.getType()->isStructTy())
843     return (void)markOverdefined(&EVI);
844 
845   // ResolvedUndefsIn might mark I as overdefined. Bail out, even if we would
846   // discover a concrete value later.
847   if (ValueState[&EVI].isOverdefined())
848     return (void)markOverdefined(&EVI);
849 
850   // If this is extracting from more than one level of struct, we don't know.
851   if (EVI.getNumIndices() != 1)
852     return (void)markOverdefined(&EVI);
853 
854   Value *AggVal = EVI.getAggregateOperand();
855   if (AggVal->getType()->isStructTy()) {
856     unsigned i = *EVI.idx_begin();
857     ValueLatticeElement EltVal = getStructValueState(AggVal, i);
858     mergeInValue(getValueState(&EVI), &EVI, EltVal);
859   } else {
860     // Otherwise, must be extracting from an array.
861     return (void)markOverdefined(&EVI);
862   }
863 }
864 
865 void SCCPSolver::visitInsertValueInst(InsertValueInst &IVI) {
866   auto *STy = dyn_cast<StructType>(IVI.getType());
867   if (!STy)
868     return (void)markOverdefined(&IVI);
869 
870   // ResolvedUndefsIn might mark I as overdefined. Bail out, even if we would
871   // discover a concrete value later.
872   if (isOverdefined(ValueState[&IVI]))
873     return (void)markOverdefined(&IVI);
874 
875   // If this has more than one index, we can't handle it, drive all results to
876   // undef.
877   if (IVI.getNumIndices() != 1)
878     return (void)markOverdefined(&IVI);
879 
880   Value *Aggr = IVI.getAggregateOperand();
881   unsigned Idx = *IVI.idx_begin();
882 
883   // Compute the result based on what we're inserting.
884   for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
885     // This passes through all values that aren't the inserted element.
886     if (i != Idx) {
887       ValueLatticeElement EltVal = getStructValueState(Aggr, i);
888       mergeInValue(getStructValueState(&IVI, i), &IVI, EltVal);
889       continue;
890     }
891 
892     Value *Val = IVI.getInsertedValueOperand();
893     if (Val->getType()->isStructTy())
894       // We don't track structs in structs.
895       markOverdefined(getStructValueState(&IVI, i), &IVI);
896     else {
897       ValueLatticeElement InVal = getValueState(Val);
898       mergeInValue(getStructValueState(&IVI, i), &IVI, InVal);
899     }
900   }
901 }
902 
903 void SCCPSolver::visitSelectInst(SelectInst &I) {
904   // If this select returns a struct, just mark the result overdefined.
905   // TODO: We could do a lot better than this if code actually uses this.
906   if (I.getType()->isStructTy())
907     return (void)markOverdefined(&I);
908 
909   // ResolvedUndefsIn might mark I as overdefined. Bail out, even if we would
910   // discover a concrete value later.
911   if (ValueState[&I].isOverdefined())
912     return (void)markOverdefined(&I);
913 
914   ValueLatticeElement CondValue = getValueState(I.getCondition());
915   if (CondValue.isUnknownOrUndef())
916     return;
917 
918   if (ConstantInt *CondCB = getConstantInt(CondValue)) {
919     Value *OpVal = CondCB->isZero() ? I.getFalseValue() : I.getTrueValue();
920     mergeInValue(&I, getValueState(OpVal));
921     return;
922   }
923 
924   // Otherwise, the condition is overdefined or a constant we can't evaluate.
925   // See if we can produce something better than overdefined based on the T/F
926   // value.
927   ValueLatticeElement TVal = getValueState(I.getTrueValue());
928   ValueLatticeElement FVal = getValueState(I.getFalseValue());
929 
930   bool Changed = ValueState[&I].mergeIn(TVal);
931   Changed |= ValueState[&I].mergeIn(FVal);
932   if (Changed)
933     pushToWorkListMsg(ValueState[&I], &I);
934 }
935 
936 // Handle Unary Operators.
937 void SCCPSolver::visitUnaryOperator(Instruction &I) {
938   ValueLatticeElement V0State = getValueState(I.getOperand(0));
939 
940   ValueLatticeElement &IV = ValueState[&I];
941   // ResolvedUndefsIn might mark I as overdefined. Bail out, even if we would
942   // discover a concrete value later.
943   if (isOverdefined(IV))
944     return (void)markOverdefined(&I);
945 
946   if (isConstant(V0State)) {
947     Constant *C = ConstantExpr::get(I.getOpcode(), getConstant(V0State));
948 
949     // op Y -> undef.
950     if (isa<UndefValue>(C))
951       return;
952     return (void)markConstant(IV, &I, C);
953   }
954 
955   // If something is undef, wait for it to resolve.
956   if (!isOverdefined(V0State))
957     return;
958 
959   markOverdefined(&I);
960 }
961 
962 // Handle Binary Operators.
963 void SCCPSolver::visitBinaryOperator(Instruction &I) {
964   ValueLatticeElement V1State = getValueState(I.getOperand(0));
965   ValueLatticeElement V2State = getValueState(I.getOperand(1));
966 
967   ValueLatticeElement &IV = ValueState[&I];
968   if (IV.isOverdefined())
969     return;
970 
971   // If something is undef, wait for it to resolve.
972   if (V1State.isUnknownOrUndef() || V2State.isUnknownOrUndef())
973     return;
974 
975   if (V1State.isOverdefined() && V2State.isOverdefined())
976     return (void)markOverdefined(&I);
977 
978   // If either of the operands is a constant, try to fold it to a constant.
979   // TODO: Use information from notconstant better.
980   if ((V1State.isConstant() || V2State.isConstant())) {
981     Value *V1 = isConstant(V1State) ? getConstant(V1State) : I.getOperand(0);
982     Value *V2 = isConstant(V2State) ? getConstant(V2State) : I.getOperand(1);
983     Value *R = SimplifyBinOp(I.getOpcode(), V1, V2, SimplifyQuery(DL));
984     auto *C = dyn_cast_or_null<Constant>(R);
985     if (C) {
986       // X op Y -> undef.
987       if (isa<UndefValue>(C))
988         return;
989       // Conservatively assume that the result may be based on operands that may
990       // be undef. Note that we use mergeInValue to combine the constant with
991       // the existing lattice value for I, as different constants might be found
992       // after one of the operands go to overdefined, e.g. due to one operand
993       // being a special floating value.
994       ValueLatticeElement NewV;
995       NewV.markConstant(C, /*MayIncludeUndef=*/true);
996       return (void)mergeInValue(&I, NewV);
997     }
998   }
999 
1000   // Only use ranges for binary operators on integers.
1001   if (!I.getType()->isIntegerTy())
1002     return markOverdefined(&I);
1003 
1004   // Try to simplify to a constant range.
1005   ConstantRange A = ConstantRange::getFull(I.getType()->getScalarSizeInBits());
1006   ConstantRange B = ConstantRange::getFull(I.getType()->getScalarSizeInBits());
1007   if (V1State.isConstantRange())
1008     A = V1State.getConstantRange();
1009   if (V2State.isConstantRange())
1010     B = V2State.getConstantRange();
1011 
1012   ConstantRange R = A.binaryOp(cast<BinaryOperator>(&I)->getOpcode(), B);
1013   mergeInValue(&I, ValueLatticeElement::getRange(R));
1014 
1015   // TODO: Currently we do not exploit special values that produce something
1016   // better than overdefined with an overdefined operand for vector or floating
1017   // point types, like and <4 x i32> overdefined, zeroinitializer.
1018 }
1019 
1020 // Handle ICmpInst instruction.
1021 void SCCPSolver::visitCmpInst(CmpInst &I) {
1022   // Do not cache this lookup, getValueState calls later in the function might
1023   // invalidate the reference.
1024   if (isOverdefined(ValueState[&I]))
1025     return (void)markOverdefined(&I);
1026 
1027   Value *Op1 = I.getOperand(0);
1028   Value *Op2 = I.getOperand(1);
1029 
1030   // For parameters, use ParamState which includes constant range info if
1031   // available.
1032   auto V1State = getValueState(Op1);
1033   auto V2State = getValueState(Op2);
1034 
1035   Constant *C = V1State.getCompare(I.getPredicate(), I.getType(), V2State);
1036   if (C) {
1037     if (isa<UndefValue>(C))
1038       return;
1039     ValueLatticeElement CV;
1040     CV.markConstant(C);
1041     mergeInValue(&I, CV);
1042     return;
1043   }
1044 
1045   // If operands are still unknown, wait for it to resolve.
1046   if ((V1State.isUnknownOrUndef() || V2State.isUnknownOrUndef()) &&
1047       !isConstant(ValueState[&I]))
1048     return;
1049 
1050   markOverdefined(&I);
1051 }
1052 
1053 // Handle getelementptr instructions.  If all operands are constants then we
1054 // can turn this into a getelementptr ConstantExpr.
1055 void SCCPSolver::visitGetElementPtrInst(GetElementPtrInst &I) {
1056   if (isOverdefined(ValueState[&I]))
1057     return (void)markOverdefined(&I);
1058 
1059   SmallVector<Constant*, 8> Operands;
1060   Operands.reserve(I.getNumOperands());
1061 
1062   for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
1063     ValueLatticeElement State = getValueState(I.getOperand(i));
1064     if (State.isUnknownOrUndef())
1065       return;  // Operands are not resolved yet.
1066 
1067     if (isOverdefined(State))
1068       return (void)markOverdefined(&I);
1069 
1070     if (Constant *C = getConstant(State)) {
1071       Operands.push_back(C);
1072       continue;
1073     }
1074 
1075     return (void)markOverdefined(&I);
1076   }
1077 
1078   Constant *Ptr = Operands[0];
1079   auto Indices = makeArrayRef(Operands.begin() + 1, Operands.end());
1080   Constant *C =
1081       ConstantExpr::getGetElementPtr(I.getSourceElementType(), Ptr, Indices);
1082   if (isa<UndefValue>(C))
1083       return;
1084   markConstant(&I, C);
1085 }
1086 
1087 void SCCPSolver::visitStoreInst(StoreInst &SI) {
1088   // If this store is of a struct, ignore it.
1089   if (SI.getOperand(0)->getType()->isStructTy())
1090     return;
1091 
1092   if (TrackedGlobals.empty() || !isa<GlobalVariable>(SI.getOperand(1)))
1093     return;
1094 
1095   GlobalVariable *GV = cast<GlobalVariable>(SI.getOperand(1));
1096   auto I = TrackedGlobals.find(GV);
1097   if (I == TrackedGlobals.end())
1098     return;
1099 
1100   // Get the value we are storing into the global, then merge it.
1101   mergeInValue(I->second, GV, getValueState(SI.getOperand(0)),
1102                ValueLatticeElement::MergeOptions().setCheckWiden(false));
1103   if (I->second.isOverdefined())
1104     TrackedGlobals.erase(I);      // No need to keep tracking this!
1105 }
1106 
1107 static ValueLatticeElement getValueFromMetadata(const Instruction *I) {
1108   if (MDNode *Ranges = I->getMetadata(LLVMContext::MD_range))
1109     if (I->getType()->isIntegerTy())
1110       return ValueLatticeElement::getRange(
1111           getConstantRangeFromMetadata(*Ranges));
1112   // TODO: Also handle MD_nonnull.
1113   return ValueLatticeElement::getOverdefined();
1114 }
1115 
1116 // Handle load instructions.  If the operand is a constant pointer to a constant
1117 // global, we can replace the load with the loaded constant value!
1118 void SCCPSolver::visitLoadInst(LoadInst &I) {
1119   // If this load is of a struct or the load is volatile, just mark the result
1120   // as overdefined.
1121   if (I.getType()->isStructTy() || I.isVolatile())
1122     return (void)markOverdefined(&I);
1123 
1124   // ResolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1125   // discover a concrete value later.
1126   if (ValueState[&I].isOverdefined())
1127     return (void)markOverdefined(&I);
1128 
1129   ValueLatticeElement PtrVal = getValueState(I.getOperand(0));
1130   if (PtrVal.isUnknownOrUndef())
1131     return; // The pointer is not resolved yet!
1132 
1133   ValueLatticeElement &IV = ValueState[&I];
1134 
1135   if (isConstant(PtrVal)) {
1136     Constant *Ptr = getConstant(PtrVal);
1137 
1138     // load null is undefined.
1139     if (isa<ConstantPointerNull>(Ptr)) {
1140       if (NullPointerIsDefined(I.getFunction(), I.getPointerAddressSpace()))
1141         return (void)markOverdefined(IV, &I);
1142       else
1143         return;
1144     }
1145 
1146     // Transform load (constant global) into the value loaded.
1147     if (auto *GV = dyn_cast<GlobalVariable>(Ptr)) {
1148       if (!TrackedGlobals.empty()) {
1149         // If we are tracking this global, merge in the known value for it.
1150         auto It = TrackedGlobals.find(GV);
1151         if (It != TrackedGlobals.end()) {
1152           mergeInValue(IV, &I, It->second, getMaxWidenStepsOpts());
1153           return;
1154         }
1155       }
1156     }
1157 
1158     // Transform load from a constant into a constant if possible.
1159     if (Constant *C = ConstantFoldLoadFromConstPtr(Ptr, I.getType(), DL)) {
1160       if (isa<UndefValue>(C))
1161         return;
1162       return (void)markConstant(IV, &I, C);
1163     }
1164   }
1165 
1166   // Fall back to metadata.
1167   mergeInValue(&I, getValueFromMetadata(&I));
1168 }
1169 
1170 void SCCPSolver::visitCallBase(CallBase &CB) {
1171   handleCallResult(CB);
1172   handleCallArguments(CB);
1173 }
1174 
1175 void SCCPSolver::handleCallOverdefined(CallBase &CB) {
1176   Function *F = CB.getCalledFunction();
1177 
1178   // Void return and not tracking callee, just bail.
1179   if (CB.getType()->isVoidTy())
1180     return;
1181 
1182   // Always mark struct return as overdefined.
1183   if (CB.getType()->isStructTy())
1184     return (void)markOverdefined(&CB);
1185 
1186   // Otherwise, if we have a single return value case, and if the function is
1187   // a declaration, maybe we can constant fold it.
1188   if (F && F->isDeclaration() && canConstantFoldCallTo(&CB, F)) {
1189     SmallVector<Constant *, 8> Operands;
1190     for (auto AI = CB.arg_begin(), E = CB.arg_end(); AI != E; ++AI) {
1191       if (AI->get()->getType()->isStructTy())
1192         return markOverdefined(&CB); // Can't handle struct args.
1193       ValueLatticeElement State = getValueState(*AI);
1194 
1195       if (State.isUnknownOrUndef())
1196         return; // Operands are not resolved yet.
1197       if (isOverdefined(State))
1198         return (void)markOverdefined(&CB);
1199       assert(isConstant(State) && "Unknown state!");
1200       Operands.push_back(getConstant(State));
1201     }
1202 
1203     if (isOverdefined(getValueState(&CB)))
1204       return (void)markOverdefined(&CB);
1205 
1206     // If we can constant fold this, mark the result of the call as a
1207     // constant.
1208     if (Constant *C = ConstantFoldCall(&CB, F, Operands, &GetTLI(*F))) {
1209       // call -> undef.
1210       if (isa<UndefValue>(C))
1211         return;
1212       return (void)markConstant(&CB, C);
1213     }
1214   }
1215 
1216   // Fall back to metadata.
1217   mergeInValue(&CB, getValueFromMetadata(&CB));
1218 }
1219 
1220 void SCCPSolver::handleCallArguments(CallBase &CB) {
1221   Function *F = CB.getCalledFunction();
1222   // If this is a local function that doesn't have its address taken, mark its
1223   // entry block executable and merge in the actual arguments to the call into
1224   // the formal arguments of the function.
1225   if (!TrackingIncomingArguments.empty() &&
1226       TrackingIncomingArguments.count(F)) {
1227     MarkBlockExecutable(&F->front());
1228 
1229     // Propagate information from this call site into the callee.
1230     auto CAI = CB.arg_begin();
1231     for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E;
1232          ++AI, ++CAI) {
1233       // If this argument is byval, and if the function is not readonly, there
1234       // will be an implicit copy formed of the input aggregate.
1235       if (AI->hasByValAttr() && !F->onlyReadsMemory()) {
1236         markOverdefined(&*AI);
1237         continue;
1238       }
1239 
1240       if (auto *STy = dyn_cast<StructType>(AI->getType())) {
1241         for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1242           ValueLatticeElement CallArg = getStructValueState(*CAI, i);
1243           mergeInValue(getStructValueState(&*AI, i), &*AI, CallArg,
1244                        getMaxWidenStepsOpts());
1245         }
1246       } else
1247         mergeInValue(&*AI, getValueState(*CAI), getMaxWidenStepsOpts());
1248     }
1249   }
1250 }
1251 
1252 void SCCPSolver::handleCallResult(CallBase &CB) {
1253   Function *F = CB.getCalledFunction();
1254 
1255   if (auto *II = dyn_cast<IntrinsicInst>(&CB)) {
1256     if (II->getIntrinsicID() == Intrinsic::ssa_copy) {
1257       if (ValueState[&CB].isOverdefined())
1258         return;
1259 
1260       Value *CopyOf = CB.getOperand(0);
1261       ValueLatticeElement OriginalVal = getValueState(CopyOf);
1262       auto *PI = getPredicateInfoFor(&CB);
1263       assert(PI && "Missing predicate info for ssa.copy");
1264 
1265       CmpInst *Cmp;
1266       bool TrueEdge;
1267       if (auto *PBranch = dyn_cast<PredicateBranch>(PI)) {
1268         Cmp = dyn_cast<CmpInst>(PBranch->Condition);
1269         TrueEdge = PBranch->TrueEdge;
1270       } else if (auto *PAssume = dyn_cast<PredicateAssume>(PI)) {
1271         Cmp = dyn_cast<CmpInst>(PAssume->Condition);
1272         TrueEdge = true;
1273       } else {
1274         mergeInValue(ValueState[&CB], &CB, OriginalVal);
1275         return;
1276       }
1277 
1278       // Everything below relies on the condition being a comparison.
1279       if (!Cmp) {
1280         mergeInValue(ValueState[&CB], &CB, OriginalVal);
1281         return;
1282       }
1283 
1284       Value *CmpOp0 = Cmp->getOperand(0);
1285       Value *CmpOp1 = Cmp->getOperand(1);
1286       if (CopyOf != CmpOp0 && CopyOf != CmpOp1) {
1287         mergeInValue(ValueState[&CB], &CB, OriginalVal);
1288         return;
1289       }
1290 
1291       auto Pred = Cmp->getPredicate();
1292       if (CmpOp0 != CopyOf) {
1293         std::swap(CmpOp0, CmpOp1);
1294         Pred = Cmp->getSwappedPredicate();
1295       }
1296 
1297       // Wait until CmpOp1 is resolved.
1298       if (getValueState(CmpOp1).isUnknown()) {
1299         addAdditionalUser(CmpOp1, &CB);
1300         return;
1301       }
1302 
1303       if (!TrueEdge)
1304         Pred = CmpInst::getInversePredicate(Pred);
1305 
1306       ValueLatticeElement CondVal = getValueState(CmpOp1);
1307       ValueLatticeElement &IV = ValueState[&CB];
1308       if (CondVal.isConstantRange() || OriginalVal.isConstantRange()) {
1309         auto NewCR =
1310             ConstantRange::getFull(DL.getTypeSizeInBits(CopyOf->getType()));
1311 
1312         // Get the range imposed by the condition.
1313         if (CondVal.isConstantRange())
1314           NewCR = ConstantRange::makeAllowedICmpRegion(
1315               Pred, CondVal.getConstantRange());
1316 
1317         // Combine range info for the original value with the new range from the
1318         // condition.
1319         auto OriginalCR = OriginalVal.isConstantRange()
1320                               ? OriginalVal.getConstantRange()
1321                               : ConstantRange::getFull(
1322                                     DL.getTypeSizeInBits(CopyOf->getType()));
1323         NewCR = NewCR.intersectWith(OriginalCR);
1324 
1325         addAdditionalUser(CmpOp1, &CB);
1326         // TODO: Actually filp MayIncludeUndef for the created range to false,
1327         // once most places in the optimizer respect the branches on
1328         // undef/poison are UB rule. The reason why the new range cannot be
1329         // undef is as follows below:
1330         // The new range is based on a branch condition. That guarantees that
1331         // neither of the compare operands can be undef in the branch targets,
1332         // unless we have conditions that are always true/false (e.g. icmp ule
1333         // i32, %a, i32_max). For the latter overdefined/empty range will be
1334         // inferred, but the branch will get folded accordingly anyways.
1335         mergeInValue(
1336             IV, &CB,
1337             ValueLatticeElement::getRange(NewCR, /*MayIncludeUndef=*/true));
1338         return;
1339       } else if (Pred == CmpInst::ICMP_EQ && CondVal.isConstant()) {
1340         // For non-integer values or integer constant expressions, only
1341         // propagate equal constants.
1342         addAdditionalUser(CmpOp1, &CB);
1343         mergeInValue(IV, &CB, CondVal);
1344         return;
1345       }
1346 
1347       return (void)mergeInValue(IV, &CB, OriginalVal);
1348     }
1349   }
1350 
1351   // The common case is that we aren't tracking the callee, either because we
1352   // are not doing interprocedural analysis or the callee is indirect, or is
1353   // external.  Handle these cases first.
1354   if (!F || F->isDeclaration())
1355     return handleCallOverdefined(CB);
1356 
1357   // If this is a single/zero retval case, see if we're tracking the function.
1358   if (auto *STy = dyn_cast<StructType>(F->getReturnType())) {
1359     if (!MRVFunctionsTracked.count(F))
1360       return handleCallOverdefined(CB); // Not tracking this callee.
1361 
1362     // If we are tracking this callee, propagate the result of the function
1363     // into this call site.
1364     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
1365       mergeInValue(getStructValueState(&CB, i), &CB,
1366                    TrackedMultipleRetVals[std::make_pair(F, i)],
1367                    getMaxWidenStepsOpts());
1368   } else {
1369     auto TFRVI = TrackedRetVals.find(F);
1370     if (TFRVI == TrackedRetVals.end())
1371       return handleCallOverdefined(CB); // Not tracking this callee.
1372 
1373     // If so, propagate the return value of the callee into this call result.
1374     mergeInValue(&CB, TFRVI->second, getMaxWidenStepsOpts());
1375   }
1376 }
1377 
1378 void SCCPSolver::Solve() {
1379   // Process the work lists until they are empty!
1380   while (!BBWorkList.empty() || !InstWorkList.empty() ||
1381          !OverdefinedInstWorkList.empty()) {
1382     // Process the overdefined instruction's work list first, which drives other
1383     // things to overdefined more quickly.
1384     while (!OverdefinedInstWorkList.empty()) {
1385       Value *I = OverdefinedInstWorkList.pop_back_val();
1386 
1387       LLVM_DEBUG(dbgs() << "\nPopped off OI-WL: " << *I << '\n');
1388 
1389       // "I" got into the work list because it either made the transition from
1390       // bottom to constant, or to overdefined.
1391       //
1392       // Anything on this worklist that is overdefined need not be visited
1393       // since all of its users will have already been marked as overdefined
1394       // Update all of the users of this instruction's value.
1395       //
1396       markUsersAsChanged(I);
1397     }
1398 
1399     // Process the instruction work list.
1400     while (!InstWorkList.empty()) {
1401       Value *I = InstWorkList.pop_back_val();
1402 
1403       LLVM_DEBUG(dbgs() << "\nPopped off I-WL: " << *I << '\n');
1404 
1405       // "I" got into the work list because it made the transition from undef to
1406       // constant.
1407       //
1408       // Anything on this worklist that is overdefined need not be visited
1409       // since all of its users will have already been marked as overdefined.
1410       // Update all of the users of this instruction's value.
1411       //
1412       if (I->getType()->isStructTy() || !getValueState(I).isOverdefined())
1413         markUsersAsChanged(I);
1414     }
1415 
1416     // Process the basic block work list.
1417     while (!BBWorkList.empty()) {
1418       BasicBlock *BB = BBWorkList.back();
1419       BBWorkList.pop_back();
1420 
1421       LLVM_DEBUG(dbgs() << "\nPopped off BBWL: " << *BB << '\n');
1422 
1423       // Notify all instructions in this basic block that they are newly
1424       // executable.
1425       visit(BB);
1426     }
1427   }
1428 }
1429 
1430 /// ResolvedUndefsIn - While solving the dataflow for a function, we assume
1431 /// that branches on undef values cannot reach any of their successors.
1432 /// However, this is not a safe assumption.  After we solve dataflow, this
1433 /// method should be use to handle this.  If this returns true, the solver
1434 /// should be rerun.
1435 ///
1436 /// This method handles this by finding an unresolved branch and marking it one
1437 /// of the edges from the block as being feasible, even though the condition
1438 /// doesn't say it would otherwise be.  This allows SCCP to find the rest of the
1439 /// CFG and only slightly pessimizes the analysis results (by marking one,
1440 /// potentially infeasible, edge feasible).  This cannot usefully modify the
1441 /// constraints on the condition of the branch, as that would impact other users
1442 /// of the value.
1443 ///
1444 /// This scan also checks for values that use undefs. It conservatively marks
1445 /// them as overdefined.
1446 bool SCCPSolver::ResolvedUndefsIn(Function &F) {
1447   for (BasicBlock &BB : F) {
1448     if (!BBExecutable.count(&BB))
1449       continue;
1450 
1451     for (Instruction &I : BB) {
1452       // Look for instructions which produce undef values.
1453       if (I.getType()->isVoidTy()) continue;
1454 
1455       if (auto *STy = dyn_cast<StructType>(I.getType())) {
1456         // Only a few things that can be structs matter for undef.
1457 
1458         // Tracked calls must never be marked overdefined in ResolvedUndefsIn.
1459         if (auto *CB = dyn_cast<CallBase>(&I))
1460           if (Function *F = CB->getCalledFunction())
1461             if (MRVFunctionsTracked.count(F))
1462               continue;
1463 
1464         // extractvalue and insertvalue don't need to be marked; they are
1465         // tracked as precisely as their operands.
1466         if (isa<ExtractValueInst>(I) || isa<InsertValueInst>(I))
1467           continue;
1468         // Send the results of everything else to overdefined.  We could be
1469         // more precise than this but it isn't worth bothering.
1470         for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1471           ValueLatticeElement &LV = getStructValueState(&I, i);
1472           if (LV.isUnknownOrUndef())
1473             markOverdefined(LV, &I);
1474         }
1475         continue;
1476       }
1477 
1478       ValueLatticeElement &LV = getValueState(&I);
1479       if (!LV.isUnknownOrUndef())
1480         continue;
1481 
1482       // There are two reasons a call can have an undef result
1483       // 1. It could be tracked.
1484       // 2. It could be constant-foldable.
1485       // Because of the way we solve return values, tracked calls must
1486       // never be marked overdefined in ResolvedUndefsIn.
1487       if (auto *CB = dyn_cast<CallBase>(&I))
1488         if (Function *F = CB->getCalledFunction())
1489           if (TrackedRetVals.count(F))
1490             continue;
1491 
1492       if (isa<LoadInst>(I)) {
1493         // A load here means one of two things: a load of undef from a global,
1494         // a load from an unknown pointer.  Either way, having it return undef
1495         // is okay.
1496         continue;
1497       }
1498 
1499       markOverdefined(&I);
1500       return true;
1501     }
1502 
1503     // Check to see if we have a branch or switch on an undefined value.  If so
1504     // we force the branch to go one way or the other to make the successor
1505     // values live.  It doesn't really matter which way we force it.
1506     Instruction *TI = BB.getTerminator();
1507     if (auto *BI = dyn_cast<BranchInst>(TI)) {
1508       if (!BI->isConditional()) continue;
1509       if (!getValueState(BI->getCondition()).isUnknownOrUndef())
1510         continue;
1511 
1512       // If the input to SCCP is actually branch on undef, fix the undef to
1513       // false.
1514       if (isa<UndefValue>(BI->getCondition())) {
1515         BI->setCondition(ConstantInt::getFalse(BI->getContext()));
1516         markEdgeExecutable(&BB, TI->getSuccessor(1));
1517         return true;
1518       }
1519 
1520       // Otherwise, it is a branch on a symbolic value which is currently
1521       // considered to be undef.  Make sure some edge is executable, so a
1522       // branch on "undef" always flows somewhere.
1523       // FIXME: Distinguish between dead code and an LLVM "undef" value.
1524       BasicBlock *DefaultSuccessor = TI->getSuccessor(1);
1525       if (markEdgeExecutable(&BB, DefaultSuccessor))
1526         return true;
1527 
1528       continue;
1529     }
1530 
1531    if (auto *IBR = dyn_cast<IndirectBrInst>(TI)) {
1532       // Indirect branch with no successor ?. Its ok to assume it branches
1533       // to no target.
1534       if (IBR->getNumSuccessors() < 1)
1535         continue;
1536 
1537       if (!getValueState(IBR->getAddress()).isUnknownOrUndef())
1538         continue;
1539 
1540       // If the input to SCCP is actually branch on undef, fix the undef to
1541       // the first successor of the indirect branch.
1542       if (isa<UndefValue>(IBR->getAddress())) {
1543         IBR->setAddress(BlockAddress::get(IBR->getSuccessor(0)));
1544         markEdgeExecutable(&BB, IBR->getSuccessor(0));
1545         return true;
1546       }
1547 
1548       // Otherwise, it is a branch on a symbolic value which is currently
1549       // considered to be undef.  Make sure some edge is executable, so a
1550       // branch on "undef" always flows somewhere.
1551       // FIXME: IndirectBr on "undef" doesn't actually need to go anywhere:
1552       // we can assume the branch has undefined behavior instead.
1553       BasicBlock *DefaultSuccessor = IBR->getSuccessor(0);
1554       if (markEdgeExecutable(&BB, DefaultSuccessor))
1555         return true;
1556 
1557       continue;
1558     }
1559 
1560     if (auto *SI = dyn_cast<SwitchInst>(TI)) {
1561       if (!SI->getNumCases() ||
1562           !getValueState(SI->getCondition()).isUnknownOrUndef())
1563         continue;
1564 
1565       // If the input to SCCP is actually switch on undef, fix the undef to
1566       // the first constant.
1567       if (isa<UndefValue>(SI->getCondition())) {
1568         SI->setCondition(SI->case_begin()->getCaseValue());
1569         markEdgeExecutable(&BB, SI->case_begin()->getCaseSuccessor());
1570         return true;
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: Distinguish between dead code and an LLVM "undef" value.
1577       BasicBlock *DefaultSuccessor = SI->case_begin()->getCaseSuccessor();
1578       if (markEdgeExecutable(&BB, DefaultSuccessor))
1579         return true;
1580 
1581       continue;
1582     }
1583   }
1584 
1585   return false;
1586 }
1587 
1588 static bool tryToReplaceWithConstant(SCCPSolver &Solver, Value *V) {
1589   Constant *Const = nullptr;
1590   if (V->getType()->isStructTy()) {
1591     std::vector<ValueLatticeElement> IVs = Solver.getStructLatticeValueFor(V);
1592     if (any_of(IVs,
1593                [](const ValueLatticeElement &LV) { return isOverdefined(LV); }))
1594       return false;
1595     std::vector<Constant *> ConstVals;
1596     auto *ST = cast<StructType>(V->getType());
1597     for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) {
1598       ValueLatticeElement V = IVs[i];
1599       ConstVals.push_back(isConstant(V)
1600                               ? Solver.getConstant(V)
1601                               : UndefValue::get(ST->getElementType(i)));
1602     }
1603     Const = ConstantStruct::get(ST, ConstVals);
1604   } else {
1605     const ValueLatticeElement &IV = Solver.getLatticeValueFor(V);
1606     if (isOverdefined(IV))
1607       return false;
1608 
1609     Const =
1610         isConstant(IV) ? Solver.getConstant(IV) : UndefValue::get(V->getType());
1611   }
1612   assert(Const && "Constant is nullptr here!");
1613 
1614   // Replacing `musttail` instructions with constant breaks `musttail` invariant
1615   // unless the call itself can be removed
1616   CallInst *CI = dyn_cast<CallInst>(V);
1617   if (CI && CI->isMustTailCall() && !CI->isSafeToRemove()) {
1618     Function *F = CI->getCalledFunction();
1619 
1620     // Don't zap returns of the callee
1621     if (F)
1622       Solver.AddMustTailCallee(F);
1623 
1624     LLVM_DEBUG(dbgs() << "  Can\'t treat the result of musttail call : " << *CI
1625                       << " as a constant\n");
1626     return false;
1627   }
1628 
1629   LLVM_DEBUG(dbgs() << "  Constant: " << *Const << " = " << *V << '\n');
1630 
1631   // Replaces all of the uses of a variable with uses of the constant.
1632   V->replaceAllUsesWith(Const);
1633   return true;
1634 }
1635 
1636 static bool simplifyInstsInBlock(SCCPSolver &Solver, BasicBlock &BB,
1637                                  SmallPtrSetImpl<Value *> &InsertedValues,
1638                                  Statistic &InstRemovedStat,
1639                                  Statistic &InstReplacedStat) {
1640   bool MadeChanges = false;
1641   for (Instruction &Inst : make_early_inc_range(BB)) {
1642     if (Inst.getType()->isVoidTy())
1643       continue;
1644     if (tryToReplaceWithConstant(Solver, &Inst)) {
1645       if (Inst.isSafeToRemove())
1646         Inst.eraseFromParent();
1647       // Hey, we just changed something!
1648       MadeChanges = true;
1649       ++InstRemovedStat;
1650     } else if (isa<SExtInst>(&Inst)) {
1651       Value *ExtOp = Inst.getOperand(0);
1652       if (isa<Constant>(ExtOp) || InsertedValues.count(ExtOp))
1653         continue;
1654       const ValueLatticeElement &IV = Solver.getLatticeValueFor(ExtOp);
1655       if (!IV.isConstantRange(/*UndefAllowed=*/false))
1656         continue;
1657       if (IV.getConstantRange().isAllNonNegative()) {
1658         auto *ZExt = new ZExtInst(ExtOp, Inst.getType(), "", &Inst);
1659         InsertedValues.insert(ZExt);
1660         Inst.replaceAllUsesWith(ZExt);
1661         Solver.removeLatticeValueFor(&Inst);
1662         Inst.eraseFromParent();
1663         InstReplacedStat++;
1664         MadeChanges = true;
1665       }
1666     }
1667   }
1668   return MadeChanges;
1669 }
1670 
1671 // runSCCP() - Run the Sparse Conditional Constant Propagation algorithm,
1672 // and return true if the function was modified.
1673 static bool runSCCP(Function &F, const DataLayout &DL,
1674                     const TargetLibraryInfo *TLI) {
1675   LLVM_DEBUG(dbgs() << "SCCP on function '" << F.getName() << "'\n");
1676   SCCPSolver Solver(
1677       DL, [TLI](Function &F) -> const TargetLibraryInfo & { return *TLI; },
1678       F.getContext());
1679 
1680   // Mark the first block of the function as being executable.
1681   Solver.MarkBlockExecutable(&F.front());
1682 
1683   // Mark all arguments to the function as being overdefined.
1684   for (Argument &AI : F.args())
1685     Solver.markOverdefined(&AI);
1686 
1687   // Solve for constants.
1688   bool ResolvedUndefs = true;
1689   while (ResolvedUndefs) {
1690     Solver.Solve();
1691     LLVM_DEBUG(dbgs() << "RESOLVING UNDEFs\n");
1692     ResolvedUndefs = Solver.ResolvedUndefsIn(F);
1693   }
1694 
1695   bool MadeChanges = false;
1696 
1697   // If we decided that there are basic blocks that are dead in this function,
1698   // delete their contents now.  Note that we cannot actually delete the blocks,
1699   // as we cannot modify the CFG of the function.
1700 
1701   SmallPtrSet<Value *, 32> InsertedValues;
1702   for (BasicBlock &BB : F) {
1703     if (!Solver.isBlockExecutable(&BB)) {
1704       LLVM_DEBUG(dbgs() << "  BasicBlock Dead:" << BB);
1705 
1706       ++NumDeadBlocks;
1707       NumInstRemoved += removeAllNonTerminatorAndEHPadInstructions(&BB);
1708 
1709       MadeChanges = true;
1710       continue;
1711     }
1712 
1713     MadeChanges |= simplifyInstsInBlock(Solver, BB, InsertedValues,
1714                                         NumInstRemoved, NumInstReplaced);
1715   }
1716 
1717   return MadeChanges;
1718 }
1719 
1720 PreservedAnalyses SCCPPass::run(Function &F, FunctionAnalysisManager &AM) {
1721   const DataLayout &DL = F.getParent()->getDataLayout();
1722   auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
1723   if (!runSCCP(F, DL, &TLI))
1724     return PreservedAnalyses::all();
1725 
1726   auto PA = PreservedAnalyses();
1727   PA.preserve<GlobalsAA>();
1728   PA.preserveSet<CFGAnalyses>();
1729   return PA;
1730 }
1731 
1732 namespace {
1733 
1734 //===--------------------------------------------------------------------===//
1735 //
1736 /// SCCP Class - This class uses the SCCPSolver to implement a per-function
1737 /// Sparse Conditional Constant Propagator.
1738 ///
1739 class SCCPLegacyPass : public FunctionPass {
1740 public:
1741   // Pass identification, replacement for typeid
1742   static char ID;
1743 
1744   SCCPLegacyPass() : FunctionPass(ID) {
1745     initializeSCCPLegacyPassPass(*PassRegistry::getPassRegistry());
1746   }
1747 
1748   void getAnalysisUsage(AnalysisUsage &AU) const override {
1749     AU.addRequired<TargetLibraryInfoWrapperPass>();
1750     AU.addPreserved<GlobalsAAWrapperPass>();
1751     AU.setPreservesCFG();
1752   }
1753 
1754   // runOnFunction - Run the Sparse Conditional Constant Propagation
1755   // algorithm, and return true if the function was modified.
1756   bool runOnFunction(Function &F) override {
1757     if (skipFunction(F))
1758       return false;
1759     const DataLayout &DL = F.getParent()->getDataLayout();
1760     const TargetLibraryInfo *TLI =
1761         &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
1762     return runSCCP(F, DL, TLI);
1763   }
1764 };
1765 
1766 } // end anonymous namespace
1767 
1768 char SCCPLegacyPass::ID = 0;
1769 
1770 INITIALIZE_PASS_BEGIN(SCCPLegacyPass, "sccp",
1771                       "Sparse Conditional Constant Propagation", false, false)
1772 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
1773 INITIALIZE_PASS_END(SCCPLegacyPass, "sccp",
1774                     "Sparse Conditional Constant Propagation", false, false)
1775 
1776 // createSCCPPass - This is the public interface to this file.
1777 FunctionPass *llvm::createSCCPPass() { return new SCCPLegacyPass(); }
1778 
1779 static void findReturnsToZap(Function &F,
1780                              SmallVector<ReturnInst *, 8> &ReturnsToZap,
1781                              SCCPSolver &Solver) {
1782   // We can only do this if we know that nothing else can call the function.
1783   if (!Solver.isArgumentTrackedFunction(&F))
1784     return;
1785 
1786   // There is a non-removable musttail call site of this function. Zapping
1787   // returns is not allowed.
1788   if (Solver.isMustTailCallee(&F)) {
1789     LLVM_DEBUG(dbgs() << "Can't zap returns of the function : " << F.getName()
1790                       << " due to present musttail call of it\n");
1791     return;
1792   }
1793 
1794   assert(
1795       all_of(F.users(),
1796              [&Solver](User *U) {
1797                if (isa<Instruction>(U) &&
1798                    !Solver.isBlockExecutable(cast<Instruction>(U)->getParent()))
1799                  return true;
1800                // Non-callsite uses are not impacted by zapping. Also, constant
1801                // uses (like blockaddresses) could stuck around, without being
1802                // used in the underlying IR, meaning we do not have lattice
1803                // values for them.
1804                if (!isa<CallBase>(U))
1805                  return true;
1806                if (U->getType()->isStructTy()) {
1807                  return all_of(Solver.getStructLatticeValueFor(U),
1808                                [](const ValueLatticeElement &LV) {
1809                                  return !isOverdefined(LV);
1810                                });
1811                }
1812                return !isOverdefined(Solver.getLatticeValueFor(U));
1813              }) &&
1814       "We can only zap functions where all live users have a concrete value");
1815 
1816   for (BasicBlock &BB : F) {
1817     if (CallInst *CI = BB.getTerminatingMustTailCall()) {
1818       LLVM_DEBUG(dbgs() << "Can't zap return of the block due to present "
1819                         << "musttail call : " << *CI << "\n");
1820       (void)CI;
1821       return;
1822     }
1823 
1824     if (auto *RI = dyn_cast<ReturnInst>(BB.getTerminator()))
1825       if (!isa<UndefValue>(RI->getOperand(0)))
1826         ReturnsToZap.push_back(RI);
1827   }
1828 }
1829 
1830 // Update the condition for terminators that are branching on indeterminate
1831 // values, forcing them to use a specific edge.
1832 static void forceIndeterminateEdge(Instruction* I, SCCPSolver &Solver) {
1833   BasicBlock *Dest = nullptr;
1834   Constant *C = nullptr;
1835   if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
1836     if (!isa<ConstantInt>(SI->getCondition())) {
1837       // Indeterminate switch; use first case value.
1838       Dest = SI->case_begin()->getCaseSuccessor();
1839       C = SI->case_begin()->getCaseValue();
1840     }
1841   } else if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
1842     if (!isa<ConstantInt>(BI->getCondition())) {
1843       // Indeterminate branch; use false.
1844       Dest = BI->getSuccessor(1);
1845       C = ConstantInt::getFalse(BI->getContext());
1846     }
1847   } else if (IndirectBrInst *IBR = dyn_cast<IndirectBrInst>(I)) {
1848     if (!isa<BlockAddress>(IBR->getAddress()->stripPointerCasts())) {
1849       // Indeterminate indirectbr; use successor 0.
1850       Dest = IBR->getSuccessor(0);
1851       C = BlockAddress::get(IBR->getSuccessor(0));
1852     }
1853   } else {
1854     llvm_unreachable("Unexpected terminator instruction");
1855   }
1856   if (C) {
1857     assert(Solver.isEdgeFeasible(I->getParent(), Dest) &&
1858            "Didn't find feasible edge?");
1859     (void)Dest;
1860 
1861     I->setOperand(0, C);
1862   }
1863 }
1864 
1865 bool llvm::runIPSCCP(
1866     Module &M, const DataLayout &DL,
1867     std::function<const TargetLibraryInfo &(Function &)> GetTLI,
1868     function_ref<AnalysisResultsForFn(Function &)> getAnalysis) {
1869   SCCPSolver Solver(DL, GetTLI, M.getContext());
1870 
1871   // Loop over all functions, marking arguments to those with their addresses
1872   // taken or that are external as overdefined.
1873   for (Function &F : M) {
1874     if (F.isDeclaration())
1875       continue;
1876 
1877     Solver.addAnalysis(F, getAnalysis(F));
1878 
1879     // Determine if we can track the function's return values. If so, add the
1880     // function to the solver's set of return-tracked functions.
1881     if (canTrackReturnsInterprocedurally(&F))
1882       Solver.AddTrackedFunction(&F);
1883 
1884     // Determine if we can track the function's arguments. If so, add the
1885     // function to the solver's set of argument-tracked functions.
1886     if (canTrackArgumentsInterprocedurally(&F)) {
1887       Solver.AddArgumentTrackedFunction(&F);
1888       continue;
1889     }
1890 
1891     // Assume the function is called.
1892     Solver.MarkBlockExecutable(&F.front());
1893 
1894     // Assume nothing about the incoming arguments.
1895     for (Argument &AI : F.args())
1896       Solver.markOverdefined(&AI);
1897   }
1898 
1899   // Determine if we can track any of the module's global variables. If so, add
1900   // the global variables we can track to the solver's set of tracked global
1901   // variables.
1902   for (GlobalVariable &G : M.globals()) {
1903     G.removeDeadConstantUsers();
1904     if (canTrackGlobalVariableInterprocedurally(&G))
1905       Solver.TrackValueOfGlobalVariable(&G);
1906   }
1907 
1908   // Solve for constants.
1909   bool ResolvedUndefs = true;
1910   Solver.Solve();
1911   while (ResolvedUndefs) {
1912     LLVM_DEBUG(dbgs() << "RESOLVING UNDEFS\n");
1913     ResolvedUndefs = false;
1914     for (Function &F : M)
1915       if (Solver.ResolvedUndefsIn(F)) {
1916         // We run Solve() after we resolved an undef in a function, because
1917         // we might deduce a fact that eliminates an undef in another function.
1918         Solver.Solve();
1919         ResolvedUndefs = true;
1920       }
1921   }
1922 
1923   bool MadeChanges = false;
1924 
1925   // Iterate over all of the instructions in the module, replacing them with
1926   // constants if we have found them to be of constant values.
1927 
1928   for (Function &F : M) {
1929     if (F.isDeclaration())
1930       continue;
1931 
1932     SmallVector<BasicBlock *, 512> BlocksToErase;
1933 
1934     if (Solver.isBlockExecutable(&F.front()))
1935       for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E;
1936            ++AI) {
1937         if (!AI->use_empty() && tryToReplaceWithConstant(Solver, &*AI)) {
1938           ++IPNumArgsElimed;
1939           continue;
1940         }
1941       }
1942 
1943     SmallPtrSet<Value *, 32> InsertedValues;
1944     for (BasicBlock &BB : F) {
1945       if (!Solver.isBlockExecutable(&BB)) {
1946         LLVM_DEBUG(dbgs() << "  BasicBlock Dead:" << BB);
1947         ++NumDeadBlocks;
1948 
1949         MadeChanges = true;
1950 
1951         if (&BB != &F.front())
1952           BlocksToErase.push_back(&BB);
1953         continue;
1954       }
1955 
1956       MadeChanges |= simplifyInstsInBlock(Solver, BB, InsertedValues,
1957                                           IPNumInstRemoved, IPNumInstReplaced);
1958     }
1959 
1960     DomTreeUpdater DTU = Solver.getDTU(F);
1961     // Change dead blocks to unreachable. We do it after replacing constants
1962     // in all executable blocks, because changeToUnreachable may remove PHI
1963     // nodes in executable blocks we found values for. The function's entry
1964     // block is not part of BlocksToErase, so we have to handle it separately.
1965     for (BasicBlock *BB : BlocksToErase) {
1966       NumInstRemoved +=
1967           changeToUnreachable(BB->getFirstNonPHI(), /*UseLLVMTrap=*/false,
1968                               /*PreserveLCSSA=*/false, &DTU);
1969     }
1970     if (!Solver.isBlockExecutable(&F.front()))
1971       NumInstRemoved += changeToUnreachable(F.front().getFirstNonPHI(),
1972                                             /*UseLLVMTrap=*/false,
1973                                             /*PreserveLCSSA=*/false, &DTU);
1974 
1975     // Now that all instructions in the function are constant folded,
1976     // use ConstantFoldTerminator to get rid of in-edges, record DT updates and
1977     // delete dead BBs.
1978     for (BasicBlock *DeadBB : BlocksToErase) {
1979       // If there are any PHI nodes in this successor, drop entries for BB now.
1980       for (Value::user_iterator UI = DeadBB->user_begin(),
1981                                 UE = DeadBB->user_end();
1982            UI != UE;) {
1983         // Grab the user and then increment the iterator early, as the user
1984         // will be deleted. Step past all adjacent uses from the same user.
1985         auto *I = dyn_cast<Instruction>(*UI);
1986         do { ++UI; } while (UI != UE && *UI == I);
1987 
1988         // Ignore blockaddress users; BasicBlock's dtor will handle them.
1989         if (!I) continue;
1990 
1991         // If we have forced an edge for an indeterminate value, then force the
1992         // terminator to fold to that edge.
1993         forceIndeterminateEdge(I, Solver);
1994         BasicBlock *InstBB = I->getParent();
1995         bool Folded = ConstantFoldTerminator(InstBB,
1996                                              /*DeleteDeadConditions=*/false,
1997                                              /*TLI=*/nullptr, &DTU);
1998         assert(Folded &&
1999               "Expect TermInst on constantint or blockaddress to be folded");
2000         (void) Folded;
2001         // If we folded the terminator to an unconditional branch to another
2002         // dead block, replace it with Unreachable, to avoid trying to fold that
2003         // branch again.
2004         BranchInst *BI = cast<BranchInst>(InstBB->getTerminator());
2005         if (BI && BI->isUnconditional() &&
2006             !Solver.isBlockExecutable(BI->getSuccessor(0))) {
2007           InstBB->getTerminator()->eraseFromParent();
2008           new UnreachableInst(InstBB->getContext(), InstBB);
2009         }
2010       }
2011       // Mark dead BB for deletion.
2012       DTU.deleteBB(DeadBB);
2013     }
2014 
2015     for (BasicBlock &BB : F) {
2016       for (BasicBlock::iterator BI = BB.begin(), E = BB.end(); BI != E;) {
2017         Instruction *Inst = &*BI++;
2018         if (Solver.getPredicateInfoFor(Inst)) {
2019           if (auto *II = dyn_cast<IntrinsicInst>(Inst)) {
2020             if (II->getIntrinsicID() == Intrinsic::ssa_copy) {
2021               Value *Op = II->getOperand(0);
2022               Inst->replaceAllUsesWith(Op);
2023               Inst->eraseFromParent();
2024             }
2025           }
2026         }
2027       }
2028     }
2029   }
2030 
2031   // If we inferred constant or undef return values for a function, we replaced
2032   // all call uses with the inferred value.  This means we don't need to bother
2033   // actually returning anything from the function.  Replace all return
2034   // instructions with return undef.
2035   //
2036   // Do this in two stages: first identify the functions we should process, then
2037   // actually zap their returns.  This is important because we can only do this
2038   // if the address of the function isn't taken.  In cases where a return is the
2039   // last use of a function, the order of processing functions would affect
2040   // whether other functions are optimizable.
2041   SmallVector<ReturnInst*, 8> ReturnsToZap;
2042 
2043   for (const auto &I : Solver.getTrackedRetVals()) {
2044     Function *F = I.first;
2045     if (isOverdefined(I.second) || F->getReturnType()->isVoidTy())
2046       continue;
2047     findReturnsToZap(*F, ReturnsToZap, Solver);
2048   }
2049 
2050   for (auto F : Solver.getMRVFunctionsTracked()) {
2051     assert(F->getReturnType()->isStructTy() &&
2052            "The return type should be a struct");
2053     StructType *STy = cast<StructType>(F->getReturnType());
2054     if (Solver.isStructLatticeConstant(F, STy))
2055       findReturnsToZap(*F, ReturnsToZap, Solver);
2056   }
2057 
2058   // Zap all returns which we've identified as zap to change.
2059   for (unsigned i = 0, e = ReturnsToZap.size(); i != e; ++i) {
2060     Function *F = ReturnsToZap[i]->getParent()->getParent();
2061     ReturnsToZap[i]->setOperand(0, UndefValue::get(F->getReturnType()));
2062   }
2063 
2064   // If we inferred constant or undef values for globals variables, we can
2065   // delete the global and any stores that remain to it.
2066   for (auto &I : make_early_inc_range(Solver.getTrackedGlobals())) {
2067     GlobalVariable *GV = I.first;
2068     if (isOverdefined(I.second))
2069       continue;
2070     LLVM_DEBUG(dbgs() << "Found that GV '" << GV->getName()
2071                       << "' is constant!\n");
2072     while (!GV->use_empty()) {
2073       StoreInst *SI = cast<StoreInst>(GV->user_back());
2074       SI->eraseFromParent();
2075       MadeChanges = true;
2076     }
2077     M.getGlobalList().erase(GV);
2078     ++IPNumGlobalConst;
2079   }
2080 
2081   return MadeChanges;
2082 }
2083