blob: 2d8f3ee614279e88e1e2fa433397ea27e385240e [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 Lattner59b6b8e2002-01-21 23:17:48 +000018#include "llvm/Transforms/Scalar/ConstantProp.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 Lattner138a1242001-06-27 23:38:11 +000029#include <algorithm>
Chris Lattner138a1242001-06-27 23:38:11 +000030#include <set>
Chris Lattner697954c2002-01-20 22:54:45 +000031#include <iostream>
32using std::cerr;
Chris Lattner138a1242001-06-27 23:38:11 +000033
Chris Lattner59f0ce22002-05-02 21:18:01 +000034#if 0 // Enable this to get SCCP debug output
35#define DEBUG_SCCP(X) X
36#else
37#define DEBUG_SCCP(X)
38#endif
39
Chris Lattner138a1242001-06-27 23:38:11 +000040// InstVal class - This class represents the different lattice values that an
Chris Lattnerf57b8452002-04-27 06:56:12 +000041// instruction may occupy. It is a simple class with value semantics.
Chris Lattner138a1242001-06-27 23:38:11 +000042//
Chris Lattner0dbfc052002-04-29 21:26:08 +000043namespace {
Chris Lattner138a1242001-06-27 23:38:11 +000044class InstVal {
45 enum {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000046 undefined, // This instruction has no known value
47 constant, // This instruction has a constant value
Chris Lattner138a1242001-06-27 23:38:11 +000048 // Range, // This instruction is known to fall within a range
Chris Lattnere9bb2df2001-12-03 22:26:30 +000049 overdefined // This instruction has an unknown value
50 } LatticeValue; // The current lattice position
51 Constant *ConstantVal; // If Constant value, the current value
Chris Lattner138a1242001-06-27 23:38:11 +000052public:
Chris Lattnere9bb2df2001-12-03 22:26:30 +000053 inline InstVal() : LatticeValue(undefined), ConstantVal(0) {}
Chris Lattner138a1242001-06-27 23:38:11 +000054
55 // markOverdefined - Return true if this is a new status to be in...
56 inline bool markOverdefined() {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000057 if (LatticeValue != overdefined) {
58 LatticeValue = overdefined;
Chris Lattner138a1242001-06-27 23:38:11 +000059 return true;
60 }
61 return false;
62 }
63
64 // markConstant - Return true if this is a new status for us...
Chris Lattnere9bb2df2001-12-03 22:26:30 +000065 inline bool markConstant(Constant *V) {
66 if (LatticeValue != constant) {
67 LatticeValue = constant;
Chris Lattner138a1242001-06-27 23:38:11 +000068 ConstantVal = V;
69 return true;
70 } else {
Chris Lattnerb70d82f2001-09-07 16:43:22 +000071 assert(ConstantVal == V && "Marking constant with different value");
Chris Lattner138a1242001-06-27 23:38:11 +000072 }
73 return false;
74 }
75
Chris Lattnere9bb2df2001-12-03 22:26:30 +000076 inline bool isUndefined() const { return LatticeValue == undefined; }
77 inline bool isConstant() const { return LatticeValue == constant; }
78 inline bool isOverdefined() const { return LatticeValue == overdefined; }
Chris Lattner138a1242001-06-27 23:38:11 +000079
Chris Lattnere9bb2df2001-12-03 22:26:30 +000080 inline Constant *getConstant() const { return ConstantVal; }
Chris Lattner138a1242001-06-27 23:38:11 +000081};
82
Chris Lattner0dbfc052002-04-29 21:26:08 +000083} // end anonymous namespace
Chris Lattner138a1242001-06-27 23:38:11 +000084
85
86//===----------------------------------------------------------------------===//
87// SCCP Class
88//
89// This class does all of the work of Sparse Conditional Constant Propogation.
Chris Lattner138a1242001-06-27 23:38:11 +000090//
Chris Lattner0dbfc052002-04-29 21:26:08 +000091namespace {
92class SCCP : public FunctionPass, public InstVisitor<SCCP> {
Chris Lattner697954c2002-01-20 22:54:45 +000093 std::set<BasicBlock*> BBExecutable;// The basic blocks that are executable
94 std::map<Value*, InstVal> ValueState; // The state each value is in...
Chris Lattner138a1242001-06-27 23:38:11 +000095
Chris Lattner071d0ad2002-05-07 04:29:32 +000096 std::vector<Instruction*> InstWorkList;// The instruction work list
Chris Lattner697954c2002-01-20 22:54:45 +000097 std::vector<BasicBlock*> BBWorkList; // The BasicBlock work list
Chris Lattner138a1242001-06-27 23:38:11 +000098public:
99
Chris Lattner0dbfc052002-04-29 21:26:08 +0000100 const char *getPassName() const {
101 return "Sparse Conditional Constant Propogation";
102 }
Chris Lattner138a1242001-06-27 23:38:11 +0000103
Chris Lattner0dbfc052002-04-29 21:26:08 +0000104 // runOnFunction - Run the Sparse Conditional Constant Propogation algorithm,
105 // and return true if the function was modified.
106 //
107 bool runOnFunction(Function *F);
108
109 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerb9a66342002-05-02 21:44:00 +0000110 AU.preservesCFG();
Chris Lattner0dbfc052002-04-29 21:26:08 +0000111 }
112
Chris Lattner138a1242001-06-27 23:38:11 +0000113
114 //===--------------------------------------------------------------------===//
115 // The implementation of this class
116 //
117private:
Chris Lattner2a632552002-04-18 15:13:15 +0000118 friend class InstVisitor<SCCP>; // Allow callbacks from visitor
Chris Lattner138a1242001-06-27 23:38:11 +0000119
120 // markValueOverdefined - Make a value be marked as "constant". If the value
121 // is not already a constant, add it to the instruction work list so that
122 // the users of the instruction are updated later.
123 //
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000124 inline bool markConstant(Instruction *I, Constant *V) {
Chris Lattner59f0ce22002-05-02 21:18:01 +0000125 DEBUG_SCCP(cerr << "markConstant: " << V << " = " << I);
126
Chris Lattner138a1242001-06-27 23:38:11 +0000127 if (ValueState[I].markConstant(V)) {
Chris Lattner071d0ad2002-05-07 04:29:32 +0000128 InstWorkList.push_back(I);
Chris Lattner138a1242001-06-27 23:38:11 +0000129 return true;
130 }
131 return false;
132 }
133
134 // markValueOverdefined - Make a value be marked as "overdefined". If the
135 // value is not already overdefined, add it to the instruction work list so
136 // that the users of the instruction are updated later.
137 //
138 inline bool markOverdefined(Value *V) {
139 if (ValueState[V].markOverdefined()) {
Chris Lattner9636a912001-10-01 16:18:37 +0000140 if (Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner59f0ce22002-05-02 21:18:01 +0000141 DEBUG_SCCP(cerr << "markOverdefined: " << V);
Chris Lattner071d0ad2002-05-07 04:29:32 +0000142 InstWorkList.push_back(I); // Only instructions go on the work list
Chris Lattner138a1242001-06-27 23:38:11 +0000143 }
144 return true;
145 }
146 return false;
147 }
148
149 // getValueState - Return the InstVal object that corresponds to the value.
150 // This function is neccesary because not all values should start out in the
Chris Lattner73e21422002-04-09 19:48:49 +0000151 // underdefined state... Argument's should be overdefined, and
Chris Lattner79df7c02002-03-26 18:01:55 +0000152 // constants should be marked as constants. If a value is not known to be an
Chris Lattner138a1242001-06-27 23:38:11 +0000153 // Instruction object, then use this accessor to get its value from the map.
154 //
155 inline InstVal &getValueState(Value *V) {
Chris Lattner697954c2002-01-20 22:54:45 +0000156 std::map<Value*, InstVal>::iterator I = ValueState.find(V);
Chris Lattner138a1242001-06-27 23:38:11 +0000157 if (I != ValueState.end()) return I->second; // Common case, in the map
158
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000159 if (Constant *CPV = dyn_cast<Constant>(V)) { // Constants are constant
Chris Lattner138a1242001-06-27 23:38:11 +0000160 ValueState[CPV].markConstant(CPV);
Chris Lattner73e21422002-04-09 19:48:49 +0000161 } else if (isa<Argument>(V)) { // Arguments are overdefined
Chris Lattner138a1242001-06-27 23:38:11 +0000162 ValueState[V].markOverdefined();
163 }
164 // All others are underdefined by default...
165 return ValueState[V];
166 }
167
168 // markExecutable - Mark a basic block as executable, adding it to the BB
169 // work list if it is not already executable...
170 //
171 void markExecutable(BasicBlock *BB) {
172 if (BBExecutable.count(BB)) return;
Chris Lattner59f0ce22002-05-02 21:18:01 +0000173 DEBUG_SCCP(cerr << "Marking BB Executable: " << BB);
Chris Lattner138a1242001-06-27 23:38:11 +0000174 BBExecutable.insert(BB); // Basic block is executable!
175 BBWorkList.push_back(BB); // Add the block to the work list!
176 }
177
Chris Lattner138a1242001-06-27 23:38:11 +0000178
Chris Lattner2a632552002-04-18 15:13:15 +0000179 // visit implementations - Something changed in this instruction... Either an
Chris Lattnercb056de2001-06-29 23:56:23 +0000180 // operand made a transition, or the instruction is newly executable. Change
181 // the value type of I to reflect these changes if appropriate.
182 //
Chris Lattner2a632552002-04-18 15:13:15 +0000183 void visitPHINode(PHINode *I);
184
185 // Terminators
186 void visitReturnInst(ReturnInst *I) { /*does not have an effect*/ }
Chris Lattnerb9a66342002-05-02 21:44:00 +0000187 void visitTerminatorInst(TerminatorInst *TI);
Chris Lattner2a632552002-04-18 15:13:15 +0000188
189 void visitUnaryOperator(Instruction *I);
190 void visitCastInst(CastInst *I) { visitUnaryOperator(I); }
191 void visitBinaryOperator(Instruction *I);
192 void visitShiftInst(ShiftInst *I) { visitBinaryOperator(I); }
193
194 // Instructions that cannot be folded away...
Chris Lattner59f0ce22002-05-02 21:18:01 +0000195 void visitStoreInst (Instruction *I) { /*returns void*/ }
Chris Lattner2a632552002-04-18 15:13:15 +0000196 void visitMemAccessInst (Instruction *I) { markOverdefined(I); }
197 void visitCallInst (Instruction *I) { markOverdefined(I); }
198 void visitInvokeInst (Instruction *I) { markOverdefined(I); }
199 void visitAllocationInst(Instruction *I) { markOverdefined(I); }
Chris Lattner59f0ce22002-05-02 21:18:01 +0000200 void visitFreeInst (Instruction *I) { /*returns void*/ }
Chris Lattner2a632552002-04-18 15:13:15 +0000201
202 void visitInstruction(Instruction *I) {
203 // If a new instruction is added to LLVM that we don't handle...
204 cerr << "SCCP: Don't know how to handle: " << I;
205 markOverdefined(I); // Just in case
206 }
Chris Lattnercb056de2001-06-29 23:56:23 +0000207
Chris Lattnerb9a66342002-05-02 21:44:00 +0000208 // getFeasibleSuccessors - Return a vector of booleans to indicate which
209 // successors are reachable from a given terminator instruction.
210 //
211 void getFeasibleSuccessors(TerminatorInst *I, std::vector<bool> &Succs);
212
Chris Lattner59f0ce22002-05-02 21:18:01 +0000213 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
214 // block to the 'To' basic block is currently feasible...
215 //
216 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To);
217
Chris Lattnercb056de2001-06-29 23:56:23 +0000218 // OperandChangedState - This method is invoked on all of the users of an
219 // instruction that was just changed state somehow.... Based on this
220 // information, we need to update the specified user of this instruction.
221 //
Chris Lattner59f0ce22002-05-02 21:18:01 +0000222 void OperandChangedState(User *U) {
223 // Only instructions use other variable values!
224 Instruction *I = cast<Instruction>(U);
225 if (!BBExecutable.count(I->getParent())) return;// Inst not executable yet!
226 visit(I);
227 }
Chris Lattnercb056de2001-06-29 23:56:23 +0000228};
Chris Lattner0dbfc052002-04-29 21:26:08 +0000229} // end anonymous namespace
230
231
232// createSCCPPass - This is the public interface to this file...
233//
234Pass *createSCCPPass() {
235 return new SCCP();
236}
237
Chris Lattner138a1242001-06-27 23:38:11 +0000238
239
240//===----------------------------------------------------------------------===//
241// SCCP Class Implementation
242
243
Chris Lattner0dbfc052002-04-29 21:26:08 +0000244// runOnFunction() - Run the Sparse Conditional Constant Propogation algorithm,
245// and return true if the function was modified.
Chris Lattner138a1242001-06-27 23:38:11 +0000246//
Chris Lattner0dbfc052002-04-29 21:26:08 +0000247bool SCCP::runOnFunction(Function *F) {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000248 // Mark the first block of the function as being executable...
Chris Lattner0dbfc052002-04-29 21:26:08 +0000249 markExecutable(F->front());
Chris Lattner138a1242001-06-27 23:38:11 +0000250
251 // Process the work lists until their are empty!
252 while (!BBWorkList.empty() || !InstWorkList.empty()) {
253 // Process the instruction work list...
254 while (!InstWorkList.empty()) {
Chris Lattner071d0ad2002-05-07 04:29:32 +0000255 Instruction *I = InstWorkList.back();
256 InstWorkList.pop_back();
Chris Lattner138a1242001-06-27 23:38:11 +0000257
Chris Lattner59f0ce22002-05-02 21:18:01 +0000258 DEBUG_SCCP(cerr << "\nPopped off I-WL: " << I);
Chris Lattner138a1242001-06-27 23:38:11 +0000259
260
261 // "I" got into the work list because it either made the transition from
262 // bottom to constant, or to Overdefined.
263 //
264 // Update all of the users of this instruction's value...
265 //
266 for_each(I->use_begin(), I->use_end(),
267 bind_obj(this, &SCCP::OperandChangedState));
268 }
269
270 // Process the basic block work list...
271 while (!BBWorkList.empty()) {
272 BasicBlock *BB = BBWorkList.back();
273 BBWorkList.pop_back();
274
Chris Lattner59f0ce22002-05-02 21:18:01 +0000275 DEBUG_SCCP(cerr << "\nPopped off BBWL: " << BB);
Chris Lattner138a1242001-06-27 23:38:11 +0000276
277 // If this block only has a single successor, mark it as executable as
278 // well... if not, terminate the do loop.
279 //
280 if (BB->getTerminator()->getNumSuccessors() == 1)
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000281 markExecutable(BB->getTerminator()->getSuccessor(0));
Chris Lattner138a1242001-06-27 23:38:11 +0000282
Chris Lattner2a632552002-04-18 15:13:15 +0000283 // Notify all instructions in this basic block that they are newly
284 // executable.
285 visit(BB);
Chris Lattner138a1242001-06-27 23:38:11 +0000286 }
287 }
288
Chris Lattner904ec282002-05-02 21:49:50 +0000289#if 0
Chris Lattner0dbfc052002-04-29 21:26:08 +0000290 for (Function::iterator BBI = F->begin(), BBEnd = F->end();
Chris Lattner79df7c02002-03-26 18:01:55 +0000291 BBI != BBEnd; ++BBI)
Chris Lattner138a1242001-06-27 23:38:11 +0000292 if (!BBExecutable.count(*BBI))
293 cerr << "BasicBlock Dead:" << *BBI;
294#endif
295
296
Chris Lattnerf57b8452002-04-27 06:56:12 +0000297 // Iterate over all of the instructions in a function, replacing them with
Chris Lattner138a1242001-06-27 23:38:11 +0000298 // constants if we have found them to be of constant values.
299 //
300 bool MadeChanges = false;
Chris Lattner0dbfc052002-04-29 21:26:08 +0000301 for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI) {
302 BasicBlock *BB = *FI;
Chris Lattner221d6882002-02-12 21:07:25 +0000303 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
304 Instruction *Inst = *BI;
305 InstVal &IV = ValueState[Inst];
306 if (IV.isConstant()) {
307 Constant *Const = IV.getConstant();
Chris Lattner59f0ce22002-05-02 21:18:01 +0000308 DEBUG_SCCP(cerr << "Constant: " << Inst << " is: " << Const);
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000309
Chris Lattner221d6882002-02-12 21:07:25 +0000310 // Replaces all of the uses of a variable with uses of the constant.
311 Inst->replaceAllUsesWith(Const);
Chris Lattner138a1242001-06-27 23:38:11 +0000312
Chris Lattner0e9c5152002-05-02 20:32:51 +0000313 // Remove the operator from the list of definitions... and delete it.
314 delete BB->getInstList().remove(BI);
Chris Lattner138a1242001-06-27 23:38:11 +0000315
Chris Lattner221d6882002-02-12 21:07:25 +0000316 // Hey, we just changed something!
317 MadeChanges = true;
Chris Lattnerb9a66342002-05-02 21:44:00 +0000318 } else {
319 ++BI;
Chris Lattner221d6882002-02-12 21:07:25 +0000320 }
Chris Lattner138a1242001-06-27 23:38:11 +0000321 }
322 }
323
Chris Lattner59f0ce22002-05-02 21:18:01 +0000324 // Reset state so that the next invocation will have empty data structures
Chris Lattner0dbfc052002-04-29 21:26:08 +0000325 BBExecutable.clear();
326 ValueState.clear();
327
Chris Lattnerb70d82f2001-09-07 16:43:22 +0000328 return MadeChanges;
Chris Lattner138a1242001-06-27 23:38:11 +0000329}
330
Chris Lattnerb9a66342002-05-02 21:44:00 +0000331
332// getFeasibleSuccessors - Return a vector of booleans to indicate which
333// successors are reachable from a given terminator instruction.
334//
335void SCCP::getFeasibleSuccessors(TerminatorInst *TI, std::vector<bool> &Succs) {
336 assert(Succs.size() == TI->getNumSuccessors() && "Succs vector wrong size!");
337 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
338 if (BI->isUnconditional()) {
339 Succs[0] = true;
340 } else {
341 InstVal &BCValue = getValueState(BI->getCondition());
342 if (BCValue.isOverdefined()) {
343 // Overdefined condition variables mean the branch could go either way.
344 Succs[0] = Succs[1] = true;
345 } else if (BCValue.isConstant()) {
346 // Constant condition variables mean the branch can only go a single way
347 Succs[BCValue.getConstant() == ConstantBool::False] = true;
348 }
349 }
350 } else if (InvokeInst *II = dyn_cast<InvokeInst>(TI)) {
351 // Invoke instructions successors are always executable.
352 Succs[0] = Succs[1] = true;
353 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
354 InstVal &SCValue = getValueState(SI->getCondition());
355 if (SCValue.isOverdefined()) { // Overdefined condition?
356 // All destinations are executable!
357 Succs.assign(TI->getNumSuccessors(), true);
358 } else if (SCValue.isConstant()) {
359 Constant *CPV = SCValue.getConstant();
360 // Make sure to skip the "default value" which isn't a value
361 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) {
362 if (SI->getSuccessorValue(i) == CPV) {// Found the right branch...
363 Succs[i] = true;
364 return;
365 }
366 }
367
368 // Constant value not equal to any of the branches... must execute
369 // default branch then...
370 Succs[0] = true;
371 }
372 } else {
373 cerr << "SCCP: Don't know how to handle: " << TI;
374 Succs.assign(TI->getNumSuccessors(), true);
375 }
376}
377
378
Chris Lattner59f0ce22002-05-02 21:18:01 +0000379// isEdgeFeasible - Return true if the control flow edge from the 'From' basic
380// block to the 'To' basic block is currently feasible...
381//
382bool SCCP::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
383 assert(BBExecutable.count(To) && "Dest should always be alive!");
384
385 // Make sure the source basic block is executable!!
386 if (!BBExecutable.count(From)) return false;
387
Chris Lattnerb9a66342002-05-02 21:44:00 +0000388 // Check to make sure this edge itself is actually feasible now...
389 TerminatorInst *FT = From->getTerminator();
390 std::vector<bool> SuccFeasible(FT->getNumSuccessors());
391 getFeasibleSuccessors(FT, SuccFeasible);
392
393 // Check all edges from From to To. If any are feasible, return true.
394 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
395 if (FT->getSuccessor(i) == To && SuccFeasible[i])
396 return true;
397
398 // Otherwise, none of the edges are actually feasible at this time...
399 return false;
Chris Lattner59f0ce22002-05-02 21:18:01 +0000400}
Chris Lattner138a1242001-06-27 23:38:11 +0000401
Chris Lattner2a632552002-04-18 15:13:15 +0000402// visit Implementations - Something changed in this instruction... Either an
Chris Lattner138a1242001-06-27 23:38:11 +0000403// operand made a transition, or the instruction is newly executable. Change
404// the value type of I to reflect these changes if appropriate. This method
405// makes sure to do the following actions:
406//
407// 1. If a phi node merges two constants in, and has conflicting value coming
408// from different branches, or if the PHI node merges in an overdefined
409// value, then the PHI node becomes overdefined.
410// 2. If a phi node merges only constants in, and they all agree on value, the
411// PHI node becomes a constant value equal to that.
412// 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
413// 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
414// 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
415// 6. If a conditional branch has a value that is constant, make the selected
416// destination executable
417// 7. If a conditional branch has a value that is overdefined, make all
418// successors executable.
419//
Chris Lattner138a1242001-06-27 23:38:11 +0000420
Chris Lattner2a632552002-04-18 15:13:15 +0000421void SCCP::visitPHINode(PHINode *PN) {
422 unsigned NumValues = PN->getNumIncomingValues(), i;
423 InstVal *OperandIV = 0;
Chris Lattner138a1242001-06-27 23:38:11 +0000424
Chris Lattner2a632552002-04-18 15:13:15 +0000425 // Look at all of the executable operands of the PHI node. If any of them
426 // are overdefined, the PHI becomes overdefined as well. If they are all
427 // constant, and they agree with each other, the PHI becomes the identical
428 // constant. If they are constant and don't agree, the PHI is overdefined.
429 // If there are no executable operands, the PHI remains undefined.
430 //
431 for (i = 0; i < NumValues; ++i) {
Chris Lattner59f0ce22002-05-02 21:18:01 +0000432 if (isEdgeFeasible(PN->getIncomingBlock(i), PN->getParent())) {
Chris Lattner2a632552002-04-18 15:13:15 +0000433 InstVal &IV = getValueState(PN->getIncomingValue(i));
434 if (IV.isUndefined()) continue; // Doesn't influence PHI node.
435 if (IV.isOverdefined()) { // PHI node becomes overdefined!
436 markOverdefined(PN);
437 return;
438 }
Chris Lattner138a1242001-06-27 23:38:11 +0000439
Chris Lattner2a632552002-04-18 15:13:15 +0000440 if (OperandIV == 0) { // Grab the first value...
441 OperandIV = &IV;
442 } else { // Another value is being merged in!
443 // There is already a reachable operand. If we conflict with it,
444 // then the PHI node becomes overdefined. If we agree with it, we
445 // can continue on.
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000446
Chris Lattner2a632552002-04-18 15:13:15 +0000447 // Check to see if there are two different constants merging...
448 if (IV.getConstant() != OperandIV->getConstant()) {
449 // Yes there is. This means the PHI node is not constant.
450 // You must be overdefined poor PHI.
451 //
452 markOverdefined(PN); // The PHI node now becomes overdefined
453 return; // I'm done analyzing you
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000454 }
Chris Lattner138a1242001-06-27 23:38:11 +0000455 }
456 }
Chris Lattner138a1242001-06-27 23:38:11 +0000457 }
458
Chris Lattner2a632552002-04-18 15:13:15 +0000459 // If we exited the loop, this means that the PHI node only has constant
460 // arguments that agree with each other(and OperandIV is a pointer to one
461 // of their InstVal's) or OperandIV is null because there are no defined
462 // incoming arguments. If this is the case, the PHI remains undefined.
Chris Lattner138a1242001-06-27 23:38:11 +0000463 //
Chris Lattner2a632552002-04-18 15:13:15 +0000464 if (OperandIV) {
465 assert(OperandIV->isConstant() && "Should only be here for constants!");
466 markConstant(PN, OperandIV->getConstant()); // Aquire operand value
Chris Lattner138a1242001-06-27 23:38:11 +0000467 }
Chris Lattner138a1242001-06-27 23:38:11 +0000468}
469
Chris Lattnerb9a66342002-05-02 21:44:00 +0000470void SCCP::visitTerminatorInst(TerminatorInst *TI) {
471 std::vector<bool> SuccFeasible(TI->getNumSuccessors());
472 getFeasibleSuccessors(TI, SuccFeasible);
Chris Lattner138a1242001-06-27 23:38:11 +0000473
Chris Lattnerb9a66342002-05-02 21:44:00 +0000474 // Mark all feasible successors executable...
475 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
476 if (SuccFeasible[i])
477 markExecutable(TI->getSuccessor(i));
Chris Lattner2a632552002-04-18 15:13:15 +0000478}
479
480void SCCP::visitUnaryOperator(Instruction *I) {
481 Value *V = I->getOperand(0);
482 InstVal &VState = getValueState(V);
483 if (VState.isOverdefined()) { // Inherit overdefinedness of operand
484 markOverdefined(I);
485 } else if (VState.isConstant()) { // Propogate constant value
486 Constant *Result = isa<CastInst>(I)
487 ? ConstantFoldCastInstruction(VState.getConstant(), I->getType())
488 : ConstantFoldUnaryInstruction(I->getOpcode(), VState.getConstant());
489
490 if (Result) {
491 // This instruction constant folds!
492 markConstant(I, Result);
493 } else {
494 markOverdefined(I); // Don't know how to fold this instruction. :(
495 }
496 }
497}
498
499// Handle BinaryOperators and Shift Instructions...
500void SCCP::visitBinaryOperator(Instruction *I) {
501 InstVal &V1State = getValueState(I->getOperand(0));
502 InstVal &V2State = getValueState(I->getOperand(1));
503 if (V1State.isOverdefined() || V2State.isOverdefined()) {
504 markOverdefined(I);
505 } else if (V1State.isConstant() && V2State.isConstant()) {
Chris Lattner4c1061f2002-05-06 03:01:37 +0000506 Constant *Result = 0;
507 if (isa<BinaryOperator>(I))
508 Result = ConstantFoldBinaryInstruction(I->getOpcode(),
509 V1State.getConstant(),
510 V2State.getConstant());
511 else if (isa<ShiftInst>(I))
512 Result = ConstantFoldShiftInstruction(I->getOpcode(),
513 V1State.getConstant(),
514 V2State.getConstant());
Chris Lattner2a632552002-04-18 15:13:15 +0000515 if (Result)
Chris Lattner0e9c5152002-05-02 20:32:51 +0000516 markConstant(I, Result); // This instruction constant folds!
Chris Lattner2a632552002-04-18 15:13:15 +0000517 else
518 markOverdefined(I); // Don't know how to fold this instruction. :(
519 }
520}