blob: 24d383b280861606bf1be3e3163c92f48a33abf1 [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 Lattner59f0ce22002-05-02 21:18:01 +000037#if 0 // Enable this to get SCCP debug output
38#define DEBUG_SCCP(X) X
39#else
40#define DEBUG_SCCP(X)
41#endif
42
Chris Lattner138a1242001-06-27 23:38:11 +000043// InstVal class - This class represents the different lattice values that an
Chris Lattnerf57b8452002-04-27 06:56:12 +000044// instruction may occupy. It is a simple class with value semantics.
Chris Lattner138a1242001-06-27 23:38:11 +000045//
Chris Lattner0dbfc052002-04-29 21:26:08 +000046namespace {
Chris Lattner138a1242001-06-27 23:38:11 +000047class InstVal {
48 enum {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000049 undefined, // This instruction has no known value
50 constant, // This instruction has a constant value
Chris Lattner138a1242001-06-27 23:38:11 +000051 // Range, // This instruction is known to fall within a range
Chris Lattnere9bb2df2001-12-03 22:26:30 +000052 overdefined // This instruction has an unknown value
53 } LatticeValue; // The current lattice position
54 Constant *ConstantVal; // If Constant value, the current value
Chris Lattner138a1242001-06-27 23:38:11 +000055public:
Chris Lattnere9bb2df2001-12-03 22:26:30 +000056 inline InstVal() : LatticeValue(undefined), ConstantVal(0) {}
Chris Lattner138a1242001-06-27 23:38:11 +000057
58 // markOverdefined - Return true if this is a new status to be in...
59 inline bool markOverdefined() {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000060 if (LatticeValue != overdefined) {
61 LatticeValue = overdefined;
Chris Lattner138a1242001-06-27 23:38:11 +000062 return true;
63 }
64 return false;
65 }
66
67 // markConstant - Return true if this is a new status for us...
Chris Lattnere9bb2df2001-12-03 22:26:30 +000068 inline bool markConstant(Constant *V) {
69 if (LatticeValue != constant) {
70 LatticeValue = constant;
Chris Lattner138a1242001-06-27 23:38:11 +000071 ConstantVal = V;
72 return true;
73 } else {
Chris Lattnerb70d82f2001-09-07 16:43:22 +000074 assert(ConstantVal == V && "Marking constant with different value");
Chris Lattner138a1242001-06-27 23:38:11 +000075 }
76 return false;
77 }
78
Chris Lattnere9bb2df2001-12-03 22:26:30 +000079 inline bool isUndefined() const { return LatticeValue == undefined; }
80 inline bool isConstant() const { return LatticeValue == constant; }
81 inline bool isOverdefined() const { return LatticeValue == overdefined; }
Chris Lattner138a1242001-06-27 23:38:11 +000082
Chris Lattnere9bb2df2001-12-03 22:26:30 +000083 inline Constant *getConstant() const { return ConstantVal; }
Chris Lattner138a1242001-06-27 23:38:11 +000084};
85
Chris Lattner0dbfc052002-04-29 21:26:08 +000086} // end anonymous namespace
Chris Lattner138a1242001-06-27 23:38:11 +000087
88
89//===----------------------------------------------------------------------===//
90// SCCP Class
91//
92// This class does all of the work of Sparse Conditional Constant Propogation.
Chris Lattner138a1242001-06-27 23:38:11 +000093//
Chris Lattner0dbfc052002-04-29 21:26:08 +000094namespace {
95class SCCP : public FunctionPass, public InstVisitor<SCCP> {
Chris Lattner697954c2002-01-20 22:54:45 +000096 std::set<BasicBlock*> BBExecutable;// The basic blocks that are executable
97 std::map<Value*, InstVal> ValueState; // The state each value is in...
Chris Lattner138a1242001-06-27 23:38:11 +000098
Chris Lattner071d0ad2002-05-07 04:29:32 +000099 std::vector<Instruction*> InstWorkList;// The instruction work list
Chris Lattner697954c2002-01-20 22:54:45 +0000100 std::vector<BasicBlock*> BBWorkList; // The BasicBlock work list
Chris Lattner138a1242001-06-27 23:38:11 +0000101public:
102
Chris Lattner0dbfc052002-04-29 21:26:08 +0000103 const char *getPassName() const {
104 return "Sparse Conditional Constant Propogation";
105 }
Chris Lattner138a1242001-06-27 23:38:11 +0000106
Chris Lattner0dbfc052002-04-29 21:26:08 +0000107 // runOnFunction - Run the Sparse Conditional Constant Propogation algorithm,
108 // and return true if the function was modified.
109 //
110 bool runOnFunction(Function *F);
111
112 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerb9a66342002-05-02 21:44:00 +0000113 AU.preservesCFG();
Chris Lattner0dbfc052002-04-29 21:26:08 +0000114 }
115
Chris Lattner138a1242001-06-27 23:38:11 +0000116
117 //===--------------------------------------------------------------------===//
118 // The implementation of this class
119 //
120private:
Chris Lattner2a632552002-04-18 15:13:15 +0000121 friend class InstVisitor<SCCP>; // Allow callbacks from visitor
Chris Lattner138a1242001-06-27 23:38:11 +0000122
123 // markValueOverdefined - Make a value be marked as "constant". If the value
124 // is not already a constant, add it to the instruction work list so that
125 // the users of the instruction are updated later.
126 //
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000127 inline bool markConstant(Instruction *I, Constant *V) {
Chris Lattner59f0ce22002-05-02 21:18:01 +0000128 DEBUG_SCCP(cerr << "markConstant: " << V << " = " << I);
129
Chris Lattner138a1242001-06-27 23:38:11 +0000130 if (ValueState[I].markConstant(V)) {
Chris Lattner071d0ad2002-05-07 04:29:32 +0000131 InstWorkList.push_back(I);
Chris Lattner138a1242001-06-27 23:38:11 +0000132 return true;
133 }
134 return false;
135 }
136
137 // markValueOverdefined - Make a value be marked as "overdefined". If the
138 // value is not already overdefined, add it to the instruction work list so
139 // that the users of the instruction are updated later.
140 //
141 inline bool markOverdefined(Value *V) {
142 if (ValueState[V].markOverdefined()) {
Chris Lattner9636a912001-10-01 16:18:37 +0000143 if (Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner59f0ce22002-05-02 21:18:01 +0000144 DEBUG_SCCP(cerr << "markOverdefined: " << V);
Chris Lattner071d0ad2002-05-07 04:29:32 +0000145 InstWorkList.push_back(I); // Only instructions go on the work list
Chris Lattner138a1242001-06-27 23:38:11 +0000146 }
147 return true;
148 }
149 return false;
150 }
151
152 // getValueState - Return the InstVal object that corresponds to the value.
153 // This function is neccesary because not all values should start out in the
Chris Lattner73e21422002-04-09 19:48:49 +0000154 // underdefined state... Argument's should be overdefined, and
Chris Lattner79df7c02002-03-26 18:01:55 +0000155 // constants should be marked as constants. If a value is not known to be an
Chris Lattner138a1242001-06-27 23:38:11 +0000156 // Instruction object, then use this accessor to get its value from the map.
157 //
158 inline InstVal &getValueState(Value *V) {
Chris Lattner697954c2002-01-20 22:54:45 +0000159 std::map<Value*, InstVal>::iterator I = ValueState.find(V);
Chris Lattner138a1242001-06-27 23:38:11 +0000160 if (I != ValueState.end()) return I->second; // Common case, in the map
161
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000162 if (Constant *CPV = dyn_cast<Constant>(V)) { // Constants are constant
Chris Lattner138a1242001-06-27 23:38:11 +0000163 ValueState[CPV].markConstant(CPV);
Chris Lattner73e21422002-04-09 19:48:49 +0000164 } else if (isa<Argument>(V)) { // Arguments are overdefined
Chris Lattner138a1242001-06-27 23:38:11 +0000165 ValueState[V].markOverdefined();
166 }
167 // All others are underdefined by default...
168 return ValueState[V];
169 }
170
171 // markExecutable - Mark a basic block as executable, adding it to the BB
172 // work list if it is not already executable...
173 //
174 void markExecutable(BasicBlock *BB) {
175 if (BBExecutable.count(BB)) return;
Chris Lattner59f0ce22002-05-02 21:18:01 +0000176 DEBUG_SCCP(cerr << "Marking BB Executable: " << BB);
Chris Lattner138a1242001-06-27 23:38:11 +0000177 BBExecutable.insert(BB); // Basic block is executable!
178 BBWorkList.push_back(BB); // Add the block to the work list!
Chris Lattner618b4a12002-05-20 20:48:03 +0000179
180 // Visit all of the PHI nodes that merge values from this block... Because
181 // this block is newly executable, PHI nodes that used to be constant now
182 // may not be. Note that we only mark PHI nodes that live in blocks that
183 // can execute!
184 //
185 for (Value::use_iterator I = BB->use_begin(), E = BB->use_end(); I != E;++I)
186 if (PHINode *PN = dyn_cast<PHINode>(*I))
187 if (BBExecutable.count(PN->getParent()))
188 visitPHINode(PN);
Chris Lattner138a1242001-06-27 23:38:11 +0000189 }
190
Chris Lattner138a1242001-06-27 23:38:11 +0000191
Chris Lattner2a632552002-04-18 15:13:15 +0000192 // visit implementations - Something changed in this instruction... Either an
Chris Lattnercb056de2001-06-29 23:56:23 +0000193 // operand made a transition, or the instruction is newly executable. Change
194 // the value type of I to reflect these changes if appropriate.
195 //
Chris Lattner2a632552002-04-18 15:13:15 +0000196 void visitPHINode(PHINode *I);
197
198 // Terminators
199 void visitReturnInst(ReturnInst *I) { /*does not have an effect*/ }
Chris Lattnerb9a66342002-05-02 21:44:00 +0000200 void visitTerminatorInst(TerminatorInst *TI);
Chris Lattner2a632552002-04-18 15:13:15 +0000201
202 void visitUnaryOperator(Instruction *I);
203 void visitCastInst(CastInst *I) { visitUnaryOperator(I); }
204 void visitBinaryOperator(Instruction *I);
205 void visitShiftInst(ShiftInst *I) { visitBinaryOperator(I); }
206
207 // Instructions that cannot be folded away...
Chris Lattner59f0ce22002-05-02 21:18:01 +0000208 void visitStoreInst (Instruction *I) { /*returns void*/ }
Chris Lattner2a632552002-04-18 15:13:15 +0000209 void visitMemAccessInst (Instruction *I) { markOverdefined(I); }
210 void visitCallInst (Instruction *I) { markOverdefined(I); }
211 void visitInvokeInst (Instruction *I) { markOverdefined(I); }
212 void visitAllocationInst(Instruction *I) { markOverdefined(I); }
Chris Lattner59f0ce22002-05-02 21:18:01 +0000213 void visitFreeInst (Instruction *I) { /*returns void*/ }
Chris Lattner2a632552002-04-18 15:13:15 +0000214
215 void visitInstruction(Instruction *I) {
216 // If a new instruction is added to LLVM that we don't handle...
217 cerr << "SCCP: Don't know how to handle: " << I;
218 markOverdefined(I); // Just in case
219 }
Chris Lattnercb056de2001-06-29 23:56:23 +0000220
Chris Lattnerb9a66342002-05-02 21:44:00 +0000221 // getFeasibleSuccessors - Return a vector of booleans to indicate which
222 // successors are reachable from a given terminator instruction.
223 //
224 void getFeasibleSuccessors(TerminatorInst *I, std::vector<bool> &Succs);
225
Chris Lattner59f0ce22002-05-02 21:18:01 +0000226 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
227 // block to the 'To' basic block is currently feasible...
228 //
229 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To);
230
Chris Lattnercb056de2001-06-29 23:56:23 +0000231 // OperandChangedState - This method is invoked on all of the users of an
232 // instruction that was just changed state somehow.... Based on this
233 // information, we need to update the specified user of this instruction.
234 //
Chris Lattner59f0ce22002-05-02 21:18:01 +0000235 void OperandChangedState(User *U) {
236 // Only instructions use other variable values!
237 Instruction *I = cast<Instruction>(U);
238 if (!BBExecutable.count(I->getParent())) return;// Inst not executable yet!
239 visit(I);
240 }
Chris Lattnercb056de2001-06-29 23:56:23 +0000241};
Chris Lattner0dbfc052002-04-29 21:26:08 +0000242} // end anonymous namespace
243
244
245// createSCCPPass - This is the public interface to this file...
246//
247Pass *createSCCPPass() {
248 return new SCCP();
249}
250
Chris Lattner138a1242001-06-27 23:38:11 +0000251
252
253//===----------------------------------------------------------------------===//
254// SCCP Class Implementation
255
256
Chris Lattner0dbfc052002-04-29 21:26:08 +0000257// runOnFunction() - Run the Sparse Conditional Constant Propogation algorithm,
258// and return true if the function was modified.
Chris Lattner138a1242001-06-27 23:38:11 +0000259//
Chris Lattner0dbfc052002-04-29 21:26:08 +0000260bool SCCP::runOnFunction(Function *F) {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000261 // Mark the first block of the function as being executable...
Chris Lattner0dbfc052002-04-29 21:26:08 +0000262 markExecutable(F->front());
Chris Lattner138a1242001-06-27 23:38:11 +0000263
264 // Process the work lists until their are empty!
265 while (!BBWorkList.empty() || !InstWorkList.empty()) {
266 // Process the instruction work list...
267 while (!InstWorkList.empty()) {
Chris Lattner071d0ad2002-05-07 04:29:32 +0000268 Instruction *I = InstWorkList.back();
269 InstWorkList.pop_back();
Chris Lattner138a1242001-06-27 23:38:11 +0000270
Chris Lattner59f0ce22002-05-02 21:18:01 +0000271 DEBUG_SCCP(cerr << "\nPopped off I-WL: " << I);
Chris Lattner138a1242001-06-27 23:38:11 +0000272
273
274 // "I" got into the work list because it either made the transition from
275 // bottom to constant, or to Overdefined.
276 //
277 // Update all of the users of this instruction's value...
278 //
279 for_each(I->use_begin(), I->use_end(),
280 bind_obj(this, &SCCP::OperandChangedState));
281 }
282
283 // Process the basic block work list...
284 while (!BBWorkList.empty()) {
285 BasicBlock *BB = BBWorkList.back();
286 BBWorkList.pop_back();
287
Chris Lattner59f0ce22002-05-02 21:18:01 +0000288 DEBUG_SCCP(cerr << "\nPopped off BBWL: " << BB);
Chris Lattner138a1242001-06-27 23:38:11 +0000289
290 // If this block only has a single successor, mark it as executable as
291 // well... if not, terminate the do loop.
292 //
293 if (BB->getTerminator()->getNumSuccessors() == 1)
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000294 markExecutable(BB->getTerminator()->getSuccessor(0));
Chris Lattner138a1242001-06-27 23:38:11 +0000295
Chris Lattner2a632552002-04-18 15:13:15 +0000296 // Notify all instructions in this basic block that they are newly
297 // executable.
298 visit(BB);
Chris Lattner138a1242001-06-27 23:38:11 +0000299 }
300 }
301
Chris Lattner904ec282002-05-02 21:49:50 +0000302#if 0
Chris Lattner0dbfc052002-04-29 21:26:08 +0000303 for (Function::iterator BBI = F->begin(), BBEnd = F->end();
Chris Lattner79df7c02002-03-26 18:01:55 +0000304 BBI != BBEnd; ++BBI)
Chris Lattner138a1242001-06-27 23:38:11 +0000305 if (!BBExecutable.count(*BBI))
306 cerr << "BasicBlock Dead:" << *BBI;
307#endif
308
309
Chris Lattnerf57b8452002-04-27 06:56:12 +0000310 // Iterate over all of the instructions in a function, replacing them with
Chris Lattner138a1242001-06-27 23:38:11 +0000311 // constants if we have found them to be of constant values.
312 //
313 bool MadeChanges = false;
Chris Lattner0dbfc052002-04-29 21:26:08 +0000314 for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI) {
315 BasicBlock *BB = *FI;
Chris Lattner221d6882002-02-12 21:07:25 +0000316 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
317 Instruction *Inst = *BI;
318 InstVal &IV = ValueState[Inst];
319 if (IV.isConstant()) {
320 Constant *Const = IV.getConstant();
Chris Lattner618b4a12002-05-20 20:48:03 +0000321 DEBUG_SCCP(cerr << "Constant: " << Const << " = " << Inst);
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000322
Chris Lattner221d6882002-02-12 21:07:25 +0000323 // Replaces all of the uses of a variable with uses of the constant.
324 Inst->replaceAllUsesWith(Const);
Chris Lattner138a1242001-06-27 23:38:11 +0000325
Chris Lattner0e9c5152002-05-02 20:32:51 +0000326 // Remove the operator from the list of definitions... and delete it.
327 delete BB->getInstList().remove(BI);
Chris Lattner138a1242001-06-27 23:38:11 +0000328
Chris Lattner221d6882002-02-12 21:07:25 +0000329 // Hey, we just changed something!
330 MadeChanges = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000331 ++NumInstRemoved;
Chris Lattnerb9a66342002-05-02 21:44:00 +0000332 } else {
333 ++BI;
Chris Lattner221d6882002-02-12 21:07:25 +0000334 }
Chris Lattner138a1242001-06-27 23:38:11 +0000335 }
336 }
337
Chris Lattner59f0ce22002-05-02 21:18:01 +0000338 // Reset state so that the next invocation will have empty data structures
Chris Lattner0dbfc052002-04-29 21:26:08 +0000339 BBExecutable.clear();
340 ValueState.clear();
341
Chris Lattnerb70d82f2001-09-07 16:43:22 +0000342 return MadeChanges;
Chris Lattner138a1242001-06-27 23:38:11 +0000343}
344
Chris Lattnerb9a66342002-05-02 21:44:00 +0000345
346// getFeasibleSuccessors - Return a vector of booleans to indicate which
347// successors are reachable from a given terminator instruction.
348//
349void SCCP::getFeasibleSuccessors(TerminatorInst *TI, std::vector<bool> &Succs) {
350 assert(Succs.size() == TI->getNumSuccessors() && "Succs vector wrong size!");
351 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
352 if (BI->isUnconditional()) {
353 Succs[0] = true;
354 } else {
355 InstVal &BCValue = getValueState(BI->getCondition());
356 if (BCValue.isOverdefined()) {
357 // Overdefined condition variables mean the branch could go either way.
358 Succs[0] = Succs[1] = true;
359 } else if (BCValue.isConstant()) {
360 // Constant condition variables mean the branch can only go a single way
361 Succs[BCValue.getConstant() == ConstantBool::False] = true;
362 }
363 }
364 } else if (InvokeInst *II = dyn_cast<InvokeInst>(TI)) {
365 // Invoke instructions successors are always executable.
366 Succs[0] = Succs[1] = true;
367 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
368 InstVal &SCValue = getValueState(SI->getCondition());
369 if (SCValue.isOverdefined()) { // Overdefined condition?
370 // All destinations are executable!
371 Succs.assign(TI->getNumSuccessors(), true);
372 } else if (SCValue.isConstant()) {
373 Constant *CPV = SCValue.getConstant();
374 // Make sure to skip the "default value" which isn't a value
375 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) {
376 if (SI->getSuccessorValue(i) == CPV) {// Found the right branch...
377 Succs[i] = true;
378 return;
379 }
380 }
381
382 // Constant value not equal to any of the branches... must execute
383 // default branch then...
384 Succs[0] = true;
385 }
386 } else {
387 cerr << "SCCP: Don't know how to handle: " << TI;
388 Succs.assign(TI->getNumSuccessors(), true);
389 }
390}
391
392
Chris Lattner59f0ce22002-05-02 21:18:01 +0000393// isEdgeFeasible - Return true if the control flow edge from the 'From' basic
394// block to the 'To' basic block is currently feasible...
395//
396bool SCCP::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
397 assert(BBExecutable.count(To) && "Dest should always be alive!");
398
399 // Make sure the source basic block is executable!!
400 if (!BBExecutable.count(From)) return false;
401
Chris Lattnerb9a66342002-05-02 21:44:00 +0000402 // Check to make sure this edge itself is actually feasible now...
403 TerminatorInst *FT = From->getTerminator();
404 std::vector<bool> SuccFeasible(FT->getNumSuccessors());
405 getFeasibleSuccessors(FT, SuccFeasible);
406
407 // Check all edges from From to To. If any are feasible, return true.
408 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
409 if (FT->getSuccessor(i) == To && SuccFeasible[i])
410 return true;
411
412 // Otherwise, none of the edges are actually feasible at this time...
413 return false;
Chris Lattner59f0ce22002-05-02 21:18:01 +0000414}
Chris Lattner138a1242001-06-27 23:38:11 +0000415
Chris Lattner2a632552002-04-18 15:13:15 +0000416// visit Implementations - Something changed in this instruction... Either an
Chris Lattner138a1242001-06-27 23:38:11 +0000417// operand made a transition, or the instruction is newly executable. Change
418// the value type of I to reflect these changes if appropriate. This method
419// makes sure to do the following actions:
420//
421// 1. If a phi node merges two constants in, and has conflicting value coming
422// from different branches, or if the PHI node merges in an overdefined
423// value, then the PHI node becomes overdefined.
424// 2. If a phi node merges only constants in, and they all agree on value, the
425// PHI node becomes a constant value equal to that.
426// 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
427// 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
428// 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
429// 6. If a conditional branch has a value that is constant, make the selected
430// destination executable
431// 7. If a conditional branch has a value that is overdefined, make all
432// successors executable.
433//
Chris Lattner138a1242001-06-27 23:38:11 +0000434
Chris Lattner2a632552002-04-18 15:13:15 +0000435void SCCP::visitPHINode(PHINode *PN) {
436 unsigned NumValues = PN->getNumIncomingValues(), i;
437 InstVal *OperandIV = 0;
Chris Lattner138a1242001-06-27 23:38:11 +0000438
Chris Lattner2a632552002-04-18 15:13:15 +0000439 // Look at all of the executable operands of the PHI node. If any of them
440 // are overdefined, the PHI becomes overdefined as well. If they are all
441 // constant, and they agree with each other, the PHI becomes the identical
442 // constant. If they are constant and don't agree, the PHI is overdefined.
443 // If there are no executable operands, the PHI remains undefined.
444 //
445 for (i = 0; i < NumValues; ++i) {
Chris Lattner59f0ce22002-05-02 21:18:01 +0000446 if (isEdgeFeasible(PN->getIncomingBlock(i), PN->getParent())) {
Chris Lattner2a632552002-04-18 15:13:15 +0000447 InstVal &IV = getValueState(PN->getIncomingValue(i));
448 if (IV.isUndefined()) continue; // Doesn't influence PHI node.
449 if (IV.isOverdefined()) { // PHI node becomes overdefined!
450 markOverdefined(PN);
451 return;
452 }
Chris Lattner138a1242001-06-27 23:38:11 +0000453
Chris Lattner2a632552002-04-18 15:13:15 +0000454 if (OperandIV == 0) { // Grab the first value...
455 OperandIV = &IV;
456 } else { // Another value is being merged in!
457 // There is already a reachable operand. If we conflict with it,
458 // then the PHI node becomes overdefined. If we agree with it, we
459 // can continue on.
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000460
Chris Lattner2a632552002-04-18 15:13:15 +0000461 // Check to see if there are two different constants merging...
462 if (IV.getConstant() != OperandIV->getConstant()) {
463 // Yes there is. This means the PHI node is not constant.
464 // You must be overdefined poor PHI.
465 //
466 markOverdefined(PN); // The PHI node now becomes overdefined
467 return; // I'm done analyzing you
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000468 }
Chris Lattner138a1242001-06-27 23:38:11 +0000469 }
470 }
Chris Lattner138a1242001-06-27 23:38:11 +0000471 }
472
Chris Lattner2a632552002-04-18 15:13:15 +0000473 // If we exited the loop, this means that the PHI node only has constant
474 // arguments that agree with each other(and OperandIV is a pointer to one
475 // of their InstVal's) or OperandIV is null because there are no defined
476 // incoming arguments. If this is the case, the PHI remains undefined.
Chris Lattner138a1242001-06-27 23:38:11 +0000477 //
Chris Lattner2a632552002-04-18 15:13:15 +0000478 if (OperandIV) {
479 assert(OperandIV->isConstant() && "Should only be here for constants!");
480 markConstant(PN, OperandIV->getConstant()); // Aquire operand value
Chris Lattner138a1242001-06-27 23:38:11 +0000481 }
Chris Lattner138a1242001-06-27 23:38:11 +0000482}
483
Chris Lattnerb9a66342002-05-02 21:44:00 +0000484void SCCP::visitTerminatorInst(TerminatorInst *TI) {
485 std::vector<bool> SuccFeasible(TI->getNumSuccessors());
486 getFeasibleSuccessors(TI, SuccFeasible);
Chris Lattner138a1242001-06-27 23:38:11 +0000487
Chris Lattnerb9a66342002-05-02 21:44:00 +0000488 // Mark all feasible successors executable...
489 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
490 if (SuccFeasible[i])
491 markExecutable(TI->getSuccessor(i));
Chris Lattner2a632552002-04-18 15:13:15 +0000492}
493
494void SCCP::visitUnaryOperator(Instruction *I) {
495 Value *V = I->getOperand(0);
496 InstVal &VState = getValueState(V);
497 if (VState.isOverdefined()) { // Inherit overdefinedness of operand
498 markOverdefined(I);
499 } else if (VState.isConstant()) { // Propogate constant value
500 Constant *Result = isa<CastInst>(I)
501 ? ConstantFoldCastInstruction(VState.getConstant(), I->getType())
502 : ConstantFoldUnaryInstruction(I->getOpcode(), VState.getConstant());
503
504 if (Result) {
505 // This instruction constant folds!
506 markConstant(I, Result);
507 } else {
508 markOverdefined(I); // Don't know how to fold this instruction. :(
509 }
510 }
511}
512
513// Handle BinaryOperators and Shift Instructions...
514void SCCP::visitBinaryOperator(Instruction *I) {
515 InstVal &V1State = getValueState(I->getOperand(0));
516 InstVal &V2State = getValueState(I->getOperand(1));
517 if (V1State.isOverdefined() || V2State.isOverdefined()) {
518 markOverdefined(I);
519 } else if (V1State.isConstant() && V2State.isConstant()) {
Chris Lattner4c1061f2002-05-06 03:01:37 +0000520 Constant *Result = 0;
521 if (isa<BinaryOperator>(I))
522 Result = ConstantFoldBinaryInstruction(I->getOpcode(),
523 V1State.getConstant(),
524 V2State.getConstant());
525 else if (isa<ShiftInst>(I))
526 Result = ConstantFoldShiftInstruction(I->getOpcode(),
527 V1State.getConstant(),
528 V2State.getConstant());
Chris Lattner2a632552002-04-18 15:13:15 +0000529 if (Result)
Chris Lattner0e9c5152002-05-02 20:32:51 +0000530 markConstant(I, Result); // This instruction constant folds!
Chris Lattner2a632552002-04-18 15:13:15 +0000531 else
532 markOverdefined(I); // Don't know how to fold this instruction. :(
533 }
534}