blob: a35d2e8df4de86ae368cd05434ad43a460e2e3df [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"
James Molloyefbba722015-09-10 10:22:12 +000027#include "llvm/Analysis/GlobalsModRef.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/Analysis/ConstantFolding.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000029#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000030#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000031#include "llvm/IR/Constants.h"
32#include "llvm/IR/DataLayout.h"
33#include "llvm/IR/DerivedTypes.h"
Chandler Carruth7da14f12014-03-06 03:23:41 +000034#include "llvm/IR/InstVisitor.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/Instructions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/Pass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000037#include "llvm/Support/Debug.h"
38#include "llvm/Support/ErrorHandling.h"
39#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000040#include "llvm/Transforms/IPO.h"
41#include "llvm/Transforms/Utils/Local.h"
Chris Lattner347389d2001-06-27 23:38:11 +000042#include <algorithm>
Chris Lattner49525f82004-01-09 06:02:20 +000043using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000044
Chandler Carruth964daaa2014-04-22 02:55:47 +000045#define DEBUG_TYPE "sccp"
46
Chris Lattner79a42ac2006-12-19 21:40:18 +000047STATISTIC(NumInstRemoved, "Number of instructions removed");
48STATISTIC(NumDeadBlocks , "Number of basic blocks unreachable");
49
Nick Lewycky35e92c72008-03-08 07:48:41 +000050STATISTIC(IPNumInstRemoved, "Number of instructions removed by IPSCCP");
Chris Lattner79a42ac2006-12-19 21:40:18 +000051STATISTIC(IPNumArgsElimed ,"Number of arguments constant propagated by IPSCCP");
52STATISTIC(IPNumGlobalConst, "Number of globals found to be constant by IPSCCP");
53
Chris Lattner7d325382002-04-29 21:26:08 +000054namespace {
Chris Lattner1847f6d2006-12-20 06:21:33 +000055/// LatticeVal class - This class represents the different lattice values that
56/// an LLVM value may occupy. It is a simple class with value semantics.
57///
Chris Lattner2dd09db2009-09-02 06:11:42 +000058class LatticeVal {
Chris Lattnerefdd2bb2009-11-02 02:20:32 +000059 enum LatticeValueTy {
Chris Lattner1847f6d2006-12-20 06:21:33 +000060 /// undefined - This LLVM Value has no known value yet.
61 undefined,
Jakub Staszak632a3552012-01-18 21:16:33 +000062
Chris Lattner1847f6d2006-12-20 06:21:33 +000063 /// constant - This LLVM Value has a specific constant value.
64 constant,
65
66 /// forcedconstant - This LLVM Value was thought to be undef until
67 /// ResolvedUndefsIn. This is treated just like 'constant', but if merged
68 /// with another (different) constant, it goes to overdefined, instead of
69 /// asserting.
70 forcedconstant,
Jakub Staszak632a3552012-01-18 21:16:33 +000071
Chris Lattner1847f6d2006-12-20 06:21:33 +000072 /// overdefined - This instruction is not known to be constant, and we know
73 /// it has a value.
74 overdefined
Chris Lattnerefdd2bb2009-11-02 02:20:32 +000075 };
76
77 /// Val: This stores the current lattice value along with the Constant* for
78 /// the constant if this is a 'constant' or 'forcedconstant' value.
79 PointerIntPair<Constant *, 2, LatticeValueTy> Val;
Jakub Staszak632a3552012-01-18 21:16:33 +000080
Chris Lattnerefdd2bb2009-11-02 02:20:32 +000081 LatticeValueTy getLatticeValue() const {
82 return Val.getInt();
83 }
Jakub Staszak632a3552012-01-18 21:16:33 +000084
Chris Lattner347389d2001-06-27 23:38:11 +000085public:
Craig Topperf40110f2014-04-25 05:29:35 +000086 LatticeVal() : Val(nullptr, undefined) {}
Jakub Staszak632a3552012-01-18 21:16:33 +000087
Chris Lattner7ccf1a62009-11-02 03:03:42 +000088 bool isUndefined() const { return getLatticeValue() == undefined; }
89 bool isConstant() const {
Chris Lattnerefdd2bb2009-11-02 02:20:32 +000090 return getLatticeValue() == constant || getLatticeValue() == forcedconstant;
91 }
Chris Lattner7ccf1a62009-11-02 03:03:42 +000092 bool isOverdefined() const { return getLatticeValue() == overdefined; }
Jakub Staszak632a3552012-01-18 21:16:33 +000093
Chris Lattner7ccf1a62009-11-02 03:03:42 +000094 Constant *getConstant() const {
Chris Lattnerefdd2bb2009-11-02 02:20:32 +000095 assert(isConstant() && "Cannot get the constant of a non-constant!");
96 return Val.getPointer();
97 }
Jakub Staszak632a3552012-01-18 21:16:33 +000098
Chris Lattnerefdd2bb2009-11-02 02:20:32 +000099 /// markOverdefined - Return true if this is a change in status.
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000100 bool markOverdefined() {
Chris Lattnerefdd2bb2009-11-02 02:20:32 +0000101 if (isOverdefined())
102 return false;
Jakub Staszak632a3552012-01-18 21:16:33 +0000103
Chris Lattnerefdd2bb2009-11-02 02:20:32 +0000104 Val.setInt(overdefined);
105 return true;
Chris Lattner347389d2001-06-27 23:38:11 +0000106 }
107
Chris Lattnerefdd2bb2009-11-02 02:20:32 +0000108 /// markConstant - Return true if this is a change in status.
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000109 bool markConstant(Constant *V) {
Chris Lattnere1d5cd92009-11-03 16:50:11 +0000110 if (getLatticeValue() == constant) { // Constant but not forcedconstant.
Chris Lattnerefdd2bb2009-11-02 02:20:32 +0000111 assert(getConstant() == V && "Marking constant with different value");
112 return false;
Chris Lattner347389d2001-06-27 23:38:11 +0000113 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000114
Chris Lattnerefdd2bb2009-11-02 02:20:32 +0000115 if (isUndefined()) {
116 Val.setInt(constant);
117 assert(V && "Marking constant with NULL");
118 Val.setPointer(V);
119 } else {
Jakub Staszak632a3552012-01-18 21:16:33 +0000120 assert(getLatticeValue() == forcedconstant &&
Chris Lattnerefdd2bb2009-11-02 02:20:32 +0000121 "Cannot move from overdefined to constant!");
122 // Stay at forcedconstant if the constant is the same.
123 if (V == getConstant()) return false;
Jakub Staszak632a3552012-01-18 21:16:33 +0000124
Chris Lattnerefdd2bb2009-11-02 02:20:32 +0000125 // Otherwise, we go to overdefined. Assumptions made based on the
126 // forced value are possibly wrong. Assuming this is another constant
127 // could expose a contradiction.
128 Val.setInt(overdefined);
129 }
130 return true;
Chris Lattner347389d2001-06-27 23:38:11 +0000131 }
132
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000133 /// getConstantInt - If this is a constant with a ConstantInt value, return it
134 /// otherwise return null.
135 ConstantInt *getConstantInt() const {
136 if (isConstant())
137 return dyn_cast<ConstantInt>(getConstant());
Craig Topperf40110f2014-04-25 05:29:35 +0000138 return nullptr;
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000139 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000140
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000141 void markForcedConstant(Constant *V) {
Chris Lattnerefdd2bb2009-11-02 02:20:32 +0000142 assert(isUndefined() && "Can't force a defined value!");
143 Val.setInt(forcedconstant);
144 Val.setPointer(V);
Chris Lattner05fe6842004-01-12 03:57:30 +0000145 }
Chris Lattner347389d2001-06-27 23:38:11 +0000146};
Chris Lattnere405ed92009-11-02 02:47:51 +0000147} // end anonymous namespace.
148
149
150namespace {
Chris Lattner347389d2001-06-27 23:38:11 +0000151
Chris Lattner347389d2001-06-27 23:38:11 +0000152//===----------------------------------------------------------------------===//
Chris Lattner347389d2001-06-27 23:38:11 +0000153//
Chris Lattner074be1f2004-11-15 04:44:20 +0000154/// SCCPSolver - This class is a general purpose solver for Sparse Conditional
155/// Constant Propagation.
156///
157class SCCPSolver : public InstVisitor<SCCPSolver> {
Mehdi Amini46a43552015-03-04 18:43:29 +0000158 const DataLayout &DL;
Chad Rosiere6de63d2011-12-01 21:29:16 +0000159 const TargetLibraryInfo *TLI;
Nick Lewycky77cb8e62011-07-25 21:16:04 +0000160 SmallPtrSet<BasicBlock*, 8> BBExecutable; // The BBs that are executable.
Chris Lattnerf5484032009-11-02 05:55:40 +0000161 DenseMap<Value*, LatticeVal> ValueState; // The state each value is in.
Chris Lattner347389d2001-06-27 23:38:11 +0000162
Chris Lattner156b8c72009-11-03 23:40:48 +0000163 /// StructValueState - This maintains ValueState for values that have
164 /// StructType, for example for formal arguments, calls, insertelement, etc.
165 ///
166 DenseMap<std::pair<Value*, unsigned>, LatticeVal> StructValueState;
Jakub Staszak632a3552012-01-18 21:16:33 +0000167
Chris Lattner91dbae62004-12-11 05:15:59 +0000168 /// GlobalValue - If we are tracking any values for the contents of a global
169 /// variable, we keep a mapping from the constant accessor to the element of
170 /// the global, to the currently known value. If the value becomes
171 /// overdefined, it's entry is simply removed from this map.
Chris Lattner067d6072007-02-02 20:38:30 +0000172 DenseMap<GlobalVariable*, LatticeVal> TrackedGlobals;
Chris Lattner91dbae62004-12-11 05:15:59 +0000173
Devang Patela7a20752008-03-11 05:46:42 +0000174 /// TrackedRetVals - If we are tracking arguments into and the return
Chris Lattnerb4394642004-12-10 08:02:06 +0000175 /// value out of a function, it will have an entry in this map, indicating
176 /// what the known return value for the function is.
Devang Patela7a20752008-03-11 05:46:42 +0000177 DenseMap<Function*, LatticeVal> TrackedRetVals;
178
179 /// TrackedMultipleRetVals - Same as TrackedRetVals, but used for functions
180 /// that return multiple values.
Chris Lattner65938fc2008-08-23 23:36:38 +0000181 DenseMap<std::pair<Function*, unsigned>, LatticeVal> TrackedMultipleRetVals;
Jakub Staszak632a3552012-01-18 21:16:33 +0000182
Chris Lattner156b8c72009-11-03 23:40:48 +0000183 /// MRVFunctionsTracked - Each function in TrackedMultipleRetVals is
184 /// represented here for efficient lookup.
185 SmallPtrSet<Function*, 16> MRVFunctionsTracked;
Chris Lattnerb4394642004-12-10 08:02:06 +0000186
Chris Lattner2c427232009-11-03 20:52:57 +0000187 /// TrackingIncomingArguments - This is the set of functions for whose
188 /// arguments we make optimistic assumptions about and try to prove as
189 /// constants.
Chris Lattnercde8de52009-11-03 19:24:51 +0000190 SmallPtrSet<Function*, 16> TrackingIncomingArguments;
Jakub Staszak632a3552012-01-18 21:16:33 +0000191
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000192 /// The reason for two worklists is that overdefined is the lowest state
193 /// on the lattice, and moving things to overdefined as fast as possible
194 /// makes SCCP converge much faster.
195 ///
196 /// By having a separate worklist, we accomplish this because everything
197 /// possibly overdefined will become overdefined at the soonest possible
198 /// point.
Chris Lattner65938fc2008-08-23 23:36:38 +0000199 SmallVector<Value*, 64> OverdefinedInstWorkList;
200 SmallVector<Value*, 64> InstWorkList;
Chris Lattnerd79334d2004-07-15 23:36:43 +0000201
202
Chris Lattner65938fc2008-08-23 23:36:38 +0000203 SmallVector<BasicBlock*, 64> BBWorkList; // The BasicBlock work list
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000204
205 /// KnownFeasibleEdges - Entries in this set are edges which have already had
206 /// PHI nodes retriggered.
Chris Lattner65938fc2008-08-23 23:36:38 +0000207 typedef std::pair<BasicBlock*, BasicBlock*> Edge;
208 DenseSet<Edge> KnownFeasibleEdges;
Chris Lattner347389d2001-06-27 23:38:11 +0000209public:
Mehdi Amini46a43552015-03-04 18:43:29 +0000210 SCCPSolver(const DataLayout &DL, const TargetLibraryInfo *tli)
211 : DL(DL), TLI(tli) {}
Chris Lattner347389d2001-06-27 23:38:11 +0000212
Chris Lattner074be1f2004-11-15 04:44:20 +0000213 /// MarkBlockExecutable - This method can be used by clients to mark all of
214 /// the blocks that are known to be intrinsically live in the processed unit.
Chris Lattner809aee22009-11-02 06:11:23 +0000215 ///
216 /// This returns true if the block was not considered live before.
217 bool MarkBlockExecutable(BasicBlock *BB) {
David Blaikie70573dc2014-11-19 07:49:26 +0000218 if (!BBExecutable.insert(BB).second)
219 return false;
Nick Lewycky5cd95382013-06-26 00:30:18 +0000220 DEBUG(dbgs() << "Marking Block Executable: " << BB->getName() << '\n');
Chris Lattner074be1f2004-11-15 04:44:20 +0000221 BBWorkList.push_back(BB); // Add the block to the work list!
Chris Lattner809aee22009-11-02 06:11:23 +0000222 return true;
Chris Lattner7d325382002-04-29 21:26:08 +0000223 }
224
Chris Lattner91dbae62004-12-11 05:15:59 +0000225 /// TrackValueOfGlobalVariable - Clients can use this method to
Chris Lattnerb4394642004-12-10 08:02:06 +0000226 /// inform the SCCPSolver that it should track loads and stores to the
227 /// specified global variable if it can. This is only legal to call if
228 /// performing Interprocedural SCCP.
Chris Lattner91dbae62004-12-11 05:15:59 +0000229 void TrackValueOfGlobalVariable(GlobalVariable *GV) {
Chris Lattner156b8c72009-11-03 23:40:48 +0000230 // We only track the contents of scalar globals.
231 if (GV->getType()->getElementType()->isSingleValueType()) {
Chris Lattner91dbae62004-12-11 05:15:59 +0000232 LatticeVal &IV = TrackedGlobals[GV];
233 if (!isa<UndefValue>(GV->getInitializer()))
234 IV.markConstant(GV->getInitializer());
235 }
236 }
Chris Lattnerb4394642004-12-10 08:02:06 +0000237
238 /// AddTrackedFunction - If the SCCP solver is supposed to track calls into
239 /// and out of the specified function (which cannot have its address taken),
240 /// this method must be called.
241 void AddTrackedFunction(Function *F) {
Chris Lattnerb4394642004-12-10 08:02:06 +0000242 // Add an entry, F -> undef.
Chris Lattner229907c2011-07-18 04:54:35 +0000243 if (StructType *STy = dyn_cast<StructType>(F->getReturnType())) {
Chris Lattner156b8c72009-11-03 23:40:48 +0000244 MRVFunctionsTracked.insert(F);
Devang Patela7a20752008-03-11 05:46:42 +0000245 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Chris Lattner5a58a4d2008-04-23 05:38:20 +0000246 TrackedMultipleRetVals.insert(std::make_pair(std::make_pair(F, i),
247 LatticeVal()));
248 } else
249 TrackedRetVals.insert(std::make_pair(F, LatticeVal()));
Chris Lattnerb4394642004-12-10 08:02:06 +0000250 }
251
Chris Lattnercde8de52009-11-03 19:24:51 +0000252 void AddArgumentTrackedFunction(Function *F) {
253 TrackingIncomingArguments.insert(F);
254 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000255
Chris Lattner074be1f2004-11-15 04:44:20 +0000256 /// Solve - Solve for constants and executable blocks.
257 ///
258 void Solve();
Chris Lattner347389d2001-06-27 23:38:11 +0000259
Chris Lattner1847f6d2006-12-20 06:21:33 +0000260 /// ResolvedUndefsIn - While solving the dataflow for a function, we assume
Chris Lattner7285f432004-12-10 20:41:50 +0000261 /// that branches on undef values cannot reach any of their successors.
262 /// However, this is not a safe assumption. After we solve dataflow, this
263 /// method should be use to handle this. If this returns true, the solver
264 /// should be rerun.
Chris Lattner1847f6d2006-12-20 06:21:33 +0000265 bool ResolvedUndefsIn(Function &F);
Chris Lattner7285f432004-12-10 20:41:50 +0000266
Chris Lattneradd44f32008-08-23 23:39:31 +0000267 bool isBlockExecutable(BasicBlock *BB) const {
268 return BBExecutable.count(BB);
Chris Lattner074be1f2004-11-15 04:44:20 +0000269 }
270
Chris Lattnerb5a13d42009-11-02 02:54:24 +0000271 LatticeVal getLatticeValueFor(Value *V) const {
Chris Lattnerf5484032009-11-02 05:55:40 +0000272 DenseMap<Value*, LatticeVal>::const_iterator I = ValueState.find(V);
Chris Lattnerb5a13d42009-11-02 02:54:24 +0000273 assert(I != ValueState.end() && "V is not in valuemap!");
274 return I->second;
Chris Lattner074be1f2004-11-15 04:44:20 +0000275 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000276
Devang Patela7a20752008-03-11 05:46:42 +0000277 /// getTrackedRetVals - Get the inferred return value map.
Chris Lattner99e12952004-12-11 02:53:57 +0000278 ///
Devang Patela7a20752008-03-11 05:46:42 +0000279 const DenseMap<Function*, LatticeVal> &getTrackedRetVals() {
280 return TrackedRetVals;
Chris Lattner99e12952004-12-11 02:53:57 +0000281 }
282
Chris Lattner91dbae62004-12-11 05:15:59 +0000283 /// getTrackedGlobals - Get and return the set of inferred initializers for
284 /// global variables.
Chris Lattner067d6072007-02-02 20:38:30 +0000285 const DenseMap<GlobalVariable*, LatticeVal> &getTrackedGlobals() {
Chris Lattner91dbae62004-12-11 05:15:59 +0000286 return TrackedGlobals;
287 }
288
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000289 void markOverdefined(Value *V) {
Duncan Sands19d0b472010-02-16 11:11:14 +0000290 assert(!V->getType()->isStructTy() && "Should use other method");
Chris Lattnerc33fd462007-03-04 04:50:21 +0000291 markOverdefined(ValueState[V], V);
292 }
Chris Lattner99e12952004-12-11 02:53:57 +0000293
Chris Lattner156b8c72009-11-03 23:40:48 +0000294 /// markAnythingOverdefined - Mark the specified value overdefined. This
295 /// works with both scalars and structs.
296 void markAnythingOverdefined(Value *V) {
Chris Lattner229907c2011-07-18 04:54:35 +0000297 if (StructType *STy = dyn_cast<StructType>(V->getType()))
Chris Lattner156b8c72009-11-03 23:40:48 +0000298 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
299 markOverdefined(getStructValueState(V, i), V);
300 else
301 markOverdefined(V);
302 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000303
Chris Lattner347389d2001-06-27 23:38:11 +0000304private:
Chris Lattnerd79334d2004-07-15 23:36:43 +0000305 // markConstant - Make a value be marked as "constant". If the value
Misha Brukmanb1c93172005-04-21 23:48:37 +0000306 // is not already a constant, add it to the instruction work list so that
Chris Lattner347389d2001-06-27 23:38:11 +0000307 // the users of the instruction are updated later.
308 //
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000309 void markConstant(LatticeVal &IV, Value *V, Constant *C) {
310 if (!IV.markConstant(C)) return;
David Greene389fc3b2010-01-05 01:27:15 +0000311 DEBUG(dbgs() << "markConstant: " << *C << ": " << *V << '\n');
Chris Lattnerc6c153b2010-04-09 01:14:31 +0000312 if (IV.isOverdefined())
313 OverdefinedInstWorkList.push_back(V);
314 else
315 InstWorkList.push_back(V);
Chris Lattner7324f7c2003-10-08 16:21:03 +0000316 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000317
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000318 void markConstant(Value *V, Constant *C) {
Duncan Sands19d0b472010-02-16 11:11:14 +0000319 assert(!V->getType()->isStructTy() && "Should use other method");
Chris Lattnerb4394642004-12-10 08:02:06 +0000320 markConstant(ValueState[V], V, C);
Chris Lattner347389d2001-06-27 23:38:11 +0000321 }
322
Chris Lattnerf5484032009-11-02 05:55:40 +0000323 void markForcedConstant(Value *V, Constant *C) {
Duncan Sands19d0b472010-02-16 11:11:14 +0000324 assert(!V->getType()->isStructTy() && "Should use other method");
Chris Lattnerc6c153b2010-04-09 01:14:31 +0000325 LatticeVal &IV = ValueState[V];
326 IV.markForcedConstant(C);
David Greene389fc3b2010-01-05 01:27:15 +0000327 DEBUG(dbgs() << "markForcedConstant: " << *C << ": " << *V << '\n');
Chris Lattnerc6c153b2010-04-09 01:14:31 +0000328 if (IV.isOverdefined())
329 OverdefinedInstWorkList.push_back(V);
330 else
331 InstWorkList.push_back(V);
Chris Lattnerf5484032009-11-02 05:55:40 +0000332 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000333
334
Chris Lattnerd79334d2004-07-15 23:36:43 +0000335 // markOverdefined - Make a value be marked as "overdefined". If the
Misha Brukmanb1c93172005-04-21 23:48:37 +0000336 // value is not already overdefined, add it to the overdefined instruction
Chris Lattnerd79334d2004-07-15 23:36:43 +0000337 // work list so that the users of the instruction are updated later.
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000338 void markOverdefined(LatticeVal &IV, Value *V) {
339 if (!IV.markOverdefined()) return;
Jakub Staszak632a3552012-01-18 21:16:33 +0000340
David Greene389fc3b2010-01-05 01:27:15 +0000341 DEBUG(dbgs() << "markOverdefined: ";
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000342 if (Function *F = dyn_cast<Function>(V))
David Greene389fc3b2010-01-05 01:27:15 +0000343 dbgs() << "Function '" << F->getName() << "'\n";
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000344 else
David Greene389fc3b2010-01-05 01:27:15 +0000345 dbgs() << *V << '\n');
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000346 // Only instructions go on the work list
347 OverdefinedInstWorkList.push_back(V);
Chris Lattner7324f7c2003-10-08 16:21:03 +0000348 }
Chris Lattnerb4394642004-12-10 08:02:06 +0000349
Chris Lattnerf5484032009-11-02 05:55:40 +0000350 void mergeInValue(LatticeVal &IV, Value *V, LatticeVal MergeWithV) {
Chris Lattnerb4394642004-12-10 08:02:06 +0000351 if (IV.isOverdefined() || MergeWithV.isUndefined())
352 return; // Noop.
353 if (MergeWithV.isOverdefined())
354 markOverdefined(IV, V);
355 else if (IV.isUndefined())
356 markConstant(IV, V, MergeWithV.getConstant());
357 else if (IV.getConstant() != MergeWithV.getConstant())
358 markOverdefined(IV, V);
Chris Lattner347389d2001-06-27 23:38:11 +0000359 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000360
Chris Lattnerf5484032009-11-02 05:55:40 +0000361 void mergeInValue(Value *V, LatticeVal MergeWithV) {
Duncan Sands19d0b472010-02-16 11:11:14 +0000362 assert(!V->getType()->isStructTy() && "Should use other method");
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000363 mergeInValue(ValueState[V], V, MergeWithV);
Chris Lattner06a0ed12006-02-08 02:38:11 +0000364 }
365
Chris Lattner347389d2001-06-27 23:38:11 +0000366
Chris Lattnerf5484032009-11-02 05:55:40 +0000367 /// getValueState - Return the LatticeVal object that corresponds to the
368 /// value. This function handles the case when the value hasn't been seen yet
369 /// by properly seeding constants etc.
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000370 LatticeVal &getValueState(Value *V) {
Duncan Sands19d0b472010-02-16 11:11:14 +0000371 assert(!V->getType()->isStructTy() && "Should use getStructValueState");
Chris Lattner646354b2004-10-16 18:09:41 +0000372
Benjamin Kramer3fcbb822009-11-05 14:33:27 +0000373 std::pair<DenseMap<Value*, LatticeVal>::iterator, bool> I =
374 ValueState.insert(std::make_pair(V, LatticeVal()));
375 LatticeVal &LV = I.first->second;
376
377 if (!I.second)
378 return LV; // Common case, already in the map.
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000379
Chris Lattner1847f6d2006-12-20 06:21:33 +0000380 if (Constant *C = dyn_cast<Constant>(V)) {
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000381 // Undef values remain undefined.
382 if (!isa<UndefValue>(V))
Chris Lattner067d6072007-02-02 20:38:30 +0000383 LV.markConstant(C); // Constants are constant
Chris Lattnerdd6522e2002-08-30 23:39:00 +0000384 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000385
Chris Lattnera3c39d32009-11-02 02:33:50 +0000386 // All others are underdefined by default.
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000387 return LV;
Chris Lattner347389d2001-06-27 23:38:11 +0000388 }
389
Chris Lattner156b8c72009-11-03 23:40:48 +0000390 /// getStructValueState - Return the LatticeVal object that corresponds to the
391 /// value/field pair. This function handles the case when the value hasn't
392 /// been seen yet by properly seeding constants etc.
393 LatticeVal &getStructValueState(Value *V, unsigned i) {
Duncan Sands19d0b472010-02-16 11:11:14 +0000394 assert(V->getType()->isStructTy() && "Should use getValueState");
Chris Lattner156b8c72009-11-03 23:40:48 +0000395 assert(i < cast<StructType>(V->getType())->getNumElements() &&
396 "Invalid element #");
Benjamin Kramer3fcbb822009-11-05 14:33:27 +0000397
398 std::pair<DenseMap<std::pair<Value*, unsigned>, LatticeVal>::iterator,
399 bool> I = StructValueState.insert(
400 std::make_pair(std::make_pair(V, i), LatticeVal()));
401 LatticeVal &LV = I.first->second;
402
403 if (!I.second)
404 return LV; // Common case, already in the map.
405
Chris Lattner156b8c72009-11-03 23:40:48 +0000406 if (Constant *C = dyn_cast<Constant>(V)) {
Chris Lattnerfa775002012-01-26 02:32:04 +0000407 Constant *Elt = C->getAggregateElement(i);
Nadav Rotem465834c2012-07-24 10:51:42 +0000408
Craig Topperf40110f2014-04-25 05:29:35 +0000409 if (!Elt)
Chris Lattner156b8c72009-11-03 23:40:48 +0000410 LV.markOverdefined(); // Unknown sort of constant.
Chris Lattnerfa775002012-01-26 02:32:04 +0000411 else if (isa<UndefValue>(Elt))
412 ; // Undef values remain undefined.
413 else
414 LV.markConstant(Elt); // Constants are constant.
Chris Lattner156b8c72009-11-03 23:40:48 +0000415 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000416
Chris Lattner156b8c72009-11-03 23:40:48 +0000417 // All others are underdefined by default.
418 return LV;
419 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000420
Chris Lattner156b8c72009-11-03 23:40:48 +0000421
Chris Lattnerf5484032009-11-02 05:55:40 +0000422 /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
423 /// work list if it is not already executable.
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000424 void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
425 if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
426 return; // This edge is already known to be executable!
427
Chris Lattner809aee22009-11-02 06:11:23 +0000428 if (!MarkBlockExecutable(Dest)) {
429 // If the destination is already executable, we just made an *edge*
430 // feasible that wasn't before. Revisit the PHI nodes in the block
431 // because they have potentially new operands.
David Greene389fc3b2010-01-05 01:27:15 +0000432 DEBUG(dbgs() << "Marking Edge Executable: " << Source->getName()
Nick Lewycky5cd95382013-06-26 00:30:18 +0000433 << " -> " << Dest->getName() << '\n');
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000434
Chris Lattner809aee22009-11-02 06:11:23 +0000435 PHINode *PN;
436 for (BasicBlock::iterator I = Dest->begin();
437 (PN = dyn_cast<PHINode>(I)); ++I)
438 visitPHINode(*PN);
Chris Lattnercccc5c72003-04-25 02:50:03 +0000439 }
Chris Lattner347389d2001-06-27 23:38:11 +0000440 }
441
Chris Lattner074be1f2004-11-15 04:44:20 +0000442 // getFeasibleSuccessors - Return a vector of booleans to indicate which
443 // successors are reachable from a given terminator instruction.
444 //
Craig Topperb94011f2013-07-14 04:42:23 +0000445 void getFeasibleSuccessors(TerminatorInst &TI, SmallVectorImpl<bool> &Succs);
Chris Lattner074be1f2004-11-15 04:44:20 +0000446
447 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
Chris Lattnera3c39d32009-11-02 02:33:50 +0000448 // block to the 'To' basic block is currently feasible.
Chris Lattner074be1f2004-11-15 04:44:20 +0000449 //
450 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To);
451
452 // OperandChangedState - This method is invoked on all of the users of an
Chris Lattnera3c39d32009-11-02 02:33:50 +0000453 // instruction that was just changed state somehow. Based on this
Chris Lattner074be1f2004-11-15 04:44:20 +0000454 // information, we need to update the specified user of this instruction.
455 //
Chris Lattnerfb141812009-11-03 03:42:51 +0000456 void OperandChangedState(Instruction *I) {
457 if (BBExecutable.count(I->getParent())) // Inst is executable?
458 visit(*I);
Chris Lattner074be1f2004-11-15 04:44:20 +0000459 }
Dale Johannesend3a58c82010-11-30 20:23:21 +0000460
Chris Lattner074be1f2004-11-15 04:44:20 +0000461private:
462 friend class InstVisitor<SCCPSolver>;
Chris Lattner347389d2001-06-27 23:38:11 +0000463
Chris Lattnera3c39d32009-11-02 02:33:50 +0000464 // visit implementations - Something changed in this instruction. Either an
Chris Lattner10b250e2001-06-29 23:56:23 +0000465 // operand made a transition, or the instruction is newly executable. Change
466 // the value type of I to reflect these changes if appropriate.
Chris Lattner113f4f42002-06-25 16:13:24 +0000467 void visitPHINode(PHINode &I);
Chris Lattner6e560792002-04-18 15:13:15 +0000468
469 // Terminators
Chris Lattnerb4394642004-12-10 08:02:06 +0000470 void visitReturnInst(ReturnInst &I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000471 void visitTerminatorInst(TerminatorInst &TI);
Chris Lattner6e560792002-04-18 15:13:15 +0000472
Chris Lattner6e1a1b12002-08-14 17:53:45 +0000473 void visitCastInst(CastInst &I);
Chris Lattner59db22d2004-03-12 05:52:44 +0000474 void visitSelectInst(SelectInst &I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000475 void visitBinaryOperator(Instruction &I);
Reid Spencer266e42b2006-12-23 06:05:41 +0000476 void visitCmpInst(CmpInst &I);
Robert Bocchinobd518d12006-01-10 19:05:05 +0000477 void visitExtractElementInst(ExtractElementInst &I);
Robert Bocchino6dce2502006-01-17 20:06:55 +0000478 void visitInsertElementInst(InsertElementInst &I);
Chris Lattner17bd6052006-04-08 01:19:12 +0000479 void visitShuffleVectorInst(ShuffleVectorInst &I);
Dan Gohman041f9d02008-06-20 01:15:44 +0000480 void visitExtractValueInst(ExtractValueInst &EVI);
481 void visitInsertValueInst(InsertValueInst &IVI);
Bill Wendlingfae14752011-08-12 20:24:12 +0000482 void visitLandingPadInst(LandingPadInst &I) { markAnythingOverdefined(&I); }
David Majnemereb518bd2015-08-04 08:21:40 +0000483 void visitCleanupPadInst(CleanupPadInst &CPI) { markAnythingOverdefined(&CPI); }
484 void visitCatchPadInst(CatchPadInst &CPI) {
485 markAnythingOverdefined(&CPI);
486 visitTerminatorInst(CPI);
487 }
Chris Lattner6e560792002-04-18 15:13:15 +0000488
Chris Lattnera3c39d32009-11-02 02:33:50 +0000489 // Instructions that cannot be folded away.
Chris Lattnerf5484032009-11-02 05:55:40 +0000490 void visitStoreInst (StoreInst &I);
Chris Lattner49f74522004-01-12 04:29:41 +0000491 void visitLoadInst (LoadInst &I);
Chris Lattnerdd6522e2002-08-30 23:39:00 +0000492 void visitGetElementPtrInst(GetElementPtrInst &I);
Victor Hernandeze2971492009-10-24 04:23:03 +0000493 void visitCallInst (CallInst &I) {
Gabor Greif62f0aac2010-07-28 22:50:26 +0000494 visitCallSite(&I);
Victor Hernandez5d034492009-09-18 22:35:49 +0000495 }
Chris Lattnerb4394642004-12-10 08:02:06 +0000496 void visitInvokeInst (InvokeInst &II) {
Gabor Greif62f0aac2010-07-28 22:50:26 +0000497 visitCallSite(&II);
Chris Lattnerb4394642004-12-10 08:02:06 +0000498 visitTerminatorInst(II);
Chris Lattnerdf741d62003-08-27 01:08:35 +0000499 }
Chris Lattnerb4394642004-12-10 08:02:06 +0000500 void visitCallSite (CallSite CS);
Bill Wendlingf891bf82011-07-31 06:30:59 +0000501 void visitResumeInst (TerminatorInst &I) { /*returns void*/ }
Chris Lattner646354b2004-10-16 18:09:41 +0000502 void visitUnreachableInst(TerminatorInst &I) { /*returns void*/ }
Eli Friedman89b694b2011-07-27 01:08:30 +0000503 void visitFenceInst (FenceInst &I) { /*returns void*/ }
Tim Northover6bf04e42014-06-13 14:54:09 +0000504 void visitAtomicCmpXchgInst(AtomicCmpXchgInst &I) {
505 markAnythingOverdefined(&I);
506 }
Eli Friedman366bcce2011-08-02 21:35:16 +0000507 void visitAtomicRMWInst (AtomicRMWInst &I) { markOverdefined(&I); }
Victor Hernandez8acf2952009-10-23 21:09:37 +0000508 void visitAllocaInst (Instruction &I) { markOverdefined(&I); }
Chris Lattner156b8c72009-11-03 23:40:48 +0000509 void visitVAArgInst (Instruction &I) { markAnythingOverdefined(&I); }
Chris Lattner6e560792002-04-18 15:13:15 +0000510
Chris Lattner113f4f42002-06-25 16:13:24 +0000511 void visitInstruction(Instruction &I) {
Chris Lattnera3c39d32009-11-02 02:33:50 +0000512 // If a new instruction is added to LLVM that we don't handle.
Nick Lewycky5cd95382013-06-26 00:30:18 +0000513 dbgs() << "SCCP: Don't know how to handle: " << I << '\n';
Chris Lattner156b8c72009-11-03 23:40:48 +0000514 markAnythingOverdefined(&I); // Just in case
Chris Lattner6e560792002-04-18 15:13:15 +0000515 }
Chris Lattner10b250e2001-06-29 23:56:23 +0000516};
Chris Lattnerb28b6802002-07-23 18:06:35 +0000517
Duncan Sands2be91fc2007-07-20 08:56:21 +0000518} // end anonymous namespace
519
520
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000521// getFeasibleSuccessors - Return a vector of booleans to indicate which
522// successors are reachable from a given terminator instruction.
523//
Chris Lattner074be1f2004-11-15 04:44:20 +0000524void SCCPSolver::getFeasibleSuccessors(TerminatorInst &TI,
Craig Topperb94011f2013-07-14 04:42:23 +0000525 SmallVectorImpl<bool> &Succs) {
Chris Lattnercccc5c72003-04-25 02:50:03 +0000526 Succs.resize(TI.getNumSuccessors());
Chris Lattner113f4f42002-06-25 16:13:24 +0000527 if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000528 if (BI->isUnconditional()) {
529 Succs[0] = true;
Chris Lattner6df5cec2009-11-02 02:30:06 +0000530 return;
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000531 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000532
Chris Lattnerf5484032009-11-02 05:55:40 +0000533 LatticeVal BCValue = getValueState(BI->getCondition());
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000534 ConstantInt *CI = BCValue.getConstantInt();
Craig Topperf40110f2014-04-25 05:29:35 +0000535 if (!CI) {
Chris Lattner6df5cec2009-11-02 02:30:06 +0000536 // Overdefined condition variables, and branches on unfoldable constant
537 // conditions, mean the branch could go either way.
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000538 if (!BCValue.isUndefined())
539 Succs[0] = Succs[1] = true;
Chris Lattner6df5cec2009-11-02 02:30:06 +0000540 return;
541 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000542
Chris Lattner6df5cec2009-11-02 02:30:06 +0000543 // Constant condition variables mean the branch can only go a single way.
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000544 Succs[CI->isZero()] = true;
Chris Lattneree8b9512009-10-29 01:21:20 +0000545 return;
546 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000547
David Majnemer654e1302015-07-31 17:58:14 +0000548 // Unwinding instructions successors are always executable.
549 if (TI.isExceptional()) {
550 Succs.assign(TI.getNumSuccessors(), true);
Chris Lattneree8b9512009-10-29 01:21:20 +0000551 return;
552 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000553
Chris Lattneree8b9512009-10-29 01:21:20 +0000554 if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) {
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +0000555 if (!SI->getNumCases()) {
Eli Friedman56f2f212011-08-16 21:12:35 +0000556 Succs[0] = true;
557 return;
558 }
Chris Lattnerf5484032009-11-02 05:55:40 +0000559 LatticeVal SCValue = getValueState(SI->getCondition());
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000560 ConstantInt *CI = SCValue.getConstantInt();
Jakub Staszak632a3552012-01-18 21:16:33 +0000561
Craig Topperf40110f2014-04-25 05:29:35 +0000562 if (!CI) { // Overdefined or undefined condition?
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000563 // All destinations are executable!
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000564 if (!SCValue.isUndefined())
565 Succs.assign(TI.getNumSuccessors(), true);
566 return;
567 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000568
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +0000569 Succs[SI->findCaseValue(CI).getSuccessorIndex()] = true;
Chris Lattneree8b9512009-10-29 01:21:20 +0000570 return;
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000571 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000572
Chris Lattneree8b9512009-10-29 01:21:20 +0000573 // TODO: This could be improved if the operand is a [cast of a] BlockAddress.
574 if (isa<IndirectBrInst>(&TI)) {
575 // Just mark all destinations executable!
576 Succs.assign(TI.getNumSuccessors(), true);
577 return;
578 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000579
Chris Lattneree8b9512009-10-29 01:21:20 +0000580#ifndef NDEBUG
David Greene389fc3b2010-01-05 01:27:15 +0000581 dbgs() << "Unknown terminator instruction: " << TI << '\n';
Chris Lattneree8b9512009-10-29 01:21:20 +0000582#endif
583 llvm_unreachable("SCCP: Don't know how to handle this terminator!");
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000584}
585
586
Chris Lattner13b52e72002-05-02 21:18:01 +0000587// isEdgeFeasible - Return true if the control flow edge from the 'From' basic
Chris Lattnera3c39d32009-11-02 02:33:50 +0000588// block to the 'To' basic block is currently feasible.
Chris Lattner13b52e72002-05-02 21:18:01 +0000589//
Chris Lattner074be1f2004-11-15 04:44:20 +0000590bool SCCPSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
Chris Lattner13b52e72002-05-02 21:18:01 +0000591 assert(BBExecutable.count(To) && "Dest should always be alive!");
592
593 // Make sure the source basic block is executable!!
594 if (!BBExecutable.count(From)) return false;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000595
Chris Lattnera3c39d32009-11-02 02:33:50 +0000596 // Check to make sure this edge itself is actually feasible now.
Chris Lattner71ac22ff2003-10-08 15:47:41 +0000597 TerminatorInst *TI = From->getTerminator();
598 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
599 if (BI->isUnconditional())
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000600 return true;
Jakub Staszak632a3552012-01-18 21:16:33 +0000601
Chris Lattnerf5484032009-11-02 05:55:40 +0000602 LatticeVal BCValue = getValueState(BI->getCondition());
Chris Lattnerfe992d42004-01-12 17:40:36 +0000603
Chris Lattner6df5cec2009-11-02 02:30:06 +0000604 // Overdefined condition variables mean the branch could go either way,
605 // undef conditions mean that neither edge is feasible yet.
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000606 ConstantInt *CI = BCValue.getConstantInt();
Craig Topperf40110f2014-04-25 05:29:35 +0000607 if (!CI)
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000608 return !BCValue.isUndefined();
Jakub Staszak632a3552012-01-18 21:16:33 +0000609
Chris Lattner6df5cec2009-11-02 02:30:06 +0000610 // Constant condition variables mean the branch can only go a single way.
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000611 return BI->getSuccessor(CI->isZero()) == To;
Chris Lattneree8b9512009-10-29 01:21:20 +0000612 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000613
David Majnemer654e1302015-07-31 17:58:14 +0000614 // Unwinding instructions successors are always executable.
615 if (TI->isExceptional())
Chris Lattner71ac22ff2003-10-08 15:47:41 +0000616 return true;
Jakub Staszak632a3552012-01-18 21:16:33 +0000617
Chris Lattneree8b9512009-10-29 01:21:20 +0000618 if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +0000619 if (SI->getNumCases() < 1)
Eli Friedman56f2f212011-08-16 21:12:35 +0000620 return true;
621
Chris Lattnerf5484032009-11-02 05:55:40 +0000622 LatticeVal SCValue = getValueState(SI->getCondition());
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000623 ConstantInt *CI = SCValue.getConstantInt();
Jakub Staszak632a3552012-01-18 21:16:33 +0000624
Craig Topperf40110f2014-04-25 05:29:35 +0000625 if (!CI)
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000626 return !SCValue.isUndefined();
Chris Lattnerfe992d42004-01-12 17:40:36 +0000627
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +0000628 return SI->findCaseValue(CI).getCaseSuccessor() == To;
Chris Lattner71ac22ff2003-10-08 15:47:41 +0000629 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000630
Chris Lattneree8b9512009-10-29 01:21:20 +0000631 // Just mark all destinations executable!
632 // TODO: This could be improved if the operand is a [cast of a] BlockAddress.
Eli Friedman3de2ddc2011-05-21 19:13:10 +0000633 if (isa<IndirectBrInst>(TI))
Chris Lattneree8b9512009-10-29 01:21:20 +0000634 return true;
Jakub Staszak632a3552012-01-18 21:16:33 +0000635
Chris Lattneree8b9512009-10-29 01:21:20 +0000636#ifndef NDEBUG
David Greene389fc3b2010-01-05 01:27:15 +0000637 dbgs() << "Unknown terminator instruction: " << *TI << '\n';
Chris Lattneree8b9512009-10-29 01:21:20 +0000638#endif
Craig Toppere73658d2014-04-28 04:05:08 +0000639 llvm_unreachable(nullptr);
Chris Lattner13b52e72002-05-02 21:18:01 +0000640}
Chris Lattner347389d2001-06-27 23:38:11 +0000641
Chris Lattnera3c39d32009-11-02 02:33:50 +0000642// visit Implementations - Something changed in this instruction, either an
Chris Lattner347389d2001-06-27 23:38:11 +0000643// operand made a transition, or the instruction is newly executable. Change
644// the value type of I to reflect these changes if appropriate. This method
645// makes sure to do the following actions:
646//
647// 1. If a phi node merges two constants in, and has conflicting value coming
648// from different branches, or if the PHI node merges in an overdefined
649// value, then the PHI node becomes overdefined.
650// 2. If a phi node merges only constants in, and they all agree on value, the
651// PHI node becomes a constant value equal to that.
652// 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
653// 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
654// 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
655// 6. If a conditional branch has a value that is constant, make the selected
656// destination executable
657// 7. If a conditional branch has a value that is overdefined, make all
658// successors executable.
659//
Chris Lattner074be1f2004-11-15 04:44:20 +0000660void SCCPSolver::visitPHINode(PHINode &PN) {
Chris Lattner156b8c72009-11-03 23:40:48 +0000661 // If this PN returns a struct, just mark the result overdefined.
662 // TODO: We could do a lot better than this if code actually uses this.
Duncan Sands19d0b472010-02-16 11:11:14 +0000663 if (PN.getType()->isStructTy())
Chris Lattner156b8c72009-11-03 23:40:48 +0000664 return markAnythingOverdefined(&PN);
Jakub Staszak632a3552012-01-18 21:16:33 +0000665
Eli Friedman0a309292011-11-11 01:16:15 +0000666 if (getValueState(&PN).isOverdefined())
Chris Lattner05fe6842004-01-12 03:57:30 +0000667 return; // Quick exit
Chris Lattner347389d2001-06-27 23:38:11 +0000668
Chris Lattner7a7b1142004-03-16 19:49:59 +0000669 // Super-extra-high-degree PHI nodes are unlikely to ever be marked constant,
670 // and slow us down a lot. Just mark them overdefined.
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000671 if (PN.getNumIncomingValues() > 64)
Chris Lattnerf5484032009-11-02 05:55:40 +0000672 return markOverdefined(&PN);
Jakub Staszak632a3552012-01-18 21:16:33 +0000673
Chris Lattner6e560792002-04-18 15:13:15 +0000674 // Look at all of the executable operands of the PHI node. If any of them
675 // are overdefined, the PHI becomes overdefined as well. If they are all
676 // constant, and they agree with each other, the PHI becomes the identical
677 // constant. If they are constant and don't agree, the PHI is overdefined.
678 // If there are no executable operands, the PHI remains undefined.
679 //
Craig Topperf40110f2014-04-25 05:29:35 +0000680 Constant *OperandVal = nullptr;
Chris Lattnercccc5c72003-04-25 02:50:03 +0000681 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
Chris Lattnerf5484032009-11-02 05:55:40 +0000682 LatticeVal IV = getValueState(PN.getIncomingValue(i));
Chris Lattnercccc5c72003-04-25 02:50:03 +0000683 if (IV.isUndefined()) continue; // Doesn't influence PHI node.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000684
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000685 if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent()))
686 continue;
Jakub Staszak632a3552012-01-18 21:16:33 +0000687
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000688 if (IV.isOverdefined()) // PHI node becomes overdefined!
689 return markOverdefined(&PN);
Chris Lattner7e270582003-06-24 20:29:52 +0000690
Craig Topperf40110f2014-04-25 05:29:35 +0000691 if (!OperandVal) { // Grab the first value.
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000692 OperandVal = IV.getConstant();
693 continue;
Chris Lattner347389d2001-06-27 23:38:11 +0000694 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000695
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000696 // There is already a reachable operand. If we conflict with it,
697 // then the PHI node becomes overdefined. If we agree with it, we
698 // can continue on.
Jakub Staszak632a3552012-01-18 21:16:33 +0000699
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000700 // Check to see if there are two different constants merging, if so, the PHI
701 // node is overdefined.
702 if (IV.getConstant() != OperandVal)
703 return markOverdefined(&PN);
Chris Lattner347389d2001-06-27 23:38:11 +0000704 }
705
Chris Lattner6e560792002-04-18 15:13:15 +0000706 // If we exited the loop, this means that the PHI node only has constant
Chris Lattnercccc5c72003-04-25 02:50:03 +0000707 // arguments that agree with each other(and OperandVal is the constant) or
708 // OperandVal is null because there are no defined incoming arguments. If
709 // this is the case, the PHI remains undefined.
Chris Lattner347389d2001-06-27 23:38:11 +0000710 //
Chris Lattnercccc5c72003-04-25 02:50:03 +0000711 if (OperandVal)
Chris Lattner65938fc2008-08-23 23:36:38 +0000712 markConstant(&PN, OperandVal); // Acquire operand value
Chris Lattner347389d2001-06-27 23:38:11 +0000713}
714
Chris Lattnerb4394642004-12-10 08:02:06 +0000715void SCCPSolver::visitReturnInst(ReturnInst &I) {
Chris Lattnerf5484032009-11-02 05:55:40 +0000716 if (I.getNumOperands() == 0) return; // ret void
Chris Lattnerb4394642004-12-10 08:02:06 +0000717
Chris Lattnerb4394642004-12-10 08:02:06 +0000718 Function *F = I.getParent()->getParent();
Chris Lattner156b8c72009-11-03 23:40:48 +0000719 Value *ResultOp = I.getOperand(0);
Jakub Staszak632a3552012-01-18 21:16:33 +0000720
Devang Patela7a20752008-03-11 05:46:42 +0000721 // If we are tracking the return value of this function, merge it in.
Duncan Sands19d0b472010-02-16 11:11:14 +0000722 if (!TrackedRetVals.empty() && !ResultOp->getType()->isStructTy()) {
Chris Lattner067d6072007-02-02 20:38:30 +0000723 DenseMap<Function*, LatticeVal>::iterator TFRVI =
Devang Patela7a20752008-03-11 05:46:42 +0000724 TrackedRetVals.find(F);
Chris Lattnerfb141812009-11-03 03:42:51 +0000725 if (TFRVI != TrackedRetVals.end()) {
Chris Lattner156b8c72009-11-03 23:40:48 +0000726 mergeInValue(TFRVI->second, F, getValueState(ResultOp));
Devang Patela7a20752008-03-11 05:46:42 +0000727 return;
728 }
729 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000730
Chris Lattner5a58a4d2008-04-23 05:38:20 +0000731 // Handle functions that return multiple values.
Chris Lattner156b8c72009-11-03 23:40:48 +0000732 if (!TrackedMultipleRetVals.empty()) {
Chris Lattner229907c2011-07-18 04:54:35 +0000733 if (StructType *STy = dyn_cast<StructType>(ResultOp->getType()))
Chris Lattner156b8c72009-11-03 23:40:48 +0000734 if (MRVFunctionsTracked.count(F))
735 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
736 mergeInValue(TrackedMultipleRetVals[std::make_pair(F, i)], F,
737 getStructValueState(ResultOp, i));
Jakub Staszak632a3552012-01-18 21:16:33 +0000738
Chris Lattnerb4394642004-12-10 08:02:06 +0000739 }
740}
741
Chris Lattner074be1f2004-11-15 04:44:20 +0000742void SCCPSolver::visitTerminatorInst(TerminatorInst &TI) {
Chris Lattner37d400a2007-02-02 21:15:06 +0000743 SmallVector<bool, 16> SuccFeasible;
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000744 getFeasibleSuccessors(TI, SuccFeasible);
Chris Lattner347389d2001-06-27 23:38:11 +0000745
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000746 BasicBlock *BB = TI.getParent();
747
Chris Lattnera3c39d32009-11-02 02:33:50 +0000748 // Mark all feasible successors executable.
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000749 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000750 if (SuccFeasible[i])
751 markEdgeExecutable(BB, TI.getSuccessor(i));
Chris Lattner6e560792002-04-18 15:13:15 +0000752}
753
Chris Lattner074be1f2004-11-15 04:44:20 +0000754void SCCPSolver::visitCastInst(CastInst &I) {
Chris Lattnerf5484032009-11-02 05:55:40 +0000755 LatticeVal OpSt = getValueState(I.getOperand(0));
756 if (OpSt.isOverdefined()) // Inherit overdefinedness of operand
Chris Lattner113f4f42002-06-25 16:13:24 +0000757 markOverdefined(&I);
Chris Lattnerf5484032009-11-02 05:55:40 +0000758 else if (OpSt.isConstant()) // Propagate constant value
Jakub Staszak632a3552012-01-18 21:16:33 +0000759 markConstant(&I, ConstantExpr::getCast(I.getOpcode(),
Chris Lattnerf5484032009-11-02 05:55:40 +0000760 OpSt.getConstant(), I.getType()));
Chris Lattner6e560792002-04-18 15:13:15 +0000761}
762
Chris Lattner156b8c72009-11-03 23:40:48 +0000763
Dan Gohman041f9d02008-06-20 01:15:44 +0000764void SCCPSolver::visitExtractValueInst(ExtractValueInst &EVI) {
Chris Lattner156b8c72009-11-03 23:40:48 +0000765 // If this returns a struct, mark all elements over defined, we don't track
766 // structs in structs.
Duncan Sands19d0b472010-02-16 11:11:14 +0000767 if (EVI.getType()->isStructTy())
Chris Lattner156b8c72009-11-03 23:40:48 +0000768 return markAnythingOverdefined(&EVI);
Jakub Staszak632a3552012-01-18 21:16:33 +0000769
Chris Lattner156b8c72009-11-03 23:40:48 +0000770 // If this is extracting from more than one level of struct, we don't know.
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000771 if (EVI.getNumIndices() != 1)
772 return markOverdefined(&EVI);
Dan Gohman041f9d02008-06-20 01:15:44 +0000773
Chris Lattner156b8c72009-11-03 23:40:48 +0000774 Value *AggVal = EVI.getAggregateOperand();
Duncan Sands19d0b472010-02-16 11:11:14 +0000775 if (AggVal->getType()->isStructTy()) {
Chris Lattner02e2cee2009-11-10 22:02:09 +0000776 unsigned i = *EVI.idx_begin();
777 LatticeVal EltVal = getStructValueState(AggVal, i);
778 mergeInValue(getValueState(&EVI), &EVI, EltVal);
779 } else {
780 // Otherwise, must be extracting from an array.
781 return markOverdefined(&EVI);
782 }
Dan Gohman041f9d02008-06-20 01:15:44 +0000783}
784
785void SCCPSolver::visitInsertValueInst(InsertValueInst &IVI) {
Chris Lattner229907c2011-07-18 04:54:35 +0000786 StructType *STy = dyn_cast<StructType>(IVI.getType());
Craig Topperf40110f2014-04-25 05:29:35 +0000787 if (!STy)
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000788 return markOverdefined(&IVI);
Jakub Staszak632a3552012-01-18 21:16:33 +0000789
Chris Lattner156b8c72009-11-03 23:40:48 +0000790 // If this has more than one index, we can't handle it, drive all results to
791 // undef.
792 if (IVI.getNumIndices() != 1)
793 return markAnythingOverdefined(&IVI);
Jakub Staszak632a3552012-01-18 21:16:33 +0000794
Chris Lattner156b8c72009-11-03 23:40:48 +0000795 Value *Aggr = IVI.getAggregateOperand();
796 unsigned Idx = *IVI.idx_begin();
Jakub Staszak632a3552012-01-18 21:16:33 +0000797
Chris Lattner156b8c72009-11-03 23:40:48 +0000798 // Compute the result based on what we're inserting.
799 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
800 // This passes through all values that aren't the inserted element.
801 if (i != Idx) {
802 LatticeVal EltVal = getStructValueState(Aggr, i);
803 mergeInValue(getStructValueState(&IVI, i), &IVI, EltVal);
804 continue;
805 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000806
Chris Lattner156b8c72009-11-03 23:40:48 +0000807 Value *Val = IVI.getInsertedValueOperand();
Duncan Sands19d0b472010-02-16 11:11:14 +0000808 if (Val->getType()->isStructTy())
Chris Lattner156b8c72009-11-03 23:40:48 +0000809 // We don't track structs in structs.
810 markOverdefined(getStructValueState(&IVI, i), &IVI);
811 else {
812 LatticeVal InVal = getValueState(Val);
813 mergeInValue(getStructValueState(&IVI, i), &IVI, InVal);
814 }
815 }
Dan Gohman041f9d02008-06-20 01:15:44 +0000816}
817
Chris Lattner074be1f2004-11-15 04:44:20 +0000818void SCCPSolver::visitSelectInst(SelectInst &I) {
Chris Lattner156b8c72009-11-03 23:40:48 +0000819 // If this select returns a struct, just mark the result overdefined.
820 // TODO: We could do a lot better than this if code actually uses this.
Duncan Sands19d0b472010-02-16 11:11:14 +0000821 if (I.getType()->isStructTy())
Chris Lattner156b8c72009-11-03 23:40:48 +0000822 return markAnythingOverdefined(&I);
Jakub Staszak632a3552012-01-18 21:16:33 +0000823
Chris Lattnerf5484032009-11-02 05:55:40 +0000824 LatticeVal CondValue = getValueState(I.getCondition());
Chris Lattner06a0ed12006-02-08 02:38:11 +0000825 if (CondValue.isUndefined())
826 return;
Jakub Staszak632a3552012-01-18 21:16:33 +0000827
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000828 if (ConstantInt *CondCB = CondValue.getConstantInt()) {
Chris Lattnerf5484032009-11-02 05:55:40 +0000829 Value *OpVal = CondCB->isZero() ? I.getFalseValue() : I.getTrueValue();
830 mergeInValue(&I, getValueState(OpVal));
Chris Lattner9e97fbe2009-11-02 03:21:36 +0000831 return;
Chris Lattner06a0ed12006-02-08 02:38:11 +0000832 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000833
Chris Lattner06a0ed12006-02-08 02:38:11 +0000834 // Otherwise, the condition is overdefined or a constant we can't evaluate.
835 // See if we can produce something better than overdefined based on the T/F
836 // value.
Chris Lattnerf5484032009-11-02 05:55:40 +0000837 LatticeVal TVal = getValueState(I.getTrueValue());
838 LatticeVal FVal = getValueState(I.getFalseValue());
Jakub Staszak632a3552012-01-18 21:16:33 +0000839
Chris Lattner06a0ed12006-02-08 02:38:11 +0000840 // select ?, C, C -> C.
Jakub Staszak632a3552012-01-18 21:16:33 +0000841 if (TVal.isConstant() && FVal.isConstant() &&
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000842 TVal.getConstant() == FVal.getConstant())
843 return markConstant(&I, FVal.getConstant());
Chris Lattner06a0ed12006-02-08 02:38:11 +0000844
Chris Lattnerf5484032009-11-02 05:55:40 +0000845 if (TVal.isUndefined()) // select ?, undef, X -> X.
846 return mergeInValue(&I, FVal);
847 if (FVal.isUndefined()) // select ?, X, undef -> X.
848 return mergeInValue(&I, TVal);
849 markOverdefined(&I);
Chris Lattner59db22d2004-03-12 05:52:44 +0000850}
851
Chris Lattnerf5484032009-11-02 05:55:40 +0000852// Handle Binary Operators.
Chris Lattner074be1f2004-11-15 04:44:20 +0000853void SCCPSolver::visitBinaryOperator(Instruction &I) {
Chris Lattnerf5484032009-11-02 05:55:40 +0000854 LatticeVal V1State = getValueState(I.getOperand(0));
855 LatticeVal V2State = getValueState(I.getOperand(1));
Jakub Staszak632a3552012-01-18 21:16:33 +0000856
Chris Lattner4f031622004-11-15 05:03:30 +0000857 LatticeVal &IV = ValueState[&I];
Chris Lattner05fe6842004-01-12 03:57:30 +0000858 if (IV.isOverdefined()) return;
859
Chris Lattnerf5484032009-11-02 05:55:40 +0000860 if (V1State.isConstant() && V2State.isConstant())
861 return markConstant(IV, &I,
862 ConstantExpr::get(I.getOpcode(), V1State.getConstant(),
863 V2State.getConstant()));
Jakub Staszak632a3552012-01-18 21:16:33 +0000864
Chris Lattnerf5484032009-11-02 05:55:40 +0000865 // If something is undef, wait for it to resolve.
866 if (!V1State.isOverdefined() && !V2State.isOverdefined())
867 return;
Jakub Staszak632a3552012-01-18 21:16:33 +0000868
Chris Lattnerf5484032009-11-02 05:55:40 +0000869 // Otherwise, one of our operands is overdefined. Try to produce something
870 // better than overdefined with some tricks.
Jakub Staszak632a3552012-01-18 21:16:33 +0000871
Chris Lattnerf5484032009-11-02 05:55:40 +0000872 // If this is an AND or OR with 0 or -1, it doesn't matter that the other
873 // operand is overdefined.
874 if (I.getOpcode() == Instruction::And || I.getOpcode() == Instruction::Or) {
Craig Topperf40110f2014-04-25 05:29:35 +0000875 LatticeVal *NonOverdefVal = nullptr;
Chris Lattnerf5484032009-11-02 05:55:40 +0000876 if (!V1State.isOverdefined())
877 NonOverdefVal = &V1State;
878 else if (!V2State.isOverdefined())
879 NonOverdefVal = &V2State;
Chris Lattner05fe6842004-01-12 03:57:30 +0000880
Chris Lattnerf5484032009-11-02 05:55:40 +0000881 if (NonOverdefVal) {
882 if (NonOverdefVal->isUndefined()) {
883 // Could annihilate value.
884 if (I.getOpcode() == Instruction::And)
885 markConstant(IV, &I, Constant::getNullValue(I.getType()));
Chris Lattner229907c2011-07-18 04:54:35 +0000886 else if (VectorType *PT = dyn_cast<VectorType>(I.getType()))
Chris Lattnerf5484032009-11-02 05:55:40 +0000887 markConstant(IV, &I, Constant::getAllOnesValue(PT));
888 else
889 markConstant(IV, &I,
890 Constant::getAllOnesValue(I.getType()));
891 return;
Chris Lattnercbc01612004-12-11 23:15:19 +0000892 }
Jakub Staszak632a3552012-01-18 21:16:33 +0000893
Chris Lattnerf5484032009-11-02 05:55:40 +0000894 if (I.getOpcode() == Instruction::And) {
895 // X and 0 = 0
896 if (NonOverdefVal->getConstant()->isNullValue())
897 return markConstant(IV, &I, NonOverdefVal->getConstant());
898 } else {
899 if (ConstantInt *CI = NonOverdefVal->getConstantInt())
900 if (CI->isAllOnesValue()) // X or -1 = -1
901 return markConstant(IV, &I, NonOverdefVal->getConstant());
Chris Lattnercbc01612004-12-11 23:15:19 +0000902 }
903 }
Chris Lattnerf5484032009-11-02 05:55:40 +0000904 }
Chris Lattnercbc01612004-12-11 23:15:19 +0000905
906
Chris Lattnerf5484032009-11-02 05:55:40 +0000907 markOverdefined(&I);
Chris Lattner6e560792002-04-18 15:13:15 +0000908}
Chris Lattnerdd6522e2002-08-30 23:39:00 +0000909
Chris Lattnera3c39d32009-11-02 02:33:50 +0000910// Handle ICmpInst instruction.
Reid Spencer266e42b2006-12-23 06:05:41 +0000911void SCCPSolver::visitCmpInst(CmpInst &I) {
Chris Lattnerf5484032009-11-02 05:55:40 +0000912 LatticeVal V1State = getValueState(I.getOperand(0));
913 LatticeVal V2State = getValueState(I.getOperand(1));
914
Reid Spencer266e42b2006-12-23 06:05:41 +0000915 LatticeVal &IV = ValueState[&I];
916 if (IV.isOverdefined()) return;
917
Chris Lattnerf5484032009-11-02 05:55:40 +0000918 if (V1State.isConstant() && V2State.isConstant())
Jakub Staszak632a3552012-01-18 21:16:33 +0000919 return markConstant(IV, &I, ConstantExpr::getCompare(I.getPredicate(),
920 V1State.getConstant(),
Chris Lattnerf5484032009-11-02 05:55:40 +0000921 V2State.getConstant()));
Jakub Staszak632a3552012-01-18 21:16:33 +0000922
Chris Lattnerf5484032009-11-02 05:55:40 +0000923 // If operands are still undefined, wait for it to resolve.
924 if (!V1State.isOverdefined() && !V2State.isOverdefined())
925 return;
Jakub Staszak632a3552012-01-18 21:16:33 +0000926
Chris Lattnerf5484032009-11-02 05:55:40 +0000927 markOverdefined(&I);
Reid Spencer266e42b2006-12-23 06:05:41 +0000928}
929
Robert Bocchinobd518d12006-01-10 19:05:05 +0000930void SCCPSolver::visitExtractElementInst(ExtractElementInst &I) {
Chris Lattner156b8c72009-11-03 23:40:48 +0000931 // TODO : SCCP does not handle vectors properly.
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000932 return markOverdefined(&I);
Devang Patel21efc732006-12-04 23:54:59 +0000933
934#if 0
Robert Bocchinobd518d12006-01-10 19:05:05 +0000935 LatticeVal &ValState = getValueState(I.getOperand(0));
936 LatticeVal &IdxState = getValueState(I.getOperand(1));
937
938 if (ValState.isOverdefined() || IdxState.isOverdefined())
939 markOverdefined(&I);
940 else if(ValState.isConstant() && IdxState.isConstant())
941 markConstant(&I, ConstantExpr::getExtractElement(ValState.getConstant(),
942 IdxState.getConstant()));
Devang Patel21efc732006-12-04 23:54:59 +0000943#endif
Robert Bocchinobd518d12006-01-10 19:05:05 +0000944}
945
Robert Bocchino6dce2502006-01-17 20:06:55 +0000946void SCCPSolver::visitInsertElementInst(InsertElementInst &I) {
Chris Lattner156b8c72009-11-03 23:40:48 +0000947 // TODO : SCCP does not handle vectors properly.
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000948 return markOverdefined(&I);
Devang Patel21efc732006-12-04 23:54:59 +0000949#if 0
Robert Bocchino6dce2502006-01-17 20:06:55 +0000950 LatticeVal &ValState = getValueState(I.getOperand(0));
951 LatticeVal &EltState = getValueState(I.getOperand(1));
952 LatticeVal &IdxState = getValueState(I.getOperand(2));
953
954 if (ValState.isOverdefined() || EltState.isOverdefined() ||
955 IdxState.isOverdefined())
956 markOverdefined(&I);
957 else if(ValState.isConstant() && EltState.isConstant() &&
958 IdxState.isConstant())
959 markConstant(&I, ConstantExpr::getInsertElement(ValState.getConstant(),
960 EltState.getConstant(),
961 IdxState.getConstant()));
962 else if (ValState.isUndefined() && EltState.isConstant() &&
Jakub Staszak632a3552012-01-18 21:16:33 +0000963 IdxState.isConstant())
Chris Lattner28d921d2007-04-14 23:32:02 +0000964 markConstant(&I,ConstantExpr::getInsertElement(UndefValue::get(I.getType()),
965 EltState.getConstant(),
966 IdxState.getConstant()));
Devang Patel21efc732006-12-04 23:54:59 +0000967#endif
Robert Bocchino6dce2502006-01-17 20:06:55 +0000968}
969
Chris Lattner17bd6052006-04-08 01:19:12 +0000970void SCCPSolver::visitShuffleVectorInst(ShuffleVectorInst &I) {
Chris Lattner156b8c72009-11-03 23:40:48 +0000971 // TODO : SCCP does not handle vectors properly.
Chris Lattner7ccf1a62009-11-02 03:03:42 +0000972 return markOverdefined(&I);
Devang Patel21efc732006-12-04 23:54:59 +0000973#if 0
Chris Lattner17bd6052006-04-08 01:19:12 +0000974 LatticeVal &V1State = getValueState(I.getOperand(0));
975 LatticeVal &V2State = getValueState(I.getOperand(1));
976 LatticeVal &MaskState = getValueState(I.getOperand(2));
977
978 if (MaskState.isUndefined() ||
979 (V1State.isUndefined() && V2State.isUndefined()))
980 return; // Undefined output if mask or both inputs undefined.
Jakub Staszak632a3552012-01-18 21:16:33 +0000981
Chris Lattner17bd6052006-04-08 01:19:12 +0000982 if (V1State.isOverdefined() || V2State.isOverdefined() ||
983 MaskState.isOverdefined()) {
984 markOverdefined(&I);
985 } else {
986 // A mix of constant/undef inputs.
Jakub Staszak632a3552012-01-18 21:16:33 +0000987 Constant *V1 = V1State.isConstant() ?
Chris Lattner17bd6052006-04-08 01:19:12 +0000988 V1State.getConstant() : UndefValue::get(I.getType());
Jakub Staszak632a3552012-01-18 21:16:33 +0000989 Constant *V2 = V2State.isConstant() ?
Chris Lattner17bd6052006-04-08 01:19:12 +0000990 V2State.getConstant() : UndefValue::get(I.getType());
Jakub Staszak632a3552012-01-18 21:16:33 +0000991 Constant *Mask = MaskState.isConstant() ?
Chris Lattner17bd6052006-04-08 01:19:12 +0000992 MaskState.getConstant() : UndefValue::get(I.getOperand(2)->getType());
993 markConstant(&I, ConstantExpr::getShuffleVector(V1, V2, Mask));
994 }
Devang Patel21efc732006-12-04 23:54:59 +0000995#endif
Chris Lattner17bd6052006-04-08 01:19:12 +0000996}
997
Chris Lattnera3c39d32009-11-02 02:33:50 +0000998// Handle getelementptr instructions. If all operands are constants then we
Chris Lattnerdd6522e2002-08-30 23:39:00 +0000999// can turn this into a getelementptr ConstantExpr.
1000//
Chris Lattner074be1f2004-11-15 04:44:20 +00001001void SCCPSolver::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattnerb70ef3c2009-11-02 23:25:39 +00001002 if (ValueState[&I].isOverdefined()) return;
Chris Lattner49f74522004-01-12 04:29:41 +00001003
Chris Lattner0e7ec672007-02-02 20:51:48 +00001004 SmallVector<Constant*, 8> Operands;
Chris Lattnerdd6522e2002-08-30 23:39:00 +00001005 Operands.reserve(I.getNumOperands());
1006
1007 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
Chris Lattnerf5484032009-11-02 05:55:40 +00001008 LatticeVal State = getValueState(I.getOperand(i));
Chris Lattnerdd6522e2002-08-30 23:39:00 +00001009 if (State.isUndefined())
Chris Lattnera3c39d32009-11-02 02:33:50 +00001010 return; // Operands are not resolved yet.
Jakub Staszak632a3552012-01-18 21:16:33 +00001011
Chris Lattner7ccf1a62009-11-02 03:03:42 +00001012 if (State.isOverdefined())
Chris Lattnerb70ef3c2009-11-02 23:25:39 +00001013 return markOverdefined(&I);
Chris Lattner7ccf1a62009-11-02 03:03:42 +00001014
Chris Lattnerdd6522e2002-08-30 23:39:00 +00001015 assert(State.isConstant() && "Unknown state!");
1016 Operands.push_back(State.getConstant());
1017 }
1018
1019 Constant *Ptr = Operands[0];
Craig Toppere1d12942014-08-27 05:25:25 +00001020 auto Indices = makeArrayRef(Operands.begin() + 1, Operands.end());
David Blaikie4a2e73b2015-04-02 18:55:32 +00001021 markConstant(&I, ConstantExpr::getGetElementPtr(I.getSourceElementType(), Ptr,
1022 Indices));
Chris Lattnerdd6522e2002-08-30 23:39:00 +00001023}
Brian Gaeke960707c2003-11-11 22:41:34 +00001024
Chris Lattnerf5484032009-11-02 05:55:40 +00001025void SCCPSolver::visitStoreInst(StoreInst &SI) {
Chris Lattner156b8c72009-11-03 23:40:48 +00001026 // If this store is of a struct, ignore it.
Duncan Sands19d0b472010-02-16 11:11:14 +00001027 if (SI.getOperand(0)->getType()->isStructTy())
Chris Lattner156b8c72009-11-03 23:40:48 +00001028 return;
Jakub Staszak632a3552012-01-18 21:16:33 +00001029
Chris Lattner91dbae62004-12-11 05:15:59 +00001030 if (TrackedGlobals.empty() || !isa<GlobalVariable>(SI.getOperand(1)))
1031 return;
Jakub Staszak632a3552012-01-18 21:16:33 +00001032
Chris Lattner91dbae62004-12-11 05:15:59 +00001033 GlobalVariable *GV = cast<GlobalVariable>(SI.getOperand(1));
Chris Lattner067d6072007-02-02 20:38:30 +00001034 DenseMap<GlobalVariable*, LatticeVal>::iterator I = TrackedGlobals.find(GV);
Chris Lattner91dbae62004-12-11 05:15:59 +00001035 if (I == TrackedGlobals.end() || I->second.isOverdefined()) return;
1036
Chris Lattnerf5484032009-11-02 05:55:40 +00001037 // Get the value we are storing into the global, then merge it.
1038 mergeInValue(I->second, GV, getValueState(SI.getOperand(0)));
Chris Lattner91dbae62004-12-11 05:15:59 +00001039 if (I->second.isOverdefined())
1040 TrackedGlobals.erase(I); // No need to keep tracking this!
1041}
1042
1043
Chris Lattner49f74522004-01-12 04:29:41 +00001044// Handle load instructions. If the operand is a constant pointer to a constant
1045// global, we can replace the load with the loaded constant value!
Chris Lattner074be1f2004-11-15 04:44:20 +00001046void SCCPSolver::visitLoadInst(LoadInst &I) {
Chris Lattner156b8c72009-11-03 23:40:48 +00001047 // If this load is of a struct, just mark the result overdefined.
Duncan Sands19d0b472010-02-16 11:11:14 +00001048 if (I.getType()->isStructTy())
Chris Lattner156b8c72009-11-03 23:40:48 +00001049 return markAnythingOverdefined(&I);
Jakub Staszak632a3552012-01-18 21:16:33 +00001050
Chris Lattnerf5484032009-11-02 05:55:40 +00001051 LatticeVal PtrVal = getValueState(I.getOperand(0));
Chris Lattnere77c9aa2009-11-02 06:06:14 +00001052 if (PtrVal.isUndefined()) return; // The pointer is not resolved yet!
Jakub Staszak632a3552012-01-18 21:16:33 +00001053
Chris Lattner4f031622004-11-15 05:03:30 +00001054 LatticeVal &IV = ValueState[&I];
Chris Lattner49f74522004-01-12 04:29:41 +00001055 if (IV.isOverdefined()) return;
1056
Chris Lattnerf5484032009-11-02 05:55:40 +00001057 if (!PtrVal.isConstant() || I.isVolatile())
1058 return markOverdefined(IV, &I);
Jakub Staszak632a3552012-01-18 21:16:33 +00001059
Chris Lattnere77c9aa2009-11-02 06:06:14 +00001060 Constant *Ptr = PtrVal.getConstant();
Misha Brukmanb1c93172005-04-21 23:48:37 +00001061
Chris Lattnerf5484032009-11-02 05:55:40 +00001062 // load null -> null
1063 if (isa<ConstantPointerNull>(Ptr) && I.getPointerAddressSpace() == 0)
David Majnemer9402e272015-07-01 05:37:57 +00001064 return markConstant(IV, &I, UndefValue::get(I.getType()));
Jakub Staszak632a3552012-01-18 21:16:33 +00001065
Chris Lattnerf5484032009-11-02 05:55:40 +00001066 // Transform load (constant global) into the value loaded.
1067 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) {
Chris Lattnere77c9aa2009-11-02 06:06:14 +00001068 if (!TrackedGlobals.empty()) {
Chris Lattnerf5484032009-11-02 05:55:40 +00001069 // If we are tracking this global, merge in the known value for it.
1070 DenseMap<GlobalVariable*, LatticeVal>::iterator It =
1071 TrackedGlobals.find(GV);
1072 if (It != TrackedGlobals.end()) {
1073 mergeInValue(IV, &I, It->second);
1074 return;
Chris Lattner49f74522004-01-12 04:29:41 +00001075 }
Chris Lattner91dbae62004-12-11 05:15:59 +00001076 }
Chris Lattner49f74522004-01-12 04:29:41 +00001077 }
1078
Chris Lattnere77c9aa2009-11-02 06:06:14 +00001079 // Transform load from a constant into a constant if possible.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001080 if (Constant *C = ConstantFoldLoadFromConstPtr(Ptr, DL))
Chris Lattnere77c9aa2009-11-02 06:06:14 +00001081 return markConstant(IV, &I, C);
Chris Lattnerf5484032009-11-02 05:55:40 +00001082
Chris Lattner49f74522004-01-12 04:29:41 +00001083 // Otherwise we cannot say for certain what value this load will produce.
1084 // Bail out.
1085 markOverdefined(IV, &I);
1086}
Chris Lattnerff9362a2004-04-13 19:43:54 +00001087
Chris Lattnerb4394642004-12-10 08:02:06 +00001088void SCCPSolver::visitCallSite(CallSite CS) {
1089 Function *F = CS.getCalledFunction();
Chris Lattnerb4394642004-12-10 08:02:06 +00001090 Instruction *I = CS.getInstruction();
Jakub Staszak632a3552012-01-18 21:16:33 +00001091
Chris Lattner5a58a4d2008-04-23 05:38:20 +00001092 // The common case is that we aren't tracking the callee, either because we
1093 // are not doing interprocedural analysis or the callee is indirect, or is
1094 // external. Handle these cases first.
Craig Topperf40110f2014-04-25 05:29:35 +00001095 if (!F || F->isDeclaration()) {
Chris Lattner5a58a4d2008-04-23 05:38:20 +00001096CallOverdefined:
1097 // Void return and not tracking callee, just bail.
Chris Lattnerfdd87902009-10-05 05:54:46 +00001098 if (I->getType()->isVoidTy()) return;
Jakub Staszak632a3552012-01-18 21:16:33 +00001099
Chris Lattner5a58a4d2008-04-23 05:38:20 +00001100 // Otherwise, if we have a single return value case, and if the function is
1101 // a declaration, maybe we can constant fold it.
Duncan Sands19d0b472010-02-16 11:11:14 +00001102 if (F && F->isDeclaration() && !I->getType()->isStructTy() &&
Chris Lattner5a58a4d2008-04-23 05:38:20 +00001103 canConstantFoldCallTo(F)) {
Jakub Staszak632a3552012-01-18 21:16:33 +00001104
Chris Lattner5a58a4d2008-04-23 05:38:20 +00001105 SmallVector<Constant*, 8> Operands;
1106 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
1107 AI != E; ++AI) {
Chris Lattnerf5484032009-11-02 05:55:40 +00001108 LatticeVal State = getValueState(*AI);
Jakub Staszak632a3552012-01-18 21:16:33 +00001109
Chris Lattner5a58a4d2008-04-23 05:38:20 +00001110 if (State.isUndefined())
1111 return; // Operands are not resolved yet.
Chris Lattner7ccf1a62009-11-02 03:03:42 +00001112 if (State.isOverdefined())
1113 return markOverdefined(I);
Chris Lattner5a58a4d2008-04-23 05:38:20 +00001114 assert(State.isConstant() && "Unknown state!");
1115 Operands.push_back(State.getConstant());
1116 }
Jakub Staszak632a3552012-01-18 21:16:33 +00001117
David Majnemer2098b86f2014-11-07 08:54:19 +00001118 if (getValueState(I).isOverdefined())
1119 return;
1120
Chris Lattner5a58a4d2008-04-23 05:38:20 +00001121 // If we can constant fold this, mark the result of the call as a
1122 // constant.
Chad Rosiere6de63d2011-12-01 21:29:16 +00001123 if (Constant *C = ConstantFoldCall(F, Operands, TLI))
Chris Lattner7ccf1a62009-11-02 03:03:42 +00001124 return markConstant(I, C);
Chris Lattnerff9362a2004-04-13 19:43:54 +00001125 }
Chris Lattner5a58a4d2008-04-23 05:38:20 +00001126
1127 // Otherwise, we don't know anything about this call, mark it overdefined.
Chris Lattner156b8c72009-11-03 23:40:48 +00001128 return markAnythingOverdefined(I);
Chris Lattnerff9362a2004-04-13 19:43:54 +00001129 }
1130
Chris Lattnercde8de52009-11-03 19:24:51 +00001131 // If this is a local function that doesn't have its address taken, mark its
1132 // entry block executable and merge in the actual arguments to the call into
1133 // the formal arguments of the function.
1134 if (!TrackingIncomingArguments.empty() && TrackingIncomingArguments.count(F)){
1135 MarkBlockExecutable(F->begin());
Jakub Staszak632a3552012-01-18 21:16:33 +00001136
Chris Lattnercde8de52009-11-03 19:24:51 +00001137 // Propagate information from this call site into the callee.
1138 CallSite::arg_iterator CAI = CS.arg_begin();
1139 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1140 AI != E; ++AI, ++CAI) {
1141 // If this argument is byval, and if the function is not readonly, there
1142 // will be an implicit copy formed of the input aggregate.
1143 if (AI->hasByValAttr() && !F->onlyReadsMemory()) {
1144 markOverdefined(AI);
1145 continue;
1146 }
Jakub Staszak632a3552012-01-18 21:16:33 +00001147
Chris Lattner229907c2011-07-18 04:54:35 +00001148 if (StructType *STy = dyn_cast<StructType>(AI->getType())) {
Chris Lattner762b56f2009-11-04 18:57:42 +00001149 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1150 LatticeVal CallArg = getStructValueState(*CAI, i);
1151 mergeInValue(getStructValueState(AI, i), AI, CallArg);
1152 }
Chris Lattner156b8c72009-11-03 23:40:48 +00001153 } else {
1154 mergeInValue(AI, getValueState(*CAI));
1155 }
Chris Lattnercde8de52009-11-03 19:24:51 +00001156 }
1157 }
Jakub Staszak632a3552012-01-18 21:16:33 +00001158
Chris Lattner5a58a4d2008-04-23 05:38:20 +00001159 // If this is a single/zero retval case, see if we're tracking the function.
Chris Lattner229907c2011-07-18 04:54:35 +00001160 if (StructType *STy = dyn_cast<StructType>(F->getReturnType())) {
Chris Lattner156b8c72009-11-03 23:40:48 +00001161 if (!MRVFunctionsTracked.count(F))
1162 goto CallOverdefined; // Not tracking this callee.
Jakub Staszak632a3552012-01-18 21:16:33 +00001163
Chris Lattner156b8c72009-11-03 23:40:48 +00001164 // If we are tracking this callee, propagate the result of the function
1165 // into this call site.
1166 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Jakub Staszak632a3552012-01-18 21:16:33 +00001167 mergeInValue(getStructValueState(I, i), I,
Chris Lattner156b8c72009-11-03 23:40:48 +00001168 TrackedMultipleRetVals[std::make_pair(F, i)]);
1169 } else {
1170 DenseMap<Function*, LatticeVal>::iterator TFRVI = TrackedRetVals.find(F);
1171 if (TFRVI == TrackedRetVals.end())
1172 goto CallOverdefined; // Not tracking this callee.
Jakub Staszak632a3552012-01-18 21:16:33 +00001173
Chris Lattner5a58a4d2008-04-23 05:38:20 +00001174 // If so, propagate the return value of the callee into this call result.
1175 mergeInValue(I, TFRVI->second);
Chris Lattner5a58a4d2008-04-23 05:38:20 +00001176 }
Chris Lattnerff9362a2004-04-13 19:43:54 +00001177}
Chris Lattner074be1f2004-11-15 04:44:20 +00001178
Chris Lattner074be1f2004-11-15 04:44:20 +00001179void SCCPSolver::Solve() {
1180 // Process the work lists until they are empty!
Misha Brukmanb1c93172005-04-21 23:48:37 +00001181 while (!BBWorkList.empty() || !InstWorkList.empty() ||
Jeff Cohen82639852005-04-23 21:38:35 +00001182 !OverdefinedInstWorkList.empty()) {
Chris Lattnerf5484032009-11-02 05:55:40 +00001183 // Process the overdefined instruction's work list first, which drives other
1184 // things to overdefined more quickly.
Chris Lattner074be1f2004-11-15 04:44:20 +00001185 while (!OverdefinedInstWorkList.empty()) {
Chris Lattnerf5484032009-11-02 05:55:40 +00001186 Value *I = OverdefinedInstWorkList.pop_back_val();
Chris Lattner074be1f2004-11-15 04:44:20 +00001187
David Greene389fc3b2010-01-05 01:27:15 +00001188 DEBUG(dbgs() << "\nPopped off OI-WL: " << *I << '\n');
Misha Brukmanb1c93172005-04-21 23:48:37 +00001189
Chris Lattner074be1f2004-11-15 04:44:20 +00001190 // "I" got into the work list because it either made the transition from
Chad Rosier4d87d452013-02-20 20:15:55 +00001191 // bottom to constant, or to overdefined.
Chris Lattner074be1f2004-11-15 04:44:20 +00001192 //
1193 // Anything on this worklist that is overdefined need not be visited
1194 // since all of its users will have already been marked as overdefined
Chris Lattnera3c39d32009-11-02 02:33:50 +00001195 // Update all of the users of this instruction's value.
Chris Lattner074be1f2004-11-15 04:44:20 +00001196 //
Chandler Carruthcdf47882014-03-09 03:16:01 +00001197 for (User *U : I->users())
1198 if (Instruction *UI = dyn_cast<Instruction>(U))
1199 OperandChangedState(UI);
Chris Lattner074be1f2004-11-15 04:44:20 +00001200 }
Jakub Staszak632a3552012-01-18 21:16:33 +00001201
Chris Lattnera3c39d32009-11-02 02:33:50 +00001202 // Process the instruction work list.
Chris Lattner074be1f2004-11-15 04:44:20 +00001203 while (!InstWorkList.empty()) {
Chris Lattnerf5484032009-11-02 05:55:40 +00001204 Value *I = InstWorkList.pop_back_val();
Chris Lattner074be1f2004-11-15 04:44:20 +00001205
David Greene389fc3b2010-01-05 01:27:15 +00001206 DEBUG(dbgs() << "\nPopped off I-WL: " << *I << '\n');
Misha Brukmanb1c93172005-04-21 23:48:37 +00001207
Chris Lattnerf5484032009-11-02 05:55:40 +00001208 // "I" got into the work list because it made the transition from undef to
1209 // constant.
Chris Lattner074be1f2004-11-15 04:44:20 +00001210 //
1211 // Anything on this worklist that is overdefined need not be visited
1212 // since all of its users will have already been marked as overdefined.
Chris Lattnera3c39d32009-11-02 02:33:50 +00001213 // Update all of the users of this instruction's value.
Chris Lattner074be1f2004-11-15 04:44:20 +00001214 //
Duncan Sands19d0b472010-02-16 11:11:14 +00001215 if (I->getType()->isStructTy() || !getValueState(I).isOverdefined())
Chandler Carruthcdf47882014-03-09 03:16:01 +00001216 for (User *U : I->users())
1217 if (Instruction *UI = dyn_cast<Instruction>(U))
1218 OperandChangedState(UI);
Chris Lattner074be1f2004-11-15 04:44:20 +00001219 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001220
Chris Lattnera3c39d32009-11-02 02:33:50 +00001221 // Process the basic block work list.
Chris Lattner074be1f2004-11-15 04:44:20 +00001222 while (!BBWorkList.empty()) {
1223 BasicBlock *BB = BBWorkList.back();
1224 BBWorkList.pop_back();
Misha Brukmanb1c93172005-04-21 23:48:37 +00001225
David Greene389fc3b2010-01-05 01:27:15 +00001226 DEBUG(dbgs() << "\nPopped off BBWL: " << *BB << '\n');
Misha Brukmanb1c93172005-04-21 23:48:37 +00001227
Chris Lattner074be1f2004-11-15 04:44:20 +00001228 // Notify all instructions in this basic block that they are newly
1229 // executable.
1230 visit(BB);
1231 }
1232 }
1233}
1234
Chris Lattner1847f6d2006-12-20 06:21:33 +00001235/// ResolvedUndefsIn - While solving the dataflow for a function, we assume
Chris Lattner7285f432004-12-10 20:41:50 +00001236/// that branches on undef values cannot reach any of their successors.
1237/// However, this is not a safe assumption. After we solve dataflow, this
1238/// method should be use to handle this. If this returns true, the solver
1239/// should be rerun.
Chris Lattneraf170962006-10-22 05:59:17 +00001240///
1241/// This method handles this by finding an unresolved branch and marking it one
1242/// of the edges from the block as being feasible, even though the condition
1243/// doesn't say it would otherwise be. This allows SCCP to find the rest of the
1244/// CFG and only slightly pessimizes the analysis results (by marking one,
Chris Lattner1847f6d2006-12-20 06:21:33 +00001245/// potentially infeasible, edge feasible). This cannot usefully modify the
Chris Lattneraf170962006-10-22 05:59:17 +00001246/// constraints on the condition of the branch, as that would impact other users
1247/// of the value.
Chris Lattner1847f6d2006-12-20 06:21:33 +00001248///
1249/// This scan also checks for values that use undefs, whose results are actually
1250/// defined. For example, 'zext i8 undef to i32' should produce all zeros
1251/// conservatively, as "(zext i8 X -> i32) & 0xFF00" must always return zero,
1252/// even if X isn't defined.
1253bool SCCPSolver::ResolvedUndefsIn(Function &F) {
Chris Lattneraf170962006-10-22 05:59:17 +00001254 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1255 if (!BBExecutable.count(BB))
1256 continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001257
Chris Lattner1847f6d2006-12-20 06:21:33 +00001258 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
1259 // Look for instructions which produce undef values.
Chris Lattnerfdd87902009-10-05 05:54:46 +00001260 if (I->getType()->isVoidTy()) continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001261
Chris Lattner229907c2011-07-18 04:54:35 +00001262 if (StructType *STy = dyn_cast<StructType>(I->getType())) {
Eli Friedman1815b682011-09-20 23:28:51 +00001263 // Only a few things that can be structs matter for undef.
1264
1265 // Tracked calls must never be marked overdefined in ResolvedUndefsIn.
1266 if (CallSite CS = CallSite(I))
1267 if (Function *F = CS.getCalledFunction())
1268 if (MRVFunctionsTracked.count(F))
1269 continue;
1270
1271 // extractvalue and insertvalue don't need to be marked; they are
Jakub Staszak632a3552012-01-18 21:16:33 +00001272 // tracked as precisely as their operands.
Eli Friedman1815b682011-09-20 23:28:51 +00001273 if (isa<ExtractValueInst>(I) || isa<InsertValueInst>(I))
1274 continue;
1275
1276 // Send the results of everything else to overdefined. We could be
1277 // more precise than this but it isn't worth bothering.
1278 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1279 LatticeVal &LV = getStructValueState(I, i);
1280 if (LV.isUndefined())
1281 markOverdefined(LV, I);
Chris Lattner156b8c72009-11-03 23:40:48 +00001282 }
1283 continue;
1284 }
Eli Friedman0793eb42011-08-16 22:06:31 +00001285
Chris Lattner1847f6d2006-12-20 06:21:33 +00001286 LatticeVal &LV = getValueState(I);
1287 if (!LV.isUndefined()) continue;
1288
Eli Friedmand7749be2011-08-17 18:10:43 +00001289 // extractvalue is safe; check here because the argument is a struct.
1290 if (isa<ExtractValueInst>(I))
1291 continue;
1292
1293 // Compute the operand LatticeVals, for convenience below.
1294 // Anything taking a struct is conservatively assumed to require
1295 // overdefined markings.
1296 if (I->getOperand(0)->getType()->isStructTy()) {
1297 markOverdefined(I);
1298 return true;
1299 }
Chris Lattnerf5484032009-11-02 05:55:40 +00001300 LatticeVal Op0LV = getValueState(I->getOperand(0));
Chris Lattner1847f6d2006-12-20 06:21:33 +00001301 LatticeVal Op1LV;
Eli Friedmand7749be2011-08-17 18:10:43 +00001302 if (I->getNumOperands() == 2) {
1303 if (I->getOperand(1)->getType()->isStructTy()) {
1304 markOverdefined(I);
1305 return true;
1306 }
1307
Chris Lattner1847f6d2006-12-20 06:21:33 +00001308 Op1LV = getValueState(I->getOperand(1));
Eli Friedmand7749be2011-08-17 18:10:43 +00001309 }
Chris Lattner1847f6d2006-12-20 06:21:33 +00001310 // If this is an instructions whose result is defined even if the input is
1311 // not fully defined, propagate the information.
Chris Lattner229907c2011-07-18 04:54:35 +00001312 Type *ITy = I->getType();
Chris Lattner1847f6d2006-12-20 06:21:33 +00001313 switch (I->getOpcode()) {
Eli Friedman0793eb42011-08-16 22:06:31 +00001314 case Instruction::Add:
1315 case Instruction::Sub:
1316 case Instruction::Trunc:
1317 case Instruction::FPTrunc:
1318 case Instruction::BitCast:
1319 break; // Any undef -> undef
1320 case Instruction::FSub:
1321 case Instruction::FAdd:
1322 case Instruction::FMul:
1323 case Instruction::FDiv:
1324 case Instruction::FRem:
1325 // Floating-point binary operation: be conservative.
1326 if (Op0LV.isUndefined() && Op1LV.isUndefined())
1327 markForcedConstant(I, Constant::getNullValue(ITy));
1328 else
1329 markOverdefined(I);
1330 return true;
Chris Lattner1847f6d2006-12-20 06:21:33 +00001331 case Instruction::ZExt:
Eli Friedman0793eb42011-08-16 22:06:31 +00001332 case Instruction::SExt:
1333 case Instruction::FPToUI:
1334 case Instruction::FPToSI:
1335 case Instruction::FPExt:
1336 case Instruction::PtrToInt:
1337 case Instruction::IntToPtr:
1338 case Instruction::SIToFP:
1339 case Instruction::UIToFP:
1340 // undef -> 0; some outputs are impossible
Chris Lattnerf5484032009-11-02 05:55:40 +00001341 markForcedConstant(I, Constant::getNullValue(ITy));
Chris Lattner1847f6d2006-12-20 06:21:33 +00001342 return true;
1343 case Instruction::Mul:
1344 case Instruction::And:
Eli Friedman0793eb42011-08-16 22:06:31 +00001345 // Both operands undef -> undef
1346 if (Op0LV.isUndefined() && Op1LV.isUndefined())
1347 break;
Chris Lattner1847f6d2006-12-20 06:21:33 +00001348 // undef * X -> 0. X could be zero.
1349 // undef & X -> 0. X could be zero.
Chris Lattnerf5484032009-11-02 05:55:40 +00001350 markForcedConstant(I, Constant::getNullValue(ITy));
Chris Lattner1847f6d2006-12-20 06:21:33 +00001351 return true;
1352
1353 case Instruction::Or:
Eli Friedman0793eb42011-08-16 22:06:31 +00001354 // Both operands undef -> undef
1355 if (Op0LV.isUndefined() && Op1LV.isUndefined())
1356 break;
Chris Lattner1847f6d2006-12-20 06:21:33 +00001357 // undef | X -> -1. X could be -1.
Chris Lattnerf5484032009-11-02 05:55:40 +00001358 markForcedConstant(I, Constant::getAllOnesValue(ITy));
Chris Lattner806adaf2007-01-04 02:12:40 +00001359 return true;
Chris Lattner1847f6d2006-12-20 06:21:33 +00001360
Eli Friedman0793eb42011-08-16 22:06:31 +00001361 case Instruction::Xor:
1362 // undef ^ undef -> 0; strictly speaking, this is not strictly
1363 // necessary, but we try to be nice to people who expect this
1364 // behavior in simple cases
1365 if (Op0LV.isUndefined() && Op1LV.isUndefined()) {
1366 markForcedConstant(I, Constant::getNullValue(ITy));
1367 return true;
1368 }
1369 // undef ^ X -> undef
1370 break;
1371
Chris Lattner1847f6d2006-12-20 06:21:33 +00001372 case Instruction::SDiv:
1373 case Instruction::UDiv:
1374 case Instruction::SRem:
1375 case Instruction::URem:
1376 // X / undef -> undef. No change.
1377 // X % undef -> undef. No change.
1378 if (Op1LV.isUndefined()) break;
Jakub Staszak632a3552012-01-18 21:16:33 +00001379
Chris Lattner1847f6d2006-12-20 06:21:33 +00001380 // undef / X -> 0. X could be maxint.
1381 // undef % X -> 0. X could be 1.
Chris Lattnerf5484032009-11-02 05:55:40 +00001382 markForcedConstant(I, Constant::getNullValue(ITy));
Chris Lattner1847f6d2006-12-20 06:21:33 +00001383 return true;
Jakub Staszak632a3552012-01-18 21:16:33 +00001384
Chris Lattner1847f6d2006-12-20 06:21:33 +00001385 case Instruction::AShr:
Eli Friedman0793eb42011-08-16 22:06:31 +00001386 // X >>a undef -> undef.
1387 if (Op1LV.isUndefined()) break;
1388
1389 // undef >>a X -> all ones
1390 markForcedConstant(I, Constant::getAllOnesValue(ITy));
Chris Lattner1847f6d2006-12-20 06:21:33 +00001391 return true;
1392 case Instruction::LShr:
1393 case Instruction::Shl:
Eli Friedman0793eb42011-08-16 22:06:31 +00001394 // X << undef -> undef.
1395 // X >> undef -> undef.
1396 if (Op1LV.isUndefined()) break;
1397
1398 // undef << X -> 0
1399 // undef >> X -> 0
Chris Lattnerf5484032009-11-02 05:55:40 +00001400 markForcedConstant(I, Constant::getNullValue(ITy));
Chris Lattner1847f6d2006-12-20 06:21:33 +00001401 return true;
1402 case Instruction::Select:
Eli Friedman0793eb42011-08-16 22:06:31 +00001403 Op1LV = getValueState(I->getOperand(1));
Chris Lattner1847f6d2006-12-20 06:21:33 +00001404 // undef ? X : Y -> X or Y. There could be commonality between X/Y.
1405 if (Op0LV.isUndefined()) {
1406 if (!Op1LV.isConstant()) // Pick the constant one if there is any.
1407 Op1LV = getValueState(I->getOperand(2));
1408 } else if (Op1LV.isUndefined()) {
1409 // c ? undef : undef -> undef. No change.
1410 Op1LV = getValueState(I->getOperand(2));
1411 if (Op1LV.isUndefined())
1412 break;
1413 // Otherwise, c ? undef : x -> x.
1414 } else {
1415 // Leave Op1LV as Operand(1)'s LatticeValue.
1416 }
Jakub Staszak632a3552012-01-18 21:16:33 +00001417
Chris Lattner1847f6d2006-12-20 06:21:33 +00001418 if (Op1LV.isConstant())
Chris Lattnerf5484032009-11-02 05:55:40 +00001419 markForcedConstant(I, Op1LV.getConstant());
Chris Lattner1847f6d2006-12-20 06:21:33 +00001420 else
Chris Lattnerf5484032009-11-02 05:55:40 +00001421 markOverdefined(I);
Chris Lattner1847f6d2006-12-20 06:21:33 +00001422 return true;
Eli Friedman0793eb42011-08-16 22:06:31 +00001423 case Instruction::Load:
1424 // A load here means one of two things: a load of undef from a global,
1425 // a load from an unknown pointer. Either way, having it return undef
1426 // is okay.
1427 break;
1428 case Instruction::ICmp:
1429 // X == undef -> undef. Other comparisons get more complicated.
1430 if (cast<ICmpInst>(I)->isEquality())
1431 break;
1432 markOverdefined(I);
1433 return true;
Eli Friedman1815b682011-09-20 23:28:51 +00001434 case Instruction::Call:
1435 case Instruction::Invoke: {
1436 // There are two reasons a call can have an undef result
1437 // 1. It could be tracked.
1438 // 2. It could be constant-foldable.
1439 // Because of the way we solve return values, tracked calls must
1440 // never be marked overdefined in ResolvedUndefsIn.
1441 if (Function *F = CallSite(I).getCalledFunction())
1442 if (TrackedRetVals.count(F))
1443 break;
1444
1445 // If the call is constant-foldable, we mark it overdefined because
1446 // we do not know what return values are valid.
1447 markOverdefined(I);
1448 return true;
1449 }
Eli Friedman0793eb42011-08-16 22:06:31 +00001450 default:
1451 // If we don't know what should happen here, conservatively mark it
Chris Lattner5c207c82008-05-24 03:59:33 +00001452 // overdefined.
Chris Lattnerf5484032009-11-02 05:55:40 +00001453 markOverdefined(I);
Chris Lattner5c207c82008-05-24 03:59:33 +00001454 return true;
Chris Lattner1847f6d2006-12-20 06:21:33 +00001455 }
1456 }
Jakub Staszak632a3552012-01-18 21:16:33 +00001457
Chris Lattneradca6082010-04-05 22:14:48 +00001458 // Check to see if we have a branch or switch on an undefined value. If so
1459 // we force the branch to go one way or the other to make the successor
1460 // values live. It doesn't really matter which way we force it.
Chris Lattneraf170962006-10-22 05:59:17 +00001461 TerminatorInst *TI = BB->getTerminator();
1462 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
1463 if (!BI->isConditional()) continue;
1464 if (!getValueState(BI->getCondition()).isUndefined())
1465 continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001466
Chris Lattneradca6082010-04-05 22:14:48 +00001467 // If the input to SCCP is actually branch on undef, fix the undef to
1468 // false.
1469 if (isa<UndefValue>(BI->getCondition())) {
1470 BI->setCondition(ConstantInt::getFalse(BI->getContext()));
1471 markEdgeExecutable(BB, TI->getSuccessor(1));
1472 return true;
1473 }
Jakub Staszak632a3552012-01-18 21:16:33 +00001474
Chris Lattneradca6082010-04-05 22:14:48 +00001475 // Otherwise, it is a branch on a symbolic value which is currently
1476 // considered to be undef. Handle this by forcing the input value to the
1477 // branch to false.
1478 markForcedConstant(BI->getCondition(),
1479 ConstantInt::getFalse(TI->getContext()));
1480 return true;
1481 }
Jakub Staszak632a3552012-01-18 21:16:33 +00001482
Chris Lattneradca6082010-04-05 22:14:48 +00001483 if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00001484 if (!SI->getNumCases())
Dale Johannesenfecb8822008-05-23 01:01:31 +00001485 continue;
Chris Lattneraf170962006-10-22 05:59:17 +00001486 if (!getValueState(SI->getCondition()).isUndefined())
1487 continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001488
Chris Lattneradca6082010-04-05 22:14:48 +00001489 // If the input to SCCP is actually switch on undef, fix the undef to
1490 // the first constant.
1491 if (isa<UndefValue>(SI->getCondition())) {
Stepan Dyatkovskiy97b02fc2012-03-11 06:09:17 +00001492 SI->setCondition(SI->case_begin().getCaseValue());
1493 markEdgeExecutable(BB, SI->case_begin().getCaseSuccessor());
Chris Lattneradca6082010-04-05 22:14:48 +00001494 return true;
1495 }
Jakub Staszak632a3552012-01-18 21:16:33 +00001496
Stepan Dyatkovskiy97b02fc2012-03-11 06:09:17 +00001497 markForcedConstant(SI->getCondition(), SI->case_begin().getCaseValue());
Chris Lattneradca6082010-04-05 22:14:48 +00001498 return true;
Chris Lattner7285f432004-12-10 20:41:50 +00001499 }
Chris Lattneraf170962006-10-22 05:59:17 +00001500 }
Chris Lattner2f687fd2004-12-11 06:05:53 +00001501
Chris Lattneraf170962006-10-22 05:59:17 +00001502 return false;
Chris Lattner7285f432004-12-10 20:41:50 +00001503}
1504
Chris Lattner074be1f2004-11-15 04:44:20 +00001505
1506namespace {
Chris Lattner1890f942004-11-15 07:15:04 +00001507 //===--------------------------------------------------------------------===//
Chris Lattner074be1f2004-11-15 04:44:20 +00001508 //
Chris Lattner1890f942004-11-15 07:15:04 +00001509 /// SCCP Class - This class uses the SCCPSolver to implement a per-function
Reid Spencere8a74ee2006-12-31 22:26:06 +00001510 /// Sparse Conditional Constant Propagator.
Chris Lattner1890f942004-11-15 07:15:04 +00001511 ///
Chris Lattner2dd09db2009-09-02 06:11:42 +00001512 struct SCCP : public FunctionPass {
Craig Topper3e4c6972014-03-05 09:10:37 +00001513 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001514 AU.addRequired<TargetLibraryInfoWrapperPass>();
James Molloyefbba722015-09-10 10:22:12 +00001515 AU.addPreserved<GlobalsAAWrapperPass>();
Chad Rosiere6de63d2011-12-01 21:29:16 +00001516 }
Nick Lewyckye7da2d62007-05-06 13:37:16 +00001517 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +00001518 SCCP() : FunctionPass(ID) {
1519 initializeSCCPPass(*PassRegistry::getPassRegistry());
1520 }
Devang Patel09f162c2007-05-01 21:15:47 +00001521
Chris Lattner1890f942004-11-15 07:15:04 +00001522 // runOnFunction - Run the Sparse Conditional Constant Propagation
1523 // algorithm, and return true if the function was modified.
1524 //
Craig Topper3e4c6972014-03-05 09:10:37 +00001525 bool runOnFunction(Function &F) override;
Chris Lattner1890f942004-11-15 07:15:04 +00001526 };
Chris Lattner074be1f2004-11-15 04:44:20 +00001527} // end anonymous namespace
1528
Dan Gohmand78c4002008-05-13 00:00:25 +00001529char SCCP::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +00001530INITIALIZE_PASS(SCCP, "sccp",
Owen Andersondf7a4f22010-10-07 22:25:06 +00001531 "Sparse Conditional Constant Propagation", false, false)
Chris Lattner074be1f2004-11-15 04:44:20 +00001532
Chris Lattnera3c39d32009-11-02 02:33:50 +00001533// createSCCPPass - This is the public interface to this file.
Chris Lattner074be1f2004-11-15 04:44:20 +00001534FunctionPass *llvm::createSCCPPass() {
1535 return new SCCP();
1536}
1537
Chris Lattnere405ed92009-11-02 02:47:51 +00001538static void DeleteInstructionInBlock(BasicBlock *BB) {
David Greene389fc3b2010-01-05 01:27:15 +00001539 DEBUG(dbgs() << " BasicBlock Dead:" << *BB);
Chris Lattnere405ed92009-11-02 02:47:51 +00001540 ++NumDeadBlocks;
Bill Wendling770d0f02011-08-31 20:55:20 +00001541
1542 // Check to see if there are non-terminating instructions to delete.
1543 if (isa<TerminatorInst>(BB->begin()))
1544 return;
1545
Bill Wendling321fb372011-09-04 09:43:36 +00001546 // Delete the instructions backwards, as it has a reduced likelihood of having
1547 // to update as many def-use and use-def chains.
1548 Instruction *EndInst = BB->getTerminator(); // Last not to be deleted.
1549 while (EndInst != BB->begin()) {
1550 // Delete the next to last instruction.
1551 BasicBlock::iterator I = EndInst;
1552 Instruction *Inst = --I;
Bill Wendlingbf8280f2011-09-01 21:28:33 +00001553 if (!Inst->use_empty())
1554 Inst->replaceAllUsesWith(UndefValue::get(Inst->getType()));
David Majnemereb518bd2015-08-04 08:21:40 +00001555 if (Inst->isEHPad()) {
Bill Wendling321fb372011-09-04 09:43:36 +00001556 EndInst = Inst;
Bill Wendling770d0f02011-08-31 20:55:20 +00001557 continue;
Bill Wendling321fb372011-09-04 09:43:36 +00001558 }
Bill Wendlingbf8280f2011-09-01 21:28:33 +00001559 BB->getInstList().erase(Inst);
Chris Lattnere405ed92009-11-02 02:47:51 +00001560 ++NumInstRemoved;
1561 }
1562}
Chris Lattner074be1f2004-11-15 04:44:20 +00001563
Chris Lattner074be1f2004-11-15 04:44:20 +00001564// runOnFunction() - Run the Sparse Conditional Constant Propagation algorithm,
1565// and return true if the function was modified.
1566//
1567bool SCCP::runOnFunction(Function &F) {
Paul Robinsonaf4e64d2014-02-06 00:07:05 +00001568 if (skipOptnoneFunction(F))
1569 return false;
1570
David Greene389fc3b2010-01-05 01:27:15 +00001571 DEBUG(dbgs() << "SCCP on function '" << F.getName() << "'\n");
Mehdi Amini46a43552015-03-04 18:43:29 +00001572 const DataLayout &DL = F.getParent()->getDataLayout();
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001573 const TargetLibraryInfo *TLI =
1574 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Rafael Espindola37dc9e12014-02-21 00:06:31 +00001575 SCCPSolver Solver(DL, TLI);
Chris Lattner074be1f2004-11-15 04:44:20 +00001576
1577 // Mark the first block of the function as being executable.
1578 Solver.MarkBlockExecutable(F.begin());
1579
Chris Lattnerd18c16b2004-11-15 05:45:33 +00001580 // Mark all arguments to the function as being overdefined.
Chris Lattner28d921d2007-04-14 23:32:02 +00001581 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E;++AI)
Chris Lattner156b8c72009-11-03 23:40:48 +00001582 Solver.markAnythingOverdefined(AI);
Chris Lattnerd18c16b2004-11-15 05:45:33 +00001583
Chris Lattner074be1f2004-11-15 04:44:20 +00001584 // Solve for constants.
Chris Lattner1847f6d2006-12-20 06:21:33 +00001585 bool ResolvedUndefs = true;
1586 while (ResolvedUndefs) {
Chris Lattner7285f432004-12-10 20:41:50 +00001587 Solver.Solve();
David Greene389fc3b2010-01-05 01:27:15 +00001588 DEBUG(dbgs() << "RESOLVING UNDEFs\n");
Chris Lattner1847f6d2006-12-20 06:21:33 +00001589 ResolvedUndefs = Solver.ResolvedUndefsIn(F);
Chris Lattner7285f432004-12-10 20:41:50 +00001590 }
Chris Lattner074be1f2004-11-15 04:44:20 +00001591
Chris Lattnerd18c16b2004-11-15 05:45:33 +00001592 bool MadeChanges = false;
1593
1594 // If we decided that there are basic blocks that are dead in this function,
1595 // delete their contents now. Note that we cannot actually delete the blocks,
1596 // as we cannot modify the CFG of the function.
Chris Lattnerc33fd462007-03-04 04:50:21 +00001597
Chris Lattnere405ed92009-11-02 02:47:51 +00001598 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
Chris Lattneradd44f32008-08-23 23:39:31 +00001599 if (!Solver.isBlockExecutable(BB)) {
Chris Lattnere405ed92009-11-02 02:47:51 +00001600 DeleteInstructionInBlock(BB);
1601 MadeChanges = true;
1602 continue;
Chris Lattner074be1f2004-11-15 04:44:20 +00001603 }
Jakub Staszak632a3552012-01-18 21:16:33 +00001604
Chris Lattnere405ed92009-11-02 02:47:51 +00001605 // Iterate over all of the instructions in a function, replacing them with
1606 // constants if we have found them to be of constant values.
1607 //
1608 for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
1609 Instruction *Inst = BI++;
1610 if (Inst->getType()->isVoidTy() || isa<TerminatorInst>(Inst))
1611 continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001612
Chris Lattner156b8c72009-11-03 23:40:48 +00001613 // TODO: Reconstruct structs from their elements.
Duncan Sands19d0b472010-02-16 11:11:14 +00001614 if (Inst->getType()->isStructTy())
Chris Lattner156b8c72009-11-03 23:40:48 +00001615 continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001616
Chris Lattnerb5a13d42009-11-02 02:54:24 +00001617 LatticeVal IV = Solver.getLatticeValueFor(Inst);
1618 if (IV.isOverdefined())
Chris Lattnere405ed92009-11-02 02:47:51 +00001619 continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001620
Chris Lattnere405ed92009-11-02 02:47:51 +00001621 Constant *Const = IV.isConstant()
1622 ? IV.getConstant() : UndefValue::get(Inst->getType());
Nick Lewycky5cd95382013-06-26 00:30:18 +00001623 DEBUG(dbgs() << " Constant: " << *Const << " = " << *Inst << '\n');
Chris Lattnere405ed92009-11-02 02:47:51 +00001624
1625 // Replaces all of the uses of a variable with uses of the constant.
1626 Inst->replaceAllUsesWith(Const);
Jakub Staszak632a3552012-01-18 21:16:33 +00001627
Chris Lattnere405ed92009-11-02 02:47:51 +00001628 // Delete the instruction.
1629 Inst->eraseFromParent();
Jakub Staszak632a3552012-01-18 21:16:33 +00001630
Chris Lattnere405ed92009-11-02 02:47:51 +00001631 // Hey, we just changed something!
1632 MadeChanges = true;
1633 ++NumInstRemoved;
1634 }
1635 }
Chris Lattner074be1f2004-11-15 04:44:20 +00001636
1637 return MadeChanges;
1638}
Chris Lattnerb4394642004-12-10 08:02:06 +00001639
1640namespace {
Chris Lattnerb4394642004-12-10 08:02:06 +00001641 //===--------------------------------------------------------------------===//
1642 //
1643 /// IPSCCP Class - This class implements interprocedural Sparse Conditional
1644 /// Constant Propagation.
1645 ///
Chris Lattner2dd09db2009-09-02 06:11:42 +00001646 struct IPSCCP : public ModulePass {
Craig Topper3e4c6972014-03-05 09:10:37 +00001647 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001648 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chad Rosiere6de63d2011-12-01 21:29:16 +00001649 }
Devang Patel8c78a0b2007-05-03 01:11:54 +00001650 static char ID;
Owen Anderson6c18d1a2010-10-19 17:21:58 +00001651 IPSCCP() : ModulePass(ID) {
1652 initializeIPSCCPPass(*PassRegistry::getPassRegistry());
1653 }
Craig Topper3e4c6972014-03-05 09:10:37 +00001654 bool runOnModule(Module &M) override;
Chris Lattnerb4394642004-12-10 08:02:06 +00001655 };
Chris Lattnerb4394642004-12-10 08:02:06 +00001656} // end anonymous namespace
1657
Dan Gohmand78c4002008-05-13 00:00:25 +00001658char IPSCCP::ID = 0;
Chad Rosiere6de63d2011-12-01 21:29:16 +00001659INITIALIZE_PASS_BEGIN(IPSCCP, "ipsccp",
1660 "Interprocedural Sparse Conditional Constant Propagation",
1661 false, false)
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001662INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Chad Rosiere6de63d2011-12-01 21:29:16 +00001663INITIALIZE_PASS_END(IPSCCP, "ipsccp",
Owen Andersona57b97e2010-07-21 22:09:45 +00001664 "Interprocedural Sparse Conditional Constant Propagation",
Owen Andersondf7a4f22010-10-07 22:25:06 +00001665 false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +00001666
Chris Lattnera3c39d32009-11-02 02:33:50 +00001667// createIPSCCPPass - This is the public interface to this file.
Chris Lattnerb4394642004-12-10 08:02:06 +00001668ModulePass *llvm::createIPSCCPPass() {
1669 return new IPSCCP();
1670}
1671
1672
Gabor Greif9027ffb2010-03-24 10:29:52 +00001673static bool AddressIsTaken(const GlobalValue *GV) {
Chris Lattner8cb10a12005-04-19 19:16:19 +00001674 // Delete any dead constantexpr klingons.
1675 GV->removeDeadConstantUsers();
1676
Chandler Carruthcdf47882014-03-09 03:16:01 +00001677 for (const Use &U : GV->uses()) {
1678 const User *UR = U.getUser();
1679 if (const StoreInst *SI = dyn_cast<StoreInst>(UR)) {
Chris Lattner91dbae62004-12-11 05:15:59 +00001680 if (SI->getOperand(0) == GV || SI->isVolatile())
1681 return true; // Storing addr of GV.
Chandler Carruthcdf47882014-03-09 03:16:01 +00001682 } else if (isa<InvokeInst>(UR) || isa<CallInst>(UR)) {
Chris Lattnerb4394642004-12-10 08:02:06 +00001683 // Make sure we are calling the function, not passing the address.
Chandler Carruthcdf47882014-03-09 03:16:01 +00001684 ImmutableCallSite CS(cast<Instruction>(UR));
1685 if (!CS.isCallee(&U))
Nick Lewyckyd73806a2008-11-03 03:49:14 +00001686 return true;
Chandler Carruthcdf47882014-03-09 03:16:01 +00001687 } else if (const LoadInst *LI = dyn_cast<LoadInst>(UR)) {
Chris Lattner91dbae62004-12-11 05:15:59 +00001688 if (LI->isVolatile())
1689 return true;
Chandler Carruthcdf47882014-03-09 03:16:01 +00001690 } else if (isa<BlockAddress>(UR)) {
Chris Lattner1a8b80e2009-11-01 06:11:53 +00001691 // blockaddress doesn't take the address of the function, it takes addr
1692 // of label.
Chris Lattner91dbae62004-12-11 05:15:59 +00001693 } else {
Chris Lattnerb4394642004-12-10 08:02:06 +00001694 return true;
1695 }
Gabor Greif9027ffb2010-03-24 10:29:52 +00001696 }
Chris Lattnerb4394642004-12-10 08:02:06 +00001697 return false;
1698}
1699
1700bool IPSCCP::runOnModule(Module &M) {
Mehdi Amini46a43552015-03-04 18:43:29 +00001701 const DataLayout &DL = M.getDataLayout();
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001702 const TargetLibraryInfo *TLI =
1703 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Rafael Espindola37dc9e12014-02-21 00:06:31 +00001704 SCCPSolver Solver(DL, TLI);
Chris Lattnerb4394642004-12-10 08:02:06 +00001705
Chris Lattner363226d2010-08-12 22:25:23 +00001706 // AddressTakenFunctions - This set keeps track of the address-taken functions
1707 // that are in the input. As IPSCCP runs through and simplifies code,
1708 // functions that were address taken can end up losing their
1709 // address-taken-ness. Because of this, we keep track of their addresses from
1710 // the first pass so we can use them for the later simplification pass.
1711 SmallPtrSet<Function*, 32> AddressTakenFunctions;
Jakub Staszak632a3552012-01-18 21:16:33 +00001712
Chris Lattnerb4394642004-12-10 08:02:06 +00001713 // Loop over all functions, marking arguments to those with their addresses
1714 // taken or that are external as overdefined.
1715 //
Chris Lattner47837c52009-11-02 06:34:04 +00001716 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
1717 if (F->isDeclaration())
1718 continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001719
Chris Lattnerfb141812009-11-03 03:42:51 +00001720 // If this is a strong or ODR definition of this function, then we can
1721 // propagate information about its result into callsites of it.
Chris Lattner156b8c72009-11-03 23:40:48 +00001722 if (!F->mayBeOverridden())
Chris Lattnerb4394642004-12-10 08:02:06 +00001723 Solver.AddTrackedFunction(F);
Jakub Staszak632a3552012-01-18 21:16:33 +00001724
Chris Lattnerfb141812009-11-03 03:42:51 +00001725 // If this function only has direct calls that we can see, we can track its
1726 // arguments and return value aggressively, and can assume it is not called
1727 // unless we see evidence to the contrary.
Chris Lattner363226d2010-08-12 22:25:23 +00001728 if (F->hasLocalLinkage()) {
1729 if (AddressIsTaken(F))
1730 AddressTakenFunctions.insert(F);
1731 else {
1732 Solver.AddArgumentTrackedFunction(F);
1733 continue;
1734 }
Chris Lattnercde8de52009-11-03 19:24:51 +00001735 }
Chris Lattnerfb141812009-11-03 03:42:51 +00001736
1737 // Assume the function is called.
1738 Solver.MarkBlockExecutable(F->begin());
Jakub Staszak632a3552012-01-18 21:16:33 +00001739
Chris Lattnerfb141812009-11-03 03:42:51 +00001740 // Assume nothing about the incoming arguments.
1741 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1742 AI != E; ++AI)
Chris Lattner156b8c72009-11-03 23:40:48 +00001743 Solver.markAnythingOverdefined(AI);
Chris Lattner47837c52009-11-02 06:34:04 +00001744 }
Chris Lattnerb4394642004-12-10 08:02:06 +00001745
Chris Lattner91dbae62004-12-11 05:15:59 +00001746 // Loop over global variables. We inform the solver about any internal global
1747 // variables that do not have their 'addresses taken'. If they don't have
1748 // their addresses taken, we can propagate constants through them.
Chris Lattner8cb10a12005-04-19 19:16:19 +00001749 for (Module::global_iterator G = M.global_begin(), E = M.global_end();
1750 G != E; ++G)
Rafael Espindola6de96a12009-01-15 20:18:42 +00001751 if (!G->isConstant() && G->hasLocalLinkage() && !AddressIsTaken(G))
Chris Lattner91dbae62004-12-11 05:15:59 +00001752 Solver.TrackValueOfGlobalVariable(G);
1753
Chris Lattnerb4394642004-12-10 08:02:06 +00001754 // Solve for constants.
Chris Lattner1847f6d2006-12-20 06:21:33 +00001755 bool ResolvedUndefs = true;
1756 while (ResolvedUndefs) {
Chris Lattner7285f432004-12-10 20:41:50 +00001757 Solver.Solve();
1758
David Greene389fc3b2010-01-05 01:27:15 +00001759 DEBUG(dbgs() << "RESOLVING UNDEFS\n");
Chris Lattner1847f6d2006-12-20 06:21:33 +00001760 ResolvedUndefs = false;
Chris Lattner7285f432004-12-10 20:41:50 +00001761 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
Chris Lattner1847f6d2006-12-20 06:21:33 +00001762 ResolvedUndefs |= Solver.ResolvedUndefsIn(*F);
Chris Lattner7285f432004-12-10 20:41:50 +00001763 }
Chris Lattnerb4394642004-12-10 08:02:06 +00001764
1765 bool MadeChanges = false;
1766
1767 // Iterate over all of the instructions in the module, replacing them with
1768 // constants if we have found them to be of constant values.
1769 //
Chris Lattner65938fc2008-08-23 23:36:38 +00001770 SmallVector<BasicBlock*, 512> BlocksToErase;
Chris Lattner37d400a2007-02-02 21:15:06 +00001771
Chris Lattnerb4394642004-12-10 08:02:06 +00001772 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
Chris Lattnere82b0872009-11-02 03:25:55 +00001773 if (Solver.isBlockExecutable(F->begin())) {
1774 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1775 AI != E; ++AI) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001776 if (AI->use_empty() || AI->getType()->isStructTy()) continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001777
Chris Lattner156b8c72009-11-03 23:40:48 +00001778 // TODO: Could use getStructLatticeValueFor to find out if the entire
1779 // result is a constant and replace it entirely if so.
1780
Chris Lattnere82b0872009-11-02 03:25:55 +00001781 LatticeVal IV = Solver.getLatticeValueFor(AI);
1782 if (IV.isOverdefined()) continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001783
Chris Lattnere82b0872009-11-02 03:25:55 +00001784 Constant *CST = IV.isConstant() ?
1785 IV.getConstant() : UndefValue::get(AI->getType());
David Greene389fc3b2010-01-05 01:27:15 +00001786 DEBUG(dbgs() << "*** Arg " << *AI << " = " << *CST <<"\n");
Jakub Staszak632a3552012-01-18 21:16:33 +00001787
Chris Lattnere82b0872009-11-02 03:25:55 +00001788 // Replaces all of the uses of a variable with uses of the
1789 // constant.
1790 AI->replaceAllUsesWith(CST);
1791 ++IPNumArgsElimed;
1792 }
Chris Lattnerb5a13d42009-11-02 02:54:24 +00001793 }
Chris Lattnerb4394642004-12-10 08:02:06 +00001794
Chris Lattnere405ed92009-11-02 02:47:51 +00001795 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Chris Lattneradd44f32008-08-23 23:39:31 +00001796 if (!Solver.isBlockExecutable(BB)) {
Chris Lattnere405ed92009-11-02 02:47:51 +00001797 DeleteInstructionInBlock(BB);
1798 MadeChanges = true;
Chris Lattner7285f432004-12-10 20:41:50 +00001799
Chris Lattnerbae4b642004-12-10 22:29:08 +00001800 TerminatorInst *TI = BB->getTerminator();
Pete Cooperebcd7482015-08-06 20:22:46 +00001801 for (BasicBlock *Succ : TI->successors()) {
Dan Gohmanc731c972007-10-03 19:26:29 +00001802 if (!Succ->empty() && isa<PHINode>(Succ->begin()))
Pete Cooperebcd7482015-08-06 20:22:46 +00001803 Succ->removePredecessor(BB);
Chris Lattnerbae4b642004-12-10 22:29:08 +00001804 }
Chris Lattner99e12952004-12-11 02:53:57 +00001805 if (!TI->use_empty())
Owen Andersonb292b8c2009-07-30 23:03:37 +00001806 TI->replaceAllUsesWith(UndefValue::get(TI->getType()));
Chris Lattnere405ed92009-11-02 02:47:51 +00001807 TI->eraseFromParent();
David Majnemer6cc21f92015-07-07 18:49:41 +00001808 new UnreachableInst(M.getContext(), BB);
Chris Lattnerbae4b642004-12-10 22:29:08 +00001809
Chris Lattner8525ebe2004-12-11 05:32:19 +00001810 if (&*BB != &F->front())
1811 BlocksToErase.push_back(BB);
Chris Lattnere405ed92009-11-02 02:47:51 +00001812 continue;
Chris Lattnerb4394642004-12-10 08:02:06 +00001813 }
Jakub Staszak632a3552012-01-18 21:16:33 +00001814
Chris Lattnere405ed92009-11-02 02:47:51 +00001815 for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
1816 Instruction *Inst = BI++;
Duncan Sands19d0b472010-02-16 11:11:14 +00001817 if (Inst->getType()->isVoidTy() || Inst->getType()->isStructTy())
Chris Lattnere405ed92009-11-02 02:47:51 +00001818 continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001819
Chris Lattner156b8c72009-11-03 23:40:48 +00001820 // TODO: Could use getStructLatticeValueFor to find out if the entire
1821 // result is a constant and replace it entirely if so.
Jakub Staszak632a3552012-01-18 21:16:33 +00001822
Chris Lattnerb5a13d42009-11-02 02:54:24 +00001823 LatticeVal IV = Solver.getLatticeValueFor(Inst);
1824 if (IV.isOverdefined())
Chris Lattnere405ed92009-11-02 02:47:51 +00001825 continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001826
Chris Lattnere405ed92009-11-02 02:47:51 +00001827 Constant *Const = IV.isConstant()
1828 ? IV.getConstant() : UndefValue::get(Inst->getType());
Nick Lewycky5cd95382013-06-26 00:30:18 +00001829 DEBUG(dbgs() << " Constant: " << *Const << " = " << *Inst << '\n');
Chris Lattnere405ed92009-11-02 02:47:51 +00001830
1831 // Replaces all of the uses of a variable with uses of the
1832 // constant.
1833 Inst->replaceAllUsesWith(Const);
Jakub Staszak632a3552012-01-18 21:16:33 +00001834
Chris Lattnere405ed92009-11-02 02:47:51 +00001835 // Delete the instruction.
1836 if (!isa<CallInst>(Inst) && !isa<TerminatorInst>(Inst))
1837 Inst->eraseFromParent();
1838
1839 // Hey, we just changed something!
1840 MadeChanges = true;
1841 ++IPNumInstRemoved;
1842 }
1843 }
Chris Lattnerbae4b642004-12-10 22:29:08 +00001844
1845 // Now that all instructions in the function are constant folded, erase dead
1846 // blocks, because we can now use ConstantFoldTerminator to get rid of
1847 // in-edges.
1848 for (unsigned i = 0, e = BlocksToErase.size(); i != e; ++i) {
1849 // If there are any PHI nodes in this successor, drop entries for BB now.
1850 BasicBlock *DeadBB = BlocksToErase[i];
Chandler Carruthcdf47882014-03-09 03:16:01 +00001851 for (Value::user_iterator UI = DeadBB->user_begin(),
1852 UE = DeadBB->user_end();
1853 UI != UE;) {
Dan Gohman1f522d92009-11-23 16:13:39 +00001854 // Grab the user and then increment the iterator early, as the user
1855 // will be deleted. Step past all adjacent uses from the same user.
1856 Instruction *I = dyn_cast<Instruction>(*UI);
1857 do { ++UI; } while (UI != UE && *UI == I);
1858
Dan Gohmand15302a2009-11-20 20:19:14 +00001859 // Ignore blockaddress users; BasicBlock's dtor will handle them.
Dan Gohmand15302a2009-11-20 20:19:14 +00001860 if (!I) continue;
1861
Chris Lattnerbae4b642004-12-10 22:29:08 +00001862 bool Folded = ConstantFoldTerminator(I->getParent());
Chris Lattnerfe7b6ef2006-10-23 18:57:02 +00001863 if (!Folded) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001864 // The constant folder may not have been able to fold the terminator
Chris Lattnerfe7b6ef2006-10-23 18:57:02 +00001865 // if this is a branch or switch on undef. Fold it manually as a
1866 // branch to the first successor.
Devang Patel45f1ae02008-11-21 01:52:59 +00001867#ifndef NDEBUG
Chris Lattnerfe7b6ef2006-10-23 18:57:02 +00001868 if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
1869 assert(BI->isConditional() && isa<UndefValue>(BI->getCondition()) &&
1870 "Branch should be foldable!");
1871 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
1872 assert(isa<UndefValue>(SI->getCondition()) && "Switch should fold");
1873 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001874 llvm_unreachable("Didn't fold away reference to block!");
Chris Lattnerfe7b6ef2006-10-23 18:57:02 +00001875 }
Devang Patel45f1ae02008-11-21 01:52:59 +00001876#endif
Jakub Staszak632a3552012-01-18 21:16:33 +00001877
Chris Lattnerfe7b6ef2006-10-23 18:57:02 +00001878 // Make this an uncond branch to the first successor.
1879 TerminatorInst *TI = I->getParent()->getTerminator();
Gabor Greife9ecc682008-04-06 20:25:17 +00001880 BranchInst::Create(TI->getSuccessor(0), TI);
Jakub Staszak632a3552012-01-18 21:16:33 +00001881
Chris Lattnerfe7b6ef2006-10-23 18:57:02 +00001882 // Remove entries in successor phi nodes to remove edges.
1883 for (unsigned i = 1, e = TI->getNumSuccessors(); i != e; ++i)
1884 TI->getSuccessor(i)->removePredecessor(TI->getParent());
Jakub Staszak632a3552012-01-18 21:16:33 +00001885
Chris Lattnerfe7b6ef2006-10-23 18:57:02 +00001886 // Remove the old terminator.
1887 TI->eraseFromParent();
1888 }
Chris Lattnerbae4b642004-12-10 22:29:08 +00001889 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001890
Chris Lattnerbae4b642004-12-10 22:29:08 +00001891 // Finally, delete the basic block.
1892 F->getBasicBlockList().erase(DeadBB);
1893 }
Chris Lattner37d400a2007-02-02 21:15:06 +00001894 BlocksToErase.clear();
Chris Lattnerb4394642004-12-10 08:02:06 +00001895 }
Chris Lattner99e12952004-12-11 02:53:57 +00001896
1897 // If we inferred constant or undef return values for a function, we replaced
1898 // all call uses with the inferred value. This means we don't need to bother
1899 // actually returning anything from the function. Replace all return
1900 // instructions with return undef.
Chris Lattnerd887f1d2010-02-27 00:07:42 +00001901 //
1902 // Do this in two stages: first identify the functions we should process, then
1903 // actually zap their returns. This is important because we can only do this
Chris Lattner2af7e3d2010-02-27 07:50:40 +00001904 // if the address of the function isn't taken. In cases where a return is the
Chris Lattnerd887f1d2010-02-27 00:07:42 +00001905 // last use of a function, the order of processing functions would affect
Chris Lattner2af7e3d2010-02-27 07:50:40 +00001906 // whether other functions are optimizable.
Chris Lattnerd887f1d2010-02-27 00:07:42 +00001907 SmallVector<ReturnInst*, 8> ReturnsToZap;
Jakub Staszak632a3552012-01-18 21:16:33 +00001908
Devang Patele418de32008-03-11 17:32:05 +00001909 // TODO: Process multiple value ret instructions also.
Devang Patela7a20752008-03-11 05:46:42 +00001910 const DenseMap<Function*, LatticeVal> &RV = Solver.getTrackedRetVals();
Chris Lattner067d6072007-02-02 20:38:30 +00001911 for (DenseMap<Function*, LatticeVal>::const_iterator I = RV.begin(),
Chris Lattnerfb141812009-11-03 03:42:51 +00001912 E = RV.end(); I != E; ++I) {
1913 Function *F = I->first;
1914 if (I->second.isOverdefined() || F->getReturnType()->isVoidTy())
1915 continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001916
Chris Lattnerfb141812009-11-03 03:42:51 +00001917 // We can only do this if we know that nothing else can call the function.
Chris Lattner363226d2010-08-12 22:25:23 +00001918 if (!F->hasLocalLinkage() || AddressTakenFunctions.count(F))
Chris Lattnerfb141812009-11-03 03:42:51 +00001919 continue;
Jakub Staszak632a3552012-01-18 21:16:33 +00001920
Chris Lattnerfb141812009-11-03 03:42:51 +00001921 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
1922 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))
1923 if (!isa<UndefValue>(RI->getOperand(0)))
Chris Lattnerd887f1d2010-02-27 00:07:42 +00001924 ReturnsToZap.push_back(RI);
1925 }
1926
1927 // Zap all returns which we've identified as zap to change.
1928 for (unsigned i = 0, e = ReturnsToZap.size(); i != e; ++i) {
1929 Function *F = ReturnsToZap[i]->getParent()->getParent();
1930 ReturnsToZap[i]->setOperand(0, UndefValue::get(F->getReturnType()));
Chris Lattnerfb141812009-11-03 03:42:51 +00001931 }
Jakub Staszak632a3552012-01-18 21:16:33 +00001932
Chad Rosierbb2a6da2012-03-28 00:35:33 +00001933 // If we inferred constant or undef values for globals variables, we can
1934 // delete the global and any stores that remain to it.
Chris Lattner067d6072007-02-02 20:38:30 +00001935 const DenseMap<GlobalVariable*, LatticeVal> &TG = Solver.getTrackedGlobals();
1936 for (DenseMap<GlobalVariable*, LatticeVal>::const_iterator I = TG.begin(),
Chris Lattner91dbae62004-12-11 05:15:59 +00001937 E = TG.end(); I != E; ++I) {
1938 GlobalVariable *GV = I->first;
1939 assert(!I->second.isOverdefined() &&
1940 "Overdefined values should have been taken out of the map!");
David Greene389fc3b2010-01-05 01:27:15 +00001941 DEBUG(dbgs() << "Found that GV '" << GV->getName() << "' is constant!\n");
Chris Lattner91dbae62004-12-11 05:15:59 +00001942 while (!GV->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001943 StoreInst *SI = cast<StoreInst>(GV->user_back());
Chris Lattner91dbae62004-12-11 05:15:59 +00001944 SI->eraseFromParent();
1945 }
1946 M.getGlobalList().erase(GV);
Chris Lattner2f687fd2004-12-11 06:05:53 +00001947 ++IPNumGlobalConst;
Chris Lattner91dbae62004-12-11 05:15:59 +00001948 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001949
Chris Lattnerb4394642004-12-10 08:02:06 +00001950 return MadeChanges;
1951}