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