1 //===- SCCP.cpp - Sparse Conditional Constant Propagation -----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements sparse conditional constant propagation and merging:
11 //
12 // Specifically, this:
13 //   * Assumes values are constant unless proven otherwise
14 //   * Assumes BasicBlocks are dead unless proven otherwise
15 //   * Proves values to be constant, and replaces them with constants
16 //   * Proves conditional branches to be unconditional
17 //
18 //===----------------------------------------------------------------------===//
19 
20 #include "llvm/Transforms/IPO/SCCP.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/DenseSet.h"
23 #include "llvm/ADT/PointerIntPair.h"
24 #include "llvm/ADT/SmallPtrSet.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/Statistic.h"
27 #include "llvm/Analysis/ConstantFolding.h"
28 #include "llvm/Analysis/GlobalsModRef.h"
29 #include "llvm/Analysis/TargetLibraryInfo.h"
30 #include "llvm/IR/CallSite.h"
31 #include "llvm/IR/Constants.h"
32 #include "llvm/IR/DataLayout.h"
33 #include "llvm/IR/DerivedTypes.h"
34 #include "llvm/IR/InstVisitor.h"
35 #include "llvm/IR/Instructions.h"
36 #include "llvm/Pass.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include "llvm/Support/raw_ostream.h"
40 #include "llvm/Transforms/IPO.h"
41 #include "llvm/Transforms/Scalar.h"
42 #include "llvm/Transforms/Scalar/SCCP.h"
43 #include "llvm/Transforms/Utils/Local.h"
44 #include <algorithm>
45 using namespace llvm;
46 
47 #define DEBUG_TYPE "sccp"
48 
49 STATISTIC(NumInstRemoved, "Number of instructions removed");
50 STATISTIC(NumDeadBlocks , "Number of basic blocks unreachable");
51 
52 STATISTIC(IPNumInstRemoved, "Number of instructions removed by IPSCCP");
53 STATISTIC(IPNumArgsElimed ,"Number of arguments constant propagated by IPSCCP");
54 STATISTIC(IPNumGlobalConst, "Number of globals found to be constant by IPSCCP");
55 
56 namespace {
57 /// LatticeVal class - This class represents the different lattice values that
58 /// an LLVM value may occupy.  It is a simple class with value semantics.
59 ///
60 class LatticeVal {
61   enum LatticeValueTy {
62     /// unknown - This LLVM Value has no known value yet.
63     unknown,
64 
65     /// constant - This LLVM Value has a specific constant value.
66     constant,
67 
68     /// forcedconstant - This LLVM Value was thought to be undef until
69     /// ResolvedUndefsIn.  This is treated just like 'constant', but if merged
70     /// with another (different) constant, it goes to overdefined, instead of
71     /// asserting.
72     forcedconstant,
73 
74     /// overdefined - This instruction is not known to be constant, and we know
75     /// it has a value.
76     overdefined
77   };
78 
79   /// Val: This stores the current lattice value along with the Constant* for
80   /// the constant if this is a 'constant' or 'forcedconstant' value.
81   PointerIntPair<Constant *, 2, LatticeValueTy> Val;
82 
83   LatticeValueTy getLatticeValue() const {
84     return Val.getInt();
85   }
86 
87 public:
88   LatticeVal() : Val(nullptr, unknown) {}
89 
90   bool isUnknown() const { return getLatticeValue() == unknown; }
91   bool isConstant() const {
92     return getLatticeValue() == constant || getLatticeValue() == forcedconstant;
93   }
94   bool isOverdefined() const { return getLatticeValue() == overdefined; }
95 
96   Constant *getConstant() const {
97     assert(isConstant() && "Cannot get the constant of a non-constant!");
98     return Val.getPointer();
99   }
100 
101   /// markOverdefined - Return true if this is a change in status.
102   bool markOverdefined() {
103     if (isOverdefined())
104       return false;
105 
106     Val.setInt(overdefined);
107     return true;
108   }
109 
110   /// markConstant - Return true if this is a change in status.
111   bool markConstant(Constant *V) {
112     if (getLatticeValue() == constant) { // Constant but not forcedconstant.
113       assert(getConstant() == V && "Marking constant with different value");
114       return false;
115     }
116 
117     if (isUnknown()) {
118       Val.setInt(constant);
119       assert(V && "Marking constant with NULL");
120       Val.setPointer(V);
121     } else {
122       assert(getLatticeValue() == forcedconstant &&
123              "Cannot move from overdefined to constant!");
124       // Stay at forcedconstant if the constant is the same.
125       if (V == getConstant()) return false;
126 
127       // Otherwise, we go to overdefined.  Assumptions made based on the
128       // forced value are possibly wrong.  Assuming this is another constant
129       // could expose a contradiction.
130       Val.setInt(overdefined);
131     }
132     return true;
133   }
134 
135   /// getConstantInt - If this is a constant with a ConstantInt value, return it
136   /// otherwise return null.
137   ConstantInt *getConstantInt() const {
138     if (isConstant())
139       return dyn_cast<ConstantInt>(getConstant());
140     return nullptr;
141   }
142 
143   void markForcedConstant(Constant *V) {
144     assert(isUnknown() && "Can't force a defined value!");
145     Val.setInt(forcedconstant);
146     Val.setPointer(V);
147   }
148 };
149 } // end anonymous namespace.
150 
151 
152 namespace {
153 
154 //===----------------------------------------------------------------------===//
155 //
156 /// SCCPSolver - This class is a general purpose solver for Sparse Conditional
157 /// Constant Propagation.
158 ///
159 class SCCPSolver : public InstVisitor<SCCPSolver> {
160   const DataLayout &DL;
161   const TargetLibraryInfo *TLI;
162   SmallPtrSet<BasicBlock*, 8> BBExecutable; // The BBs that are executable.
163   DenseMap<Value*, LatticeVal> ValueState;  // The state each value is in.
164 
165   /// StructValueState - This maintains ValueState for values that have
166   /// StructType, for example for formal arguments, calls, insertelement, etc.
167   ///
168   DenseMap<std::pair<Value*, unsigned>, LatticeVal> StructValueState;
169 
170   /// GlobalValue - If we are tracking any values for the contents of a global
171   /// variable, we keep a mapping from the constant accessor to the element of
172   /// the global, to the currently known value.  If the value becomes
173   /// overdefined, it's entry is simply removed from this map.
174   DenseMap<GlobalVariable*, LatticeVal> TrackedGlobals;
175 
176   /// TrackedRetVals - If we are tracking arguments into and the return
177   /// value out of a function, it will have an entry in this map, indicating
178   /// what the known return value for the function is.
179   DenseMap<Function*, LatticeVal> TrackedRetVals;
180 
181   /// TrackedMultipleRetVals - Same as TrackedRetVals, but used for functions
182   /// that return multiple values.
183   DenseMap<std::pair<Function*, unsigned>, LatticeVal> TrackedMultipleRetVals;
184 
185   /// MRVFunctionsTracked - Each function in TrackedMultipleRetVals is
186   /// represented here for efficient lookup.
187   SmallPtrSet<Function*, 16> MRVFunctionsTracked;
188 
189   /// TrackingIncomingArguments - This is the set of functions for whose
190   /// arguments we make optimistic assumptions about and try to prove as
191   /// constants.
192   SmallPtrSet<Function*, 16> TrackingIncomingArguments;
193 
194   /// The reason for two worklists is that overdefined is the lowest state
195   /// on the lattice, and moving things to overdefined as fast as possible
196   /// makes SCCP converge much faster.
197   ///
198   /// By having a separate worklist, we accomplish this because everything
199   /// possibly overdefined will become overdefined at the soonest possible
200   /// point.
201   SmallVector<Value*, 64> OverdefinedInstWorkList;
202   SmallVector<Value*, 64> InstWorkList;
203 
204 
205   SmallVector<BasicBlock*, 64>  BBWorkList;  // The BasicBlock work list
206 
207   /// KnownFeasibleEdges - Entries in this set are edges which have already had
208   /// PHI nodes retriggered.
209   typedef std::pair<BasicBlock*, BasicBlock*> Edge;
210   DenseSet<Edge> KnownFeasibleEdges;
211 public:
212   SCCPSolver(const DataLayout &DL, const TargetLibraryInfo *tli)
213       : DL(DL), TLI(tli) {}
214 
215   /// MarkBlockExecutable - This method can be used by clients to mark all of
216   /// the blocks that are known to be intrinsically live in the processed unit.
217   ///
218   /// This returns true if the block was not considered live before.
219   bool MarkBlockExecutable(BasicBlock *BB) {
220     if (!BBExecutable.insert(BB).second)
221       return false;
222     DEBUG(dbgs() << "Marking Block Executable: " << BB->getName() << '\n');
223     BBWorkList.push_back(BB);  // Add the block to the work list!
224     return true;
225   }
226 
227   /// TrackValueOfGlobalVariable - Clients can use this method to
228   /// inform the SCCPSolver that it should track loads and stores to the
229   /// specified global variable if it can.  This is only legal to call if
230   /// performing Interprocedural SCCP.
231   void TrackValueOfGlobalVariable(GlobalVariable *GV) {
232     // We only track the contents of scalar globals.
233     if (GV->getValueType()->isSingleValueType()) {
234       LatticeVal &IV = TrackedGlobals[GV];
235       if (!isa<UndefValue>(GV->getInitializer()))
236         IV.markConstant(GV->getInitializer());
237     }
238   }
239 
240   /// AddTrackedFunction - If the SCCP solver is supposed to track calls into
241   /// and out of the specified function (which cannot have its address taken),
242   /// this method must be called.
243   void AddTrackedFunction(Function *F) {
244     // Add an entry, F -> undef.
245     if (auto *STy = dyn_cast<StructType>(F->getReturnType())) {
246       MRVFunctionsTracked.insert(F);
247       for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
248         TrackedMultipleRetVals.insert(std::make_pair(std::make_pair(F, i),
249                                                      LatticeVal()));
250     } else
251       TrackedRetVals.insert(std::make_pair(F, LatticeVal()));
252   }
253 
254   void AddArgumentTrackedFunction(Function *F) {
255     TrackingIncomingArguments.insert(F);
256   }
257 
258   /// Solve - Solve for constants and executable blocks.
259   ///
260   void Solve();
261 
262   /// ResolvedUndefsIn - While solving the dataflow for a function, we assume
263   /// that branches on undef values cannot reach any of their successors.
264   /// However, this is not a safe assumption.  After we solve dataflow, this
265   /// method should be use to handle this.  If this returns true, the solver
266   /// should be rerun.
267   bool ResolvedUndefsIn(Function &F);
268 
269   bool isBlockExecutable(BasicBlock *BB) const {
270     return BBExecutable.count(BB);
271   }
272 
273   std::vector<LatticeVal> getStructLatticeValueFor(Value *V) const {
274     std::vector<LatticeVal> StructValues;
275     auto *STy = dyn_cast<StructType>(V->getType());
276     assert(STy && "getStructLatticeValueFor() can be called only on structs");
277     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
278       auto I = StructValueState.find(std::make_pair(V, i));
279       assert(I != StructValueState.end() && "Value not in valuemap!");
280       StructValues.push_back(I->second);
281     }
282     return StructValues;
283   }
284 
285   LatticeVal getLatticeValueFor(Value *V) const {
286     DenseMap<Value*, LatticeVal>::const_iterator I = ValueState.find(V);
287     assert(I != ValueState.end() && "V is not in valuemap!");
288     return I->second;
289   }
290 
291   /// getTrackedRetVals - Get the inferred return value map.
292   ///
293   const DenseMap<Function*, LatticeVal> &getTrackedRetVals() {
294     return TrackedRetVals;
295   }
296 
297   /// getTrackedGlobals - Get and return the set of inferred initializers for
298   /// global variables.
299   const DenseMap<GlobalVariable*, LatticeVal> &getTrackedGlobals() {
300     return TrackedGlobals;
301   }
302 
303   /// getMRVFunctionsTracked - Get the set of functions which return multiple
304   /// values tracked by the pass.
305   const SmallPtrSet<Function *, 16> getMRVFunctionsTracked() {
306     return MRVFunctionsTracked;
307   }
308 
309   /// markOverdefined - Mark the specified value overdefined.  This
310   /// works with both scalars and structs.
311   void markOverdefined(Value *V) {
312     if (auto *STy = dyn_cast<StructType>(V->getType()))
313       for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
314         markOverdefined(getStructValueState(V, i), V);
315     else
316       markOverdefined(ValueState[V], V);
317   }
318 
319   // isStructLatticeConstant - Return true if all the lattice values
320   // corresponding to elements of the structure are not overdefined,
321   // false otherwise.
322   bool isStructLatticeConstant(Function *F, StructType *STy) {
323     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
324       const auto &It = TrackedMultipleRetVals.find(std::make_pair(F, i));
325       assert(It != TrackedMultipleRetVals.end());
326       LatticeVal LV = It->second;
327       if (LV.isOverdefined())
328         return false;
329     }
330     return true;
331   }
332 
333 private:
334   // pushToWorkList - Helper for markConstant/markForcedConstant/markOverdefined
335   void pushToWorkList(LatticeVal &IV, Value *V) {
336     if (IV.isOverdefined())
337       return OverdefinedInstWorkList.push_back(V);
338     InstWorkList.push_back(V);
339   }
340 
341   // markConstant - Make a value be marked as "constant".  If the value
342   // is not already a constant, add it to the instruction work list so that
343   // the users of the instruction are updated later.
344   //
345   void markConstant(LatticeVal &IV, Value *V, Constant *C) {
346     if (!IV.markConstant(C)) return;
347     DEBUG(dbgs() << "markConstant: " << *C << ": " << *V << '\n');
348     pushToWorkList(IV, V);
349   }
350 
351   void markConstant(Value *V, Constant *C) {
352     assert(!V->getType()->isStructTy() && "structs should use mergeInValue");
353     markConstant(ValueState[V], V, C);
354   }
355 
356   void markForcedConstant(Value *V, Constant *C) {
357     assert(!V->getType()->isStructTy() && "structs should use mergeInValue");
358     LatticeVal &IV = ValueState[V];
359     IV.markForcedConstant(C);
360     DEBUG(dbgs() << "markForcedConstant: " << *C << ": " << *V << '\n');
361     pushToWorkList(IV, V);
362   }
363 
364 
365   // markOverdefined - Make a value be marked as "overdefined". If the
366   // value is not already overdefined, add it to the overdefined instruction
367   // work list so that the users of the instruction are updated later.
368   void markOverdefined(LatticeVal &IV, Value *V) {
369     if (!IV.markOverdefined()) return;
370 
371     DEBUG(dbgs() << "markOverdefined: ";
372           if (auto *F = dyn_cast<Function>(V))
373             dbgs() << "Function '" << F->getName() << "'\n";
374           else
375             dbgs() << *V << '\n');
376     // Only instructions go on the work list
377     pushToWorkList(IV, V);
378   }
379 
380   void mergeInValue(LatticeVal &IV, Value *V, LatticeVal MergeWithV) {
381     if (IV.isOverdefined() || MergeWithV.isUnknown())
382       return;  // Noop.
383     if (MergeWithV.isOverdefined())
384       return markOverdefined(IV, V);
385     if (IV.isUnknown())
386       return markConstant(IV, V, MergeWithV.getConstant());
387     if (IV.getConstant() != MergeWithV.getConstant())
388       return markOverdefined(IV, V);
389   }
390 
391   void mergeInValue(Value *V, LatticeVal MergeWithV) {
392     assert(!V->getType()->isStructTy() &&
393            "non-structs should use markConstant");
394     mergeInValue(ValueState[V], V, MergeWithV);
395   }
396 
397 
398   /// getValueState - Return the LatticeVal object that corresponds to the
399   /// value.  This function handles the case when the value hasn't been seen yet
400   /// by properly seeding constants etc.
401   LatticeVal &getValueState(Value *V) {
402     assert(!V->getType()->isStructTy() && "Should use getStructValueState");
403 
404     std::pair<DenseMap<Value*, LatticeVal>::iterator, bool> I =
405       ValueState.insert(std::make_pair(V, LatticeVal()));
406     LatticeVal &LV = I.first->second;
407 
408     if (!I.second)
409       return LV;  // Common case, already in the map.
410 
411     if (auto *C = dyn_cast<Constant>(V)) {
412       // Undef values remain unknown.
413       if (!isa<UndefValue>(V))
414         LV.markConstant(C);          // Constants are constant
415     }
416 
417     // All others are underdefined by default.
418     return LV;
419   }
420 
421   /// getStructValueState - Return the LatticeVal object that corresponds to the
422   /// value/field pair.  This function handles the case when the value hasn't
423   /// been seen yet by properly seeding constants etc.
424   LatticeVal &getStructValueState(Value *V, unsigned i) {
425     assert(V->getType()->isStructTy() && "Should use getValueState");
426     assert(i < cast<StructType>(V->getType())->getNumElements() &&
427            "Invalid element #");
428 
429     std::pair<DenseMap<std::pair<Value*, unsigned>, LatticeVal>::iterator,
430               bool> I = StructValueState.insert(
431                         std::make_pair(std::make_pair(V, i), LatticeVal()));
432     LatticeVal &LV = I.first->second;
433 
434     if (!I.second)
435       return LV;  // Common case, already in the map.
436 
437     if (auto *C = dyn_cast<Constant>(V)) {
438       Constant *Elt = C->getAggregateElement(i);
439 
440       if (!Elt)
441         LV.markOverdefined();      // Unknown sort of constant.
442       else if (isa<UndefValue>(Elt))
443         ; // Undef values remain unknown.
444       else
445         LV.markConstant(Elt);      // Constants are constant.
446     }
447 
448     // All others are underdefined by default.
449     return LV;
450   }
451 
452 
453   /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
454   /// work list if it is not already executable.
455   void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
456     if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
457       return;  // This edge is already known to be executable!
458 
459     if (!MarkBlockExecutable(Dest)) {
460       // If the destination is already executable, we just made an *edge*
461       // feasible that wasn't before.  Revisit the PHI nodes in the block
462       // because they have potentially new operands.
463       DEBUG(dbgs() << "Marking Edge Executable: " << Source->getName()
464             << " -> " << Dest->getName() << '\n');
465 
466       PHINode *PN;
467       for (BasicBlock::iterator I = Dest->begin();
468            (PN = dyn_cast<PHINode>(I)); ++I)
469         visitPHINode(*PN);
470     }
471   }
472 
473   // getFeasibleSuccessors - Return a vector of booleans to indicate which
474   // successors are reachable from a given terminator instruction.
475   //
476   void getFeasibleSuccessors(TerminatorInst &TI, SmallVectorImpl<bool> &Succs);
477 
478   // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
479   // block to the 'To' basic block is currently feasible.
480   //
481   bool isEdgeFeasible(BasicBlock *From, BasicBlock *To);
482 
483   // OperandChangedState - This method is invoked on all of the users of an
484   // instruction that was just changed state somehow.  Based on this
485   // information, we need to update the specified user of this instruction.
486   //
487   void OperandChangedState(Instruction *I) {
488     if (BBExecutable.count(I->getParent()))   // Inst is executable?
489       visit(*I);
490   }
491 
492 private:
493   friend class InstVisitor<SCCPSolver>;
494 
495   // visit implementations - Something changed in this instruction.  Either an
496   // operand made a transition, or the instruction is newly executable.  Change
497   // the value type of I to reflect these changes if appropriate.
498   void visitPHINode(PHINode &I);
499 
500   // Terminators
501   void visitReturnInst(ReturnInst &I);
502   void visitTerminatorInst(TerminatorInst &TI);
503 
504   void visitCastInst(CastInst &I);
505   void visitSelectInst(SelectInst &I);
506   void visitBinaryOperator(Instruction &I);
507   void visitCmpInst(CmpInst &I);
508   void visitExtractValueInst(ExtractValueInst &EVI);
509   void visitInsertValueInst(InsertValueInst &IVI);
510   void visitLandingPadInst(LandingPadInst &I) { markOverdefined(&I); }
511   void visitFuncletPadInst(FuncletPadInst &FPI) {
512     markOverdefined(&FPI);
513   }
514   void visitCatchSwitchInst(CatchSwitchInst &CPI) {
515     markOverdefined(&CPI);
516     visitTerminatorInst(CPI);
517   }
518 
519   // Instructions that cannot be folded away.
520   void visitStoreInst     (StoreInst &I);
521   void visitLoadInst      (LoadInst &I);
522   void visitGetElementPtrInst(GetElementPtrInst &I);
523   void visitCallInst      (CallInst &I) {
524     visitCallSite(&I);
525   }
526   void visitInvokeInst    (InvokeInst &II) {
527     visitCallSite(&II);
528     visitTerminatorInst(II);
529   }
530   void visitCallSite      (CallSite CS);
531   void visitResumeInst    (TerminatorInst &I) { /*returns void*/ }
532   void visitUnreachableInst(TerminatorInst &I) { /*returns void*/ }
533   void visitFenceInst     (FenceInst &I) { /*returns void*/ }
534   void visitAtomicCmpXchgInst(AtomicCmpXchgInst &I) {
535     markOverdefined(&I);
536   }
537   void visitAtomicRMWInst (AtomicRMWInst &I) { markOverdefined(&I); }
538   void visitAllocaInst    (Instruction &I) { markOverdefined(&I); }
539   void visitVAArgInst     (Instruction &I) { markOverdefined(&I); }
540 
541   void visitInstruction(Instruction &I) {
542     // If a new instruction is added to LLVM that we don't handle.
543     DEBUG(dbgs() << "SCCP: Don't know how to handle: " << I << '\n');
544     markOverdefined(&I);   // Just in case
545   }
546 };
547 
548 } // end anonymous namespace
549 
550 
551 // getFeasibleSuccessors - Return a vector of booleans to indicate which
552 // successors are reachable from a given terminator instruction.
553 //
554 void SCCPSolver::getFeasibleSuccessors(TerminatorInst &TI,
555                                        SmallVectorImpl<bool> &Succs) {
556   Succs.resize(TI.getNumSuccessors());
557   if (auto *BI = dyn_cast<BranchInst>(&TI)) {
558     if (BI->isUnconditional()) {
559       Succs[0] = true;
560       return;
561     }
562 
563     LatticeVal BCValue = getValueState(BI->getCondition());
564     ConstantInt *CI = BCValue.getConstantInt();
565     if (!CI) {
566       // Overdefined condition variables, and branches on unfoldable constant
567       // conditions, mean the branch could go either way.
568       if (!BCValue.isUnknown())
569         Succs[0] = Succs[1] = true;
570       return;
571     }
572 
573     // Constant condition variables mean the branch can only go a single way.
574     Succs[CI->isZero()] = true;
575     return;
576   }
577 
578   // Unwinding instructions successors are always executable.
579   if (TI.isExceptional()) {
580     Succs.assign(TI.getNumSuccessors(), true);
581     return;
582   }
583 
584   if (auto *SI = dyn_cast<SwitchInst>(&TI)) {
585     if (!SI->getNumCases()) {
586       Succs[0] = true;
587       return;
588     }
589     LatticeVal SCValue = getValueState(SI->getCondition());
590     ConstantInt *CI = SCValue.getConstantInt();
591 
592     if (!CI) {   // Overdefined or unknown condition?
593       // All destinations are executable!
594       if (!SCValue.isUnknown())
595         Succs.assign(TI.getNumSuccessors(), true);
596       return;
597     }
598 
599     Succs[SI->findCaseValue(CI).getSuccessorIndex()] = true;
600     return;
601   }
602 
603   // TODO: This could be improved if the operand is a [cast of a] BlockAddress.
604   if (isa<IndirectBrInst>(&TI)) {
605     // Just mark all destinations executable!
606     Succs.assign(TI.getNumSuccessors(), true);
607     return;
608   }
609 
610   DEBUG(dbgs() << "Unknown terminator instruction: " << TI << '\n');
611   llvm_unreachable("SCCP: Don't know how to handle this terminator!");
612 }
613 
614 
615 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
616 // block to the 'To' basic block is currently feasible.
617 //
618 bool SCCPSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
619   assert(BBExecutable.count(To) && "Dest should always be alive!");
620 
621   // Make sure the source basic block is executable!!
622   if (!BBExecutable.count(From)) return false;
623 
624   // Check to make sure this edge itself is actually feasible now.
625   TerminatorInst *TI = From->getTerminator();
626   if (auto *BI = dyn_cast<BranchInst>(TI)) {
627     if (BI->isUnconditional())
628       return true;
629 
630     LatticeVal BCValue = getValueState(BI->getCondition());
631 
632     // Overdefined condition variables mean the branch could go either way,
633     // undef conditions mean that neither edge is feasible yet.
634     ConstantInt *CI = BCValue.getConstantInt();
635     if (!CI)
636       return !BCValue.isUnknown();
637 
638     // Constant condition variables mean the branch can only go a single way.
639     return BI->getSuccessor(CI->isZero()) == To;
640   }
641 
642   // Unwinding instructions successors are always executable.
643   if (TI->isExceptional())
644     return true;
645 
646   if (auto *SI = dyn_cast<SwitchInst>(TI)) {
647     if (SI->getNumCases() < 1)
648       return true;
649 
650     LatticeVal SCValue = getValueState(SI->getCondition());
651     ConstantInt *CI = SCValue.getConstantInt();
652 
653     if (!CI)
654       return !SCValue.isUnknown();
655 
656     return SI->findCaseValue(CI).getCaseSuccessor() == To;
657   }
658 
659   // Just mark all destinations executable!
660   // TODO: This could be improved if the operand is a [cast of a] BlockAddress.
661   if (isa<IndirectBrInst>(TI))
662     return true;
663 
664   DEBUG(dbgs() << "Unknown terminator instruction: " << *TI << '\n');
665   llvm_unreachable("SCCP: Don't know how to handle this terminator!");
666 }
667 
668 // visit Implementations - Something changed in this instruction, either an
669 // operand made a transition, or the instruction is newly executable.  Change
670 // the value type of I to reflect these changes if appropriate.  This method
671 // makes sure to do the following actions:
672 //
673 // 1. If a phi node merges two constants in, and has conflicting value coming
674 //    from different branches, or if the PHI node merges in an overdefined
675 //    value, then the PHI node becomes overdefined.
676 // 2. If a phi node merges only constants in, and they all agree on value, the
677 //    PHI node becomes a constant value equal to that.
678 // 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
679 // 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
680 // 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
681 // 6. If a conditional branch has a value that is constant, make the selected
682 //    destination executable
683 // 7. If a conditional branch has a value that is overdefined, make all
684 //    successors executable.
685 //
686 void SCCPSolver::visitPHINode(PHINode &PN) {
687   // If this PN returns a struct, just mark the result overdefined.
688   // TODO: We could do a lot better than this if code actually uses this.
689   if (PN.getType()->isStructTy())
690     return markOverdefined(&PN);
691 
692   if (getValueState(&PN).isOverdefined())
693     return;  // Quick exit
694 
695   // Super-extra-high-degree PHI nodes are unlikely to ever be marked constant,
696   // and slow us down a lot.  Just mark them overdefined.
697   if (PN.getNumIncomingValues() > 64)
698     return markOverdefined(&PN);
699 
700   // Look at all of the executable operands of the PHI node.  If any of them
701   // are overdefined, the PHI becomes overdefined as well.  If they are all
702   // constant, and they agree with each other, the PHI becomes the identical
703   // constant.  If they are constant and don't agree, the PHI is overdefined.
704   // If there are no executable operands, the PHI remains unknown.
705   //
706   Constant *OperandVal = nullptr;
707   for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
708     LatticeVal IV = getValueState(PN.getIncomingValue(i));
709     if (IV.isUnknown()) continue;  // Doesn't influence PHI node.
710 
711     if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent()))
712       continue;
713 
714     if (IV.isOverdefined())    // PHI node becomes overdefined!
715       return markOverdefined(&PN);
716 
717     if (!OperandVal) {   // Grab the first value.
718       OperandVal = IV.getConstant();
719       continue;
720     }
721 
722     // There is already a reachable operand.  If we conflict with it,
723     // then the PHI node becomes overdefined.  If we agree with it, we
724     // can continue on.
725 
726     // Check to see if there are two different constants merging, if so, the PHI
727     // node is overdefined.
728     if (IV.getConstant() != OperandVal)
729       return markOverdefined(&PN);
730   }
731 
732   // If we exited the loop, this means that the PHI node only has constant
733   // arguments that agree with each other(and OperandVal is the constant) or
734   // OperandVal is null because there are no defined incoming arguments.  If
735   // this is the case, the PHI remains unknown.
736   //
737   if (OperandVal)
738     markConstant(&PN, OperandVal);      // Acquire operand value
739 }
740 
741 void SCCPSolver::visitReturnInst(ReturnInst &I) {
742   if (I.getNumOperands() == 0) return;  // ret void
743 
744   Function *F = I.getParent()->getParent();
745   Value *ResultOp = I.getOperand(0);
746 
747   // If we are tracking the return value of this function, merge it in.
748   if (!TrackedRetVals.empty() && !ResultOp->getType()->isStructTy()) {
749     DenseMap<Function*, LatticeVal>::iterator TFRVI =
750       TrackedRetVals.find(F);
751     if (TFRVI != TrackedRetVals.end()) {
752       mergeInValue(TFRVI->second, F, getValueState(ResultOp));
753       return;
754     }
755   }
756 
757   // Handle functions that return multiple values.
758   if (!TrackedMultipleRetVals.empty()) {
759     if (auto *STy = dyn_cast<StructType>(ResultOp->getType()))
760       if (MRVFunctionsTracked.count(F))
761         for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
762           mergeInValue(TrackedMultipleRetVals[std::make_pair(F, i)], F,
763                        getStructValueState(ResultOp, i));
764 
765   }
766 }
767 
768 void SCCPSolver::visitTerminatorInst(TerminatorInst &TI) {
769   SmallVector<bool, 16> SuccFeasible;
770   getFeasibleSuccessors(TI, SuccFeasible);
771 
772   BasicBlock *BB = TI.getParent();
773 
774   // Mark all feasible successors executable.
775   for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
776     if (SuccFeasible[i])
777       markEdgeExecutable(BB, TI.getSuccessor(i));
778 }
779 
780 void SCCPSolver::visitCastInst(CastInst &I) {
781   LatticeVal OpSt = getValueState(I.getOperand(0));
782   if (OpSt.isOverdefined())          // Inherit overdefinedness of operand
783     markOverdefined(&I);
784   else if (OpSt.isConstant()) {
785     // Fold the constant as we build.
786     Constant *C = ConstantFoldCastOperand(I.getOpcode(), OpSt.getConstant(),
787                                           I.getType(), DL);
788     if (isa<UndefValue>(C))
789       return;
790     // Propagate constant value
791     markConstant(&I, C);
792   }
793 }
794 
795 
796 void SCCPSolver::visitExtractValueInst(ExtractValueInst &EVI) {
797   // If this returns a struct, mark all elements over defined, we don't track
798   // structs in structs.
799   if (EVI.getType()->isStructTy())
800     return markOverdefined(&EVI);
801 
802   // If this is extracting from more than one level of struct, we don't know.
803   if (EVI.getNumIndices() != 1)
804     return markOverdefined(&EVI);
805 
806   Value *AggVal = EVI.getAggregateOperand();
807   if (AggVal->getType()->isStructTy()) {
808     unsigned i = *EVI.idx_begin();
809     LatticeVal EltVal = getStructValueState(AggVal, i);
810     mergeInValue(getValueState(&EVI), &EVI, EltVal);
811   } else {
812     // Otherwise, must be extracting from an array.
813     return markOverdefined(&EVI);
814   }
815 }
816 
817 void SCCPSolver::visitInsertValueInst(InsertValueInst &IVI) {
818   auto *STy = dyn_cast<StructType>(IVI.getType());
819   if (!STy)
820     return markOverdefined(&IVI);
821 
822   // If this has more than one index, we can't handle it, drive all results to
823   // undef.
824   if (IVI.getNumIndices() != 1)
825     return markOverdefined(&IVI);
826 
827   Value *Aggr = IVI.getAggregateOperand();
828   unsigned Idx = *IVI.idx_begin();
829 
830   // Compute the result based on what we're inserting.
831   for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
832     // This passes through all values that aren't the inserted element.
833     if (i != Idx) {
834       LatticeVal EltVal = getStructValueState(Aggr, i);
835       mergeInValue(getStructValueState(&IVI, i), &IVI, EltVal);
836       continue;
837     }
838 
839     Value *Val = IVI.getInsertedValueOperand();
840     if (Val->getType()->isStructTy())
841       // We don't track structs in structs.
842       markOverdefined(getStructValueState(&IVI, i), &IVI);
843     else {
844       LatticeVal InVal = getValueState(Val);
845       mergeInValue(getStructValueState(&IVI, i), &IVI, InVal);
846     }
847   }
848 }
849 
850 void SCCPSolver::visitSelectInst(SelectInst &I) {
851   // If this select returns a struct, just mark the result overdefined.
852   // TODO: We could do a lot better than this if code actually uses this.
853   if (I.getType()->isStructTy())
854     return markOverdefined(&I);
855 
856   LatticeVal CondValue = getValueState(I.getCondition());
857   if (CondValue.isUnknown())
858     return;
859 
860   if (ConstantInt *CondCB = CondValue.getConstantInt()) {
861     Value *OpVal = CondCB->isZero() ? I.getFalseValue() : I.getTrueValue();
862     mergeInValue(&I, getValueState(OpVal));
863     return;
864   }
865 
866   // Otherwise, the condition is overdefined or a constant we can't evaluate.
867   // See if we can produce something better than overdefined based on the T/F
868   // value.
869   LatticeVal TVal = getValueState(I.getTrueValue());
870   LatticeVal FVal = getValueState(I.getFalseValue());
871 
872   // select ?, C, C -> C.
873   if (TVal.isConstant() && FVal.isConstant() &&
874       TVal.getConstant() == FVal.getConstant())
875     return markConstant(&I, FVal.getConstant());
876 
877   if (TVal.isUnknown())   // select ?, undef, X -> X.
878     return mergeInValue(&I, FVal);
879   if (FVal.isUnknown())   // select ?, X, undef -> X.
880     return mergeInValue(&I, TVal);
881   markOverdefined(&I);
882 }
883 
884 // Handle Binary Operators.
885 void SCCPSolver::visitBinaryOperator(Instruction &I) {
886   LatticeVal V1State = getValueState(I.getOperand(0));
887   LatticeVal V2State = getValueState(I.getOperand(1));
888 
889   LatticeVal &IV = ValueState[&I];
890   if (IV.isOverdefined()) return;
891 
892   if (V1State.isConstant() && V2State.isConstant()) {
893     Constant *C = ConstantExpr::get(I.getOpcode(), V1State.getConstant(),
894                                     V2State.getConstant());
895     // X op Y -> undef.
896     if (isa<UndefValue>(C))
897       return;
898     return markConstant(IV, &I, C);
899   }
900 
901   // If something is undef, wait for it to resolve.
902   if (!V1State.isOverdefined() && !V2State.isOverdefined())
903     return;
904 
905   // Otherwise, one of our operands is overdefined.  Try to produce something
906   // better than overdefined with some tricks.
907   // If this is 0 / Y, it doesn't matter that the second operand is
908   // overdefined, and we can replace it with zero.
909   if (I.getOpcode() == Instruction::UDiv || I.getOpcode() == Instruction::SDiv)
910     if (V1State.isConstant() && V1State.getConstant()->isNullValue())
911       return markConstant(IV, &I, V1State.getConstant());
912 
913   // If this is:
914   // -> AND/MUL with 0
915   // -> OR with -1
916   // it doesn't matter that the other operand is overdefined.
917   if (I.getOpcode() == Instruction::And || I.getOpcode() == Instruction::Mul ||
918       I.getOpcode() == Instruction::Or) {
919     LatticeVal *NonOverdefVal = nullptr;
920     if (!V1State.isOverdefined())
921       NonOverdefVal = &V1State;
922     else if (!V2State.isOverdefined())
923       NonOverdefVal = &V2State;
924 
925     if (NonOverdefVal) {
926       if (NonOverdefVal->isUnknown())
927         return;
928 
929       if (I.getOpcode() == Instruction::And ||
930           I.getOpcode() == Instruction::Mul) {
931         // X and 0 = 0
932         // X * 0 = 0
933         if (NonOverdefVal->getConstant()->isNullValue())
934           return markConstant(IV, &I, NonOverdefVal->getConstant());
935       } else {
936         // X or -1 = -1
937         if (ConstantInt *CI = NonOverdefVal->getConstantInt())
938           if (CI->isAllOnesValue())
939             return markConstant(IV, &I, NonOverdefVal->getConstant());
940       }
941     }
942   }
943 
944 
945   markOverdefined(&I);
946 }
947 
948 // Handle ICmpInst instruction.
949 void SCCPSolver::visitCmpInst(CmpInst &I) {
950   LatticeVal V1State = getValueState(I.getOperand(0));
951   LatticeVal V2State = getValueState(I.getOperand(1));
952 
953   LatticeVal &IV = ValueState[&I];
954   if (IV.isOverdefined()) return;
955 
956   if (V1State.isConstant() && V2State.isConstant()) {
957     Constant *C = ConstantExpr::getCompare(
958         I.getPredicate(), V1State.getConstant(), V2State.getConstant());
959     if (isa<UndefValue>(C))
960       return;
961     return markConstant(IV, &I, C);
962   }
963 
964   // If operands are still unknown, wait for it to resolve.
965   if (!V1State.isOverdefined() && !V2State.isOverdefined())
966     return;
967 
968   markOverdefined(&I);
969 }
970 
971 // Handle getelementptr instructions.  If all operands are constants then we
972 // can turn this into a getelementptr ConstantExpr.
973 //
974 void SCCPSolver::visitGetElementPtrInst(GetElementPtrInst &I) {
975   if (ValueState[&I].isOverdefined()) return;
976 
977   SmallVector<Constant*, 8> Operands;
978   Operands.reserve(I.getNumOperands());
979 
980   for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
981     LatticeVal State = getValueState(I.getOperand(i));
982     if (State.isUnknown())
983       return;  // Operands are not resolved yet.
984 
985     if (State.isOverdefined())
986       return markOverdefined(&I);
987 
988     assert(State.isConstant() && "Unknown state!");
989     Operands.push_back(State.getConstant());
990   }
991 
992   Constant *Ptr = Operands[0];
993   auto Indices = makeArrayRef(Operands.begin() + 1, Operands.end());
994   Constant *C =
995       ConstantExpr::getGetElementPtr(I.getSourceElementType(), Ptr, Indices);
996   if (isa<UndefValue>(C))
997       return;
998   markConstant(&I, C);
999 }
1000 
1001 void SCCPSolver::visitStoreInst(StoreInst &SI) {
1002   // If this store is of a struct, ignore it.
1003   if (SI.getOperand(0)->getType()->isStructTy())
1004     return;
1005 
1006   if (TrackedGlobals.empty() || !isa<GlobalVariable>(SI.getOperand(1)))
1007     return;
1008 
1009   GlobalVariable *GV = cast<GlobalVariable>(SI.getOperand(1));
1010   DenseMap<GlobalVariable*, LatticeVal>::iterator I = TrackedGlobals.find(GV);
1011   if (I == TrackedGlobals.end() || I->second.isOverdefined()) return;
1012 
1013   // Get the value we are storing into the global, then merge it.
1014   mergeInValue(I->second, GV, getValueState(SI.getOperand(0)));
1015   if (I->second.isOverdefined())
1016     TrackedGlobals.erase(I);      // No need to keep tracking this!
1017 }
1018 
1019 
1020 // Handle load instructions.  If the operand is a constant pointer to a constant
1021 // global, we can replace the load with the loaded constant value!
1022 void SCCPSolver::visitLoadInst(LoadInst &I) {
1023   // If this load is of a struct, just mark the result overdefined.
1024   if (I.getType()->isStructTy())
1025     return markOverdefined(&I);
1026 
1027   LatticeVal PtrVal = getValueState(I.getOperand(0));
1028   if (PtrVal.isUnknown()) return;   // The pointer is not resolved yet!
1029 
1030   LatticeVal &IV = ValueState[&I];
1031   if (IV.isOverdefined()) return;
1032 
1033   if (!PtrVal.isConstant() || I.isVolatile())
1034     return markOverdefined(IV, &I);
1035 
1036   Constant *Ptr = PtrVal.getConstant();
1037 
1038   // load null is undefined.
1039   if (isa<ConstantPointerNull>(Ptr) && I.getPointerAddressSpace() == 0)
1040     return;
1041 
1042   // Transform load (constant global) into the value loaded.
1043   if (auto *GV = dyn_cast<GlobalVariable>(Ptr)) {
1044     if (!TrackedGlobals.empty()) {
1045       // If we are tracking this global, merge in the known value for it.
1046       DenseMap<GlobalVariable*, LatticeVal>::iterator It =
1047         TrackedGlobals.find(GV);
1048       if (It != TrackedGlobals.end()) {
1049         mergeInValue(IV, &I, It->second);
1050         return;
1051       }
1052     }
1053   }
1054 
1055   // Transform load from a constant into a constant if possible.
1056   if (Constant *C = ConstantFoldLoadFromConstPtr(Ptr, I.getType(), DL)) {
1057     if (isa<UndefValue>(C))
1058       return;
1059     return markConstant(IV, &I, C);
1060   }
1061 
1062   // Otherwise we cannot say for certain what value this load will produce.
1063   // Bail out.
1064   markOverdefined(IV, &I);
1065 }
1066 
1067 void SCCPSolver::visitCallSite(CallSite CS) {
1068   Function *F = CS.getCalledFunction();
1069   Instruction *I = CS.getInstruction();
1070 
1071   // The common case is that we aren't tracking the callee, either because we
1072   // are not doing interprocedural analysis or the callee is indirect, or is
1073   // external.  Handle these cases first.
1074   if (!F || F->isDeclaration()) {
1075 CallOverdefined:
1076     // Void return and not tracking callee, just bail.
1077     if (I->getType()->isVoidTy()) return;
1078 
1079     // Otherwise, if we have a single return value case, and if the function is
1080     // a declaration, maybe we can constant fold it.
1081     if (F && F->isDeclaration() && !I->getType()->isStructTy() &&
1082         canConstantFoldCallTo(F)) {
1083 
1084       SmallVector<Constant*, 8> Operands;
1085       for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
1086            AI != E; ++AI) {
1087         LatticeVal State = getValueState(*AI);
1088 
1089         if (State.isUnknown())
1090           return;  // Operands are not resolved yet.
1091         if (State.isOverdefined())
1092           return markOverdefined(I);
1093         assert(State.isConstant() && "Unknown state!");
1094         Operands.push_back(State.getConstant());
1095       }
1096 
1097       if (getValueState(I).isOverdefined())
1098         return;
1099 
1100       // If we can constant fold this, mark the result of the call as a
1101       // constant.
1102       if (Constant *C = ConstantFoldCall(F, Operands, TLI)) {
1103         // call -> undef.
1104         if (isa<UndefValue>(C))
1105           return;
1106         return markConstant(I, C);
1107       }
1108     }
1109 
1110     // Otherwise, we don't know anything about this call, mark it overdefined.
1111     return markOverdefined(I);
1112   }
1113 
1114   // If this is a local function that doesn't have its address taken, mark its
1115   // entry block executable and merge in the actual arguments to the call into
1116   // the formal arguments of the function.
1117   if (!TrackingIncomingArguments.empty() && TrackingIncomingArguments.count(F)){
1118     MarkBlockExecutable(&F->front());
1119 
1120     // Propagate information from this call site into the callee.
1121     CallSite::arg_iterator CAI = CS.arg_begin();
1122     for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1123          AI != E; ++AI, ++CAI) {
1124       // If this argument is byval, and if the function is not readonly, there
1125       // will be an implicit copy formed of the input aggregate.
1126       if (AI->hasByValAttr() && !F->onlyReadsMemory()) {
1127         markOverdefined(&*AI);
1128         continue;
1129       }
1130 
1131       if (auto *STy = dyn_cast<StructType>(AI->getType())) {
1132         for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1133           LatticeVal CallArg = getStructValueState(*CAI, i);
1134           mergeInValue(getStructValueState(&*AI, i), &*AI, CallArg);
1135         }
1136       } else {
1137         mergeInValue(&*AI, getValueState(*CAI));
1138       }
1139     }
1140   }
1141 
1142   // If this is a single/zero retval case, see if we're tracking the function.
1143   if (auto *STy = dyn_cast<StructType>(F->getReturnType())) {
1144     if (!MRVFunctionsTracked.count(F))
1145       goto CallOverdefined;  // Not tracking this callee.
1146 
1147     // If we are tracking this callee, propagate the result of the function
1148     // into this call site.
1149     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
1150       mergeInValue(getStructValueState(I, i), I,
1151                    TrackedMultipleRetVals[std::make_pair(F, i)]);
1152   } else {
1153     DenseMap<Function*, LatticeVal>::iterator TFRVI = TrackedRetVals.find(F);
1154     if (TFRVI == TrackedRetVals.end())
1155       goto CallOverdefined;  // Not tracking this callee.
1156 
1157     // If so, propagate the return value of the callee into this call result.
1158     mergeInValue(I, TFRVI->second);
1159   }
1160 }
1161 
1162 void SCCPSolver::Solve() {
1163   // Process the work lists until they are empty!
1164   while (!BBWorkList.empty() || !InstWorkList.empty() ||
1165          !OverdefinedInstWorkList.empty()) {
1166     // Process the overdefined instruction's work list first, which drives other
1167     // things to overdefined more quickly.
1168     while (!OverdefinedInstWorkList.empty()) {
1169       Value *I = OverdefinedInstWorkList.pop_back_val();
1170 
1171       DEBUG(dbgs() << "\nPopped off OI-WL: " << *I << '\n');
1172 
1173       // "I" got into the work list because it either made the transition from
1174       // bottom to constant, or to overdefined.
1175       //
1176       // Anything on this worklist that is overdefined need not be visited
1177       // since all of its users will have already been marked as overdefined
1178       // Update all of the users of this instruction's value.
1179       //
1180       for (User *U : I->users())
1181         if (auto *UI = dyn_cast<Instruction>(U))
1182           OperandChangedState(UI);
1183     }
1184 
1185     // Process the instruction work list.
1186     while (!InstWorkList.empty()) {
1187       Value *I = InstWorkList.pop_back_val();
1188 
1189       DEBUG(dbgs() << "\nPopped off I-WL: " << *I << '\n');
1190 
1191       // "I" got into the work list because it made the transition from undef to
1192       // constant.
1193       //
1194       // Anything on this worklist that is overdefined need not be visited
1195       // since all of its users will have already been marked as overdefined.
1196       // Update all of the users of this instruction's value.
1197       //
1198       if (I->getType()->isStructTy() || !getValueState(I).isOverdefined())
1199         for (User *U : I->users())
1200           if (auto *UI = dyn_cast<Instruction>(U))
1201             OperandChangedState(UI);
1202     }
1203 
1204     // Process the basic block work list.
1205     while (!BBWorkList.empty()) {
1206       BasicBlock *BB = BBWorkList.back();
1207       BBWorkList.pop_back();
1208 
1209       DEBUG(dbgs() << "\nPopped off BBWL: " << *BB << '\n');
1210 
1211       // Notify all instructions in this basic block that they are newly
1212       // executable.
1213       visit(BB);
1214     }
1215   }
1216 }
1217 
1218 /// ResolvedUndefsIn - While solving the dataflow for a function, we assume
1219 /// that branches on undef values cannot reach any of their successors.
1220 /// However, this is not a safe assumption.  After we solve dataflow, this
1221 /// method should be use to handle this.  If this returns true, the solver
1222 /// should be rerun.
1223 ///
1224 /// This method handles this by finding an unresolved branch and marking it one
1225 /// of the edges from the block as being feasible, even though the condition
1226 /// doesn't say it would otherwise be.  This allows SCCP to find the rest of the
1227 /// CFG and only slightly pessimizes the analysis results (by marking one,
1228 /// potentially infeasible, edge feasible).  This cannot usefully modify the
1229 /// constraints on the condition of the branch, as that would impact other users
1230 /// of the value.
1231 ///
1232 /// This scan also checks for values that use undefs, whose results are actually
1233 /// defined.  For example, 'zext i8 undef to i32' should produce all zeros
1234 /// conservatively, as "(zext i8 X -> i32) & 0xFF00" must always return zero,
1235 /// even if X isn't defined.
1236 bool SCCPSolver::ResolvedUndefsIn(Function &F) {
1237   for (BasicBlock &BB : F) {
1238     if (!BBExecutable.count(&BB))
1239       continue;
1240 
1241     for (Instruction &I : BB) {
1242       // Look for instructions which produce undef values.
1243       if (I.getType()->isVoidTy()) continue;
1244 
1245       if (auto *STy = dyn_cast<StructType>(I.getType())) {
1246         // Only a few things that can be structs matter for undef.
1247 
1248         // Tracked calls must never be marked overdefined in ResolvedUndefsIn.
1249         if (CallSite CS = CallSite(&I))
1250           if (Function *F = CS.getCalledFunction())
1251             if (MRVFunctionsTracked.count(F))
1252               continue;
1253 
1254         // extractvalue and insertvalue don't need to be marked; they are
1255         // tracked as precisely as their operands.
1256         if (isa<ExtractValueInst>(I) || isa<InsertValueInst>(I))
1257           continue;
1258 
1259         // Send the results of everything else to overdefined.  We could be
1260         // more precise than this but it isn't worth bothering.
1261         for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1262           LatticeVal &LV = getStructValueState(&I, i);
1263           if (LV.isUnknown())
1264             markOverdefined(LV, &I);
1265         }
1266         continue;
1267       }
1268 
1269       LatticeVal &LV = getValueState(&I);
1270       if (!LV.isUnknown()) continue;
1271 
1272       // extractvalue is safe; check here because the argument is a struct.
1273       if (isa<ExtractValueInst>(I))
1274         continue;
1275 
1276       // Compute the operand LatticeVals, for convenience below.
1277       // Anything taking a struct is conservatively assumed to require
1278       // overdefined markings.
1279       if (I.getOperand(0)->getType()->isStructTy()) {
1280         markOverdefined(&I);
1281         return true;
1282       }
1283       LatticeVal Op0LV = getValueState(I.getOperand(0));
1284       LatticeVal Op1LV;
1285       if (I.getNumOperands() == 2) {
1286         if (I.getOperand(1)->getType()->isStructTy()) {
1287           markOverdefined(&I);
1288           return true;
1289         }
1290 
1291         Op1LV = getValueState(I.getOperand(1));
1292       }
1293       // If this is an instructions whose result is defined even if the input is
1294       // not fully defined, propagate the information.
1295       Type *ITy = I.getType();
1296       switch (I.getOpcode()) {
1297       case Instruction::Add:
1298       case Instruction::Sub:
1299       case Instruction::Trunc:
1300       case Instruction::FPTrunc:
1301       case Instruction::BitCast:
1302         break; // Any undef -> undef
1303       case Instruction::FSub:
1304       case Instruction::FAdd:
1305       case Instruction::FMul:
1306       case Instruction::FDiv:
1307       case Instruction::FRem:
1308         // Floating-point binary operation: be conservative.
1309         if (Op0LV.isUnknown() && Op1LV.isUnknown())
1310           markForcedConstant(&I, Constant::getNullValue(ITy));
1311         else
1312           markOverdefined(&I);
1313         return true;
1314       case Instruction::ZExt:
1315       case Instruction::SExt:
1316       case Instruction::FPToUI:
1317       case Instruction::FPToSI:
1318       case Instruction::FPExt:
1319       case Instruction::PtrToInt:
1320       case Instruction::IntToPtr:
1321       case Instruction::SIToFP:
1322       case Instruction::UIToFP:
1323         // undef -> 0; some outputs are impossible
1324         markForcedConstant(&I, Constant::getNullValue(ITy));
1325         return true;
1326       case Instruction::Mul:
1327       case Instruction::And:
1328         // Both operands undef -> undef
1329         if (Op0LV.isUnknown() && Op1LV.isUnknown())
1330           break;
1331         // undef * X -> 0.   X could be zero.
1332         // undef & X -> 0.   X could be zero.
1333         markForcedConstant(&I, Constant::getNullValue(ITy));
1334         return true;
1335 
1336       case Instruction::Or:
1337         // Both operands undef -> undef
1338         if (Op0LV.isUnknown() && Op1LV.isUnknown())
1339           break;
1340         // undef | X -> -1.   X could be -1.
1341         markForcedConstant(&I, Constant::getAllOnesValue(ITy));
1342         return true;
1343 
1344       case Instruction::Xor:
1345         // undef ^ undef -> 0; strictly speaking, this is not strictly
1346         // necessary, but we try to be nice to people who expect this
1347         // behavior in simple cases
1348         if (Op0LV.isUnknown() && Op1LV.isUnknown()) {
1349           markForcedConstant(&I, Constant::getNullValue(ITy));
1350           return true;
1351         }
1352         // undef ^ X -> undef
1353         break;
1354 
1355       case Instruction::SDiv:
1356       case Instruction::UDiv:
1357       case Instruction::SRem:
1358       case Instruction::URem:
1359         // X / undef -> undef.  No change.
1360         // X % undef -> undef.  No change.
1361         if (Op1LV.isUnknown()) break;
1362 
1363         // X / 0 -> undef.  No change.
1364         // X % 0 -> undef.  No change.
1365         if (Op1LV.isConstant() && Op1LV.getConstant()->isZeroValue())
1366           break;
1367 
1368         // undef / X -> 0.   X could be maxint.
1369         // undef % X -> 0.   X could be 1.
1370         markForcedConstant(&I, Constant::getNullValue(ITy));
1371         return true;
1372 
1373       case Instruction::AShr:
1374         // X >>a undef -> undef.
1375         if (Op1LV.isUnknown()) break;
1376 
1377         // Shifting by the bitwidth or more is undefined.
1378         if (Op1LV.isConstant()) {
1379           if (auto *ShiftAmt = Op1LV.getConstantInt())
1380             if (ShiftAmt->getLimitedValue() >=
1381                 ShiftAmt->getType()->getScalarSizeInBits())
1382               break;
1383         }
1384 
1385         // undef >>a X -> 0
1386         markForcedConstant(&I, Constant::getNullValue(ITy));
1387         return true;
1388       case Instruction::LShr:
1389       case Instruction::Shl:
1390         // X << undef -> undef.
1391         // X >> undef -> undef.
1392         if (Op1LV.isUnknown()) break;
1393 
1394         // Shifting by the bitwidth or more is undefined.
1395         if (Op1LV.isConstant()) {
1396           if (auto *ShiftAmt = Op1LV.getConstantInt())
1397             if (ShiftAmt->getLimitedValue() >=
1398                 ShiftAmt->getType()->getScalarSizeInBits())
1399               break;
1400         }
1401 
1402         // undef << X -> 0
1403         // undef >> X -> 0
1404         markForcedConstant(&I, Constant::getNullValue(ITy));
1405         return true;
1406       case Instruction::Select:
1407         Op1LV = getValueState(I.getOperand(1));
1408         // undef ? X : Y  -> X or Y.  There could be commonality between X/Y.
1409         if (Op0LV.isUnknown()) {
1410           if (!Op1LV.isConstant())  // Pick the constant one if there is any.
1411             Op1LV = getValueState(I.getOperand(2));
1412         } else if (Op1LV.isUnknown()) {
1413           // c ? undef : undef -> undef.  No change.
1414           Op1LV = getValueState(I.getOperand(2));
1415           if (Op1LV.isUnknown())
1416             break;
1417           // Otherwise, c ? undef : x -> x.
1418         } else {
1419           // Leave Op1LV as Operand(1)'s LatticeValue.
1420         }
1421 
1422         if (Op1LV.isConstant())
1423           markForcedConstant(&I, Op1LV.getConstant());
1424         else
1425           markOverdefined(&I);
1426         return true;
1427       case Instruction::Load:
1428         // A load here means one of two things: a load of undef from a global,
1429         // a load from an unknown pointer.  Either way, having it return undef
1430         // is okay.
1431         break;
1432       case Instruction::ICmp:
1433         // X == undef -> undef.  Other comparisons get more complicated.
1434         if (cast<ICmpInst>(&I)->isEquality())
1435           break;
1436         markOverdefined(&I);
1437         return true;
1438       case Instruction::Call:
1439       case Instruction::Invoke: {
1440         // There are two reasons a call can have an undef result
1441         // 1. It could be tracked.
1442         // 2. It could be constant-foldable.
1443         // Because of the way we solve return values, tracked calls must
1444         // never be marked overdefined in ResolvedUndefsIn.
1445         if (Function *F = CallSite(&I).getCalledFunction())
1446           if (TrackedRetVals.count(F))
1447             break;
1448 
1449         // If the call is constant-foldable, we mark it overdefined because
1450         // we do not know what return values are valid.
1451         markOverdefined(&I);
1452         return true;
1453       }
1454       default:
1455         // If we don't know what should happen here, conservatively mark it
1456         // overdefined.
1457         markOverdefined(&I);
1458         return true;
1459       }
1460     }
1461 
1462     // Check to see if we have a branch or switch on an undefined value.  If so
1463     // we force the branch to go one way or the other to make the successor
1464     // values live.  It doesn't really matter which way we force it.
1465     TerminatorInst *TI = BB.getTerminator();
1466     if (auto *BI = dyn_cast<BranchInst>(TI)) {
1467       if (!BI->isConditional()) continue;
1468       if (!getValueState(BI->getCondition()).isUnknown())
1469         continue;
1470 
1471       // If the input to SCCP is actually branch on undef, fix the undef to
1472       // false.
1473       if (isa<UndefValue>(BI->getCondition())) {
1474         BI->setCondition(ConstantInt::getFalse(BI->getContext()));
1475         markEdgeExecutable(&BB, TI->getSuccessor(1));
1476         return true;
1477       }
1478 
1479       // Otherwise, it is a branch on a symbolic value which is currently
1480       // considered to be undef.  Handle this by forcing the input value to the
1481       // branch to false.
1482       markForcedConstant(BI->getCondition(),
1483                          ConstantInt::getFalse(TI->getContext()));
1484       return true;
1485     }
1486 
1487     if (auto *SI = dyn_cast<SwitchInst>(TI)) {
1488       if (!SI->getNumCases() || !getValueState(SI->getCondition()).isUnknown())
1489         continue;
1490 
1491       // If the input to SCCP is actually switch on undef, fix the undef to
1492       // the first constant.
1493       if (isa<UndefValue>(SI->getCondition())) {
1494         SI->setCondition(SI->case_begin().getCaseValue());
1495         markEdgeExecutable(&BB, SI->case_begin().getCaseSuccessor());
1496         return true;
1497       }
1498 
1499       markForcedConstant(SI->getCondition(), SI->case_begin().getCaseValue());
1500       return true;
1501     }
1502   }
1503 
1504   return false;
1505 }
1506 
1507 static bool tryToReplaceWithConstant(SCCPSolver &Solver, Value *V) {
1508   Constant *Const = nullptr;
1509   if (V->getType()->isStructTy()) {
1510     std::vector<LatticeVal> IVs = Solver.getStructLatticeValueFor(V);
1511     if (any_of(IVs, [](const LatticeVal &LV) { return LV.isOverdefined(); }))
1512       return false;
1513     std::vector<Constant *> ConstVals;
1514     auto *ST = dyn_cast<StructType>(V->getType());
1515     for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) {
1516       LatticeVal V = IVs[i];
1517       ConstVals.push_back(V.isConstant()
1518                               ? V.getConstant()
1519                               : UndefValue::get(ST->getElementType(i)));
1520     }
1521     Const = ConstantStruct::get(ST, ConstVals);
1522   } else {
1523     LatticeVal IV = Solver.getLatticeValueFor(V);
1524     if (IV.isOverdefined())
1525       return false;
1526     Const = IV.isConstant() ? IV.getConstant() : UndefValue::get(V->getType());
1527   }
1528   assert(Const && "Constant is nullptr here!");
1529   DEBUG(dbgs() << "  Constant: " << *Const << " = " << *V << '\n');
1530 
1531   // Replaces all of the uses of a variable with uses of the constant.
1532   V->replaceAllUsesWith(Const);
1533   return true;
1534 }
1535 
1536 // runSCCP() - Run the Sparse Conditional Constant Propagation algorithm,
1537 // and return true if the function was modified.
1538 //
1539 static bool runSCCP(Function &F, const DataLayout &DL,
1540                     const TargetLibraryInfo *TLI) {
1541   DEBUG(dbgs() << "SCCP on function '" << F.getName() << "'\n");
1542   SCCPSolver Solver(DL, TLI);
1543 
1544   // Mark the first block of the function as being executable.
1545   Solver.MarkBlockExecutable(&F.front());
1546 
1547   // Mark all arguments to the function as being overdefined.
1548   for (Argument &AI : F.args())
1549     Solver.markOverdefined(&AI);
1550 
1551   // Solve for constants.
1552   bool ResolvedUndefs = true;
1553   while (ResolvedUndefs) {
1554     Solver.Solve();
1555     DEBUG(dbgs() << "RESOLVING UNDEFs\n");
1556     ResolvedUndefs = Solver.ResolvedUndefsIn(F);
1557   }
1558 
1559   bool MadeChanges = false;
1560 
1561   // If we decided that there are basic blocks that are dead in this function,
1562   // delete their contents now.  Note that we cannot actually delete the blocks,
1563   // as we cannot modify the CFG of the function.
1564 
1565   for (BasicBlock &BB : F) {
1566     if (!Solver.isBlockExecutable(&BB)) {
1567       DEBUG(dbgs() << "  BasicBlock Dead:" << BB);
1568 
1569       ++NumDeadBlocks;
1570       NumInstRemoved += removeAllNonTerminatorAndEHPadInstructions(&BB);
1571 
1572       MadeChanges = true;
1573       continue;
1574     }
1575 
1576     // Iterate over all of the instructions in a function, replacing them with
1577     // constants if we have found them to be of constant values.
1578     //
1579     for (BasicBlock::iterator BI = BB.begin(), E = BB.end(); BI != E;) {
1580       Instruction *Inst = &*BI++;
1581       if (Inst->getType()->isVoidTy() || isa<TerminatorInst>(Inst))
1582         continue;
1583 
1584       if (tryToReplaceWithConstant(Solver, Inst)) {
1585         if (isInstructionTriviallyDead(Inst))
1586           Inst->eraseFromParent();
1587         // Hey, we just changed something!
1588         MadeChanges = true;
1589         ++NumInstRemoved;
1590       }
1591     }
1592   }
1593 
1594   return MadeChanges;
1595 }
1596 
1597 PreservedAnalyses SCCPPass::run(Function &F, FunctionAnalysisManager &AM) {
1598   const DataLayout &DL = F.getParent()->getDataLayout();
1599   auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
1600   if (!runSCCP(F, DL, &TLI))
1601     return PreservedAnalyses::all();
1602 
1603   auto PA = PreservedAnalyses();
1604   PA.preserve<GlobalsAA>();
1605   return PA;
1606 }
1607 
1608 namespace {
1609 //===--------------------------------------------------------------------===//
1610 //
1611 /// SCCP Class - This class uses the SCCPSolver to implement a per-function
1612 /// Sparse Conditional Constant Propagator.
1613 ///
1614 class SCCPLegacyPass : public FunctionPass {
1615 public:
1616   void getAnalysisUsage(AnalysisUsage &AU) const override {
1617     AU.addRequired<TargetLibraryInfoWrapperPass>();
1618     AU.addPreserved<GlobalsAAWrapperPass>();
1619   }
1620   static char ID; // Pass identification, replacement for typeid
1621   SCCPLegacyPass() : FunctionPass(ID) {
1622     initializeSCCPLegacyPassPass(*PassRegistry::getPassRegistry());
1623   }
1624 
1625   // runOnFunction - Run the Sparse Conditional Constant Propagation
1626   // algorithm, and return true if the function was modified.
1627   //
1628   bool runOnFunction(Function &F) override {
1629     if (skipFunction(F))
1630       return false;
1631     const DataLayout &DL = F.getParent()->getDataLayout();
1632     const TargetLibraryInfo *TLI =
1633         &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
1634     return runSCCP(F, DL, TLI);
1635   }
1636 };
1637 } // end anonymous namespace
1638 
1639 char SCCPLegacyPass::ID = 0;
1640 INITIALIZE_PASS_BEGIN(SCCPLegacyPass, "sccp",
1641                       "Sparse Conditional Constant Propagation", false, false)
1642 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
1643 INITIALIZE_PASS_END(SCCPLegacyPass, "sccp",
1644                     "Sparse Conditional Constant Propagation", false, false)
1645 
1646 // createSCCPPass - This is the public interface to this file.
1647 FunctionPass *llvm::createSCCPPass() { return new SCCPLegacyPass(); }
1648 
1649 static bool AddressIsTaken(const GlobalValue *GV) {
1650   // Delete any dead constantexpr klingons.
1651   GV->removeDeadConstantUsers();
1652 
1653   for (const Use &U : GV->uses()) {
1654     const User *UR = U.getUser();
1655     if (const auto *SI = dyn_cast<StoreInst>(UR)) {
1656       if (SI->getOperand(0) == GV || SI->isVolatile())
1657         return true;  // Storing addr of GV.
1658     } else if (isa<InvokeInst>(UR) || isa<CallInst>(UR)) {
1659       // Make sure we are calling the function, not passing the address.
1660       ImmutableCallSite CS(cast<Instruction>(UR));
1661       if (!CS.isCallee(&U))
1662         return true;
1663     } else if (const auto *LI = dyn_cast<LoadInst>(UR)) {
1664       if (LI->isVolatile())
1665         return true;
1666     } else if (isa<BlockAddress>(UR)) {
1667       // blockaddress doesn't take the address of the function, it takes addr
1668       // of label.
1669     } else {
1670       return true;
1671     }
1672   }
1673   return false;
1674 }
1675 
1676 static void findReturnsToZap(Function &F,
1677                              SmallPtrSet<Function *, 32> &AddressTakenFunctions,
1678                              SmallVector<ReturnInst *, 8> &ReturnsToZap) {
1679   // We can only do this if we know that nothing else can call the function.
1680   if (!F.hasLocalLinkage() || AddressTakenFunctions.count(&F))
1681     return;
1682 
1683   for (BasicBlock &BB : F)
1684     if (auto *RI = dyn_cast<ReturnInst>(BB.getTerminator()))
1685       if (!isa<UndefValue>(RI->getOperand(0)))
1686         ReturnsToZap.push_back(RI);
1687 }
1688 
1689 static bool runIPSCCP(Module &M, const DataLayout &DL,
1690                       const TargetLibraryInfo *TLI) {
1691   SCCPSolver Solver(DL, TLI);
1692 
1693   // AddressTakenFunctions - This set keeps track of the address-taken functions
1694   // that are in the input.  As IPSCCP runs through and simplifies code,
1695   // functions that were address taken can end up losing their
1696   // address-taken-ness.  Because of this, we keep track of their addresses from
1697   // the first pass so we can use them for the later simplification pass.
1698   SmallPtrSet<Function*, 32> AddressTakenFunctions;
1699 
1700   // Loop over all functions, marking arguments to those with their addresses
1701   // taken or that are external as overdefined.
1702   //
1703   for (Function &F : M) {
1704     if (F.isDeclaration())
1705       continue;
1706 
1707     // If this is an exact definition of this function, then we can propagate
1708     // information about its result into callsites of it.
1709     // Don't touch naked functions. They may contain asm returning a
1710     // value we don't see, so we may end up interprocedurally propagating
1711     // the return value incorrectly.
1712     if (F.hasExactDefinition() && !F.hasFnAttribute(Attribute::Naked))
1713       Solver.AddTrackedFunction(&F);
1714 
1715     // If this function only has direct calls that we can see, we can track its
1716     // arguments and return value aggressively, and can assume it is not called
1717     // unless we see evidence to the contrary.
1718     if (F.hasLocalLinkage()) {
1719       if (AddressIsTaken(&F))
1720         AddressTakenFunctions.insert(&F);
1721       else {
1722         Solver.AddArgumentTrackedFunction(&F);
1723         continue;
1724       }
1725     }
1726 
1727     // Assume the function is called.
1728     Solver.MarkBlockExecutable(&F.front());
1729 
1730     // Assume nothing about the incoming arguments.
1731     for (Argument &AI : F.args())
1732       Solver.markOverdefined(&AI);
1733   }
1734 
1735   // Loop over global variables.  We inform the solver about any internal global
1736   // variables that do not have their 'addresses taken'.  If they don't have
1737   // their addresses taken, we can propagate constants through them.
1738   for (GlobalVariable &G : M.globals())
1739     if (!G.isConstant() && G.hasLocalLinkage() && !AddressIsTaken(&G))
1740       Solver.TrackValueOfGlobalVariable(&G);
1741 
1742   // Solve for constants.
1743   bool ResolvedUndefs = true;
1744   while (ResolvedUndefs) {
1745     Solver.Solve();
1746 
1747     DEBUG(dbgs() << "RESOLVING UNDEFS\n");
1748     ResolvedUndefs = false;
1749     for (Function &F : M)
1750       ResolvedUndefs |= Solver.ResolvedUndefsIn(F);
1751   }
1752 
1753   bool MadeChanges = false;
1754 
1755   // Iterate over all of the instructions in the module, replacing them with
1756   // constants if we have found them to be of constant values.
1757   //
1758   SmallVector<BasicBlock*, 512> BlocksToErase;
1759 
1760   for (Function &F : M) {
1761     if (F.isDeclaration())
1762       continue;
1763 
1764     if (Solver.isBlockExecutable(&F.front())) {
1765       for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E;
1766            ++AI) {
1767         if (AI->use_empty())
1768           continue;
1769         if (tryToReplaceWithConstant(Solver, &*AI))
1770           ++IPNumArgsElimed;
1771       }
1772     }
1773 
1774     for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1775       if (!Solver.isBlockExecutable(&*BB)) {
1776         DEBUG(dbgs() << "  BasicBlock Dead:" << *BB);
1777 
1778         ++NumDeadBlocks;
1779         NumInstRemoved +=
1780             changeToUnreachable(BB->getFirstNonPHI(), /*UseLLVMTrap=*/false);
1781 
1782         MadeChanges = true;
1783 
1784         if (&*BB != &F.front())
1785           BlocksToErase.push_back(&*BB);
1786         continue;
1787       }
1788 
1789       for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
1790         Instruction *Inst = &*BI++;
1791         if (Inst->getType()->isVoidTy())
1792           continue;
1793         if (tryToReplaceWithConstant(Solver, Inst)) {
1794           if (!isa<CallInst>(Inst) && !isa<TerminatorInst>(Inst))
1795             Inst->eraseFromParent();
1796           // Hey, we just changed something!
1797           MadeChanges = true;
1798           ++IPNumInstRemoved;
1799         }
1800       }
1801     }
1802 
1803     // Now that all instructions in the function are constant folded, erase dead
1804     // blocks, because we can now use ConstantFoldTerminator to get rid of
1805     // in-edges.
1806     for (unsigned i = 0, e = BlocksToErase.size(); i != e; ++i) {
1807       // If there are any PHI nodes in this successor, drop entries for BB now.
1808       BasicBlock *DeadBB = BlocksToErase[i];
1809       for (Value::user_iterator UI = DeadBB->user_begin(),
1810                                 UE = DeadBB->user_end();
1811            UI != UE;) {
1812         // Grab the user and then increment the iterator early, as the user
1813         // will be deleted. Step past all adjacent uses from the same user.
1814         auto *I = dyn_cast<Instruction>(*UI);
1815         do { ++UI; } while (UI != UE && *UI == I);
1816 
1817         // Ignore blockaddress users; BasicBlock's dtor will handle them.
1818         if (!I) continue;
1819 
1820         bool Folded = ConstantFoldTerminator(I->getParent());
1821         assert(Folded &&
1822               "Expect TermInst on constantint or blockaddress to be folded");
1823         (void) Folded;
1824       }
1825 
1826       // Finally, delete the basic block.
1827       F.getBasicBlockList().erase(DeadBB);
1828     }
1829     BlocksToErase.clear();
1830   }
1831 
1832   // If we inferred constant or undef return values for a function, we replaced
1833   // all call uses with the inferred value.  This means we don't need to bother
1834   // actually returning anything from the function.  Replace all return
1835   // instructions with return undef.
1836   //
1837   // Do this in two stages: first identify the functions we should process, then
1838   // actually zap their returns.  This is important because we can only do this
1839   // if the address of the function isn't taken.  In cases where a return is the
1840   // last use of a function, the order of processing functions would affect
1841   // whether other functions are optimizable.
1842   SmallVector<ReturnInst*, 8> ReturnsToZap;
1843 
1844   const DenseMap<Function*, LatticeVal> &RV = Solver.getTrackedRetVals();
1845   for (const auto &I : RV) {
1846     Function *F = I.first;
1847     if (I.second.isOverdefined() || F->getReturnType()->isVoidTy())
1848       continue;
1849     findReturnsToZap(*F, AddressTakenFunctions, ReturnsToZap);
1850   }
1851 
1852   for (const auto &F : Solver.getMRVFunctionsTracked()) {
1853     assert(F->getReturnType()->isStructTy() &&
1854            "The return type should be a struct");
1855     StructType *STy = cast<StructType>(F->getReturnType());
1856     if (Solver.isStructLatticeConstant(F, STy))
1857       findReturnsToZap(*F, AddressTakenFunctions, ReturnsToZap);
1858   }
1859 
1860   // Zap all returns which we've identified as zap to change.
1861   for (unsigned i = 0, e = ReturnsToZap.size(); i != e; ++i) {
1862     Function *F = ReturnsToZap[i]->getParent()->getParent();
1863     ReturnsToZap[i]->setOperand(0, UndefValue::get(F->getReturnType()));
1864   }
1865 
1866   // If we inferred constant or undef values for globals variables, we can
1867   // delete the global and any stores that remain to it.
1868   const DenseMap<GlobalVariable*, LatticeVal> &TG = Solver.getTrackedGlobals();
1869   for (DenseMap<GlobalVariable*, LatticeVal>::const_iterator I = TG.begin(),
1870          E = TG.end(); I != E; ++I) {
1871     GlobalVariable *GV = I->first;
1872     assert(!I->second.isOverdefined() &&
1873            "Overdefined values should have been taken out of the map!");
1874     DEBUG(dbgs() << "Found that GV '" << GV->getName() << "' is constant!\n");
1875     while (!GV->use_empty()) {
1876       StoreInst *SI = cast<StoreInst>(GV->user_back());
1877       SI->eraseFromParent();
1878     }
1879     M.getGlobalList().erase(GV);
1880     ++IPNumGlobalConst;
1881   }
1882 
1883   return MadeChanges;
1884 }
1885 
1886 PreservedAnalyses IPSCCPPass::run(Module &M, ModuleAnalysisManager &AM) {
1887   const DataLayout &DL = M.getDataLayout();
1888   auto &TLI = AM.getResult<TargetLibraryAnalysis>(M);
1889   if (!runIPSCCP(M, DL, &TLI))
1890     return PreservedAnalyses::all();
1891   return PreservedAnalyses::none();
1892 }
1893 
1894 namespace {
1895 //===--------------------------------------------------------------------===//
1896 //
1897 /// IPSCCP Class - This class implements interprocedural Sparse Conditional
1898 /// Constant Propagation.
1899 ///
1900 class IPSCCPLegacyPass : public ModulePass {
1901 public:
1902   static char ID;
1903 
1904   IPSCCPLegacyPass() : ModulePass(ID) {
1905     initializeIPSCCPLegacyPassPass(*PassRegistry::getPassRegistry());
1906   }
1907 
1908   bool runOnModule(Module &M) override {
1909     if (skipModule(M))
1910       return false;
1911     const DataLayout &DL = M.getDataLayout();
1912     const TargetLibraryInfo *TLI =
1913         &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
1914     return runIPSCCP(M, DL, TLI);
1915   }
1916 
1917   void getAnalysisUsage(AnalysisUsage &AU) const override {
1918     AU.addRequired<TargetLibraryInfoWrapperPass>();
1919   }
1920 };
1921 } // end anonymous namespace
1922 
1923 char IPSCCPLegacyPass::ID = 0;
1924 INITIALIZE_PASS_BEGIN(IPSCCPLegacyPass, "ipsccp",
1925                       "Interprocedural Sparse Conditional Constant Propagation",
1926                       false, false)
1927 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
1928 INITIALIZE_PASS_END(IPSCCPLegacyPass, "ipsccp",
1929                     "Interprocedural Sparse Conditional Constant Propagation",
1930                     false, false)
1931 
1932 // createIPSCCPPass - This is the public interface to this file.
1933 ModulePass *llvm::createIPSCCPPass() { return new IPSCCPLegacyPass(); }
1934