blob: df55037c520b30632b2f0b80e74a52724db0bf81 [file] [log] [blame]
Misha Brukman82c89b92003-05-20 21:01:22 +00001//===- SCCP.cpp - Sparse Conditional Constant Propagation -----------------===//
Chris Lattner138a1242001-06-27 23:38:11 +00002//
Misha Brukman82c89b92003-05-20 21:01:22 +00003// This file implements sparse conditional constant propagation and merging:
Chris Lattner138a1242001-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 Lattner2a88bb72002-08-30 23:39:00 +00009// * Proves conditional branches to be unconditional
Chris Lattner138a1242001-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 Lattner022103b2002-05-07 20:03:00 +000017#include "llvm/Transforms/Scalar.h"
Chris Lattner968ddc92002-04-08 20:18:09 +000018#include "llvm/ConstantHandling.h"
Chris Lattner79df7c02002-03-26 18:01:55 +000019#include "llvm/Function.h"
Chris Lattner9de28282003-04-25 02:50:03 +000020#include "llvm/Instructions.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000021#include "llvm/Pass.h"
Chris Lattner2a632552002-04-18 15:13:15 +000022#include "llvm/Support/InstVisitor.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000023#include "Support/STLExtras.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000024#include "Support/Statistic.h"
Chris Lattner138a1242001-06-27 23:38:11 +000025#include <algorithm>
Chris Lattner138a1242001-06-27 23:38:11 +000026#include <set>
27
Chris Lattner138a1242001-06-27 23:38:11 +000028// InstVal class - This class represents the different lattice values that an
Chris Lattnerf57b8452002-04-27 06:56:12 +000029// instruction may occupy. It is a simple class with value semantics.
Chris Lattner138a1242001-06-27 23:38:11 +000030//
Chris Lattner0dbfc052002-04-29 21:26:08 +000031namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000032 Statistic<> NumInstRemoved("sccp", "Number of instructions removed");
33
Chris Lattner138a1242001-06-27 23:38:11 +000034class InstVal {
35 enum {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000036 undefined, // This instruction has no known value
37 constant, // This instruction has a constant value
Chris Lattnere9bb2df2001-12-03 22:26:30 +000038 overdefined // This instruction has an unknown value
39 } LatticeValue; // The current lattice position
40 Constant *ConstantVal; // If Constant value, the current value
Chris Lattner138a1242001-06-27 23:38:11 +000041public:
Chris Lattnere9bb2df2001-12-03 22:26:30 +000042 inline InstVal() : LatticeValue(undefined), ConstantVal(0) {}
Chris Lattner138a1242001-06-27 23:38:11 +000043
44 // markOverdefined - Return true if this is a new status to be in...
45 inline bool markOverdefined() {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000046 if (LatticeValue != overdefined) {
47 LatticeValue = overdefined;
Chris Lattner138a1242001-06-27 23:38:11 +000048 return true;
49 }
50 return false;
51 }
52
53 // markConstant - Return true if this is a new status for us...
Chris Lattnere9bb2df2001-12-03 22:26:30 +000054 inline bool markConstant(Constant *V) {
55 if (LatticeValue != constant) {
56 LatticeValue = constant;
Chris Lattner138a1242001-06-27 23:38:11 +000057 ConstantVal = V;
58 return true;
59 } else {
Chris Lattnerb70d82f2001-09-07 16:43:22 +000060 assert(ConstantVal == V && "Marking constant with different value");
Chris Lattner138a1242001-06-27 23:38:11 +000061 }
62 return false;
63 }
64
Chris Lattnere9bb2df2001-12-03 22:26:30 +000065 inline bool isUndefined() const { return LatticeValue == undefined; }
66 inline bool isConstant() const { return LatticeValue == constant; }
67 inline bool isOverdefined() const { return LatticeValue == overdefined; }
Chris Lattner138a1242001-06-27 23:38:11 +000068
Chris Lattnere9bb2df2001-12-03 22:26:30 +000069 inline Constant *getConstant() const { return ConstantVal; }
Chris Lattner138a1242001-06-27 23:38:11 +000070};
71
Chris Lattner0dbfc052002-04-29 21:26:08 +000072} // end anonymous namespace
Chris Lattner138a1242001-06-27 23:38:11 +000073
74
75//===----------------------------------------------------------------------===//
76// SCCP Class
77//
Misha Brukman82c89b92003-05-20 21:01:22 +000078// This class does all of the work of Sparse Conditional Constant Propagation.
Chris Lattner138a1242001-06-27 23:38:11 +000079//
Chris Lattner0dbfc052002-04-29 21:26:08 +000080namespace {
81class SCCP : public FunctionPass, public InstVisitor<SCCP> {
Chris Lattner697954c2002-01-20 22:54:45 +000082 std::set<BasicBlock*> BBExecutable;// The basic blocks that are executable
83 std::map<Value*, InstVal> ValueState; // The state each value is in...
Chris Lattner138a1242001-06-27 23:38:11 +000084
Chris Lattner071d0ad2002-05-07 04:29:32 +000085 std::vector<Instruction*> InstWorkList;// The instruction work list
Chris Lattner697954c2002-01-20 22:54:45 +000086 std::vector<BasicBlock*> BBWorkList; // The BasicBlock work list
Chris Lattner138a1242001-06-27 23:38:11 +000087public:
88
Misha Brukman82c89b92003-05-20 21:01:22 +000089 // runOnFunction - Run the Sparse Conditional Constant Propagation algorithm,
Chris Lattner0dbfc052002-04-29 21:26:08 +000090 // and return true if the function was modified.
91 //
Chris Lattner7e708292002-06-25 16:13:24 +000092 bool runOnFunction(Function &F);
Chris Lattner0dbfc052002-04-29 21:26:08 +000093
94 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000095 AU.setPreservesCFG();
Chris Lattner0dbfc052002-04-29 21:26:08 +000096 }
97
Chris Lattner138a1242001-06-27 23:38:11 +000098
99 //===--------------------------------------------------------------------===//
100 // The implementation of this class
101 //
102private:
Chris Lattner2a632552002-04-18 15:13:15 +0000103 friend class InstVisitor<SCCP>; // Allow callbacks from visitor
Chris Lattner138a1242001-06-27 23:38:11 +0000104
105 // markValueOverdefined - Make a value be marked as "constant". If the value
106 // is not already a constant, add it to the instruction work list so that
107 // the users of the instruction are updated later.
108 //
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000109 inline bool markConstant(Instruction *I, Constant *V) {
Chris Lattner138a1242001-06-27 23:38:11 +0000110 if (ValueState[I].markConstant(V)) {
Chris Lattner9de28282003-04-25 02:50:03 +0000111 DEBUG(std::cerr << "markConstant: " << V << " = " << I);
Chris Lattner071d0ad2002-05-07 04:29:32 +0000112 InstWorkList.push_back(I);
Chris Lattner138a1242001-06-27 23:38:11 +0000113 return true;
114 }
115 return false;
116 }
117
118 // markValueOverdefined - Make a value be marked as "overdefined". If the
119 // value is not already overdefined, add it to the instruction work list so
120 // that the users of the instruction are updated later.
121 //
122 inline bool markOverdefined(Value *V) {
123 if (ValueState[V].markOverdefined()) {
Chris Lattner9636a912001-10-01 16:18:37 +0000124 if (Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner9de28282003-04-25 02:50:03 +0000125 DEBUG(std::cerr << "markOverdefined: " << V);
Chris Lattner071d0ad2002-05-07 04:29:32 +0000126 InstWorkList.push_back(I); // Only instructions go on the work list
Chris Lattner138a1242001-06-27 23:38:11 +0000127 }
128 return true;
129 }
130 return false;
131 }
132
133 // getValueState - Return the InstVal object that corresponds to the value.
134 // This function is neccesary because not all values should start out in the
Chris Lattner73e21422002-04-09 19:48:49 +0000135 // underdefined state... Argument's should be overdefined, and
Chris Lattner79df7c02002-03-26 18:01:55 +0000136 // constants should be marked as constants. If a value is not known to be an
Chris Lattner138a1242001-06-27 23:38:11 +0000137 // Instruction object, then use this accessor to get its value from the map.
138 //
139 inline InstVal &getValueState(Value *V) {
Chris Lattner697954c2002-01-20 22:54:45 +0000140 std::map<Value*, InstVal>::iterator I = ValueState.find(V);
Chris Lattner138a1242001-06-27 23:38:11 +0000141 if (I != ValueState.end()) return I->second; // Common case, in the map
142
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000143 if (Constant *CPV = dyn_cast<Constant>(V)) { // Constants are constant
Chris Lattner138a1242001-06-27 23:38:11 +0000144 ValueState[CPV].markConstant(CPV);
Chris Lattner73e21422002-04-09 19:48:49 +0000145 } else if (isa<Argument>(V)) { // Arguments are overdefined
Chris Lattner138a1242001-06-27 23:38:11 +0000146 ValueState[V].markOverdefined();
Chris Lattner2a88bb72002-08-30 23:39:00 +0000147 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
148 // The address of a global is a constant...
149 ValueState[V].markConstant(ConstantPointerRef::get(GV));
150 }
Chris Lattner138a1242001-06-27 23:38:11 +0000151 // All others are underdefined by default...
152 return ValueState[V];
153 }
154
155 // markExecutable - Mark a basic block as executable, adding it to the BB
156 // work list if it is not already executable...
157 //
158 void markExecutable(BasicBlock *BB) {
Chris Lattner9de28282003-04-25 02:50:03 +0000159 if (BBExecutable.count(BB)) {
160 // BB is already executable, but we may have just made an edge feasible
161 // that wasn't before. Add the PHI nodes to the work list so that they
162 // can be rechecked.
163 for (BasicBlock::iterator I = BB->begin();
164 PHINode *PN = dyn_cast<PHINode>(I); ++I)
Chris Lattnerbceb2b02003-04-25 03:35:10 +0000165 visitPHINode(*PN);
Chris Lattner9de28282003-04-25 02:50:03 +0000166
167 } else {
168 DEBUG(std::cerr << "Marking BB Executable: " << *BB);
169 BBExecutable.insert(BB); // Basic block is executable!
170 BBWorkList.push_back(BB); // Add the block to the work list!
171 }
Chris Lattner138a1242001-06-27 23:38:11 +0000172 }
173
Chris Lattner138a1242001-06-27 23:38:11 +0000174
Chris Lattner2a632552002-04-18 15:13:15 +0000175 // visit implementations - Something changed in this instruction... Either an
Chris Lattnercb056de2001-06-29 23:56:23 +0000176 // operand made a transition, or the instruction is newly executable. Change
177 // the value type of I to reflect these changes if appropriate.
178 //
Chris Lattner7e708292002-06-25 16:13:24 +0000179 void visitPHINode(PHINode &I);
Chris Lattner2a632552002-04-18 15:13:15 +0000180
181 // Terminators
Chris Lattner7e708292002-06-25 16:13:24 +0000182 void visitReturnInst(ReturnInst &I) { /*does not have an effect*/ }
183 void visitTerminatorInst(TerminatorInst &TI);
Chris Lattner2a632552002-04-18 15:13:15 +0000184
Chris Lattnerb8047602002-08-14 17:53:45 +0000185 void visitCastInst(CastInst &I);
Chris Lattner7e708292002-06-25 16:13:24 +0000186 void visitBinaryOperator(Instruction &I);
187 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
Chris Lattner2a632552002-04-18 15:13:15 +0000188
189 // Instructions that cannot be folded away...
Chris Lattner7e708292002-06-25 16:13:24 +0000190 void visitStoreInst (Instruction &I) { /*returns void*/ }
Chris Lattnercc63f1c2002-08-22 23:37:20 +0000191 void visitLoadInst (Instruction &I) { markOverdefined(&I); }
Chris Lattner2a88bb72002-08-30 23:39:00 +0000192 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattner7e708292002-06-25 16:13:24 +0000193 void visitCallInst (Instruction &I) { markOverdefined(&I); }
194 void visitInvokeInst (Instruction &I) { markOverdefined(&I); }
195 void visitAllocationInst(Instruction &I) { markOverdefined(&I); }
Chris Lattner1d16ec72003-05-08 02:50:13 +0000196 void visitVarArgInst (Instruction &I) { markOverdefined(&I); }
Chris Lattner7e708292002-06-25 16:13:24 +0000197 void visitFreeInst (Instruction &I) { /*returns void*/ }
Chris Lattner2a632552002-04-18 15:13:15 +0000198
Chris Lattner7e708292002-06-25 16:13:24 +0000199 void visitInstruction(Instruction &I) {
Chris Lattner2a632552002-04-18 15:13:15 +0000200 // If a new instruction is added to LLVM that we don't handle...
Chris Lattner9de28282003-04-25 02:50:03 +0000201 std::cerr << "SCCP: Don't know how to handle: " << I;
Chris Lattner7e708292002-06-25 16:13:24 +0000202 markOverdefined(&I); // Just in case
Chris Lattner2a632552002-04-18 15:13:15 +0000203 }
Chris Lattnercb056de2001-06-29 23:56:23 +0000204
Chris Lattnerb9a66342002-05-02 21:44:00 +0000205 // getFeasibleSuccessors - Return a vector of booleans to indicate which
206 // successors are reachable from a given terminator instruction.
207 //
Chris Lattner7e708292002-06-25 16:13:24 +0000208 void getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs);
Chris Lattnerb9a66342002-05-02 21:44:00 +0000209
Chris Lattner59f0ce22002-05-02 21:18:01 +0000210 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
211 // block to the 'To' basic block is currently feasible...
212 //
213 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To);
214
Chris Lattnercb056de2001-06-29 23:56:23 +0000215 // OperandChangedState - This method is invoked on all of the users of an
216 // instruction that was just changed state somehow.... Based on this
217 // information, we need to update the specified user of this instruction.
218 //
Chris Lattner59f0ce22002-05-02 21:18:01 +0000219 void OperandChangedState(User *U) {
220 // Only instructions use other variable values!
Chris Lattner7e708292002-06-25 16:13:24 +0000221 Instruction &I = cast<Instruction>(*U);
Chris Lattner9de28282003-04-25 02:50:03 +0000222 if (BBExecutable.count(I.getParent())) // Inst is executable?
223 visit(I);
Chris Lattner59f0ce22002-05-02 21:18:01 +0000224 }
Chris Lattnercb056de2001-06-29 23:56:23 +0000225};
Chris Lattnerf6293092002-07-23 18:06:35 +0000226
Chris Lattner9de28282003-04-25 02:50:03 +0000227 RegisterOpt<SCCP> X("sccp", "Sparse Conditional Constant Propagation");
Chris Lattner0dbfc052002-04-29 21:26:08 +0000228} // end anonymous namespace
229
230
231// createSCCPPass - This is the public interface to this file...
232//
233Pass *createSCCPPass() {
234 return new SCCP();
235}
236
Chris Lattner138a1242001-06-27 23:38:11 +0000237
Chris Lattner138a1242001-06-27 23:38:11 +0000238//===----------------------------------------------------------------------===//
239// SCCP Class Implementation
240
241
Misha Brukman82c89b92003-05-20 21:01:22 +0000242// runOnFunction() - Run the Sparse Conditional Constant Propagation algorithm,
Chris Lattner0dbfc052002-04-29 21:26:08 +0000243// and return true if the function was modified.
Chris Lattner138a1242001-06-27 23:38:11 +0000244//
Chris Lattner7e708292002-06-25 16:13:24 +0000245bool SCCP::runOnFunction(Function &F) {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000246 // Mark the first block of the function as being executable...
Chris Lattner7e708292002-06-25 16:13:24 +0000247 markExecutable(&F.front());
Chris Lattner138a1242001-06-27 23:38:11 +0000248
249 // Process the work lists until their are empty!
250 while (!BBWorkList.empty() || !InstWorkList.empty()) {
251 // Process the instruction work list...
252 while (!InstWorkList.empty()) {
Chris Lattner071d0ad2002-05-07 04:29:32 +0000253 Instruction *I = InstWorkList.back();
254 InstWorkList.pop_back();
Chris Lattner138a1242001-06-27 23:38:11 +0000255
Chris Lattner9de28282003-04-25 02:50:03 +0000256 DEBUG(std::cerr << "\nPopped off I-WL: " << I);
Chris Lattner138a1242001-06-27 23:38:11 +0000257
258 // "I" got into the work list because it either made the transition from
259 // bottom to constant, or to Overdefined.
260 //
261 // Update all of the users of this instruction's value...
262 //
263 for_each(I->use_begin(), I->use_end(),
264 bind_obj(this, &SCCP::OperandChangedState));
265 }
266
267 // Process the basic block work list...
268 while (!BBWorkList.empty()) {
269 BasicBlock *BB = BBWorkList.back();
270 BBWorkList.pop_back();
271
Chris Lattner9de28282003-04-25 02:50:03 +0000272 DEBUG(std::cerr << "\nPopped off BBWL: " << BB);
Chris Lattner138a1242001-06-27 23:38:11 +0000273
Chris Lattner2a632552002-04-18 15:13:15 +0000274 // Notify all instructions in this basic block that they are newly
275 // executable.
276 visit(BB);
Chris Lattner138a1242001-06-27 23:38:11 +0000277 }
278 }
279
Chris Lattnerf016ea42002-05-22 17:17:27 +0000280 if (DebugFlag) {
Chris Lattner7e708292002-06-25 16:13:24 +0000281 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
282 if (!BBExecutable.count(I))
Chris Lattner9de28282003-04-25 02:50:03 +0000283 std::cerr << "BasicBlock Dead:" << *I;
Chris Lattnerf016ea42002-05-22 17:17:27 +0000284 }
Chris Lattner138a1242001-06-27 23:38:11 +0000285
Chris Lattnerf57b8452002-04-27 06:56:12 +0000286 // Iterate over all of the instructions in a function, replacing them with
Chris Lattner138a1242001-06-27 23:38:11 +0000287 // constants if we have found them to be of constant values.
288 //
289 bool MadeChanges = false;
Chris Lattner7e708292002-06-25 16:13:24 +0000290 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB)
Chris Lattner221d6882002-02-12 21:07:25 +0000291 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
Chris Lattner7e708292002-06-25 16:13:24 +0000292 Instruction &Inst = *BI;
293 InstVal &IV = ValueState[&Inst];
Chris Lattner221d6882002-02-12 21:07:25 +0000294 if (IV.isConstant()) {
295 Constant *Const = IV.getConstant();
Chris Lattner9de28282003-04-25 02:50:03 +0000296 DEBUG(std::cerr << "Constant: " << Const << " = " << Inst);
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000297
Chris Lattner221d6882002-02-12 21:07:25 +0000298 // Replaces all of the uses of a variable with uses of the constant.
Chris Lattner7e708292002-06-25 16:13:24 +0000299 Inst.replaceAllUsesWith(Const);
Chris Lattner138a1242001-06-27 23:38:11 +0000300
Chris Lattner0e9c5152002-05-02 20:32:51 +0000301 // Remove the operator from the list of definitions... and delete it.
Chris Lattner7e708292002-06-25 16:13:24 +0000302 BI = BB->getInstList().erase(BI);
Chris Lattner138a1242001-06-27 23:38:11 +0000303
Chris Lattner221d6882002-02-12 21:07:25 +0000304 // Hey, we just changed something!
305 MadeChanges = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000306 ++NumInstRemoved;
Chris Lattnerb9a66342002-05-02 21:44:00 +0000307 } else {
308 ++BI;
Chris Lattner221d6882002-02-12 21:07:25 +0000309 }
Chris Lattner138a1242001-06-27 23:38:11 +0000310 }
Chris Lattner138a1242001-06-27 23:38:11 +0000311
Chris Lattner59f0ce22002-05-02 21:18:01 +0000312 // Reset state so that the next invocation will have empty data structures
Chris Lattner0dbfc052002-04-29 21:26:08 +0000313 BBExecutable.clear();
314 ValueState.clear();
Chris Lattneraf663462002-11-04 02:54:22 +0000315 std::vector<Instruction*>().swap(InstWorkList);
316 std::vector<BasicBlock*>().swap(BBWorkList);
Chris Lattner0dbfc052002-04-29 21:26:08 +0000317
Chris Lattnerb70d82f2001-09-07 16:43:22 +0000318 return MadeChanges;
Chris Lattner138a1242001-06-27 23:38:11 +0000319}
320
Chris Lattnerb9a66342002-05-02 21:44:00 +0000321
322// getFeasibleSuccessors - Return a vector of booleans to indicate which
323// successors are reachable from a given terminator instruction.
324//
Chris Lattner7e708292002-06-25 16:13:24 +0000325void SCCP::getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs) {
Chris Lattner9de28282003-04-25 02:50:03 +0000326 Succs.resize(TI.getNumSuccessors());
Chris Lattner7e708292002-06-25 16:13:24 +0000327 if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
Chris Lattnerb9a66342002-05-02 21:44:00 +0000328 if (BI->isUnconditional()) {
329 Succs[0] = true;
330 } else {
331 InstVal &BCValue = getValueState(BI->getCondition());
332 if (BCValue.isOverdefined()) {
333 // Overdefined condition variables mean the branch could go either way.
334 Succs[0] = Succs[1] = true;
335 } else if (BCValue.isConstant()) {
336 // Constant condition variables mean the branch can only go a single way
337 Succs[BCValue.getConstant() == ConstantBool::False] = true;
338 }
339 }
Chris Lattner7e708292002-06-25 16:13:24 +0000340 } else if (InvokeInst *II = dyn_cast<InvokeInst>(&TI)) {
Chris Lattnerb9a66342002-05-02 21:44:00 +0000341 // Invoke instructions successors are always executable.
342 Succs[0] = Succs[1] = true;
Chris Lattner7e708292002-06-25 16:13:24 +0000343 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) {
Chris Lattnerb9a66342002-05-02 21:44:00 +0000344 InstVal &SCValue = getValueState(SI->getCondition());
345 if (SCValue.isOverdefined()) { // Overdefined condition?
346 // All destinations are executable!
Chris Lattner7e708292002-06-25 16:13:24 +0000347 Succs.assign(TI.getNumSuccessors(), true);
Chris Lattnerb9a66342002-05-02 21:44:00 +0000348 } else if (SCValue.isConstant()) {
349 Constant *CPV = SCValue.getConstant();
350 // Make sure to skip the "default value" which isn't a value
351 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) {
352 if (SI->getSuccessorValue(i) == CPV) {// Found the right branch...
353 Succs[i] = true;
354 return;
355 }
356 }
357
358 // Constant value not equal to any of the branches... must execute
359 // default branch then...
360 Succs[0] = true;
361 }
362 } else {
Chris Lattner9de28282003-04-25 02:50:03 +0000363 std::cerr << "SCCP: Don't know how to handle: " << TI;
Chris Lattner7e708292002-06-25 16:13:24 +0000364 Succs.assign(TI.getNumSuccessors(), true);
Chris Lattnerb9a66342002-05-02 21:44:00 +0000365 }
366}
367
368
Chris Lattner59f0ce22002-05-02 21:18:01 +0000369// isEdgeFeasible - Return true if the control flow edge from the 'From' basic
370// block to the 'To' basic block is currently feasible...
371//
372bool SCCP::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
373 assert(BBExecutable.count(To) && "Dest should always be alive!");
374
375 // Make sure the source basic block is executable!!
376 if (!BBExecutable.count(From)) return false;
377
Chris Lattnerb9a66342002-05-02 21:44:00 +0000378 // Check to make sure this edge itself is actually feasible now...
379 TerminatorInst *FT = From->getTerminator();
Chris Lattner9de28282003-04-25 02:50:03 +0000380 std::vector<bool> SuccFeasible;
Chris Lattner7e708292002-06-25 16:13:24 +0000381 getFeasibleSuccessors(*FT, SuccFeasible);
Chris Lattnerb9a66342002-05-02 21:44:00 +0000382
383 // Check all edges from From to To. If any are feasible, return true.
384 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
385 if (FT->getSuccessor(i) == To && SuccFeasible[i])
386 return true;
387
388 // Otherwise, none of the edges are actually feasible at this time...
389 return false;
Chris Lattner59f0ce22002-05-02 21:18:01 +0000390}
Chris Lattner138a1242001-06-27 23:38:11 +0000391
Chris Lattner2a632552002-04-18 15:13:15 +0000392// visit Implementations - Something changed in this instruction... Either an
Chris Lattner138a1242001-06-27 23:38:11 +0000393// operand made a transition, or the instruction is newly executable. Change
394// the value type of I to reflect these changes if appropriate. This method
395// makes sure to do the following actions:
396//
397// 1. If a phi node merges two constants in, and has conflicting value coming
398// from different branches, or if the PHI node merges in an overdefined
399// value, then the PHI node becomes overdefined.
400// 2. If a phi node merges only constants in, and they all agree on value, the
401// PHI node becomes a constant value equal to that.
402// 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
403// 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
404// 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
405// 6. If a conditional branch has a value that is constant, make the selected
406// destination executable
407// 7. If a conditional branch has a value that is overdefined, make all
408// successors executable.
409//
Chris Lattner7e708292002-06-25 16:13:24 +0000410void SCCP::visitPHINode(PHINode &PN) {
Chris Lattner9de28282003-04-25 02:50:03 +0000411 if (getValueState(&PN).isOverdefined()) return; // Quick exit
Chris Lattner138a1242001-06-27 23:38:11 +0000412
Chris Lattner2a632552002-04-18 15:13:15 +0000413 // Look at all of the executable operands of the PHI node. If any of them
414 // are overdefined, the PHI becomes overdefined as well. If they are all
415 // constant, and they agree with each other, the PHI becomes the identical
416 // constant. If they are constant and don't agree, the PHI is overdefined.
417 // If there are no executable operands, the PHI remains undefined.
418 //
Chris Lattner9de28282003-04-25 02:50:03 +0000419 Constant *OperandVal = 0;
420 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
421 InstVal &IV = getValueState(PN.getIncomingValue(i));
422 if (IV.isUndefined()) continue; // Doesn't influence PHI node.
Chris Lattner9de28282003-04-25 02:50:03 +0000423
Chris Lattner7e708292002-06-25 16:13:24 +0000424 if (isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent())) {
Chris Lattner38b5ae42003-06-24 20:29:52 +0000425 if (IV.isOverdefined()) { // PHI node becomes overdefined!
426 markOverdefined(&PN);
427 return;
428 }
429
Chris Lattner9de28282003-04-25 02:50:03 +0000430 if (OperandVal == 0) { // Grab the first value...
431 OperandVal = IV.getConstant();
Chris Lattner2a632552002-04-18 15:13:15 +0000432 } else { // Another value is being merged in!
433 // There is already a reachable operand. If we conflict with it,
434 // then the PHI node becomes overdefined. If we agree with it, we
435 // can continue on.
Chris Lattner9de28282003-04-25 02:50:03 +0000436
Chris Lattner2a632552002-04-18 15:13:15 +0000437 // Check to see if there are two different constants merging...
Chris Lattner9de28282003-04-25 02:50:03 +0000438 if (IV.getConstant() != OperandVal) {
Chris Lattner2a632552002-04-18 15:13:15 +0000439 // Yes there is. This means the PHI node is not constant.
440 // You must be overdefined poor PHI.
441 //
Chris Lattner7e708292002-06-25 16:13:24 +0000442 markOverdefined(&PN); // The PHI node now becomes overdefined
Chris Lattner2a632552002-04-18 15:13:15 +0000443 return; // I'm done analyzing you
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000444 }
Chris Lattner138a1242001-06-27 23:38:11 +0000445 }
446 }
Chris Lattner138a1242001-06-27 23:38:11 +0000447 }
448
Chris Lattner2a632552002-04-18 15:13:15 +0000449 // If we exited the loop, this means that the PHI node only has constant
Chris Lattner9de28282003-04-25 02:50:03 +0000450 // arguments that agree with each other(and OperandVal is the constant) or
451 // OperandVal is null because there are no defined incoming arguments. If
452 // this is the case, the PHI remains undefined.
Chris Lattner138a1242001-06-27 23:38:11 +0000453 //
Chris Lattner9de28282003-04-25 02:50:03 +0000454 if (OperandVal)
455 markConstant(&PN, OperandVal); // Aquire operand value
Chris Lattner138a1242001-06-27 23:38:11 +0000456}
457
Chris Lattner7e708292002-06-25 16:13:24 +0000458void SCCP::visitTerminatorInst(TerminatorInst &TI) {
Chris Lattner9de28282003-04-25 02:50:03 +0000459 std::vector<bool> SuccFeasible;
Chris Lattnerb9a66342002-05-02 21:44:00 +0000460 getFeasibleSuccessors(TI, SuccFeasible);
Chris Lattner138a1242001-06-27 23:38:11 +0000461
Chris Lattnerb9a66342002-05-02 21:44:00 +0000462 // Mark all feasible successors executable...
463 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
Chris Lattner7f9412b2002-05-22 16:07:20 +0000464 if (SuccFeasible[i]) {
Chris Lattner7e708292002-06-25 16:13:24 +0000465 BasicBlock *Succ = TI.getSuccessor(i);
Chris Lattner7f9412b2002-05-22 16:07:20 +0000466 markExecutable(Succ);
Chris Lattner7f9412b2002-05-22 16:07:20 +0000467 }
Chris Lattner2a632552002-04-18 15:13:15 +0000468}
469
Chris Lattnerb8047602002-08-14 17:53:45 +0000470void SCCP::visitCastInst(CastInst &I) {
Chris Lattner7e708292002-06-25 16:13:24 +0000471 Value *V = I.getOperand(0);
Chris Lattner2a632552002-04-18 15:13:15 +0000472 InstVal &VState = getValueState(V);
473 if (VState.isOverdefined()) { // Inherit overdefinedness of operand
Chris Lattner7e708292002-06-25 16:13:24 +0000474 markOverdefined(&I);
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000475 } else if (VState.isConstant()) { // Propagate constant value
Chris Lattnerb8047602002-08-14 17:53:45 +0000476 Constant *Result =
477 ConstantFoldCastInstruction(VState.getConstant(), I.getType());
Chris Lattner2a632552002-04-18 15:13:15 +0000478
479 if (Result) {
480 // This instruction constant folds!
Chris Lattner7e708292002-06-25 16:13:24 +0000481 markConstant(&I, Result);
Chris Lattner2a632552002-04-18 15:13:15 +0000482 } else {
Chris Lattner7e708292002-06-25 16:13:24 +0000483 markOverdefined(&I); // Don't know how to fold this instruction. :(
Chris Lattner2a632552002-04-18 15:13:15 +0000484 }
485 }
486}
487
488// Handle BinaryOperators and Shift Instructions...
Chris Lattner7e708292002-06-25 16:13:24 +0000489void SCCP::visitBinaryOperator(Instruction &I) {
490 InstVal &V1State = getValueState(I.getOperand(0));
491 InstVal &V2State = getValueState(I.getOperand(1));
Chris Lattner2a632552002-04-18 15:13:15 +0000492 if (V1State.isOverdefined() || V2State.isOverdefined()) {
Chris Lattner7e708292002-06-25 16:13:24 +0000493 markOverdefined(&I);
Chris Lattner2a632552002-04-18 15:13:15 +0000494 } else if (V1State.isConstant() && V2State.isConstant()) {
Chris Lattner4c1061f2002-05-06 03:01:37 +0000495 Constant *Result = 0;
496 if (isa<BinaryOperator>(I))
Chris Lattner7e708292002-06-25 16:13:24 +0000497 Result = ConstantFoldBinaryInstruction(I.getOpcode(),
Chris Lattner4c1061f2002-05-06 03:01:37 +0000498 V1State.getConstant(),
499 V2State.getConstant());
500 else if (isa<ShiftInst>(I))
Chris Lattner7e708292002-06-25 16:13:24 +0000501 Result = ConstantFoldShiftInstruction(I.getOpcode(),
Chris Lattner4c1061f2002-05-06 03:01:37 +0000502 V1State.getConstant(),
503 V2State.getConstant());
Chris Lattner2a632552002-04-18 15:13:15 +0000504 if (Result)
Chris Lattner7e708292002-06-25 16:13:24 +0000505 markConstant(&I, Result); // This instruction constant folds!
Chris Lattner2a632552002-04-18 15:13:15 +0000506 else
Chris Lattner7e708292002-06-25 16:13:24 +0000507 markOverdefined(&I); // Don't know how to fold this instruction. :(
Chris Lattner2a632552002-04-18 15:13:15 +0000508 }
509}
Chris Lattner2a88bb72002-08-30 23:39:00 +0000510
511// Handle getelementptr instructions... if all operands are constants then we
512// can turn this into a getelementptr ConstantExpr.
513//
514void SCCP::visitGetElementPtrInst(GetElementPtrInst &I) {
515 std::vector<Constant*> Operands;
516 Operands.reserve(I.getNumOperands());
517
518 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
519 InstVal &State = getValueState(I.getOperand(i));
520 if (State.isUndefined())
521 return; // Operands are not resolved yet...
522 else if (State.isOverdefined()) {
523 markOverdefined(&I);
524 return;
525 }
526 assert(State.isConstant() && "Unknown state!");
527 Operands.push_back(State.getConstant());
528 }
529
530 Constant *Ptr = Operands[0];
531 Operands.erase(Operands.begin()); // Erase the pointer from idx list...
532
533 markConstant(&I, ConstantExpr::getGetElementPtr(Ptr, Operands));
534}