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