blob: 66818bc3b63bbbe2c7fbfcc009ac44aba250b170 [file] [log] [blame]
Chris Lattner138a1242001-06-27 23:38:11 +00001//===- SCCP.cpp - Sparse Conditional Constant Propogation -----------------===//
2//
3// This file implements sparse conditional constant propogation and merging:
4//
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 Lattner7061dc52001-12-03 18:02:31 +000020#include "llvm/iPHINode.h"
Chris Lattner3b7bfdb2001-07-14 06:11:51 +000021#include "llvm/iMemory.h"
Chris Lattner138a1242001-06-27 23:38:11 +000022#include "llvm/iTerminators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000023#include "llvm/iOther.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000024#include "llvm/Pass.h"
Chris Lattner2a632552002-04-18 15:13:15 +000025#include "llvm/Support/InstVisitor.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000026#include "Support/STLExtras.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000027#include "Support/Statistic.h"
Chris Lattner138a1242001-06-27 23:38:11 +000028#include <algorithm>
Chris Lattner138a1242001-06-27 23:38:11 +000029#include <set>
Chris Lattner697954c2002-01-20 22:54:45 +000030using std::cerr;
Chris Lattner138a1242001-06-27 23:38:11 +000031
Chris Lattner138a1242001-06-27 23:38:11 +000032// InstVal class - This class represents the different lattice values that an
Chris Lattnerf57b8452002-04-27 06:56:12 +000033// instruction may occupy. It is a simple class with value semantics.
Chris Lattner138a1242001-06-27 23:38:11 +000034//
Chris Lattner0dbfc052002-04-29 21:26:08 +000035namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000036 Statistic<> NumInstRemoved("sccp", "Number of instructions removed");
37
Chris Lattner138a1242001-06-27 23:38:11 +000038class InstVal {
39 enum {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000040 undefined, // This instruction has no known value
41 constant, // This instruction has a constant value
Chris Lattnere9bb2df2001-12-03 22:26:30 +000042 overdefined // This instruction has an unknown value
43 } LatticeValue; // The current lattice position
44 Constant *ConstantVal; // If Constant value, the current value
Chris Lattner138a1242001-06-27 23:38:11 +000045public:
Chris Lattnere9bb2df2001-12-03 22:26:30 +000046 inline InstVal() : LatticeValue(undefined), ConstantVal(0) {}
Chris Lattner138a1242001-06-27 23:38:11 +000047
48 // markOverdefined - Return true if this is a new status to be in...
49 inline bool markOverdefined() {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000050 if (LatticeValue != overdefined) {
51 LatticeValue = overdefined;
Chris Lattner138a1242001-06-27 23:38:11 +000052 return true;
53 }
54 return false;
55 }
56
57 // markConstant - Return true if this is a new status for us...
Chris Lattnere9bb2df2001-12-03 22:26:30 +000058 inline bool markConstant(Constant *V) {
59 if (LatticeValue != constant) {
60 LatticeValue = constant;
Chris Lattner138a1242001-06-27 23:38:11 +000061 ConstantVal = V;
62 return true;
63 } else {
Chris Lattnerb70d82f2001-09-07 16:43:22 +000064 assert(ConstantVal == V && "Marking constant with different value");
Chris Lattner138a1242001-06-27 23:38:11 +000065 }
66 return false;
67 }
68
Chris Lattnere9bb2df2001-12-03 22:26:30 +000069 inline bool isUndefined() const { return LatticeValue == undefined; }
70 inline bool isConstant() const { return LatticeValue == constant; }
71 inline bool isOverdefined() const { return LatticeValue == overdefined; }
Chris Lattner138a1242001-06-27 23:38:11 +000072
Chris Lattnere9bb2df2001-12-03 22:26:30 +000073 inline Constant *getConstant() const { return ConstantVal; }
Chris Lattner138a1242001-06-27 23:38:11 +000074};
75
Chris Lattner0dbfc052002-04-29 21:26:08 +000076} // end anonymous namespace
Chris Lattner138a1242001-06-27 23:38:11 +000077
78
79//===----------------------------------------------------------------------===//
80// SCCP Class
81//
82// This class does all of the work of Sparse Conditional Constant Propogation.
Chris Lattner138a1242001-06-27 23:38:11 +000083//
Chris Lattner0dbfc052002-04-29 21:26:08 +000084namespace {
85class SCCP : public FunctionPass, public InstVisitor<SCCP> {
Chris Lattner697954c2002-01-20 22:54:45 +000086 std::set<BasicBlock*> BBExecutable;// The basic blocks that are executable
87 std::map<Value*, InstVal> ValueState; // The state each value is in...
Chris Lattner138a1242001-06-27 23:38:11 +000088
Chris Lattner071d0ad2002-05-07 04:29:32 +000089 std::vector<Instruction*> InstWorkList;// The instruction work list
Chris Lattner697954c2002-01-20 22:54:45 +000090 std::vector<BasicBlock*> BBWorkList; // The BasicBlock work list
Chris Lattner138a1242001-06-27 23:38:11 +000091public:
92
Chris Lattner0dbfc052002-04-29 21:26:08 +000093 // runOnFunction - Run the Sparse Conditional Constant Propogation algorithm,
94 // and return true if the function was modified.
95 //
Chris Lattner7e708292002-06-25 16:13:24 +000096 bool runOnFunction(Function &F);
Chris Lattner0dbfc052002-04-29 21:26:08 +000097
98 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000099 AU.setPreservesCFG();
Chris Lattner0dbfc052002-04-29 21:26:08 +0000100 }
101
Chris Lattner138a1242001-06-27 23:38:11 +0000102
103 //===--------------------------------------------------------------------===//
104 // The implementation of this class
105 //
106private:
Chris Lattner2a632552002-04-18 15:13:15 +0000107 friend class InstVisitor<SCCP>; // Allow callbacks from visitor
Chris Lattner138a1242001-06-27 23:38:11 +0000108
109 // markValueOverdefined - Make a value be marked as "constant". If the value
110 // is not already a constant, add it to the instruction work list so that
111 // the users of the instruction are updated later.
112 //
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000113 inline bool markConstant(Instruction *I, Constant *V) {
Chris Lattnerf016ea42002-05-22 17:17:27 +0000114 DEBUG(cerr << "markConstant: " << V << " = " << I);
Chris Lattner59f0ce22002-05-02 21:18:01 +0000115
Chris Lattner138a1242001-06-27 23:38:11 +0000116 if (ValueState[I].markConstant(V)) {
Chris Lattner071d0ad2002-05-07 04:29:32 +0000117 InstWorkList.push_back(I);
Chris Lattner138a1242001-06-27 23:38:11 +0000118 return true;
119 }
120 return false;
121 }
122
123 // markValueOverdefined - Make a value be marked as "overdefined". If the
124 // value is not already overdefined, add it to the instruction work list so
125 // that the users of the instruction are updated later.
126 //
127 inline bool markOverdefined(Value *V) {
128 if (ValueState[V].markOverdefined()) {
Chris Lattner9636a912001-10-01 16:18:37 +0000129 if (Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattnerf016ea42002-05-22 17:17:27 +0000130 DEBUG(cerr << "markOverdefined: " << V);
Chris Lattner071d0ad2002-05-07 04:29:32 +0000131 InstWorkList.push_back(I); // Only instructions go on the work list
Chris Lattner138a1242001-06-27 23:38:11 +0000132 }
133 return true;
134 }
135 return false;
136 }
137
138 // getValueState - Return the InstVal object that corresponds to the value.
139 // This function is neccesary because not all values should start out in the
Chris Lattner73e21422002-04-09 19:48:49 +0000140 // underdefined state... Argument's should be overdefined, and
Chris Lattner79df7c02002-03-26 18:01:55 +0000141 // constants should be marked as constants. If a value is not known to be an
Chris Lattner138a1242001-06-27 23:38:11 +0000142 // Instruction object, then use this accessor to get its value from the map.
143 //
144 inline InstVal &getValueState(Value *V) {
Chris Lattner697954c2002-01-20 22:54:45 +0000145 std::map<Value*, InstVal>::iterator I = ValueState.find(V);
Chris Lattner138a1242001-06-27 23:38:11 +0000146 if (I != ValueState.end()) return I->second; // Common case, in the map
147
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000148 if (Constant *CPV = dyn_cast<Constant>(V)) { // Constants are constant
Chris Lattner138a1242001-06-27 23:38:11 +0000149 ValueState[CPV].markConstant(CPV);
Chris Lattner73e21422002-04-09 19:48:49 +0000150 } else if (isa<Argument>(V)) { // Arguments are overdefined
Chris Lattner138a1242001-06-27 23:38:11 +0000151 ValueState[V].markOverdefined();
Chris Lattner2a88bb72002-08-30 23:39:00 +0000152 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
153 // The address of a global is a constant...
154 ValueState[V].markConstant(ConstantPointerRef::get(GV));
155 }
Chris Lattner138a1242001-06-27 23:38:11 +0000156 // All others are underdefined by default...
157 return ValueState[V];
158 }
159
160 // markExecutable - Mark a basic block as executable, adding it to the BB
161 // work list if it is not already executable...
162 //
163 void markExecutable(BasicBlock *BB) {
164 if (BBExecutable.count(BB)) return;
Chris Lattner7e708292002-06-25 16:13:24 +0000165 DEBUG(cerr << "Marking BB Executable: " << *BB);
Chris Lattner138a1242001-06-27 23:38:11 +0000166 BBExecutable.insert(BB); // Basic block is executable!
167 BBWorkList.push_back(BB); // Add the block to the work list!
168 }
169
Chris Lattner138a1242001-06-27 23:38:11 +0000170
Chris Lattner2a632552002-04-18 15:13:15 +0000171 // visit implementations - Something changed in this instruction... Either an
Chris Lattnercb056de2001-06-29 23:56:23 +0000172 // operand made a transition, or the instruction is newly executable. Change
173 // the value type of I to reflect these changes if appropriate.
174 //
Chris Lattner7e708292002-06-25 16:13:24 +0000175 void visitPHINode(PHINode &I);
Chris Lattner2a632552002-04-18 15:13:15 +0000176
177 // Terminators
Chris Lattner7e708292002-06-25 16:13:24 +0000178 void visitReturnInst(ReturnInst &I) { /*does not have an effect*/ }
179 void visitTerminatorInst(TerminatorInst &TI);
Chris Lattner2a632552002-04-18 15:13:15 +0000180
Chris Lattnerb8047602002-08-14 17:53:45 +0000181 void visitCastInst(CastInst &I);
Chris Lattner7e708292002-06-25 16:13:24 +0000182 void visitBinaryOperator(Instruction &I);
183 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
Chris Lattner2a632552002-04-18 15:13:15 +0000184
185 // Instructions that cannot be folded away...
Chris Lattner7e708292002-06-25 16:13:24 +0000186 void visitStoreInst (Instruction &I) { /*returns void*/ }
Chris Lattnercc63f1c2002-08-22 23:37:20 +0000187 void visitLoadInst (Instruction &I) { markOverdefined(&I); }
Chris Lattner2a88bb72002-08-30 23:39:00 +0000188 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattner7e708292002-06-25 16:13:24 +0000189 void visitCallInst (Instruction &I) { markOverdefined(&I); }
190 void visitInvokeInst (Instruction &I) { markOverdefined(&I); }
191 void visitAllocationInst(Instruction &I) { markOverdefined(&I); }
192 void visitFreeInst (Instruction &I) { /*returns void*/ }
Chris Lattner2a632552002-04-18 15:13:15 +0000193
Chris Lattner7e708292002-06-25 16:13:24 +0000194 void visitInstruction(Instruction &I) {
Chris Lattner2a632552002-04-18 15:13:15 +0000195 // If a new instruction is added to LLVM that we don't handle...
196 cerr << "SCCP: Don't know how to handle: " << I;
Chris Lattner7e708292002-06-25 16:13:24 +0000197 markOverdefined(&I); // Just in case
Chris Lattner2a632552002-04-18 15:13:15 +0000198 }
Chris Lattnercb056de2001-06-29 23:56:23 +0000199
Chris Lattnerb9a66342002-05-02 21:44:00 +0000200 // getFeasibleSuccessors - Return a vector of booleans to indicate which
201 // successors are reachable from a given terminator instruction.
202 //
Chris Lattner7e708292002-06-25 16:13:24 +0000203 void getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs);
Chris Lattnerb9a66342002-05-02 21:44:00 +0000204
Chris Lattner59f0ce22002-05-02 21:18:01 +0000205 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
206 // block to the 'To' basic block is currently feasible...
207 //
208 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To);
209
Chris Lattnercb056de2001-06-29 23:56:23 +0000210 // OperandChangedState - This method is invoked on all of the users of an
211 // instruction that was just changed state somehow.... Based on this
212 // information, we need to update the specified user of this instruction.
213 //
Chris Lattner59f0ce22002-05-02 21:18:01 +0000214 void OperandChangedState(User *U) {
215 // Only instructions use other variable values!
Chris Lattner7e708292002-06-25 16:13:24 +0000216 Instruction &I = cast<Instruction>(*U);
217 if (!BBExecutable.count(I.getParent())) return;// Inst not executable yet!
Chris Lattner59f0ce22002-05-02 21:18:01 +0000218 visit(I);
219 }
Chris Lattnercb056de2001-06-29 23:56:23 +0000220};
Chris Lattnerf6293092002-07-23 18:06:35 +0000221
Chris Lattnera6275cc2002-07-26 21:12:46 +0000222 RegisterOpt<SCCP> X("sccp", "Sparse Conditional Constant Propogation");
Chris Lattner0dbfc052002-04-29 21:26:08 +0000223} // end anonymous namespace
224
225
226// createSCCPPass - This is the public interface to this file...
227//
228Pass *createSCCPPass() {
229 return new SCCP();
230}
231
Chris Lattner138a1242001-06-27 23:38:11 +0000232
Chris Lattner138a1242001-06-27 23:38:11 +0000233//===----------------------------------------------------------------------===//
234// SCCP Class Implementation
235
236
Chris Lattner0dbfc052002-04-29 21:26:08 +0000237// runOnFunction() - Run the Sparse Conditional Constant Propogation algorithm,
238// and return true if the function was modified.
Chris Lattner138a1242001-06-27 23:38:11 +0000239//
Chris Lattner7e708292002-06-25 16:13:24 +0000240bool SCCP::runOnFunction(Function &F) {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000241 // Mark the first block of the function as being executable...
Chris Lattner7e708292002-06-25 16:13:24 +0000242 markExecutable(&F.front());
Chris Lattner138a1242001-06-27 23:38:11 +0000243
244 // Process the work lists until their are empty!
245 while (!BBWorkList.empty() || !InstWorkList.empty()) {
246 // Process the instruction work list...
247 while (!InstWorkList.empty()) {
Chris Lattner071d0ad2002-05-07 04:29:32 +0000248 Instruction *I = InstWorkList.back();
249 InstWorkList.pop_back();
Chris Lattner138a1242001-06-27 23:38:11 +0000250
Chris Lattnerf016ea42002-05-22 17:17:27 +0000251 DEBUG(cerr << "\nPopped off I-WL: " << I);
Chris Lattner138a1242001-06-27 23:38:11 +0000252
253
254 // "I" got into the work list because it either made the transition from
255 // bottom to constant, or to Overdefined.
256 //
257 // Update all of the users of this instruction's value...
258 //
259 for_each(I->use_begin(), I->use_end(),
260 bind_obj(this, &SCCP::OperandChangedState));
261 }
262
263 // Process the basic block work list...
264 while (!BBWorkList.empty()) {
265 BasicBlock *BB = BBWorkList.back();
266 BBWorkList.pop_back();
267
Chris Lattnerf016ea42002-05-22 17:17:27 +0000268 DEBUG(cerr << "\nPopped off BBWL: " << BB);
Chris Lattner138a1242001-06-27 23:38:11 +0000269
270 // If this block only has a single successor, mark it as executable as
271 // well... if not, terminate the do loop.
272 //
273 if (BB->getTerminator()->getNumSuccessors() == 1)
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000274 markExecutable(BB->getTerminator()->getSuccessor(0));
Chris Lattner138a1242001-06-27 23:38:11 +0000275
Chris Lattner2a632552002-04-18 15:13:15 +0000276 // Notify all instructions in this basic block that they are newly
277 // executable.
278 visit(BB);
Chris Lattner138a1242001-06-27 23:38:11 +0000279 }
280 }
281
Chris Lattnerf016ea42002-05-22 17:17:27 +0000282 if (DebugFlag) {
Chris Lattner7e708292002-06-25 16:13:24 +0000283 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
284 if (!BBExecutable.count(I))
Chris Lattnerf016ea42002-05-22 17:17:27 +0000285 cerr << "BasicBlock Dead:" << *I;
286 }
Chris Lattner138a1242001-06-27 23:38:11 +0000287
Chris Lattnerf57b8452002-04-27 06:56:12 +0000288 // Iterate over all of the instructions in a function, replacing them with
Chris Lattner138a1242001-06-27 23:38:11 +0000289 // constants if we have found them to be of constant values.
290 //
291 bool MadeChanges = false;
Chris Lattner7e708292002-06-25 16:13:24 +0000292 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB)
Chris Lattner221d6882002-02-12 21:07:25 +0000293 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
Chris Lattner7e708292002-06-25 16:13:24 +0000294 Instruction &Inst = *BI;
295 InstVal &IV = ValueState[&Inst];
Chris Lattner221d6882002-02-12 21:07:25 +0000296 if (IV.isConstant()) {
297 Constant *Const = IV.getConstant();
Chris Lattnerf016ea42002-05-22 17:17:27 +0000298 DEBUG(cerr << "Constant: " << Const << " = " << Inst);
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000299
Chris Lattner221d6882002-02-12 21:07:25 +0000300 // Replaces all of the uses of a variable with uses of the constant.
Chris Lattner7e708292002-06-25 16:13:24 +0000301 Inst.replaceAllUsesWith(Const);
Chris Lattner138a1242001-06-27 23:38:11 +0000302
Chris Lattner0e9c5152002-05-02 20:32:51 +0000303 // Remove the operator from the list of definitions... and delete it.
Chris Lattner7e708292002-06-25 16:13:24 +0000304 BI = BB->getInstList().erase(BI);
Chris Lattner138a1242001-06-27 23:38:11 +0000305
Chris Lattner221d6882002-02-12 21:07:25 +0000306 // Hey, we just changed something!
307 MadeChanges = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000308 ++NumInstRemoved;
Chris Lattnerb9a66342002-05-02 21:44:00 +0000309 } else {
310 ++BI;
Chris Lattner221d6882002-02-12 21:07:25 +0000311 }
Chris Lattner138a1242001-06-27 23:38:11 +0000312 }
Chris Lattner138a1242001-06-27 23:38:11 +0000313
Chris Lattner59f0ce22002-05-02 21:18:01 +0000314 // Reset state so that the next invocation will have empty data structures
Chris Lattner0dbfc052002-04-29 21:26:08 +0000315 BBExecutable.clear();
316 ValueState.clear();
Chris Lattneraf663462002-11-04 02:54:22 +0000317 std::vector<Instruction*>().swap(InstWorkList);
318 std::vector<BasicBlock*>().swap(BBWorkList);
Chris Lattner0dbfc052002-04-29 21:26:08 +0000319
Chris Lattnerb70d82f2001-09-07 16:43:22 +0000320 return MadeChanges;
Chris Lattner138a1242001-06-27 23:38:11 +0000321}
322
Chris Lattnerb9a66342002-05-02 21:44:00 +0000323
324// getFeasibleSuccessors - Return a vector of booleans to indicate which
325// successors are reachable from a given terminator instruction.
326//
Chris Lattner7e708292002-06-25 16:13:24 +0000327void SCCP::getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs) {
328 assert(Succs.size() == TI.getNumSuccessors() && "Succs vector wrong size!");
329 if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
Chris Lattnerb9a66342002-05-02 21:44:00 +0000330 if (BI->isUnconditional()) {
331 Succs[0] = true;
332 } else {
333 InstVal &BCValue = getValueState(BI->getCondition());
334 if (BCValue.isOverdefined()) {
335 // Overdefined condition variables mean the branch could go either way.
336 Succs[0] = Succs[1] = true;
337 } else if (BCValue.isConstant()) {
338 // Constant condition variables mean the branch can only go a single way
339 Succs[BCValue.getConstant() == ConstantBool::False] = true;
340 }
341 }
Chris Lattner7e708292002-06-25 16:13:24 +0000342 } else if (InvokeInst *II = dyn_cast<InvokeInst>(&TI)) {
Chris Lattnerb9a66342002-05-02 21:44:00 +0000343 // Invoke instructions successors are always executable.
344 Succs[0] = Succs[1] = true;
Chris Lattner7e708292002-06-25 16:13:24 +0000345 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) {
Chris Lattnerb9a66342002-05-02 21:44:00 +0000346 InstVal &SCValue = getValueState(SI->getCondition());
347 if (SCValue.isOverdefined()) { // Overdefined condition?
348 // All destinations are executable!
Chris Lattner7e708292002-06-25 16:13:24 +0000349 Succs.assign(TI.getNumSuccessors(), true);
Chris Lattnerb9a66342002-05-02 21:44:00 +0000350 } else if (SCValue.isConstant()) {
351 Constant *CPV = SCValue.getConstant();
352 // Make sure to skip the "default value" which isn't a value
353 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) {
354 if (SI->getSuccessorValue(i) == CPV) {// Found the right branch...
355 Succs[i] = true;
356 return;
357 }
358 }
359
360 // Constant value not equal to any of the branches... must execute
361 // default branch then...
362 Succs[0] = true;
363 }
364 } else {
365 cerr << "SCCP: Don't know how to handle: " << TI;
Chris Lattner7e708292002-06-25 16:13:24 +0000366 Succs.assign(TI.getNumSuccessors(), true);
Chris Lattnerb9a66342002-05-02 21:44:00 +0000367 }
368}
369
370
Chris Lattner59f0ce22002-05-02 21:18:01 +0000371// isEdgeFeasible - Return true if the control flow edge from the 'From' basic
372// block to the 'To' basic block is currently feasible...
373//
374bool SCCP::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
375 assert(BBExecutable.count(To) && "Dest should always be alive!");
376
377 // Make sure the source basic block is executable!!
378 if (!BBExecutable.count(From)) return false;
379
Chris Lattnerb9a66342002-05-02 21:44:00 +0000380 // Check to make sure this edge itself is actually feasible now...
381 TerminatorInst *FT = From->getTerminator();
382 std::vector<bool> SuccFeasible(FT->getNumSuccessors());
Chris Lattner7e708292002-06-25 16:13:24 +0000383 getFeasibleSuccessors(*FT, SuccFeasible);
Chris Lattnerb9a66342002-05-02 21:44:00 +0000384
385 // Check all edges from From to To. If any are feasible, return true.
386 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
387 if (FT->getSuccessor(i) == To && SuccFeasible[i])
388 return true;
389
390 // Otherwise, none of the edges are actually feasible at this time...
391 return false;
Chris Lattner59f0ce22002-05-02 21:18:01 +0000392}
Chris Lattner138a1242001-06-27 23:38:11 +0000393
Chris Lattner2a632552002-04-18 15:13:15 +0000394// visit Implementations - Something changed in this instruction... Either an
Chris Lattner138a1242001-06-27 23:38:11 +0000395// operand made a transition, or the instruction is newly executable. Change
396// the value type of I to reflect these changes if appropriate. This method
397// makes sure to do the following actions:
398//
399// 1. If a phi node merges two constants in, and has conflicting value coming
400// from different branches, or if the PHI node merges in an overdefined
401// value, then the PHI node becomes overdefined.
402// 2. If a phi node merges only constants in, and they all agree on value, the
403// PHI node becomes a constant value equal to that.
404// 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
405// 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
406// 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
407// 6. If a conditional branch has a value that is constant, make the selected
408// destination executable
409// 7. If a conditional branch has a value that is overdefined, make all
410// successors executable.
411//
Chris Lattner138a1242001-06-27 23:38:11 +0000412
Chris Lattner7e708292002-06-25 16:13:24 +0000413void SCCP::visitPHINode(PHINode &PN) {
414 unsigned NumValues = PN.getNumIncomingValues(), i;
Chris Lattner2a632552002-04-18 15:13:15 +0000415 InstVal *OperandIV = 0;
Chris Lattner138a1242001-06-27 23:38:11 +0000416
Chris Lattner2a632552002-04-18 15:13:15 +0000417 // Look at all of the executable operands of the PHI node. If any of them
418 // are overdefined, the PHI becomes overdefined as well. If they are all
419 // constant, and they agree with each other, the PHI becomes the identical
420 // constant. If they are constant and don't agree, the PHI is overdefined.
421 // If there are no executable operands, the PHI remains undefined.
422 //
423 for (i = 0; i < NumValues; ++i) {
Chris Lattner7e708292002-06-25 16:13:24 +0000424 if (isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent())) {
425 InstVal &IV = getValueState(PN.getIncomingValue(i));
Chris Lattner2a632552002-04-18 15:13:15 +0000426 if (IV.isUndefined()) continue; // Doesn't influence PHI node.
427 if (IV.isOverdefined()) { // PHI node becomes overdefined!
Chris Lattner7e708292002-06-25 16:13:24 +0000428 markOverdefined(&PN);
Chris Lattner2a632552002-04-18 15:13:15 +0000429 return;
430 }
Chris Lattner138a1242001-06-27 23:38:11 +0000431
Chris Lattner2a632552002-04-18 15:13:15 +0000432 if (OperandIV == 0) { // Grab the first value...
433 OperandIV = &IV;
434 } else { // Another value is being merged in!
435 // There is already a reachable operand. If we conflict with it,
436 // then the PHI node becomes overdefined. If we agree with it, we
437 // can continue on.
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000438
Chris Lattner2a632552002-04-18 15:13:15 +0000439 // Check to see if there are two different constants merging...
440 if (IV.getConstant() != OperandIV->getConstant()) {
441 // Yes there is. This means the PHI node is not constant.
442 // You must be overdefined poor PHI.
443 //
Chris Lattner7e708292002-06-25 16:13:24 +0000444 markOverdefined(&PN); // The PHI node now becomes overdefined
Chris Lattner2a632552002-04-18 15:13:15 +0000445 return; // I'm done analyzing you
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000446 }
Chris Lattner138a1242001-06-27 23:38:11 +0000447 }
448 }
Chris Lattner138a1242001-06-27 23:38:11 +0000449 }
450
Chris Lattner2a632552002-04-18 15:13:15 +0000451 // If we exited the loop, this means that the PHI node only has constant
452 // arguments that agree with each other(and OperandIV is a pointer to one
453 // of their InstVal's) or OperandIV is null because there are no defined
454 // incoming arguments. If this is the case, the PHI remains undefined.
Chris Lattner138a1242001-06-27 23:38:11 +0000455 //
Chris Lattner2a632552002-04-18 15:13:15 +0000456 if (OperandIV) {
457 assert(OperandIV->isConstant() && "Should only be here for constants!");
Chris Lattner7e708292002-06-25 16:13:24 +0000458 markConstant(&PN, OperandIV->getConstant()); // Aquire operand value
Chris Lattner138a1242001-06-27 23:38:11 +0000459 }
Chris Lattner138a1242001-06-27 23:38:11 +0000460}
461
Chris Lattner7e708292002-06-25 16:13:24 +0000462void SCCP::visitTerminatorInst(TerminatorInst &TI) {
463 std::vector<bool> SuccFeasible(TI.getNumSuccessors());
Chris Lattnerb9a66342002-05-02 21:44:00 +0000464 getFeasibleSuccessors(TI, SuccFeasible);
Chris Lattner138a1242001-06-27 23:38:11 +0000465
Chris Lattnerb9a66342002-05-02 21:44:00 +0000466 // Mark all feasible successors executable...
467 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
Chris Lattner7f9412b2002-05-22 16:07:20 +0000468 if (SuccFeasible[i]) {
Chris Lattner7e708292002-06-25 16:13:24 +0000469 BasicBlock *Succ = TI.getSuccessor(i);
Chris Lattner7f9412b2002-05-22 16:07:20 +0000470 markExecutable(Succ);
471
472 // Visit all of the PHI nodes that merge values from this block...
473 // Because this edge may be new executable, and PHI nodes that used to be
474 // constant now may not be.
475 //
476 for (BasicBlock::iterator I = Succ->begin();
Chris Lattner7e708292002-06-25 16:13:24 +0000477 PHINode *PN = dyn_cast<PHINode>(&*I); ++I)
478 visitPHINode(*PN);
Chris Lattner7f9412b2002-05-22 16:07:20 +0000479 }
Chris Lattner2a632552002-04-18 15:13:15 +0000480}
481
Chris Lattnerb8047602002-08-14 17:53:45 +0000482void SCCP::visitCastInst(CastInst &I) {
Chris Lattner7e708292002-06-25 16:13:24 +0000483 Value *V = I.getOperand(0);
Chris Lattner2a632552002-04-18 15:13:15 +0000484 InstVal &VState = getValueState(V);
485 if (VState.isOverdefined()) { // Inherit overdefinedness of operand
Chris Lattner7e708292002-06-25 16:13:24 +0000486 markOverdefined(&I);
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000487 } else if (VState.isConstant()) { // Propagate constant value
Chris Lattnerb8047602002-08-14 17:53:45 +0000488 Constant *Result =
489 ConstantFoldCastInstruction(VState.getConstant(), I.getType());
Chris Lattner2a632552002-04-18 15:13:15 +0000490
491 if (Result) {
492 // This instruction constant folds!
Chris Lattner7e708292002-06-25 16:13:24 +0000493 markConstant(&I, Result);
Chris Lattner2a632552002-04-18 15:13:15 +0000494 } else {
Chris Lattner7e708292002-06-25 16:13:24 +0000495 markOverdefined(&I); // Don't know how to fold this instruction. :(
Chris Lattner2a632552002-04-18 15:13:15 +0000496 }
497 }
498}
499
500// Handle BinaryOperators and Shift Instructions...
Chris Lattner7e708292002-06-25 16:13:24 +0000501void SCCP::visitBinaryOperator(Instruction &I) {
502 InstVal &V1State = getValueState(I.getOperand(0));
503 InstVal &V2State = getValueState(I.getOperand(1));
Chris Lattner2a632552002-04-18 15:13:15 +0000504 if (V1State.isOverdefined() || V2State.isOverdefined()) {
Chris Lattner7e708292002-06-25 16:13:24 +0000505 markOverdefined(&I);
Chris Lattner2a632552002-04-18 15:13:15 +0000506 } else if (V1State.isConstant() && V2State.isConstant()) {
Chris Lattner4c1061f2002-05-06 03:01:37 +0000507 Constant *Result = 0;
508 if (isa<BinaryOperator>(I))
Chris Lattner7e708292002-06-25 16:13:24 +0000509 Result = ConstantFoldBinaryInstruction(I.getOpcode(),
Chris Lattner4c1061f2002-05-06 03:01:37 +0000510 V1State.getConstant(),
511 V2State.getConstant());
512 else if (isa<ShiftInst>(I))
Chris Lattner7e708292002-06-25 16:13:24 +0000513 Result = ConstantFoldShiftInstruction(I.getOpcode(),
Chris Lattner4c1061f2002-05-06 03:01:37 +0000514 V1State.getConstant(),
515 V2State.getConstant());
Chris Lattner2a632552002-04-18 15:13:15 +0000516 if (Result)
Chris Lattner7e708292002-06-25 16:13:24 +0000517 markConstant(&I, Result); // This instruction constant folds!
Chris Lattner2a632552002-04-18 15:13:15 +0000518 else
Chris Lattner7e708292002-06-25 16:13:24 +0000519 markOverdefined(&I); // Don't know how to fold this instruction. :(
Chris Lattner2a632552002-04-18 15:13:15 +0000520 }
521}
Chris Lattner2a88bb72002-08-30 23:39:00 +0000522
523// Handle getelementptr instructions... if all operands are constants then we
524// can turn this into a getelementptr ConstantExpr.
525//
526void SCCP::visitGetElementPtrInst(GetElementPtrInst &I) {
527 std::vector<Constant*> Operands;
528 Operands.reserve(I.getNumOperands());
529
530 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
531 InstVal &State = getValueState(I.getOperand(i));
532 if (State.isUndefined())
533 return; // Operands are not resolved yet...
534 else if (State.isOverdefined()) {
535 markOverdefined(&I);
536 return;
537 }
538 assert(State.isConstant() && "Unknown state!");
539 Operands.push_back(State.getConstant());
540 }
541
542 Constant *Ptr = Operands[0];
543 Operands.erase(Operands.begin()); // Erase the pointer from idx list...
544
545 markConstant(&I, ConstantExpr::getGetElementPtr(Ptr, Operands));
546}