blob: b721ca0965c64611eeee27c501a53702e5d2612d [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 Lattnerb9a66342002-05-02 21:44:00 +00009// * Proves conditional branches constant, and unconditionalizes them
Chris Lattner138a1242001-06-27 23:38:11 +000010// * Folds multiple identical constants in the constant pool together
11//
12// Notice that:
13// * This pass has a habit of making definitions be dead. It is a good idea
14// to to run a DCE pass sometime after running this pass.
15//
16//===----------------------------------------------------------------------===//
17
Chris Lattner022103b2002-05-07 20:03:00 +000018#include "llvm/Transforms/Scalar.h"
Chris Lattner968ddc92002-04-08 20:18:09 +000019#include "llvm/ConstantHandling.h"
Chris Lattner79df7c02002-03-26 18:01:55 +000020#include "llvm/Function.h"
Chris Lattner6d7491c2002-05-07 18:11:30 +000021#include "llvm/BasicBlock.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000022#include "llvm/iPHINode.h"
Chris Lattner3b7bfdb2001-07-14 06:11:51 +000023#include "llvm/iMemory.h"
Chris Lattner138a1242001-06-27 23:38:11 +000024#include "llvm/iTerminators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000025#include "llvm/iOther.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000026#include "llvm/Pass.h"
Chris Lattner2a632552002-04-18 15:13:15 +000027#include "llvm/Support/InstVisitor.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000028#include "Support/STLExtras.h"
Chris Lattner3dec1f22002-05-10 15:38:35 +000029#include "Support/StatisticReporter.h"
Chris Lattner138a1242001-06-27 23:38:11 +000030#include <algorithm>
Chris Lattner138a1242001-06-27 23:38:11 +000031#include <set>
Chris Lattner697954c2002-01-20 22:54:45 +000032#include <iostream>
33using std::cerr;
Chris Lattner138a1242001-06-27 23:38:11 +000034
Chris Lattner3dec1f22002-05-10 15:38:35 +000035static Statistic<> NumInstRemoved("sccp\t\t- Number of instructions removed");
36
Chris Lattner138a1242001-06-27 23:38:11 +000037// InstVal class - This class represents the different lattice values that an
Chris Lattnerf57b8452002-04-27 06:56:12 +000038// instruction may occupy. It is a simple class with value semantics.
Chris Lattner138a1242001-06-27 23:38:11 +000039//
Chris Lattner0dbfc052002-04-29 21:26:08 +000040namespace {
Chris Lattner138a1242001-06-27 23:38:11 +000041class InstVal {
42 enum {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000043 undefined, // This instruction has no known value
44 constant, // This instruction has a constant value
Chris Lattner138a1242001-06-27 23:38:11 +000045 // Range, // This instruction is known to fall within a range
Chris Lattnere9bb2df2001-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 Lattner138a1242001-06-27 23:38:11 +000049public:
Chris Lattnere9bb2df2001-12-03 22:26:30 +000050 inline InstVal() : LatticeValue(undefined), ConstantVal(0) {}
Chris Lattner138a1242001-06-27 23:38:11 +000051
52 // markOverdefined - Return true if this is a new status to be in...
53 inline bool markOverdefined() {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000054 if (LatticeValue != overdefined) {
55 LatticeValue = overdefined;
Chris Lattner138a1242001-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 Lattnere9bb2df2001-12-03 22:26:30 +000062 inline bool markConstant(Constant *V) {
63 if (LatticeValue != constant) {
64 LatticeValue = constant;
Chris Lattner138a1242001-06-27 23:38:11 +000065 ConstantVal = V;
66 return true;
67 } else {
Chris Lattnerb70d82f2001-09-07 16:43:22 +000068 assert(ConstantVal == V && "Marking constant with different value");
Chris Lattner138a1242001-06-27 23:38:11 +000069 }
70 return false;
71 }
72
Chris Lattnere9bb2df2001-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 Lattner138a1242001-06-27 23:38:11 +000076
Chris Lattnere9bb2df2001-12-03 22:26:30 +000077 inline Constant *getConstant() const { return ConstantVal; }
Chris Lattner138a1242001-06-27 23:38:11 +000078};
79
Chris Lattner0dbfc052002-04-29 21:26:08 +000080} // end anonymous namespace
Chris Lattner138a1242001-06-27 23:38:11 +000081
82
83//===----------------------------------------------------------------------===//
84// SCCP Class
85//
86// This class does all of the work of Sparse Conditional Constant Propogation.
Chris Lattner138a1242001-06-27 23:38:11 +000087//
Chris Lattner0dbfc052002-04-29 21:26:08 +000088namespace {
89class SCCP : public FunctionPass, public InstVisitor<SCCP> {
Chris Lattner697954c2002-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 Lattner138a1242001-06-27 23:38:11 +000092
Chris Lattner071d0ad2002-05-07 04:29:32 +000093 std::vector<Instruction*> InstWorkList;// The instruction work list
Chris Lattner697954c2002-01-20 22:54:45 +000094 std::vector<BasicBlock*> BBWorkList; // The BasicBlock work list
Chris Lattner138a1242001-06-27 23:38:11 +000095public:
96
Chris Lattner0dbfc052002-04-29 21:26:08 +000097 // runOnFunction - Run the Sparse Conditional Constant Propogation algorithm,
98 // and return true if the function was modified.
99 //
Chris Lattner7e708292002-06-25 16:13:24 +0000100 bool runOnFunction(Function &F);
Chris Lattner0dbfc052002-04-29 21:26:08 +0000101
102 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerb9a66342002-05-02 21:44:00 +0000103 AU.preservesCFG();
Chris Lattner0dbfc052002-04-29 21:26:08 +0000104 }
105
Chris Lattner138a1242001-06-27 23:38:11 +0000106
107 //===--------------------------------------------------------------------===//
108 // The implementation of this class
109 //
110private:
Chris Lattner2a632552002-04-18 15:13:15 +0000111 friend class InstVisitor<SCCP>; // Allow callbacks from visitor
Chris Lattner138a1242001-06-27 23:38:11 +0000112
113 // markValueOverdefined - Make a value be marked as "constant". If the value
114 // is not already a constant, add it to the instruction work list so that
115 // the users of the instruction are updated later.
116 //
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000117 inline bool markConstant(Instruction *I, Constant *V) {
Chris Lattnerf016ea42002-05-22 17:17:27 +0000118 DEBUG(cerr << "markConstant: " << V << " = " << I);
Chris Lattner59f0ce22002-05-02 21:18:01 +0000119
Chris Lattner138a1242001-06-27 23:38:11 +0000120 if (ValueState[I].markConstant(V)) {
Chris Lattner071d0ad2002-05-07 04:29:32 +0000121 InstWorkList.push_back(I);
Chris Lattner138a1242001-06-27 23:38:11 +0000122 return true;
123 }
124 return false;
125 }
126
127 // markValueOverdefined - Make a value be marked as "overdefined". If the
128 // value is not already overdefined, add it to the instruction work list so
129 // that the users of the instruction are updated later.
130 //
131 inline bool markOverdefined(Value *V) {
132 if (ValueState[V].markOverdefined()) {
Chris Lattner9636a912001-10-01 16:18:37 +0000133 if (Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattnerf016ea42002-05-22 17:17:27 +0000134 DEBUG(cerr << "markOverdefined: " << V);
Chris Lattner071d0ad2002-05-07 04:29:32 +0000135 InstWorkList.push_back(I); // Only instructions go on the work list
Chris Lattner138a1242001-06-27 23:38:11 +0000136 }
137 return true;
138 }
139 return false;
140 }
141
142 // getValueState - Return the InstVal object that corresponds to the value.
143 // This function is neccesary because not all values should start out in the
Chris Lattner73e21422002-04-09 19:48:49 +0000144 // underdefined state... Argument's should be overdefined, and
Chris Lattner79df7c02002-03-26 18:01:55 +0000145 // constants should be marked as constants. If a value is not known to be an
Chris Lattner138a1242001-06-27 23:38:11 +0000146 // Instruction object, then use this accessor to get its value from the map.
147 //
148 inline InstVal &getValueState(Value *V) {
Chris Lattner697954c2002-01-20 22:54:45 +0000149 std::map<Value*, InstVal>::iterator I = ValueState.find(V);
Chris Lattner138a1242001-06-27 23:38:11 +0000150 if (I != ValueState.end()) return I->second; // Common case, in the map
151
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000152 if (Constant *CPV = dyn_cast<Constant>(V)) { // Constants are constant
Chris Lattner138a1242001-06-27 23:38:11 +0000153 ValueState[CPV].markConstant(CPV);
Chris Lattner73e21422002-04-09 19:48:49 +0000154 } else if (isa<Argument>(V)) { // Arguments are overdefined
Chris Lattner138a1242001-06-27 23:38:11 +0000155 ValueState[V].markOverdefined();
156 }
157 // All others are underdefined by default...
158 return ValueState[V];
159 }
160
161 // markExecutable - Mark a basic block as executable, adding it to the BB
162 // work list if it is not already executable...
163 //
164 void markExecutable(BasicBlock *BB) {
165 if (BBExecutable.count(BB)) return;
Chris Lattner7e708292002-06-25 16:13:24 +0000166 DEBUG(cerr << "Marking BB Executable: " << *BB);
Chris Lattner138a1242001-06-27 23:38:11 +0000167 BBExecutable.insert(BB); // Basic block is executable!
168 BBWorkList.push_back(BB); // Add the block to the work list!
169 }
170
Chris Lattner138a1242001-06-27 23:38:11 +0000171
Chris Lattner2a632552002-04-18 15:13:15 +0000172 // visit implementations - Something changed in this instruction... Either an
Chris Lattnercb056de2001-06-29 23:56:23 +0000173 // operand made a transition, or the instruction is newly executable. Change
174 // the value type of I to reflect these changes if appropriate.
175 //
Chris Lattner7e708292002-06-25 16:13:24 +0000176 void visitPHINode(PHINode &I);
Chris Lattner2a632552002-04-18 15:13:15 +0000177
178 // Terminators
Chris Lattner7e708292002-06-25 16:13:24 +0000179 void visitReturnInst(ReturnInst &I) { /*does not have an effect*/ }
180 void visitTerminatorInst(TerminatorInst &TI);
Chris Lattner2a632552002-04-18 15:13:15 +0000181
Chris Lattner7e708292002-06-25 16:13:24 +0000182 void visitUnaryOperator(Instruction &I);
183 void visitCastInst(CastInst &I) { visitUnaryOperator(I); }
184 void visitBinaryOperator(Instruction &I);
185 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
Chris Lattner2a632552002-04-18 15:13:15 +0000186
187 // Instructions that cannot be folded away...
Chris Lattner7e708292002-06-25 16:13:24 +0000188 void visitStoreInst (Instruction &I) { /*returns void*/ }
189 void visitMemAccessInst (Instruction &I) { markOverdefined(&I); }
190 void visitCallInst (Instruction &I) { markOverdefined(&I); }
191 void visitInvokeInst (Instruction &I) { markOverdefined(&I); }
192 void visitAllocationInst(Instruction &I) { markOverdefined(&I); }
193 void visitFreeInst (Instruction &I) { /*returns void*/ }
Chris Lattner2a632552002-04-18 15:13:15 +0000194
Chris Lattner7e708292002-06-25 16:13:24 +0000195 void visitInstruction(Instruction &I) {
Chris Lattner2a632552002-04-18 15:13:15 +0000196 // If a new instruction is added to LLVM that we don't handle...
197 cerr << "SCCP: Don't know how to handle: " << I;
Chris Lattner7e708292002-06-25 16:13:24 +0000198 markOverdefined(&I); // Just in case
Chris Lattner2a632552002-04-18 15:13:15 +0000199 }
Chris Lattnercb056de2001-06-29 23:56:23 +0000200
Chris Lattnerb9a66342002-05-02 21:44:00 +0000201 // getFeasibleSuccessors - Return a vector of booleans to indicate which
202 // successors are reachable from a given terminator instruction.
203 //
Chris Lattner7e708292002-06-25 16:13:24 +0000204 void getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs);
Chris Lattnerb9a66342002-05-02 21:44:00 +0000205
Chris Lattner59f0ce22002-05-02 21:18:01 +0000206 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
207 // block to the 'To' basic block is currently feasible...
208 //
209 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To);
210
Chris Lattnercb056de2001-06-29 23:56:23 +0000211 // OperandChangedState - This method is invoked on all of the users of an
212 // instruction that was just changed state somehow.... Based on this
213 // information, we need to update the specified user of this instruction.
214 //
Chris Lattner59f0ce22002-05-02 21:18:01 +0000215 void OperandChangedState(User *U) {
216 // Only instructions use other variable values!
Chris Lattner7e708292002-06-25 16:13:24 +0000217 Instruction &I = cast<Instruction>(*U);
218 if (!BBExecutable.count(I.getParent())) return;// Inst not executable yet!
Chris Lattner59f0ce22002-05-02 21:18:01 +0000219 visit(I);
220 }
Chris Lattnercb056de2001-06-29 23:56:23 +0000221};
Chris Lattnerf6293092002-07-23 18:06:35 +0000222
Chris Lattnera6275cc2002-07-26 21:12:46 +0000223 RegisterOpt<SCCP> X("sccp", "Sparse Conditional Constant Propogation");
Chris Lattner0dbfc052002-04-29 21:26:08 +0000224} // end anonymous namespace
225
226
227// createSCCPPass - This is the public interface to this file...
228//
229Pass *createSCCPPass() {
230 return new SCCP();
231}
232
Chris Lattner138a1242001-06-27 23:38:11 +0000233
Chris Lattner138a1242001-06-27 23:38:11 +0000234//===----------------------------------------------------------------------===//
235// SCCP Class Implementation
236
237
Chris Lattner0dbfc052002-04-29 21:26:08 +0000238// runOnFunction() - Run the Sparse Conditional Constant Propogation algorithm,
239// and return true if the function was modified.
Chris Lattner138a1242001-06-27 23:38:11 +0000240//
Chris Lattner7e708292002-06-25 16:13:24 +0000241bool SCCP::runOnFunction(Function &F) {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000242 // Mark the first block of the function as being executable...
Chris Lattner7e708292002-06-25 16:13:24 +0000243 markExecutable(&F.front());
Chris Lattner138a1242001-06-27 23:38:11 +0000244
245 // Process the work lists until their are empty!
246 while (!BBWorkList.empty() || !InstWorkList.empty()) {
247 // Process the instruction work list...
248 while (!InstWorkList.empty()) {
Chris Lattner071d0ad2002-05-07 04:29:32 +0000249 Instruction *I = InstWorkList.back();
250 InstWorkList.pop_back();
Chris Lattner138a1242001-06-27 23:38:11 +0000251
Chris Lattnerf016ea42002-05-22 17:17:27 +0000252 DEBUG(cerr << "\nPopped off I-WL: " << I);
Chris Lattner138a1242001-06-27 23:38:11 +0000253
254
255 // "I" got into the work list because it either made the transition from
256 // bottom to constant, or to Overdefined.
257 //
258 // Update all of the users of this instruction's value...
259 //
260 for_each(I->use_begin(), I->use_end(),
261 bind_obj(this, &SCCP::OperandChangedState));
262 }
263
264 // Process the basic block work list...
265 while (!BBWorkList.empty()) {
266 BasicBlock *BB = BBWorkList.back();
267 BBWorkList.pop_back();
268
Chris Lattnerf016ea42002-05-22 17:17:27 +0000269 DEBUG(cerr << "\nPopped off BBWL: " << BB);
Chris Lattner138a1242001-06-27 23:38:11 +0000270
271 // If this block only has a single successor, mark it as executable as
272 // well... if not, terminate the do loop.
273 //
274 if (BB->getTerminator()->getNumSuccessors() == 1)
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000275 markExecutable(BB->getTerminator()->getSuccessor(0));
Chris Lattner138a1242001-06-27 23:38:11 +0000276
Chris Lattner2a632552002-04-18 15:13:15 +0000277 // Notify all instructions in this basic block that they are newly
278 // executable.
279 visit(BB);
Chris Lattner138a1242001-06-27 23:38:11 +0000280 }
281 }
282
Chris Lattnerf016ea42002-05-22 17:17:27 +0000283 if (DebugFlag) {
Chris Lattner7e708292002-06-25 16:13:24 +0000284 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
285 if (!BBExecutable.count(I))
Chris Lattnerf016ea42002-05-22 17:17:27 +0000286 cerr << "BasicBlock Dead:" << *I;
287 }
Chris Lattner138a1242001-06-27 23:38:11 +0000288
Chris Lattnerf57b8452002-04-27 06:56:12 +0000289 // Iterate over all of the instructions in a function, replacing them with
Chris Lattner138a1242001-06-27 23:38:11 +0000290 // constants if we have found them to be of constant values.
291 //
292 bool MadeChanges = false;
Chris Lattner7e708292002-06-25 16:13:24 +0000293 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB)
Chris Lattner221d6882002-02-12 21:07:25 +0000294 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
Chris Lattner7e708292002-06-25 16:13:24 +0000295 Instruction &Inst = *BI;
296 InstVal &IV = ValueState[&Inst];
Chris Lattner221d6882002-02-12 21:07:25 +0000297 if (IV.isConstant()) {
298 Constant *Const = IV.getConstant();
Chris Lattnerf016ea42002-05-22 17:17:27 +0000299 DEBUG(cerr << "Constant: " << Const << " = " << Inst);
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000300
Chris Lattner221d6882002-02-12 21:07:25 +0000301 // Replaces all of the uses of a variable with uses of the constant.
Chris Lattner7e708292002-06-25 16:13:24 +0000302 Inst.replaceAllUsesWith(Const);
Chris Lattner138a1242001-06-27 23:38:11 +0000303
Chris Lattner0e9c5152002-05-02 20:32:51 +0000304 // Remove the operator from the list of definitions... and delete it.
Chris Lattner7e708292002-06-25 16:13:24 +0000305 BI = BB->getInstList().erase(BI);
Chris Lattner138a1242001-06-27 23:38:11 +0000306
Chris Lattner221d6882002-02-12 21:07:25 +0000307 // Hey, we just changed something!
308 MadeChanges = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000309 ++NumInstRemoved;
Chris Lattnerb9a66342002-05-02 21:44:00 +0000310 } else {
311 ++BI;
Chris Lattner221d6882002-02-12 21:07:25 +0000312 }
Chris Lattner138a1242001-06-27 23:38:11 +0000313 }
Chris Lattner138a1242001-06-27 23:38:11 +0000314
Chris Lattner59f0ce22002-05-02 21:18:01 +0000315 // Reset state so that the next invocation will have empty data structures
Chris Lattner0dbfc052002-04-29 21:26:08 +0000316 BBExecutable.clear();
317 ValueState.clear();
318
Chris Lattnerb70d82f2001-09-07 16:43:22 +0000319 return MadeChanges;
Chris Lattner138a1242001-06-27 23:38:11 +0000320}
321
Chris Lattnerb9a66342002-05-02 21:44:00 +0000322
323// getFeasibleSuccessors - Return a vector of booleans to indicate which
324// successors are reachable from a given terminator instruction.
325//
Chris Lattner7e708292002-06-25 16:13:24 +0000326void SCCP::getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs) {
327 assert(Succs.size() == TI.getNumSuccessors() && "Succs vector wrong size!");
328 if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
Chris Lattnerb9a66342002-05-02 21:44:00 +0000329 if (BI->isUnconditional()) {
330 Succs[0] = true;
331 } else {
332 InstVal &BCValue = getValueState(BI->getCondition());
333 if (BCValue.isOverdefined()) {
334 // Overdefined condition variables mean the branch could go either way.
335 Succs[0] = Succs[1] = true;
336 } else if (BCValue.isConstant()) {
337 // Constant condition variables mean the branch can only go a single way
338 Succs[BCValue.getConstant() == ConstantBool::False] = true;
339 }
340 }
Chris Lattner7e708292002-06-25 16:13:24 +0000341 } else if (InvokeInst *II = dyn_cast<InvokeInst>(&TI)) {
Chris Lattnerb9a66342002-05-02 21:44:00 +0000342 // Invoke instructions successors are always executable.
343 Succs[0] = Succs[1] = true;
Chris Lattner7e708292002-06-25 16:13:24 +0000344 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) {
Chris Lattnerb9a66342002-05-02 21:44:00 +0000345 InstVal &SCValue = getValueState(SI->getCondition());
346 if (SCValue.isOverdefined()) { // Overdefined condition?
347 // All destinations are executable!
Chris Lattner7e708292002-06-25 16:13:24 +0000348 Succs.assign(TI.getNumSuccessors(), true);
Chris Lattnerb9a66342002-05-02 21:44:00 +0000349 } else if (SCValue.isConstant()) {
350 Constant *CPV = SCValue.getConstant();
351 // Make sure to skip the "default value" which isn't a value
352 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) {
353 if (SI->getSuccessorValue(i) == CPV) {// Found the right branch...
354 Succs[i] = true;
355 return;
356 }
357 }
358
359 // Constant value not equal to any of the branches... must execute
360 // default branch then...
361 Succs[0] = true;
362 }
363 } else {
364 cerr << "SCCP: Don't know how to handle: " << TI;
Chris Lattner7e708292002-06-25 16:13:24 +0000365 Succs.assign(TI.getNumSuccessors(), true);
Chris Lattnerb9a66342002-05-02 21:44:00 +0000366 }
367}
368
369
Chris Lattner59f0ce22002-05-02 21:18:01 +0000370// isEdgeFeasible - Return true if the control flow edge from the 'From' basic
371// block to the 'To' basic block is currently feasible...
372//
373bool SCCP::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
374 assert(BBExecutable.count(To) && "Dest should always be alive!");
375
376 // Make sure the source basic block is executable!!
377 if (!BBExecutable.count(From)) return false;
378
Chris Lattnerb9a66342002-05-02 21:44:00 +0000379 // Check to make sure this edge itself is actually feasible now...
380 TerminatorInst *FT = From->getTerminator();
381 std::vector<bool> SuccFeasible(FT->getNumSuccessors());
Chris Lattner7e708292002-06-25 16:13:24 +0000382 getFeasibleSuccessors(*FT, SuccFeasible);
Chris Lattnerb9a66342002-05-02 21:44:00 +0000383
384 // Check all edges from From to To. If any are feasible, return true.
385 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
386 if (FT->getSuccessor(i) == To && SuccFeasible[i])
387 return true;
388
389 // Otherwise, none of the edges are actually feasible at this time...
390 return false;
Chris Lattner59f0ce22002-05-02 21:18:01 +0000391}
Chris Lattner138a1242001-06-27 23:38:11 +0000392
Chris Lattner2a632552002-04-18 15:13:15 +0000393// visit Implementations - Something changed in this instruction... Either an
Chris Lattner138a1242001-06-27 23:38:11 +0000394// operand made a transition, or the instruction is newly executable. Change
395// the value type of I to reflect these changes if appropriate. This method
396// makes sure to do the following actions:
397//
398// 1. If a phi node merges two constants in, and has conflicting value coming
399// from different branches, or if the PHI node merges in an overdefined
400// value, then the PHI node becomes overdefined.
401// 2. If a phi node merges only constants in, and they all agree on value, the
402// PHI node becomes a constant value equal to that.
403// 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
404// 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
405// 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
406// 6. If a conditional branch has a value that is constant, make the selected
407// destination executable
408// 7. If a conditional branch has a value that is overdefined, make all
409// successors executable.
410//
Chris Lattner138a1242001-06-27 23:38:11 +0000411
Chris Lattner7e708292002-06-25 16:13:24 +0000412void SCCP::visitPHINode(PHINode &PN) {
413 unsigned NumValues = PN.getNumIncomingValues(), i;
Chris Lattner2a632552002-04-18 15:13:15 +0000414 InstVal *OperandIV = 0;
Chris Lattner138a1242001-06-27 23:38:11 +0000415
Chris Lattner2a632552002-04-18 15:13:15 +0000416 // Look at all of the executable operands of the PHI node. If any of them
417 // are overdefined, the PHI becomes overdefined as well. If they are all
418 // constant, and they agree with each other, the PHI becomes the identical
419 // constant. If they are constant and don't agree, the PHI is overdefined.
420 // If there are no executable operands, the PHI remains undefined.
421 //
422 for (i = 0; i < NumValues; ++i) {
Chris Lattner7e708292002-06-25 16:13:24 +0000423 if (isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent())) {
424 InstVal &IV = getValueState(PN.getIncomingValue(i));
Chris Lattner2a632552002-04-18 15:13:15 +0000425 if (IV.isUndefined()) continue; // Doesn't influence PHI node.
426 if (IV.isOverdefined()) { // PHI node becomes overdefined!
Chris Lattner7e708292002-06-25 16:13:24 +0000427 markOverdefined(&PN);
Chris Lattner2a632552002-04-18 15:13:15 +0000428 return;
429 }
Chris Lattner138a1242001-06-27 23:38:11 +0000430
Chris Lattner2a632552002-04-18 15:13:15 +0000431 if (OperandIV == 0) { // Grab the first value...
432 OperandIV = &IV;
433 } else { // Another value is being merged in!
434 // There is already a reachable operand. If we conflict with it,
435 // then the PHI node becomes overdefined. If we agree with it, we
436 // can continue on.
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000437
Chris Lattner2a632552002-04-18 15:13:15 +0000438 // Check to see if there are two different constants merging...
439 if (IV.getConstant() != OperandIV->getConstant()) {
440 // Yes there is. This means the PHI node is not constant.
441 // You must be overdefined poor PHI.
442 //
Chris Lattner7e708292002-06-25 16:13:24 +0000443 markOverdefined(&PN); // The PHI node now becomes overdefined
Chris Lattner2a632552002-04-18 15:13:15 +0000444 return; // I'm done analyzing you
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000445 }
Chris Lattner138a1242001-06-27 23:38:11 +0000446 }
447 }
Chris Lattner138a1242001-06-27 23:38:11 +0000448 }
449
Chris Lattner2a632552002-04-18 15:13:15 +0000450 // If we exited the loop, this means that the PHI node only has constant
451 // arguments that agree with each other(and OperandIV is a pointer to one
452 // of their InstVal's) or OperandIV is null because there are no defined
453 // incoming arguments. If this is the case, the PHI remains undefined.
Chris Lattner138a1242001-06-27 23:38:11 +0000454 //
Chris Lattner2a632552002-04-18 15:13:15 +0000455 if (OperandIV) {
456 assert(OperandIV->isConstant() && "Should only be here for constants!");
Chris Lattner7e708292002-06-25 16:13:24 +0000457 markConstant(&PN, OperandIV->getConstant()); // Aquire operand value
Chris Lattner138a1242001-06-27 23:38:11 +0000458 }
Chris Lattner138a1242001-06-27 23:38:11 +0000459}
460
Chris Lattner7e708292002-06-25 16:13:24 +0000461void SCCP::visitTerminatorInst(TerminatorInst &TI) {
462 std::vector<bool> SuccFeasible(TI.getNumSuccessors());
Chris Lattnerb9a66342002-05-02 21:44:00 +0000463 getFeasibleSuccessors(TI, SuccFeasible);
Chris Lattner138a1242001-06-27 23:38:11 +0000464
Chris Lattnerb9a66342002-05-02 21:44:00 +0000465 // Mark all feasible successors executable...
466 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
Chris Lattner7f9412b2002-05-22 16:07:20 +0000467 if (SuccFeasible[i]) {
Chris Lattner7e708292002-06-25 16:13:24 +0000468 BasicBlock *Succ = TI.getSuccessor(i);
Chris Lattner7f9412b2002-05-22 16:07:20 +0000469 markExecutable(Succ);
470
471 // Visit all of the PHI nodes that merge values from this block...
472 // Because this edge may be new executable, and PHI nodes that used to be
473 // constant now may not be.
474 //
475 for (BasicBlock::iterator I = Succ->begin();
Chris Lattner7e708292002-06-25 16:13:24 +0000476 PHINode *PN = dyn_cast<PHINode>(&*I); ++I)
477 visitPHINode(*PN);
Chris Lattner7f9412b2002-05-22 16:07:20 +0000478 }
Chris Lattner2a632552002-04-18 15:13:15 +0000479}
480
Chris Lattner7e708292002-06-25 16:13:24 +0000481void SCCP::visitUnaryOperator(Instruction &I) {
482 Value *V = I.getOperand(0);
Chris Lattner2a632552002-04-18 15:13:15 +0000483 InstVal &VState = getValueState(V);
484 if (VState.isOverdefined()) { // Inherit overdefinedness of operand
Chris Lattner7e708292002-06-25 16:13:24 +0000485 markOverdefined(&I);
Chris Lattner2a632552002-04-18 15:13:15 +0000486 } else if (VState.isConstant()) { // Propogate constant value
487 Constant *Result = isa<CastInst>(I)
Chris Lattner7e708292002-06-25 16:13:24 +0000488 ? ConstantFoldCastInstruction(VState.getConstant(), I.getType())
489 : ConstantFoldUnaryInstruction(I.getOpcode(), VState.getConstant());
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}