blob: 48c116d5f35ab6fe427f260f1c151b299532aba0 [file] [log] [blame]
Misha Brukman373086d2003-05-20 21:01:22 +00001//===- SCCP.cpp - Sparse Conditional Constant Propagation -----------------===//
Chris Lattner347389d2001-06-27 23:38:11 +00002//
Misha Brukman373086d2003-05-20 21:01:22 +00003// This file implements sparse conditional constant propagation and merging:
Chris Lattner347389d2001-06-27 23:38:11 +00004//
5// Specifically, this:
6// * Assumes values are constant unless proven otherwise
7// * Assumes BasicBlocks are dead unless proven otherwise
8// * Proves values to be constant, and replaces them with constants
Chris Lattnerdd6522e2002-08-30 23:39:00 +00009// * Proves conditional branches to be unconditional
Chris Lattner347389d2001-06-27 23:38:11 +000010//
11// Notice that:
12// * This pass has a habit of making definitions be dead. It is a good idea
13// to to run a DCE pass sometime after running this pass.
14//
15//===----------------------------------------------------------------------===//
16
Chris Lattnerb4cfa7f2002-05-07 20:03:00 +000017#include "llvm/Transforms/Scalar.h"
Chris Lattner65b529f2002-04-08 20:18:09 +000018#include "llvm/ConstantHandling.h"
Chris Lattner57698e22002-03-26 18:01:55 +000019#include "llvm/Function.h"
Chris Lattnercccc5c72003-04-25 02:50:03 +000020#include "llvm/Instructions.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000021#include "llvm/Pass.h"
Chris Lattner6e560792002-04-18 15:13:15 +000022#include "llvm/Support/InstVisitor.h"
Chris Lattner8abcd562003-08-01 22:15:03 +000023#include "Support/Debug.h"
Chris Lattnerbf3a0992002-10-01 22:38:41 +000024#include "Support/Statistic.h"
Chris Lattner8abcd562003-08-01 22:15:03 +000025#include "Support/STLExtras.h"
Chris Lattner347389d2001-06-27 23:38:11 +000026#include <algorithm>
Chris Lattner347389d2001-06-27 23:38:11 +000027#include <set>
28
Chris Lattner347389d2001-06-27 23:38:11 +000029// InstVal class - This class represents the different lattice values that an
Chris Lattnerc8e66542002-04-27 06:56:12 +000030// instruction may occupy. It is a simple class with value semantics.
Chris Lattner347389d2001-06-27 23:38:11 +000031//
Chris Lattner7d325382002-04-29 21:26:08 +000032namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000033 Statistic<> NumInstRemoved("sccp", "Number of instructions removed");
34
Chris Lattner347389d2001-06-27 23:38:11 +000035class InstVal {
36 enum {
Chris Lattner3462ae32001-12-03 22:26:30 +000037 undefined, // This instruction has no known value
38 constant, // This instruction has a constant value
Chris Lattner3462ae32001-12-03 22:26:30 +000039 overdefined // This instruction has an unknown value
40 } LatticeValue; // The current lattice position
41 Constant *ConstantVal; // If Constant value, the current value
Chris Lattner347389d2001-06-27 23:38:11 +000042public:
Chris Lattner3462ae32001-12-03 22:26:30 +000043 inline InstVal() : LatticeValue(undefined), ConstantVal(0) {}
Chris Lattner347389d2001-06-27 23:38:11 +000044
45 // markOverdefined - Return true if this is a new status to be in...
46 inline bool markOverdefined() {
Chris Lattner3462ae32001-12-03 22:26:30 +000047 if (LatticeValue != overdefined) {
48 LatticeValue = overdefined;
Chris Lattner347389d2001-06-27 23:38:11 +000049 return true;
50 }
51 return false;
52 }
53
54 // markConstant - Return true if this is a new status for us...
Chris Lattner3462ae32001-12-03 22:26:30 +000055 inline bool markConstant(Constant *V) {
56 if (LatticeValue != constant) {
57 LatticeValue = constant;
Chris Lattner347389d2001-06-27 23:38:11 +000058 ConstantVal = V;
59 return true;
60 } else {
Chris Lattnerdae05dc2001-09-07 16:43:22 +000061 assert(ConstantVal == V && "Marking constant with different value");
Chris Lattner347389d2001-06-27 23:38:11 +000062 }
63 return false;
64 }
65
Chris Lattner3462ae32001-12-03 22:26:30 +000066 inline bool isUndefined() const { return LatticeValue == undefined; }
67 inline bool isConstant() const { return LatticeValue == constant; }
68 inline bool isOverdefined() const { return LatticeValue == overdefined; }
Chris Lattner347389d2001-06-27 23:38:11 +000069
Chris Lattner3462ae32001-12-03 22:26:30 +000070 inline Constant *getConstant() const { return ConstantVal; }
Chris Lattner347389d2001-06-27 23:38:11 +000071};
72
Chris Lattner7d325382002-04-29 21:26:08 +000073} // end anonymous namespace
Chris Lattner347389d2001-06-27 23:38:11 +000074
75
76//===----------------------------------------------------------------------===//
77// SCCP Class
78//
Misha Brukman373086d2003-05-20 21:01:22 +000079// This class does all of the work of Sparse Conditional Constant Propagation.
Chris Lattner347389d2001-06-27 23:38:11 +000080//
Chris Lattner7d325382002-04-29 21:26:08 +000081namespace {
82class SCCP : public FunctionPass, public InstVisitor<SCCP> {
Chris Lattner7f74a562002-01-20 22:54:45 +000083 std::set<BasicBlock*> BBExecutable;// The basic blocks that are executable
84 std::map<Value*, InstVal> ValueState; // The state each value is in...
Chris Lattner347389d2001-06-27 23:38:11 +000085
Chris Lattnerd66a6e32002-05-07 04:29:32 +000086 std::vector<Instruction*> InstWorkList;// The instruction work list
Chris Lattner7f74a562002-01-20 22:54:45 +000087 std::vector<BasicBlock*> BBWorkList; // The BasicBlock work list
Chris Lattner0bbbe5d2003-10-08 16:55:34 +000088
89 /// KnownFeasibleEdges - Entries in this set are edges which have already had
90 /// PHI nodes retriggered.
91 typedef std::pair<BasicBlock*,BasicBlock*> Edge;
92 std::set<Edge> KnownFeasibleEdges;
Chris Lattner347389d2001-06-27 23:38:11 +000093public:
94
Misha Brukman373086d2003-05-20 21:01:22 +000095 // runOnFunction - Run the Sparse Conditional Constant Propagation algorithm,
Chris Lattner7d325382002-04-29 21:26:08 +000096 // and return true if the function was modified.
97 //
Chris Lattner113f4f42002-06-25 16:13:24 +000098 bool runOnFunction(Function &F);
Chris Lattner7d325382002-04-29 21:26:08 +000099
100 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner820d9712002-10-21 20:00:28 +0000101 AU.setPreservesCFG();
Chris Lattner7d325382002-04-29 21:26:08 +0000102 }
103
Chris Lattner347389d2001-06-27 23:38:11 +0000104
105 //===--------------------------------------------------------------------===//
106 // The implementation of this class
107 //
108private:
Chris Lattner6e560792002-04-18 15:13:15 +0000109 friend class InstVisitor<SCCP>; // Allow callbacks from visitor
Chris Lattner347389d2001-06-27 23:38:11 +0000110
111 // markValueOverdefined - Make a value be marked as "constant". If the value
112 // is not already a constant, add it to the instruction work list so that
113 // the users of the instruction are updated later.
114 //
Chris Lattner7324f7c2003-10-08 16:21:03 +0000115 inline void markConstant(InstVal &IV, Instruction *I, Constant *C) {
116 if (IV.markConstant(C)) {
117 DEBUG(std::cerr << "markConstant: " << *C << ": " << *I);
Chris Lattnerd66a6e32002-05-07 04:29:32 +0000118 InstWorkList.push_back(I);
Chris Lattner347389d2001-06-27 23:38:11 +0000119 }
Chris Lattner7324f7c2003-10-08 16:21:03 +0000120 }
121 inline void markConstant(Instruction *I, Constant *C) {
122 markConstant(ValueState[I], I, C);
Chris Lattner347389d2001-06-27 23:38:11 +0000123 }
124
125 // markValueOverdefined - Make a value be marked as "overdefined". If the
126 // value is not already overdefined, add it to the instruction work list so
127 // that the users of the instruction are updated later.
128 //
Chris Lattner7324f7c2003-10-08 16:21:03 +0000129 inline void markOverdefined(InstVal &IV, Instruction *I) {
130 if (IV.markOverdefined()) {
131 DEBUG(std::cerr << "markOverdefined: " << *I);
132 InstWorkList.push_back(I); // Only instructions go on the work list
Chris Lattner347389d2001-06-27 23:38:11 +0000133 }
Chris Lattner7324f7c2003-10-08 16:21:03 +0000134 }
135 inline void markOverdefined(Instruction *I) {
136 markOverdefined(ValueState[I], I);
Chris Lattner347389d2001-06-27 23:38:11 +0000137 }
138
139 // getValueState - Return the InstVal object that corresponds to the value.
Misha Brukman7eb05a12003-08-18 14:43:39 +0000140 // This function is necessary because not all values should start out in the
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000141 // underdefined state... Argument's should be overdefined, and
Chris Lattner57698e22002-03-26 18:01:55 +0000142 // constants should be marked as constants. If a value is not known to be an
Chris Lattner347389d2001-06-27 23:38:11 +0000143 // Instruction object, then use this accessor to get its value from the map.
144 //
145 inline InstVal &getValueState(Value *V) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000146 std::map<Value*, InstVal>::iterator I = ValueState.find(V);
Chris Lattner347389d2001-06-27 23:38:11 +0000147 if (I != ValueState.end()) return I->second; // Common case, in the map
148
Chris Lattner3462ae32001-12-03 22:26:30 +0000149 if (Constant *CPV = dyn_cast<Constant>(V)) { // Constants are constant
Chris Lattner347389d2001-06-27 23:38:11 +0000150 ValueState[CPV].markConstant(CPV);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000151 } else if (isa<Argument>(V)) { // Arguments are overdefined
Chris Lattner347389d2001-06-27 23:38:11 +0000152 ValueState[V].markOverdefined();
Chris Lattnerdd6522e2002-08-30 23:39:00 +0000153 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
154 // The address of a global is a constant...
155 ValueState[V].markConstant(ConstantPointerRef::get(GV));
156 }
Chris Lattner347389d2001-06-27 23:38:11 +0000157 // All others are underdefined by default...
158 return ValueState[V];
159 }
160
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000161 // markEdgeExecutable - Mark a basic block as executable, adding it to the BB
Chris Lattner347389d2001-06-27 23:38:11 +0000162 // work list if it is not already executable...
163 //
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000164 void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
165 if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
166 return; // This edge is already known to be executable!
167
168 if (BBExecutable.count(Dest)) {
169 DEBUG(std::cerr << "Marking Edge Executable: " << Source->getName()
170 << " -> " << Dest->getName() << "\n");
171
172 // The destination is already executable, but we just made an edge
173 // feasible that wasn't before. Add the PHI nodes to the work list so
174 // that they can be rechecked.
175 for (BasicBlock::iterator I = Dest->begin();
Chris Lattnercccc5c72003-04-25 02:50:03 +0000176 PHINode *PN = dyn_cast<PHINode>(I); ++I)
Chris Lattner3c982762003-04-25 03:35:10 +0000177 visitPHINode(*PN);
Chris Lattnercccc5c72003-04-25 02:50:03 +0000178
179 } else {
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000180 DEBUG(std::cerr << "Marking Block Executable: " << Dest->getName()<<"\n");
181 BBExecutable.insert(Dest); // Basic block is executable!
182 BBWorkList.push_back(Dest); // Add the block to the work list!
Chris Lattnercccc5c72003-04-25 02:50:03 +0000183 }
Chris Lattner347389d2001-06-27 23:38:11 +0000184 }
185
Chris Lattner347389d2001-06-27 23:38:11 +0000186
Chris Lattner6e560792002-04-18 15:13:15 +0000187 // visit implementations - Something changed in this instruction... Either an
Chris Lattner10b250e2001-06-29 23:56:23 +0000188 // operand made a transition, or the instruction is newly executable. Change
189 // the value type of I to reflect these changes if appropriate.
190 //
Chris Lattner113f4f42002-06-25 16:13:24 +0000191 void visitPHINode(PHINode &I);
Chris Lattner6e560792002-04-18 15:13:15 +0000192
193 // Terminators
Chris Lattner113f4f42002-06-25 16:13:24 +0000194 void visitReturnInst(ReturnInst &I) { /*does not have an effect*/ }
195 void visitTerminatorInst(TerminatorInst &TI);
Chris Lattner6e560792002-04-18 15:13:15 +0000196
Chris Lattner6e1a1b12002-08-14 17:53:45 +0000197 void visitCastInst(CastInst &I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000198 void visitBinaryOperator(Instruction &I);
199 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
Chris Lattner6e560792002-04-18 15:13:15 +0000200
201 // Instructions that cannot be folded away...
Chris Lattner113f4f42002-06-25 16:13:24 +0000202 void visitStoreInst (Instruction &I) { /*returns void*/ }
Chris Lattnerdfb3a2c2002-08-22 23:37:20 +0000203 void visitLoadInst (Instruction &I) { markOverdefined(&I); }
Chris Lattnerdd6522e2002-08-30 23:39:00 +0000204 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000205 void visitCallInst (Instruction &I) { markOverdefined(&I); }
Chris Lattnerdf741d62003-08-27 01:08:35 +0000206 void visitInvokeInst (TerminatorInst &I) {
Chris Lattner7324f7c2003-10-08 16:21:03 +0000207 if (I.getType() != Type::VoidTy) markOverdefined(&I);
Chris Lattnerdf741d62003-08-27 01:08:35 +0000208 visitTerminatorInst(I);
209 }
Chris Lattner9c58cf62003-09-08 18:54:55 +0000210 void visitUnwindInst (TerminatorInst &I) { /*returns void*/ }
Chris Lattner113f4f42002-06-25 16:13:24 +0000211 void visitAllocationInst(Instruction &I) { markOverdefined(&I); }
Chris Lattnerb0269722003-05-08 02:50:13 +0000212 void visitVarArgInst (Instruction &I) { markOverdefined(&I); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000213 void visitFreeInst (Instruction &I) { /*returns void*/ }
Chris Lattner6e560792002-04-18 15:13:15 +0000214
Chris Lattner113f4f42002-06-25 16:13:24 +0000215 void visitInstruction(Instruction &I) {
Chris Lattner6e560792002-04-18 15:13:15 +0000216 // If a new instruction is added to LLVM that we don't handle...
Chris Lattnercccc5c72003-04-25 02:50:03 +0000217 std::cerr << "SCCP: Don't know how to handle: " << I;
Chris Lattner113f4f42002-06-25 16:13:24 +0000218 markOverdefined(&I); // Just in case
Chris Lattner6e560792002-04-18 15:13:15 +0000219 }
Chris Lattner10b250e2001-06-29 23:56:23 +0000220
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000221 // getFeasibleSuccessors - Return a vector of booleans to indicate which
222 // successors are reachable from a given terminator instruction.
223 //
Chris Lattner113f4f42002-06-25 16:13:24 +0000224 void getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs);
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000225
Chris Lattner13b52e72002-05-02 21:18:01 +0000226 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
227 // block to the 'To' basic block is currently feasible...
228 //
229 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To);
230
Chris Lattner10b250e2001-06-29 23:56:23 +0000231 // OperandChangedState - This method is invoked on all of the users of an
232 // instruction that was just changed state somehow.... Based on this
233 // information, we need to update the specified user of this instruction.
234 //
Chris Lattner13b52e72002-05-02 21:18:01 +0000235 void OperandChangedState(User *U) {
236 // Only instructions use other variable values!
Chris Lattner113f4f42002-06-25 16:13:24 +0000237 Instruction &I = cast<Instruction>(*U);
Chris Lattnercccc5c72003-04-25 02:50:03 +0000238 if (BBExecutable.count(I.getParent())) // Inst is executable?
239 visit(I);
Chris Lattner13b52e72002-05-02 21:18:01 +0000240 }
Chris Lattner10b250e2001-06-29 23:56:23 +0000241};
Chris Lattnerb28b6802002-07-23 18:06:35 +0000242
Chris Lattnercccc5c72003-04-25 02:50:03 +0000243 RegisterOpt<SCCP> X("sccp", "Sparse Conditional Constant Propagation");
Chris Lattner7d325382002-04-29 21:26:08 +0000244} // end anonymous namespace
245
246
247// createSCCPPass - This is the public interface to this file...
248//
249Pass *createSCCPPass() {
250 return new SCCP();
251}
252
Chris Lattner347389d2001-06-27 23:38:11 +0000253
Chris Lattner347389d2001-06-27 23:38:11 +0000254//===----------------------------------------------------------------------===//
255// SCCP Class Implementation
256
257
Misha Brukman373086d2003-05-20 21:01:22 +0000258// runOnFunction() - Run the Sparse Conditional Constant Propagation algorithm,
Chris Lattner7d325382002-04-29 21:26:08 +0000259// and return true if the function was modified.
Chris Lattner347389d2001-06-27 23:38:11 +0000260//
Chris Lattner113f4f42002-06-25 16:13:24 +0000261bool SCCP::runOnFunction(Function &F) {
Chris Lattnerc8e66542002-04-27 06:56:12 +0000262 // Mark the first block of the function as being executable...
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000263 BBExecutable.insert(F.begin()); // Basic block is executable!
264 BBWorkList.push_back(F.begin()); // Add the block to the work list!
Chris Lattner347389d2001-06-27 23:38:11 +0000265
266 // Process the work lists until their are empty!
267 while (!BBWorkList.empty() || !InstWorkList.empty()) {
268 // Process the instruction work list...
269 while (!InstWorkList.empty()) {
Chris Lattnerd66a6e32002-05-07 04:29:32 +0000270 Instruction *I = InstWorkList.back();
271 InstWorkList.pop_back();
Chris Lattner347389d2001-06-27 23:38:11 +0000272
Chris Lattnercccc5c72003-04-25 02:50:03 +0000273 DEBUG(std::cerr << "\nPopped off I-WL: " << I);
Chris Lattner347389d2001-06-27 23:38:11 +0000274
275 // "I" got into the work list because it either made the transition from
276 // bottom to constant, or to Overdefined.
277 //
278 // Update all of the users of this instruction's value...
279 //
280 for_each(I->use_begin(), I->use_end(),
281 bind_obj(this, &SCCP::OperandChangedState));
282 }
283
284 // Process the basic block work list...
285 while (!BBWorkList.empty()) {
286 BasicBlock *BB = BBWorkList.back();
287 BBWorkList.pop_back();
288
Chris Lattnercccc5c72003-04-25 02:50:03 +0000289 DEBUG(std::cerr << "\nPopped off BBWL: " << BB);
Chris Lattner347389d2001-06-27 23:38:11 +0000290
Chris Lattner6e560792002-04-18 15:13:15 +0000291 // Notify all instructions in this basic block that they are newly
292 // executable.
293 visit(BB);
Chris Lattner347389d2001-06-27 23:38:11 +0000294 }
295 }
296
Chris Lattner71cbd422002-05-22 17:17:27 +0000297 if (DebugFlag) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000298 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
299 if (!BBExecutable.count(I))
Chris Lattnercccc5c72003-04-25 02:50:03 +0000300 std::cerr << "BasicBlock Dead:" << *I;
Chris Lattner71cbd422002-05-22 17:17:27 +0000301 }
Chris Lattner347389d2001-06-27 23:38:11 +0000302
Chris Lattnerc8e66542002-04-27 06:56:12 +0000303 // Iterate over all of the instructions in a function, replacing them with
Chris Lattner347389d2001-06-27 23:38:11 +0000304 // constants if we have found them to be of constant values.
305 //
306 bool MadeChanges = false;
Chris Lattner113f4f42002-06-25 16:13:24 +0000307 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB)
Chris Lattner60a65912002-02-12 21:07:25 +0000308 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000309 Instruction &Inst = *BI;
310 InstVal &IV = ValueState[&Inst];
Chris Lattner60a65912002-02-12 21:07:25 +0000311 if (IV.isConstant()) {
312 Constant *Const = IV.getConstant();
Chris Lattnercccc5c72003-04-25 02:50:03 +0000313 DEBUG(std::cerr << "Constant: " << Const << " = " << Inst);
Chris Lattnerc4ad64c2001-11-26 18:57:38 +0000314
Chris Lattner60a65912002-02-12 21:07:25 +0000315 // Replaces all of the uses of a variable with uses of the constant.
Chris Lattner113f4f42002-06-25 16:13:24 +0000316 Inst.replaceAllUsesWith(Const);
Chris Lattner347389d2001-06-27 23:38:11 +0000317
Chris Lattner5364d1a2002-05-02 20:32:51 +0000318 // Remove the operator from the list of definitions... and delete it.
Chris Lattner113f4f42002-06-25 16:13:24 +0000319 BI = BB->getInstList().erase(BI);
Chris Lattner347389d2001-06-27 23:38:11 +0000320
Chris Lattner60a65912002-02-12 21:07:25 +0000321 // Hey, we just changed something!
322 MadeChanges = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000323 ++NumInstRemoved;
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000324 } else {
325 ++BI;
Chris Lattner60a65912002-02-12 21:07:25 +0000326 }
Chris Lattner347389d2001-06-27 23:38:11 +0000327 }
Chris Lattner347389d2001-06-27 23:38:11 +0000328
Chris Lattner13b52e72002-05-02 21:18:01 +0000329 // Reset state so that the next invocation will have empty data structures
Chris Lattner7d325382002-04-29 21:26:08 +0000330 BBExecutable.clear();
331 ValueState.clear();
Chris Lattner669c6cf2002-11-04 02:54:22 +0000332 std::vector<Instruction*>().swap(InstWorkList);
333 std::vector<BasicBlock*>().swap(BBWorkList);
Chris Lattner7d325382002-04-29 21:26:08 +0000334
Chris Lattnerdae05dc2001-09-07 16:43:22 +0000335 return MadeChanges;
Chris Lattner347389d2001-06-27 23:38:11 +0000336}
337
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000338
339// getFeasibleSuccessors - Return a vector of booleans to indicate which
340// successors are reachable from a given terminator instruction.
341//
Chris Lattner113f4f42002-06-25 16:13:24 +0000342void SCCP::getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs) {
Chris Lattnercccc5c72003-04-25 02:50:03 +0000343 Succs.resize(TI.getNumSuccessors());
Chris Lattner113f4f42002-06-25 16:13:24 +0000344 if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000345 if (BI->isUnconditional()) {
346 Succs[0] = true;
347 } else {
348 InstVal &BCValue = getValueState(BI->getCondition());
349 if (BCValue.isOverdefined()) {
350 // Overdefined condition variables mean the branch could go either way.
351 Succs[0] = Succs[1] = true;
352 } else if (BCValue.isConstant()) {
353 // Constant condition variables mean the branch can only go a single way
354 Succs[BCValue.getConstant() == ConstantBool::False] = true;
355 }
356 }
Chris Lattner113f4f42002-06-25 16:13:24 +0000357 } else if (InvokeInst *II = dyn_cast<InvokeInst>(&TI)) {
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000358 // Invoke instructions successors are always executable.
359 Succs[0] = Succs[1] = true;
Chris Lattner113f4f42002-06-25 16:13:24 +0000360 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) {
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000361 InstVal &SCValue = getValueState(SI->getCondition());
362 if (SCValue.isOverdefined()) { // Overdefined condition?
363 // All destinations are executable!
Chris Lattner113f4f42002-06-25 16:13:24 +0000364 Succs.assign(TI.getNumSuccessors(), true);
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000365 } else if (SCValue.isConstant()) {
366 Constant *CPV = SCValue.getConstant();
367 // Make sure to skip the "default value" which isn't a value
368 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) {
369 if (SI->getSuccessorValue(i) == CPV) {// Found the right branch...
370 Succs[i] = true;
371 return;
372 }
373 }
374
375 // Constant value not equal to any of the branches... must execute
376 // default branch then...
377 Succs[0] = true;
378 }
379 } else {
Chris Lattnercccc5c72003-04-25 02:50:03 +0000380 std::cerr << "SCCP: Don't know how to handle: " << TI;
Chris Lattner113f4f42002-06-25 16:13:24 +0000381 Succs.assign(TI.getNumSuccessors(), true);
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000382 }
383}
384
385
Chris Lattner13b52e72002-05-02 21:18:01 +0000386// isEdgeFeasible - Return true if the control flow edge from the 'From' basic
387// block to the 'To' basic block is currently feasible...
388//
389bool SCCP::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
390 assert(BBExecutable.count(To) && "Dest should always be alive!");
391
392 // Make sure the source basic block is executable!!
393 if (!BBExecutable.count(From)) return false;
394
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000395 // Check to make sure this edge itself is actually feasible now...
Chris Lattner71ac22ff2003-10-08 15:47:41 +0000396 TerminatorInst *TI = From->getTerminator();
397 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
398 if (BI->isUnconditional())
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000399 return true;
Chris Lattner71ac22ff2003-10-08 15:47:41 +0000400 else {
401 InstVal &BCValue = getValueState(BI->getCondition());
402 if (BCValue.isOverdefined()) {
403 // Overdefined condition variables mean the branch could go either way.
404 return true;
405 } else if (BCValue.isConstant()) {
406 // Constant condition variables mean the branch can only go a single way
407 return BI->getSuccessor(BCValue.getConstant() ==
408 ConstantBool::False) == To;
409 }
410 return false;
411 }
412 } else if (InvokeInst *II = dyn_cast<InvokeInst>(TI)) {
413 // Invoke instructions successors are always executable.
414 return true;
415 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
416 InstVal &SCValue = getValueState(SI->getCondition());
417 if (SCValue.isOverdefined()) { // Overdefined condition?
418 // All destinations are executable!
419 return true;
420 } else if (SCValue.isConstant()) {
421 Constant *CPV = SCValue.getConstant();
422 // Make sure to skip the "default value" which isn't a value
423 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i)
424 if (SI->getSuccessorValue(i) == CPV) // Found the taken branch...
425 return SI->getSuccessor(i) == To;
426
427 // Constant value not equal to any of the branches... must execute
428 // default branch then...
429 return SI->getDefaultDest() == To;
430 }
431 return false;
432 } else {
433 std::cerr << "Unknown terminator instruction: " << *TI;
434 abort();
435 }
Chris Lattner13b52e72002-05-02 21:18:01 +0000436}
Chris Lattner347389d2001-06-27 23:38:11 +0000437
Chris Lattner6e560792002-04-18 15:13:15 +0000438// visit Implementations - Something changed in this instruction... Either an
Chris Lattner347389d2001-06-27 23:38:11 +0000439// operand made a transition, or the instruction is newly executable. Change
440// the value type of I to reflect these changes if appropriate. This method
441// makes sure to do the following actions:
442//
443// 1. If a phi node merges two constants in, and has conflicting value coming
444// from different branches, or if the PHI node merges in an overdefined
445// value, then the PHI node becomes overdefined.
446// 2. If a phi node merges only constants in, and they all agree on value, the
447// PHI node becomes a constant value equal to that.
448// 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
449// 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
450// 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
451// 6. If a conditional branch has a value that is constant, make the selected
452// destination executable
453// 7. If a conditional branch has a value that is overdefined, make all
454// successors executable.
455//
Chris Lattner113f4f42002-06-25 16:13:24 +0000456void SCCP::visitPHINode(PHINode &PN) {
Chris Lattner7324f7c2003-10-08 16:21:03 +0000457 InstVal &PNIV = getValueState(&PN);
458 if (PNIV.isOverdefined()) return; // Quick exit
Chris Lattner347389d2001-06-27 23:38:11 +0000459
Chris Lattner6e560792002-04-18 15:13:15 +0000460 // Look at all of the executable operands of the PHI node. If any of them
461 // are overdefined, the PHI becomes overdefined as well. If they are all
462 // constant, and they agree with each other, the PHI becomes the identical
463 // constant. If they are constant and don't agree, the PHI is overdefined.
464 // If there are no executable operands, the PHI remains undefined.
465 //
Chris Lattnercccc5c72003-04-25 02:50:03 +0000466 Constant *OperandVal = 0;
467 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
468 InstVal &IV = getValueState(PN.getIncomingValue(i));
469 if (IV.isUndefined()) continue; // Doesn't influence PHI node.
Chris Lattnercccc5c72003-04-25 02:50:03 +0000470
Chris Lattner113f4f42002-06-25 16:13:24 +0000471 if (isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent())) {
Chris Lattner7e270582003-06-24 20:29:52 +0000472 if (IV.isOverdefined()) { // PHI node becomes overdefined!
Chris Lattner7324f7c2003-10-08 16:21:03 +0000473 markOverdefined(PNIV, &PN);
Chris Lattner7e270582003-06-24 20:29:52 +0000474 return;
475 }
476
Chris Lattnercccc5c72003-04-25 02:50:03 +0000477 if (OperandVal == 0) { // Grab the first value...
478 OperandVal = IV.getConstant();
Chris Lattner6e560792002-04-18 15:13:15 +0000479 } else { // Another value is being merged in!
480 // There is already a reachable operand. If we conflict with it,
481 // then the PHI node becomes overdefined. If we agree with it, we
482 // can continue on.
Chris Lattnercccc5c72003-04-25 02:50:03 +0000483
Chris Lattner6e560792002-04-18 15:13:15 +0000484 // Check to see if there are two different constants merging...
Chris Lattnercccc5c72003-04-25 02:50:03 +0000485 if (IV.getConstant() != OperandVal) {
Chris Lattner6e560792002-04-18 15:13:15 +0000486 // Yes there is. This means the PHI node is not constant.
487 // You must be overdefined poor PHI.
488 //
Chris Lattner7324f7c2003-10-08 16:21:03 +0000489 markOverdefined(PNIV, &PN); // The PHI node now becomes overdefined
Chris Lattner6e560792002-04-18 15:13:15 +0000490 return; // I'm done analyzing you
Chris Lattnerc4ad64c2001-11-26 18:57:38 +0000491 }
Chris Lattner347389d2001-06-27 23:38:11 +0000492 }
493 }
Chris Lattner347389d2001-06-27 23:38:11 +0000494 }
495
Chris Lattner6e560792002-04-18 15:13:15 +0000496 // If we exited the loop, this means that the PHI node only has constant
Chris Lattnercccc5c72003-04-25 02:50:03 +0000497 // arguments that agree with each other(and OperandVal is the constant) or
498 // OperandVal is null because there are no defined incoming arguments. If
499 // this is the case, the PHI remains undefined.
Chris Lattner347389d2001-06-27 23:38:11 +0000500 //
Chris Lattnercccc5c72003-04-25 02:50:03 +0000501 if (OperandVal)
Chris Lattner7324f7c2003-10-08 16:21:03 +0000502 markConstant(PNIV, &PN, OperandVal); // Aquire operand value
Chris Lattner347389d2001-06-27 23:38:11 +0000503}
504
Chris Lattner113f4f42002-06-25 16:13:24 +0000505void SCCP::visitTerminatorInst(TerminatorInst &TI) {
Chris Lattnercccc5c72003-04-25 02:50:03 +0000506 std::vector<bool> SuccFeasible;
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000507 getFeasibleSuccessors(TI, SuccFeasible);
Chris Lattner347389d2001-06-27 23:38:11 +0000508
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000509 BasicBlock *BB = TI.getParent();
510
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000511 // Mark all feasible successors executable...
512 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000513 if (SuccFeasible[i])
514 markEdgeExecutable(BB, TI.getSuccessor(i));
Chris Lattner6e560792002-04-18 15:13:15 +0000515}
516
Chris Lattner6e1a1b12002-08-14 17:53:45 +0000517void SCCP::visitCastInst(CastInst &I) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000518 Value *V = I.getOperand(0);
Chris Lattner6e560792002-04-18 15:13:15 +0000519 InstVal &VState = getValueState(V);
520 if (VState.isOverdefined()) { // Inherit overdefinedness of operand
Chris Lattner113f4f42002-06-25 16:13:24 +0000521 markOverdefined(&I);
Misha Brukman632df282002-10-29 23:06:16 +0000522 } else if (VState.isConstant()) { // Propagate constant value
Chris Lattner6e1a1b12002-08-14 17:53:45 +0000523 Constant *Result =
524 ConstantFoldCastInstruction(VState.getConstant(), I.getType());
Chris Lattner6e560792002-04-18 15:13:15 +0000525
Chris Lattner7324f7c2003-10-08 16:21:03 +0000526 if (Result) // If this instruction constant folds!
Chris Lattner113f4f42002-06-25 16:13:24 +0000527 markConstant(&I, Result);
Chris Lattner7324f7c2003-10-08 16:21:03 +0000528 else
Chris Lattner113f4f42002-06-25 16:13:24 +0000529 markOverdefined(&I); // Don't know how to fold this instruction. :(
Chris Lattner6e560792002-04-18 15:13:15 +0000530 }
531}
532
533// Handle BinaryOperators and Shift Instructions...
Chris Lattner113f4f42002-06-25 16:13:24 +0000534void SCCP::visitBinaryOperator(Instruction &I) {
535 InstVal &V1State = getValueState(I.getOperand(0));
536 InstVal &V2State = getValueState(I.getOperand(1));
Chris Lattner6e560792002-04-18 15:13:15 +0000537 if (V1State.isOverdefined() || V2State.isOverdefined()) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000538 markOverdefined(&I);
Chris Lattner6e560792002-04-18 15:13:15 +0000539 } else if (V1State.isConstant() && V2State.isConstant()) {
Chris Lattner940daed2002-05-06 03:01:37 +0000540 Constant *Result = 0;
541 if (isa<BinaryOperator>(I))
Chris Lattner113f4f42002-06-25 16:13:24 +0000542 Result = ConstantFoldBinaryInstruction(I.getOpcode(),
Chris Lattner940daed2002-05-06 03:01:37 +0000543 V1State.getConstant(),
544 V2State.getConstant());
545 else if (isa<ShiftInst>(I))
Chris Lattner113f4f42002-06-25 16:13:24 +0000546 Result = ConstantFoldShiftInstruction(I.getOpcode(),
Chris Lattner940daed2002-05-06 03:01:37 +0000547 V1State.getConstant(),
548 V2State.getConstant());
Chris Lattner6e560792002-04-18 15:13:15 +0000549 if (Result)
Chris Lattner113f4f42002-06-25 16:13:24 +0000550 markConstant(&I, Result); // This instruction constant folds!
Chris Lattner6e560792002-04-18 15:13:15 +0000551 else
Chris Lattner113f4f42002-06-25 16:13:24 +0000552 markOverdefined(&I); // Don't know how to fold this instruction. :(
Chris Lattner6e560792002-04-18 15:13:15 +0000553 }
554}
Chris Lattnerdd6522e2002-08-30 23:39:00 +0000555
556// Handle getelementptr instructions... if all operands are constants then we
557// can turn this into a getelementptr ConstantExpr.
558//
559void SCCP::visitGetElementPtrInst(GetElementPtrInst &I) {
560 std::vector<Constant*> Operands;
561 Operands.reserve(I.getNumOperands());
562
563 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
564 InstVal &State = getValueState(I.getOperand(i));
565 if (State.isUndefined())
566 return; // Operands are not resolved yet...
567 else if (State.isOverdefined()) {
568 markOverdefined(&I);
569 return;
570 }
571 assert(State.isConstant() && "Unknown state!");
572 Operands.push_back(State.getConstant());
573 }
574
575 Constant *Ptr = Operands[0];
576 Operands.erase(Operands.begin()); // Erase the pointer from idx list...
577
578 markConstant(&I, ConstantExpr::getGetElementPtr(Ptr, Operands));
579}