blob: 01e3e26ab536c9e0bca6ae2ada04ceff775ab79b [file] [log] [blame]
Misha Brukman373086d2003-05-20 21:01:22 +00001//===- SCCP.cpp - Sparse Conditional Constant Propagation -----------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner347389d2001-06-27 23:38:11 +00009//
Misha Brukman373086d2003-05-20 21:01:22 +000010// This file implements sparse conditional constant propagation and merging:
Chris Lattner347389d2001-06-27 23:38:11 +000011//
12// Specifically, this:
13// * Assumes values are constant unless proven otherwise
14// * Assumes BasicBlocks are dead unless proven otherwise
15// * Proves values to be constant, and replaces them with constants
Chris Lattnerdd6522e2002-08-30 23:39:00 +000016// * Proves conditional branches to be unconditional
Chris Lattner347389d2001-06-27 23:38:11 +000017//
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 Lattnerb4cfa7f2002-05-07 20:03:00 +000024#include "llvm/Transforms/Scalar.h"
Chris Lattner65b529f2002-04-08 20:18:09 +000025#include "llvm/ConstantHandling.h"
Chris Lattner57698e22002-03-26 18:01:55 +000026#include "llvm/Function.h"
Chris Lattnercccc5c72003-04-25 02:50:03 +000027#include "llvm/Instructions.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000028#include "llvm/Pass.h"
Chris Lattner6e560792002-04-18 15:13:15 +000029#include "llvm/Support/InstVisitor.h"
Chris Lattner8abcd562003-08-01 22:15:03 +000030#include "Support/Debug.h"
Chris Lattnerbf3a0992002-10-01 22:38:41 +000031#include "Support/Statistic.h"
Chris Lattner8abcd562003-08-01 22:15:03 +000032#include "Support/STLExtras.h"
Chris Lattner347389d2001-06-27 23:38:11 +000033#include <algorithm>
Chris Lattner347389d2001-06-27 23:38:11 +000034#include <set>
35
Chris Lattner347389d2001-06-27 23:38:11 +000036// InstVal class - This class represents the different lattice values that an
Chris Lattnerc8e66542002-04-27 06:56:12 +000037// instruction may occupy. It is a simple class with value semantics.
Chris Lattner347389d2001-06-27 23:38:11 +000038//
Chris Lattner7d325382002-04-29 21:26:08 +000039namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000040 Statistic<> NumInstRemoved("sccp", "Number of instructions removed");
41
Chris Lattner347389d2001-06-27 23:38:11 +000042class InstVal {
43 enum {
Chris Lattner3462ae32001-12-03 22:26:30 +000044 undefined, // This instruction has no known value
45 constant, // This instruction has a constant value
Chris Lattner3462ae32001-12-03 22:26:30 +000046 overdefined // This instruction has an unknown value
47 } LatticeValue; // The current lattice position
48 Constant *ConstantVal; // If Constant value, the current value
Chris Lattner347389d2001-06-27 23:38:11 +000049public:
Chris Lattner3462ae32001-12-03 22:26:30 +000050 inline InstVal() : LatticeValue(undefined), ConstantVal(0) {}
Chris Lattner347389d2001-06-27 23:38:11 +000051
52 // markOverdefined - Return true if this is a new status to be in...
53 inline bool markOverdefined() {
Chris Lattner3462ae32001-12-03 22:26:30 +000054 if (LatticeValue != overdefined) {
55 LatticeValue = overdefined;
Chris Lattner347389d2001-06-27 23:38:11 +000056 return true;
57 }
58 return false;
59 }
60
61 // markConstant - Return true if this is a new status for us...
Chris Lattner3462ae32001-12-03 22:26:30 +000062 inline bool markConstant(Constant *V) {
63 if (LatticeValue != constant) {
64 LatticeValue = constant;
Chris Lattner347389d2001-06-27 23:38:11 +000065 ConstantVal = V;
66 return true;
67 } else {
Chris Lattnerdae05dc2001-09-07 16:43:22 +000068 assert(ConstantVal == V && "Marking constant with different value");
Chris Lattner347389d2001-06-27 23:38:11 +000069 }
70 return false;
71 }
72
Chris Lattner3462ae32001-12-03 22:26:30 +000073 inline bool isUndefined() const { return LatticeValue == undefined; }
74 inline bool isConstant() const { return LatticeValue == constant; }
75 inline bool isOverdefined() const { return LatticeValue == overdefined; }
Chris Lattner347389d2001-06-27 23:38:11 +000076
Chris Lattner3462ae32001-12-03 22:26:30 +000077 inline Constant *getConstant() const { return ConstantVal; }
Chris Lattner347389d2001-06-27 23:38:11 +000078};
79
Chris Lattner7d325382002-04-29 21:26:08 +000080} // end anonymous namespace
Chris Lattner347389d2001-06-27 23:38:11 +000081
82
83//===----------------------------------------------------------------------===//
84// SCCP Class
85//
Misha Brukman373086d2003-05-20 21:01:22 +000086// This class does all of the work of Sparse Conditional Constant Propagation.
Chris Lattner347389d2001-06-27 23:38:11 +000087//
Chris Lattner7d325382002-04-29 21:26:08 +000088namespace {
89class SCCP : public FunctionPass, public InstVisitor<SCCP> {
Chris Lattner7f74a562002-01-20 22:54:45 +000090 std::set<BasicBlock*> BBExecutable;// The basic blocks that are executable
91 std::map<Value*, InstVal> ValueState; // The state each value is in...
Chris Lattner347389d2001-06-27 23:38:11 +000092
Chris Lattnerd66a6e32002-05-07 04:29:32 +000093 std::vector<Instruction*> InstWorkList;// The instruction work list
Chris Lattner7f74a562002-01-20 22:54:45 +000094 std::vector<BasicBlock*> BBWorkList; // The BasicBlock work list
Chris Lattner0bbbe5d2003-10-08 16:55:34 +000095
96 /// KnownFeasibleEdges - Entries in this set are edges which have already had
97 /// PHI nodes retriggered.
98 typedef std::pair<BasicBlock*,BasicBlock*> Edge;
99 std::set<Edge> KnownFeasibleEdges;
Chris Lattner347389d2001-06-27 23:38:11 +0000100public:
101
Misha Brukman373086d2003-05-20 21:01:22 +0000102 // runOnFunction - Run the Sparse Conditional Constant Propagation algorithm,
Chris Lattner7d325382002-04-29 21:26:08 +0000103 // and return true if the function was modified.
104 //
Chris Lattner113f4f42002-06-25 16:13:24 +0000105 bool runOnFunction(Function &F);
Chris Lattner7d325382002-04-29 21:26:08 +0000106
107 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner820d9712002-10-21 20:00:28 +0000108 AU.setPreservesCFG();
Chris Lattner7d325382002-04-29 21:26:08 +0000109 }
110
Chris Lattner347389d2001-06-27 23:38:11 +0000111
112 //===--------------------------------------------------------------------===//
113 // The implementation of this class
114 //
115private:
Chris Lattner6e560792002-04-18 15:13:15 +0000116 friend class InstVisitor<SCCP>; // Allow callbacks from visitor
Chris Lattner347389d2001-06-27 23:38:11 +0000117
118 // markValueOverdefined - Make a value be marked as "constant". If the value
119 // is not already a constant, add it to the instruction work list so that
120 // the users of the instruction are updated later.
121 //
Chris Lattner7324f7c2003-10-08 16:21:03 +0000122 inline void markConstant(InstVal &IV, Instruction *I, Constant *C) {
123 if (IV.markConstant(C)) {
124 DEBUG(std::cerr << "markConstant: " << *C << ": " << *I);
Chris Lattnerd66a6e32002-05-07 04:29:32 +0000125 InstWorkList.push_back(I);
Chris Lattner347389d2001-06-27 23:38:11 +0000126 }
Chris Lattner7324f7c2003-10-08 16:21:03 +0000127 }
128 inline void markConstant(Instruction *I, Constant *C) {
129 markConstant(ValueState[I], I, C);
Chris Lattner347389d2001-06-27 23:38:11 +0000130 }
131
132 // markValueOverdefined - Make a value be marked as "overdefined". If the
133 // value is not already overdefined, add it to the instruction work list so
134 // that the users of the instruction are updated later.
135 //
Chris Lattner7324f7c2003-10-08 16:21:03 +0000136 inline void markOverdefined(InstVal &IV, Instruction *I) {
137 if (IV.markOverdefined()) {
138 DEBUG(std::cerr << "markOverdefined: " << *I);
139 InstWorkList.push_back(I); // Only instructions go on the work list
Chris Lattner347389d2001-06-27 23:38:11 +0000140 }
Chris Lattner7324f7c2003-10-08 16:21:03 +0000141 }
142 inline void markOverdefined(Instruction *I) {
143 markOverdefined(ValueState[I], I);
Chris Lattner347389d2001-06-27 23:38:11 +0000144 }
145
146 // getValueState - Return the InstVal object that corresponds to the value.
Misha Brukman7eb05a12003-08-18 14:43:39 +0000147 // This function is necessary because not all values should start out in the
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000148 // underdefined state... Argument's should be overdefined, and
Chris Lattner57698e22002-03-26 18:01:55 +0000149 // constants should be marked as constants. If a value is not known to be an
Chris Lattner347389d2001-06-27 23:38:11 +0000150 // Instruction object, then use this accessor to get its value from the map.
151 //
152 inline InstVal &getValueState(Value *V) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000153 std::map<Value*, InstVal>::iterator I = ValueState.find(V);
Chris Lattner347389d2001-06-27 23:38:11 +0000154 if (I != ValueState.end()) return I->second; // Common case, in the map
155
Chris Lattner3462ae32001-12-03 22:26:30 +0000156 if (Constant *CPV = dyn_cast<Constant>(V)) { // Constants are constant
Chris Lattner347389d2001-06-27 23:38:11 +0000157 ValueState[CPV].markConstant(CPV);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000158 } else if (isa<Argument>(V)) { // Arguments are overdefined
Chris Lattner347389d2001-06-27 23:38:11 +0000159 ValueState[V].markOverdefined();
Chris Lattnerdd6522e2002-08-30 23:39:00 +0000160 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
161 // The address of a global is a constant...
162 ValueState[V].markConstant(ConstantPointerRef::get(GV));
163 }
Chris Lattner347389d2001-06-27 23:38:11 +0000164 // All others are underdefined by default...
165 return ValueState[V];
166 }
167
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000168 // markEdgeExecutable - Mark a basic block as executable, adding it to the BB
Chris Lattner347389d2001-06-27 23:38:11 +0000169 // work list if it is not already executable...
170 //
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000171 void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
172 if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
173 return; // This edge is already known to be executable!
174
175 if (BBExecutable.count(Dest)) {
176 DEBUG(std::cerr << "Marking Edge Executable: " << Source->getName()
177 << " -> " << Dest->getName() << "\n");
178
179 // The destination is already executable, but we just made an edge
Chris Lattner35e56e72003-10-08 16:56:11 +0000180 // feasible that wasn't before. Revisit the PHI nodes in the block
181 // because they have potentially new operands.
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000182 for (BasicBlock::iterator I = Dest->begin();
Chris Lattnercccc5c72003-04-25 02:50:03 +0000183 PHINode *PN = dyn_cast<PHINode>(I); ++I)
Chris Lattner3c982762003-04-25 03:35:10 +0000184 visitPHINode(*PN);
Chris Lattnercccc5c72003-04-25 02:50:03 +0000185
186 } else {
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000187 DEBUG(std::cerr << "Marking Block Executable: " << Dest->getName()<<"\n");
188 BBExecutable.insert(Dest); // Basic block is executable!
189 BBWorkList.push_back(Dest); // Add the block to the work list!
Chris Lattnercccc5c72003-04-25 02:50:03 +0000190 }
Chris Lattner347389d2001-06-27 23:38:11 +0000191 }
192
Chris Lattner347389d2001-06-27 23:38:11 +0000193
Chris Lattner6e560792002-04-18 15:13:15 +0000194 // visit implementations - Something changed in this instruction... Either an
Chris Lattner10b250e2001-06-29 23:56:23 +0000195 // operand made a transition, or the instruction is newly executable. Change
196 // the value type of I to reflect these changes if appropriate.
197 //
Chris Lattner113f4f42002-06-25 16:13:24 +0000198 void visitPHINode(PHINode &I);
Chris Lattner6e560792002-04-18 15:13:15 +0000199
200 // Terminators
Chris Lattner113f4f42002-06-25 16:13:24 +0000201 void visitReturnInst(ReturnInst &I) { /*does not have an effect*/ }
202 void visitTerminatorInst(TerminatorInst &TI);
Chris Lattner6e560792002-04-18 15:13:15 +0000203
Chris Lattner6e1a1b12002-08-14 17:53:45 +0000204 void visitCastInst(CastInst &I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000205 void visitBinaryOperator(Instruction &I);
206 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
Chris Lattner6e560792002-04-18 15:13:15 +0000207
208 // Instructions that cannot be folded away...
Chris Lattner113f4f42002-06-25 16:13:24 +0000209 void visitStoreInst (Instruction &I) { /*returns void*/ }
Chris Lattnerdfb3a2c2002-08-22 23:37:20 +0000210 void visitLoadInst (Instruction &I) { markOverdefined(&I); }
Chris Lattnerdd6522e2002-08-30 23:39:00 +0000211 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000212 void visitCallInst (Instruction &I) { markOverdefined(&I); }
Chris Lattnerdf741d62003-08-27 01:08:35 +0000213 void visitInvokeInst (TerminatorInst &I) {
Chris Lattner7324f7c2003-10-08 16:21:03 +0000214 if (I.getType() != Type::VoidTy) markOverdefined(&I);
Chris Lattnerdf741d62003-08-27 01:08:35 +0000215 visitTerminatorInst(I);
216 }
Chris Lattner9c58cf62003-09-08 18:54:55 +0000217 void visitUnwindInst (TerminatorInst &I) { /*returns void*/ }
Chris Lattner113f4f42002-06-25 16:13:24 +0000218 void visitAllocationInst(Instruction &I) { markOverdefined(&I); }
Chris Lattnerf0fc9be2003-10-18 05:56:52 +0000219 void visitVANextInst (Instruction &I) { markOverdefined(&I); }
220 void visitVAArgInst (Instruction &I) { markOverdefined(&I); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000221 void visitFreeInst (Instruction &I) { /*returns void*/ }
Chris Lattner6e560792002-04-18 15:13:15 +0000222
Chris Lattner113f4f42002-06-25 16:13:24 +0000223 void visitInstruction(Instruction &I) {
Chris Lattner6e560792002-04-18 15:13:15 +0000224 // If a new instruction is added to LLVM that we don't handle...
Chris Lattnercccc5c72003-04-25 02:50:03 +0000225 std::cerr << "SCCP: Don't know how to handle: " << I;
Chris Lattner113f4f42002-06-25 16:13:24 +0000226 markOverdefined(&I); // Just in case
Chris Lattner6e560792002-04-18 15:13:15 +0000227 }
Chris Lattner10b250e2001-06-29 23:56:23 +0000228
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000229 // getFeasibleSuccessors - Return a vector of booleans to indicate which
230 // successors are reachable from a given terminator instruction.
231 //
Chris Lattner113f4f42002-06-25 16:13:24 +0000232 void getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs);
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000233
Chris Lattner13b52e72002-05-02 21:18:01 +0000234 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
235 // block to the 'To' basic block is currently feasible...
236 //
237 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To);
238
Chris Lattner10b250e2001-06-29 23:56:23 +0000239 // OperandChangedState - This method is invoked on all of the users of an
240 // instruction that was just changed state somehow.... Based on this
241 // information, we need to update the specified user of this instruction.
242 //
Chris Lattner13b52e72002-05-02 21:18:01 +0000243 void OperandChangedState(User *U) {
244 // Only instructions use other variable values!
Chris Lattner113f4f42002-06-25 16:13:24 +0000245 Instruction &I = cast<Instruction>(*U);
Chris Lattnercccc5c72003-04-25 02:50:03 +0000246 if (BBExecutable.count(I.getParent())) // Inst is executable?
247 visit(I);
Chris Lattner13b52e72002-05-02 21:18:01 +0000248 }
Chris Lattner10b250e2001-06-29 23:56:23 +0000249};
Chris Lattnerb28b6802002-07-23 18:06:35 +0000250
Chris Lattnercccc5c72003-04-25 02:50:03 +0000251 RegisterOpt<SCCP> X("sccp", "Sparse Conditional Constant Propagation");
Chris Lattner7d325382002-04-29 21:26:08 +0000252} // end anonymous namespace
253
254
255// createSCCPPass - This is the public interface to this file...
256//
257Pass *createSCCPPass() {
258 return new SCCP();
259}
260
Chris Lattner347389d2001-06-27 23:38:11 +0000261
Chris Lattner347389d2001-06-27 23:38:11 +0000262//===----------------------------------------------------------------------===//
263// SCCP Class Implementation
264
265
Misha Brukman373086d2003-05-20 21:01:22 +0000266// runOnFunction() - Run the Sparse Conditional Constant Propagation algorithm,
Chris Lattner7d325382002-04-29 21:26:08 +0000267// and return true if the function was modified.
Chris Lattner347389d2001-06-27 23:38:11 +0000268//
Chris Lattner113f4f42002-06-25 16:13:24 +0000269bool SCCP::runOnFunction(Function &F) {
Chris Lattnerc8e66542002-04-27 06:56:12 +0000270 // Mark the first block of the function as being executable...
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000271 BBExecutable.insert(F.begin()); // Basic block is executable!
272 BBWorkList.push_back(F.begin()); // Add the block to the work list!
Chris Lattner347389d2001-06-27 23:38:11 +0000273
274 // Process the work lists until their are empty!
275 while (!BBWorkList.empty() || !InstWorkList.empty()) {
276 // Process the instruction work list...
277 while (!InstWorkList.empty()) {
Chris Lattnerd66a6e32002-05-07 04:29:32 +0000278 Instruction *I = InstWorkList.back();
279 InstWorkList.pop_back();
Chris Lattner347389d2001-06-27 23:38:11 +0000280
Chris Lattnercccc5c72003-04-25 02:50:03 +0000281 DEBUG(std::cerr << "\nPopped off I-WL: " << I);
Chris Lattner347389d2001-06-27 23:38:11 +0000282
283 // "I" got into the work list because it either made the transition from
284 // bottom to constant, or to Overdefined.
285 //
286 // Update all of the users of this instruction's value...
287 //
288 for_each(I->use_begin(), I->use_end(),
289 bind_obj(this, &SCCP::OperandChangedState));
290 }
291
292 // Process the basic block work list...
293 while (!BBWorkList.empty()) {
294 BasicBlock *BB = BBWorkList.back();
295 BBWorkList.pop_back();
296
Chris Lattnercccc5c72003-04-25 02:50:03 +0000297 DEBUG(std::cerr << "\nPopped off BBWL: " << BB);
Chris Lattner347389d2001-06-27 23:38:11 +0000298
Chris Lattner6e560792002-04-18 15:13:15 +0000299 // Notify all instructions in this basic block that they are newly
300 // executable.
301 visit(BB);
Chris Lattner347389d2001-06-27 23:38:11 +0000302 }
303 }
304
Chris Lattner71cbd422002-05-22 17:17:27 +0000305 if (DebugFlag) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000306 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
307 if (!BBExecutable.count(I))
Chris Lattnercccc5c72003-04-25 02:50:03 +0000308 std::cerr << "BasicBlock Dead:" << *I;
Chris Lattner71cbd422002-05-22 17:17:27 +0000309 }
Chris Lattner347389d2001-06-27 23:38:11 +0000310
Chris Lattnerc8e66542002-04-27 06:56:12 +0000311 // Iterate over all of the instructions in a function, replacing them with
Chris Lattner347389d2001-06-27 23:38:11 +0000312 // constants if we have found them to be of constant values.
313 //
314 bool MadeChanges = false;
Chris Lattner113f4f42002-06-25 16:13:24 +0000315 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB)
Chris Lattner60a65912002-02-12 21:07:25 +0000316 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000317 Instruction &Inst = *BI;
318 InstVal &IV = ValueState[&Inst];
Chris Lattner60a65912002-02-12 21:07:25 +0000319 if (IV.isConstant()) {
320 Constant *Const = IV.getConstant();
Chris Lattnercccc5c72003-04-25 02:50:03 +0000321 DEBUG(std::cerr << "Constant: " << Const << " = " << Inst);
Chris Lattnerc4ad64c2001-11-26 18:57:38 +0000322
Chris Lattner60a65912002-02-12 21:07:25 +0000323 // Replaces all of the uses of a variable with uses of the constant.
Chris Lattner113f4f42002-06-25 16:13:24 +0000324 Inst.replaceAllUsesWith(Const);
Chris Lattner347389d2001-06-27 23:38:11 +0000325
Chris Lattner5364d1a2002-05-02 20:32:51 +0000326 // Remove the operator from the list of definitions... and delete it.
Chris Lattner113f4f42002-06-25 16:13:24 +0000327 BI = BB->getInstList().erase(BI);
Chris Lattner347389d2001-06-27 23:38:11 +0000328
Chris Lattner60a65912002-02-12 21:07:25 +0000329 // Hey, we just changed something!
330 MadeChanges = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000331 ++NumInstRemoved;
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000332 } else {
333 ++BI;
Chris Lattner60a65912002-02-12 21:07:25 +0000334 }
Chris Lattner347389d2001-06-27 23:38:11 +0000335 }
Chris Lattner347389d2001-06-27 23:38:11 +0000336
Chris Lattner13b52e72002-05-02 21:18:01 +0000337 // Reset state so that the next invocation will have empty data structures
Chris Lattner7d325382002-04-29 21:26:08 +0000338 BBExecutable.clear();
339 ValueState.clear();
Chris Lattner669c6cf2002-11-04 02:54:22 +0000340 std::vector<Instruction*>().swap(InstWorkList);
341 std::vector<BasicBlock*>().swap(BBWorkList);
Chris Lattner7d325382002-04-29 21:26:08 +0000342
Chris Lattnerdae05dc2001-09-07 16:43:22 +0000343 return MadeChanges;
Chris Lattner347389d2001-06-27 23:38:11 +0000344}
345
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000346
347// getFeasibleSuccessors - Return a vector of booleans to indicate which
348// successors are reachable from a given terminator instruction.
349//
Chris Lattner113f4f42002-06-25 16:13:24 +0000350void SCCP::getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs) {
Chris Lattnercccc5c72003-04-25 02:50:03 +0000351 Succs.resize(TI.getNumSuccessors());
Chris Lattner113f4f42002-06-25 16:13:24 +0000352 if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000353 if (BI->isUnconditional()) {
354 Succs[0] = true;
355 } else {
356 InstVal &BCValue = getValueState(BI->getCondition());
357 if (BCValue.isOverdefined()) {
358 // Overdefined condition variables mean the branch could go either way.
359 Succs[0] = Succs[1] = true;
360 } else if (BCValue.isConstant()) {
361 // Constant condition variables mean the branch can only go a single way
362 Succs[BCValue.getConstant() == ConstantBool::False] = true;
363 }
364 }
Chris Lattner113f4f42002-06-25 16:13:24 +0000365 } else if (InvokeInst *II = dyn_cast<InvokeInst>(&TI)) {
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000366 // Invoke instructions successors are always executable.
367 Succs[0] = Succs[1] = true;
Chris Lattner113f4f42002-06-25 16:13:24 +0000368 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) {
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000369 InstVal &SCValue = getValueState(SI->getCondition());
370 if (SCValue.isOverdefined()) { // Overdefined condition?
371 // All destinations are executable!
Chris Lattner113f4f42002-06-25 16:13:24 +0000372 Succs.assign(TI.getNumSuccessors(), true);
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000373 } else if (SCValue.isConstant()) {
374 Constant *CPV = SCValue.getConstant();
375 // Make sure to skip the "default value" which isn't a value
376 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) {
377 if (SI->getSuccessorValue(i) == CPV) {// Found the right branch...
378 Succs[i] = true;
379 return;
380 }
381 }
382
383 // Constant value not equal to any of the branches... must execute
384 // default branch then...
385 Succs[0] = true;
386 }
387 } else {
Chris Lattnercccc5c72003-04-25 02:50:03 +0000388 std::cerr << "SCCP: Don't know how to handle: " << TI;
Chris Lattner113f4f42002-06-25 16:13:24 +0000389 Succs.assign(TI.getNumSuccessors(), true);
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000390 }
391}
392
393
Chris Lattner13b52e72002-05-02 21:18:01 +0000394// isEdgeFeasible - Return true if the control flow edge from the 'From' basic
395// block to the 'To' basic block is currently feasible...
396//
397bool SCCP::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
398 assert(BBExecutable.count(To) && "Dest should always be alive!");
399
400 // Make sure the source basic block is executable!!
401 if (!BBExecutable.count(From)) return false;
402
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000403 // Check to make sure this edge itself is actually feasible now...
Chris Lattner71ac22ff2003-10-08 15:47:41 +0000404 TerminatorInst *TI = From->getTerminator();
405 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
406 if (BI->isUnconditional())
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000407 return true;
Chris Lattner71ac22ff2003-10-08 15:47:41 +0000408 else {
409 InstVal &BCValue = getValueState(BI->getCondition());
410 if (BCValue.isOverdefined()) {
411 // Overdefined condition variables mean the branch could go either way.
412 return true;
413 } else if (BCValue.isConstant()) {
414 // Constant condition variables mean the branch can only go a single way
415 return BI->getSuccessor(BCValue.getConstant() ==
416 ConstantBool::False) == To;
417 }
418 return false;
419 }
420 } else if (InvokeInst *II = dyn_cast<InvokeInst>(TI)) {
421 // Invoke instructions successors are always executable.
422 return true;
423 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
424 InstVal &SCValue = getValueState(SI->getCondition());
425 if (SCValue.isOverdefined()) { // Overdefined condition?
426 // All destinations are executable!
427 return true;
428 } else if (SCValue.isConstant()) {
429 Constant *CPV = SCValue.getConstant();
430 // Make sure to skip the "default value" which isn't a value
431 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i)
432 if (SI->getSuccessorValue(i) == CPV) // Found the taken branch...
433 return SI->getSuccessor(i) == To;
434
435 // Constant value not equal to any of the branches... must execute
436 // default branch then...
437 return SI->getDefaultDest() == To;
438 }
439 return false;
440 } else {
441 std::cerr << "Unknown terminator instruction: " << *TI;
442 abort();
443 }
Chris Lattner13b52e72002-05-02 21:18:01 +0000444}
Chris Lattner347389d2001-06-27 23:38:11 +0000445
Chris Lattner6e560792002-04-18 15:13:15 +0000446// visit Implementations - Something changed in this instruction... Either an
Chris Lattner347389d2001-06-27 23:38:11 +0000447// operand made a transition, or the instruction is newly executable. Change
448// the value type of I to reflect these changes if appropriate. This method
449// makes sure to do the following actions:
450//
451// 1. If a phi node merges two constants in, and has conflicting value coming
452// from different branches, or if the PHI node merges in an overdefined
453// value, then the PHI node becomes overdefined.
454// 2. If a phi node merges only constants in, and they all agree on value, the
455// PHI node becomes a constant value equal to that.
456// 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
457// 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
458// 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
459// 6. If a conditional branch has a value that is constant, make the selected
460// destination executable
461// 7. If a conditional branch has a value that is overdefined, make all
462// successors executable.
463//
Chris Lattner113f4f42002-06-25 16:13:24 +0000464void SCCP::visitPHINode(PHINode &PN) {
Chris Lattner7324f7c2003-10-08 16:21:03 +0000465 InstVal &PNIV = getValueState(&PN);
466 if (PNIV.isOverdefined()) return; // Quick exit
Chris Lattner347389d2001-06-27 23:38:11 +0000467
Chris Lattner6e560792002-04-18 15:13:15 +0000468 // Look at all of the executable operands of the PHI node. If any of them
469 // are overdefined, the PHI becomes overdefined as well. If they are all
470 // constant, and they agree with each other, the PHI becomes the identical
471 // constant. If they are constant and don't agree, the PHI is overdefined.
472 // If there are no executable operands, the PHI remains undefined.
473 //
Chris Lattnercccc5c72003-04-25 02:50:03 +0000474 Constant *OperandVal = 0;
475 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
476 InstVal &IV = getValueState(PN.getIncomingValue(i));
477 if (IV.isUndefined()) continue; // Doesn't influence PHI node.
Chris Lattnercccc5c72003-04-25 02:50:03 +0000478
Chris Lattner113f4f42002-06-25 16:13:24 +0000479 if (isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent())) {
Chris Lattner7e270582003-06-24 20:29:52 +0000480 if (IV.isOverdefined()) { // PHI node becomes overdefined!
Chris Lattner7324f7c2003-10-08 16:21:03 +0000481 markOverdefined(PNIV, &PN);
Chris Lattner7e270582003-06-24 20:29:52 +0000482 return;
483 }
484
Chris Lattnercccc5c72003-04-25 02:50:03 +0000485 if (OperandVal == 0) { // Grab the first value...
486 OperandVal = IV.getConstant();
Chris Lattner6e560792002-04-18 15:13:15 +0000487 } else { // Another value is being merged in!
488 // There is already a reachable operand. If we conflict with it,
489 // then the PHI node becomes overdefined. If we agree with it, we
490 // can continue on.
Chris Lattnercccc5c72003-04-25 02:50:03 +0000491
Chris Lattner6e560792002-04-18 15:13:15 +0000492 // Check to see if there are two different constants merging...
Chris Lattnercccc5c72003-04-25 02:50:03 +0000493 if (IV.getConstant() != OperandVal) {
Chris Lattner6e560792002-04-18 15:13:15 +0000494 // Yes there is. This means the PHI node is not constant.
495 // You must be overdefined poor PHI.
496 //
Chris Lattner7324f7c2003-10-08 16:21:03 +0000497 markOverdefined(PNIV, &PN); // The PHI node now becomes overdefined
Chris Lattner6e560792002-04-18 15:13:15 +0000498 return; // I'm done analyzing you
Chris Lattnerc4ad64c2001-11-26 18:57:38 +0000499 }
Chris Lattner347389d2001-06-27 23:38:11 +0000500 }
501 }
Chris Lattner347389d2001-06-27 23:38:11 +0000502 }
503
Chris Lattner6e560792002-04-18 15:13:15 +0000504 // If we exited the loop, this means that the PHI node only has constant
Chris Lattnercccc5c72003-04-25 02:50:03 +0000505 // arguments that agree with each other(and OperandVal is the constant) or
506 // OperandVal is null because there are no defined incoming arguments. If
507 // this is the case, the PHI remains undefined.
Chris Lattner347389d2001-06-27 23:38:11 +0000508 //
Chris Lattnercccc5c72003-04-25 02:50:03 +0000509 if (OperandVal)
Misha Brukman8b2bd4e2003-10-10 17:57:28 +0000510 markConstant(PNIV, &PN, OperandVal); // Acquire operand value
Chris Lattner347389d2001-06-27 23:38:11 +0000511}
512
Chris Lattner113f4f42002-06-25 16:13:24 +0000513void SCCP::visitTerminatorInst(TerminatorInst &TI) {
Chris Lattnercccc5c72003-04-25 02:50:03 +0000514 std::vector<bool> SuccFeasible;
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000515 getFeasibleSuccessors(TI, SuccFeasible);
Chris Lattner347389d2001-06-27 23:38:11 +0000516
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000517 BasicBlock *BB = TI.getParent();
518
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000519 // Mark all feasible successors executable...
520 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000521 if (SuccFeasible[i])
522 markEdgeExecutable(BB, TI.getSuccessor(i));
Chris Lattner6e560792002-04-18 15:13:15 +0000523}
524
Chris Lattner6e1a1b12002-08-14 17:53:45 +0000525void SCCP::visitCastInst(CastInst &I) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000526 Value *V = I.getOperand(0);
Chris Lattner6e560792002-04-18 15:13:15 +0000527 InstVal &VState = getValueState(V);
528 if (VState.isOverdefined()) { // Inherit overdefinedness of operand
Chris Lattner113f4f42002-06-25 16:13:24 +0000529 markOverdefined(&I);
Misha Brukman632df282002-10-29 23:06:16 +0000530 } else if (VState.isConstant()) { // Propagate constant value
Chris Lattner6e1a1b12002-08-14 17:53:45 +0000531 Constant *Result =
532 ConstantFoldCastInstruction(VState.getConstant(), I.getType());
Chris Lattner6e560792002-04-18 15:13:15 +0000533
Chris Lattner7324f7c2003-10-08 16:21:03 +0000534 if (Result) // If this instruction constant folds!
Chris Lattner113f4f42002-06-25 16:13:24 +0000535 markConstant(&I, Result);
Chris Lattner7324f7c2003-10-08 16:21:03 +0000536 else
Chris Lattner113f4f42002-06-25 16:13:24 +0000537 markOverdefined(&I); // Don't know how to fold this instruction. :(
Chris Lattner6e560792002-04-18 15:13:15 +0000538 }
539}
540
541// Handle BinaryOperators and Shift Instructions...
Chris Lattner113f4f42002-06-25 16:13:24 +0000542void SCCP::visitBinaryOperator(Instruction &I) {
543 InstVal &V1State = getValueState(I.getOperand(0));
544 InstVal &V2State = getValueState(I.getOperand(1));
Chris Lattner6e560792002-04-18 15:13:15 +0000545 if (V1State.isOverdefined() || V2State.isOverdefined()) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000546 markOverdefined(&I);
Chris Lattner6e560792002-04-18 15:13:15 +0000547 } else if (V1State.isConstant() && V2State.isConstant()) {
Chris Lattner940daed2002-05-06 03:01:37 +0000548 Constant *Result = 0;
549 if (isa<BinaryOperator>(I))
Chris Lattner113f4f42002-06-25 16:13:24 +0000550 Result = ConstantFoldBinaryInstruction(I.getOpcode(),
Chris Lattner940daed2002-05-06 03:01:37 +0000551 V1State.getConstant(),
552 V2State.getConstant());
553 else if (isa<ShiftInst>(I))
Chris Lattner113f4f42002-06-25 16:13:24 +0000554 Result = ConstantFoldShiftInstruction(I.getOpcode(),
Chris Lattner940daed2002-05-06 03:01:37 +0000555 V1State.getConstant(),
556 V2State.getConstant());
Chris Lattner6e560792002-04-18 15:13:15 +0000557 if (Result)
Chris Lattner113f4f42002-06-25 16:13:24 +0000558 markConstant(&I, Result); // This instruction constant folds!
Chris Lattner6e560792002-04-18 15:13:15 +0000559 else
Chris Lattner113f4f42002-06-25 16:13:24 +0000560 markOverdefined(&I); // Don't know how to fold this instruction. :(
Chris Lattner6e560792002-04-18 15:13:15 +0000561 }
562}
Chris Lattnerdd6522e2002-08-30 23:39:00 +0000563
564// Handle getelementptr instructions... if all operands are constants then we
565// can turn this into a getelementptr ConstantExpr.
566//
567void SCCP::visitGetElementPtrInst(GetElementPtrInst &I) {
568 std::vector<Constant*> Operands;
569 Operands.reserve(I.getNumOperands());
570
571 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
572 InstVal &State = getValueState(I.getOperand(i));
573 if (State.isUndefined())
574 return; // Operands are not resolved yet...
575 else if (State.isOverdefined()) {
576 markOverdefined(&I);
577 return;
578 }
579 assert(State.isConstant() && "Unknown state!");
580 Operands.push_back(State.getConstant());
581 }
582
583 Constant *Ptr = Operands[0];
584 Operands.erase(Operands.begin()); // Erase the pointer from idx list...
585
586 markConstant(&I, ConstantExpr::getGetElementPtr(Ptr, Operands));
587}