blob: 875a0078741b15fefce6734dc2fc3448d6130605 [file] [log] [blame]
Misha Brukman373086d2003-05-20 21:01:22 +00001//===- SCCP.cpp - Sparse Conditional Constant Propagation -----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner347389d2001-06-27 23:38:11 +00009//
Misha Brukman373086d2003-05-20 21:01:22 +000010// This file implements sparse conditional constant propagation and merging:
Chris Lattner347389d2001-06-27 23:38:11 +000011//
12// Specifically, this:
13// * Assumes values are constant unless proven otherwise
14// * Assumes BasicBlocks are dead unless proven otherwise
15// * Proves values to be constant, and replaces them with constants
Chris Lattnerdd6522e2002-08-30 23:39:00 +000016// * Proves conditional branches to be unconditional
Chris Lattner347389d2001-06-27 23:38:11 +000017//
Chris Lattner347389d2001-06-27 23:38:11 +000018//===----------------------------------------------------------------------===//
19
Chris Lattnerb4cfa7f2002-05-07 20:03:00 +000020#include "llvm/Transforms/Scalar.h"
Chris Lattner067d6072007-02-02 20:38:30 +000021#include "llvm/ADT/DenseMap.h"
Chris Lattner65938fc2008-08-23 23:36:38 +000022#include "llvm/ADT/DenseSet.h"
Chris Lattnerefdd2bb2009-11-02 02:20:32 +000023#include "llvm/ADT/PointerIntPair.h"
Chris Lattner809aee22009-11-02 06:11:23 +000024#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner0d74d3c2007-01-30 23:15:19 +000025#include "llvm/ADT/SmallVector.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000026#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Analysis/ConstantFolding.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000028#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000029#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/Constants.h"
31#include "llvm/IR/DataLayout.h"
32#include "llvm/IR/DerivedTypes.h"
Chandler Carruth7da14f12014-03-06 03:23:41 +000033#include "llvm/IR/InstVisitor.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/Instructions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000035#include "llvm/Pass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/Support/Debug.h"
37#include "llvm/Support/ErrorHandling.h"
38#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000039#include "llvm/Transforms/IPO.h"
40#include "llvm/Transforms/Utils/Local.h"
Chris Lattner347389d2001-06-27 23:38:11 +000041#include <algorithm>
Chris Lattner49525f82004-01-09 06:02:20 +000042using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000043
Chandler Carruth964daaa2014-04-22 02:55:47 +000044#define DEBUG_TYPE "sccp"
45
Chris Lattner79a42ac2006-12-19 21:40:18 +000046STATISTIC(NumInstRemoved, "Number of instructions removed");
47STATISTIC(NumDeadBlocks , "Number of basic blocks unreachable");
48
Nick Lewycky35e92c72008-03-08 07:48:41 +000049STATISTIC(IPNumInstRemoved, "Number of instructions removed by IPSCCP");
Chris Lattner79a42ac2006-12-19 21:40:18 +000050STATISTIC(IPNumArgsElimed ,"Number of arguments constant propagated by IPSCCP");
51STATISTIC(IPNumGlobalConst, "Number of globals found to be constant by IPSCCP");
52
Chris Lattner7d325382002-04-29 21:26:08 +000053namespace {
Chris Lattner1847f6d2006-12-20 06:21:33 +000054/// LatticeVal class - This class represents the different lattice values that
55/// an LLVM value may occupy. It is a simple class with value semantics.
56///
Chris Lattner2dd09db2009-09-02 06:11:42 +000057class LatticeVal {
Chris Lattnerefdd2bb2009-11-02 02:20:32 +000058 enum LatticeValueTy {
Chris Lattner1847f6d2006-12-20 06:21:33 +000059 /// undefined - This LLVM Value has no known value yet.
60 undefined,
Jakub Staszak632a3552012-01-18 21:16:33 +000061
Chris Lattner1847f6d2006-12-20 06:21:33 +000062 /// constant - This LLVM Value has a specific constant value.
63 constant,
64
65 /// forcedconstant - This LLVM Value was thought to be undef until
66 /// ResolvedUndefsIn. This is treated just like 'constant', but if merged
67 /// with another (different) constant, it goes to overdefined, instead of
68 /// asserting.
69 forcedconstant,
Jakub Staszak632a3552012-01-18 21:16:33 +000070
Chris Lattner1847f6d2006-12-20 06:21:33 +000071 /// overdefined - This instruction is not known to be constant, and we know
72 /// it has a value.
73 overdefined
Chris Lattnerefdd2bb2009-11-02 02:20:32 +000074 };
75
76 /// Val: This stores the current lattice value along with the Constant* for
77 /// the constant if this is a 'constant' or 'forcedconstant' value.
78 PointerIntPair<Constant *, 2, LatticeValueTy> Val;
Jakub Staszak632a3552012-01-18 21:16:33 +000079
Chris Lattnerefdd2bb2009-11-02 02:20:32 +000080 LatticeValueTy getLatticeValue() const {
81 return Val.getInt();
82 }
Jakub Staszak632a3552012-01-18 21:16:33 +000083
Chris Lattner347389d2001-06-27 23:38:11 +000084public:
Craig Topperf40110f2014-04-25 05:29:35 +000085 LatticeVal() : Val(nullptr, undefined) {}
Jakub Staszak632a3552012-01-18 21:16:33 +000086
Chris Lattner7ccf1a62009-11-02 03:03:42 +000087 bool isUndefined() const { return getLatticeValue() == undefined; }
88 bool isConstant() const {
Chris Lattnerefdd2bb2009-11-02 02:20:32 +000089 return getLatticeValue() == constant || getLatticeValue() == forcedconstant;
90 }
Chris Lattner7ccf1a62009-11-02 03:03:42 +000091 bool isOverdefined() const { return getLatticeValue() == overdefined; }
Jakub Staszak632a3552012-01-18 21:16:33 +000092
Chris Lattner7ccf1a62009-11-02 03:03:42 +000093 Constant *getConstant() const {
Chris Lattnerefdd2bb2009-11-02 02:20:32 +000094 assert(isConstant() && "Cannot get the constant of a non-constant!");
95 return Val.getPointer();
96 }
Jakub Staszak632a3552012-01-18 21:16:33 +000097
Chris Lattnerefdd2bb2009-11-02 02:20:32 +000098 /// markOverdefined - Return true if this is a change in status.
Chris Lattner7ccf1a62009-11-02 03:03:42 +000099 bool markOverdefined() {
Chris Lattnerefdd2bb2009-11-02 02:20:32 +0000100 if (isOverdefined())
101 return false;
Jakub Staszak632a3552012-01-18 21:16:33 +0000102
Chris Lattnerefdd2bb2009-11-02 02:20:32 +0000103 Val.setInt(overdefined);
104 return true;
Chris Lattner347389d2001-06-27 23:38:11 +0000105 }
106
Chris Lattnerefdd2bb2009-11-02 02:20:32 +0000107 /// markConstant - Return true if this is a change in status.
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000108 bool markConstant(Constant *V) {
Chris Lattnere1d5cd92009-11-03 16:50:11 +0000109 if (getLatticeValue() == constant) { // Constant but not forcedconstant.
Chris Lattnerefdd2bb2009-11-02 02:20:32 +0000110 assert(getConstant() == V && "Marking constant with different value");
111 return false;
Chris Lattner347389d2001-06-27 23:38:11 +0000112 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000113
Chris Lattnerefdd2bb2009-11-02 02:20:32 +0000114 if (isUndefined()) {
115 Val.setInt(constant);
116 assert(V && "Marking constant with NULL");
117 Val.setPointer(V);
118 } else {
Jakub Staszak632a3552012-01-18 21:16:33 +0000119 assert(getLatticeValue() == forcedconstant &&
Chris Lattnerefdd2bb2009-11-02 02:20:32 +0000120 "Cannot move from overdefined to constant!");
121 // Stay at forcedconstant if the constant is the same.
122 if (V == getConstant()) return false;
Jakub Staszak632a3552012-01-18 21:16:33 +0000123
Chris Lattnerefdd2bb2009-11-02 02:20:32 +0000124 // Otherwise, we go to overdefined. Assumptions made based on the
125 // forced value are possibly wrong. Assuming this is another constant
126 // could expose a contradiction.
127 Val.setInt(overdefined);
128 }
129 return true;
Chris Lattner347389d2001-06-27 23:38:11 +0000130 }
131
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000132 /// getConstantInt - If this is a constant with a ConstantInt value, return it
133 /// otherwise return null.
134 ConstantInt *getConstantInt() const {
135 if (isConstant())
136 return dyn_cast<ConstantInt>(getConstant());
Craig Topperf40110f2014-04-25 05:29:35 +0000137 return nullptr;
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000138 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000139
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000140 void markForcedConstant(Constant *V) {
Chris Lattnerefdd2bb2009-11-02 02:20:32 +0000141 assert(isUndefined() && "Can't force a defined value!");
142 Val.setInt(forcedconstant);
143 Val.setPointer(V);
Chris Lattner05fe6842004-01-12 03:57:30 +0000144 }
Chris Lattner347389d2001-06-27 23:38:11 +0000145};
Chris Lattnere405ed92009-11-02 02:47:51 +0000146} // end anonymous namespace.
147
148
149namespace {
Chris Lattner347389d2001-06-27 23:38:11 +0000150
Chris Lattner347389d2001-06-27 23:38:11 +0000151//===----------------------------------------------------------------------===//
Chris Lattner347389d2001-06-27 23:38:11 +0000152//
Chris Lattner074be1f2004-11-15 04:44:20 +0000153/// SCCPSolver - This class is a general purpose solver for Sparse Conditional
154/// Constant Propagation.
155///
156class SCCPSolver : public InstVisitor<SCCPSolver> {
Mehdi Amini46a43552015-03-04 18:43:29 +0000157 const DataLayout &DL;
Chad Rosiere6de63d2011-12-01 21:29:16 +0000158 const TargetLibraryInfo *TLI;
Nick Lewycky77cb8e62011-07-25 21:16:04 +0000159 SmallPtrSet<BasicBlock*, 8> BBExecutable; // The BBs that are executable.
Chris Lattnerf5484032009-11-02 05:55:40 +0000160 DenseMap<Value*, LatticeVal> ValueState; // The state each value is in.
Chris Lattner347389d2001-06-27 23:38:11 +0000161
Chris Lattner156b8c72009-11-03 23:40:48 +0000162 /// StructValueState - This maintains ValueState for values that have
163 /// StructType, for example for formal arguments, calls, insertelement, etc.
164 ///
165 DenseMap<std::pair<Value*, unsigned>, LatticeVal> StructValueState;
Jakub Staszak632a3552012-01-18 21:16:33 +0000166
Chris Lattner91dbae62004-12-11 05:15:59 +0000167 /// GlobalValue - If we are tracking any values for the contents of a global
168 /// variable, we keep a mapping from the constant accessor to the element of
169 /// the global, to the currently known value. If the value becomes
170 /// overdefined, it's entry is simply removed from this map.
Chris Lattner067d6072007-02-02 20:38:30 +0000171 DenseMap<GlobalVariable*, LatticeVal> TrackedGlobals;
Chris Lattner91dbae62004-12-11 05:15:59 +0000172
Devang Patela7a20752008-03-11 05:46:42 +0000173 /// TrackedRetVals - If we are tracking arguments into and the return
Chris Lattnerb4394642004-12-10 08:02:06 +0000174 /// value out of a function, it will have an entry in this map, indicating
175 /// what the known return value for the function is.
Devang Patela7a20752008-03-11 05:46:42 +0000176 DenseMap<Function*, LatticeVal> TrackedRetVals;
177
178 /// TrackedMultipleRetVals - Same as TrackedRetVals, but used for functions
179 /// that return multiple values.
Chris Lattner65938fc2008-08-23 23:36:38 +0000180 DenseMap<std::pair<Function*, unsigned>, LatticeVal> TrackedMultipleRetVals;
Jakub Staszak632a3552012-01-18 21:16:33 +0000181
Chris Lattner156b8c72009-11-03 23:40:48 +0000182 /// MRVFunctionsTracked - Each function in TrackedMultipleRetVals is
183 /// represented here for efficient lookup.
184 SmallPtrSet<Function*, 16> MRVFunctionsTracked;
Chris Lattnerb4394642004-12-10 08:02:06 +0000185
Chris Lattner2c427232009-11-03 20:52:57 +0000186 /// TrackingIncomingArguments - This is the set of functions for whose
187 /// arguments we make optimistic assumptions about and try to prove as
188 /// constants.
Chris Lattnercde8de52009-11-03 19:24:51 +0000189 SmallPtrSet<Function*, 16> TrackingIncomingArguments;
Jakub Staszak632a3552012-01-18 21:16:33 +0000190
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000191 /// The reason for two worklists is that overdefined is the lowest state
192 /// on the lattice, and moving things to overdefined as fast as possible
193 /// makes SCCP converge much faster.
194 ///
195 /// By having a separate worklist, we accomplish this because everything
196 /// possibly overdefined will become overdefined at the soonest possible
197 /// point.
Chris Lattner65938fc2008-08-23 23:36:38 +0000198 SmallVector<Value*, 64> OverdefinedInstWorkList;
199 SmallVector<Value*, 64> InstWorkList;
Chris Lattnerd79334d2004-07-15 23:36:43 +0000200
201
Chris Lattner65938fc2008-08-23 23:36:38 +0000202 SmallVector<BasicBlock*, 64> BBWorkList; // The BasicBlock work list
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000203
204 /// KnownFeasibleEdges - Entries in this set are edges which have already had
205 /// PHI nodes retriggered.
Chris Lattner65938fc2008-08-23 23:36:38 +0000206 typedef std::pair<BasicBlock*, BasicBlock*> Edge;
207 DenseSet<Edge> KnownFeasibleEdges;
Chris Lattner347389d2001-06-27 23:38:11 +0000208public:
Mehdi Amini46a43552015-03-04 18:43:29 +0000209 SCCPSolver(const DataLayout &DL, const TargetLibraryInfo *tli)
210 : DL(DL), TLI(tli) {}
Chris Lattner347389d2001-06-27 23:38:11 +0000211
Chris Lattner074be1f2004-11-15 04:44:20 +0000212 /// MarkBlockExecutable - This method can be used by clients to mark all of
213 /// the blocks that are known to be intrinsically live in the processed unit.
Chris Lattner809aee22009-11-02 06:11:23 +0000214 ///
215 /// This returns true if the block was not considered live before.
216 bool MarkBlockExecutable(BasicBlock *BB) {
David Blaikie70573dc2014-11-19 07:49:26 +0000217 if (!BBExecutable.insert(BB).second)
218 return false;
Nick Lewycky5cd95382013-06-26 00:30:18 +0000219 DEBUG(dbgs() << "Marking Block Executable: " << BB->getName() << '\n');
Chris Lattner074be1f2004-11-15 04:44:20 +0000220 BBWorkList.push_back(BB); // Add the block to the work list!
Chris Lattner809aee22009-11-02 06:11:23 +0000221 return true;
Chris Lattner7d325382002-04-29 21:26:08 +0000222 }
223
Chris Lattner91dbae62004-12-11 05:15:59 +0000224 /// TrackValueOfGlobalVariable - Clients can use this method to
Chris Lattnerb4394642004-12-10 08:02:06 +0000225 /// inform the SCCPSolver that it should track loads and stores to the
226 /// specified global variable if it can. This is only legal to call if
227 /// performing Interprocedural SCCP.
Chris Lattner91dbae62004-12-11 05:15:59 +0000228 void TrackValueOfGlobalVariable(GlobalVariable *GV) {
Chris Lattner156b8c72009-11-03 23:40:48 +0000229 // We only track the contents of scalar globals.
230 if (GV->getType()->getElementType()->isSingleValueType()) {
Chris Lattner91dbae62004-12-11 05:15:59 +0000231 LatticeVal &IV = TrackedGlobals[GV];
232 if (!isa<UndefValue>(GV->getInitializer()))
233 IV.markConstant(GV->getInitializer());
234 }
235 }
Chris Lattnerb4394642004-12-10 08:02:06 +0000236
237 /// AddTrackedFunction - If the SCCP solver is supposed to track calls into
238 /// and out of the specified function (which cannot have its address taken),
239 /// this method must be called.
240 void AddTrackedFunction(Function *F) {
Chris Lattnerb4394642004-12-10 08:02:06 +0000241 // Add an entry, F -> undef.
Chris Lattner229907c2011-07-18 04:54:35 +0000242 if (StructType *STy = dyn_cast<StructType>(F->getReturnType())) {
Chris Lattner156b8c72009-11-03 23:40:48 +0000243 MRVFunctionsTracked.insert(F);
Devang Patela7a20752008-03-11 05:46:42 +0000244 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Chris Lattner5a58a4d2008-04-23 05:38:20 +0000245 TrackedMultipleRetVals.insert(std::make_pair(std::make_pair(F, i),
246 LatticeVal()));
247 } else
248 TrackedRetVals.insert(std::make_pair(F, LatticeVal()));
Chris Lattnerb4394642004-12-10 08:02:06 +0000249 }
250
Chris Lattnercde8de52009-11-03 19:24:51 +0000251 void AddArgumentTrackedFunction(Function *F) {
252 TrackingIncomingArguments.insert(F);
253 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000254
Chris Lattner074be1f2004-11-15 04:44:20 +0000255 /// Solve - Solve for constants and executable blocks.
256 ///
257 void Solve();
Chris Lattner347389d2001-06-27 23:38:11 +0000258
Chris Lattner1847f6d2006-12-20 06:21:33 +0000259 /// ResolvedUndefsIn - While solving the dataflow for a function, we assume
Chris Lattner7285f432004-12-10 20:41:50 +0000260 /// that branches on undef values cannot reach any of their successors.
261 /// However, this is not a safe assumption. After we solve dataflow, this
262 /// method should be use to handle this. If this returns true, the solver
263 /// should be rerun.
Chris Lattner1847f6d2006-12-20 06:21:33 +0000264 bool ResolvedUndefsIn(Function &F);
Chris Lattner7285f432004-12-10 20:41:50 +0000265
Chris Lattneradd44f32008-08-23 23:39:31 +0000266 bool isBlockExecutable(BasicBlock *BB) const {
267 return BBExecutable.count(BB);
Chris Lattner074be1f2004-11-15 04:44:20 +0000268 }
269
Chris Lattnerb5a13d42009-11-02 02:54:24 +0000270 LatticeVal getLatticeValueFor(Value *V) const {
Chris Lattnerf5484032009-11-02 05:55:40 +0000271 DenseMap<Value*, LatticeVal>::const_iterator I = ValueState.find(V);
Chris Lattnerb5a13d42009-11-02 02:54:24 +0000272 assert(I != ValueState.end() && "V is not in valuemap!");
273 return I->second;
Chris Lattner074be1f2004-11-15 04:44:20 +0000274 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000275
Devang Patela7a20752008-03-11 05:46:42 +0000276 /// getTrackedRetVals - Get the inferred return value map.
Chris Lattner99e12952004-12-11 02:53:57 +0000277 ///
Devang Patela7a20752008-03-11 05:46:42 +0000278 const DenseMap<Function*, LatticeVal> &getTrackedRetVals() {
279 return TrackedRetVals;
Chris Lattner99e12952004-12-11 02:53:57 +0000280 }
281
Chris Lattner91dbae62004-12-11 05:15:59 +0000282 /// getTrackedGlobals - Get and return the set of inferred initializers for
283 /// global variables.
Chris Lattner067d6072007-02-02 20:38:30 +0000284 const DenseMap<GlobalVariable*, LatticeVal> &getTrackedGlobals() {
Chris Lattner91dbae62004-12-11 05:15:59 +0000285 return TrackedGlobals;
286 }
287
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000288 void markOverdefined(Value *V) {
Duncan Sands19d0b472010-02-16 11:11:14 +0000289 assert(!V->getType()->isStructTy() && "Should use other method");
Chris Lattnerc33fd462007-03-04 04:50:21 +0000290 markOverdefined(ValueState[V], V);
291 }
Chris Lattner99e12952004-12-11 02:53:57 +0000292
Chris Lattner156b8c72009-11-03 23:40:48 +0000293 /// markAnythingOverdefined - Mark the specified value overdefined. This
294 /// works with both scalars and structs.
295 void markAnythingOverdefined(Value *V) {
Chris Lattner229907c2011-07-18 04:54:35 +0000296 if (StructType *STy = dyn_cast<StructType>(V->getType()))
Chris Lattner156b8c72009-11-03 23:40:48 +0000297 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
298 markOverdefined(getStructValueState(V, i), V);
299 else
300 markOverdefined(V);
301 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000302
Chris Lattner347389d2001-06-27 23:38:11 +0000303private:
Chris Lattnerd79334d2004-07-15 23:36:43 +0000304 // markConstant - Make a value be marked as "constant". If the value
Misha Brukmanb1c93172005-04-21 23:48:37 +0000305 // is not already a constant, add it to the instruction work list so that
Chris Lattner347389d2001-06-27 23:38:11 +0000306 // the users of the instruction are updated later.
307 //
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000308 void markConstant(LatticeVal &IV, Value *V, Constant *C) {
309 if (!IV.markConstant(C)) return;
David Greene389fc3b2010-01-05 01:27:15 +0000310 DEBUG(dbgs() << "markConstant: " << *C << ": " << *V << '\n');
Chris Lattnerc6c153b2010-04-09 01:14:31 +0000311 if (IV.isOverdefined())
312 OverdefinedInstWorkList.push_back(V);
313 else
314 InstWorkList.push_back(V);
Chris Lattner7324f7c2003-10-08 16:21:03 +0000315 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000316
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000317 void markConstant(Value *V, Constant *C) {
Duncan Sands19d0b472010-02-16 11:11:14 +0000318 assert(!V->getType()->isStructTy() && "Should use other method");
Chris Lattnerb4394642004-12-10 08:02:06 +0000319 markConstant(ValueState[V], V, C);
Chris Lattner347389d2001-06-27 23:38:11 +0000320 }
321
Chris Lattnerf5484032009-11-02 05:55:40 +0000322 void markForcedConstant(Value *V, Constant *C) {
Duncan Sands19d0b472010-02-16 11:11:14 +0000323 assert(!V->getType()->isStructTy() && "Should use other method");
Chris Lattnerc6c153b2010-04-09 01:14:31 +0000324 LatticeVal &IV = ValueState[V];
325 IV.markForcedConstant(C);
David Greene389fc3b2010-01-05 01:27:15 +0000326 DEBUG(dbgs() << "markForcedConstant: " << *C << ": " << *V << '\n');
Chris Lattnerc6c153b2010-04-09 01:14:31 +0000327 if (IV.isOverdefined())
328 OverdefinedInstWorkList.push_back(V);
329 else
330 InstWorkList.push_back(V);
Chris Lattnerf5484032009-11-02 05:55:40 +0000331 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000332
333
Chris Lattnerd79334d2004-07-15 23:36:43 +0000334 // markOverdefined - Make a value be marked as "overdefined". If the
Misha Brukmanb1c93172005-04-21 23:48:37 +0000335 // value is not already overdefined, add it to the overdefined instruction
Chris Lattnerd79334d2004-07-15 23:36:43 +0000336 // work list so that the users of the instruction are updated later.
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000337 void markOverdefined(LatticeVal &IV, Value *V) {
338 if (!IV.markOverdefined()) return;
Jakub Staszak632a3552012-01-18 21:16:33 +0000339
David Greene389fc3b2010-01-05 01:27:15 +0000340 DEBUG(dbgs() << "markOverdefined: ";
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000341 if (Function *F = dyn_cast<Function>(V))
David Greene389fc3b2010-01-05 01:27:15 +0000342 dbgs() << "Function '" << F->getName() << "'\n";
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000343 else
David Greene389fc3b2010-01-05 01:27:15 +0000344 dbgs() << *V << '\n');
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000345 // Only instructions go on the work list
346 OverdefinedInstWorkList.push_back(V);
Chris Lattner7324f7c2003-10-08 16:21:03 +0000347 }
Chris Lattnerb4394642004-12-10 08:02:06 +0000348
Chris Lattnerf5484032009-11-02 05:55:40 +0000349 void mergeInValue(LatticeVal &IV, Value *V, LatticeVal MergeWithV) {
Chris Lattnerb4394642004-12-10 08:02:06 +0000350 if (IV.isOverdefined() || MergeWithV.isUndefined())
351 return; // Noop.
352 if (MergeWithV.isOverdefined())
353 markOverdefined(IV, V);
354 else if (IV.isUndefined())
355 markConstant(IV, V, MergeWithV.getConstant());
356 else if (IV.getConstant() != MergeWithV.getConstant())
357 markOverdefined(IV, V);
Chris Lattner347389d2001-06-27 23:38:11 +0000358 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000359
Chris Lattnerf5484032009-11-02 05:55:40 +0000360 void mergeInValue(Value *V, LatticeVal MergeWithV) {
Duncan Sands19d0b472010-02-16 11:11:14 +0000361 assert(!V->getType()->isStructTy() && "Should use other method");
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000362 mergeInValue(ValueState[V], V, MergeWithV);
Chris Lattner06a0ed12006-02-08 02:38:11 +0000363 }
364
Chris Lattner347389d2001-06-27 23:38:11 +0000365
Chris Lattnerf5484032009-11-02 05:55:40 +0000366 /// getValueState - Return the LatticeVal object that corresponds to the
367 /// value. This function handles the case when the value hasn't been seen yet
368 /// by properly seeding constants etc.
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000369 LatticeVal &getValueState(Value *V) {
Duncan Sands19d0b472010-02-16 11:11:14 +0000370 assert(!V->getType()->isStructTy() && "Should use getStructValueState");
Chris Lattner646354b2004-10-16 18:09:41 +0000371
Benjamin Kramer3fcbb822009-11-05 14:33:27 +0000372 std::pair<DenseMap<Value*, LatticeVal>::iterator, bool> I =
373 ValueState.insert(std::make_pair(V, LatticeVal()));
374 LatticeVal &LV = I.first->second;
375
376 if (!I.second)
377 return LV; // Common case, already in the map.
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000378
Chris Lattner1847f6d2006-12-20 06:21:33 +0000379 if (Constant *C = dyn_cast<Constant>(V)) {
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000380 // Undef values remain undefined.
381 if (!isa<UndefValue>(V))
Chris Lattner067d6072007-02-02 20:38:30 +0000382 LV.markConstant(C); // Constants are constant
Chris Lattnerdd6522e2002-08-30 23:39:00 +0000383 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000384
Chris Lattnera3c39d32009-11-02 02:33:50 +0000385 // All others are underdefined by default.
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000386 return LV;
Chris Lattner347389d2001-06-27 23:38:11 +0000387 }
388
Chris Lattner156b8c72009-11-03 23:40:48 +0000389 /// getStructValueState - Return the LatticeVal object that corresponds to the
390 /// value/field pair. This function handles the case when the value hasn't
391 /// been seen yet by properly seeding constants etc.
392 LatticeVal &getStructValueState(Value *V, unsigned i) {
Duncan Sands19d0b472010-02-16 11:11:14 +0000393 assert(V->getType()->isStructTy() && "Should use getValueState");
Chris Lattner156b8c72009-11-03 23:40:48 +0000394 assert(i < cast<StructType>(V->getType())->getNumElements() &&
395 "Invalid element #");
Benjamin Kramer3fcbb822009-11-05 14:33:27 +0000396
397 std::pair<DenseMap<std::pair<Value*, unsigned>, LatticeVal>::iterator,
398 bool> I = StructValueState.insert(
399 std::make_pair(std::make_pair(V, i), LatticeVal()));
400 LatticeVal &LV = I.first->second;
401
402 if (!I.second)
403 return LV; // Common case, already in the map.
404
Chris Lattner156b8c72009-11-03 23:40:48 +0000405 if (Constant *C = dyn_cast<Constant>(V)) {
Chris Lattnerfa775002012-01-26 02:32:04 +0000406 Constant *Elt = C->getAggregateElement(i);
Nadav Rotem465834c2012-07-24 10:51:42 +0000407
Craig Topperf40110f2014-04-25 05:29:35 +0000408 if (!Elt)
Chris Lattner156b8c72009-11-03 23:40:48 +0000409 LV.markOverdefined(); // Unknown sort of constant.
Chris Lattnerfa775002012-01-26 02:32:04 +0000410 else if (isa<UndefValue>(Elt))
411 ; // Undef values remain undefined.
412 else
413 LV.markConstant(Elt); // Constants are constant.
Chris Lattner156b8c72009-11-03 23:40:48 +0000414 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000415
Chris Lattner156b8c72009-11-03 23:40:48 +0000416 // All others are underdefined by default.
417 return LV;
418 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000419
Chris Lattner156b8c72009-11-03 23:40:48 +0000420
Chris Lattnerf5484032009-11-02 05:55:40 +0000421 /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
422 /// work list if it is not already executable.
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000423 void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
424 if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
425 return; // This edge is already known to be executable!
426
Chris Lattner809aee22009-11-02 06:11:23 +0000427 if (!MarkBlockExecutable(Dest)) {
428 // If the destination is already executable, we just made an *edge*
429 // feasible that wasn't before. Revisit the PHI nodes in the block
430 // because they have potentially new operands.
David Greene389fc3b2010-01-05 01:27:15 +0000431 DEBUG(dbgs() << "Marking Edge Executable: " << Source->getName()
Nick Lewycky5cd95382013-06-26 00:30:18 +0000432 << " -> " << Dest->getName() << '\n');
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000433
Chris Lattner809aee22009-11-02 06:11:23 +0000434 PHINode *PN;
435 for (BasicBlock::iterator I = Dest->begin();
436 (PN = dyn_cast<PHINode>(I)); ++I)
437 visitPHINode(*PN);
Chris Lattnercccc5c72003-04-25 02:50:03 +0000438 }
Chris Lattner347389d2001-06-27 23:38:11 +0000439 }
440
Chris Lattner074be1f2004-11-15 04:44:20 +0000441 // getFeasibleSuccessors - Return a vector of booleans to indicate which
442 // successors are reachable from a given terminator instruction.
443 //
Craig Topperb94011f2013-07-14 04:42:23 +0000444 void getFeasibleSuccessors(TerminatorInst &TI, SmallVectorImpl<bool> &Succs);
Chris Lattner074be1f2004-11-15 04:44:20 +0000445
446 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
Chris Lattnera3c39d32009-11-02 02:33:50 +0000447 // block to the 'To' basic block is currently feasible.
Chris Lattner074be1f2004-11-15 04:44:20 +0000448 //
449 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To);
450
451 // OperandChangedState - This method is invoked on all of the users of an
Chris Lattnera3c39d32009-11-02 02:33:50 +0000452 // instruction that was just changed state somehow. Based on this
Chris Lattner074be1f2004-11-15 04:44:20 +0000453 // information, we need to update the specified user of this instruction.
454 //
Chris Lattnerfb141812009-11-03 03:42:51 +0000455 void OperandChangedState(Instruction *I) {
456 if (BBExecutable.count(I->getParent())) // Inst is executable?
457 visit(*I);
Chris Lattner074be1f2004-11-15 04:44:20 +0000458 }
Dale Johannesend3a58c82010-11-30 20:23:21 +0000459
Chris Lattner074be1f2004-11-15 04:44:20 +0000460private:
461 friend class InstVisitor<SCCPSolver>;
Chris Lattner347389d2001-06-27 23:38:11 +0000462
Chris Lattnera3c39d32009-11-02 02:33:50 +0000463 // visit implementations - Something changed in this instruction. Either an
Chris Lattner10b250e2001-06-29 23:56:23 +0000464 // operand made a transition, or the instruction is newly executable. Change
465 // the value type of I to reflect these changes if appropriate.
Chris Lattner113f4f42002-06-25 16:13:24 +0000466 void visitPHINode(PHINode &I);
Chris Lattner6e560792002-04-18 15:13:15 +0000467
468 // Terminators
Chris Lattnerb4394642004-12-10 08:02:06 +0000469 void visitReturnInst(ReturnInst &I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000470 void visitTerminatorInst(TerminatorInst &TI);
Chris Lattner6e560792002-04-18 15:13:15 +0000471
Chris Lattner6e1a1b12002-08-14 17:53:45 +0000472 void visitCastInst(CastInst &I);
Chris Lattner59db22d2004-03-12 05:52:44 +0000473 void visitSelectInst(SelectInst &I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000474 void visitBinaryOperator(Instruction &I);
Reid Spencer266e42b2006-12-23 06:05:41 +0000475 void visitCmpInst(CmpInst &I);
Robert Bocchinobd518d12006-01-10 19:05:05 +0000476 void visitExtractElementInst(ExtractElementInst &I);
Robert Bocchino6dce2502006-01-17 20:06:55 +0000477 void visitInsertElementInst(InsertElementInst &I);
Chris Lattner17bd6052006-04-08 01:19:12 +0000478 void visitShuffleVectorInst(ShuffleVectorInst &I);
Dan Gohman041f9d02008-06-20 01:15:44 +0000479 void visitExtractValueInst(ExtractValueInst &EVI);
480 void visitInsertValueInst(InsertValueInst &IVI);
Bill Wendlingfae14752011-08-12 20:24:12 +0000481 void visitLandingPadInst(LandingPadInst &I) { markAnythingOverdefined(&I); }
Chris Lattner6e560792002-04-18 15:13:15 +0000482
Chris Lattnera3c39d32009-11-02 02:33:50 +0000483 // Instructions that cannot be folded away.
Chris Lattnerf5484032009-11-02 05:55:40 +0000484 void visitStoreInst (StoreInst &I);
Chris Lattner49f74522004-01-12 04:29:41 +0000485 void visitLoadInst (LoadInst &I);
Chris Lattnerdd6522e2002-08-30 23:39:00 +0000486 void visitGetElementPtrInst(GetElementPtrInst &I);
Victor Hernandeze2971492009-10-24 04:23:03 +0000487 void visitCallInst (CallInst &I) {
Gabor Greif62f0aac2010-07-28 22:50:26 +0000488 visitCallSite(&I);
Victor Hernandez5d034492009-09-18 22:35:49 +0000489 }
Chris Lattnerb4394642004-12-10 08:02:06 +0000490 void visitInvokeInst (InvokeInst &II) {
Gabor Greif62f0aac2010-07-28 22:50:26 +0000491 visitCallSite(&II);
Chris Lattnerb4394642004-12-10 08:02:06 +0000492 visitTerminatorInst(II);
Chris Lattnerdf741d62003-08-27 01:08:35 +0000493 }
Chris Lattnerb4394642004-12-10 08:02:06 +0000494 void visitCallSite (CallSite CS);
Bill Wendlingf891bf82011-07-31 06:30:59 +0000495 void visitResumeInst (TerminatorInst &I) { /*returns void*/ }
Chris Lattner646354b2004-10-16 18:09:41 +0000496 void visitUnreachableInst(TerminatorInst &I) { /*returns void*/ }
Eli Friedman89b694b2011-07-27 01:08:30 +0000497 void visitFenceInst (FenceInst &I) { /*returns void*/ }
Tim Northover6bf04e42014-06-13 14:54:09 +0000498 void visitAtomicCmpXchgInst(AtomicCmpXchgInst &I) {
499 markAnythingOverdefined(&I);
500 }
Eli Friedman366bcce2011-08-02 21:35:16 +0000501 void visitAtomicRMWInst (AtomicRMWInst &I) { markOverdefined(&I); }
Victor Hernandez8acf2952009-10-23 21:09:37 +0000502 void visitAllocaInst (Instruction &I) { markOverdefined(&I); }
Chris Lattner156b8c72009-11-03 23:40:48 +0000503 void visitVAArgInst (Instruction &I) { markAnythingOverdefined(&I); }
Chris Lattner6e560792002-04-18 15:13:15 +0000504
Chris Lattner113f4f42002-06-25 16:13:24 +0000505 void visitInstruction(Instruction &I) {
Chris Lattnera3c39d32009-11-02 02:33:50 +0000506 // If a new instruction is added to LLVM that we don't handle.
Nick Lewycky5cd95382013-06-26 00:30:18 +0000507 dbgs() << "SCCP: Don't know how to handle: " << I << '\n';
Chris Lattner156b8c72009-11-03 23:40:48 +0000508 markAnythingOverdefined(&I); // Just in case
Chris Lattner6e560792002-04-18 15:13:15 +0000509 }
Chris Lattner10b250e2001-06-29 23:56:23 +0000510};
Chris Lattnerb28b6802002-07-23 18:06:35 +0000511
Duncan Sands2be91fc2007-07-20 08:56:21 +0000512} // end anonymous namespace
513
514
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000515// getFeasibleSuccessors - Return a vector of booleans to indicate which
516// successors are reachable from a given terminator instruction.
517//
Chris Lattner074be1f2004-11-15 04:44:20 +0000518void SCCPSolver::getFeasibleSuccessors(TerminatorInst &TI,
Craig Topperb94011f2013-07-14 04:42:23 +0000519 SmallVectorImpl<bool> &Succs) {
Chris Lattnercccc5c72003-04-25 02:50:03 +0000520 Succs.resize(TI.getNumSuccessors());
Chris Lattner113f4f42002-06-25 16:13:24 +0000521 if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000522 if (BI->isUnconditional()) {
523 Succs[0] = true;
Chris Lattner6df5cec2009-11-02 02:30:06 +0000524 return;
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000525 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000526
Chris Lattnerf5484032009-11-02 05:55:40 +0000527 LatticeVal BCValue = getValueState(BI->getCondition());
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000528 ConstantInt *CI = BCValue.getConstantInt();
Craig Topperf40110f2014-04-25 05:29:35 +0000529 if (!CI) {
Chris Lattner6df5cec2009-11-02 02:30:06 +0000530 // Overdefined condition variables, and branches on unfoldable constant
531 // conditions, mean the branch could go either way.
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000532 if (!BCValue.isUndefined())
533 Succs[0] = Succs[1] = true;
Chris Lattner6df5cec2009-11-02 02:30:06 +0000534 return;
535 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000536
Chris Lattner6df5cec2009-11-02 02:30:06 +0000537 // Constant condition variables mean the branch can only go a single way.
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000538 Succs[CI->isZero()] = true;
Chris Lattneree8b9512009-10-29 01:21:20 +0000539 return;
540 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000541
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000542 if (isa<InvokeInst>(TI)) {
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000543 // Invoke instructions successors are always executable.
544 Succs[0] = Succs[1] = true;
Chris Lattneree8b9512009-10-29 01:21:20 +0000545 return;
546 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000547
Chris Lattneree8b9512009-10-29 01:21:20 +0000548 if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) {
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +0000549 if (!SI->getNumCases()) {
Eli Friedman56f2f212011-08-16 21:12:35 +0000550 Succs[0] = true;
551 return;
552 }
Chris Lattnerf5484032009-11-02 05:55:40 +0000553 LatticeVal SCValue = getValueState(SI->getCondition());
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000554 ConstantInt *CI = SCValue.getConstantInt();
Jakub Staszak632a3552012-01-18 21:16:33 +0000555
Craig Topperf40110f2014-04-25 05:29:35 +0000556 if (!CI) { // Overdefined or undefined condition?
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000557 // All destinations are executable!
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000558 if (!SCValue.isUndefined())
559 Succs.assign(TI.getNumSuccessors(), true);
560 return;
561 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000562
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +0000563 Succs[SI->findCaseValue(CI).getSuccessorIndex()] = true;
Chris Lattneree8b9512009-10-29 01:21:20 +0000564 return;
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000565 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000566
Chris Lattneree8b9512009-10-29 01:21:20 +0000567 // TODO: This could be improved if the operand is a [cast of a] BlockAddress.
568 if (isa<IndirectBrInst>(&TI)) {
569 // Just mark all destinations executable!
570 Succs.assign(TI.getNumSuccessors(), true);
571 return;
572 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000573
Chris Lattneree8b9512009-10-29 01:21:20 +0000574#ifndef NDEBUG
David Greene389fc3b2010-01-05 01:27:15 +0000575 dbgs() << "Unknown terminator instruction: " << TI << '\n';
Chris Lattneree8b9512009-10-29 01:21:20 +0000576#endif
577 llvm_unreachable("SCCP: Don't know how to handle this terminator!");
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000578}
579
580
Chris Lattner13b52e72002-05-02 21:18:01 +0000581// isEdgeFeasible - Return true if the control flow edge from the 'From' basic
Chris Lattnera3c39d32009-11-02 02:33:50 +0000582// block to the 'To' basic block is currently feasible.
Chris Lattner13b52e72002-05-02 21:18:01 +0000583//
Chris Lattner074be1f2004-11-15 04:44:20 +0000584bool SCCPSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
Chris Lattner13b52e72002-05-02 21:18:01 +0000585 assert(BBExecutable.count(To) && "Dest should always be alive!");
586
587 // Make sure the source basic block is executable!!
588 if (!BBExecutable.count(From)) return false;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000589
Chris Lattnera3c39d32009-11-02 02:33:50 +0000590 // Check to make sure this edge itself is actually feasible now.
Chris Lattner71ac22ff2003-10-08 15:47:41 +0000591 TerminatorInst *TI = From->getTerminator();
592 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
593 if (BI->isUnconditional())
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000594 return true;
Jakub Staszak632a3552012-01-18 21:16:33 +0000595
Chris Lattnerf5484032009-11-02 05:55:40 +0000596 LatticeVal BCValue = getValueState(BI->getCondition());
Chris Lattnerfe992d42004-01-12 17:40:36 +0000597
Chris Lattner6df5cec2009-11-02 02:30:06 +0000598 // Overdefined condition variables mean the branch could go either way,
599 // undef conditions mean that neither edge is feasible yet.
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000600 ConstantInt *CI = BCValue.getConstantInt();
Craig Topperf40110f2014-04-25 05:29:35 +0000601 if (!CI)
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000602 return !BCValue.isUndefined();
Jakub Staszak632a3552012-01-18 21:16:33 +0000603
Chris Lattner6df5cec2009-11-02 02:30:06 +0000604 // Constant condition variables mean the branch can only go a single way.
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000605 return BI->getSuccessor(CI->isZero()) == To;
Chris Lattneree8b9512009-10-29 01:21:20 +0000606 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000607
Chris Lattneree8b9512009-10-29 01:21:20 +0000608 // Invoke instructions successors are always executable.
609 if (isa<InvokeInst>(TI))
Chris Lattner71ac22ff2003-10-08 15:47:41 +0000610 return true;
Jakub Staszak632a3552012-01-18 21:16:33 +0000611
Chris Lattneree8b9512009-10-29 01:21:20 +0000612 if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +0000613 if (SI->getNumCases() < 1)
Eli Friedman56f2f212011-08-16 21:12:35 +0000614 return true;
615
Chris Lattnerf5484032009-11-02 05:55:40 +0000616 LatticeVal SCValue = getValueState(SI->getCondition());
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000617 ConstantInt *CI = SCValue.getConstantInt();
Jakub Staszak632a3552012-01-18 21:16:33 +0000618
Craig Topperf40110f2014-04-25 05:29:35 +0000619 if (!CI)
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000620 return !SCValue.isUndefined();
Chris Lattnerfe992d42004-01-12 17:40:36 +0000621
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +0000622 return SI->findCaseValue(CI).getCaseSuccessor() == To;
Chris Lattner71ac22ff2003-10-08 15:47:41 +0000623 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000624
Chris Lattneree8b9512009-10-29 01:21:20 +0000625 // Just mark all destinations executable!
626 // TODO: This could be improved if the operand is a [cast of a] BlockAddress.
Eli Friedman3de2ddc2011-05-21 19:13:10 +0000627 if (isa<IndirectBrInst>(TI))
Chris Lattneree8b9512009-10-29 01:21:20 +0000628 return true;
Jakub Staszak632a3552012-01-18 21:16:33 +0000629
Chris Lattneree8b9512009-10-29 01:21:20 +0000630#ifndef NDEBUG
David Greene389fc3b2010-01-05 01:27:15 +0000631 dbgs() << "Unknown terminator instruction: " << *TI << '\n';
Chris Lattneree8b9512009-10-29 01:21:20 +0000632#endif
Craig Toppere73658d2014-04-28 04:05:08 +0000633 llvm_unreachable(nullptr);
Chris Lattner13b52e72002-05-02 21:18:01 +0000634}
Chris Lattner347389d2001-06-27 23:38:11 +0000635
Chris Lattnera3c39d32009-11-02 02:33:50 +0000636// visit Implementations - Something changed in this instruction, either an
Chris Lattner347389d2001-06-27 23:38:11 +0000637// operand made a transition, or the instruction is newly executable. Change
638// the value type of I to reflect these changes if appropriate. This method
639// makes sure to do the following actions:
640//
641// 1. If a phi node merges two constants in, and has conflicting value coming
642// from different branches, or if the PHI node merges in an overdefined
643// value, then the PHI node becomes overdefined.
644// 2. If a phi node merges only constants in, and they all agree on value, the
645// PHI node becomes a constant value equal to that.
646// 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
647// 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
648// 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
649// 6. If a conditional branch has a value that is constant, make the selected
650// destination executable
651// 7. If a conditional branch has a value that is overdefined, make all
652// successors executable.
653//
Chris Lattner074be1f2004-11-15 04:44:20 +0000654void SCCPSolver::visitPHINode(PHINode &PN) {
Chris Lattner156b8c72009-11-03 23:40:48 +0000655 // If this PN returns a struct, just mark the result overdefined.
656 // TODO: We could do a lot better than this if code actually uses this.
Duncan Sands19d0b472010-02-16 11:11:14 +0000657 if (PN.getType()->isStructTy())
Chris Lattner156b8c72009-11-03 23:40:48 +0000658 return markAnythingOverdefined(&PN);
Jakub Staszak632a3552012-01-18 21:16:33 +0000659
Eli Friedman0a309292011-11-11 01:16:15 +0000660 if (getValueState(&PN).isOverdefined())
Chris Lattner05fe6842004-01-12 03:57:30 +0000661 return; // Quick exit
Chris Lattner347389d2001-06-27 23:38:11 +0000662
Chris Lattner7a7b1142004-03-16 19:49:59 +0000663 // Super-extra-high-degree PHI nodes are unlikely to ever be marked constant,
664 // and slow us down a lot. Just mark them overdefined.
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000665 if (PN.getNumIncomingValues() > 64)
Chris Lattnerf5484032009-11-02 05:55:40 +0000666 return markOverdefined(&PN);
Jakub Staszak632a3552012-01-18 21:16:33 +0000667
Chris Lattner6e560792002-04-18 15:13:15 +0000668 // Look at all of the executable operands of the PHI node. If any of them
669 // are overdefined, the PHI becomes overdefined as well. If they are all
670 // constant, and they agree with each other, the PHI becomes the identical
671 // constant. If they are constant and don't agree, the PHI is overdefined.
672 // If there are no executable operands, the PHI remains undefined.
673 //
Craig Topperf40110f2014-04-25 05:29:35 +0000674 Constant *OperandVal = nullptr;
Chris Lattnercccc5c72003-04-25 02:50:03 +0000675 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
Chris Lattnerf5484032009-11-02 05:55:40 +0000676 LatticeVal IV = getValueState(PN.getIncomingValue(i));
Chris Lattnercccc5c72003-04-25 02:50:03 +0000677 if (IV.isUndefined()) continue; // Doesn't influence PHI node.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000678
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000679 if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent()))
680 continue;
Jakub Staszak632a3552012-01-18 21:16:33 +0000681
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000682 if (IV.isOverdefined()) // PHI node becomes overdefined!
683 return markOverdefined(&PN);
Chris Lattner7e270582003-06-24 20:29:52 +0000684
Craig Topperf40110f2014-04-25 05:29:35 +0000685 if (!OperandVal) { // Grab the first value.
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000686 OperandVal = IV.getConstant();
687 continue;
Chris Lattner347389d2001-06-27 23:38:11 +0000688 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000689
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000690 // There is already a reachable operand. If we conflict with it,
691 // then the PHI node becomes overdefined. If we agree with it, we
692 // can continue on.
Jakub Staszak632a3552012-01-18 21:16:33 +0000693
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000694 // Check to see if there are two different constants merging, if so, the PHI
695 // node is overdefined.
696 if (IV.getConstant() != OperandVal)
697 return markOverdefined(&PN);
Chris Lattner347389d2001-06-27 23:38:11 +0000698 }
699
Chris Lattner6e560792002-04-18 15:13:15 +0000700 // If we exited the loop, this means that the PHI node only has constant
Chris Lattnercccc5c72003-04-25 02:50:03 +0000701 // arguments that agree with each other(and OperandVal is the constant) or
702 // OperandVal is null because there are no defined incoming arguments. If
703 // this is the case, the PHI remains undefined.
Chris Lattner347389d2001-06-27 23:38:11 +0000704 //
Chris Lattnercccc5c72003-04-25 02:50:03 +0000705 if (OperandVal)
Chris Lattner65938fc2008-08-23 23:36:38 +0000706 markConstant(&PN, OperandVal); // Acquire operand value
Chris Lattner347389d2001-06-27 23:38:11 +0000707}
708
Chris Lattnerb4394642004-12-10 08:02:06 +0000709void SCCPSolver::visitReturnInst(ReturnInst &I) {
Chris Lattnerf5484032009-11-02 05:55:40 +0000710 if (I.getNumOperands() == 0) return; // ret void
Chris Lattnerb4394642004-12-10 08:02:06 +0000711
Chris Lattnerb4394642004-12-10 08:02:06 +0000712 Function *F = I.getParent()->getParent();
Chris Lattner156b8c72009-11-03 23:40:48 +0000713 Value *ResultOp = I.getOperand(0);
Jakub Staszak632a3552012-01-18 21:16:33 +0000714
Devang Patela7a20752008-03-11 05:46:42 +0000715 // If we are tracking the return value of this function, merge it in.
Duncan Sands19d0b472010-02-16 11:11:14 +0000716 if (!TrackedRetVals.empty() && !ResultOp->getType()->isStructTy()) {
Chris Lattner067d6072007-02-02 20:38:30 +0000717 DenseMap<Function*, LatticeVal>::iterator TFRVI =
Devang Patela7a20752008-03-11 05:46:42 +0000718 TrackedRetVals.find(F);
Chris Lattnerfb141812009-11-03 03:42:51 +0000719 if (TFRVI != TrackedRetVals.end()) {
Chris Lattner156b8c72009-11-03 23:40:48 +0000720 mergeInValue(TFRVI->second, F, getValueState(ResultOp));
Devang Patela7a20752008-03-11 05:46:42 +0000721 return;
722 }
723 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000724
Chris Lattner5a58a4d2008-04-23 05:38:20 +0000725 // Handle functions that return multiple values.
Chris Lattner156b8c72009-11-03 23:40:48 +0000726 if (!TrackedMultipleRetVals.empty()) {
Chris Lattner229907c2011-07-18 04:54:35 +0000727 if (StructType *STy = dyn_cast<StructType>(ResultOp->getType()))
Chris Lattner156b8c72009-11-03 23:40:48 +0000728 if (MRVFunctionsTracked.count(F))
729 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
730 mergeInValue(TrackedMultipleRetVals[std::make_pair(F, i)], F,
731 getStructValueState(ResultOp, i));
Jakub Staszak632a3552012-01-18 21:16:33 +0000732
Chris Lattnerb4394642004-12-10 08:02:06 +0000733 }
734}
735
Chris Lattner074be1f2004-11-15 04:44:20 +0000736void SCCPSolver::visitTerminatorInst(TerminatorInst &TI) {
Chris Lattner37d400a2007-02-02 21:15:06 +0000737 SmallVector<bool, 16> SuccFeasible;
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000738 getFeasibleSuccessors(TI, SuccFeasible);
Chris Lattner347389d2001-06-27 23:38:11 +0000739
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000740 BasicBlock *BB = TI.getParent();
741
Chris Lattnera3c39d32009-11-02 02:33:50 +0000742 // Mark all feasible successors executable.
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000743 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000744 if (SuccFeasible[i])
745 markEdgeExecutable(BB, TI.getSuccessor(i));
Chris Lattner6e560792002-04-18 15:13:15 +0000746}
747
Chris Lattner074be1f2004-11-15 04:44:20 +0000748void SCCPSolver::visitCastInst(CastInst &I) {
Chris Lattnerf5484032009-11-02 05:55:40 +0000749 LatticeVal OpSt = getValueState(I.getOperand(0));
750 if (OpSt.isOverdefined()) // Inherit overdefinedness of operand
Chris Lattner113f4f42002-06-25 16:13:24 +0000751 markOverdefined(&I);
Chris Lattnerf5484032009-11-02 05:55:40 +0000752 else if (OpSt.isConstant()) // Propagate constant value
Jakub Staszak632a3552012-01-18 21:16:33 +0000753 markConstant(&I, ConstantExpr::getCast(I.getOpcode(),
Chris Lattnerf5484032009-11-02 05:55:40 +0000754 OpSt.getConstant(), I.getType()));
Chris Lattner6e560792002-04-18 15:13:15 +0000755}
756
Chris Lattner156b8c72009-11-03 23:40:48 +0000757
Dan Gohman041f9d02008-06-20 01:15:44 +0000758void SCCPSolver::visitExtractValueInst(ExtractValueInst &EVI) {
Chris Lattner156b8c72009-11-03 23:40:48 +0000759 // If this returns a struct, mark all elements over defined, we don't track
760 // structs in structs.
Duncan Sands19d0b472010-02-16 11:11:14 +0000761 if (EVI.getType()->isStructTy())
Chris Lattner156b8c72009-11-03 23:40:48 +0000762 return markAnythingOverdefined(&EVI);
Jakub Staszak632a3552012-01-18 21:16:33 +0000763
Chris Lattner156b8c72009-11-03 23:40:48 +0000764 // If this is extracting from more than one level of struct, we don't know.
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000765 if (EVI.getNumIndices() != 1)
766 return markOverdefined(&EVI);
Dan Gohman041f9d02008-06-20 01:15:44 +0000767
Chris Lattner156b8c72009-11-03 23:40:48 +0000768 Value *AggVal = EVI.getAggregateOperand();
Duncan Sands19d0b472010-02-16 11:11:14 +0000769 if (AggVal->getType()->isStructTy()) {
Chris Lattner02e2cee2009-11-10 22:02:09 +0000770 unsigned i = *EVI.idx_begin();
771 LatticeVal EltVal = getStructValueState(AggVal, i);
772 mergeInValue(getValueState(&EVI), &EVI, EltVal);
773 } else {
774 // Otherwise, must be extracting from an array.
775 return markOverdefined(&EVI);
776 }
Dan Gohman041f9d02008-06-20 01:15:44 +0000777}
778
779void SCCPSolver::visitInsertValueInst(InsertValueInst &IVI) {
Chris Lattner229907c2011-07-18 04:54:35 +0000780 StructType *STy = dyn_cast<StructType>(IVI.getType());
Craig Topperf40110f2014-04-25 05:29:35 +0000781 if (!STy)
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000782 return markOverdefined(&IVI);
Jakub Staszak632a3552012-01-18 21:16:33 +0000783
Chris Lattner156b8c72009-11-03 23:40:48 +0000784 // If this has more than one index, we can't handle it, drive all results to
785 // undef.
786 if (IVI.getNumIndices() != 1)
787 return markAnythingOverdefined(&IVI);
Jakub Staszak632a3552012-01-18 21:16:33 +0000788
Chris Lattner156b8c72009-11-03 23:40:48 +0000789 Value *Aggr = IVI.getAggregateOperand();
790 unsigned Idx = *IVI.idx_begin();
Jakub Staszak632a3552012-01-18 21:16:33 +0000791
Chris Lattner156b8c72009-11-03 23:40:48 +0000792 // Compute the result based on what we're inserting.
793 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
794 // This passes through all values that aren't the inserted element.
795 if (i != Idx) {
796 LatticeVal EltVal = getStructValueState(Aggr, i);
797 mergeInValue(getStructValueState(&IVI, i), &IVI, EltVal);
798 continue;
799 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000800
Chris Lattner156b8c72009-11-03 23:40:48 +0000801 Value *Val = IVI.getInsertedValueOperand();
Duncan Sands19d0b472010-02-16 11:11:14 +0000802 if (Val->getType()->isStructTy())
Chris Lattner156b8c72009-11-03 23:40:48 +0000803 // We don't track structs in structs.
804 markOverdefined(getStructValueState(&IVI, i), &IVI);
805 else {
806 LatticeVal InVal = getValueState(Val);
807 mergeInValue(getStructValueState(&IVI, i), &IVI, InVal);
808 }
809 }
Dan Gohman041f9d02008-06-20 01:15:44 +0000810}
811
Chris Lattner074be1f2004-11-15 04:44:20 +0000812void SCCPSolver::visitSelectInst(SelectInst &I) {
Chris Lattner156b8c72009-11-03 23:40:48 +0000813 // If this select returns a struct, just mark the result overdefined.
814 // TODO: We could do a lot better than this if code actually uses this.
Duncan Sands19d0b472010-02-16 11:11:14 +0000815 if (I.getType()->isStructTy())
Chris Lattner156b8c72009-11-03 23:40:48 +0000816 return markAnythingOverdefined(&I);
Jakub Staszak632a3552012-01-18 21:16:33 +0000817
Chris Lattnerf5484032009-11-02 05:55:40 +0000818 LatticeVal CondValue = getValueState(I.getCondition());
Chris Lattner06a0ed12006-02-08 02:38:11 +0000819 if (CondValue.isUndefined())
820 return;
Jakub Staszak632a3552012-01-18 21:16:33 +0000821
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000822 if (ConstantInt *CondCB = CondValue.getConstantInt()) {
Chris Lattnerf5484032009-11-02 05:55:40 +0000823 Value *OpVal = CondCB->isZero() ? I.getFalseValue() : I.getTrueValue();
824 mergeInValue(&I, getValueState(OpVal));
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000825 return;
Chris Lattner06a0ed12006-02-08 02:38:11 +0000826 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000827
Chris Lattner06a0ed12006-02-08 02:38:11 +0000828 // Otherwise, the condition is overdefined or a constant we can't evaluate.
829 // See if we can produce something better than overdefined based on the T/F
830 // value.
Chris Lattnerf5484032009-11-02 05:55:40 +0000831 LatticeVal TVal = getValueState(I.getTrueValue());
832 LatticeVal FVal = getValueState(I.getFalseValue());
Jakub Staszak632a3552012-01-18 21:16:33 +0000833
Chris Lattner06a0ed12006-02-08 02:38:11 +0000834 // select ?, C, C -> C.
Jakub Staszak632a3552012-01-18 21:16:33 +0000835 if (TVal.isConstant() && FVal.isConstant() &&
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000836 TVal.getConstant() == FVal.getConstant())
837 return markConstant(&I, FVal.getConstant());
Chris Lattner06a0ed12006-02-08 02:38:11 +0000838
Chris Lattnerf5484032009-11-02 05:55:40 +0000839 if (TVal.isUndefined()) // select ?, undef, X -> X.
840 return mergeInValue(&I, FVal);
841 if (FVal.isUndefined()) // select ?, X, undef -> X.
842 return mergeInValue(&I, TVal);
843 markOverdefined(&I);
Chris Lattner59db22d2004-03-12 05:52:44 +0000844}
845
Chris Lattnerf5484032009-11-02 05:55:40 +0000846// Handle Binary Operators.
Chris Lattner074be1f2004-11-15 04:44:20 +0000847void SCCPSolver::visitBinaryOperator(Instruction &I) {
Chris Lattnerf5484032009-11-02 05:55:40 +0000848 LatticeVal V1State = getValueState(I.getOperand(0));
849 LatticeVal V2State = getValueState(I.getOperand(1));
Jakub Staszak632a3552012-01-18 21:16:33 +0000850
Chris Lattner4f031622004-11-15 05:03:30 +0000851 LatticeVal &IV = ValueState[&I];
Chris Lattner05fe6842004-01-12 03:57:30 +0000852 if (IV.isOverdefined()) return;
853
Chris Lattnerf5484032009-11-02 05:55:40 +0000854 if (V1State.isConstant() && V2State.isConstant())
855 return markConstant(IV, &I,
856 ConstantExpr::get(I.getOpcode(), V1State.getConstant(),
857 V2State.getConstant()));
Jakub Staszak632a3552012-01-18 21:16:33 +0000858
Chris Lattnerf5484032009-11-02 05:55:40 +0000859 // If something is undef, wait for it to resolve.
860 if (!V1State.isOverdefined() && !V2State.isOverdefined())
861 return;
Jakub Staszak632a3552012-01-18 21:16:33 +0000862
Chris Lattnerf5484032009-11-02 05:55:40 +0000863 // Otherwise, one of our operands is overdefined. Try to produce something
864 // better than overdefined with some tricks.
Jakub Staszak632a3552012-01-18 21:16:33 +0000865
Chris Lattnerf5484032009-11-02 05:55:40 +0000866 // If this is an AND or OR with 0 or -1, it doesn't matter that the other
867 // operand is overdefined.
868 if (I.getOpcode() == Instruction::And || I.getOpcode() == Instruction::Or) {
Craig Topperf40110f2014-04-25 05:29:35 +0000869 LatticeVal *NonOverdefVal = nullptr;
Chris Lattnerf5484032009-11-02 05:55:40 +0000870 if (!V1State.isOverdefined())
871 NonOverdefVal = &V1State;
872 else if (!V2State.isOverdefined())
873 NonOverdefVal = &V2State;
Chris Lattner05fe6842004-01-12 03:57:30 +0000874
Chris Lattnerf5484032009-11-02 05:55:40 +0000875 if (NonOverdefVal) {
876 if (NonOverdefVal->isUndefined()) {
877 // Could annihilate value.
878 if (I.getOpcode() == Instruction::And)
879 markConstant(IV, &I, Constant::getNullValue(I.getType()));
Chris Lattner229907c2011-07-18 04:54:35 +0000880 else if (VectorType *PT = dyn_cast<VectorType>(I.getType()))
Chris Lattnerf5484032009-11-02 05:55:40 +0000881 markConstant(IV, &I, Constant::getAllOnesValue(PT));
882 else
883 markConstant(IV, &I,
884 Constant::getAllOnesValue(I.getType()));
885 return;
Chris Lattnercbc01612004-12-11 23:15:19 +0000886 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000887
Chris Lattnerf5484032009-11-02 05:55:40 +0000888 if (I.getOpcode() == Instruction::And) {
889 // X and 0 = 0
890 if (NonOverdefVal->getConstant()->isNullValue())
891 return markConstant(IV, &I, NonOverdefVal->getConstant());
892 } else {
893 if (ConstantInt *CI = NonOverdefVal->getConstantInt())
894 if (CI->isAllOnesValue()) // X or -1 = -1
895 return markConstant(IV, &I, NonOverdefVal->getConstant());
Chris Lattnercbc01612004-12-11 23:15:19 +0000896 }
897 }
Chris Lattnerf5484032009-11-02 05:55:40 +0000898 }
Chris Lattnercbc01612004-12-11 23:15:19 +0000899
900
Chris Lattnerf5484032009-11-02 05:55:40 +0000901 markOverdefined(&I);
Chris Lattner6e560792002-04-18 15:13:15 +0000902}
Chris Lattnerdd6522e2002-08-30 23:39:00 +0000903
Chris Lattnera3c39d32009-11-02 02:33:50 +0000904// Handle ICmpInst instruction.
Reid Spencer266e42b2006-12-23 06:05:41 +0000905void SCCPSolver::visitCmpInst(CmpInst &I) {
Chris Lattnerf5484032009-11-02 05:55:40 +0000906 LatticeVal V1State = getValueState(I.getOperand(0));
907 LatticeVal V2State = getValueState(I.getOperand(1));
908
Reid Spencer266e42b2006-12-23 06:05:41 +0000909 LatticeVal &IV = ValueState[&I];
910 if (IV.isOverdefined()) return;
911
Chris Lattnerf5484032009-11-02 05:55:40 +0000912 if (V1State.isConstant() && V2State.isConstant())
Jakub Staszak632a3552012-01-18 21:16:33 +0000913 return markConstant(IV, &I, ConstantExpr::getCompare(I.getPredicate(),
914 V1State.getConstant(),
Chris Lattnerf5484032009-11-02 05:55:40 +0000915 V2State.getConstant()));
Jakub Staszak632a3552012-01-18 21:16:33 +0000916
Chris Lattnerf5484032009-11-02 05:55:40 +0000917 // If operands are still undefined, wait for it to resolve.
918 if (!V1State.isOverdefined() && !V2State.isOverdefined())
919 return;
Jakub Staszak632a3552012-01-18 21:16:33 +0000920
Chris Lattnerf5484032009-11-02 05:55:40 +0000921 markOverdefined(&I);
Reid Spencer266e42b2006-12-23 06:05:41 +0000922}
923
Robert Bocchinobd518d12006-01-10 19:05:05 +0000924void SCCPSolver::visitExtractElementInst(ExtractElementInst &I) {
Chris Lattner156b8c72009-11-03 23:40:48 +0000925 // TODO : SCCP does not handle vectors properly.
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000926 return markOverdefined(&I);
Devang Patel21efc732006-12-04 23:54:59 +0000927
928#if 0
Robert Bocchinobd518d12006-01-10 19:05:05 +0000929 LatticeVal &ValState = getValueState(I.getOperand(0));
930 LatticeVal &IdxState = getValueState(I.getOperand(1));
931
932 if (ValState.isOverdefined() || IdxState.isOverdefined())
933 markOverdefined(&I);
934 else if(ValState.isConstant() && IdxState.isConstant())
935 markConstant(&I, ConstantExpr::getExtractElement(ValState.getConstant(),
936 IdxState.getConstant()));
Devang Patel21efc732006-12-04 23:54:59 +0000937#endif
Robert Bocchinobd518d12006-01-10 19:05:05 +0000938}
939
Robert Bocchino6dce2502006-01-17 20:06:55 +0000940void SCCPSolver::visitInsertElementInst(InsertElementInst &I) {
Chris Lattner156b8c72009-11-03 23:40:48 +0000941 // TODO : SCCP does not handle vectors properly.
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000942 return markOverdefined(&I);
Devang Patel21efc732006-12-04 23:54:59 +0000943#if 0
Robert Bocchino6dce2502006-01-17 20:06:55 +0000944 LatticeVal &ValState = getValueState(I.getOperand(0));
945 LatticeVal &EltState = getValueState(I.getOperand(1));
946 LatticeVal &IdxState = getValueState(I.getOperand(2));
947
948 if (ValState.isOverdefined() || EltState.isOverdefined() ||
949 IdxState.isOverdefined())
950 markOverdefined(&I);
951 else if(ValState.isConstant() && EltState.isConstant() &&
952 IdxState.isConstant())
953 markConstant(&I, ConstantExpr::getInsertElement(ValState.getConstant(),
954 EltState.getConstant(),
955 IdxState.getConstant()));
956 else if (ValState.isUndefined() && EltState.isConstant() &&
Jakub Staszak632a3552012-01-18 21:16:33 +0000957 IdxState.isConstant())
Chris Lattner28d921d2007-04-14 23:32:02 +0000958 markConstant(&I,ConstantExpr::getInsertElement(UndefValue::get(I.getType()),
959 EltState.getConstant(),
960 IdxState.getConstant()));
Devang Patel21efc732006-12-04 23:54:59 +0000961#endif
Robert Bocchino6dce2502006-01-17 20:06:55 +0000962}
963
Chris Lattner17bd6052006-04-08 01:19:12 +0000964void SCCPSolver::visitShuffleVectorInst(ShuffleVectorInst &I) {
Chris Lattner156b8c72009-11-03 23:40:48 +0000965 // TODO : SCCP does not handle vectors properly.
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000966 return markOverdefined(&I);
Devang Patel21efc732006-12-04 23:54:59 +0000967#if 0
Chris Lattner17bd6052006-04-08 01:19:12 +0000968 LatticeVal &V1State = getValueState(I.getOperand(0));
969 LatticeVal &V2State = getValueState(I.getOperand(1));
970 LatticeVal &MaskState = getValueState(I.getOperand(2));
971
972 if (MaskState.isUndefined() ||
973 (V1State.isUndefined() && V2State.isUndefined()))
974 return; // Undefined output if mask or both inputs undefined.
Jakub Staszak632a3552012-01-18 21:16:33 +0000975
Chris Lattner17bd6052006-04-08 01:19:12 +0000976 if (V1State.isOverdefined() || V2State.isOverdefined() ||
977 MaskState.isOverdefined()) {
978 markOverdefined(&I);
979 } else {
980 // A mix of constant/undef inputs.
Jakub Staszak632a3552012-01-18 21:16:33 +0000981 Constant *V1 = V1State.isConstant() ?
Chris Lattner17bd6052006-04-08 01:19:12 +0000982 V1State.getConstant() : UndefValue::get(I.getType());
Jakub Staszak632a3552012-01-18 21:16:33 +0000983 Constant *V2 = V2State.isConstant() ?
Chris Lattner17bd6052006-04-08 01:19:12 +0000984 V2State.getConstant() : UndefValue::get(I.getType());
Jakub Staszak632a3552012-01-18 21:16:33 +0000985 Constant *Mask = MaskState.isConstant() ?
Chris Lattner17bd6052006-04-08 01:19:12 +0000986 MaskState.getConstant() : UndefValue::get(I.getOperand(2)->getType());
987 markConstant(&I, ConstantExpr::getShuffleVector(V1, V2, Mask));
988 }
Devang Patel21efc732006-12-04 23:54:59 +0000989#endif
Chris Lattner17bd6052006-04-08 01:19:12 +0000990}
991
Chris Lattnera3c39d32009-11-02 02:33:50 +0000992// Handle getelementptr instructions. If all operands are constants then we
Chris Lattnerdd6522e2002-08-30 23:39:00 +0000993// can turn this into a getelementptr ConstantExpr.
994//
Chris Lattner074be1f2004-11-15 04:44:20 +0000995void SCCPSolver::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattnerb70ef3c2009-11-02 23:25:39 +0000996 if (ValueState[&I].isOverdefined()) return;
Chris Lattner49f74522004-01-12 04:29:41 +0000997
Chris Lattner0e7ec672007-02-02 20:51:48 +0000998 SmallVector<Constant*, 8> Operands;
Chris Lattnerdd6522e2002-08-30 23:39:00 +0000999 Operands.reserve(I.getNumOperands());
1000
1001 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
Chris Lattnerf5484032009-11-02 05:55:40 +00001002 LatticeVal State = getValueState(I.getOperand(i));
Chris Lattnerdd6522e2002-08-30 23:39:00 +00001003 if (State.isUndefined())
Chris Lattnera3c39d32009-11-02 02:33:50 +00001004 return; // Operands are not resolved yet.
Jakub Staszak632a3552012-01-18 21:16:33 +00001005
Chris Lattner7ccf1a62009-11-02 03:03:42 +00001006 if (State.isOverdefined())
Chris Lattnerb70ef3c2009-11-02 23:25:39 +00001007 return markOverdefined(&I);
Chris Lattner7ccf1a62009-11-02 03:03:42 +00001008
Chris Lattnerdd6522e2002-08-30 23:39:00 +00001009 assert(State.isConstant() && "Unknown state!");
1010 Operands.push_back(State.getConstant());
1011 }
1012
1013 Constant *Ptr = Operands[0];
Craig Toppere1d12942014-08-27 05:25:25 +00001014 auto Indices = makeArrayRef(Operands.begin() + 1, Operands.end());
Jay Foaded8db7d2011-07-21 14:31:17 +00001015 markConstant(&I, ConstantExpr::getGetElementPtr(Ptr, Indices));
Chris Lattnerdd6522e2002-08-30 23:39:00 +00001016}
Brian Gaeke960707c2003-11-11 22:41:34 +00001017
Chris Lattnerf5484032009-11-02 05:55:40 +00001018void SCCPSolver::visitStoreInst(StoreInst &SI) {
Chris Lattner156b8c72009-11-03 23:40:48 +00001019 // If this store is of a struct, ignore it.
Duncan Sands19d0b472010-02-16 11:11:14 +00001020 if (SI.getOperand(0)->getType()->isStructTy())
Chris Lattner156b8c72009-11-03 23:40:48 +00001021 return;
Jakub Staszak632a3552012-01-18 21:16:33 +00001022
Chris Lattner91dbae62004-12-11 05:15:59 +00001023 if (TrackedGlobals.empty() || !isa<GlobalVariable>(SI.getOperand(1)))
1024 return;
Jakub Staszak632a3552012-01-18 21:16:33 +00001025
Chris Lattner91dbae62004-12-11 05:15:59 +00001026 GlobalVariable *GV = cast<GlobalVariable>(SI.getOperand(1));
Chris Lattner067d6072007-02-02 20:38:30 +00001027 DenseMap<GlobalVariable*, LatticeVal>::iterator I = TrackedGlobals.find(GV);
Chris Lattner91dbae62004-12-11 05:15:59 +00001028 if (I == TrackedGlobals.end() || I->second.isOverdefined()) return;
1029
Chris Lattnerf5484032009-11-02 05:55:40 +00001030 // Get the value we are storing into the global, then merge it.
1031 mergeInValue(I->second, GV, getValueState(SI.getOperand(0)));
Chris Lattner91dbae62004-12-11 05:15:59 +00001032 if (I->second.isOverdefined())
1033 TrackedGlobals.erase(I); // No need to keep tracking this!
1034}
1035
1036
Chris Lattner49f74522004-01-12 04:29:41 +00001037// Handle load instructions. If the operand is a constant pointer to a constant
1038// global, we can replace the load with the loaded constant value!
Chris Lattner074be1f2004-11-15 04:44:20 +00001039void SCCPSolver::visitLoadInst(LoadInst &I) {
Chris Lattner156b8c72009-11-03 23:40:48 +00001040 // If this load is of a struct, just mark the result overdefined.
Duncan Sands19d0b472010-02-16 11:11:14 +00001041 if (I.getType()->isStructTy())
Chris Lattner156b8c72009-11-03 23:40:48 +00001042 return markAnythingOverdefined(&I);
Jakub Staszak632a3552012-01-18 21:16:33 +00001043
Chris Lattnerf5484032009-11-02 05:55:40 +00001044 LatticeVal PtrVal = getValueState(I.getOperand(0));
Chris Lattnere77c9aa2009-11-02 06:06:14 +00001045 if (PtrVal.isUndefined()) return; // The pointer is not resolved yet!
Jakub Staszak632a3552012-01-18 21:16:33 +00001046
Chris Lattner4f031622004-11-15 05:03:30 +00001047 LatticeVal &IV = ValueState[&I];
Chris Lattner49f74522004-01-12 04:29:41 +00001048 if (IV.isOverdefined()) return;
1049
Chris Lattnerf5484032009-11-02 05:55:40 +00001050 if (!PtrVal.isConstant() || I.isVolatile())
1051 return markOverdefined(IV, &I);
Jakub Staszak632a3552012-01-18 21:16:33 +00001052
Chris Lattnere77c9aa2009-11-02 06:06:14 +00001053 Constant *Ptr = PtrVal.getConstant();
Misha Brukmanb1c93172005-04-21 23:48:37 +00001054
Chris Lattnerf5484032009-11-02 05:55:40 +00001055 // load null -> null
1056 if (isa<ConstantPointerNull>(Ptr) && I.getPointerAddressSpace() == 0)
1057 return markConstant(IV, &I, Constant::getNullValue(I.getType()));
Jakub Staszak632a3552012-01-18 21:16:33 +00001058
Chris Lattnerf5484032009-11-02 05:55:40 +00001059 // Transform load (constant global) into the value loaded.
1060 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) {
Chris Lattnere77c9aa2009-11-02 06:06:14 +00001061 if (!TrackedGlobals.empty()) {
Chris Lattnerf5484032009-11-02 05:55:40 +00001062 // If we are tracking this global, merge in the known value for it.
1063 DenseMap<GlobalVariable*, LatticeVal>::iterator It =
1064 TrackedGlobals.find(GV);
1065 if (It != TrackedGlobals.end()) {
1066 mergeInValue(IV, &I, It->second);
1067 return;
Chris Lattner49f74522004-01-12 04:29:41 +00001068 }
Chris Lattner91dbae62004-12-11 05:15:59 +00001069 }
Chris Lattner49f74522004-01-12 04:29:41 +00001070 }
1071
Chris Lattnere77c9aa2009-11-02 06:06:14 +00001072 // Transform load from a constant into a constant if possible.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001073 if (Constant *C = ConstantFoldLoadFromConstPtr(Ptr, DL))
Chris Lattnere77c9aa2009-11-02 06:06:14 +00001074 return markConstant(IV, &I, C);
Chris Lattnerf5484032009-11-02 05:55:40 +00001075
Chris Lattner49f74522004-01-12 04:29:41 +00001076 // Otherwise we cannot say for certain what value this load will produce.
1077 // Bail out.
1078 markOverdefined(IV, &I);
1079}
Chris Lattnerff9362a2004-04-13 19:43:54 +00001080
Chris Lattnerb4394642004-12-10 08:02:06 +00001081void SCCPSolver::visitCallSite(CallSite CS) {
1082 Function *F = CS.getCalledFunction();
Chris Lattnerb4394642004-12-10 08:02:06 +00001083 Instruction *I = CS.getInstruction();
Jakub Staszak632a3552012-01-18 21:16:33 +00001084
Chris Lattner5a58a4d2008-04-23 05:38:20 +00001085 // The common case is that we aren't tracking the callee, either because we
1086 // are not doing interprocedural analysis or the callee is indirect, or is
1087 // external. Handle these cases first.
Craig Topperf40110f2014-04-25 05:29:35 +00001088 if (!F || F->isDeclaration()) {
Chris Lattner5a58a4d2008-04-23 05:38:20 +00001089CallOverdefined:
1090 // Void return and not tracking callee, just bail.
Chris Lattnerfdd87902009-10-05 05:54:46 +00001091 if (I->getType()->isVoidTy()) return;
Jakub Staszak632a3552012-01-18 21:16:33 +00001092
Chris Lattner5a58a4d2008-04-23 05:38:20 +00001093 // Otherwise, if we have a single return value case, and if the function is
1094 // a declaration, maybe we can constant fold it.
Duncan Sands19d0b472010-02-16 11:11:14 +00001095 if (F && F->isDeclaration() && !I->getType()->isStructTy() &&
Chris Lattner5a58a4d2008-04-23 05:38:20 +00001096 canConstantFoldCallTo(F)) {
Jakub Staszak632a3552012-01-18 21:16:33 +00001097
Chris Lattner5a58a4d2008-04-23 05:38:20 +00001098 SmallVector<Constant*, 8> Operands;
1099 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
1100 AI != E; ++AI) {
Chris Lattnerf5484032009-11-02 05:55:40 +00001101 LatticeVal State = getValueState(*AI);
Jakub Staszak632a3552012-01-18 21:16:33 +00001102
Chris Lattner5a58a4d2008-04-23 05:38:20 +00001103 if (State.isUndefined())
1104 return; // Operands are not resolved yet.
Chris Lattner7ccf1a62009-11-02 03:03:42 +00001105 if (State.isOverdefined())
1106 return markOverdefined(I);
Chris Lattner5a58a4d2008-04-23 05:38:20 +00001107 assert(State.isConstant() && "Unknown state!");
1108 Operands.push_back(State.getConstant());
1109 }
Jakub Staszak632a3552012-01-18 21:16:33 +00001110
David Majnemer2098b86f2014-11-07 08:54:19 +00001111 if (getValueState(I).isOverdefined())
1112 return;
1113
Chris Lattner5a58a4d2008-04-23 05:38:20 +00001114 // If we can constant fold this, mark the result of the call as a
1115 // constant.
Chad Rosiere6de63d2011-12-01 21:29:16 +00001116 if (Constant *C = ConstantFoldCall(F, Operands, TLI))
Chris Lattner7ccf1a62009-11-02 03:03:42 +00001117 return markConstant(I, C);
Chris Lattnerff9362a2004-04-13 19:43:54 +00001118 }
Chris Lattner5a58a4d2008-04-23 05:38:20 +00001119
1120 // Otherwise, we don't know anything about this call, mark it overdefined.
Chris Lattner156b8c72009-11-03 23:40:48 +00001121 return markAnythingOverdefined(I);
Chris Lattnerff9362a2004-04-13 19:43:54 +00001122 }
1123
Chris Lattnercde8de52009-11-03 19:24:51 +00001124 // If this is a local function that doesn't have its address taken, mark its
1125 // entry block executable and merge in the actual arguments to the call into
1126 // the formal arguments of the function.
1127 if (!TrackingIncomingArguments.empty() && TrackingIncomingArguments.count(F)){
1128 MarkBlockExecutable(F->begin());
Jakub Staszak632a3552012-01-18 21:16:33 +00001129
Chris Lattnercde8de52009-11-03 19:24:51 +00001130 // Propagate information from this call site into the callee.
1131 CallSite::arg_iterator CAI = CS.arg_begin();
1132 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1133 AI != E; ++AI, ++CAI) {
1134 // If this argument is byval, and if the function is not readonly, there
1135 // will be an implicit copy formed of the input aggregate.
1136 if (AI->hasByValAttr() && !F->onlyReadsMemory()) {
1137 markOverdefined(AI);
1138 continue;
1139 }
Jakub Staszak632a3552012-01-18 21:16:33 +00001140
Chris Lattner229907c2011-07-18 04:54:35 +00001141 if (StructType *STy = dyn_cast<StructType>(AI->getType())) {
Chris Lattner762b56f2009-11-04 18:57:42 +00001142 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1143 LatticeVal CallArg = getStructValueState(*CAI, i);
1144 mergeInValue(getStructValueState(AI, i), AI, CallArg);
1145 }
Chris Lattner156b8c72009-11-03 23:40:48 +00001146 } else {
1147 mergeInValue(AI, getValueState(*CAI));
1148 }
Chris Lattnercde8de52009-11-03 19:24:51 +00001149 }
1150 }
Jakub Staszak632a3552012-01-18 21:16:33 +00001151
Chris Lattner5a58a4d2008-04-23 05:38:20 +00001152 // If this is a single/zero retval case, see if we're tracking the function.
Chris Lattner229907c2011-07-18 04:54:35 +00001153 if (StructType *STy = dyn_cast<StructType>(F->getReturnType())) {
Chris Lattner156b8c72009-11-03 23:40:48 +00001154 if (!MRVFunctionsTracked.count(F))
1155 goto CallOverdefined; // Not tracking this callee.
Jakub Staszak632a3552012-01-18 21:16:33 +00001156
Chris Lattner156b8c72009-11-03 23:40:48 +00001157 // If we are tracking this callee, propagate the result of the function
1158 // into this call site.
1159 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Jakub Staszak632a3552012-01-18 21:16:33 +00001160 mergeInValue(getStructValueState(I, i), I,
Chris Lattner156b8c72009-11-03 23:40:48 +00001161 TrackedMultipleRetVals[std::make_pair(F, i)]);
1162 } else {
1163 DenseMap<Function*, LatticeVal>::iterator TFRVI = TrackedRetVals.find(F);
1164 if (TFRVI == TrackedRetVals.end())
1165 goto CallOverdefined; // Not tracking this callee.
Jakub Staszak632a3552012-01-18 21:16:33 +00001166
Chris Lattner5a58a4d2008-04-23 05:38:20 +00001167 // If so, propagate the return value of the callee into this call result.
1168 mergeInValue(I, TFRVI->second);
Chris Lattner5a58a4d2008-04-23 05:38:20 +00001169 }
Chris Lattnerff9362a2004-04-13 19:43:54 +00001170}
Chris Lattner074be1f2004-11-15 04:44:20 +00001171
Chris Lattner074be1f2004-11-15 04:44:20 +00001172void SCCPSolver::Solve() {
1173 // Process the work lists until they are empty!
Misha Brukmanb1c93172005-04-21 23:48:37 +00001174 while (!BBWorkList.empty() || !InstWorkList.empty() ||
Jeff Cohen82639852005-04-23 21:38:35 +00001175 !OverdefinedInstWorkList.empty()) {
Chris Lattnerf5484032009-11-02 05:55:40 +00001176 // Process the overdefined instruction's work list first, which drives other
1177 // things to overdefined more quickly.
Chris Lattner074be1f2004-11-15 04:44:20 +00001178 while (!OverdefinedInstWorkList.empty()) {
Chris Lattnerf5484032009-11-02 05:55:40 +00001179 Value *I = OverdefinedInstWorkList.pop_back_val();
Chris Lattner074be1f2004-11-15 04:44:20 +00001180
David Greene389fc3b2010-01-05 01:27:15 +00001181 DEBUG(dbgs() << "\nPopped off OI-WL: " << *I << '\n');
Misha Brukmanb1c93172005-04-21 23:48:37 +00001182
Chris Lattner074be1f2004-11-15 04:44:20 +00001183 // "I" got into the work list because it either made the transition from
Chad Rosier4d87d452013-02-20 20:15:55 +00001184 // bottom to constant, or to overdefined.
Chris Lattner074be1f2004-11-15 04:44:20 +00001185 //
1186 // Anything on this worklist that is overdefined need not be visited
1187 // since all of its users will have already been marked as overdefined
Chris Lattnera3c39d32009-11-02 02:33:50 +00001188 // Update all of the users of this instruction's value.
Chris Lattner074be1f2004-11-15 04:44:20 +00001189 //
Chandler Carruthcdf47882014-03-09 03:16:01 +00001190 for (User *U : I->users())
1191 if (Instruction *UI = dyn_cast<Instruction>(U))
1192 OperandChangedState(UI);
Chris Lattner074be1f2004-11-15 04:44:20 +00001193 }
Jakub Staszak632a3552012-01-18 21:16:33 +00001194
Chris Lattnera3c39d32009-11-02 02:33:50 +00001195 // Process the instruction work list.
Chris Lattner074be1f2004-11-15 04:44:20 +00001196 while (!InstWorkList.empty()) {
Chris Lattnerf5484032009-11-02 05:55:40 +00001197 Value *I = InstWorkList.pop_back_val();
Chris Lattner074be1f2004-11-15 04:44:20 +00001198
David Greene389fc3b2010-01-05 01:27:15 +00001199 DEBUG(dbgs() << "\nPopped off I-WL: " << *I << '\n');
Misha Brukmanb1c93172005-04-21 23:48:37 +00001200
Chris Lattnerf5484032009-11-02 05:55:40 +00001201 // "I" got into the work list because it made the transition from undef to
1202 // constant.
Chris Lattner074be1f2004-11-15 04:44:20 +00001203 //
1204 // Anything on this worklist that is overdefined need not be visited
1205 // since all of its users will have already been marked as overdefined.
Chris Lattnera3c39d32009-11-02 02:33:50 +00001206 // Update all of the users of this instruction's value.
Chris Lattner074be1f2004-11-15 04:44:20 +00001207 //
Duncan Sands19d0b472010-02-16 11:11:14 +00001208 if (I->getType()->isStructTy() || !getValueState(I).isOverdefined())
Chandler Carruthcdf47882014-03-09 03:16:01 +00001209 for (User *U : I->users())
1210 if (Instruction *UI = dyn_cast<Instruction>(U))
1211 OperandChangedState(UI);
Chris Lattner074be1f2004-11-15 04:44:20 +00001212 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001213
Chris Lattnera3c39d32009-11-02 02:33:50 +00001214 // Process the basic block work list.
Chris Lattner074be1f2004-11-15 04:44:20 +00001215 while (!BBWorkList.empty()) {
1216 BasicBlock *BB = BBWorkList.back();
1217 BBWorkList.pop_back();
Misha Brukmanb1c93172005-04-21 23:48:37 +00001218
David Greene389fc3b2010-01-05 01:27:15 +00001219 DEBUG(dbgs() << "\nPopped off BBWL: " << *BB << '\n');
Misha Brukmanb1c93172005-04-21 23:48:37 +00001220
Chris Lattner074be1f2004-11-15 04:44:20 +00001221 // Notify all instructions in this basic block that they are newly
1222 // executable.
1223 visit(BB);
1224 }
1225 }
1226}
1227
Chris Lattner1847f6d2006-12-20 06:21:33 +00001228/// ResolvedUndefsIn - While solving the dataflow for a function, we assume
Chris Lattner7285f432004-12-10 20:41:50 +00001229/// that branches on undef values cannot reach any of their successors.
1230/// However, this is not a safe assumption. After we solve dataflow, this
1231/// method should be use to handle this. If this returns true, the solver
1232/// should be rerun.
Chris Lattneraf170962006-10-22 05:59:17 +00001233///
1234/// This method handles this by finding an unresolved branch and marking it one
1235/// of the edges from the block as being feasible, even though the condition
1236/// doesn't say it would otherwise be. This allows SCCP to find the rest of the
1237/// CFG and only slightly pessimizes the analysis results (by marking one,
Chris Lattner1847f6d2006-12-20 06:21:33 +00001238/// potentially infeasible, edge feasible). This cannot usefully modify the
Chris Lattneraf170962006-10-22 05:59:17 +00001239/// constraints on the condition of the branch, as that would impact other users
1240/// of the value.
Chris Lattner1847f6d2006-12-20 06:21:33 +00001241///
1242/// This scan also checks for values that use undefs, whose results are actually
1243/// defined. For example, 'zext i8 undef to i32' should produce all zeros
1244/// conservatively, as "(zext i8 X -> i32) & 0xFF00" must always return zero,
1245/// even if X isn't defined.
1246bool SCCPSolver::ResolvedUndefsIn(Function &F) {
Chris Lattneraf170962006-10-22 05:59:17 +00001247 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1248 if (!BBExecutable.count(BB))
1249 continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001250
Chris Lattner1847f6d2006-12-20 06:21:33 +00001251 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
1252 // Look for instructions which produce undef values.
Chris Lattnerfdd87902009-10-05 05:54:46 +00001253 if (I->getType()->isVoidTy()) continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001254
Chris Lattner229907c2011-07-18 04:54:35 +00001255 if (StructType *STy = dyn_cast<StructType>(I->getType())) {
Eli Friedman1815b682011-09-20 23:28:51 +00001256 // Only a few things that can be structs matter for undef.
1257
1258 // Tracked calls must never be marked overdefined in ResolvedUndefsIn.
1259 if (CallSite CS = CallSite(I))
1260 if (Function *F = CS.getCalledFunction())
1261 if (MRVFunctionsTracked.count(F))
1262 continue;
1263
1264 // extractvalue and insertvalue don't need to be marked; they are
Jakub Staszak632a3552012-01-18 21:16:33 +00001265 // tracked as precisely as their operands.
Eli Friedman1815b682011-09-20 23:28:51 +00001266 if (isa<ExtractValueInst>(I) || isa<InsertValueInst>(I))
1267 continue;
1268
1269 // Send the results of everything else to overdefined. We could be
1270 // more precise than this but it isn't worth bothering.
1271 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1272 LatticeVal &LV = getStructValueState(I, i);
1273 if (LV.isUndefined())
1274 markOverdefined(LV, I);
Chris Lattner156b8c72009-11-03 23:40:48 +00001275 }
1276 continue;
1277 }
Eli Friedman0793eb42011-08-16 22:06:31 +00001278
Chris Lattner1847f6d2006-12-20 06:21:33 +00001279 LatticeVal &LV = getValueState(I);
1280 if (!LV.isUndefined()) continue;
1281
Eli Friedmand7749be2011-08-17 18:10:43 +00001282 // extractvalue is safe; check here because the argument is a struct.
1283 if (isa<ExtractValueInst>(I))
1284 continue;
1285
1286 // Compute the operand LatticeVals, for convenience below.
1287 // Anything taking a struct is conservatively assumed to require
1288 // overdefined markings.
1289 if (I->getOperand(0)->getType()->isStructTy()) {
1290 markOverdefined(I);
1291 return true;
1292 }
Chris Lattnerf5484032009-11-02 05:55:40 +00001293 LatticeVal Op0LV = getValueState(I->getOperand(0));
Chris Lattner1847f6d2006-12-20 06:21:33 +00001294 LatticeVal Op1LV;
Eli Friedmand7749be2011-08-17 18:10:43 +00001295 if (I->getNumOperands() == 2) {
1296 if (I->getOperand(1)->getType()->isStructTy()) {
1297 markOverdefined(I);
1298 return true;
1299 }
1300
Chris Lattner1847f6d2006-12-20 06:21:33 +00001301 Op1LV = getValueState(I->getOperand(1));
Eli Friedmand7749be2011-08-17 18:10:43 +00001302 }
Chris Lattner1847f6d2006-12-20 06:21:33 +00001303 // If this is an instructions whose result is defined even if the input is
1304 // not fully defined, propagate the information.
Chris Lattner229907c2011-07-18 04:54:35 +00001305 Type *ITy = I->getType();
Chris Lattner1847f6d2006-12-20 06:21:33 +00001306 switch (I->getOpcode()) {
Eli Friedman0793eb42011-08-16 22:06:31 +00001307 case Instruction::Add:
1308 case Instruction::Sub:
1309 case Instruction::Trunc:
1310 case Instruction::FPTrunc:
1311 case Instruction::BitCast:
1312 break; // Any undef -> undef
1313 case Instruction::FSub:
1314 case Instruction::FAdd:
1315 case Instruction::FMul:
1316 case Instruction::FDiv:
1317 case Instruction::FRem:
1318 // Floating-point binary operation: be conservative.
1319 if (Op0LV.isUndefined() && Op1LV.isUndefined())
1320 markForcedConstant(I, Constant::getNullValue(ITy));
1321 else
1322 markOverdefined(I);
1323 return true;
Chris Lattner1847f6d2006-12-20 06:21:33 +00001324 case Instruction::ZExt:
Eli Friedman0793eb42011-08-16 22:06:31 +00001325 case Instruction::SExt:
1326 case Instruction::FPToUI:
1327 case Instruction::FPToSI:
1328 case Instruction::FPExt:
1329 case Instruction::PtrToInt:
1330 case Instruction::IntToPtr:
1331 case Instruction::SIToFP:
1332 case Instruction::UIToFP:
1333 // undef -> 0; some outputs are impossible
Chris Lattnerf5484032009-11-02 05:55:40 +00001334 markForcedConstant(I, Constant::getNullValue(ITy));
Chris Lattner1847f6d2006-12-20 06:21:33 +00001335 return true;
1336 case Instruction::Mul:
1337 case Instruction::And:
Eli Friedman0793eb42011-08-16 22:06:31 +00001338 // Both operands undef -> undef
1339 if (Op0LV.isUndefined() && Op1LV.isUndefined())
1340 break;
Chris Lattner1847f6d2006-12-20 06:21:33 +00001341 // undef * X -> 0. X could be zero.
1342 // undef & X -> 0. X could be zero.
Chris Lattnerf5484032009-11-02 05:55:40 +00001343 markForcedConstant(I, Constant::getNullValue(ITy));
Chris Lattner1847f6d2006-12-20 06:21:33 +00001344 return true;
1345
1346 case Instruction::Or:
Eli Friedman0793eb42011-08-16 22:06:31 +00001347 // Both operands undef -> undef
1348 if (Op0LV.isUndefined() && Op1LV.isUndefined())
1349 break;
Chris Lattner1847f6d2006-12-20 06:21:33 +00001350 // undef | X -> -1. X could be -1.
Chris Lattnerf5484032009-11-02 05:55:40 +00001351 markForcedConstant(I, Constant::getAllOnesValue(ITy));
Chris Lattner806adaf2007-01-04 02:12:40 +00001352 return true;
Chris Lattner1847f6d2006-12-20 06:21:33 +00001353
Eli Friedman0793eb42011-08-16 22:06:31 +00001354 case Instruction::Xor:
1355 // undef ^ undef -> 0; strictly speaking, this is not strictly
1356 // necessary, but we try to be nice to people who expect this
1357 // behavior in simple cases
1358 if (Op0LV.isUndefined() && Op1LV.isUndefined()) {
1359 markForcedConstant(I, Constant::getNullValue(ITy));
1360 return true;
1361 }
1362 // undef ^ X -> undef
1363 break;
1364
Chris Lattner1847f6d2006-12-20 06:21:33 +00001365 case Instruction::SDiv:
1366 case Instruction::UDiv:
1367 case Instruction::SRem:
1368 case Instruction::URem:
1369 // X / undef -> undef. No change.
1370 // X % undef -> undef. No change.
1371 if (Op1LV.isUndefined()) break;
Jakub Staszak632a3552012-01-18 21:16:33 +00001372
Chris Lattner1847f6d2006-12-20 06:21:33 +00001373 // undef / X -> 0. X could be maxint.
1374 // undef % X -> 0. X could be 1.
Chris Lattnerf5484032009-11-02 05:55:40 +00001375 markForcedConstant(I, Constant::getNullValue(ITy));
Chris Lattner1847f6d2006-12-20 06:21:33 +00001376 return true;
Jakub Staszak632a3552012-01-18 21:16:33 +00001377
Chris Lattner1847f6d2006-12-20 06:21:33 +00001378 case Instruction::AShr:
Eli Friedman0793eb42011-08-16 22:06:31 +00001379 // X >>a undef -> undef.
1380 if (Op1LV.isUndefined()) break;
1381
1382 // undef >>a X -> all ones
1383 markForcedConstant(I, Constant::getAllOnesValue(ITy));
Chris Lattner1847f6d2006-12-20 06:21:33 +00001384 return true;
1385 case Instruction::LShr:
1386 case Instruction::Shl:
Eli Friedman0793eb42011-08-16 22:06:31 +00001387 // X << undef -> undef.
1388 // X >> undef -> undef.
1389 if (Op1LV.isUndefined()) break;
1390
1391 // undef << X -> 0
1392 // undef >> X -> 0
Chris Lattnerf5484032009-11-02 05:55:40 +00001393 markForcedConstant(I, Constant::getNullValue(ITy));
Chris Lattner1847f6d2006-12-20 06:21:33 +00001394 return true;
1395 case Instruction::Select:
Eli Friedman0793eb42011-08-16 22:06:31 +00001396 Op1LV = getValueState(I->getOperand(1));
Chris Lattner1847f6d2006-12-20 06:21:33 +00001397 // undef ? X : Y -> X or Y. There could be commonality between X/Y.
1398 if (Op0LV.isUndefined()) {
1399 if (!Op1LV.isConstant()) // Pick the constant one if there is any.
1400 Op1LV = getValueState(I->getOperand(2));
1401 } else if (Op1LV.isUndefined()) {
1402 // c ? undef : undef -> undef. No change.
1403 Op1LV = getValueState(I->getOperand(2));
1404 if (Op1LV.isUndefined())
1405 break;
1406 // Otherwise, c ? undef : x -> x.
1407 } else {
1408 // Leave Op1LV as Operand(1)'s LatticeValue.
1409 }
Jakub Staszak632a3552012-01-18 21:16:33 +00001410
Chris Lattner1847f6d2006-12-20 06:21:33 +00001411 if (Op1LV.isConstant())
Chris Lattnerf5484032009-11-02 05:55:40 +00001412 markForcedConstant(I, Op1LV.getConstant());
Chris Lattner1847f6d2006-12-20 06:21:33 +00001413 else
Chris Lattnerf5484032009-11-02 05:55:40 +00001414 markOverdefined(I);
Chris Lattner1847f6d2006-12-20 06:21:33 +00001415 return true;
Eli Friedman0793eb42011-08-16 22:06:31 +00001416 case Instruction::Load:
1417 // A load here means one of two things: a load of undef from a global,
1418 // a load from an unknown pointer. Either way, having it return undef
1419 // is okay.
1420 break;
1421 case Instruction::ICmp:
1422 // X == undef -> undef. Other comparisons get more complicated.
1423 if (cast<ICmpInst>(I)->isEquality())
1424 break;
1425 markOverdefined(I);
1426 return true;
Eli Friedman1815b682011-09-20 23:28:51 +00001427 case Instruction::Call:
1428 case Instruction::Invoke: {
1429 // There are two reasons a call can have an undef result
1430 // 1. It could be tracked.
1431 // 2. It could be constant-foldable.
1432 // Because of the way we solve return values, tracked calls must
1433 // never be marked overdefined in ResolvedUndefsIn.
1434 if (Function *F = CallSite(I).getCalledFunction())
1435 if (TrackedRetVals.count(F))
1436 break;
1437
1438 // If the call is constant-foldable, we mark it overdefined because
1439 // we do not know what return values are valid.
1440 markOverdefined(I);
1441 return true;
1442 }
Eli Friedman0793eb42011-08-16 22:06:31 +00001443 default:
1444 // If we don't know what should happen here, conservatively mark it
Chris Lattner5c207c82008-05-24 03:59:33 +00001445 // overdefined.
Chris Lattnerf5484032009-11-02 05:55:40 +00001446 markOverdefined(I);
Chris Lattner5c207c82008-05-24 03:59:33 +00001447 return true;
Chris Lattner1847f6d2006-12-20 06:21:33 +00001448 }
1449 }
Jakub Staszak632a3552012-01-18 21:16:33 +00001450
Chris Lattneradca6082010-04-05 22:14:48 +00001451 // Check to see if we have a branch or switch on an undefined value. If so
1452 // we force the branch to go one way or the other to make the successor
1453 // values live. It doesn't really matter which way we force it.
Chris Lattneraf170962006-10-22 05:59:17 +00001454 TerminatorInst *TI = BB->getTerminator();
1455 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
1456 if (!BI->isConditional()) continue;
1457 if (!getValueState(BI->getCondition()).isUndefined())
1458 continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001459
Chris Lattneradca6082010-04-05 22:14:48 +00001460 // If the input to SCCP is actually branch on undef, fix the undef to
1461 // false.
1462 if (isa<UndefValue>(BI->getCondition())) {
1463 BI->setCondition(ConstantInt::getFalse(BI->getContext()));
1464 markEdgeExecutable(BB, TI->getSuccessor(1));
1465 return true;
1466 }
Jakub Staszak632a3552012-01-18 21:16:33 +00001467
Chris Lattneradca6082010-04-05 22:14:48 +00001468 // Otherwise, it is a branch on a symbolic value which is currently
1469 // considered to be undef. Handle this by forcing the input value to the
1470 // branch to false.
1471 markForcedConstant(BI->getCondition(),
1472 ConstantInt::getFalse(TI->getContext()));
1473 return true;
1474 }
Jakub Staszak632a3552012-01-18 21:16:33 +00001475
Chris Lattneradca6082010-04-05 22:14:48 +00001476 if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00001477 if (!SI->getNumCases())
Dale Johannesenfecb8822008-05-23 01:01:31 +00001478 continue;
Chris Lattneraf170962006-10-22 05:59:17 +00001479 if (!getValueState(SI->getCondition()).isUndefined())
1480 continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001481
Chris Lattneradca6082010-04-05 22:14:48 +00001482 // If the input to SCCP is actually switch on undef, fix the undef to
1483 // the first constant.
1484 if (isa<UndefValue>(SI->getCondition())) {
Stepan Dyatkovskiy97b02fc2012-03-11 06:09:17 +00001485 SI->setCondition(SI->case_begin().getCaseValue());
1486 markEdgeExecutable(BB, SI->case_begin().getCaseSuccessor());
Chris Lattneradca6082010-04-05 22:14:48 +00001487 return true;
1488 }
Jakub Staszak632a3552012-01-18 21:16:33 +00001489
Stepan Dyatkovskiy97b02fc2012-03-11 06:09:17 +00001490 markForcedConstant(SI->getCondition(), SI->case_begin().getCaseValue());
Chris Lattneradca6082010-04-05 22:14:48 +00001491 return true;
Chris Lattner7285f432004-12-10 20:41:50 +00001492 }
Chris Lattneraf170962006-10-22 05:59:17 +00001493 }
Chris Lattner2f687fd2004-12-11 06:05:53 +00001494
Chris Lattneraf170962006-10-22 05:59:17 +00001495 return false;
Chris Lattner7285f432004-12-10 20:41:50 +00001496}
1497
Chris Lattner074be1f2004-11-15 04:44:20 +00001498
1499namespace {
Chris Lattner1890f942004-11-15 07:15:04 +00001500 //===--------------------------------------------------------------------===//
Chris Lattner074be1f2004-11-15 04:44:20 +00001501 //
Chris Lattner1890f942004-11-15 07:15:04 +00001502 /// SCCP Class - This class uses the SCCPSolver to implement a per-function
Reid Spencere8a74ee2006-12-31 22:26:06 +00001503 /// Sparse Conditional Constant Propagator.
Chris Lattner1890f942004-11-15 07:15:04 +00001504 ///
Chris Lattner2dd09db2009-09-02 06:11:42 +00001505 struct SCCP : public FunctionPass {
Craig Topper3e4c6972014-03-05 09:10:37 +00001506 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001507 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chad Rosiere6de63d2011-12-01 21:29:16 +00001508 }
Nick Lewyckye7da2d62007-05-06 13:37:16 +00001509 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +00001510 SCCP() : FunctionPass(ID) {
1511 initializeSCCPPass(*PassRegistry::getPassRegistry());
1512 }
Devang Patel09f162c2007-05-01 21:15:47 +00001513
Chris Lattner1890f942004-11-15 07:15:04 +00001514 // runOnFunction - Run the Sparse Conditional Constant Propagation
1515 // algorithm, and return true if the function was modified.
1516 //
Craig Topper3e4c6972014-03-05 09:10:37 +00001517 bool runOnFunction(Function &F) override;
Chris Lattner1890f942004-11-15 07:15:04 +00001518 };
Chris Lattner074be1f2004-11-15 04:44:20 +00001519} // end anonymous namespace
1520
Dan Gohmand78c4002008-05-13 00:00:25 +00001521char SCCP::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +00001522INITIALIZE_PASS(SCCP, "sccp",
Owen Andersondf7a4f22010-10-07 22:25:06 +00001523 "Sparse Conditional Constant Propagation", false, false)
Chris Lattner074be1f2004-11-15 04:44:20 +00001524
Chris Lattnera3c39d32009-11-02 02:33:50 +00001525// createSCCPPass - This is the public interface to this file.
Chris Lattner074be1f2004-11-15 04:44:20 +00001526FunctionPass *llvm::createSCCPPass() {
1527 return new SCCP();
1528}
1529
Chris Lattnere405ed92009-11-02 02:47:51 +00001530static void DeleteInstructionInBlock(BasicBlock *BB) {
David Greene389fc3b2010-01-05 01:27:15 +00001531 DEBUG(dbgs() << " BasicBlock Dead:" << *BB);
Chris Lattnere405ed92009-11-02 02:47:51 +00001532 ++NumDeadBlocks;
Bill Wendling770d0f02011-08-31 20:55:20 +00001533
1534 // Check to see if there are non-terminating instructions to delete.
1535 if (isa<TerminatorInst>(BB->begin()))
1536 return;
1537
Bill Wendling321fb372011-09-04 09:43:36 +00001538 // Delete the instructions backwards, as it has a reduced likelihood of having
1539 // to update as many def-use and use-def chains.
1540 Instruction *EndInst = BB->getTerminator(); // Last not to be deleted.
1541 while (EndInst != BB->begin()) {
1542 // Delete the next to last instruction.
1543 BasicBlock::iterator I = EndInst;
1544 Instruction *Inst = --I;
Bill Wendlingbf8280f2011-09-01 21:28:33 +00001545 if (!Inst->use_empty())
1546 Inst->replaceAllUsesWith(UndefValue::get(Inst->getType()));
Bill Wendling321fb372011-09-04 09:43:36 +00001547 if (isa<LandingPadInst>(Inst)) {
1548 EndInst = Inst;
Bill Wendling770d0f02011-08-31 20:55:20 +00001549 continue;
Bill Wendling321fb372011-09-04 09:43:36 +00001550 }
Bill Wendlingbf8280f2011-09-01 21:28:33 +00001551 BB->getInstList().erase(Inst);
Chris Lattnere405ed92009-11-02 02:47:51 +00001552 ++NumInstRemoved;
1553 }
1554}
Chris Lattner074be1f2004-11-15 04:44:20 +00001555
Chris Lattner074be1f2004-11-15 04:44:20 +00001556// runOnFunction() - Run the Sparse Conditional Constant Propagation algorithm,
1557// and return true if the function was modified.
1558//
1559bool SCCP::runOnFunction(Function &F) {
Paul Robinsonaf4e64d2014-02-06 00:07:05 +00001560 if (skipOptnoneFunction(F))
1561 return false;
1562
David Greene389fc3b2010-01-05 01:27:15 +00001563 DEBUG(dbgs() << "SCCP on function '" << F.getName() << "'\n");
Mehdi Amini46a43552015-03-04 18:43:29 +00001564 const DataLayout &DL = F.getParent()->getDataLayout();
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001565 const TargetLibraryInfo *TLI =
1566 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Rafael Espindola37dc9e12014-02-21 00:06:31 +00001567 SCCPSolver Solver(DL, TLI);
Chris Lattner074be1f2004-11-15 04:44:20 +00001568
1569 // Mark the first block of the function as being executable.
1570 Solver.MarkBlockExecutable(F.begin());
1571
Chris Lattnerd18c16b2004-11-15 05:45:33 +00001572 // Mark all arguments to the function as being overdefined.
Chris Lattner28d921d2007-04-14 23:32:02 +00001573 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E;++AI)
Chris Lattner156b8c72009-11-03 23:40:48 +00001574 Solver.markAnythingOverdefined(AI);
Chris Lattnerd18c16b2004-11-15 05:45:33 +00001575
Chris Lattner074be1f2004-11-15 04:44:20 +00001576 // Solve for constants.
Chris Lattner1847f6d2006-12-20 06:21:33 +00001577 bool ResolvedUndefs = true;
1578 while (ResolvedUndefs) {
Chris Lattner7285f432004-12-10 20:41:50 +00001579 Solver.Solve();
David Greene389fc3b2010-01-05 01:27:15 +00001580 DEBUG(dbgs() << "RESOLVING UNDEFs\n");
Chris Lattner1847f6d2006-12-20 06:21:33 +00001581 ResolvedUndefs = Solver.ResolvedUndefsIn(F);
Chris Lattner7285f432004-12-10 20:41:50 +00001582 }
Chris Lattner074be1f2004-11-15 04:44:20 +00001583
Chris Lattnerd18c16b2004-11-15 05:45:33 +00001584 bool MadeChanges = false;
1585
1586 // If we decided that there are basic blocks that are dead in this function,
1587 // delete their contents now. Note that we cannot actually delete the blocks,
1588 // as we cannot modify the CFG of the function.
Chris Lattnerc33fd462007-03-04 04:50:21 +00001589
Chris Lattnere405ed92009-11-02 02:47:51 +00001590 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
Chris Lattneradd44f32008-08-23 23:39:31 +00001591 if (!Solver.isBlockExecutable(BB)) {
Chris Lattnere405ed92009-11-02 02:47:51 +00001592 DeleteInstructionInBlock(BB);
1593 MadeChanges = true;
1594 continue;
Chris Lattner074be1f2004-11-15 04:44:20 +00001595 }
Jakub Staszak632a3552012-01-18 21:16:33 +00001596
Chris Lattnere405ed92009-11-02 02:47:51 +00001597 // Iterate over all of the instructions in a function, replacing them with
1598 // constants if we have found them to be of constant values.
1599 //
1600 for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
1601 Instruction *Inst = BI++;
1602 if (Inst->getType()->isVoidTy() || isa<TerminatorInst>(Inst))
1603 continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001604
Chris Lattner156b8c72009-11-03 23:40:48 +00001605 // TODO: Reconstruct structs from their elements.
Duncan Sands19d0b472010-02-16 11:11:14 +00001606 if (Inst->getType()->isStructTy())
Chris Lattner156b8c72009-11-03 23:40:48 +00001607 continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001608
Chris Lattnerb5a13d42009-11-02 02:54:24 +00001609 LatticeVal IV = Solver.getLatticeValueFor(Inst);
1610 if (IV.isOverdefined())
Chris Lattnere405ed92009-11-02 02:47:51 +00001611 continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001612
Chris Lattnere405ed92009-11-02 02:47:51 +00001613 Constant *Const = IV.isConstant()
1614 ? IV.getConstant() : UndefValue::get(Inst->getType());
Nick Lewycky5cd95382013-06-26 00:30:18 +00001615 DEBUG(dbgs() << " Constant: " << *Const << " = " << *Inst << '\n');
Chris Lattnere405ed92009-11-02 02:47:51 +00001616
1617 // Replaces all of the uses of a variable with uses of the constant.
1618 Inst->replaceAllUsesWith(Const);
Jakub Staszak632a3552012-01-18 21:16:33 +00001619
Chris Lattnere405ed92009-11-02 02:47:51 +00001620 // Delete the instruction.
1621 Inst->eraseFromParent();
Jakub Staszak632a3552012-01-18 21:16:33 +00001622
Chris Lattnere405ed92009-11-02 02:47:51 +00001623 // Hey, we just changed something!
1624 MadeChanges = true;
1625 ++NumInstRemoved;
1626 }
1627 }
Chris Lattner074be1f2004-11-15 04:44:20 +00001628
1629 return MadeChanges;
1630}
Chris Lattnerb4394642004-12-10 08:02:06 +00001631
1632namespace {
Chris Lattnerb4394642004-12-10 08:02:06 +00001633 //===--------------------------------------------------------------------===//
1634 //
1635 /// IPSCCP Class - This class implements interprocedural Sparse Conditional
1636 /// Constant Propagation.
1637 ///
Chris Lattner2dd09db2009-09-02 06:11:42 +00001638 struct IPSCCP : public ModulePass {
Craig Topper3e4c6972014-03-05 09:10:37 +00001639 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001640 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chad Rosiere6de63d2011-12-01 21:29:16 +00001641 }
Devang Patel8c78a0b2007-05-03 01:11:54 +00001642 static char ID;
Owen Anderson6c18d1a2010-10-19 17:21:58 +00001643 IPSCCP() : ModulePass(ID) {
1644 initializeIPSCCPPass(*PassRegistry::getPassRegistry());
1645 }
Craig Topper3e4c6972014-03-05 09:10:37 +00001646 bool runOnModule(Module &M) override;
Chris Lattnerb4394642004-12-10 08:02:06 +00001647 };
Chris Lattnerb4394642004-12-10 08:02:06 +00001648} // end anonymous namespace
1649
Dan Gohmand78c4002008-05-13 00:00:25 +00001650char IPSCCP::ID = 0;
Chad Rosiere6de63d2011-12-01 21:29:16 +00001651INITIALIZE_PASS_BEGIN(IPSCCP, "ipsccp",
1652 "Interprocedural Sparse Conditional Constant Propagation",
1653 false, false)
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001654INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Chad Rosiere6de63d2011-12-01 21:29:16 +00001655INITIALIZE_PASS_END(IPSCCP, "ipsccp",
Owen Andersona57b97e2010-07-21 22:09:45 +00001656 "Interprocedural Sparse Conditional Constant Propagation",
Owen Andersondf7a4f22010-10-07 22:25:06 +00001657 false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +00001658
Chris Lattnera3c39d32009-11-02 02:33:50 +00001659// createIPSCCPPass - This is the public interface to this file.
Chris Lattnerb4394642004-12-10 08:02:06 +00001660ModulePass *llvm::createIPSCCPPass() {
1661 return new IPSCCP();
1662}
1663
1664
Gabor Greif9027ffb2010-03-24 10:29:52 +00001665static bool AddressIsTaken(const GlobalValue *GV) {
Chris Lattner8cb10a12005-04-19 19:16:19 +00001666 // Delete any dead constantexpr klingons.
1667 GV->removeDeadConstantUsers();
1668
Chandler Carruthcdf47882014-03-09 03:16:01 +00001669 for (const Use &U : GV->uses()) {
1670 const User *UR = U.getUser();
1671 if (const StoreInst *SI = dyn_cast<StoreInst>(UR)) {
Chris Lattner91dbae62004-12-11 05:15:59 +00001672 if (SI->getOperand(0) == GV || SI->isVolatile())
1673 return true; // Storing addr of GV.
Chandler Carruthcdf47882014-03-09 03:16:01 +00001674 } else if (isa<InvokeInst>(UR) || isa<CallInst>(UR)) {
Chris Lattnerb4394642004-12-10 08:02:06 +00001675 // Make sure we are calling the function, not passing the address.
Chandler Carruthcdf47882014-03-09 03:16:01 +00001676 ImmutableCallSite CS(cast<Instruction>(UR));
1677 if (!CS.isCallee(&U))
Nick Lewyckyd73806a2008-11-03 03:49:14 +00001678 return true;
Chandler Carruthcdf47882014-03-09 03:16:01 +00001679 } else if (const LoadInst *LI = dyn_cast<LoadInst>(UR)) {
Chris Lattner91dbae62004-12-11 05:15:59 +00001680 if (LI->isVolatile())
1681 return true;
Chandler Carruthcdf47882014-03-09 03:16:01 +00001682 } else if (isa<BlockAddress>(UR)) {
Chris Lattner1a8b80e2009-11-01 06:11:53 +00001683 // blockaddress doesn't take the address of the function, it takes addr
1684 // of label.
Chris Lattner91dbae62004-12-11 05:15:59 +00001685 } else {
Chris Lattnerb4394642004-12-10 08:02:06 +00001686 return true;
1687 }
Gabor Greif9027ffb2010-03-24 10:29:52 +00001688 }
Chris Lattnerb4394642004-12-10 08:02:06 +00001689 return false;
1690}
1691
1692bool IPSCCP::runOnModule(Module &M) {
Mehdi Amini46a43552015-03-04 18:43:29 +00001693 const DataLayout &DL = M.getDataLayout();
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001694 const TargetLibraryInfo *TLI =
1695 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Rafael Espindola37dc9e12014-02-21 00:06:31 +00001696 SCCPSolver Solver(DL, TLI);
Chris Lattnerb4394642004-12-10 08:02:06 +00001697
Chris Lattner363226d2010-08-12 22:25:23 +00001698 // AddressTakenFunctions - This set keeps track of the address-taken functions
1699 // that are in the input. As IPSCCP runs through and simplifies code,
1700 // functions that were address taken can end up losing their
1701 // address-taken-ness. Because of this, we keep track of their addresses from
1702 // the first pass so we can use them for the later simplification pass.
1703 SmallPtrSet<Function*, 32> AddressTakenFunctions;
Jakub Staszak632a3552012-01-18 21:16:33 +00001704
Chris Lattnerb4394642004-12-10 08:02:06 +00001705 // Loop over all functions, marking arguments to those with their addresses
1706 // taken or that are external as overdefined.
1707 //
Chris Lattner47837c52009-11-02 06:34:04 +00001708 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
1709 if (F->isDeclaration())
1710 continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001711
Chris Lattnerfb141812009-11-03 03:42:51 +00001712 // If this is a strong or ODR definition of this function, then we can
1713 // propagate information about its result into callsites of it.
Chris Lattner156b8c72009-11-03 23:40:48 +00001714 if (!F->mayBeOverridden())
Chris Lattnerb4394642004-12-10 08:02:06 +00001715 Solver.AddTrackedFunction(F);
Jakub Staszak632a3552012-01-18 21:16:33 +00001716
Chris Lattnerfb141812009-11-03 03:42:51 +00001717 // If this function only has direct calls that we can see, we can track its
1718 // arguments and return value aggressively, and can assume it is not called
1719 // unless we see evidence to the contrary.
Chris Lattner363226d2010-08-12 22:25:23 +00001720 if (F->hasLocalLinkage()) {
1721 if (AddressIsTaken(F))
1722 AddressTakenFunctions.insert(F);
1723 else {
1724 Solver.AddArgumentTrackedFunction(F);
1725 continue;
1726 }
Chris Lattnercde8de52009-11-03 19:24:51 +00001727 }
Chris Lattnerfb141812009-11-03 03:42:51 +00001728
1729 // Assume the function is called.
1730 Solver.MarkBlockExecutable(F->begin());
Jakub Staszak632a3552012-01-18 21:16:33 +00001731
Chris Lattnerfb141812009-11-03 03:42:51 +00001732 // Assume nothing about the incoming arguments.
1733 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1734 AI != E; ++AI)
Chris Lattner156b8c72009-11-03 23:40:48 +00001735 Solver.markAnythingOverdefined(AI);
Chris Lattner47837c52009-11-02 06:34:04 +00001736 }
Chris Lattnerb4394642004-12-10 08:02:06 +00001737
Chris Lattner91dbae62004-12-11 05:15:59 +00001738 // Loop over global variables. We inform the solver about any internal global
1739 // variables that do not have their 'addresses taken'. If they don't have
1740 // their addresses taken, we can propagate constants through them.
Chris Lattner8cb10a12005-04-19 19:16:19 +00001741 for (Module::global_iterator G = M.global_begin(), E = M.global_end();
1742 G != E; ++G)
Rafael Espindola6de96a12009-01-15 20:18:42 +00001743 if (!G->isConstant() && G->hasLocalLinkage() && !AddressIsTaken(G))
Chris Lattner91dbae62004-12-11 05:15:59 +00001744 Solver.TrackValueOfGlobalVariable(G);
1745
Chris Lattnerb4394642004-12-10 08:02:06 +00001746 // Solve for constants.
Chris Lattner1847f6d2006-12-20 06:21:33 +00001747 bool ResolvedUndefs = true;
1748 while (ResolvedUndefs) {
Chris Lattner7285f432004-12-10 20:41:50 +00001749 Solver.Solve();
1750
David Greene389fc3b2010-01-05 01:27:15 +00001751 DEBUG(dbgs() << "RESOLVING UNDEFS\n");
Chris Lattner1847f6d2006-12-20 06:21:33 +00001752 ResolvedUndefs = false;
Chris Lattner7285f432004-12-10 20:41:50 +00001753 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
Chris Lattner1847f6d2006-12-20 06:21:33 +00001754 ResolvedUndefs |= Solver.ResolvedUndefsIn(*F);
Chris Lattner7285f432004-12-10 20:41:50 +00001755 }
Chris Lattnerb4394642004-12-10 08:02:06 +00001756
1757 bool MadeChanges = false;
1758
1759 // Iterate over all of the instructions in the module, replacing them with
1760 // constants if we have found them to be of constant values.
1761 //
Chris Lattner65938fc2008-08-23 23:36:38 +00001762 SmallVector<BasicBlock*, 512> BlocksToErase;
Chris Lattner37d400a2007-02-02 21:15:06 +00001763
Chris Lattnerb4394642004-12-10 08:02:06 +00001764 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
Chris Lattnere82b0872009-11-02 03:25:55 +00001765 if (Solver.isBlockExecutable(F->begin())) {
1766 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1767 AI != E; ++AI) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001768 if (AI->use_empty() || AI->getType()->isStructTy()) continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001769
Chris Lattner156b8c72009-11-03 23:40:48 +00001770 // TODO: Could use getStructLatticeValueFor to find out if the entire
1771 // result is a constant and replace it entirely if so.
1772
Chris Lattnere82b0872009-11-02 03:25:55 +00001773 LatticeVal IV = Solver.getLatticeValueFor(AI);
1774 if (IV.isOverdefined()) continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001775
Chris Lattnere82b0872009-11-02 03:25:55 +00001776 Constant *CST = IV.isConstant() ?
1777 IV.getConstant() : UndefValue::get(AI->getType());
David Greene389fc3b2010-01-05 01:27:15 +00001778 DEBUG(dbgs() << "*** Arg " << *AI << " = " << *CST <<"\n");
Jakub Staszak632a3552012-01-18 21:16:33 +00001779
Chris Lattnere82b0872009-11-02 03:25:55 +00001780 // Replaces all of the uses of a variable with uses of the
1781 // constant.
1782 AI->replaceAllUsesWith(CST);
1783 ++IPNumArgsElimed;
1784 }
Chris Lattnerb5a13d42009-11-02 02:54:24 +00001785 }
Chris Lattnerb4394642004-12-10 08:02:06 +00001786
Chris Lattnere405ed92009-11-02 02:47:51 +00001787 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Chris Lattneradd44f32008-08-23 23:39:31 +00001788 if (!Solver.isBlockExecutable(BB)) {
Chris Lattnere405ed92009-11-02 02:47:51 +00001789 DeleteInstructionInBlock(BB);
1790 MadeChanges = true;
Chris Lattner7285f432004-12-10 20:41:50 +00001791
Chris Lattnerbae4b642004-12-10 22:29:08 +00001792 TerminatorInst *TI = BB->getTerminator();
Chris Lattnerbae4b642004-12-10 22:29:08 +00001793 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
1794 BasicBlock *Succ = TI->getSuccessor(i);
Dan Gohmanc731c972007-10-03 19:26:29 +00001795 if (!Succ->empty() && isa<PHINode>(Succ->begin()))
Chris Lattnerbae4b642004-12-10 22:29:08 +00001796 TI->getSuccessor(i)->removePredecessor(BB);
1797 }
Chris Lattner99e12952004-12-11 02:53:57 +00001798 if (!TI->use_empty())
Owen Andersonb292b8c2009-07-30 23:03:37 +00001799 TI->replaceAllUsesWith(UndefValue::get(TI->getType()));
Chris Lattnere405ed92009-11-02 02:47:51 +00001800 TI->eraseFromParent();
Chris Lattnerbae4b642004-12-10 22:29:08 +00001801
Chris Lattner8525ebe2004-12-11 05:32:19 +00001802 if (&*BB != &F->front())
1803 BlocksToErase.push_back(BB);
1804 else
Owen Anderson55f1c092009-08-13 21:58:54 +00001805 new UnreachableInst(M.getContext(), BB);
Chris Lattnere405ed92009-11-02 02:47:51 +00001806 continue;
Chris Lattnerb4394642004-12-10 08:02:06 +00001807 }
Jakub Staszak632a3552012-01-18 21:16:33 +00001808
Chris Lattnere405ed92009-11-02 02:47:51 +00001809 for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
1810 Instruction *Inst = BI++;
Duncan Sands19d0b472010-02-16 11:11:14 +00001811 if (Inst->getType()->isVoidTy() || Inst->getType()->isStructTy())
Chris Lattnere405ed92009-11-02 02:47:51 +00001812 continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001813
Chris Lattner156b8c72009-11-03 23:40:48 +00001814 // TODO: Could use getStructLatticeValueFor to find out if the entire
1815 // result is a constant and replace it entirely if so.
Jakub Staszak632a3552012-01-18 21:16:33 +00001816
Chris Lattnerb5a13d42009-11-02 02:54:24 +00001817 LatticeVal IV = Solver.getLatticeValueFor(Inst);
1818 if (IV.isOverdefined())
Chris Lattnere405ed92009-11-02 02:47:51 +00001819 continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001820
Chris Lattnere405ed92009-11-02 02:47:51 +00001821 Constant *Const = IV.isConstant()
1822 ? IV.getConstant() : UndefValue::get(Inst->getType());
Nick Lewycky5cd95382013-06-26 00:30:18 +00001823 DEBUG(dbgs() << " Constant: " << *Const << " = " << *Inst << '\n');
Chris Lattnere405ed92009-11-02 02:47:51 +00001824
1825 // Replaces all of the uses of a variable with uses of the
1826 // constant.
1827 Inst->replaceAllUsesWith(Const);
Jakub Staszak632a3552012-01-18 21:16:33 +00001828
Chris Lattnere405ed92009-11-02 02:47:51 +00001829 // Delete the instruction.
1830 if (!isa<CallInst>(Inst) && !isa<TerminatorInst>(Inst))
1831 Inst->eraseFromParent();
1832
1833 // Hey, we just changed something!
1834 MadeChanges = true;
1835 ++IPNumInstRemoved;
1836 }
1837 }
Chris Lattnerbae4b642004-12-10 22:29:08 +00001838
1839 // Now that all instructions in the function are constant folded, erase dead
1840 // blocks, because we can now use ConstantFoldTerminator to get rid of
1841 // in-edges.
1842 for (unsigned i = 0, e = BlocksToErase.size(); i != e; ++i) {
1843 // If there are any PHI nodes in this successor, drop entries for BB now.
1844 BasicBlock *DeadBB = BlocksToErase[i];
Chandler Carruthcdf47882014-03-09 03:16:01 +00001845 for (Value::user_iterator UI = DeadBB->user_begin(),
1846 UE = DeadBB->user_end();
1847 UI != UE;) {
Dan Gohman1f522d92009-11-23 16:13:39 +00001848 // Grab the user and then increment the iterator early, as the user
1849 // will be deleted. Step past all adjacent uses from the same user.
1850 Instruction *I = dyn_cast<Instruction>(*UI);
1851 do { ++UI; } while (UI != UE && *UI == I);
1852
Dan Gohmand15302a2009-11-20 20:19:14 +00001853 // Ignore blockaddress users; BasicBlock's dtor will handle them.
Dan Gohmand15302a2009-11-20 20:19:14 +00001854 if (!I) continue;
1855
Chris Lattnerbae4b642004-12-10 22:29:08 +00001856 bool Folded = ConstantFoldTerminator(I->getParent());
Chris Lattnerfe7b6ef2006-10-23 18:57:02 +00001857 if (!Folded) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001858 // The constant folder may not have been able to fold the terminator
Chris Lattnerfe7b6ef2006-10-23 18:57:02 +00001859 // if this is a branch or switch on undef. Fold it manually as a
1860 // branch to the first successor.
Devang Patel45f1ae02008-11-21 01:52:59 +00001861#ifndef NDEBUG
Chris Lattnerfe7b6ef2006-10-23 18:57:02 +00001862 if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
1863 assert(BI->isConditional() && isa<UndefValue>(BI->getCondition()) &&
1864 "Branch should be foldable!");
1865 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
1866 assert(isa<UndefValue>(SI->getCondition()) && "Switch should fold");
1867 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001868 llvm_unreachable("Didn't fold away reference to block!");
Chris Lattnerfe7b6ef2006-10-23 18:57:02 +00001869 }
Devang Patel45f1ae02008-11-21 01:52:59 +00001870#endif
Jakub Staszak632a3552012-01-18 21:16:33 +00001871
Chris Lattnerfe7b6ef2006-10-23 18:57:02 +00001872 // Make this an uncond branch to the first successor.
1873 TerminatorInst *TI = I->getParent()->getTerminator();
Gabor Greife9ecc682008-04-06 20:25:17 +00001874 BranchInst::Create(TI->getSuccessor(0), TI);
Jakub Staszak632a3552012-01-18 21:16:33 +00001875
Chris Lattnerfe7b6ef2006-10-23 18:57:02 +00001876 // Remove entries in successor phi nodes to remove edges.
1877 for (unsigned i = 1, e = TI->getNumSuccessors(); i != e; ++i)
1878 TI->getSuccessor(i)->removePredecessor(TI->getParent());
Jakub Staszak632a3552012-01-18 21:16:33 +00001879
Chris Lattnerfe7b6ef2006-10-23 18:57:02 +00001880 // Remove the old terminator.
1881 TI->eraseFromParent();
1882 }
Chris Lattnerbae4b642004-12-10 22:29:08 +00001883 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001884
Chris Lattnerbae4b642004-12-10 22:29:08 +00001885 // Finally, delete the basic block.
1886 F->getBasicBlockList().erase(DeadBB);
1887 }
Chris Lattner37d400a2007-02-02 21:15:06 +00001888 BlocksToErase.clear();
Chris Lattnerb4394642004-12-10 08:02:06 +00001889 }
Chris Lattner99e12952004-12-11 02:53:57 +00001890
1891 // If we inferred constant or undef return values for a function, we replaced
1892 // all call uses with the inferred value. This means we don't need to bother
1893 // actually returning anything from the function. Replace all return
1894 // instructions with return undef.
Chris Lattnerd887f1d2010-02-27 00:07:42 +00001895 //
1896 // Do this in two stages: first identify the functions we should process, then
1897 // actually zap their returns. This is important because we can only do this
Chris Lattner2af7e3d2010-02-27 07:50:40 +00001898 // if the address of the function isn't taken. In cases where a return is the
Chris Lattnerd887f1d2010-02-27 00:07:42 +00001899 // last use of a function, the order of processing functions would affect
Chris Lattner2af7e3d2010-02-27 07:50:40 +00001900 // whether other functions are optimizable.
Chris Lattnerd887f1d2010-02-27 00:07:42 +00001901 SmallVector<ReturnInst*, 8> ReturnsToZap;
Jakub Staszak632a3552012-01-18 21:16:33 +00001902
Devang Patele418de32008-03-11 17:32:05 +00001903 // TODO: Process multiple value ret instructions also.
Devang Patela7a20752008-03-11 05:46:42 +00001904 const DenseMap<Function*, LatticeVal> &RV = Solver.getTrackedRetVals();
Chris Lattner067d6072007-02-02 20:38:30 +00001905 for (DenseMap<Function*, LatticeVal>::const_iterator I = RV.begin(),
Chris Lattnerfb141812009-11-03 03:42:51 +00001906 E = RV.end(); I != E; ++I) {
1907 Function *F = I->first;
1908 if (I->second.isOverdefined() || F->getReturnType()->isVoidTy())
1909 continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001910
Chris Lattnerfb141812009-11-03 03:42:51 +00001911 // We can only do this if we know that nothing else can call the function.
Chris Lattner363226d2010-08-12 22:25:23 +00001912 if (!F->hasLocalLinkage() || AddressTakenFunctions.count(F))
Chris Lattnerfb141812009-11-03 03:42:51 +00001913 continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001914
Chris Lattnerfb141812009-11-03 03:42:51 +00001915 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
1916 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))
1917 if (!isa<UndefValue>(RI->getOperand(0)))
Chris Lattnerd887f1d2010-02-27 00:07:42 +00001918 ReturnsToZap.push_back(RI);
1919 }
1920
1921 // Zap all returns which we've identified as zap to change.
1922 for (unsigned i = 0, e = ReturnsToZap.size(); i != e; ++i) {
1923 Function *F = ReturnsToZap[i]->getParent()->getParent();
1924 ReturnsToZap[i]->setOperand(0, UndefValue::get(F->getReturnType()));
Chris Lattnerfb141812009-11-03 03:42:51 +00001925 }
Jakub Staszak632a3552012-01-18 21:16:33 +00001926
Chad Rosierbb2a6da2012-03-28 00:35:33 +00001927 // If we inferred constant or undef values for globals variables, we can
1928 // delete the global and any stores that remain to it.
Chris Lattner067d6072007-02-02 20:38:30 +00001929 const DenseMap<GlobalVariable*, LatticeVal> &TG = Solver.getTrackedGlobals();
1930 for (DenseMap<GlobalVariable*, LatticeVal>::const_iterator I = TG.begin(),
Chris Lattner91dbae62004-12-11 05:15:59 +00001931 E = TG.end(); I != E; ++I) {
1932 GlobalVariable *GV = I->first;
1933 assert(!I->second.isOverdefined() &&
1934 "Overdefined values should have been taken out of the map!");
David Greene389fc3b2010-01-05 01:27:15 +00001935 DEBUG(dbgs() << "Found that GV '" << GV->getName() << "' is constant!\n");
Chris Lattner91dbae62004-12-11 05:15:59 +00001936 while (!GV->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001937 StoreInst *SI = cast<StoreInst>(GV->user_back());
Chris Lattner91dbae62004-12-11 05:15:59 +00001938 SI->eraseFromParent();
1939 }
1940 M.getGlobalList().erase(GV);
Chris Lattner2f687fd2004-12-11 06:05:53 +00001941 ++IPNumGlobalConst;
Chris Lattner91dbae62004-12-11 05:15:59 +00001942 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001943
Chris Lattnerb4394642004-12-10 08:02:06 +00001944 return MadeChanges;
1945}