blob: 7363c71f7c3c21be997d2c43e139b855297564a8 [file] [log] [blame]
Misha Brukman82c89b92003-05-20 21:01:22 +00001//===- SCCP.cpp - Sparse Conditional Constant Propagation -----------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner138a1242001-06-27 23:38:11 +00009//
Misha Brukman82c89b92003-05-20 21:01:22 +000010// This file implements sparse conditional constant propagation and merging:
Chris Lattner138a1242001-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 Lattner2a88bb72002-08-30 23:39:00 +000016// * Proves conditional branches to be unconditional
Chris Lattner138a1242001-06-27 23:38:11 +000017//
18// Notice that:
19// * This pass has a habit of making definitions be dead. It is a good idea
20// to to run a DCE pass sometime after running this pass.
21//
22//===----------------------------------------------------------------------===//
23
Chris Lattneref36dfd2004-11-15 05:03:30 +000024#define DEBUG_TYPE "sccp"
Chris Lattner022103b2002-05-07 20:03:00 +000025#include "llvm/Transforms/Scalar.h"
Chris Lattner59acc7d2004-12-10 08:02:06 +000026#include "llvm/Transforms/IPO.h"
Chris Lattnerb7a5d3e2004-01-12 17:43:40 +000027#include "llvm/Constants.h"
Chris Lattnerdd336d12004-12-11 05:15:59 +000028#include "llvm/DerivedTypes.h"
Chris Lattner9de28282003-04-25 02:50:03 +000029#include "llvm/Instructions.h"
Owen Andersonfa5cbd62009-07-03 19:42:02 +000030#include "llvm/LLVMContext.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000031#include "llvm/Pass.h"
Chris Lattner79066fa2007-01-30 23:46:24 +000032#include "llvm/Analysis/ConstantFolding.h"
Dan Gohmanc4b65ea2008-06-20 01:15:44 +000033#include "llvm/Analysis/ValueTracking.h"
Chris Lattner58b7b082004-04-13 19:43:54 +000034#include "llvm/Transforms/Utils/Local.h"
Chris Lattner59acc7d2004-12-10 08:02:06 +000035#include "llvm/Support/CallSite.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000036#include "llvm/Support/Compiler.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000037#include "llvm/Support/Debug.h"
Chris Lattner79066fa2007-01-30 23:46:24 +000038#include "llvm/Support/InstVisitor.h"
Chris Lattnerb59673e2007-02-02 20:38:30 +000039#include "llvm/ADT/DenseMap.h"
Chris Lattnercf712de2008-08-23 23:36:38 +000040#include "llvm/ADT/DenseSet.h"
Chris Lattnercc56aad2007-02-02 20:57:39 +000041#include "llvm/ADT/SmallSet.h"
Chris Lattnercd2492e2007-01-30 23:15:19 +000042#include "llvm/ADT/SmallVector.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000043#include "llvm/ADT/Statistic.h"
44#include "llvm/ADT/STLExtras.h"
Chris Lattner138a1242001-06-27 23:38:11 +000045#include <algorithm>
Dan Gohmanc9235d22008-03-21 23:51:57 +000046#include <map>
Chris Lattnerd7456022004-01-09 06:02:20 +000047using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000048
Chris Lattner0e5f4992006-12-19 21:40:18 +000049STATISTIC(NumInstRemoved, "Number of instructions removed");
50STATISTIC(NumDeadBlocks , "Number of basic blocks unreachable");
51
Nick Lewycky6c36a0f2008-03-08 07:48:41 +000052STATISTIC(IPNumInstRemoved, "Number of instructions removed by IPSCCP");
Chris Lattner0e5f4992006-12-19 21:40:18 +000053STATISTIC(IPNumDeadBlocks , "Number of basic blocks unreachable by IPSCCP");
54STATISTIC(IPNumArgsElimed ,"Number of arguments constant propagated by IPSCCP");
55STATISTIC(IPNumGlobalConst, "Number of globals found to be constant by IPSCCP");
56
Chris Lattner0dbfc052002-04-29 21:26:08 +000057namespace {
Chris Lattner3bad2532006-12-20 06:21:33 +000058/// LatticeVal class - This class represents the different lattice values that
59/// an LLVM value may occupy. It is a simple class with value semantics.
60///
Reid Spencer9133fe22007-02-05 23:32:05 +000061class VISIBILITY_HIDDEN LatticeVal {
Misha Brukmanfd939082005-04-21 23:48:37 +000062 enum {
Chris Lattner3bad2532006-12-20 06:21:33 +000063 /// undefined - This LLVM Value has no known value yet.
64 undefined,
65
66 /// constant - This LLVM Value has a specific constant value.
67 constant,
68
69 /// forcedconstant - This LLVM Value was thought to be undef until
70 /// ResolvedUndefsIn. This is treated just like 'constant', but if merged
71 /// with another (different) constant, it goes to overdefined, instead of
72 /// asserting.
73 forcedconstant,
74
75 /// overdefined - This instruction is not known to be constant, and we know
76 /// it has a value.
77 overdefined
78 } LatticeValue; // The current lattice position
79
Chris Lattnere9bb2df2001-12-03 22:26:30 +000080 Constant *ConstantVal; // If Constant value, the current value
Chris Lattner138a1242001-06-27 23:38:11 +000081public:
Chris Lattneref36dfd2004-11-15 05:03:30 +000082 inline LatticeVal() : LatticeValue(undefined), ConstantVal(0) {}
Chris Lattner3bad2532006-12-20 06:21:33 +000083
Chris Lattner138a1242001-06-27 23:38:11 +000084 // markOverdefined - Return true if this is a new status to be in...
85 inline bool markOverdefined() {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000086 if (LatticeValue != overdefined) {
87 LatticeValue = overdefined;
Chris Lattner138a1242001-06-27 23:38:11 +000088 return true;
89 }
90 return false;
91 }
92
Chris Lattner3bad2532006-12-20 06:21:33 +000093 // markConstant - Return true if this is a new status for us.
Chris Lattnere9bb2df2001-12-03 22:26:30 +000094 inline bool markConstant(Constant *V) {
95 if (LatticeValue != constant) {
Chris Lattner3bad2532006-12-20 06:21:33 +000096 if (LatticeValue == undefined) {
97 LatticeValue = constant;
Jim Laskey52ab9042007-01-03 00:11:03 +000098 assert(V && "Marking constant with NULL");
Chris Lattner3bad2532006-12-20 06:21:33 +000099 ConstantVal = V;
100 } else {
101 assert(LatticeValue == forcedconstant &&
102 "Cannot move from overdefined to constant!");
103 // Stay at forcedconstant if the constant is the same.
104 if (V == ConstantVal) return false;
105
106 // Otherwise, we go to overdefined. Assumptions made based on the
107 // forced value are possibly wrong. Assuming this is another constant
108 // could expose a contradiction.
109 LatticeValue = overdefined;
110 }
Chris Lattner138a1242001-06-27 23:38:11 +0000111 return true;
112 } else {
Chris Lattnerb70d82f2001-09-07 16:43:22 +0000113 assert(ConstantVal == V && "Marking constant with different value");
Chris Lattner138a1242001-06-27 23:38:11 +0000114 }
115 return false;
116 }
117
Chris Lattner3bad2532006-12-20 06:21:33 +0000118 inline void markForcedConstant(Constant *V) {
119 assert(LatticeValue == undefined && "Can't force a defined value!");
120 LatticeValue = forcedconstant;
121 ConstantVal = V;
122 }
123
124 inline bool isUndefined() const { return LatticeValue == undefined; }
125 inline bool isConstant() const {
126 return LatticeValue == constant || LatticeValue == forcedconstant;
127 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000128 inline bool isOverdefined() const { return LatticeValue == overdefined; }
Chris Lattner138a1242001-06-27 23:38:11 +0000129
Chris Lattner1daee8b2004-01-12 03:57:30 +0000130 inline Constant *getConstant() const {
131 assert(isConstant() && "Cannot get the constant of a non-constant!");
132 return ConstantVal;
133 }
Chris Lattner138a1242001-06-27 23:38:11 +0000134};
135
Chris Lattner138a1242001-06-27 23:38:11 +0000136//===----------------------------------------------------------------------===//
Chris Lattner138a1242001-06-27 23:38:11 +0000137//
Chris Lattner82bec2c2004-11-15 04:44:20 +0000138/// SCCPSolver - This class is a general purpose solver for Sparse Conditional
139/// Constant Propagation.
140///
141class SCCPSolver : public InstVisitor<SCCPSolver> {
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000142 LLVMContext* Context;
Chris Lattnercf712de2008-08-23 23:36:38 +0000143 DenseSet<BasicBlock*> BBExecutable;// The basic blocks that are executable
Bill Wendling7a7cf6b2008-08-14 23:05:24 +0000144 std::map<Value*, LatticeVal> ValueState; // The state each value is in.
Chris Lattner138a1242001-06-27 23:38:11 +0000145
Chris Lattnerdd336d12004-12-11 05:15:59 +0000146 /// GlobalValue - If we are tracking any values for the contents of a global
147 /// variable, we keep a mapping from the constant accessor to the element of
148 /// the global, to the currently known value. If the value becomes
149 /// overdefined, it's entry is simply removed from this map.
Chris Lattnerb59673e2007-02-02 20:38:30 +0000150 DenseMap<GlobalVariable*, LatticeVal> TrackedGlobals;
Chris Lattnerdd336d12004-12-11 05:15:59 +0000151
Devang Patel7c490d42008-03-11 05:46:42 +0000152 /// TrackedRetVals - If we are tracking arguments into and the return
Chris Lattner59acc7d2004-12-10 08:02:06 +0000153 /// value out of a function, it will have an entry in this map, indicating
154 /// what the known return value for the function is.
Devang Patel7c490d42008-03-11 05:46:42 +0000155 DenseMap<Function*, LatticeVal> TrackedRetVals;
156
157 /// TrackedMultipleRetVals - Same as TrackedRetVals, but used for functions
158 /// that return multiple values.
Chris Lattnercf712de2008-08-23 23:36:38 +0000159 DenseMap<std::pair<Function*, unsigned>, LatticeVal> TrackedMultipleRetVals;
Chris Lattner59acc7d2004-12-10 08:02:06 +0000160
Chris Lattner80b2d6c2004-07-15 23:36:43 +0000161 // The reason for two worklists is that overdefined is the lowest state
162 // on the lattice, and moving things to overdefined as fast as possible
163 // makes SCCP converge much faster.
164 // By having a separate worklist, we accomplish this because everything
165 // possibly overdefined will become overdefined at the soonest possible
166 // point.
Chris Lattnercf712de2008-08-23 23:36:38 +0000167 SmallVector<Value*, 64> OverdefinedInstWorkList;
168 SmallVector<Value*, 64> InstWorkList;
Chris Lattner80b2d6c2004-07-15 23:36:43 +0000169
170
Chris Lattnercf712de2008-08-23 23:36:38 +0000171 SmallVector<BasicBlock*, 64> BBWorkList; // The BasicBlock work list
Chris Lattner16b18fd2003-10-08 16:55:34 +0000172
Chris Lattner1daee8b2004-01-12 03:57:30 +0000173 /// UsersOfOverdefinedPHIs - Keep track of any users of PHI nodes that are not
174 /// overdefined, despite the fact that the PHI node is overdefined.
175 std::multimap<PHINode*, Instruction*> UsersOfOverdefinedPHIs;
176
Chris Lattner16b18fd2003-10-08 16:55:34 +0000177 /// KnownFeasibleEdges - Entries in this set are edges which have already had
178 /// PHI nodes retriggered.
Chris Lattnercf712de2008-08-23 23:36:38 +0000179 typedef std::pair<BasicBlock*, BasicBlock*> Edge;
180 DenseSet<Edge> KnownFeasibleEdges;
Chris Lattner138a1242001-06-27 23:38:11 +0000181public:
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000182 void setContext(LLVMContext* C) { Context = C; }
Chris Lattner138a1242001-06-27 23:38:11 +0000183
Chris Lattner82bec2c2004-11-15 04:44:20 +0000184 /// MarkBlockExecutable - This method can be used by clients to mark all of
185 /// the blocks that are known to be intrinsically live in the processed unit.
186 void MarkBlockExecutable(BasicBlock *BB) {
Chris Lattner5c8e8d72008-05-11 01:55:59 +0000187 DOUT << "Marking Block Executable: " << BB->getNameStart() << "\n";
Chris Lattner82bec2c2004-11-15 04:44:20 +0000188 BBExecutable.insert(BB); // Basic block is executable!
189 BBWorkList.push_back(BB); // Add the block to the work list!
Chris Lattner0dbfc052002-04-29 21:26:08 +0000190 }
191
Chris Lattnerdd336d12004-12-11 05:15:59 +0000192 /// TrackValueOfGlobalVariable - Clients can use this method to
Chris Lattner59acc7d2004-12-10 08:02:06 +0000193 /// inform the SCCPSolver that it should track loads and stores to the
194 /// specified global variable if it can. This is only legal to call if
195 /// performing Interprocedural SCCP.
Chris Lattnerdd336d12004-12-11 05:15:59 +0000196 void TrackValueOfGlobalVariable(GlobalVariable *GV) {
197 const Type *ElTy = GV->getType()->getElementType();
198 if (ElTy->isFirstClassType()) {
199 LatticeVal &IV = TrackedGlobals[GV];
200 if (!isa<UndefValue>(GV->getInitializer()))
201 IV.markConstant(GV->getInitializer());
202 }
203 }
Chris Lattner59acc7d2004-12-10 08:02:06 +0000204
205 /// AddTrackedFunction - If the SCCP solver is supposed to track calls into
206 /// and out of the specified function (which cannot have its address taken),
207 /// this method must be called.
208 void AddTrackedFunction(Function *F) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000209 assert(F->hasLocalLinkage() && "Can only track internal functions!");
Chris Lattner59acc7d2004-12-10 08:02:06 +0000210 // Add an entry, F -> undef.
Devang Patel7c490d42008-03-11 05:46:42 +0000211 if (const StructType *STy = dyn_cast<StructType>(F->getReturnType())) {
212 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Chris Lattnerc6ee00b2008-04-23 05:38:20 +0000213 TrackedMultipleRetVals.insert(std::make_pair(std::make_pair(F, i),
214 LatticeVal()));
215 } else
216 TrackedRetVals.insert(std::make_pair(F, LatticeVal()));
Chris Lattner59acc7d2004-12-10 08:02:06 +0000217 }
218
Chris Lattner82bec2c2004-11-15 04:44:20 +0000219 /// Solve - Solve for constants and executable blocks.
220 ///
221 void Solve();
Chris Lattner138a1242001-06-27 23:38:11 +0000222
Chris Lattner3bad2532006-12-20 06:21:33 +0000223 /// ResolvedUndefsIn - While solving the dataflow for a function, we assume
Chris Lattnerfc6ac502004-12-10 20:41:50 +0000224 /// that branches on undef values cannot reach any of their successors.
225 /// However, this is not a safe assumption. After we solve dataflow, this
226 /// method should be use to handle this. If this returns true, the solver
227 /// should be rerun.
Chris Lattner3bad2532006-12-20 06:21:33 +0000228 bool ResolvedUndefsIn(Function &F);
Chris Lattnerfc6ac502004-12-10 20:41:50 +0000229
Chris Lattner7eb01bf2008-08-23 23:39:31 +0000230 bool isBlockExecutable(BasicBlock *BB) const {
231 return BBExecutable.count(BB);
Chris Lattner82bec2c2004-11-15 04:44:20 +0000232 }
233
234 /// getValueMapping - Once we have solved for constants, return the mapping of
Chris Lattneref36dfd2004-11-15 05:03:30 +0000235 /// LLVM values to LatticeVals.
Bill Wendling7a7cf6b2008-08-14 23:05:24 +0000236 std::map<Value*, LatticeVal> &getValueMapping() {
Chris Lattner82bec2c2004-11-15 04:44:20 +0000237 return ValueState;
238 }
239
Devang Patel7c490d42008-03-11 05:46:42 +0000240 /// getTrackedRetVals - Get the inferred return value map.
Chris Lattner0417feb2004-12-11 02:53:57 +0000241 ///
Devang Patel7c490d42008-03-11 05:46:42 +0000242 const DenseMap<Function*, LatticeVal> &getTrackedRetVals() {
243 return TrackedRetVals;
Chris Lattner0417feb2004-12-11 02:53:57 +0000244 }
245
Chris Lattnerdd336d12004-12-11 05:15:59 +0000246 /// getTrackedGlobals - Get and return the set of inferred initializers for
247 /// global variables.
Chris Lattnerb59673e2007-02-02 20:38:30 +0000248 const DenseMap<GlobalVariable*, LatticeVal> &getTrackedGlobals() {
Chris Lattnerdd336d12004-12-11 05:15:59 +0000249 return TrackedGlobals;
250 }
251
Chris Lattner57939df2007-03-04 04:50:21 +0000252 inline void markOverdefined(Value *V) {
253 markOverdefined(ValueState[V], V);
254 }
Chris Lattner0417feb2004-12-11 02:53:57 +0000255
Chris Lattner138a1242001-06-27 23:38:11 +0000256private:
Chris Lattner80b2d6c2004-07-15 23:36:43 +0000257 // markConstant - Make a value be marked as "constant". If the value
Misha Brukmanfd939082005-04-21 23:48:37 +0000258 // is not already a constant, add it to the instruction work list so that
Chris Lattner138a1242001-06-27 23:38:11 +0000259 // the users of the instruction are updated later.
260 //
Chris Lattner59acc7d2004-12-10 08:02:06 +0000261 inline void markConstant(LatticeVal &IV, Value *V, Constant *C) {
Chris Lattner3d405b02003-10-08 16:21:03 +0000262 if (IV.markConstant(C)) {
Bill Wendlingb7427032006-11-26 09:46:52 +0000263 DOUT << "markConstant: " << *C << ": " << *V;
Chris Lattner59acc7d2004-12-10 08:02:06 +0000264 InstWorkList.push_back(V);
Chris Lattner138a1242001-06-27 23:38:11 +0000265 }
Chris Lattner3d405b02003-10-08 16:21:03 +0000266 }
Chris Lattner3bad2532006-12-20 06:21:33 +0000267
268 inline void markForcedConstant(LatticeVal &IV, Value *V, Constant *C) {
269 IV.markForcedConstant(C);
270 DOUT << "markForcedConstant: " << *C << ": " << *V;
271 InstWorkList.push_back(V);
272 }
273
Chris Lattner59acc7d2004-12-10 08:02:06 +0000274 inline void markConstant(Value *V, Constant *C) {
275 markConstant(ValueState[V], V, C);
Chris Lattner138a1242001-06-27 23:38:11 +0000276 }
277
Chris Lattner80b2d6c2004-07-15 23:36:43 +0000278 // markOverdefined - Make a value be marked as "overdefined". If the
Misha Brukmanfd939082005-04-21 23:48:37 +0000279 // value is not already overdefined, add it to the overdefined instruction
Chris Lattner80b2d6c2004-07-15 23:36:43 +0000280 // work list so that the users of the instruction are updated later.
Chris Lattner59acc7d2004-12-10 08:02:06 +0000281 inline void markOverdefined(LatticeVal &IV, Value *V) {
Chris Lattner3d405b02003-10-08 16:21:03 +0000282 if (IV.markOverdefined()) {
Bill Wendlingb7427032006-11-26 09:46:52 +0000283 DEBUG(DOUT << "markOverdefined: ";
Chris Lattnerdade2d22004-12-11 06:05:53 +0000284 if (Function *F = dyn_cast<Function>(V))
Bill Wendlingb7427032006-11-26 09:46:52 +0000285 DOUT << "Function '" << F->getName() << "'\n";
Chris Lattnerdade2d22004-12-11 06:05:53 +0000286 else
Bill Wendlingb7427032006-11-26 09:46:52 +0000287 DOUT << *V);
Chris Lattner82bec2c2004-11-15 04:44:20 +0000288 // Only instructions go on the work list
Chris Lattner59acc7d2004-12-10 08:02:06 +0000289 OverdefinedInstWorkList.push_back(V);
Chris Lattner138a1242001-06-27 23:38:11 +0000290 }
Chris Lattner3d405b02003-10-08 16:21:03 +0000291 }
Chris Lattner59acc7d2004-12-10 08:02:06 +0000292
293 inline void mergeInValue(LatticeVal &IV, Value *V, LatticeVal &MergeWithV) {
294 if (IV.isOverdefined() || MergeWithV.isUndefined())
295 return; // Noop.
296 if (MergeWithV.isOverdefined())
297 markOverdefined(IV, V);
298 else if (IV.isUndefined())
299 markConstant(IV, V, MergeWithV.getConstant());
300 else if (IV.getConstant() != MergeWithV.getConstant())
301 markOverdefined(IV, V);
Chris Lattner138a1242001-06-27 23:38:11 +0000302 }
Chris Lattnerfe243eb2006-02-08 02:38:11 +0000303
304 inline void mergeInValue(Value *V, LatticeVal &MergeWithV) {
305 return mergeInValue(ValueState[V], V, MergeWithV);
306 }
307
Chris Lattner138a1242001-06-27 23:38:11 +0000308
Chris Lattneref36dfd2004-11-15 05:03:30 +0000309 // getValueState - Return the LatticeVal object that corresponds to the value.
Misha Brukman5560c9d2003-08-18 14:43:39 +0000310 // This function is necessary because not all values should start out in the
Chris Lattner73e21422002-04-09 19:48:49 +0000311 // underdefined state... Argument's should be overdefined, and
Chris Lattner79df7c02002-03-26 18:01:55 +0000312 // constants should be marked as constants. If a value is not known to be an
Chris Lattner138a1242001-06-27 23:38:11 +0000313 // Instruction object, then use this accessor to get its value from the map.
314 //
Chris Lattneref36dfd2004-11-15 05:03:30 +0000315 inline LatticeVal &getValueState(Value *V) {
Bill Wendling7a7cf6b2008-08-14 23:05:24 +0000316 std::map<Value*, LatticeVal>::iterator I = ValueState.find(V);
Chris Lattner138a1242001-06-27 23:38:11 +0000317 if (I != ValueState.end()) return I->second; // Common case, in the map
Chris Lattner5d356a72004-10-16 18:09:41 +0000318
Chris Lattner3bad2532006-12-20 06:21:33 +0000319 if (Constant *C = dyn_cast<Constant>(V)) {
Chris Lattner7e529e42004-11-15 05:45:33 +0000320 if (isa<UndefValue>(V)) {
321 // Nothing to do, remain undefined.
322 } else {
Chris Lattnerb59673e2007-02-02 20:38:30 +0000323 LatticeVal &LV = ValueState[C];
324 LV.markConstant(C); // Constants are constant
325 return LV;
Chris Lattner7e529e42004-11-15 05:45:33 +0000326 }
Chris Lattner2a88bb72002-08-30 23:39:00 +0000327 }
Chris Lattner138a1242001-06-27 23:38:11 +0000328 // All others are underdefined by default...
329 return ValueState[V];
330 }
331
Misha Brukmanfd939082005-04-21 23:48:37 +0000332 // markEdgeExecutable - Mark a basic block as executable, adding it to the BB
Chris Lattner138a1242001-06-27 23:38:11 +0000333 // work list if it is not already executable...
Misha Brukmanfd939082005-04-21 23:48:37 +0000334 //
Chris Lattner16b18fd2003-10-08 16:55:34 +0000335 void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
336 if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
337 return; // This edge is already known to be executable!
338
339 if (BBExecutable.count(Dest)) {
Chris Lattner5c8e8d72008-05-11 01:55:59 +0000340 DOUT << "Marking Edge Executable: " << Source->getNameStart()
341 << " -> " << Dest->getNameStart() << "\n";
Chris Lattner16b18fd2003-10-08 16:55:34 +0000342
343 // The destination is already executable, but we just made an edge
Chris Lattner929c6fb2003-10-08 16:56:11 +0000344 // feasible that wasn't before. Revisit the PHI nodes in the block
345 // because they have potentially new operands.
Chris Lattner59acc7d2004-12-10 08:02:06 +0000346 for (BasicBlock::iterator I = Dest->begin(); isa<PHINode>(I); ++I)
347 visitPHINode(*cast<PHINode>(I));
Chris Lattner9de28282003-04-25 02:50:03 +0000348
349 } else {
Chris Lattner82bec2c2004-11-15 04:44:20 +0000350 MarkBlockExecutable(Dest);
Chris Lattner9de28282003-04-25 02:50:03 +0000351 }
Chris Lattner138a1242001-06-27 23:38:11 +0000352 }
353
Chris Lattner82bec2c2004-11-15 04:44:20 +0000354 // getFeasibleSuccessors - Return a vector of booleans to indicate which
355 // successors are reachable from a given terminator instruction.
356 //
Chris Lattner1c1f1122007-02-02 21:15:06 +0000357 void getFeasibleSuccessors(TerminatorInst &TI, SmallVector<bool, 16> &Succs);
Chris Lattner82bec2c2004-11-15 04:44:20 +0000358
359 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
360 // block to the 'To' basic block is currently feasible...
361 //
362 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To);
363
364 // OperandChangedState - This method is invoked on all of the users of an
365 // instruction that was just changed state somehow.... Based on this
366 // information, we need to update the specified user of this instruction.
367 //
368 void OperandChangedState(User *U) {
369 // Only instructions use other variable values!
370 Instruction &I = cast<Instruction>(*U);
371 if (BBExecutable.count(I.getParent())) // Inst is executable?
372 visit(I);
373 }
374
375private:
376 friend class InstVisitor<SCCPSolver>;
Chris Lattner138a1242001-06-27 23:38:11 +0000377
Misha Brukmanfd939082005-04-21 23:48:37 +0000378 // visit implementations - Something changed in this instruction... Either an
Chris Lattnercb056de2001-06-29 23:56:23 +0000379 // operand made a transition, or the instruction is newly executable. Change
380 // the value type of I to reflect these changes if appropriate.
381 //
Chris Lattner7e708292002-06-25 16:13:24 +0000382 void visitPHINode(PHINode &I);
Chris Lattner2a632552002-04-18 15:13:15 +0000383
384 // Terminators
Chris Lattner59acc7d2004-12-10 08:02:06 +0000385 void visitReturnInst(ReturnInst &I);
Chris Lattner7e708292002-06-25 16:13:24 +0000386 void visitTerminatorInst(TerminatorInst &TI);
Chris Lattner2a632552002-04-18 15:13:15 +0000387
Chris Lattnerb8047602002-08-14 17:53:45 +0000388 void visitCastInst(CastInst &I);
Chris Lattner6e323722004-03-12 05:52:44 +0000389 void visitSelectInst(SelectInst &I);
Chris Lattner7e708292002-06-25 16:13:24 +0000390 void visitBinaryOperator(Instruction &I);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000391 void visitCmpInst(CmpInst &I);
Robert Bocchino56107e22006-01-10 19:05:05 +0000392 void visitExtractElementInst(ExtractElementInst &I);
Robert Bocchino8fcf01e2006-01-17 20:06:55 +0000393 void visitInsertElementInst(InsertElementInst &I);
Chris Lattner543abdf2006-04-08 01:19:12 +0000394 void visitShuffleVectorInst(ShuffleVectorInst &I);
Dan Gohmanc4b65ea2008-06-20 01:15:44 +0000395 void visitExtractValueInst(ExtractValueInst &EVI);
396 void visitInsertValueInst(InsertValueInst &IVI);
Chris Lattner2a632552002-04-18 15:13:15 +0000397
398 // Instructions that cannot be folded away...
Chris Lattnerdd336d12004-12-11 05:15:59 +0000399 void visitStoreInst (Instruction &I);
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +0000400 void visitLoadInst (LoadInst &I);
Chris Lattner2a88bb72002-08-30 23:39:00 +0000401 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattner59acc7d2004-12-10 08:02:06 +0000402 void visitCallInst (CallInst &I) { visitCallSite(CallSite::get(&I)); }
403 void visitInvokeInst (InvokeInst &II) {
404 visitCallSite(CallSite::get(&II));
405 visitTerminatorInst(II);
Chris Lattner99b28e62003-08-27 01:08:35 +0000406 }
Chris Lattner59acc7d2004-12-10 08:02:06 +0000407 void visitCallSite (CallSite CS);
Chris Lattner36143fc2003-09-08 18:54:55 +0000408 void visitUnwindInst (TerminatorInst &I) { /*returns void*/ }
Chris Lattner5d356a72004-10-16 18:09:41 +0000409 void visitUnreachableInst(TerminatorInst &I) { /*returns void*/ }
Chris Lattner7e708292002-06-25 16:13:24 +0000410 void visitAllocationInst(Instruction &I) { markOverdefined(&I); }
Chris Lattnercda965e2003-10-18 05:56:52 +0000411 void visitVANextInst (Instruction &I) { markOverdefined(&I); }
412 void visitVAArgInst (Instruction &I) { markOverdefined(&I); }
Chris Lattner7e708292002-06-25 16:13:24 +0000413 void visitFreeInst (Instruction &I) { /*returns void*/ }
Chris Lattner2a632552002-04-18 15:13:15 +0000414
Chris Lattner7e708292002-06-25 16:13:24 +0000415 void visitInstruction(Instruction &I) {
Chris Lattner2a632552002-04-18 15:13:15 +0000416 // If a new instruction is added to LLVM that we don't handle...
Bill Wendlinge8156192006-12-07 01:30:32 +0000417 cerr << "SCCP: Don't know how to handle: " << I;
Chris Lattner7e708292002-06-25 16:13:24 +0000418 markOverdefined(&I); // Just in case
Chris Lattner2a632552002-04-18 15:13:15 +0000419 }
Chris Lattnercb056de2001-06-29 23:56:23 +0000420};
Chris Lattnerf6293092002-07-23 18:06:35 +0000421
Duncan Sandse2abf122007-07-20 08:56:21 +0000422} // end anonymous namespace
423
424
Chris Lattnerb9a66342002-05-02 21:44:00 +0000425// getFeasibleSuccessors - Return a vector of booleans to indicate which
426// successors are reachable from a given terminator instruction.
427//
Chris Lattner82bec2c2004-11-15 04:44:20 +0000428void SCCPSolver::getFeasibleSuccessors(TerminatorInst &TI,
Chris Lattner1c1f1122007-02-02 21:15:06 +0000429 SmallVector<bool, 16> &Succs) {
Chris Lattner9de28282003-04-25 02:50:03 +0000430 Succs.resize(TI.getNumSuccessors());
Chris Lattner7e708292002-06-25 16:13:24 +0000431 if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
Chris Lattnerb9a66342002-05-02 21:44:00 +0000432 if (BI->isUnconditional()) {
433 Succs[0] = true;
434 } else {
Chris Lattneref36dfd2004-11-15 05:03:30 +0000435 LatticeVal &BCValue = getValueState(BI->getCondition());
Chris Lattner84831642004-01-12 17:40:36 +0000436 if (BCValue.isOverdefined() ||
Reid Spencer579dca12007-01-12 04:24:46 +0000437 (BCValue.isConstant() && !isa<ConstantInt>(BCValue.getConstant()))) {
Chris Lattner84831642004-01-12 17:40:36 +0000438 // Overdefined condition variables, and branches on unfoldable constant
439 // conditions, mean the branch could go either way.
Chris Lattnerb9a66342002-05-02 21:44:00 +0000440 Succs[0] = Succs[1] = true;
441 } else if (BCValue.isConstant()) {
442 // Constant condition variables mean the branch can only go a single way
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000443 Succs[BCValue.getConstant() == Context->getConstantIntFalse()] = true;
Chris Lattnerb9a66342002-05-02 21:44:00 +0000444 }
445 }
Reid Spencer3ed469c2006-11-02 20:25:50 +0000446 } else if (isa<InvokeInst>(&TI)) {
Chris Lattnerb9a66342002-05-02 21:44:00 +0000447 // Invoke instructions successors are always executable.
448 Succs[0] = Succs[1] = true;
Chris Lattner7e708292002-06-25 16:13:24 +0000449 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) {
Chris Lattneref36dfd2004-11-15 05:03:30 +0000450 LatticeVal &SCValue = getValueState(SI->getCondition());
Chris Lattner84831642004-01-12 17:40:36 +0000451 if (SCValue.isOverdefined() || // Overdefined condition?
452 (SCValue.isConstant() && !isa<ConstantInt>(SCValue.getConstant()))) {
Chris Lattnerb9a66342002-05-02 21:44:00 +0000453 // All destinations are executable!
Chris Lattner7e708292002-06-25 16:13:24 +0000454 Succs.assign(TI.getNumSuccessors(), true);
Chris Lattner3a73c9e2008-05-10 23:56:54 +0000455 } else if (SCValue.isConstant())
456 Succs[SI->findCaseValue(cast<ConstantInt>(SCValue.getConstant()))] = true;
Chris Lattnerb9a66342002-05-02 21:44:00 +0000457 } else {
Chris Lattner1c1f1122007-02-02 21:15:06 +0000458 assert(0 && "SCCP: Don't know how to handle this terminator!");
Chris Lattnerb9a66342002-05-02 21:44:00 +0000459 }
460}
461
462
Chris Lattner59f0ce22002-05-02 21:18:01 +0000463// isEdgeFeasible - Return true if the control flow edge from the 'From' basic
464// block to the 'To' basic block is currently feasible...
465//
Chris Lattner82bec2c2004-11-15 04:44:20 +0000466bool SCCPSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
Chris Lattner59f0ce22002-05-02 21:18:01 +0000467 assert(BBExecutable.count(To) && "Dest should always be alive!");
468
469 // Make sure the source basic block is executable!!
470 if (!BBExecutable.count(From)) return false;
Misha Brukmanfd939082005-04-21 23:48:37 +0000471
Chris Lattnerb9a66342002-05-02 21:44:00 +0000472 // Check to make sure this edge itself is actually feasible now...
Chris Lattner7d275f42003-10-08 15:47:41 +0000473 TerminatorInst *TI = From->getTerminator();
474 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
475 if (BI->isUnconditional())
Chris Lattnerb9a66342002-05-02 21:44:00 +0000476 return true;
Chris Lattner7d275f42003-10-08 15:47:41 +0000477 else {
Chris Lattneref36dfd2004-11-15 05:03:30 +0000478 LatticeVal &BCValue = getValueState(BI->getCondition());
Chris Lattner7d275f42003-10-08 15:47:41 +0000479 if (BCValue.isOverdefined()) {
480 // Overdefined condition variables mean the branch could go either way.
481 return true;
482 } else if (BCValue.isConstant()) {
Chris Lattner84831642004-01-12 17:40:36 +0000483 // Not branching on an evaluatable constant?
Chris Lattner54a525d2007-01-13 00:42:58 +0000484 if (!isa<ConstantInt>(BCValue.getConstant())) return true;
Chris Lattner84831642004-01-12 17:40:36 +0000485
Chris Lattner7d275f42003-10-08 15:47:41 +0000486 // Constant condition variables mean the branch can only go a single way
Misha Brukmanfd939082005-04-21 23:48:37 +0000487 return BI->getSuccessor(BCValue.getConstant() ==
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000488 Context->getConstantIntFalse()) == To;
Chris Lattner7d275f42003-10-08 15:47:41 +0000489 }
490 return false;
491 }
Reid Spencer3ed469c2006-11-02 20:25:50 +0000492 } else if (isa<InvokeInst>(TI)) {
Chris Lattner7d275f42003-10-08 15:47:41 +0000493 // Invoke instructions successors are always executable.
494 return true;
495 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
Chris Lattneref36dfd2004-11-15 05:03:30 +0000496 LatticeVal &SCValue = getValueState(SI->getCondition());
Chris Lattner7d275f42003-10-08 15:47:41 +0000497 if (SCValue.isOverdefined()) { // Overdefined condition?
498 // All destinations are executable!
499 return true;
500 } else if (SCValue.isConstant()) {
501 Constant *CPV = SCValue.getConstant();
Chris Lattner84831642004-01-12 17:40:36 +0000502 if (!isa<ConstantInt>(CPV))
503 return true; // not a foldable constant?
504
Chris Lattner7d275f42003-10-08 15:47:41 +0000505 // Make sure to skip the "default value" which isn't a value
506 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i)
507 if (SI->getSuccessorValue(i) == CPV) // Found the taken branch...
508 return SI->getSuccessor(i) == To;
509
510 // Constant value not equal to any of the branches... must execute
511 // default branch then...
512 return SI->getDefaultDest() == To;
513 }
514 return false;
515 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +0000516 cerr << "Unknown terminator instruction: " << *TI;
Chris Lattner7d275f42003-10-08 15:47:41 +0000517 abort();
518 }
Chris Lattner59f0ce22002-05-02 21:18:01 +0000519}
Chris Lattner138a1242001-06-27 23:38:11 +0000520
Chris Lattner2a632552002-04-18 15:13:15 +0000521// visit Implementations - Something changed in this instruction... Either an
Chris Lattner138a1242001-06-27 23:38:11 +0000522// operand made a transition, or the instruction is newly executable. Change
523// the value type of I to reflect these changes if appropriate. This method
524// makes sure to do the following actions:
525//
526// 1. If a phi node merges two constants in, and has conflicting value coming
527// from different branches, or if the PHI node merges in an overdefined
528// value, then the PHI node becomes overdefined.
529// 2. If a phi node merges only constants in, and they all agree on value, the
530// PHI node becomes a constant value equal to that.
531// 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
532// 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
533// 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
534// 6. If a conditional branch has a value that is constant, make the selected
535// destination executable
536// 7. If a conditional branch has a value that is overdefined, make all
537// successors executable.
538//
Chris Lattner82bec2c2004-11-15 04:44:20 +0000539void SCCPSolver::visitPHINode(PHINode &PN) {
Chris Lattneref36dfd2004-11-15 05:03:30 +0000540 LatticeVal &PNIV = getValueState(&PN);
Chris Lattner1daee8b2004-01-12 03:57:30 +0000541 if (PNIV.isOverdefined()) {
542 // There may be instructions using this PHI node that are not overdefined
543 // themselves. If so, make sure that they know that the PHI node operand
544 // changed.
545 std::multimap<PHINode*, Instruction*>::iterator I, E;
546 tie(I, E) = UsersOfOverdefinedPHIs.equal_range(&PN);
547 if (I != E) {
Chris Lattner1c1f1122007-02-02 21:15:06 +0000548 SmallVector<Instruction*, 16> Users;
Chris Lattner1daee8b2004-01-12 03:57:30 +0000549 for (; I != E; ++I) Users.push_back(I->second);
550 while (!Users.empty()) {
551 visit(Users.back());
552 Users.pop_back();
553 }
554 }
555 return; // Quick exit
556 }
Chris Lattner138a1242001-06-27 23:38:11 +0000557
Chris Lattnera2f652d2004-03-16 19:49:59 +0000558 // Super-extra-high-degree PHI nodes are unlikely to ever be marked constant,
559 // and slow us down a lot. Just mark them overdefined.
560 if (PN.getNumIncomingValues() > 64) {
561 markOverdefined(PNIV, &PN);
562 return;
563 }
564
Chris Lattner2a632552002-04-18 15:13:15 +0000565 // Look at all of the executable operands of the PHI node. If any of them
566 // are overdefined, the PHI becomes overdefined as well. If they are all
567 // constant, and they agree with each other, the PHI becomes the identical
568 // constant. If they are constant and don't agree, the PHI is overdefined.
569 // If there are no executable operands, the PHI remains undefined.
570 //
Chris Lattner9de28282003-04-25 02:50:03 +0000571 Constant *OperandVal = 0;
572 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
Chris Lattneref36dfd2004-11-15 05:03:30 +0000573 LatticeVal &IV = getValueState(PN.getIncomingValue(i));
Chris Lattner9de28282003-04-25 02:50:03 +0000574 if (IV.isUndefined()) continue; // Doesn't influence PHI node.
Misha Brukmanfd939082005-04-21 23:48:37 +0000575
Chris Lattner7e708292002-06-25 16:13:24 +0000576 if (isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent())) {
Chris Lattner38b5ae42003-06-24 20:29:52 +0000577 if (IV.isOverdefined()) { // PHI node becomes overdefined!
Chris Lattnercf712de2008-08-23 23:36:38 +0000578 markOverdefined(&PN);
Chris Lattner38b5ae42003-06-24 20:29:52 +0000579 return;
580 }
581
Chris Lattner9de28282003-04-25 02:50:03 +0000582 if (OperandVal == 0) { // Grab the first value...
583 OperandVal = IV.getConstant();
Chris Lattner2a632552002-04-18 15:13:15 +0000584 } else { // Another value is being merged in!
585 // There is already a reachable operand. If we conflict with it,
586 // then the PHI node becomes overdefined. If we agree with it, we
587 // can continue on.
Misha Brukmanfd939082005-04-21 23:48:37 +0000588
Chris Lattner2a632552002-04-18 15:13:15 +0000589 // Check to see if there are two different constants merging...
Chris Lattner9de28282003-04-25 02:50:03 +0000590 if (IV.getConstant() != OperandVal) {
Chris Lattner2a632552002-04-18 15:13:15 +0000591 // Yes there is. This means the PHI node is not constant.
592 // You must be overdefined poor PHI.
593 //
Chris Lattnercf712de2008-08-23 23:36:38 +0000594 markOverdefined(&PN); // The PHI node now becomes overdefined
Chris Lattner2a632552002-04-18 15:13:15 +0000595 return; // I'm done analyzing you
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000596 }
Chris Lattner138a1242001-06-27 23:38:11 +0000597 }
598 }
Chris Lattner138a1242001-06-27 23:38:11 +0000599 }
600
Chris Lattner2a632552002-04-18 15:13:15 +0000601 // If we exited the loop, this means that the PHI node only has constant
Chris Lattner9de28282003-04-25 02:50:03 +0000602 // arguments that agree with each other(and OperandVal is the constant) or
603 // OperandVal is null because there are no defined incoming arguments. If
604 // this is the case, the PHI remains undefined.
Chris Lattner138a1242001-06-27 23:38:11 +0000605 //
Chris Lattner9de28282003-04-25 02:50:03 +0000606 if (OperandVal)
Chris Lattnercf712de2008-08-23 23:36:38 +0000607 markConstant(&PN, OperandVal); // Acquire operand value
Chris Lattner138a1242001-06-27 23:38:11 +0000608}
609
Chris Lattner59acc7d2004-12-10 08:02:06 +0000610void SCCPSolver::visitReturnInst(ReturnInst &I) {
611 if (I.getNumOperands() == 0) return; // Ret void
612
Chris Lattner59acc7d2004-12-10 08:02:06 +0000613 Function *F = I.getParent()->getParent();
Devang Patel7c490d42008-03-11 05:46:42 +0000614 // If we are tracking the return value of this function, merge it in.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000615 if (!F->hasLocalLinkage())
Devang Patel7c490d42008-03-11 05:46:42 +0000616 return;
617
Chris Lattnerc6ee00b2008-04-23 05:38:20 +0000618 if (!TrackedRetVals.empty() && I.getNumOperands() == 1) {
Chris Lattnerb59673e2007-02-02 20:38:30 +0000619 DenseMap<Function*, LatticeVal>::iterator TFRVI =
Devang Patel7c490d42008-03-11 05:46:42 +0000620 TrackedRetVals.find(F);
621 if (TFRVI != TrackedRetVals.end() &&
Chris Lattner59acc7d2004-12-10 08:02:06 +0000622 !TFRVI->second.isOverdefined()) {
623 LatticeVal &IV = getValueState(I.getOperand(0));
624 mergeInValue(TFRVI->second, F, IV);
Devang Patel7c490d42008-03-11 05:46:42 +0000625 return;
626 }
627 }
628
Chris Lattnerc6ee00b2008-04-23 05:38:20 +0000629 // Handle functions that return multiple values.
630 if (!TrackedMultipleRetVals.empty() && I.getNumOperands() > 1) {
631 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
Chris Lattnercf712de2008-08-23 23:36:38 +0000632 DenseMap<std::pair<Function*, unsigned>, LatticeVal>::iterator
Chris Lattnerc6ee00b2008-04-23 05:38:20 +0000633 It = TrackedMultipleRetVals.find(std::make_pair(F, i));
634 if (It == TrackedMultipleRetVals.end()) break;
635 mergeInValue(It->second, F, getValueState(I.getOperand(i)));
Chris Lattner59acc7d2004-12-10 08:02:06 +0000636 }
Dan Gohmanc4b65ea2008-06-20 01:15:44 +0000637 } else if (!TrackedMultipleRetVals.empty() &&
638 I.getNumOperands() == 1 &&
639 isa<StructType>(I.getOperand(0)->getType())) {
640 for (unsigned i = 0, e = I.getOperand(0)->getType()->getNumContainedTypes();
641 i != e; ++i) {
Chris Lattnercf712de2008-08-23 23:36:38 +0000642 DenseMap<std::pair<Function*, unsigned>, LatticeVal>::iterator
Dan Gohmanc4b65ea2008-06-20 01:15:44 +0000643 It = TrackedMultipleRetVals.find(std::make_pair(F, i));
644 if (It == TrackedMultipleRetVals.end()) break;
Nick Lewyckyd7f20b62009-06-06 23:13:08 +0000645 if (Value *Val = FindInsertedValue(I.getOperand(0), i))
646 mergeInValue(It->second, F, getValueState(Val));
Dan Gohmanc4b65ea2008-06-20 01:15:44 +0000647 }
Chris Lattner59acc7d2004-12-10 08:02:06 +0000648 }
649}
650
Chris Lattner82bec2c2004-11-15 04:44:20 +0000651void SCCPSolver::visitTerminatorInst(TerminatorInst &TI) {
Chris Lattner1c1f1122007-02-02 21:15:06 +0000652 SmallVector<bool, 16> SuccFeasible;
Chris Lattnerb9a66342002-05-02 21:44:00 +0000653 getFeasibleSuccessors(TI, SuccFeasible);
Chris Lattner138a1242001-06-27 23:38:11 +0000654
Chris Lattner16b18fd2003-10-08 16:55:34 +0000655 BasicBlock *BB = TI.getParent();
656
Chris Lattnerb9a66342002-05-02 21:44:00 +0000657 // Mark all feasible successors executable...
658 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
Chris Lattner16b18fd2003-10-08 16:55:34 +0000659 if (SuccFeasible[i])
660 markEdgeExecutable(BB, TI.getSuccessor(i));
Chris Lattner2a632552002-04-18 15:13:15 +0000661}
662
Chris Lattner82bec2c2004-11-15 04:44:20 +0000663void SCCPSolver::visitCastInst(CastInst &I) {
Chris Lattner7e708292002-06-25 16:13:24 +0000664 Value *V = I.getOperand(0);
Chris Lattneref36dfd2004-11-15 05:03:30 +0000665 LatticeVal &VState = getValueState(V);
Chris Lattnerb7a5d3e2004-01-12 17:43:40 +0000666 if (VState.isOverdefined()) // Inherit overdefinedness of operand
Chris Lattner7e708292002-06-25 16:13:24 +0000667 markOverdefined(&I);
Chris Lattnerb7a5d3e2004-01-12 17:43:40 +0000668 else if (VState.isConstant()) // Propagate constant value
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000669 markConstant(&I, Context->getConstantExprCast(I.getOpcode(),
Reid Spencer4da49122006-12-12 05:05:00 +0000670 VState.getConstant(), I.getType()));
Chris Lattner2a632552002-04-18 15:13:15 +0000671}
672
Dan Gohmanc4b65ea2008-06-20 01:15:44 +0000673void SCCPSolver::visitExtractValueInst(ExtractValueInst &EVI) {
Dan Gohman60ea2682008-06-20 16:41:17 +0000674 Value *Aggr = EVI.getAggregateOperand();
Dan Gohmanc4b65ea2008-06-20 01:15:44 +0000675
Dan Gohman60ea2682008-06-20 16:41:17 +0000676 // If the operand to the extractvalue is an undef, the result is undef.
Dan Gohmanc4b65ea2008-06-20 01:15:44 +0000677 if (isa<UndefValue>(Aggr))
678 return;
679
680 // Currently only handle single-index extractvalues.
681 if (EVI.getNumIndices() != 1) {
682 markOverdefined(&EVI);
683 return;
684 }
685
686 Function *F = 0;
687 if (CallInst *CI = dyn_cast<CallInst>(Aggr))
688 F = CI->getCalledFunction();
689 else if (InvokeInst *II = dyn_cast<InvokeInst>(Aggr))
690 F = II->getCalledFunction();
691
692 // TODO: If IPSCCP resolves the callee of this function, we could propagate a
693 // result back!
694 if (F == 0 || TrackedMultipleRetVals.empty()) {
695 markOverdefined(&EVI);
696 return;
697 }
698
Chris Lattnercf712de2008-08-23 23:36:38 +0000699 // See if we are tracking the result of the callee. If not tracking this
700 // function (for example, it is a declaration) just move to overdefined.
701 if (!TrackedMultipleRetVals.count(std::make_pair(F, *EVI.idx_begin()))) {
Dan Gohmanc4b65ea2008-06-20 01:15:44 +0000702 markOverdefined(&EVI);
703 return;
704 }
705
706 // Otherwise, the value will be merged in here as a result of CallSite
707 // handling.
708}
709
710void SCCPSolver::visitInsertValueInst(InsertValueInst &IVI) {
Dan Gohman60ea2682008-06-20 16:41:17 +0000711 Value *Aggr = IVI.getAggregateOperand();
712 Value *Val = IVI.getInsertedValueOperand();
Dan Gohmanc4b65ea2008-06-20 01:15:44 +0000713
Dan Gohman60ea2682008-06-20 16:41:17 +0000714 // If the operands to the insertvalue are undef, the result is undef.
Dan Gohmandfaceb42008-06-20 16:39:44 +0000715 if (isa<UndefValue>(Aggr) && isa<UndefValue>(Val))
Dan Gohmanc4b65ea2008-06-20 01:15:44 +0000716 return;
717
718 // Currently only handle single-index insertvalues.
719 if (IVI.getNumIndices() != 1) {
720 markOverdefined(&IVI);
721 return;
722 }
Dan Gohmandfaceb42008-06-20 16:39:44 +0000723
724 // Currently only handle insertvalue instructions that are in a single-use
725 // chain that builds up a return value.
726 for (const InsertValueInst *TmpIVI = &IVI; ; ) {
727 if (!TmpIVI->hasOneUse()) {
728 markOverdefined(&IVI);
729 return;
730 }
731 const Value *V = *TmpIVI->use_begin();
732 if (isa<ReturnInst>(V))
733 break;
734 TmpIVI = dyn_cast<InsertValueInst>(V);
735 if (!TmpIVI) {
736 markOverdefined(&IVI);
737 return;
738 }
739 }
Dan Gohmanc4b65ea2008-06-20 01:15:44 +0000740
741 // See if we are tracking the result of the callee.
742 Function *F = IVI.getParent()->getParent();
Chris Lattnercf712de2008-08-23 23:36:38 +0000743 DenseMap<std::pair<Function*, unsigned>, LatticeVal>::iterator
Dan Gohmanc4b65ea2008-06-20 01:15:44 +0000744 It = TrackedMultipleRetVals.find(std::make_pair(F, *IVI.idx_begin()));
745
746 // Merge in the inserted member value.
747 if (It != TrackedMultipleRetVals.end())
748 mergeInValue(It->second, F, getValueState(Val));
749
Dan Gohman60ea2682008-06-20 16:41:17 +0000750 // Mark the aggregate result of the IVI overdefined; any tracking that we do
751 // will be done on the individual member values.
Dan Gohmanc4b65ea2008-06-20 01:15:44 +0000752 markOverdefined(&IVI);
753}
754
Chris Lattner82bec2c2004-11-15 04:44:20 +0000755void SCCPSolver::visitSelectInst(SelectInst &I) {
Chris Lattneref36dfd2004-11-15 05:03:30 +0000756 LatticeVal &CondValue = getValueState(I.getCondition());
Chris Lattnerfe243eb2006-02-08 02:38:11 +0000757 if (CondValue.isUndefined())
758 return;
Reid Spencer579dca12007-01-12 04:24:46 +0000759 if (CondValue.isConstant()) {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000760 if (ConstantInt *CondCB = dyn_cast<ConstantInt>(CondValue.getConstant())){
Reid Spencer579dca12007-01-12 04:24:46 +0000761 mergeInValue(&I, getValueState(CondCB->getZExtValue() ? I.getTrueValue()
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000762 : I.getFalseValue()));
Chris Lattnerfe243eb2006-02-08 02:38:11 +0000763 return;
764 }
765 }
766
767 // Otherwise, the condition is overdefined or a constant we can't evaluate.
768 // See if we can produce something better than overdefined based on the T/F
769 // value.
770 LatticeVal &TVal = getValueState(I.getTrueValue());
771 LatticeVal &FVal = getValueState(I.getFalseValue());
772
773 // select ?, C, C -> C.
774 if (TVal.isConstant() && FVal.isConstant() &&
775 TVal.getConstant() == FVal.getConstant()) {
776 markConstant(&I, FVal.getConstant());
777 return;
778 }
779
780 if (TVal.isUndefined()) { // select ?, undef, X -> X.
781 mergeInValue(&I, FVal);
782 } else if (FVal.isUndefined()) { // select ?, X, undef -> X.
783 mergeInValue(&I, TVal);
784 } else {
785 markOverdefined(&I);
Chris Lattner6e323722004-03-12 05:52:44 +0000786 }
787}
788
Chris Lattner2a632552002-04-18 15:13:15 +0000789// Handle BinaryOperators and Shift Instructions...
Chris Lattner82bec2c2004-11-15 04:44:20 +0000790void SCCPSolver::visitBinaryOperator(Instruction &I) {
Chris Lattneref36dfd2004-11-15 05:03:30 +0000791 LatticeVal &IV = ValueState[&I];
Chris Lattner1daee8b2004-01-12 03:57:30 +0000792 if (IV.isOverdefined()) return;
793
Chris Lattneref36dfd2004-11-15 05:03:30 +0000794 LatticeVal &V1State = getValueState(I.getOperand(0));
795 LatticeVal &V2State = getValueState(I.getOperand(1));
Chris Lattner1daee8b2004-01-12 03:57:30 +0000796
Chris Lattner2a632552002-04-18 15:13:15 +0000797 if (V1State.isOverdefined() || V2State.isOverdefined()) {
Chris Lattnera177c672004-12-11 23:15:19 +0000798 // If this is an AND or OR with 0 or -1, it doesn't matter that the other
799 // operand is overdefined.
800 if (I.getOpcode() == Instruction::And || I.getOpcode() == Instruction::Or) {
801 LatticeVal *NonOverdefVal = 0;
802 if (!V1State.isOverdefined()) {
803 NonOverdefVal = &V1State;
804 } else if (!V2State.isOverdefined()) {
805 NonOverdefVal = &V2State;
806 }
807
808 if (NonOverdefVal) {
809 if (NonOverdefVal->isUndefined()) {
810 // Could annihilate value.
811 if (I.getOpcode() == Instruction::And)
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000812 markConstant(IV, &I, Context->getNullValue(I.getType()));
Reid Spencer9d6565a2007-02-15 02:26:10 +0000813 else if (const VectorType *PT = dyn_cast<VectorType>(I.getType()))
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000814 markConstant(IV, &I, Context->getConstantVectorAllOnesValue(PT));
Chris Lattner7ce2f8b2007-01-04 02:12:40 +0000815 else
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000816 markConstant(IV, &I,
817 Context->getConstantIntAllOnesValue(I.getType()));
Chris Lattnera177c672004-12-11 23:15:19 +0000818 return;
819 } else {
820 if (I.getOpcode() == Instruction::And) {
821 if (NonOverdefVal->getConstant()->isNullValue()) {
822 markConstant(IV, &I, NonOverdefVal->getConstant());
Jim Laskey52ab9042007-01-03 00:11:03 +0000823 return; // X and 0 = 0
Chris Lattnera177c672004-12-11 23:15:19 +0000824 }
825 } else {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000826 if (ConstantInt *CI =
827 dyn_cast<ConstantInt>(NonOverdefVal->getConstant()))
Chris Lattnera177c672004-12-11 23:15:19 +0000828 if (CI->isAllOnesValue()) {
829 markConstant(IV, &I, NonOverdefVal->getConstant());
830 return; // X or -1 = -1
831 }
832 }
833 }
834 }
835 }
836
837
Chris Lattner1daee8b2004-01-12 03:57:30 +0000838 // If both operands are PHI nodes, it is possible that this instruction has
839 // a constant value, despite the fact that the PHI node doesn't. Check for
840 // this condition now.
841 if (PHINode *PN1 = dyn_cast<PHINode>(I.getOperand(0)))
842 if (PHINode *PN2 = dyn_cast<PHINode>(I.getOperand(1)))
843 if (PN1->getParent() == PN2->getParent()) {
844 // Since the two PHI nodes are in the same basic block, they must have
845 // entries for the same predecessors. Walk the predecessor list, and
846 // if all of the incoming values are constants, and the result of
847 // evaluating this expression with all incoming value pairs is the
848 // same, then this expression is a constant even though the PHI node
849 // is not a constant!
Chris Lattneref36dfd2004-11-15 05:03:30 +0000850 LatticeVal Result;
Chris Lattner1daee8b2004-01-12 03:57:30 +0000851 for (unsigned i = 0, e = PN1->getNumIncomingValues(); i != e; ++i) {
Chris Lattneref36dfd2004-11-15 05:03:30 +0000852 LatticeVal &In1 = getValueState(PN1->getIncomingValue(i));
Chris Lattner1daee8b2004-01-12 03:57:30 +0000853 BasicBlock *InBlock = PN1->getIncomingBlock(i);
Chris Lattneref36dfd2004-11-15 05:03:30 +0000854 LatticeVal &In2 =
855 getValueState(PN2->getIncomingValueForBlock(InBlock));
Chris Lattner1daee8b2004-01-12 03:57:30 +0000856
857 if (In1.isOverdefined() || In2.isOverdefined()) {
858 Result.markOverdefined();
859 break; // Cannot fold this operation over the PHI nodes!
860 } else if (In1.isConstant() && In2.isConstant()) {
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000861 Constant *V =
862 Context->getConstantExpr(I.getOpcode(), In1.getConstant(),
Chris Lattnerb16689b2004-01-12 19:08:43 +0000863 In2.getConstant());
Chris Lattner1daee8b2004-01-12 03:57:30 +0000864 if (Result.isUndefined())
Chris Lattnerb16689b2004-01-12 19:08:43 +0000865 Result.markConstant(V);
866 else if (Result.isConstant() && Result.getConstant() != V) {
Chris Lattner1daee8b2004-01-12 03:57:30 +0000867 Result.markOverdefined();
868 break;
869 }
870 }
871 }
872
873 // If we found a constant value here, then we know the instruction is
874 // constant despite the fact that the PHI nodes are overdefined.
875 if (Result.isConstant()) {
876 markConstant(IV, &I, Result.getConstant());
877 // Remember that this instruction is virtually using the PHI node
878 // operands.
879 UsersOfOverdefinedPHIs.insert(std::make_pair(PN1, &I));
880 UsersOfOverdefinedPHIs.insert(std::make_pair(PN2, &I));
881 return;
882 } else if (Result.isUndefined()) {
883 return;
884 }
885
886 // Okay, this really is overdefined now. Since we might have
887 // speculatively thought that this was not overdefined before, and
888 // added ourselves to the UsersOfOverdefinedPHIs list for the PHIs,
889 // make sure to clean out any entries that we put there, for
890 // efficiency.
891 std::multimap<PHINode*, Instruction*>::iterator It, E;
892 tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN1);
893 while (It != E) {
894 if (It->second == &I) {
895 UsersOfOverdefinedPHIs.erase(It++);
896 } else
897 ++It;
898 }
899 tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN2);
900 while (It != E) {
901 if (It->second == &I) {
902 UsersOfOverdefinedPHIs.erase(It++);
903 } else
904 ++It;
905 }
906 }
907
908 markOverdefined(IV, &I);
Chris Lattner2a632552002-04-18 15:13:15 +0000909 } else if (V1State.isConstant() && V2State.isConstant()) {
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000910 markConstant(IV, &I,
911 Context->getConstantExpr(I.getOpcode(), V1State.getConstant(),
Chris Lattnerb16689b2004-01-12 19:08:43 +0000912 V2State.getConstant()));
Chris Lattner2a632552002-04-18 15:13:15 +0000913 }
914}
Chris Lattner2a88bb72002-08-30 23:39:00 +0000915
Reid Spencere4d87aa2006-12-23 06:05:41 +0000916// Handle ICmpInst instruction...
917void SCCPSolver::visitCmpInst(CmpInst &I) {
918 LatticeVal &IV = ValueState[&I];
919 if (IV.isOverdefined()) return;
920
921 LatticeVal &V1State = getValueState(I.getOperand(0));
922 LatticeVal &V2State = getValueState(I.getOperand(1));
923
924 if (V1State.isOverdefined() || V2State.isOverdefined()) {
925 // If both operands are PHI nodes, it is possible that this instruction has
926 // a constant value, despite the fact that the PHI node doesn't. Check for
927 // this condition now.
928 if (PHINode *PN1 = dyn_cast<PHINode>(I.getOperand(0)))
929 if (PHINode *PN2 = dyn_cast<PHINode>(I.getOperand(1)))
930 if (PN1->getParent() == PN2->getParent()) {
931 // Since the two PHI nodes are in the same basic block, they must have
932 // entries for the same predecessors. Walk the predecessor list, and
933 // if all of the incoming values are constants, and the result of
934 // evaluating this expression with all incoming value pairs is the
935 // same, then this expression is a constant even though the PHI node
936 // is not a constant!
937 LatticeVal Result;
938 for (unsigned i = 0, e = PN1->getNumIncomingValues(); i != e; ++i) {
939 LatticeVal &In1 = getValueState(PN1->getIncomingValue(i));
940 BasicBlock *InBlock = PN1->getIncomingBlock(i);
941 LatticeVal &In2 =
942 getValueState(PN2->getIncomingValueForBlock(InBlock));
943
944 if (In1.isOverdefined() || In2.isOverdefined()) {
945 Result.markOverdefined();
946 break; // Cannot fold this operation over the PHI nodes!
947 } else if (In1.isConstant() && In2.isConstant()) {
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000948 Constant *V = Context->getConstantExprCompare(I.getPredicate(),
Reid Spencere4d87aa2006-12-23 06:05:41 +0000949 In1.getConstant(),
950 In2.getConstant());
951 if (Result.isUndefined())
952 Result.markConstant(V);
953 else if (Result.isConstant() && Result.getConstant() != V) {
954 Result.markOverdefined();
955 break;
956 }
957 }
958 }
959
960 // If we found a constant value here, then we know the instruction is
961 // constant despite the fact that the PHI nodes are overdefined.
962 if (Result.isConstant()) {
963 markConstant(IV, &I, Result.getConstant());
964 // Remember that this instruction is virtually using the PHI node
965 // operands.
966 UsersOfOverdefinedPHIs.insert(std::make_pair(PN1, &I));
967 UsersOfOverdefinedPHIs.insert(std::make_pair(PN2, &I));
968 return;
969 } else if (Result.isUndefined()) {
970 return;
971 }
972
973 // Okay, this really is overdefined now. Since we might have
974 // speculatively thought that this was not overdefined before, and
975 // added ourselves to the UsersOfOverdefinedPHIs list for the PHIs,
976 // make sure to clean out any entries that we put there, for
977 // efficiency.
978 std::multimap<PHINode*, Instruction*>::iterator It, E;
979 tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN1);
980 while (It != E) {
981 if (It->second == &I) {
982 UsersOfOverdefinedPHIs.erase(It++);
983 } else
984 ++It;
985 }
986 tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN2);
987 while (It != E) {
988 if (It->second == &I) {
989 UsersOfOverdefinedPHIs.erase(It++);
990 } else
991 ++It;
992 }
993 }
994
995 markOverdefined(IV, &I);
996 } else if (V1State.isConstant() && V2State.isConstant()) {
Owen Andersonfa5cbd62009-07-03 19:42:02 +0000997 markConstant(IV, &I, Context->getConstantExprCompare(I.getPredicate(),
Reid Spencere4d87aa2006-12-23 06:05:41 +0000998 V1State.getConstant(),
999 V2State.getConstant()));
1000 }
1001}
1002
Robert Bocchino56107e22006-01-10 19:05:05 +00001003void SCCPSolver::visitExtractElementInst(ExtractElementInst &I) {
Devang Patel67a821d2006-12-04 23:54:59 +00001004 // FIXME : SCCP does not handle vectors properly.
1005 markOverdefined(&I);
1006 return;
1007
1008#if 0
Robert Bocchino56107e22006-01-10 19:05:05 +00001009 LatticeVal &ValState = getValueState(I.getOperand(0));
1010 LatticeVal &IdxState = getValueState(I.getOperand(1));
1011
1012 if (ValState.isOverdefined() || IdxState.isOverdefined())
1013 markOverdefined(&I);
1014 else if(ValState.isConstant() && IdxState.isConstant())
1015 markConstant(&I, ConstantExpr::getExtractElement(ValState.getConstant(),
1016 IdxState.getConstant()));
Devang Patel67a821d2006-12-04 23:54:59 +00001017#endif
Robert Bocchino56107e22006-01-10 19:05:05 +00001018}
1019
Robert Bocchino8fcf01e2006-01-17 20:06:55 +00001020void SCCPSolver::visitInsertElementInst(InsertElementInst &I) {
Devang Patel67a821d2006-12-04 23:54:59 +00001021 // FIXME : SCCP does not handle vectors properly.
1022 markOverdefined(&I);
1023 return;
1024#if 0
Robert Bocchino8fcf01e2006-01-17 20:06:55 +00001025 LatticeVal &ValState = getValueState(I.getOperand(0));
1026 LatticeVal &EltState = getValueState(I.getOperand(1));
1027 LatticeVal &IdxState = getValueState(I.getOperand(2));
1028
1029 if (ValState.isOverdefined() || EltState.isOverdefined() ||
1030 IdxState.isOverdefined())
1031 markOverdefined(&I);
1032 else if(ValState.isConstant() && EltState.isConstant() &&
1033 IdxState.isConstant())
1034 markConstant(&I, ConstantExpr::getInsertElement(ValState.getConstant(),
1035 EltState.getConstant(),
1036 IdxState.getConstant()));
1037 else if (ValState.isUndefined() && EltState.isConstant() &&
Devang Patel67a821d2006-12-04 23:54:59 +00001038 IdxState.isConstant())
Chris Lattnere34e9a22007-04-14 23:32:02 +00001039 markConstant(&I,ConstantExpr::getInsertElement(UndefValue::get(I.getType()),
1040 EltState.getConstant(),
1041 IdxState.getConstant()));
Devang Patel67a821d2006-12-04 23:54:59 +00001042#endif
Robert Bocchino8fcf01e2006-01-17 20:06:55 +00001043}
1044
Chris Lattner543abdf2006-04-08 01:19:12 +00001045void SCCPSolver::visitShuffleVectorInst(ShuffleVectorInst &I) {
Devang Patel67a821d2006-12-04 23:54:59 +00001046 // FIXME : SCCP does not handle vectors properly.
1047 markOverdefined(&I);
1048 return;
1049#if 0
Chris Lattner543abdf2006-04-08 01:19:12 +00001050 LatticeVal &V1State = getValueState(I.getOperand(0));
1051 LatticeVal &V2State = getValueState(I.getOperand(1));
1052 LatticeVal &MaskState = getValueState(I.getOperand(2));
1053
1054 if (MaskState.isUndefined() ||
1055 (V1State.isUndefined() && V2State.isUndefined()))
1056 return; // Undefined output if mask or both inputs undefined.
1057
1058 if (V1State.isOverdefined() || V2State.isOverdefined() ||
1059 MaskState.isOverdefined()) {
1060 markOverdefined(&I);
1061 } else {
1062 // A mix of constant/undef inputs.
1063 Constant *V1 = V1State.isConstant() ?
1064 V1State.getConstant() : UndefValue::get(I.getType());
1065 Constant *V2 = V2State.isConstant() ?
1066 V2State.getConstant() : UndefValue::get(I.getType());
1067 Constant *Mask = MaskState.isConstant() ?
1068 MaskState.getConstant() : UndefValue::get(I.getOperand(2)->getType());
1069 markConstant(&I, ConstantExpr::getShuffleVector(V1, V2, Mask));
1070 }
Devang Patel67a821d2006-12-04 23:54:59 +00001071#endif
Chris Lattner543abdf2006-04-08 01:19:12 +00001072}
1073
Chris Lattner2a88bb72002-08-30 23:39:00 +00001074// Handle getelementptr instructions... if all operands are constants then we
1075// can turn this into a getelementptr ConstantExpr.
1076//
Chris Lattner82bec2c2004-11-15 04:44:20 +00001077void SCCPSolver::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattneref36dfd2004-11-15 05:03:30 +00001078 LatticeVal &IV = ValueState[&I];
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +00001079 if (IV.isOverdefined()) return;
1080
Chris Lattnere777ff22007-02-02 20:51:48 +00001081 SmallVector<Constant*, 8> Operands;
Chris Lattner2a88bb72002-08-30 23:39:00 +00001082 Operands.reserve(I.getNumOperands());
1083
1084 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
Chris Lattneref36dfd2004-11-15 05:03:30 +00001085 LatticeVal &State = getValueState(I.getOperand(i));
Chris Lattner2a88bb72002-08-30 23:39:00 +00001086 if (State.isUndefined())
1087 return; // Operands are not resolved yet...
1088 else if (State.isOverdefined()) {
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +00001089 markOverdefined(IV, &I);
Chris Lattner2a88bb72002-08-30 23:39:00 +00001090 return;
1091 }
1092 assert(State.isConstant() && "Unknown state!");
1093 Operands.push_back(State.getConstant());
1094 }
1095
1096 Constant *Ptr = Operands[0];
1097 Operands.erase(Operands.begin()); // Erase the pointer from idx list...
1098
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001099 markConstant(IV, &I, Context->getConstantExprGetElementPtr(Ptr, &Operands[0],
Chris Lattnere777ff22007-02-02 20:51:48 +00001100 Operands.size()));
Chris Lattner2a88bb72002-08-30 23:39:00 +00001101}
Brian Gaeked0fde302003-11-11 22:41:34 +00001102
Chris Lattnerdd336d12004-12-11 05:15:59 +00001103void SCCPSolver::visitStoreInst(Instruction &SI) {
1104 if (TrackedGlobals.empty() || !isa<GlobalVariable>(SI.getOperand(1)))
1105 return;
1106 GlobalVariable *GV = cast<GlobalVariable>(SI.getOperand(1));
Chris Lattnerb59673e2007-02-02 20:38:30 +00001107 DenseMap<GlobalVariable*, LatticeVal>::iterator I = TrackedGlobals.find(GV);
Chris Lattnerdd336d12004-12-11 05:15:59 +00001108 if (I == TrackedGlobals.end() || I->second.isOverdefined()) return;
1109
1110 // Get the value we are storing into the global.
1111 LatticeVal &PtrVal = getValueState(SI.getOperand(0));
1112
1113 mergeInValue(I->second, GV, PtrVal);
1114 if (I->second.isOverdefined())
1115 TrackedGlobals.erase(I); // No need to keep tracking this!
1116}
1117
1118
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +00001119// Handle load instructions. If the operand is a constant pointer to a constant
1120// global, we can replace the load with the loaded constant value!
Chris Lattner82bec2c2004-11-15 04:44:20 +00001121void SCCPSolver::visitLoadInst(LoadInst &I) {
Chris Lattneref36dfd2004-11-15 05:03:30 +00001122 LatticeVal &IV = ValueState[&I];
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +00001123 if (IV.isOverdefined()) return;
1124
Chris Lattneref36dfd2004-11-15 05:03:30 +00001125 LatticeVal &PtrVal = getValueState(I.getOperand(0));
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +00001126 if (PtrVal.isUndefined()) return; // The pointer is not resolved yet!
1127 if (PtrVal.isConstant() && !I.isVolatile()) {
1128 Value *Ptr = PtrVal.getConstant();
Christopher Lambb15147e2007-12-29 07:56:53 +00001129 // TODO: Consider a target hook for valid address spaces for this xform.
1130 if (isa<ConstantPointerNull>(Ptr) &&
1131 cast<PointerType>(Ptr->getType())->getAddressSpace() == 0) {
Chris Lattnerc76d8032004-03-07 22:16:24 +00001132 // load null -> null
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001133 markConstant(IV, &I, Context->getNullValue(I.getType()));
Chris Lattnerc76d8032004-03-07 22:16:24 +00001134 return;
1135 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001136
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +00001137 // Transform load (constant global) into the value loaded.
Chris Lattnerdd336d12004-12-11 05:15:59 +00001138 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) {
1139 if (GV->isConstant()) {
Duncan Sands64da9402009-03-21 21:27:31 +00001140 if (GV->hasDefinitiveInitializer()) {
Chris Lattnerdd336d12004-12-11 05:15:59 +00001141 markConstant(IV, &I, GV->getInitializer());
1142 return;
1143 }
1144 } else if (!TrackedGlobals.empty()) {
1145 // If we are tracking this global, merge in the known value for it.
Chris Lattnerb59673e2007-02-02 20:38:30 +00001146 DenseMap<GlobalVariable*, LatticeVal>::iterator It =
Chris Lattnerdd336d12004-12-11 05:15:59 +00001147 TrackedGlobals.find(GV);
1148 if (It != TrackedGlobals.end()) {
1149 mergeInValue(IV, &I, It->second);
1150 return;
1151 }
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +00001152 }
Chris Lattnerdd336d12004-12-11 05:15:59 +00001153 }
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +00001154
1155 // Transform load (constantexpr_GEP global, 0, ...) into the value loaded.
1156 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
1157 if (CE->getOpcode() == Instruction::GetElementPtr)
Jeff Cohen9d809302005-04-23 21:38:35 +00001158 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0)))
Duncan Sands64da9402009-03-21 21:27:31 +00001159 if (GV->isConstant() && GV->hasDefinitiveInitializer())
Jeff Cohen9d809302005-04-23 21:38:35 +00001160 if (Constant *V =
Owen Anderson50895512009-07-06 18:42:36 +00001161 ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE,
1162 Context)) {
Jeff Cohen9d809302005-04-23 21:38:35 +00001163 markConstant(IV, &I, V);
1164 return;
1165 }
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +00001166 }
1167
1168 // Otherwise we cannot say for certain what value this load will produce.
1169 // Bail out.
1170 markOverdefined(IV, &I);
1171}
Chris Lattner58b7b082004-04-13 19:43:54 +00001172
Chris Lattner59acc7d2004-12-10 08:02:06 +00001173void SCCPSolver::visitCallSite(CallSite CS) {
1174 Function *F = CS.getCalledFunction();
Chris Lattner59acc7d2004-12-10 08:02:06 +00001175 Instruction *I = CS.getInstruction();
Chris Lattnerc6ee00b2008-04-23 05:38:20 +00001176
1177 // The common case is that we aren't tracking the callee, either because we
1178 // are not doing interprocedural analysis or the callee is indirect, or is
1179 // external. Handle these cases first.
Rafael Espindolabb46f522009-01-15 20:18:42 +00001180 if (F == 0 || !F->hasLocalLinkage()) {
Chris Lattnerc6ee00b2008-04-23 05:38:20 +00001181CallOverdefined:
1182 // Void return and not tracking callee, just bail.
1183 if (I->getType() == Type::VoidTy) return;
1184
1185 // Otherwise, if we have a single return value case, and if the function is
1186 // a declaration, maybe we can constant fold it.
1187 if (!isa<StructType>(I->getType()) && F && F->isDeclaration() &&
1188 canConstantFoldCallTo(F)) {
1189
1190 SmallVector<Constant*, 8> Operands;
1191 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
1192 AI != E; ++AI) {
1193 LatticeVal &State = getValueState(*AI);
1194 if (State.isUndefined())
1195 return; // Operands are not resolved yet.
1196 else if (State.isOverdefined()) {
1197 markOverdefined(I);
1198 return;
1199 }
1200 assert(State.isConstant() && "Unknown state!");
1201 Operands.push_back(State.getConstant());
1202 }
1203
1204 // If we can constant fold this, mark the result of the call as a
1205 // constant.
Nick Lewyckye3f1fb12009-05-28 04:08:10 +00001206 if (Constant *C = ConstantFoldCall(F, Operands.data(), Operands.size())) {
Chris Lattnerc6ee00b2008-04-23 05:38:20 +00001207 markConstant(I, C);
1208 return;
1209 }
Chris Lattner58b7b082004-04-13 19:43:54 +00001210 }
Chris Lattnerc6ee00b2008-04-23 05:38:20 +00001211
1212 // Otherwise, we don't know anything about this call, mark it overdefined.
1213 markOverdefined(I);
1214 return;
Chris Lattner58b7b082004-04-13 19:43:54 +00001215 }
1216
Chris Lattnerc6ee00b2008-04-23 05:38:20 +00001217 // If this is a single/zero retval case, see if we're tracking the function.
Dan Gohmanc4b65ea2008-06-20 01:15:44 +00001218 DenseMap<Function*, LatticeVal>::iterator TFRVI = TrackedRetVals.find(F);
1219 if (TFRVI != TrackedRetVals.end()) {
Chris Lattnerc6ee00b2008-04-23 05:38:20 +00001220 // If so, propagate the return value of the callee into this call result.
1221 mergeInValue(I, TFRVI->second);
Dan Gohmanc4b65ea2008-06-20 01:15:44 +00001222 } else if (isa<StructType>(I->getType())) {
Chris Lattnerc6ee00b2008-04-23 05:38:20 +00001223 // Check to see if we're tracking this callee, if not, handle it in the
1224 // common path above.
Chris Lattnercf712de2008-08-23 23:36:38 +00001225 DenseMap<std::pair<Function*, unsigned>, LatticeVal>::iterator
1226 TMRVI = TrackedMultipleRetVals.find(std::make_pair(F, 0));
Chris Lattnerc6ee00b2008-04-23 05:38:20 +00001227 if (TMRVI == TrackedMultipleRetVals.end())
1228 goto CallOverdefined;
1229
1230 // If we are tracking this callee, propagate the return values of the call
Dan Gohmanc4b65ea2008-06-20 01:15:44 +00001231 // into this call site. We do this by walking all the uses. Single-index
1232 // ExtractValueInst uses can be tracked; anything more complicated is
1233 // currently handled conservatively.
Chris Lattnerc6ee00b2008-04-23 05:38:20 +00001234 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
1235 UI != E; ++UI) {
Dan Gohmanc4b65ea2008-06-20 01:15:44 +00001236 if (ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(*UI)) {
1237 if (EVI->getNumIndices() == 1) {
1238 mergeInValue(EVI,
Dan Gohman60ea2682008-06-20 16:41:17 +00001239 TrackedMultipleRetVals[std::make_pair(F, *EVI->idx_begin())]);
Dan Gohmanc4b65ea2008-06-20 01:15:44 +00001240 continue;
1241 }
1242 }
1243 // The aggregate value is used in a way not handled here. Assume nothing.
1244 markOverdefined(*UI);
Chris Lattnerc6ee00b2008-04-23 05:38:20 +00001245 }
Dan Gohmanc4b65ea2008-06-20 01:15:44 +00001246 } else {
1247 // Otherwise we're not tracking this callee, so handle it in the
1248 // common path above.
1249 goto CallOverdefined;
Chris Lattnerc6ee00b2008-04-23 05:38:20 +00001250 }
1251
1252 // Finally, if this is the first call to the function hit, mark its entry
1253 // block executable.
1254 if (!BBExecutable.count(F->begin()))
1255 MarkBlockExecutable(F->begin());
1256
1257 // Propagate information from this call site into the callee.
1258 CallSite::arg_iterator CAI = CS.arg_begin();
1259 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1260 AI != E; ++AI, ++CAI) {
1261 LatticeVal &IV = ValueState[AI];
1262 if (!IV.isOverdefined())
1263 mergeInValue(IV, AI, getValueState(*CAI));
1264 }
Chris Lattner58b7b082004-04-13 19:43:54 +00001265}
Chris Lattner82bec2c2004-11-15 04:44:20 +00001266
1267
1268void SCCPSolver::Solve() {
1269 // Process the work lists until they are empty!
Misha Brukmanfd939082005-04-21 23:48:37 +00001270 while (!BBWorkList.empty() || !InstWorkList.empty() ||
Jeff Cohen9d809302005-04-23 21:38:35 +00001271 !OverdefinedInstWorkList.empty()) {
Chris Lattner82bec2c2004-11-15 04:44:20 +00001272 // Process the instruction work list...
1273 while (!OverdefinedInstWorkList.empty()) {
Chris Lattner59acc7d2004-12-10 08:02:06 +00001274 Value *I = OverdefinedInstWorkList.back();
Chris Lattner82bec2c2004-11-15 04:44:20 +00001275 OverdefinedInstWorkList.pop_back();
1276
Bill Wendlingb7427032006-11-26 09:46:52 +00001277 DOUT << "\nPopped off OI-WL: " << *I;
Misha Brukmanfd939082005-04-21 23:48:37 +00001278
Chris Lattner82bec2c2004-11-15 04:44:20 +00001279 // "I" got into the work list because it either made the transition from
1280 // bottom to constant
1281 //
1282 // Anything on this worklist that is overdefined need not be visited
1283 // since all of its users will have already been marked as overdefined
1284 // Update all of the users of this instruction's value...
1285 //
1286 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
1287 UI != E; ++UI)
1288 OperandChangedState(*UI);
1289 }
1290 // Process the instruction work list...
1291 while (!InstWorkList.empty()) {
Chris Lattner59acc7d2004-12-10 08:02:06 +00001292 Value *I = InstWorkList.back();
Chris Lattner82bec2c2004-11-15 04:44:20 +00001293 InstWorkList.pop_back();
1294
Bill Wendlingb7427032006-11-26 09:46:52 +00001295 DOUT << "\nPopped off I-WL: " << *I;
Misha Brukmanfd939082005-04-21 23:48:37 +00001296
Chris Lattner82bec2c2004-11-15 04:44:20 +00001297 // "I" got into the work list because it either made the transition from
1298 // bottom to constant
1299 //
1300 // Anything on this worklist that is overdefined need not be visited
1301 // since all of its users will have already been marked as overdefined.
1302 // Update all of the users of this instruction's value...
1303 //
1304 if (!getValueState(I).isOverdefined())
1305 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
1306 UI != E; ++UI)
1307 OperandChangedState(*UI);
1308 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001309
Chris Lattner82bec2c2004-11-15 04:44:20 +00001310 // Process the basic block work list...
1311 while (!BBWorkList.empty()) {
1312 BasicBlock *BB = BBWorkList.back();
1313 BBWorkList.pop_back();
Misha Brukmanfd939082005-04-21 23:48:37 +00001314
Bill Wendlingb7427032006-11-26 09:46:52 +00001315 DOUT << "\nPopped off BBWL: " << *BB;
Misha Brukmanfd939082005-04-21 23:48:37 +00001316
Chris Lattner82bec2c2004-11-15 04:44:20 +00001317 // Notify all instructions in this basic block that they are newly
1318 // executable.
1319 visit(BB);
1320 }
1321 }
1322}
1323
Chris Lattner3bad2532006-12-20 06:21:33 +00001324/// ResolvedUndefsIn - While solving the dataflow for a function, we assume
Chris Lattnerfc6ac502004-12-10 20:41:50 +00001325/// that branches on undef values cannot reach any of their successors.
1326/// However, this is not a safe assumption. After we solve dataflow, this
1327/// method should be use to handle this. If this returns true, the solver
1328/// should be rerun.
Chris Lattnerd2d86702006-10-22 05:59:17 +00001329///
1330/// This method handles this by finding an unresolved branch and marking it one
1331/// of the edges from the block as being feasible, even though the condition
1332/// doesn't say it would otherwise be. This allows SCCP to find the rest of the
1333/// CFG and only slightly pessimizes the analysis results (by marking one,
Chris Lattner3bad2532006-12-20 06:21:33 +00001334/// potentially infeasible, edge feasible). This cannot usefully modify the
Chris Lattnerd2d86702006-10-22 05:59:17 +00001335/// constraints on the condition of the branch, as that would impact other users
1336/// of the value.
Chris Lattner3bad2532006-12-20 06:21:33 +00001337///
1338/// This scan also checks for values that use undefs, whose results are actually
1339/// defined. For example, 'zext i8 undef to i32' should produce all zeros
1340/// conservatively, as "(zext i8 X -> i32) & 0xFF00" must always return zero,
1341/// even if X isn't defined.
1342bool SCCPSolver::ResolvedUndefsIn(Function &F) {
Chris Lattnerd2d86702006-10-22 05:59:17 +00001343 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1344 if (!BBExecutable.count(BB))
1345 continue;
Chris Lattner3bad2532006-12-20 06:21:33 +00001346
1347 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
1348 // Look for instructions which produce undef values.
1349 if (I->getType() == Type::VoidTy) continue;
1350
1351 LatticeVal &LV = getValueState(I);
1352 if (!LV.isUndefined()) continue;
1353
1354 // Get the lattice values of the first two operands for use below.
1355 LatticeVal &Op0LV = getValueState(I->getOperand(0));
1356 LatticeVal Op1LV;
1357 if (I->getNumOperands() == 2) {
1358 // If this is a two-operand instruction, and if both operands are
1359 // undefs, the result stays undef.
1360 Op1LV = getValueState(I->getOperand(1));
1361 if (Op0LV.isUndefined() && Op1LV.isUndefined())
1362 continue;
1363 }
1364
1365 // If this is an instructions whose result is defined even if the input is
1366 // not fully defined, propagate the information.
1367 const Type *ITy = I->getType();
1368 switch (I->getOpcode()) {
1369 default: break; // Leave the instruction as an undef.
1370 case Instruction::ZExt:
1371 // After a zero extend, we know the top part is zero. SExt doesn't have
1372 // to be handled here, because we don't know whether the top part is 1's
1373 // or 0's.
1374 assert(Op0LV.isUndefined());
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001375 markForcedConstant(LV, I, Context->getNullValue(ITy));
Chris Lattner3bad2532006-12-20 06:21:33 +00001376 return true;
1377 case Instruction::Mul:
1378 case Instruction::And:
1379 // undef * X -> 0. X could be zero.
1380 // undef & X -> 0. X could be zero.
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001381 markForcedConstant(LV, I, Context->getNullValue(ITy));
Chris Lattner3bad2532006-12-20 06:21:33 +00001382 return true;
1383
1384 case Instruction::Or:
1385 // undef | X -> -1. X could be -1.
Reid Spencer9d6565a2007-02-15 02:26:10 +00001386 if (const VectorType *PTy = dyn_cast<VectorType>(ITy))
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001387 markForcedConstant(LV, I,
1388 Context->getConstantVectorAllOnesValue(PTy));
Chris Lattner7ce2f8b2007-01-04 02:12:40 +00001389 else
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001390 markForcedConstant(LV, I, Context->getConstantIntAllOnesValue(ITy));
Chris Lattner7ce2f8b2007-01-04 02:12:40 +00001391 return true;
Chris Lattner3bad2532006-12-20 06:21:33 +00001392
1393 case Instruction::SDiv:
1394 case Instruction::UDiv:
1395 case Instruction::SRem:
1396 case Instruction::URem:
1397 // X / undef -> undef. No change.
1398 // X % undef -> undef. No change.
1399 if (Op1LV.isUndefined()) break;
1400
1401 // undef / X -> 0. X could be maxint.
1402 // undef % X -> 0. X could be 1.
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001403 markForcedConstant(LV, I, Context->getNullValue(ITy));
Chris Lattner3bad2532006-12-20 06:21:33 +00001404 return true;
1405
1406 case Instruction::AShr:
1407 // undef >>s X -> undef. No change.
1408 if (Op0LV.isUndefined()) break;
1409
1410 // X >>s undef -> X. X could be 0, X could have the high-bit known set.
1411 if (Op0LV.isConstant())
1412 markForcedConstant(LV, I, Op0LV.getConstant());
1413 else
1414 markOverdefined(LV, I);
1415 return true;
1416 case Instruction::LShr:
1417 case Instruction::Shl:
1418 // undef >> X -> undef. No change.
1419 // undef << X -> undef. No change.
1420 if (Op0LV.isUndefined()) break;
1421
1422 // X >> undef -> 0. X could be 0.
1423 // X << undef -> 0. X could be 0.
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001424 markForcedConstant(LV, I, Context->getNullValue(ITy));
Chris Lattner3bad2532006-12-20 06:21:33 +00001425 return true;
1426 case Instruction::Select:
1427 // undef ? X : Y -> X or Y. There could be commonality between X/Y.
1428 if (Op0LV.isUndefined()) {
1429 if (!Op1LV.isConstant()) // Pick the constant one if there is any.
1430 Op1LV = getValueState(I->getOperand(2));
1431 } else if (Op1LV.isUndefined()) {
1432 // c ? undef : undef -> undef. No change.
1433 Op1LV = getValueState(I->getOperand(2));
1434 if (Op1LV.isUndefined())
1435 break;
1436 // Otherwise, c ? undef : x -> x.
1437 } else {
1438 // Leave Op1LV as Operand(1)'s LatticeValue.
1439 }
1440
1441 if (Op1LV.isConstant())
1442 markForcedConstant(LV, I, Op1LV.getConstant());
1443 else
1444 markOverdefined(LV, I);
1445 return true;
Chris Lattner60301602008-05-24 03:59:33 +00001446 case Instruction::Call:
1447 // If a call has an undef result, it is because it is constant foldable
1448 // but one of the inputs was undef. Just force the result to
1449 // overdefined.
1450 markOverdefined(LV, I);
1451 return true;
Chris Lattner3bad2532006-12-20 06:21:33 +00001452 }
1453 }
Chris Lattnerd2d86702006-10-22 05:59:17 +00001454
1455 TerminatorInst *TI = BB->getTerminator();
1456 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
1457 if (!BI->isConditional()) continue;
1458 if (!getValueState(BI->getCondition()).isUndefined())
1459 continue;
1460 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
Dale Johannesen9bca5832008-05-23 01:01:31 +00001461 if (SI->getNumSuccessors()<2) // no cases
1462 continue;
Chris Lattnerd2d86702006-10-22 05:59:17 +00001463 if (!getValueState(SI->getCondition()).isUndefined())
1464 continue;
1465 } else {
1466 continue;
Chris Lattnerfc6ac502004-12-10 20:41:50 +00001467 }
Chris Lattnerd2d86702006-10-22 05:59:17 +00001468
Chris Lattner05bb7892008-01-28 00:32:30 +00001469 // If the edge to the second successor isn't thought to be feasible yet,
1470 // mark it so now. We pick the second one so that this goes to some
1471 // enumerated value in a switch instead of going to the default destination.
1472 if (KnownFeasibleEdges.count(Edge(BB, TI->getSuccessor(1))))
Chris Lattnerd2d86702006-10-22 05:59:17 +00001473 continue;
1474
1475 // Otherwise, it isn't already thought to be feasible. Mark it as such now
1476 // and return. This will make other blocks reachable, which will allow new
1477 // values to be discovered and existing ones to be moved in the lattice.
Chris Lattner05bb7892008-01-28 00:32:30 +00001478 markEdgeExecutable(BB, TI->getSuccessor(1));
1479
1480 // This must be a conditional branch of switch on undef. At this point,
1481 // force the old terminator to branch to the first successor. This is
1482 // required because we are now influencing the dataflow of the function with
1483 // the assumption that this edge is taken. If we leave the branch condition
1484 // as undef, then further analysis could think the undef went another way
1485 // leading to an inconsistent set of conclusions.
1486 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001487 BI->setCondition(Context->getConstantIntFalse());
Chris Lattner05bb7892008-01-28 00:32:30 +00001488 } else {
1489 SwitchInst *SI = cast<SwitchInst>(TI);
1490 SI->setCondition(SI->getCaseValue(1));
1491 }
1492
Chris Lattnerd2d86702006-10-22 05:59:17 +00001493 return true;
1494 }
Chris Lattnerdade2d22004-12-11 06:05:53 +00001495
Chris Lattnerd2d86702006-10-22 05:59:17 +00001496 return false;
Chris Lattnerfc6ac502004-12-10 20:41:50 +00001497}
1498
Chris Lattner82bec2c2004-11-15 04:44:20 +00001499
1500namespace {
Chris Lattner14051812004-11-15 07:15:04 +00001501 //===--------------------------------------------------------------------===//
Chris Lattner82bec2c2004-11-15 04:44:20 +00001502 //
Chris Lattner14051812004-11-15 07:15:04 +00001503 /// SCCP Class - This class uses the SCCPSolver to implement a per-function
Reid Spenceree5d25e2006-12-31 22:26:06 +00001504 /// Sparse Conditional Constant Propagator.
Chris Lattner14051812004-11-15 07:15:04 +00001505 ///
Reid Spencer9133fe22007-02-05 23:32:05 +00001506 struct VISIBILITY_HIDDEN SCCP : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +00001507 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +00001508 SCCP() : FunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +00001509
Chris Lattner14051812004-11-15 07:15:04 +00001510 // runOnFunction - Run the Sparse Conditional Constant Propagation
1511 // algorithm, and return true if the function was modified.
1512 //
1513 bool runOnFunction(Function &F);
Misha Brukmanfd939082005-04-21 23:48:37 +00001514
Chris Lattner14051812004-11-15 07:15:04 +00001515 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1516 AU.setPreservesCFG();
1517 }
1518 };
Chris Lattner82bec2c2004-11-15 04:44:20 +00001519} // end anonymous namespace
1520
Dan Gohman844731a2008-05-13 00:00:25 +00001521char SCCP::ID = 0;
1522static RegisterPass<SCCP>
1523X("sccp", "Sparse Conditional Constant Propagation");
Chris Lattner82bec2c2004-11-15 04:44:20 +00001524
1525// createSCCPPass - This is the public interface to this file...
1526FunctionPass *llvm::createSCCPPass() {
1527 return new SCCP();
1528}
1529
1530
Chris Lattner82bec2c2004-11-15 04:44:20 +00001531// runOnFunction() - Run the Sparse Conditional Constant Propagation algorithm,
1532// and return true if the function was modified.
1533//
1534bool SCCP::runOnFunction(Function &F) {
Chris Lattner5c8e8d72008-05-11 01:55:59 +00001535 DOUT << "SCCP on function '" << F.getNameStart() << "'\n";
Chris Lattner82bec2c2004-11-15 04:44:20 +00001536 SCCPSolver Solver;
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001537 Solver.setContext(Context);
Chris Lattner82bec2c2004-11-15 04:44:20 +00001538
1539 // Mark the first block of the function as being executable.
1540 Solver.MarkBlockExecutable(F.begin());
1541
Chris Lattner7e529e42004-11-15 05:45:33 +00001542 // Mark all arguments to the function as being overdefined.
Chris Lattnere34e9a22007-04-14 23:32:02 +00001543 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E;++AI)
Chris Lattner57939df2007-03-04 04:50:21 +00001544 Solver.markOverdefined(AI);
Chris Lattner7e529e42004-11-15 05:45:33 +00001545
Chris Lattner82bec2c2004-11-15 04:44:20 +00001546 // Solve for constants.
Chris Lattner3bad2532006-12-20 06:21:33 +00001547 bool ResolvedUndefs = true;
1548 while (ResolvedUndefs) {
Chris Lattnerfc6ac502004-12-10 20:41:50 +00001549 Solver.Solve();
Chris Lattner3bad2532006-12-20 06:21:33 +00001550 DOUT << "RESOLVING UNDEFs\n";
1551 ResolvedUndefs = Solver.ResolvedUndefsIn(F);
Chris Lattnerfc6ac502004-12-10 20:41:50 +00001552 }
Chris Lattner82bec2c2004-11-15 04:44:20 +00001553
Chris Lattner7e529e42004-11-15 05:45:33 +00001554 bool MadeChanges = false;
1555
1556 // If we decided that there are basic blocks that are dead in this function,
1557 // delete their contents now. Note that we cannot actually delete the blocks,
1558 // as we cannot modify the CFG of the function.
1559 //
Chris Lattnercf712de2008-08-23 23:36:38 +00001560 SmallVector<Instruction*, 512> Insts;
Bill Wendling7a7cf6b2008-08-14 23:05:24 +00001561 std::map<Value*, LatticeVal> &Values = Solver.getValueMapping();
Chris Lattner57939df2007-03-04 04:50:21 +00001562
Chris Lattner7e529e42004-11-15 05:45:33 +00001563 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
Chris Lattner7eb01bf2008-08-23 23:39:31 +00001564 if (!Solver.isBlockExecutable(BB)) {
Bill Wendlingb7427032006-11-26 09:46:52 +00001565 DOUT << " BasicBlock Dead:" << *BB;
Chris Lattnerb77d5d82004-11-15 07:02:42 +00001566 ++NumDeadBlocks;
1567
Chris Lattner7e529e42004-11-15 05:45:33 +00001568 // Delete the instructions backwards, as it has a reduced likelihood of
1569 // having to update as many def-use and use-def chains.
Chris Lattner7e529e42004-11-15 05:45:33 +00001570 for (BasicBlock::iterator I = BB->begin(), E = BB->getTerminator();
1571 I != E; ++I)
1572 Insts.push_back(I);
1573 while (!Insts.empty()) {
1574 Instruction *I = Insts.back();
1575 Insts.pop_back();
1576 if (!I->use_empty())
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001577 I->replaceAllUsesWith(Context->getUndef(I->getType()));
Chris Lattner7e529e42004-11-15 05:45:33 +00001578 BB->getInstList().erase(I);
1579 MadeChanges = true;
Chris Lattnerb77d5d82004-11-15 07:02:42 +00001580 ++NumInstRemoved;
Chris Lattner7e529e42004-11-15 05:45:33 +00001581 }
Chris Lattner59acc7d2004-12-10 08:02:06 +00001582 } else {
1583 // Iterate over all of the instructions in a function, replacing them with
1584 // constants if we have found them to be of constant values.
1585 //
1586 for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
1587 Instruction *Inst = BI++;
Chris Lattner7cb22ec2008-04-24 00:19:54 +00001588 if (Inst->getType() == Type::VoidTy ||
Chris Lattnerf4023a12008-04-24 00:16:28 +00001589 isa<TerminatorInst>(Inst))
1590 continue;
1591
1592 LatticeVal &IV = Values[Inst];
1593 if (!IV.isConstant() && !IV.isUndefined())
1594 continue;
1595
1596 Constant *Const = IV.isConstant()
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001597 ? IV.getConstant() : Context->getUndef(Inst->getType());
Chris Lattnerf4023a12008-04-24 00:16:28 +00001598 DOUT << " Constant: " << *Const << " = " << *Inst;
Misha Brukmanfd939082005-04-21 23:48:37 +00001599
Chris Lattnerf4023a12008-04-24 00:16:28 +00001600 // Replaces all of the uses of a variable with uses of the constant.
1601 Inst->replaceAllUsesWith(Const);
1602
1603 // Delete the instruction.
1604 Inst->eraseFromParent();
1605
1606 // Hey, we just changed something!
1607 MadeChanges = true;
1608 ++NumInstRemoved;
Chris Lattner82bec2c2004-11-15 04:44:20 +00001609 }
1610 }
1611
1612 return MadeChanges;
1613}
Chris Lattner59acc7d2004-12-10 08:02:06 +00001614
1615namespace {
Chris Lattner59acc7d2004-12-10 08:02:06 +00001616 //===--------------------------------------------------------------------===//
1617 //
1618 /// IPSCCP Class - This class implements interprocedural Sparse Conditional
1619 /// Constant Propagation.
1620 ///
Reid Spencer9133fe22007-02-05 23:32:05 +00001621 struct VISIBILITY_HIDDEN IPSCCP : public ModulePass {
Devang Patel19974732007-05-03 01:11:54 +00001622 static char ID;
Dan Gohmanae73dc12008-09-04 17:05:41 +00001623 IPSCCP() : ModulePass(&ID) {}
Chris Lattner59acc7d2004-12-10 08:02:06 +00001624 bool runOnModule(Module &M);
1625 };
Chris Lattner59acc7d2004-12-10 08:02:06 +00001626} // end anonymous namespace
1627
Dan Gohman844731a2008-05-13 00:00:25 +00001628char IPSCCP::ID = 0;
1629static RegisterPass<IPSCCP>
1630Y("ipsccp", "Interprocedural Sparse Conditional Constant Propagation");
1631
Chris Lattner59acc7d2004-12-10 08:02:06 +00001632// createIPSCCPPass - This is the public interface to this file...
1633ModulePass *llvm::createIPSCCPPass() {
1634 return new IPSCCP();
1635}
1636
1637
1638static bool AddressIsTaken(GlobalValue *GV) {
Chris Lattner7d27fc02005-04-19 19:16:19 +00001639 // Delete any dead constantexpr klingons.
1640 GV->removeDeadConstantUsers();
1641
Chris Lattner59acc7d2004-12-10 08:02:06 +00001642 for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end();
1643 UI != E; ++UI)
1644 if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
Chris Lattnerdd336d12004-12-11 05:15:59 +00001645 if (SI->getOperand(0) == GV || SI->isVolatile())
1646 return true; // Storing addr of GV.
Chris Lattner59acc7d2004-12-10 08:02:06 +00001647 } else if (isa<InvokeInst>(*UI) || isa<CallInst>(*UI)) {
1648 // Make sure we are calling the function, not passing the address.
1649 CallSite CS = CallSite::get(cast<Instruction>(*UI));
Nick Lewyckyaf386132008-11-03 03:49:14 +00001650 if (CS.hasArgument(GV))
1651 return true;
Chris Lattnerdd336d12004-12-11 05:15:59 +00001652 } else if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
1653 if (LI->isVolatile())
1654 return true;
1655 } else {
Chris Lattner59acc7d2004-12-10 08:02:06 +00001656 return true;
1657 }
1658 return false;
1659}
1660
1661bool IPSCCP::runOnModule(Module &M) {
1662 SCCPSolver Solver;
1663
1664 // Loop over all functions, marking arguments to those with their addresses
1665 // taken or that are external as overdefined.
1666 //
Chris Lattner59acc7d2004-12-10 08:02:06 +00001667 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
Rafael Espindolabb46f522009-01-15 20:18:42 +00001668 if (!F->hasLocalLinkage() || AddressIsTaken(F)) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001669 if (!F->isDeclaration())
Chris Lattner59acc7d2004-12-10 08:02:06 +00001670 Solver.MarkBlockExecutable(F->begin());
Chris Lattner7d27fc02005-04-19 19:16:19 +00001671 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1672 AI != E; ++AI)
Chris Lattner57939df2007-03-04 04:50:21 +00001673 Solver.markOverdefined(AI);
Chris Lattner59acc7d2004-12-10 08:02:06 +00001674 } else {
1675 Solver.AddTrackedFunction(F);
1676 }
1677
Chris Lattnerdd336d12004-12-11 05:15:59 +00001678 // Loop over global variables. We inform the solver about any internal global
1679 // variables that do not have their 'addresses taken'. If they don't have
1680 // their addresses taken, we can propagate constants through them.
Chris Lattner7d27fc02005-04-19 19:16:19 +00001681 for (Module::global_iterator G = M.global_begin(), E = M.global_end();
1682 G != E; ++G)
Rafael Espindolabb46f522009-01-15 20:18:42 +00001683 if (!G->isConstant() && G->hasLocalLinkage() && !AddressIsTaken(G))
Chris Lattnerdd336d12004-12-11 05:15:59 +00001684 Solver.TrackValueOfGlobalVariable(G);
1685
Chris Lattner59acc7d2004-12-10 08:02:06 +00001686 // Solve for constants.
Chris Lattner3bad2532006-12-20 06:21:33 +00001687 bool ResolvedUndefs = true;
1688 while (ResolvedUndefs) {
Chris Lattnerfc6ac502004-12-10 20:41:50 +00001689 Solver.Solve();
1690
Chris Lattner3bad2532006-12-20 06:21:33 +00001691 DOUT << "RESOLVING UNDEFS\n";
1692 ResolvedUndefs = false;
Chris Lattnerfc6ac502004-12-10 20:41:50 +00001693 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
Chris Lattner3bad2532006-12-20 06:21:33 +00001694 ResolvedUndefs |= Solver.ResolvedUndefsIn(*F);
Chris Lattnerfc6ac502004-12-10 20:41:50 +00001695 }
Chris Lattner59acc7d2004-12-10 08:02:06 +00001696
1697 bool MadeChanges = false;
1698
1699 // Iterate over all of the instructions in the module, replacing them with
1700 // constants if we have found them to be of constant values.
1701 //
Chris Lattnercf712de2008-08-23 23:36:38 +00001702 SmallVector<Instruction*, 512> Insts;
1703 SmallVector<BasicBlock*, 512> BlocksToErase;
Bill Wendling7a7cf6b2008-08-14 23:05:24 +00001704 std::map<Value*, LatticeVal> &Values = Solver.getValueMapping();
Chris Lattner1c1f1122007-02-02 21:15:06 +00001705
Chris Lattner59acc7d2004-12-10 08:02:06 +00001706 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
Chris Lattner7d27fc02005-04-19 19:16:19 +00001707 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1708 AI != E; ++AI)
Chris Lattner59acc7d2004-12-10 08:02:06 +00001709 if (!AI->use_empty()) {
1710 LatticeVal &IV = Values[AI];
1711 if (IV.isConstant() || IV.isUndefined()) {
1712 Constant *CST = IV.isConstant() ?
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001713 IV.getConstant() : Context->getUndef(AI->getType());
Bill Wendlingb7427032006-11-26 09:46:52 +00001714 DOUT << "*** Arg " << *AI << " = " << *CST <<"\n";
Misha Brukmanfd939082005-04-21 23:48:37 +00001715
Chris Lattner59acc7d2004-12-10 08:02:06 +00001716 // Replaces all of the uses of a variable with uses of the
1717 // constant.
1718 AI->replaceAllUsesWith(CST);
1719 ++IPNumArgsElimed;
1720 }
1721 }
1722
1723 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
Chris Lattner7eb01bf2008-08-23 23:39:31 +00001724 if (!Solver.isBlockExecutable(BB)) {
Bill Wendlingb7427032006-11-26 09:46:52 +00001725 DOUT << " BasicBlock Dead:" << *BB;
Chris Lattner59acc7d2004-12-10 08:02:06 +00001726 ++IPNumDeadBlocks;
Chris Lattnerfc6ac502004-12-10 20:41:50 +00001727
Chris Lattner59acc7d2004-12-10 08:02:06 +00001728 // Delete the instructions backwards, as it has a reduced likelihood of
1729 // having to update as many def-use and use-def chains.
Chris Lattner5f9e8b42004-12-10 22:29:08 +00001730 TerminatorInst *TI = BB->getTerminator();
1731 for (BasicBlock::iterator I = BB->begin(), E = TI; I != E; ++I)
Chris Lattner59acc7d2004-12-10 08:02:06 +00001732 Insts.push_back(I);
Chris Lattner5f9e8b42004-12-10 22:29:08 +00001733
Chris Lattner59acc7d2004-12-10 08:02:06 +00001734 while (!Insts.empty()) {
1735 Instruction *I = Insts.back();
1736 Insts.pop_back();
1737 if (!I->use_empty())
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001738 I->replaceAllUsesWith(Context->getUndef(I->getType()));
Chris Lattner59acc7d2004-12-10 08:02:06 +00001739 BB->getInstList().erase(I);
1740 MadeChanges = true;
1741 ++IPNumInstRemoved;
1742 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001743
Chris Lattner5f9e8b42004-12-10 22:29:08 +00001744 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
1745 BasicBlock *Succ = TI->getSuccessor(i);
Dan Gohmancb406c22007-10-03 19:26:29 +00001746 if (!Succ->empty() && isa<PHINode>(Succ->begin()))
Chris Lattner5f9e8b42004-12-10 22:29:08 +00001747 TI->getSuccessor(i)->removePredecessor(BB);
1748 }
Chris Lattner0417feb2004-12-11 02:53:57 +00001749 if (!TI->use_empty())
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001750 TI->replaceAllUsesWith(Context->getUndef(TI->getType()));
Chris Lattner5f9e8b42004-12-10 22:29:08 +00001751 BB->getInstList().erase(TI);
1752
Chris Lattner864737b2004-12-11 05:32:19 +00001753 if (&*BB != &F->front())
1754 BlocksToErase.push_back(BB);
1755 else
1756 new UnreachableInst(BB);
1757
Chris Lattner59acc7d2004-12-10 08:02:06 +00001758 } else {
1759 for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
1760 Instruction *Inst = BI++;
Chris Lattnerd9d46242009-01-14 21:01:16 +00001761 if (Inst->getType() == Type::VoidTy)
Chris Lattnereb5f4092008-04-24 00:21:50 +00001762 continue;
1763
1764 LatticeVal &IV = Values[Inst];
1765 if (!IV.isConstant() && !IV.isUndefined())
1766 continue;
1767
1768 Constant *Const = IV.isConstant()
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001769 ? IV.getConstant() : Context->getUndef(Inst->getType());
Chris Lattnereb5f4092008-04-24 00:21:50 +00001770 DOUT << " Constant: " << *Const << " = " << *Inst;
Misha Brukmanfd939082005-04-21 23:48:37 +00001771
Chris Lattnereb5f4092008-04-24 00:21:50 +00001772 // Replaces all of the uses of a variable with uses of the
1773 // constant.
1774 Inst->replaceAllUsesWith(Const);
1775
1776 // Delete the instruction.
Chris Lattnerd9d46242009-01-14 21:01:16 +00001777 if (!isa<CallInst>(Inst) && !isa<TerminatorInst>(Inst))
Chris Lattnereb5f4092008-04-24 00:21:50 +00001778 Inst->eraseFromParent();
Misha Brukmanfd939082005-04-21 23:48:37 +00001779
Chris Lattnereb5f4092008-04-24 00:21:50 +00001780 // Hey, we just changed something!
1781 MadeChanges = true;
1782 ++IPNumInstRemoved;
Chris Lattner59acc7d2004-12-10 08:02:06 +00001783 }
1784 }
Chris Lattner5f9e8b42004-12-10 22:29:08 +00001785
1786 // Now that all instructions in the function are constant folded, erase dead
1787 // blocks, because we can now use ConstantFoldTerminator to get rid of
1788 // in-edges.
1789 for (unsigned i = 0, e = BlocksToErase.size(); i != e; ++i) {
1790 // If there are any PHI nodes in this successor, drop entries for BB now.
1791 BasicBlock *DeadBB = BlocksToErase[i];
1792 while (!DeadBB->use_empty()) {
1793 Instruction *I = cast<Instruction>(DeadBB->use_back());
1794 bool Folded = ConstantFoldTerminator(I->getParent());
Chris Lattnerddaaa372006-10-23 18:57:02 +00001795 if (!Folded) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001796 // The constant folder may not have been able to fold the terminator
Chris Lattnerddaaa372006-10-23 18:57:02 +00001797 // if this is a branch or switch on undef. Fold it manually as a
1798 // branch to the first successor.
Devang Patelcb9a3542008-11-21 01:52:59 +00001799#ifndef NDEBUG
Chris Lattnerddaaa372006-10-23 18:57:02 +00001800 if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
1801 assert(BI->isConditional() && isa<UndefValue>(BI->getCondition()) &&
1802 "Branch should be foldable!");
1803 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
1804 assert(isa<UndefValue>(SI->getCondition()) && "Switch should fold");
1805 } else {
1806 assert(0 && "Didn't fold away reference to block!");
1807 }
Devang Patelcb9a3542008-11-21 01:52:59 +00001808#endif
Chris Lattnerddaaa372006-10-23 18:57:02 +00001809
1810 // Make this an uncond branch to the first successor.
1811 TerminatorInst *TI = I->getParent()->getTerminator();
Gabor Greif051a9502008-04-06 20:25:17 +00001812 BranchInst::Create(TI->getSuccessor(0), TI);
Chris Lattnerddaaa372006-10-23 18:57:02 +00001813
1814 // Remove entries in successor phi nodes to remove edges.
1815 for (unsigned i = 1, e = TI->getNumSuccessors(); i != e; ++i)
1816 TI->getSuccessor(i)->removePredecessor(TI->getParent());
1817
1818 // Remove the old terminator.
1819 TI->eraseFromParent();
1820 }
Chris Lattner5f9e8b42004-12-10 22:29:08 +00001821 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001822
Chris Lattner5f9e8b42004-12-10 22:29:08 +00001823 // Finally, delete the basic block.
1824 F->getBasicBlockList().erase(DeadBB);
1825 }
Chris Lattner1c1f1122007-02-02 21:15:06 +00001826 BlocksToErase.clear();
Chris Lattner59acc7d2004-12-10 08:02:06 +00001827 }
Chris Lattner0417feb2004-12-11 02:53:57 +00001828
1829 // If we inferred constant or undef return values for a function, we replaced
1830 // all call uses with the inferred value. This means we don't need to bother
1831 // actually returning anything from the function. Replace all return
1832 // instructions with return undef.
Devang Patel9af014f2008-03-11 17:32:05 +00001833 // TODO: Process multiple value ret instructions also.
Devang Patel7c490d42008-03-11 05:46:42 +00001834 const DenseMap<Function*, LatticeVal> &RV = Solver.getTrackedRetVals();
Chris Lattnerb59673e2007-02-02 20:38:30 +00001835 for (DenseMap<Function*, LatticeVal>::const_iterator I = RV.begin(),
Chris Lattner0417feb2004-12-11 02:53:57 +00001836 E = RV.end(); I != E; ++I)
1837 if (!I->second.isOverdefined() &&
1838 I->first->getReturnType() != Type::VoidTy) {
1839 Function *F = I->first;
1840 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
1841 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))
1842 if (!isa<UndefValue>(RI->getOperand(0)))
Owen Andersonfa5cbd62009-07-03 19:42:02 +00001843 RI->setOperand(0, Context->getUndef(F->getReturnType()));
Chris Lattner0417feb2004-12-11 02:53:57 +00001844 }
Chris Lattnerdd336d12004-12-11 05:15:59 +00001845
1846 // If we infered constant or undef values for globals variables, we can delete
1847 // the global and any stores that remain to it.
Chris Lattnerb59673e2007-02-02 20:38:30 +00001848 const DenseMap<GlobalVariable*, LatticeVal> &TG = Solver.getTrackedGlobals();
1849 for (DenseMap<GlobalVariable*, LatticeVal>::const_iterator I = TG.begin(),
Chris Lattnerdd336d12004-12-11 05:15:59 +00001850 E = TG.end(); I != E; ++I) {
1851 GlobalVariable *GV = I->first;
1852 assert(!I->second.isOverdefined() &&
1853 "Overdefined values should have been taken out of the map!");
Chris Lattner5c8e8d72008-05-11 01:55:59 +00001854 DOUT << "Found that GV '" << GV->getNameStart() << "' is constant!\n";
Chris Lattnerdd336d12004-12-11 05:15:59 +00001855 while (!GV->use_empty()) {
1856 StoreInst *SI = cast<StoreInst>(GV->use_back());
1857 SI->eraseFromParent();
1858 }
1859 M.getGlobalList().erase(GV);
Chris Lattnerdade2d22004-12-11 06:05:53 +00001860 ++IPNumGlobalConst;
Chris Lattnerdd336d12004-12-11 05:15:59 +00001861 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001862
Chris Lattner59acc7d2004-12-10 08:02:06 +00001863 return MadeChanges;
1864}