blob: 5d928c145dfeac5b3a852a1e309426adfdbdbeee [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000030#include "llvm/Pass.h"
Chris Lattner2a632552002-04-18 15:13:15 +000031#include "llvm/Support/InstVisitor.h"
Chris Lattner58b7b082004-04-13 19:43:54 +000032#include "llvm/Transforms/Utils/Local.h"
Chris Lattner59acc7d2004-12-10 08:02:06 +000033#include "llvm/Support/CallSite.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000034#include "llvm/Support/Debug.h"
35#include "llvm/ADT/hash_map"
36#include "llvm/ADT/Statistic.h"
37#include "llvm/ADT/STLExtras.h"
Chris Lattner138a1242001-06-27 23:38:11 +000038#include <algorithm>
Chris Lattner138a1242001-06-27 23:38:11 +000039#include <set>
Chris Lattnerd7456022004-01-09 06:02:20 +000040using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000041
Chris Lattneref36dfd2004-11-15 05:03:30 +000042// LatticeVal class - This class represents the different lattice values that an
Chris Lattnerf57b8452002-04-27 06:56:12 +000043// instruction may occupy. It is a simple class with value semantics.
Chris Lattner138a1242001-06-27 23:38:11 +000044//
Chris Lattner0dbfc052002-04-29 21:26:08 +000045namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000046
Chris Lattneref36dfd2004-11-15 05:03:30 +000047class LatticeVal {
Misha Brukmanfd939082005-04-21 23:48:37 +000048 enum {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000049 undefined, // This instruction has no known value
50 constant, // This instruction has a constant value
Chris Lattnere9bb2df2001-12-03 22:26:30 +000051 overdefined // This instruction has an unknown value
52 } LatticeValue; // The current lattice position
53 Constant *ConstantVal; // If Constant value, the current value
Chris Lattner138a1242001-06-27 23:38:11 +000054public:
Chris Lattneref36dfd2004-11-15 05:03:30 +000055 inline LatticeVal() : LatticeValue(undefined), ConstantVal(0) {}
Chris Lattner138a1242001-06-27 23:38:11 +000056
57 // markOverdefined - Return true if this is a new status to be in...
58 inline bool markOverdefined() {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000059 if (LatticeValue != overdefined) {
60 LatticeValue = overdefined;
Chris Lattner138a1242001-06-27 23:38:11 +000061 return true;
62 }
63 return false;
64 }
65
66 // markConstant - Return true if this is a new status for us...
Chris Lattnere9bb2df2001-12-03 22:26:30 +000067 inline bool markConstant(Constant *V) {
68 if (LatticeValue != constant) {
69 LatticeValue = constant;
Chris Lattner138a1242001-06-27 23:38:11 +000070 ConstantVal = V;
71 return true;
72 } else {
Chris Lattnerb70d82f2001-09-07 16:43:22 +000073 assert(ConstantVal == V && "Marking constant with different value");
Chris Lattner138a1242001-06-27 23:38:11 +000074 }
75 return false;
76 }
77
Chris Lattnere9bb2df2001-12-03 22:26:30 +000078 inline bool isUndefined() const { return LatticeValue == undefined; }
79 inline bool isConstant() const { return LatticeValue == constant; }
80 inline bool isOverdefined() const { return LatticeValue == overdefined; }
Chris Lattner138a1242001-06-27 23:38:11 +000081
Chris Lattner1daee8b2004-01-12 03:57:30 +000082 inline Constant *getConstant() const {
83 assert(isConstant() && "Cannot get the constant of a non-constant!");
84 return ConstantVal;
85 }
Chris Lattner138a1242001-06-27 23:38:11 +000086};
87
Chris Lattner0dbfc052002-04-29 21:26:08 +000088} // end anonymous namespace
Chris Lattner138a1242001-06-27 23:38:11 +000089
90
91//===----------------------------------------------------------------------===//
Chris Lattner138a1242001-06-27 23:38:11 +000092//
Chris Lattner82bec2c2004-11-15 04:44:20 +000093/// SCCPSolver - This class is a general purpose solver for Sparse Conditional
94/// Constant Propagation.
95///
96class SCCPSolver : public InstVisitor<SCCPSolver> {
Chris Lattner697954c2002-01-20 22:54:45 +000097 std::set<BasicBlock*> BBExecutable;// The basic blocks that are executable
Chris Lattneref36dfd2004-11-15 05:03:30 +000098 hash_map<Value*, LatticeVal> ValueState; // The state each value is in...
Chris Lattner138a1242001-06-27 23:38:11 +000099
Chris Lattnerdd336d12004-12-11 05:15:59 +0000100 /// GlobalValue - If we are tracking any values for the contents of a global
101 /// variable, we keep a mapping from the constant accessor to the element of
102 /// the global, to the currently known value. If the value becomes
103 /// overdefined, it's entry is simply removed from this map.
104 hash_map<GlobalVariable*, LatticeVal> TrackedGlobals;
105
Chris Lattner59acc7d2004-12-10 08:02:06 +0000106 /// TrackedFunctionRetVals - If we are tracking arguments into and the return
107 /// value out of a function, it will have an entry in this map, indicating
108 /// what the known return value for the function is.
109 hash_map<Function*, LatticeVal> TrackedFunctionRetVals;
110
Chris Lattner80b2d6c2004-07-15 23:36:43 +0000111 // The reason for two worklists is that overdefined is the lowest state
112 // on the lattice, and moving things to overdefined as fast as possible
113 // makes SCCP converge much faster.
114 // By having a separate worklist, we accomplish this because everything
115 // possibly overdefined will become overdefined at the soonest possible
116 // point.
Chris Lattner59acc7d2004-12-10 08:02:06 +0000117 std::vector<Value*> OverdefinedInstWorkList;
118 std::vector<Value*> InstWorkList;
Chris Lattner80b2d6c2004-07-15 23:36:43 +0000119
120
Chris Lattner697954c2002-01-20 22:54:45 +0000121 std::vector<BasicBlock*> BBWorkList; // The BasicBlock work list
Chris Lattner16b18fd2003-10-08 16:55:34 +0000122
Chris Lattner1daee8b2004-01-12 03:57:30 +0000123 /// UsersOfOverdefinedPHIs - Keep track of any users of PHI nodes that are not
124 /// overdefined, despite the fact that the PHI node is overdefined.
125 std::multimap<PHINode*, Instruction*> UsersOfOverdefinedPHIs;
126
Chris Lattner16b18fd2003-10-08 16:55:34 +0000127 /// KnownFeasibleEdges - Entries in this set are edges which have already had
128 /// PHI nodes retriggered.
129 typedef std::pair<BasicBlock*,BasicBlock*> Edge;
130 std::set<Edge> KnownFeasibleEdges;
Chris Lattner138a1242001-06-27 23:38:11 +0000131public:
132
Chris Lattner82bec2c2004-11-15 04:44:20 +0000133 /// MarkBlockExecutable - This method can be used by clients to mark all of
134 /// the blocks that are known to be intrinsically live in the processed unit.
135 void MarkBlockExecutable(BasicBlock *BB) {
Bill Wendlingb7427032006-11-26 09:46:52 +0000136 DOUT << "Marking Block Executable: " << BB->getName() << "\n";
Chris Lattner82bec2c2004-11-15 04:44:20 +0000137 BBExecutable.insert(BB); // Basic block is executable!
138 BBWorkList.push_back(BB); // Add the block to the work list!
Chris Lattner0dbfc052002-04-29 21:26:08 +0000139 }
140
Chris Lattnerdd336d12004-12-11 05:15:59 +0000141 /// TrackValueOfGlobalVariable - Clients can use this method to
Chris Lattner59acc7d2004-12-10 08:02:06 +0000142 /// inform the SCCPSolver that it should track loads and stores to the
143 /// specified global variable if it can. This is only legal to call if
144 /// performing Interprocedural SCCP.
Chris Lattnerdd336d12004-12-11 05:15:59 +0000145 void TrackValueOfGlobalVariable(GlobalVariable *GV) {
146 const Type *ElTy = GV->getType()->getElementType();
147 if (ElTy->isFirstClassType()) {
148 LatticeVal &IV = TrackedGlobals[GV];
149 if (!isa<UndefValue>(GV->getInitializer()))
150 IV.markConstant(GV->getInitializer());
151 }
152 }
Chris Lattner59acc7d2004-12-10 08:02:06 +0000153
154 /// AddTrackedFunction - If the SCCP solver is supposed to track calls into
155 /// and out of the specified function (which cannot have its address taken),
156 /// this method must be called.
157 void AddTrackedFunction(Function *F) {
158 assert(F->hasInternalLinkage() && "Can only track internal functions!");
159 // Add an entry, F -> undef.
160 TrackedFunctionRetVals[F];
161 }
162
Chris Lattner82bec2c2004-11-15 04:44:20 +0000163 /// Solve - Solve for constants and executable blocks.
164 ///
165 void Solve();
Chris Lattner138a1242001-06-27 23:38:11 +0000166
Chris Lattnerfc6ac502004-12-10 20:41:50 +0000167 /// ResolveBranchesIn - While solving the dataflow for a function, we assume
168 /// that branches on undef values cannot reach any of their successors.
169 /// However, this is not a safe assumption. After we solve dataflow, this
170 /// method should be use to handle this. If this returns true, the solver
171 /// should be rerun.
172 bool ResolveBranchesIn(Function &F);
173
Chris Lattner82bec2c2004-11-15 04:44:20 +0000174 /// getExecutableBlocks - Once we have solved for constants, return the set of
175 /// blocks that is known to be executable.
176 std::set<BasicBlock*> &getExecutableBlocks() {
177 return BBExecutable;
178 }
179
180 /// getValueMapping - Once we have solved for constants, return the mapping of
Chris Lattneref36dfd2004-11-15 05:03:30 +0000181 /// LLVM values to LatticeVals.
182 hash_map<Value*, LatticeVal> &getValueMapping() {
Chris Lattner82bec2c2004-11-15 04:44:20 +0000183 return ValueState;
184 }
185
Chris Lattner0417feb2004-12-11 02:53:57 +0000186 /// getTrackedFunctionRetVals - Get the inferred return value map.
187 ///
188 const hash_map<Function*, LatticeVal> &getTrackedFunctionRetVals() {
189 return TrackedFunctionRetVals;
190 }
191
Chris Lattnerdd336d12004-12-11 05:15:59 +0000192 /// getTrackedGlobals - Get and return the set of inferred initializers for
193 /// global variables.
194 const hash_map<GlobalVariable*, LatticeVal> &getTrackedGlobals() {
195 return TrackedGlobals;
196 }
197
Chris Lattner0417feb2004-12-11 02:53:57 +0000198
Chris Lattner138a1242001-06-27 23:38:11 +0000199private:
Chris Lattner80b2d6c2004-07-15 23:36:43 +0000200 // markConstant - Make a value be marked as "constant". If the value
Misha Brukmanfd939082005-04-21 23:48:37 +0000201 // is not already a constant, add it to the instruction work list so that
Chris Lattner138a1242001-06-27 23:38:11 +0000202 // the users of the instruction are updated later.
203 //
Chris Lattner59acc7d2004-12-10 08:02:06 +0000204 inline void markConstant(LatticeVal &IV, Value *V, Constant *C) {
Chris Lattner3d405b02003-10-08 16:21:03 +0000205 if (IV.markConstant(C)) {
Bill Wendlingb7427032006-11-26 09:46:52 +0000206 DOUT << "markConstant: " << *C << ": " << *V;
Chris Lattner59acc7d2004-12-10 08:02:06 +0000207 InstWorkList.push_back(V);
Chris Lattner138a1242001-06-27 23:38:11 +0000208 }
Chris Lattner3d405b02003-10-08 16:21:03 +0000209 }
Chris Lattner59acc7d2004-12-10 08:02:06 +0000210 inline void markConstant(Value *V, Constant *C) {
211 markConstant(ValueState[V], V, C);
Chris Lattner138a1242001-06-27 23:38:11 +0000212 }
213
Chris Lattner80b2d6c2004-07-15 23:36:43 +0000214 // markOverdefined - Make a value be marked as "overdefined". If the
Misha Brukmanfd939082005-04-21 23:48:37 +0000215 // value is not already overdefined, add it to the overdefined instruction
Chris Lattner80b2d6c2004-07-15 23:36:43 +0000216 // work list so that the users of the instruction are updated later.
Misha Brukmanfd939082005-04-21 23:48:37 +0000217
Chris Lattner59acc7d2004-12-10 08:02:06 +0000218 inline void markOverdefined(LatticeVal &IV, Value *V) {
Chris Lattner3d405b02003-10-08 16:21:03 +0000219 if (IV.markOverdefined()) {
Bill Wendlingb7427032006-11-26 09:46:52 +0000220 DEBUG(DOUT << "markOverdefined: ";
Chris Lattnerdade2d22004-12-11 06:05:53 +0000221 if (Function *F = dyn_cast<Function>(V))
Bill Wendlingb7427032006-11-26 09:46:52 +0000222 DOUT << "Function '" << F->getName() << "'\n";
Chris Lattnerdade2d22004-12-11 06:05:53 +0000223 else
Bill Wendlingb7427032006-11-26 09:46:52 +0000224 DOUT << *V);
Chris Lattner82bec2c2004-11-15 04:44:20 +0000225 // Only instructions go on the work list
Chris Lattner59acc7d2004-12-10 08:02:06 +0000226 OverdefinedInstWorkList.push_back(V);
Chris Lattner138a1242001-06-27 23:38:11 +0000227 }
Chris Lattner3d405b02003-10-08 16:21:03 +0000228 }
Chris Lattner59acc7d2004-12-10 08:02:06 +0000229 inline void markOverdefined(Value *V) {
230 markOverdefined(ValueState[V], V);
231 }
232
233 inline void mergeInValue(LatticeVal &IV, Value *V, LatticeVal &MergeWithV) {
234 if (IV.isOverdefined() || MergeWithV.isUndefined())
235 return; // Noop.
236 if (MergeWithV.isOverdefined())
237 markOverdefined(IV, V);
238 else if (IV.isUndefined())
239 markConstant(IV, V, MergeWithV.getConstant());
240 else if (IV.getConstant() != MergeWithV.getConstant())
241 markOverdefined(IV, V);
Chris Lattner138a1242001-06-27 23:38:11 +0000242 }
Chris Lattnerfe243eb2006-02-08 02:38:11 +0000243
244 inline void mergeInValue(Value *V, LatticeVal &MergeWithV) {
245 return mergeInValue(ValueState[V], V, MergeWithV);
246 }
247
Chris Lattner138a1242001-06-27 23:38:11 +0000248
Chris Lattneref36dfd2004-11-15 05:03:30 +0000249 // getValueState - Return the LatticeVal object that corresponds to the value.
Misha Brukman5560c9d2003-08-18 14:43:39 +0000250 // This function is necessary because not all values should start out in the
Chris Lattner73e21422002-04-09 19:48:49 +0000251 // underdefined state... Argument's should be overdefined, and
Chris Lattner79df7c02002-03-26 18:01:55 +0000252 // constants should be marked as constants. If a value is not known to be an
Chris Lattner138a1242001-06-27 23:38:11 +0000253 // Instruction object, then use this accessor to get its value from the map.
254 //
Chris Lattneref36dfd2004-11-15 05:03:30 +0000255 inline LatticeVal &getValueState(Value *V) {
256 hash_map<Value*, LatticeVal>::iterator I = ValueState.find(V);
Chris Lattner138a1242001-06-27 23:38:11 +0000257 if (I != ValueState.end()) return I->second; // Common case, in the map
Chris Lattner5d356a72004-10-16 18:09:41 +0000258
Chris Lattner7e529e42004-11-15 05:45:33 +0000259 if (Constant *CPV = dyn_cast<Constant>(V)) {
260 if (isa<UndefValue>(V)) {
261 // Nothing to do, remain undefined.
262 } else {
263 ValueState[CPV].markConstant(CPV); // Constants are constant
264 }
Chris Lattner2a88bb72002-08-30 23:39:00 +0000265 }
Chris Lattner138a1242001-06-27 23:38:11 +0000266 // All others are underdefined by default...
267 return ValueState[V];
268 }
269
Misha Brukmanfd939082005-04-21 23:48:37 +0000270 // markEdgeExecutable - Mark a basic block as executable, adding it to the BB
Chris Lattner138a1242001-06-27 23:38:11 +0000271 // work list if it is not already executable...
Misha Brukmanfd939082005-04-21 23:48:37 +0000272 //
Chris Lattner16b18fd2003-10-08 16:55:34 +0000273 void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
274 if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
275 return; // This edge is already known to be executable!
276
277 if (BBExecutable.count(Dest)) {
Bill Wendlingb7427032006-11-26 09:46:52 +0000278 DOUT << "Marking Edge Executable: " << Source->getName()
279 << " -> " << Dest->getName() << "\n";
Chris Lattner16b18fd2003-10-08 16:55:34 +0000280
281 // The destination is already executable, but we just made an edge
Chris Lattner929c6fb2003-10-08 16:56:11 +0000282 // feasible that wasn't before. Revisit the PHI nodes in the block
283 // because they have potentially new operands.
Chris Lattner59acc7d2004-12-10 08:02:06 +0000284 for (BasicBlock::iterator I = Dest->begin(); isa<PHINode>(I); ++I)
285 visitPHINode(*cast<PHINode>(I));
Chris Lattner9de28282003-04-25 02:50:03 +0000286
287 } else {
Chris Lattner82bec2c2004-11-15 04:44:20 +0000288 MarkBlockExecutable(Dest);
Chris Lattner9de28282003-04-25 02:50:03 +0000289 }
Chris Lattner138a1242001-06-27 23:38:11 +0000290 }
291
Chris Lattner82bec2c2004-11-15 04:44:20 +0000292 // getFeasibleSuccessors - Return a vector of booleans to indicate which
293 // successors are reachable from a given terminator instruction.
294 //
295 void getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs);
296
297 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
298 // block to the 'To' basic block is currently feasible...
299 //
300 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To);
301
302 // OperandChangedState - This method is invoked on all of the users of an
303 // instruction that was just changed state somehow.... Based on this
304 // information, we need to update the specified user of this instruction.
305 //
306 void OperandChangedState(User *U) {
307 // Only instructions use other variable values!
308 Instruction &I = cast<Instruction>(*U);
309 if (BBExecutable.count(I.getParent())) // Inst is executable?
310 visit(I);
311 }
312
313private:
314 friend class InstVisitor<SCCPSolver>;
Chris Lattner138a1242001-06-27 23:38:11 +0000315
Misha Brukmanfd939082005-04-21 23:48:37 +0000316 // visit implementations - Something changed in this instruction... Either an
Chris Lattnercb056de2001-06-29 23:56:23 +0000317 // operand made a transition, or the instruction is newly executable. Change
318 // the value type of I to reflect these changes if appropriate.
319 //
Chris Lattner7e708292002-06-25 16:13:24 +0000320 void visitPHINode(PHINode &I);
Chris Lattner2a632552002-04-18 15:13:15 +0000321
322 // Terminators
Chris Lattner59acc7d2004-12-10 08:02:06 +0000323 void visitReturnInst(ReturnInst &I);
Chris Lattner7e708292002-06-25 16:13:24 +0000324 void visitTerminatorInst(TerminatorInst &TI);
Chris Lattner2a632552002-04-18 15:13:15 +0000325
Chris Lattnerb8047602002-08-14 17:53:45 +0000326 void visitCastInst(CastInst &I);
Chris Lattner6e323722004-03-12 05:52:44 +0000327 void visitSelectInst(SelectInst &I);
Chris Lattner7e708292002-06-25 16:13:24 +0000328 void visitBinaryOperator(Instruction &I);
329 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
Robert Bocchino56107e22006-01-10 19:05:05 +0000330 void visitExtractElementInst(ExtractElementInst &I);
Robert Bocchino8fcf01e2006-01-17 20:06:55 +0000331 void visitInsertElementInst(InsertElementInst &I);
Chris Lattner543abdf2006-04-08 01:19:12 +0000332 void visitShuffleVectorInst(ShuffleVectorInst &I);
Chris Lattner2a632552002-04-18 15:13:15 +0000333
334 // Instructions that cannot be folded away...
Chris Lattnerdd336d12004-12-11 05:15:59 +0000335 void visitStoreInst (Instruction &I);
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +0000336 void visitLoadInst (LoadInst &I);
Chris Lattner2a88bb72002-08-30 23:39:00 +0000337 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattner59acc7d2004-12-10 08:02:06 +0000338 void visitCallInst (CallInst &I) { visitCallSite(CallSite::get(&I)); }
339 void visitInvokeInst (InvokeInst &II) {
340 visitCallSite(CallSite::get(&II));
341 visitTerminatorInst(II);
Chris Lattner99b28e62003-08-27 01:08:35 +0000342 }
Chris Lattner59acc7d2004-12-10 08:02:06 +0000343 void visitCallSite (CallSite CS);
Chris Lattner36143fc2003-09-08 18:54:55 +0000344 void visitUnwindInst (TerminatorInst &I) { /*returns void*/ }
Chris Lattner5d356a72004-10-16 18:09:41 +0000345 void visitUnreachableInst(TerminatorInst &I) { /*returns void*/ }
Chris Lattner7e708292002-06-25 16:13:24 +0000346 void visitAllocationInst(Instruction &I) { markOverdefined(&I); }
Chris Lattnercda965e2003-10-18 05:56:52 +0000347 void visitVANextInst (Instruction &I) { markOverdefined(&I); }
348 void visitVAArgInst (Instruction &I) { markOverdefined(&I); }
Chris Lattner7e708292002-06-25 16:13:24 +0000349 void visitFreeInst (Instruction &I) { /*returns void*/ }
Chris Lattner2a632552002-04-18 15:13:15 +0000350
Chris Lattner7e708292002-06-25 16:13:24 +0000351 void visitInstruction(Instruction &I) {
Chris Lattner2a632552002-04-18 15:13:15 +0000352 // If a new instruction is added to LLVM that we don't handle...
Bill Wendlinge8156192006-12-07 01:30:32 +0000353 cerr << "SCCP: Don't know how to handle: " << I;
Chris Lattner7e708292002-06-25 16:13:24 +0000354 markOverdefined(&I); // Just in case
Chris Lattner2a632552002-04-18 15:13:15 +0000355 }
Chris Lattnercb056de2001-06-29 23:56:23 +0000356};
Chris Lattnerf6293092002-07-23 18:06:35 +0000357
Chris Lattnerb9a66342002-05-02 21:44:00 +0000358// getFeasibleSuccessors - Return a vector of booleans to indicate which
359// successors are reachable from a given terminator instruction.
360//
Chris Lattner82bec2c2004-11-15 04:44:20 +0000361void SCCPSolver::getFeasibleSuccessors(TerminatorInst &TI,
362 std::vector<bool> &Succs) {
Chris Lattner9de28282003-04-25 02:50:03 +0000363 Succs.resize(TI.getNumSuccessors());
Chris Lattner7e708292002-06-25 16:13:24 +0000364 if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
Chris Lattnerb9a66342002-05-02 21:44:00 +0000365 if (BI->isUnconditional()) {
366 Succs[0] = true;
367 } else {
Chris Lattneref36dfd2004-11-15 05:03:30 +0000368 LatticeVal &BCValue = getValueState(BI->getCondition());
Chris Lattner84831642004-01-12 17:40:36 +0000369 if (BCValue.isOverdefined() ||
370 (BCValue.isConstant() && !isa<ConstantBool>(BCValue.getConstant()))) {
371 // Overdefined condition variables, and branches on unfoldable constant
372 // conditions, mean the branch could go either way.
Chris Lattnerb9a66342002-05-02 21:44:00 +0000373 Succs[0] = Succs[1] = true;
374 } else if (BCValue.isConstant()) {
375 // Constant condition variables mean the branch can only go a single way
Chris Lattner47811b72006-09-28 23:35:22 +0000376 Succs[BCValue.getConstant() == ConstantBool::getFalse()] = true;
Chris Lattnerb9a66342002-05-02 21:44:00 +0000377 }
378 }
Reid Spencer3ed469c2006-11-02 20:25:50 +0000379 } else if (isa<InvokeInst>(&TI)) {
Chris Lattnerb9a66342002-05-02 21:44:00 +0000380 // Invoke instructions successors are always executable.
381 Succs[0] = Succs[1] = true;
Chris Lattner7e708292002-06-25 16:13:24 +0000382 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) {
Chris Lattneref36dfd2004-11-15 05:03:30 +0000383 LatticeVal &SCValue = getValueState(SI->getCondition());
Chris Lattner84831642004-01-12 17:40:36 +0000384 if (SCValue.isOverdefined() || // Overdefined condition?
385 (SCValue.isConstant() && !isa<ConstantInt>(SCValue.getConstant()))) {
Chris Lattnerb9a66342002-05-02 21:44:00 +0000386 // All destinations are executable!
Chris Lattner7e708292002-06-25 16:13:24 +0000387 Succs.assign(TI.getNumSuccessors(), true);
Chris Lattnerb9a66342002-05-02 21:44:00 +0000388 } else if (SCValue.isConstant()) {
389 Constant *CPV = SCValue.getConstant();
390 // Make sure to skip the "default value" which isn't a value
391 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) {
392 if (SI->getSuccessorValue(i) == CPV) {// Found the right branch...
393 Succs[i] = true;
394 return;
395 }
396 }
397
398 // Constant value not equal to any of the branches... must execute
399 // default branch then...
400 Succs[0] = true;
401 }
402 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +0000403 cerr << "SCCP: Don't know how to handle: " << TI;
Chris Lattner7e708292002-06-25 16:13:24 +0000404 Succs.assign(TI.getNumSuccessors(), true);
Chris Lattnerb9a66342002-05-02 21:44:00 +0000405 }
406}
407
408
Chris Lattner59f0ce22002-05-02 21:18:01 +0000409// isEdgeFeasible - Return true if the control flow edge from the 'From' basic
410// block to the 'To' basic block is currently feasible...
411//
Chris Lattner82bec2c2004-11-15 04:44:20 +0000412bool SCCPSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
Chris Lattner59f0ce22002-05-02 21:18:01 +0000413 assert(BBExecutable.count(To) && "Dest should always be alive!");
414
415 // Make sure the source basic block is executable!!
416 if (!BBExecutable.count(From)) return false;
Misha Brukmanfd939082005-04-21 23:48:37 +0000417
Chris Lattnerb9a66342002-05-02 21:44:00 +0000418 // Check to make sure this edge itself is actually feasible now...
Chris Lattner7d275f42003-10-08 15:47:41 +0000419 TerminatorInst *TI = From->getTerminator();
420 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
421 if (BI->isUnconditional())
Chris Lattnerb9a66342002-05-02 21:44:00 +0000422 return true;
Chris Lattner7d275f42003-10-08 15:47:41 +0000423 else {
Chris Lattneref36dfd2004-11-15 05:03:30 +0000424 LatticeVal &BCValue = getValueState(BI->getCondition());
Chris Lattner7d275f42003-10-08 15:47:41 +0000425 if (BCValue.isOverdefined()) {
426 // Overdefined condition variables mean the branch could go either way.
427 return true;
428 } else if (BCValue.isConstant()) {
Chris Lattner84831642004-01-12 17:40:36 +0000429 // Not branching on an evaluatable constant?
430 if (!isa<ConstantBool>(BCValue.getConstant())) return true;
431
Chris Lattner7d275f42003-10-08 15:47:41 +0000432 // Constant condition variables mean the branch can only go a single way
Misha Brukmanfd939082005-04-21 23:48:37 +0000433 return BI->getSuccessor(BCValue.getConstant() ==
Chris Lattner47811b72006-09-28 23:35:22 +0000434 ConstantBool::getFalse()) == To;
Chris Lattner7d275f42003-10-08 15:47:41 +0000435 }
436 return false;
437 }
Reid Spencer3ed469c2006-11-02 20:25:50 +0000438 } else if (isa<InvokeInst>(TI)) {
Chris Lattner7d275f42003-10-08 15:47:41 +0000439 // Invoke instructions successors are always executable.
440 return true;
441 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
Chris Lattneref36dfd2004-11-15 05:03:30 +0000442 LatticeVal &SCValue = getValueState(SI->getCondition());
Chris Lattner7d275f42003-10-08 15:47:41 +0000443 if (SCValue.isOverdefined()) { // Overdefined condition?
444 // All destinations are executable!
445 return true;
446 } else if (SCValue.isConstant()) {
447 Constant *CPV = SCValue.getConstant();
Chris Lattner84831642004-01-12 17:40:36 +0000448 if (!isa<ConstantInt>(CPV))
449 return true; // not a foldable constant?
450
Chris Lattner7d275f42003-10-08 15:47:41 +0000451 // Make sure to skip the "default value" which isn't a value
452 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i)
453 if (SI->getSuccessorValue(i) == CPV) // Found the taken branch...
454 return SI->getSuccessor(i) == To;
455
456 // Constant value not equal to any of the branches... must execute
457 // default branch then...
458 return SI->getDefaultDest() == To;
459 }
460 return false;
461 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +0000462 cerr << "Unknown terminator instruction: " << *TI;
Chris Lattner7d275f42003-10-08 15:47:41 +0000463 abort();
464 }
Chris Lattner59f0ce22002-05-02 21:18:01 +0000465}
Chris Lattner138a1242001-06-27 23:38:11 +0000466
Chris Lattner2a632552002-04-18 15:13:15 +0000467// visit Implementations - Something changed in this instruction... Either an
Chris Lattner138a1242001-06-27 23:38:11 +0000468// operand made a transition, or the instruction is newly executable. Change
469// the value type of I to reflect these changes if appropriate. This method
470// makes sure to do the following actions:
471//
472// 1. If a phi node merges two constants in, and has conflicting value coming
473// from different branches, or if the PHI node merges in an overdefined
474// value, then the PHI node becomes overdefined.
475// 2. If a phi node merges only constants in, and they all agree on value, the
476// PHI node becomes a constant value equal to that.
477// 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
478// 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
479// 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
480// 6. If a conditional branch has a value that is constant, make the selected
481// destination executable
482// 7. If a conditional branch has a value that is overdefined, make all
483// successors executable.
484//
Chris Lattner82bec2c2004-11-15 04:44:20 +0000485void SCCPSolver::visitPHINode(PHINode &PN) {
Chris Lattneref36dfd2004-11-15 05:03:30 +0000486 LatticeVal &PNIV = getValueState(&PN);
Chris Lattner1daee8b2004-01-12 03:57:30 +0000487 if (PNIV.isOverdefined()) {
488 // There may be instructions using this PHI node that are not overdefined
489 // themselves. If so, make sure that they know that the PHI node operand
490 // changed.
491 std::multimap<PHINode*, Instruction*>::iterator I, E;
492 tie(I, E) = UsersOfOverdefinedPHIs.equal_range(&PN);
493 if (I != E) {
494 std::vector<Instruction*> Users;
495 Users.reserve(std::distance(I, E));
496 for (; I != E; ++I) Users.push_back(I->second);
497 while (!Users.empty()) {
498 visit(Users.back());
499 Users.pop_back();
500 }
501 }
502 return; // Quick exit
503 }
Chris Lattner138a1242001-06-27 23:38:11 +0000504
Chris Lattnera2f652d2004-03-16 19:49:59 +0000505 // Super-extra-high-degree PHI nodes are unlikely to ever be marked constant,
506 // and slow us down a lot. Just mark them overdefined.
507 if (PN.getNumIncomingValues() > 64) {
508 markOverdefined(PNIV, &PN);
509 return;
510 }
511
Chris Lattner2a632552002-04-18 15:13:15 +0000512 // Look at all of the executable operands of the PHI node. If any of them
513 // are overdefined, the PHI becomes overdefined as well. If they are all
514 // constant, and they agree with each other, the PHI becomes the identical
515 // constant. If they are constant and don't agree, the PHI is overdefined.
516 // If there are no executable operands, the PHI remains undefined.
517 //
Chris Lattner9de28282003-04-25 02:50:03 +0000518 Constant *OperandVal = 0;
519 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
Chris Lattneref36dfd2004-11-15 05:03:30 +0000520 LatticeVal &IV = getValueState(PN.getIncomingValue(i));
Chris Lattner9de28282003-04-25 02:50:03 +0000521 if (IV.isUndefined()) continue; // Doesn't influence PHI node.
Misha Brukmanfd939082005-04-21 23:48:37 +0000522
Chris Lattner7e708292002-06-25 16:13:24 +0000523 if (isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent())) {
Chris Lattner38b5ae42003-06-24 20:29:52 +0000524 if (IV.isOverdefined()) { // PHI node becomes overdefined!
Chris Lattner3d405b02003-10-08 16:21:03 +0000525 markOverdefined(PNIV, &PN);
Chris Lattner38b5ae42003-06-24 20:29:52 +0000526 return;
527 }
528
Chris Lattner9de28282003-04-25 02:50:03 +0000529 if (OperandVal == 0) { // Grab the first value...
530 OperandVal = IV.getConstant();
Chris Lattner2a632552002-04-18 15:13:15 +0000531 } else { // Another value is being merged in!
532 // There is already a reachable operand. If we conflict with it,
533 // then the PHI node becomes overdefined. If we agree with it, we
534 // can continue on.
Misha Brukmanfd939082005-04-21 23:48:37 +0000535
Chris Lattner2a632552002-04-18 15:13:15 +0000536 // Check to see if there are two different constants merging...
Chris Lattner9de28282003-04-25 02:50:03 +0000537 if (IV.getConstant() != OperandVal) {
Chris Lattner2a632552002-04-18 15:13:15 +0000538 // Yes there is. This means the PHI node is not constant.
539 // You must be overdefined poor PHI.
540 //
Chris Lattner3d405b02003-10-08 16:21:03 +0000541 markOverdefined(PNIV, &PN); // The PHI node now becomes overdefined
Chris Lattner2a632552002-04-18 15:13:15 +0000542 return; // I'm done analyzing you
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000543 }
Chris Lattner138a1242001-06-27 23:38:11 +0000544 }
545 }
Chris Lattner138a1242001-06-27 23:38:11 +0000546 }
547
Chris Lattner2a632552002-04-18 15:13:15 +0000548 // If we exited the loop, this means that the PHI node only has constant
Chris Lattner9de28282003-04-25 02:50:03 +0000549 // arguments that agree with each other(and OperandVal is the constant) or
550 // OperandVal is null because there are no defined incoming arguments. If
551 // this is the case, the PHI remains undefined.
Chris Lattner138a1242001-06-27 23:38:11 +0000552 //
Chris Lattner9de28282003-04-25 02:50:03 +0000553 if (OperandVal)
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000554 markConstant(PNIV, &PN, OperandVal); // Acquire operand value
Chris Lattner138a1242001-06-27 23:38:11 +0000555}
556
Chris Lattner59acc7d2004-12-10 08:02:06 +0000557void SCCPSolver::visitReturnInst(ReturnInst &I) {
558 if (I.getNumOperands() == 0) return; // Ret void
559
560 // If we are tracking the return value of this function, merge it in.
561 Function *F = I.getParent()->getParent();
562 if (F->hasInternalLinkage() && !TrackedFunctionRetVals.empty()) {
563 hash_map<Function*, LatticeVal>::iterator TFRVI =
564 TrackedFunctionRetVals.find(F);
565 if (TFRVI != TrackedFunctionRetVals.end() &&
566 !TFRVI->second.isOverdefined()) {
567 LatticeVal &IV = getValueState(I.getOperand(0));
568 mergeInValue(TFRVI->second, F, IV);
569 }
570 }
571}
572
573
Chris Lattner82bec2c2004-11-15 04:44:20 +0000574void SCCPSolver::visitTerminatorInst(TerminatorInst &TI) {
Chris Lattner9de28282003-04-25 02:50:03 +0000575 std::vector<bool> SuccFeasible;
Chris Lattnerb9a66342002-05-02 21:44:00 +0000576 getFeasibleSuccessors(TI, SuccFeasible);
Chris Lattner138a1242001-06-27 23:38:11 +0000577
Chris Lattner16b18fd2003-10-08 16:55:34 +0000578 BasicBlock *BB = TI.getParent();
579
Chris Lattnerb9a66342002-05-02 21:44:00 +0000580 // Mark all feasible successors executable...
581 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
Chris Lattner16b18fd2003-10-08 16:55:34 +0000582 if (SuccFeasible[i])
583 markEdgeExecutable(BB, TI.getSuccessor(i));
Chris Lattner2a632552002-04-18 15:13:15 +0000584}
585
Chris Lattner82bec2c2004-11-15 04:44:20 +0000586void SCCPSolver::visitCastInst(CastInst &I) {
Chris Lattner7e708292002-06-25 16:13:24 +0000587 Value *V = I.getOperand(0);
Chris Lattneref36dfd2004-11-15 05:03:30 +0000588 LatticeVal &VState = getValueState(V);
Chris Lattnerb7a5d3e2004-01-12 17:43:40 +0000589 if (VState.isOverdefined()) // Inherit overdefinedness of operand
Chris Lattner7e708292002-06-25 16:13:24 +0000590 markOverdefined(&I);
Chris Lattnerb7a5d3e2004-01-12 17:43:40 +0000591 else if (VState.isConstant()) // Propagate constant value
592 markConstant(&I, ConstantExpr::getCast(VState.getConstant(), I.getType()));
Chris Lattner2a632552002-04-18 15:13:15 +0000593}
594
Chris Lattner82bec2c2004-11-15 04:44:20 +0000595void SCCPSolver::visitSelectInst(SelectInst &I) {
Chris Lattneref36dfd2004-11-15 05:03:30 +0000596 LatticeVal &CondValue = getValueState(I.getCondition());
Chris Lattnerfe243eb2006-02-08 02:38:11 +0000597 if (CondValue.isUndefined())
598 return;
599 if (CondValue.isConstant()) {
Chris Lattner47811b72006-09-28 23:35:22 +0000600 if (ConstantBool *CondCB = dyn_cast<ConstantBool>(CondValue.getConstant())){
601 mergeInValue(&I, getValueState(CondCB->getValue() ? I.getTrueValue()
602 : I.getFalseValue()));
Chris Lattnerfe243eb2006-02-08 02:38:11 +0000603 return;
604 }
605 }
606
607 // Otherwise, the condition is overdefined or a constant we can't evaluate.
608 // See if we can produce something better than overdefined based on the T/F
609 // value.
610 LatticeVal &TVal = getValueState(I.getTrueValue());
611 LatticeVal &FVal = getValueState(I.getFalseValue());
612
613 // select ?, C, C -> C.
614 if (TVal.isConstant() && FVal.isConstant() &&
615 TVal.getConstant() == FVal.getConstant()) {
616 markConstant(&I, FVal.getConstant());
617 return;
618 }
619
620 if (TVal.isUndefined()) { // select ?, undef, X -> X.
621 mergeInValue(&I, FVal);
622 } else if (FVal.isUndefined()) { // select ?, X, undef -> X.
623 mergeInValue(&I, TVal);
624 } else {
625 markOverdefined(&I);
Chris Lattner6e323722004-03-12 05:52:44 +0000626 }
627}
628
Chris Lattner2a632552002-04-18 15:13:15 +0000629// Handle BinaryOperators and Shift Instructions...
Chris Lattner82bec2c2004-11-15 04:44:20 +0000630void SCCPSolver::visitBinaryOperator(Instruction &I) {
Chris Lattneref36dfd2004-11-15 05:03:30 +0000631 LatticeVal &IV = ValueState[&I];
Chris Lattner1daee8b2004-01-12 03:57:30 +0000632 if (IV.isOverdefined()) return;
633
Chris Lattneref36dfd2004-11-15 05:03:30 +0000634 LatticeVal &V1State = getValueState(I.getOperand(0));
635 LatticeVal &V2State = getValueState(I.getOperand(1));
Chris Lattner1daee8b2004-01-12 03:57:30 +0000636
Chris Lattner2a632552002-04-18 15:13:15 +0000637 if (V1State.isOverdefined() || V2State.isOverdefined()) {
Chris Lattnera177c672004-12-11 23:15:19 +0000638 // If this is an AND or OR with 0 or -1, it doesn't matter that the other
639 // operand is overdefined.
640 if (I.getOpcode() == Instruction::And || I.getOpcode() == Instruction::Or) {
641 LatticeVal *NonOverdefVal = 0;
642 if (!V1State.isOverdefined()) {
643 NonOverdefVal = &V1State;
644 } else if (!V2State.isOverdefined()) {
645 NonOverdefVal = &V2State;
646 }
647
648 if (NonOverdefVal) {
649 if (NonOverdefVal->isUndefined()) {
650 // Could annihilate value.
651 if (I.getOpcode() == Instruction::And)
652 markConstant(IV, &I, Constant::getNullValue(I.getType()));
653 else
654 markConstant(IV, &I, ConstantInt::getAllOnesValue(I.getType()));
655 return;
656 } else {
657 if (I.getOpcode() == Instruction::And) {
658 if (NonOverdefVal->getConstant()->isNullValue()) {
659 markConstant(IV, &I, NonOverdefVal->getConstant());
660 return; // X or 0 = -1
661 }
662 } else {
663 if (ConstantIntegral *CI =
664 dyn_cast<ConstantIntegral>(NonOverdefVal->getConstant()))
665 if (CI->isAllOnesValue()) {
666 markConstant(IV, &I, NonOverdefVal->getConstant());
667 return; // X or -1 = -1
668 }
669 }
670 }
671 }
672 }
673
674
Chris Lattner1daee8b2004-01-12 03:57:30 +0000675 // If both operands are PHI nodes, it is possible that this instruction has
676 // a constant value, despite the fact that the PHI node doesn't. Check for
677 // this condition now.
678 if (PHINode *PN1 = dyn_cast<PHINode>(I.getOperand(0)))
679 if (PHINode *PN2 = dyn_cast<PHINode>(I.getOperand(1)))
680 if (PN1->getParent() == PN2->getParent()) {
681 // Since the two PHI nodes are in the same basic block, they must have
682 // entries for the same predecessors. Walk the predecessor list, and
683 // if all of the incoming values are constants, and the result of
684 // evaluating this expression with all incoming value pairs is the
685 // same, then this expression is a constant even though the PHI node
686 // is not a constant!
Chris Lattneref36dfd2004-11-15 05:03:30 +0000687 LatticeVal Result;
Chris Lattner1daee8b2004-01-12 03:57:30 +0000688 for (unsigned i = 0, e = PN1->getNumIncomingValues(); i != e; ++i) {
Chris Lattneref36dfd2004-11-15 05:03:30 +0000689 LatticeVal &In1 = getValueState(PN1->getIncomingValue(i));
Chris Lattner1daee8b2004-01-12 03:57:30 +0000690 BasicBlock *InBlock = PN1->getIncomingBlock(i);
Chris Lattneref36dfd2004-11-15 05:03:30 +0000691 LatticeVal &In2 =
692 getValueState(PN2->getIncomingValueForBlock(InBlock));
Chris Lattner1daee8b2004-01-12 03:57:30 +0000693
694 if (In1.isOverdefined() || In2.isOverdefined()) {
695 Result.markOverdefined();
696 break; // Cannot fold this operation over the PHI nodes!
697 } else if (In1.isConstant() && In2.isConstant()) {
Chris Lattnerb16689b2004-01-12 19:08:43 +0000698 Constant *V = ConstantExpr::get(I.getOpcode(), In1.getConstant(),
699 In2.getConstant());
Chris Lattner1daee8b2004-01-12 03:57:30 +0000700 if (Result.isUndefined())
Chris Lattnerb16689b2004-01-12 19:08:43 +0000701 Result.markConstant(V);
702 else if (Result.isConstant() && Result.getConstant() != V) {
Chris Lattner1daee8b2004-01-12 03:57:30 +0000703 Result.markOverdefined();
704 break;
705 }
706 }
707 }
708
709 // If we found a constant value here, then we know the instruction is
710 // constant despite the fact that the PHI nodes are overdefined.
711 if (Result.isConstant()) {
712 markConstant(IV, &I, Result.getConstant());
713 // Remember that this instruction is virtually using the PHI node
714 // operands.
715 UsersOfOverdefinedPHIs.insert(std::make_pair(PN1, &I));
716 UsersOfOverdefinedPHIs.insert(std::make_pair(PN2, &I));
717 return;
718 } else if (Result.isUndefined()) {
719 return;
720 }
721
722 // Okay, this really is overdefined now. Since we might have
723 // speculatively thought that this was not overdefined before, and
724 // added ourselves to the UsersOfOverdefinedPHIs list for the PHIs,
725 // make sure to clean out any entries that we put there, for
726 // efficiency.
727 std::multimap<PHINode*, Instruction*>::iterator It, E;
728 tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN1);
729 while (It != E) {
730 if (It->second == &I) {
731 UsersOfOverdefinedPHIs.erase(It++);
732 } else
733 ++It;
734 }
735 tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN2);
736 while (It != E) {
737 if (It->second == &I) {
738 UsersOfOverdefinedPHIs.erase(It++);
739 } else
740 ++It;
741 }
742 }
743
744 markOverdefined(IV, &I);
Chris Lattner2a632552002-04-18 15:13:15 +0000745 } else if (V1State.isConstant() && V2State.isConstant()) {
Chris Lattnerb16689b2004-01-12 19:08:43 +0000746 markConstant(IV, &I, ConstantExpr::get(I.getOpcode(), V1State.getConstant(),
747 V2State.getConstant()));
Chris Lattner2a632552002-04-18 15:13:15 +0000748 }
749}
Chris Lattner2a88bb72002-08-30 23:39:00 +0000750
Robert Bocchino56107e22006-01-10 19:05:05 +0000751void SCCPSolver::visitExtractElementInst(ExtractElementInst &I) {
Devang Patel67a821d2006-12-04 23:54:59 +0000752 // FIXME : SCCP does not handle vectors properly.
753 markOverdefined(&I);
754 return;
755
756#if 0
Robert Bocchino56107e22006-01-10 19:05:05 +0000757 LatticeVal &ValState = getValueState(I.getOperand(0));
758 LatticeVal &IdxState = getValueState(I.getOperand(1));
759
760 if (ValState.isOverdefined() || IdxState.isOverdefined())
761 markOverdefined(&I);
762 else if(ValState.isConstant() && IdxState.isConstant())
763 markConstant(&I, ConstantExpr::getExtractElement(ValState.getConstant(),
764 IdxState.getConstant()));
Devang Patel67a821d2006-12-04 23:54:59 +0000765#endif
Robert Bocchino56107e22006-01-10 19:05:05 +0000766}
767
Robert Bocchino8fcf01e2006-01-17 20:06:55 +0000768void SCCPSolver::visitInsertElementInst(InsertElementInst &I) {
Devang Patel67a821d2006-12-04 23:54:59 +0000769 // FIXME : SCCP does not handle vectors properly.
770 markOverdefined(&I);
771 return;
772#if 0
Robert Bocchino8fcf01e2006-01-17 20:06:55 +0000773 LatticeVal &ValState = getValueState(I.getOperand(0));
774 LatticeVal &EltState = getValueState(I.getOperand(1));
775 LatticeVal &IdxState = getValueState(I.getOperand(2));
776
777 if (ValState.isOverdefined() || EltState.isOverdefined() ||
778 IdxState.isOverdefined())
779 markOverdefined(&I);
780 else if(ValState.isConstant() && EltState.isConstant() &&
781 IdxState.isConstant())
782 markConstant(&I, ConstantExpr::getInsertElement(ValState.getConstant(),
783 EltState.getConstant(),
784 IdxState.getConstant()));
785 else if (ValState.isUndefined() && EltState.isConstant() &&
Devang Patel67a821d2006-12-04 23:54:59 +0000786 IdxState.isConstant())
Robert Bocchino8fcf01e2006-01-17 20:06:55 +0000787 markConstant(&I, ConstantExpr::getInsertElement(UndefValue::get(I.getType()),
788 EltState.getConstant(),
789 IdxState.getConstant()));
Devang Patel67a821d2006-12-04 23:54:59 +0000790#endif
Robert Bocchino8fcf01e2006-01-17 20:06:55 +0000791}
792
Chris Lattner543abdf2006-04-08 01:19:12 +0000793void SCCPSolver::visitShuffleVectorInst(ShuffleVectorInst &I) {
Devang Patel67a821d2006-12-04 23:54:59 +0000794 // FIXME : SCCP does not handle vectors properly.
795 markOverdefined(&I);
796 return;
797#if 0
Chris Lattner543abdf2006-04-08 01:19:12 +0000798 LatticeVal &V1State = getValueState(I.getOperand(0));
799 LatticeVal &V2State = getValueState(I.getOperand(1));
800 LatticeVal &MaskState = getValueState(I.getOperand(2));
801
802 if (MaskState.isUndefined() ||
803 (V1State.isUndefined() && V2State.isUndefined()))
804 return; // Undefined output if mask or both inputs undefined.
805
806 if (V1State.isOverdefined() || V2State.isOverdefined() ||
807 MaskState.isOverdefined()) {
808 markOverdefined(&I);
809 } else {
810 // A mix of constant/undef inputs.
811 Constant *V1 = V1State.isConstant() ?
812 V1State.getConstant() : UndefValue::get(I.getType());
813 Constant *V2 = V2State.isConstant() ?
814 V2State.getConstant() : UndefValue::get(I.getType());
815 Constant *Mask = MaskState.isConstant() ?
816 MaskState.getConstant() : UndefValue::get(I.getOperand(2)->getType());
817 markConstant(&I, ConstantExpr::getShuffleVector(V1, V2, Mask));
818 }
Devang Patel67a821d2006-12-04 23:54:59 +0000819#endif
Chris Lattner543abdf2006-04-08 01:19:12 +0000820}
821
Chris Lattner2a88bb72002-08-30 23:39:00 +0000822// Handle getelementptr instructions... if all operands are constants then we
823// can turn this into a getelementptr ConstantExpr.
824//
Chris Lattner82bec2c2004-11-15 04:44:20 +0000825void SCCPSolver::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattneref36dfd2004-11-15 05:03:30 +0000826 LatticeVal &IV = ValueState[&I];
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +0000827 if (IV.isOverdefined()) return;
828
Chris Lattner2a88bb72002-08-30 23:39:00 +0000829 std::vector<Constant*> Operands;
830 Operands.reserve(I.getNumOperands());
831
832 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
Chris Lattneref36dfd2004-11-15 05:03:30 +0000833 LatticeVal &State = getValueState(I.getOperand(i));
Chris Lattner2a88bb72002-08-30 23:39:00 +0000834 if (State.isUndefined())
835 return; // Operands are not resolved yet...
836 else if (State.isOverdefined()) {
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +0000837 markOverdefined(IV, &I);
Chris Lattner2a88bb72002-08-30 23:39:00 +0000838 return;
839 }
840 assert(State.isConstant() && "Unknown state!");
841 Operands.push_back(State.getConstant());
842 }
843
844 Constant *Ptr = Operands[0];
845 Operands.erase(Operands.begin()); // Erase the pointer from idx list...
846
Misha Brukmanfd939082005-04-21 23:48:37 +0000847 markConstant(IV, &I, ConstantExpr::getGetElementPtr(Ptr, Operands));
Chris Lattner2a88bb72002-08-30 23:39:00 +0000848}
Brian Gaeked0fde302003-11-11 22:41:34 +0000849
Chris Lattnerdd336d12004-12-11 05:15:59 +0000850void SCCPSolver::visitStoreInst(Instruction &SI) {
851 if (TrackedGlobals.empty() || !isa<GlobalVariable>(SI.getOperand(1)))
852 return;
853 GlobalVariable *GV = cast<GlobalVariable>(SI.getOperand(1));
854 hash_map<GlobalVariable*, LatticeVal>::iterator I = TrackedGlobals.find(GV);
855 if (I == TrackedGlobals.end() || I->second.isOverdefined()) return;
856
857 // Get the value we are storing into the global.
858 LatticeVal &PtrVal = getValueState(SI.getOperand(0));
859
860 mergeInValue(I->second, GV, PtrVal);
861 if (I->second.isOverdefined())
862 TrackedGlobals.erase(I); // No need to keep tracking this!
863}
864
865
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +0000866// Handle load instructions. If the operand is a constant pointer to a constant
867// global, we can replace the load with the loaded constant value!
Chris Lattner82bec2c2004-11-15 04:44:20 +0000868void SCCPSolver::visitLoadInst(LoadInst &I) {
Chris Lattneref36dfd2004-11-15 05:03:30 +0000869 LatticeVal &IV = ValueState[&I];
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +0000870 if (IV.isOverdefined()) return;
871
Chris Lattneref36dfd2004-11-15 05:03:30 +0000872 LatticeVal &PtrVal = getValueState(I.getOperand(0));
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +0000873 if (PtrVal.isUndefined()) return; // The pointer is not resolved yet!
874 if (PtrVal.isConstant() && !I.isVolatile()) {
875 Value *Ptr = PtrVal.getConstant();
Chris Lattnerc76d8032004-03-07 22:16:24 +0000876 if (isa<ConstantPointerNull>(Ptr)) {
877 // load null -> null
878 markConstant(IV, &I, Constant::getNullValue(I.getType()));
879 return;
880 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000881
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +0000882 // Transform load (constant global) into the value loaded.
Chris Lattnerdd336d12004-12-11 05:15:59 +0000883 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) {
884 if (GV->isConstant()) {
885 if (!GV->isExternal()) {
886 markConstant(IV, &I, GV->getInitializer());
887 return;
888 }
889 } else if (!TrackedGlobals.empty()) {
890 // If we are tracking this global, merge in the known value for it.
891 hash_map<GlobalVariable*, LatticeVal>::iterator It =
892 TrackedGlobals.find(GV);
893 if (It != TrackedGlobals.end()) {
894 mergeInValue(IV, &I, It->second);
895 return;
896 }
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +0000897 }
Chris Lattnerdd336d12004-12-11 05:15:59 +0000898 }
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +0000899
900 // Transform load (constantexpr_GEP global, 0, ...) into the value loaded.
901 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
902 if (CE->getOpcode() == Instruction::GetElementPtr)
Jeff Cohen9d809302005-04-23 21:38:35 +0000903 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0)))
904 if (GV->isConstant() && !GV->isExternal())
905 if (Constant *V =
Chris Lattnerebe61202005-09-26 05:28:52 +0000906 ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) {
Jeff Cohen9d809302005-04-23 21:38:35 +0000907 markConstant(IV, &I, V);
908 return;
909 }
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +0000910 }
911
912 // Otherwise we cannot say for certain what value this load will produce.
913 // Bail out.
914 markOverdefined(IV, &I);
915}
Chris Lattner58b7b082004-04-13 19:43:54 +0000916
Chris Lattner59acc7d2004-12-10 08:02:06 +0000917void SCCPSolver::visitCallSite(CallSite CS) {
918 Function *F = CS.getCalledFunction();
919
920 // If we are tracking this function, we must make sure to bind arguments as
921 // appropriate.
922 hash_map<Function*, LatticeVal>::iterator TFRVI =TrackedFunctionRetVals.end();
923 if (F && F->hasInternalLinkage())
924 TFRVI = TrackedFunctionRetVals.find(F);
Misha Brukmanfd939082005-04-21 23:48:37 +0000925
Chris Lattner59acc7d2004-12-10 08:02:06 +0000926 if (TFRVI != TrackedFunctionRetVals.end()) {
927 // If this is the first call to the function hit, mark its entry block
928 // executable.
929 if (!BBExecutable.count(F->begin()))
930 MarkBlockExecutable(F->begin());
931
932 CallSite::arg_iterator CAI = CS.arg_begin();
Chris Lattnere4d5c442005-03-15 04:54:21 +0000933 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
Chris Lattner59acc7d2004-12-10 08:02:06 +0000934 AI != E; ++AI, ++CAI) {
935 LatticeVal &IV = ValueState[AI];
936 if (!IV.isOverdefined())
937 mergeInValue(IV, AI, getValueState(*CAI));
938 }
939 }
940 Instruction *I = CS.getInstruction();
941 if (I->getType() == Type::VoidTy) return;
942
943 LatticeVal &IV = ValueState[I];
Chris Lattner58b7b082004-04-13 19:43:54 +0000944 if (IV.isOverdefined()) return;
945
Chris Lattner59acc7d2004-12-10 08:02:06 +0000946 // Propagate the return value of the function to the value of the instruction.
947 if (TFRVI != TrackedFunctionRetVals.end()) {
948 mergeInValue(IV, I, TFRVI->second);
949 return;
950 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000951
Chris Lattner59acc7d2004-12-10 08:02:06 +0000952 if (F == 0 || !F->isExternal() || !canConstantFoldCallTo(F)) {
953 markOverdefined(IV, I);
Chris Lattner58b7b082004-04-13 19:43:54 +0000954 return;
955 }
956
957 std::vector<Constant*> Operands;
Chris Lattner59acc7d2004-12-10 08:02:06 +0000958 Operands.reserve(I->getNumOperands()-1);
Chris Lattner58b7b082004-04-13 19:43:54 +0000959
Chris Lattner59acc7d2004-12-10 08:02:06 +0000960 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
961 AI != E; ++AI) {
962 LatticeVal &State = getValueState(*AI);
Chris Lattner58b7b082004-04-13 19:43:54 +0000963 if (State.isUndefined())
964 return; // Operands are not resolved yet...
965 else if (State.isOverdefined()) {
Chris Lattner59acc7d2004-12-10 08:02:06 +0000966 markOverdefined(IV, I);
Chris Lattner58b7b082004-04-13 19:43:54 +0000967 return;
968 }
969 assert(State.isConstant() && "Unknown state!");
970 Operands.push_back(State.getConstant());
971 }
972
973 if (Constant *C = ConstantFoldCall(F, Operands))
Chris Lattner59acc7d2004-12-10 08:02:06 +0000974 markConstant(IV, I, C);
Chris Lattner58b7b082004-04-13 19:43:54 +0000975 else
Chris Lattner59acc7d2004-12-10 08:02:06 +0000976 markOverdefined(IV, I);
Chris Lattner58b7b082004-04-13 19:43:54 +0000977}
Chris Lattner82bec2c2004-11-15 04:44:20 +0000978
979
980void SCCPSolver::Solve() {
981 // Process the work lists until they are empty!
Misha Brukmanfd939082005-04-21 23:48:37 +0000982 while (!BBWorkList.empty() || !InstWorkList.empty() ||
Jeff Cohen9d809302005-04-23 21:38:35 +0000983 !OverdefinedInstWorkList.empty()) {
Chris Lattner82bec2c2004-11-15 04:44:20 +0000984 // Process the instruction work list...
985 while (!OverdefinedInstWorkList.empty()) {
Chris Lattner59acc7d2004-12-10 08:02:06 +0000986 Value *I = OverdefinedInstWorkList.back();
Chris Lattner82bec2c2004-11-15 04:44:20 +0000987 OverdefinedInstWorkList.pop_back();
988
Bill Wendlingb7427032006-11-26 09:46:52 +0000989 DOUT << "\nPopped off OI-WL: " << *I;
Misha Brukmanfd939082005-04-21 23:48:37 +0000990
Chris Lattner82bec2c2004-11-15 04:44:20 +0000991 // "I" got into the work list because it either made the transition from
992 // bottom to constant
993 //
994 // Anything on this worklist that is overdefined need not be visited
995 // since all of its users will have already been marked as overdefined
996 // Update all of the users of this instruction's value...
997 //
998 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
999 UI != E; ++UI)
1000 OperandChangedState(*UI);
1001 }
1002 // Process the instruction work list...
1003 while (!InstWorkList.empty()) {
Chris Lattner59acc7d2004-12-10 08:02:06 +00001004 Value *I = InstWorkList.back();
Chris Lattner82bec2c2004-11-15 04:44:20 +00001005 InstWorkList.pop_back();
1006
Bill Wendlingb7427032006-11-26 09:46:52 +00001007 DOUT << "\nPopped off I-WL: " << *I;
Misha Brukmanfd939082005-04-21 23:48:37 +00001008
Chris Lattner82bec2c2004-11-15 04:44:20 +00001009 // "I" got into the work list because it either made the transition from
1010 // bottom to constant
1011 //
1012 // Anything on this worklist that is overdefined need not be visited
1013 // since all of its users will have already been marked as overdefined.
1014 // Update all of the users of this instruction's value...
1015 //
1016 if (!getValueState(I).isOverdefined())
1017 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
1018 UI != E; ++UI)
1019 OperandChangedState(*UI);
1020 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001021
Chris Lattner82bec2c2004-11-15 04:44:20 +00001022 // Process the basic block work list...
1023 while (!BBWorkList.empty()) {
1024 BasicBlock *BB = BBWorkList.back();
1025 BBWorkList.pop_back();
Misha Brukmanfd939082005-04-21 23:48:37 +00001026
Bill Wendlingb7427032006-11-26 09:46:52 +00001027 DOUT << "\nPopped off BBWL: " << *BB;
Misha Brukmanfd939082005-04-21 23:48:37 +00001028
Chris Lattner82bec2c2004-11-15 04:44:20 +00001029 // Notify all instructions in this basic block that they are newly
1030 // executable.
1031 visit(BB);
1032 }
1033 }
1034}
1035
Chris Lattnerfc6ac502004-12-10 20:41:50 +00001036/// ResolveBranchesIn - While solving the dataflow for a function, we assume
1037/// that branches on undef values cannot reach any of their successors.
1038/// However, this is not a safe assumption. After we solve dataflow, this
1039/// method should be use to handle this. If this returns true, the solver
1040/// should be rerun.
Chris Lattnerd2d86702006-10-22 05:59:17 +00001041///
1042/// This method handles this by finding an unresolved branch and marking it one
1043/// of the edges from the block as being feasible, even though the condition
1044/// doesn't say it would otherwise be. This allows SCCP to find the rest of the
1045/// CFG and only slightly pessimizes the analysis results (by marking one,
1046/// potentially unfeasible, edge feasible). This cannot usefully modify the
1047/// constraints on the condition of the branch, as that would impact other users
1048/// of the value.
Chris Lattnerfc6ac502004-12-10 20:41:50 +00001049bool SCCPSolver::ResolveBranchesIn(Function &F) {
Chris Lattnerd2d86702006-10-22 05:59:17 +00001050 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1051 if (!BBExecutable.count(BB))
1052 continue;
1053
1054 TerminatorInst *TI = BB->getTerminator();
1055 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
1056 if (!BI->isConditional()) continue;
1057 if (!getValueState(BI->getCondition()).isUndefined())
1058 continue;
1059 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
1060 if (!getValueState(SI->getCondition()).isUndefined())
1061 continue;
1062 } else {
1063 continue;
Chris Lattnerfc6ac502004-12-10 20:41:50 +00001064 }
Chris Lattnerd2d86702006-10-22 05:59:17 +00001065
1066 // If the edge to the first successor isn't thought to be feasible yet, mark
1067 // it so now.
1068 if (KnownFeasibleEdges.count(Edge(BB, TI->getSuccessor(0))))
1069 continue;
1070
1071 // Otherwise, it isn't already thought to be feasible. Mark it as such now
1072 // and return. This will make other blocks reachable, which will allow new
1073 // values to be discovered and existing ones to be moved in the lattice.
1074 markEdgeExecutable(BB, TI->getSuccessor(0));
1075 return true;
1076 }
Chris Lattnerdade2d22004-12-11 06:05:53 +00001077
Chris Lattnerd2d86702006-10-22 05:59:17 +00001078 return false;
Chris Lattnerfc6ac502004-12-10 20:41:50 +00001079}
1080
Chris Lattner82bec2c2004-11-15 04:44:20 +00001081
1082namespace {
Chris Lattnerac0b6ae2006-12-06 17:46:33 +00001083 Statistic NumInstRemoved("sccp", "Number of instructions removed");
1084 Statistic NumDeadBlocks ("sccp", "Number of basic blocks unreachable");
Chris Lattner59acc7d2004-12-10 08:02:06 +00001085
Chris Lattner14051812004-11-15 07:15:04 +00001086 //===--------------------------------------------------------------------===//
Chris Lattner82bec2c2004-11-15 04:44:20 +00001087 //
Chris Lattner14051812004-11-15 07:15:04 +00001088 /// SCCP Class - This class uses the SCCPSolver to implement a per-function
1089 /// Sparse Conditional COnstant Propagator.
1090 ///
1091 struct SCCP : public FunctionPass {
1092 // runOnFunction - Run the Sparse Conditional Constant Propagation
1093 // algorithm, and return true if the function was modified.
1094 //
1095 bool runOnFunction(Function &F);
Misha Brukmanfd939082005-04-21 23:48:37 +00001096
Chris Lattner14051812004-11-15 07:15:04 +00001097 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1098 AU.setPreservesCFG();
1099 }
1100 };
Chris Lattner82bec2c2004-11-15 04:44:20 +00001101
Chris Lattner7f8897f2006-08-27 22:42:52 +00001102 RegisterPass<SCCP> X("sccp", "Sparse Conditional Constant Propagation");
Chris Lattner82bec2c2004-11-15 04:44:20 +00001103} // end anonymous namespace
1104
1105
1106// createSCCPPass - This is the public interface to this file...
1107FunctionPass *llvm::createSCCPPass() {
1108 return new SCCP();
1109}
1110
1111
Chris Lattner82bec2c2004-11-15 04:44:20 +00001112// runOnFunction() - Run the Sparse Conditional Constant Propagation algorithm,
1113// and return true if the function was modified.
1114//
1115bool SCCP::runOnFunction(Function &F) {
Bill Wendlingb7427032006-11-26 09:46:52 +00001116 DOUT << "SCCP on function '" << F.getName() << "'\n";
Chris Lattner82bec2c2004-11-15 04:44:20 +00001117 SCCPSolver Solver;
1118
1119 // Mark the first block of the function as being executable.
1120 Solver.MarkBlockExecutable(F.begin());
1121
Chris Lattner7e529e42004-11-15 05:45:33 +00001122 // Mark all arguments to the function as being overdefined.
1123 hash_map<Value*, LatticeVal> &Values = Solver.getValueMapping();
Chris Lattnere4d5c442005-03-15 04:54:21 +00001124 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E; ++AI)
Chris Lattner7e529e42004-11-15 05:45:33 +00001125 Values[AI].markOverdefined();
1126
Chris Lattner82bec2c2004-11-15 04:44:20 +00001127 // Solve for constants.
Chris Lattnerfc6ac502004-12-10 20:41:50 +00001128 bool ResolvedBranches = true;
1129 while (ResolvedBranches) {
1130 Solver.Solve();
Bill Wendlingb7427032006-11-26 09:46:52 +00001131 DOUT << "RESOLVING UNDEF BRANCHES\n";
Chris Lattnerfc6ac502004-12-10 20:41:50 +00001132 ResolvedBranches = Solver.ResolveBranchesIn(F);
1133 }
Chris Lattner82bec2c2004-11-15 04:44:20 +00001134
Chris Lattner7e529e42004-11-15 05:45:33 +00001135 bool MadeChanges = false;
1136
1137 // If we decided that there are basic blocks that are dead in this function,
1138 // delete their contents now. Note that we cannot actually delete the blocks,
1139 // as we cannot modify the CFG of the function.
1140 //
1141 std::set<BasicBlock*> &ExecutableBBs = Solver.getExecutableBlocks();
1142 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1143 if (!ExecutableBBs.count(BB)) {
Bill Wendlingb7427032006-11-26 09:46:52 +00001144 DOUT << " BasicBlock Dead:" << *BB;
Chris Lattnerb77d5d82004-11-15 07:02:42 +00001145 ++NumDeadBlocks;
1146
Chris Lattner7e529e42004-11-15 05:45:33 +00001147 // Delete the instructions backwards, as it has a reduced likelihood of
1148 // having to update as many def-use and use-def chains.
1149 std::vector<Instruction*> Insts;
1150 for (BasicBlock::iterator I = BB->begin(), E = BB->getTerminator();
1151 I != E; ++I)
1152 Insts.push_back(I);
1153 while (!Insts.empty()) {
1154 Instruction *I = Insts.back();
1155 Insts.pop_back();
1156 if (!I->use_empty())
1157 I->replaceAllUsesWith(UndefValue::get(I->getType()));
1158 BB->getInstList().erase(I);
1159 MadeChanges = true;
Chris Lattnerb77d5d82004-11-15 07:02:42 +00001160 ++NumInstRemoved;
Chris Lattner7e529e42004-11-15 05:45:33 +00001161 }
Chris Lattner59acc7d2004-12-10 08:02:06 +00001162 } else {
1163 // Iterate over all of the instructions in a function, replacing them with
1164 // constants if we have found them to be of constant values.
1165 //
1166 for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
1167 Instruction *Inst = BI++;
1168 if (Inst->getType() != Type::VoidTy) {
1169 LatticeVal &IV = Values[Inst];
1170 if (IV.isConstant() || IV.isUndefined() &&
1171 !isa<TerminatorInst>(Inst)) {
1172 Constant *Const = IV.isConstant()
1173 ? IV.getConstant() : UndefValue::get(Inst->getType());
Bill Wendlingb7427032006-11-26 09:46:52 +00001174 DOUT << " Constant: " << *Const << " = " << *Inst;
Misha Brukmanfd939082005-04-21 23:48:37 +00001175
Chris Lattner59acc7d2004-12-10 08:02:06 +00001176 // Replaces all of the uses of a variable with uses of the constant.
1177 Inst->replaceAllUsesWith(Const);
Misha Brukmanfd939082005-04-21 23:48:37 +00001178
Chris Lattner59acc7d2004-12-10 08:02:06 +00001179 // Delete the instruction.
1180 BB->getInstList().erase(Inst);
Misha Brukmanfd939082005-04-21 23:48:37 +00001181
Chris Lattner59acc7d2004-12-10 08:02:06 +00001182 // Hey, we just changed something!
1183 MadeChanges = true;
1184 ++NumInstRemoved;
Chris Lattner82bec2c2004-11-15 04:44:20 +00001185 }
Chris Lattner82bec2c2004-11-15 04:44:20 +00001186 }
1187 }
1188 }
1189
1190 return MadeChanges;
1191}
Chris Lattner59acc7d2004-12-10 08:02:06 +00001192
1193namespace {
Chris Lattnerac0b6ae2006-12-06 17:46:33 +00001194 Statistic IPNumInstRemoved("ipsccp", "Number of instructions removed");
1195 Statistic IPNumDeadBlocks ("ipsccp", "Number of basic blocks unreachable");
1196 Statistic IPNumArgsElimed ("ipsccp",
Chris Lattner59acc7d2004-12-10 08:02:06 +00001197 "Number of arguments constant propagated");
Chris Lattnerac0b6ae2006-12-06 17:46:33 +00001198 Statistic IPNumGlobalConst("ipsccp",
Chris Lattnerdd336d12004-12-11 05:15:59 +00001199 "Number of globals found to be constant");
Chris Lattner59acc7d2004-12-10 08:02:06 +00001200
1201 //===--------------------------------------------------------------------===//
1202 //
1203 /// IPSCCP Class - This class implements interprocedural Sparse Conditional
1204 /// Constant Propagation.
1205 ///
1206 struct IPSCCP : public ModulePass {
1207 bool runOnModule(Module &M);
1208 };
1209
Chris Lattner7f8897f2006-08-27 22:42:52 +00001210 RegisterPass<IPSCCP>
Chris Lattner59acc7d2004-12-10 08:02:06 +00001211 Y("ipsccp", "Interprocedural Sparse Conditional Constant Propagation");
1212} // end anonymous namespace
1213
1214// createIPSCCPPass - This is the public interface to this file...
1215ModulePass *llvm::createIPSCCPPass() {
1216 return new IPSCCP();
1217}
1218
1219
1220static bool AddressIsTaken(GlobalValue *GV) {
Chris Lattner7d27fc02005-04-19 19:16:19 +00001221 // Delete any dead constantexpr klingons.
1222 GV->removeDeadConstantUsers();
1223
Chris Lattner59acc7d2004-12-10 08:02:06 +00001224 for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end();
1225 UI != E; ++UI)
1226 if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
Chris Lattnerdd336d12004-12-11 05:15:59 +00001227 if (SI->getOperand(0) == GV || SI->isVolatile())
1228 return true; // Storing addr of GV.
Chris Lattner59acc7d2004-12-10 08:02:06 +00001229 } else if (isa<InvokeInst>(*UI) || isa<CallInst>(*UI)) {
1230 // Make sure we are calling the function, not passing the address.
1231 CallSite CS = CallSite::get(cast<Instruction>(*UI));
1232 for (CallSite::arg_iterator AI = CS.arg_begin(),
1233 E = CS.arg_end(); AI != E; ++AI)
1234 if (*AI == GV)
1235 return true;
Chris Lattnerdd336d12004-12-11 05:15:59 +00001236 } else if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
1237 if (LI->isVolatile())
1238 return true;
1239 } else {
Chris Lattner59acc7d2004-12-10 08:02:06 +00001240 return true;
1241 }
1242 return false;
1243}
1244
1245bool IPSCCP::runOnModule(Module &M) {
1246 SCCPSolver Solver;
1247
1248 // Loop over all functions, marking arguments to those with their addresses
1249 // taken or that are external as overdefined.
1250 //
1251 hash_map<Value*, LatticeVal> &Values = Solver.getValueMapping();
1252 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
1253 if (!F->hasInternalLinkage() || AddressIsTaken(F)) {
1254 if (!F->isExternal())
1255 Solver.MarkBlockExecutable(F->begin());
Chris Lattner7d27fc02005-04-19 19:16:19 +00001256 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1257 AI != E; ++AI)
Chris Lattner59acc7d2004-12-10 08:02:06 +00001258 Values[AI].markOverdefined();
1259 } else {
1260 Solver.AddTrackedFunction(F);
1261 }
1262
Chris Lattnerdd336d12004-12-11 05:15:59 +00001263 // Loop over global variables. We inform the solver about any internal global
1264 // variables that do not have their 'addresses taken'. If they don't have
1265 // their addresses taken, we can propagate constants through them.
Chris Lattner7d27fc02005-04-19 19:16:19 +00001266 for (Module::global_iterator G = M.global_begin(), E = M.global_end();
1267 G != E; ++G)
Chris Lattnerdd336d12004-12-11 05:15:59 +00001268 if (!G->isConstant() && G->hasInternalLinkage() && !AddressIsTaken(G))
1269 Solver.TrackValueOfGlobalVariable(G);
1270
Chris Lattner59acc7d2004-12-10 08:02:06 +00001271 // Solve for constants.
Chris Lattnerfc6ac502004-12-10 20:41:50 +00001272 bool ResolvedBranches = true;
1273 while (ResolvedBranches) {
1274 Solver.Solve();
1275
Bill Wendlingb7427032006-11-26 09:46:52 +00001276 DOUT << "RESOLVING UNDEF BRANCHES\n";
Chris Lattnerfc6ac502004-12-10 20:41:50 +00001277 ResolvedBranches = false;
1278 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
1279 ResolvedBranches |= Solver.ResolveBranchesIn(*F);
1280 }
Chris Lattner59acc7d2004-12-10 08:02:06 +00001281
1282 bool MadeChanges = false;
1283
1284 // Iterate over all of the instructions in the module, replacing them with
1285 // constants if we have found them to be of constant values.
1286 //
1287 std::set<BasicBlock*> &ExecutableBBs = Solver.getExecutableBlocks();
1288 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
Chris Lattner7d27fc02005-04-19 19:16:19 +00001289 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1290 AI != E; ++AI)
Chris Lattner59acc7d2004-12-10 08:02:06 +00001291 if (!AI->use_empty()) {
1292 LatticeVal &IV = Values[AI];
1293 if (IV.isConstant() || IV.isUndefined()) {
1294 Constant *CST = IV.isConstant() ?
1295 IV.getConstant() : UndefValue::get(AI->getType());
Bill Wendlingb7427032006-11-26 09:46:52 +00001296 DOUT << "*** Arg " << *AI << " = " << *CST <<"\n";
Misha Brukmanfd939082005-04-21 23:48:37 +00001297
Chris Lattner59acc7d2004-12-10 08:02:06 +00001298 // Replaces all of the uses of a variable with uses of the
1299 // constant.
1300 AI->replaceAllUsesWith(CST);
1301 ++IPNumArgsElimed;
1302 }
1303 }
1304
Chris Lattner5f9e8b42004-12-10 22:29:08 +00001305 std::vector<BasicBlock*> BlocksToErase;
Chris Lattner59acc7d2004-12-10 08:02:06 +00001306 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
1307 if (!ExecutableBBs.count(BB)) {
Bill Wendlingb7427032006-11-26 09:46:52 +00001308 DOUT << " BasicBlock Dead:" << *BB;
Chris Lattner59acc7d2004-12-10 08:02:06 +00001309 ++IPNumDeadBlocks;
Chris Lattnerfc6ac502004-12-10 20:41:50 +00001310
Chris Lattner59acc7d2004-12-10 08:02:06 +00001311 // Delete the instructions backwards, as it has a reduced likelihood of
1312 // having to update as many def-use and use-def chains.
1313 std::vector<Instruction*> Insts;
Chris Lattner5f9e8b42004-12-10 22:29:08 +00001314 TerminatorInst *TI = BB->getTerminator();
1315 for (BasicBlock::iterator I = BB->begin(), E = TI; I != E; ++I)
Chris Lattner59acc7d2004-12-10 08:02:06 +00001316 Insts.push_back(I);
Chris Lattner5f9e8b42004-12-10 22:29:08 +00001317
Chris Lattner59acc7d2004-12-10 08:02:06 +00001318 while (!Insts.empty()) {
1319 Instruction *I = Insts.back();
1320 Insts.pop_back();
1321 if (!I->use_empty())
1322 I->replaceAllUsesWith(UndefValue::get(I->getType()));
1323 BB->getInstList().erase(I);
1324 MadeChanges = true;
1325 ++IPNumInstRemoved;
1326 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001327
Chris Lattner5f9e8b42004-12-10 22:29:08 +00001328 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
1329 BasicBlock *Succ = TI->getSuccessor(i);
1330 if (Succ->begin() != Succ->end() && isa<PHINode>(Succ->begin()))
1331 TI->getSuccessor(i)->removePredecessor(BB);
1332 }
Chris Lattner0417feb2004-12-11 02:53:57 +00001333 if (!TI->use_empty())
1334 TI->replaceAllUsesWith(UndefValue::get(TI->getType()));
Chris Lattner5f9e8b42004-12-10 22:29:08 +00001335 BB->getInstList().erase(TI);
1336
Chris Lattner864737b2004-12-11 05:32:19 +00001337 if (&*BB != &F->front())
1338 BlocksToErase.push_back(BB);
1339 else
1340 new UnreachableInst(BB);
1341
Chris Lattner59acc7d2004-12-10 08:02:06 +00001342 } else {
1343 for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
1344 Instruction *Inst = BI++;
1345 if (Inst->getType() != Type::VoidTy) {
1346 LatticeVal &IV = Values[Inst];
1347 if (IV.isConstant() || IV.isUndefined() &&
1348 !isa<TerminatorInst>(Inst)) {
1349 Constant *Const = IV.isConstant()
1350 ? IV.getConstant() : UndefValue::get(Inst->getType());
Bill Wendlingb7427032006-11-26 09:46:52 +00001351 DOUT << " Constant: " << *Const << " = " << *Inst;
Misha Brukmanfd939082005-04-21 23:48:37 +00001352
Chris Lattner59acc7d2004-12-10 08:02:06 +00001353 // Replaces all of the uses of a variable with uses of the
1354 // constant.
1355 Inst->replaceAllUsesWith(Const);
Misha Brukmanfd939082005-04-21 23:48:37 +00001356
Chris Lattner59acc7d2004-12-10 08:02:06 +00001357 // Delete the instruction.
1358 if (!isa<TerminatorInst>(Inst) && !isa<CallInst>(Inst))
1359 BB->getInstList().erase(Inst);
1360
1361 // Hey, we just changed something!
1362 MadeChanges = true;
1363 ++IPNumInstRemoved;
1364 }
1365 }
1366 }
1367 }
Chris Lattner5f9e8b42004-12-10 22:29:08 +00001368
1369 // Now that all instructions in the function are constant folded, erase dead
1370 // blocks, because we can now use ConstantFoldTerminator to get rid of
1371 // in-edges.
1372 for (unsigned i = 0, e = BlocksToErase.size(); i != e; ++i) {
1373 // If there are any PHI nodes in this successor, drop entries for BB now.
1374 BasicBlock *DeadBB = BlocksToErase[i];
1375 while (!DeadBB->use_empty()) {
1376 Instruction *I = cast<Instruction>(DeadBB->use_back());
1377 bool Folded = ConstantFoldTerminator(I->getParent());
Chris Lattnerddaaa372006-10-23 18:57:02 +00001378 if (!Folded) {
1379 // The constant folder may not have been able to fold the termiantor
1380 // if this is a branch or switch on undef. Fold it manually as a
1381 // branch to the first successor.
1382 if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
1383 assert(BI->isConditional() && isa<UndefValue>(BI->getCondition()) &&
1384 "Branch should be foldable!");
1385 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
1386 assert(isa<UndefValue>(SI->getCondition()) && "Switch should fold");
1387 } else {
1388 assert(0 && "Didn't fold away reference to block!");
1389 }
1390
1391 // Make this an uncond branch to the first successor.
1392 TerminatorInst *TI = I->getParent()->getTerminator();
1393 new BranchInst(TI->getSuccessor(0), TI);
1394
1395 // Remove entries in successor phi nodes to remove edges.
1396 for (unsigned i = 1, e = TI->getNumSuccessors(); i != e; ++i)
1397 TI->getSuccessor(i)->removePredecessor(TI->getParent());
1398
1399 // Remove the old terminator.
1400 TI->eraseFromParent();
1401 }
Chris Lattner5f9e8b42004-12-10 22:29:08 +00001402 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001403
Chris Lattner5f9e8b42004-12-10 22:29:08 +00001404 // Finally, delete the basic block.
1405 F->getBasicBlockList().erase(DeadBB);
1406 }
Chris Lattner59acc7d2004-12-10 08:02:06 +00001407 }
Chris Lattner0417feb2004-12-11 02:53:57 +00001408
1409 // If we inferred constant or undef return values for a function, we replaced
1410 // all call uses with the inferred value. This means we don't need to bother
1411 // actually returning anything from the function. Replace all return
1412 // instructions with return undef.
1413 const hash_map<Function*, LatticeVal> &RV =Solver.getTrackedFunctionRetVals();
1414 for (hash_map<Function*, LatticeVal>::const_iterator I = RV.begin(),
1415 E = RV.end(); I != E; ++I)
1416 if (!I->second.isOverdefined() &&
1417 I->first->getReturnType() != Type::VoidTy) {
1418 Function *F = I->first;
1419 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
1420 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))
1421 if (!isa<UndefValue>(RI->getOperand(0)))
1422 RI->setOperand(0, UndefValue::get(F->getReturnType()));
1423 }
Chris Lattnerdd336d12004-12-11 05:15:59 +00001424
1425 // If we infered constant or undef values for globals variables, we can delete
1426 // the global and any stores that remain to it.
1427 const hash_map<GlobalVariable*, LatticeVal> &TG = Solver.getTrackedGlobals();
1428 for (hash_map<GlobalVariable*, LatticeVal>::const_iterator I = TG.begin(),
1429 E = TG.end(); I != E; ++I) {
1430 GlobalVariable *GV = I->first;
1431 assert(!I->second.isOverdefined() &&
1432 "Overdefined values should have been taken out of the map!");
Bill Wendlingb7427032006-11-26 09:46:52 +00001433 DOUT << "Found that GV '" << GV->getName()<< "' is constant!\n";
Chris Lattnerdd336d12004-12-11 05:15:59 +00001434 while (!GV->use_empty()) {
1435 StoreInst *SI = cast<StoreInst>(GV->use_back());
1436 SI->eraseFromParent();
1437 }
1438 M.getGlobalList().erase(GV);
Chris Lattnerdade2d22004-12-11 06:05:53 +00001439 ++IPNumGlobalConst;
Chris Lattnerdd336d12004-12-11 05:15:59 +00001440 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001441
Chris Lattner59acc7d2004-12-10 08:02:06 +00001442 return MadeChanges;
1443}