blob: 015c67a0d228ede9b08230b51d897f5c43109e06 [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 Lattner7061dc52001-12-03 18:02:31 +000021#include "llvm/iPHINode.h"
Chris Lattner3b7bfdb2001-07-14 06:11:51 +000022#include "llvm/iMemory.h"
Chris Lattner138a1242001-06-27 23:38:11 +000023#include "llvm/iTerminators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000024#include "llvm/iOther.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000025#include "llvm/Pass.h"
Chris Lattner2a632552002-04-18 15:13:15 +000026#include "llvm/Support/InstVisitor.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000027#include "Support/STLExtras.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 +000030#include <iostream>
31using std::cerr;
Chris Lattner138a1242001-06-27 23:38:11 +000032
Chris Lattner59f0ce22002-05-02 21:18:01 +000033#if 0 // Enable this to get SCCP debug output
34#define DEBUG_SCCP(X) X
35#else
36#define DEBUG_SCCP(X)
37#endif
38
Chris Lattner138a1242001-06-27 23:38:11 +000039// InstVal class - This class represents the different lattice values that an
Chris Lattnerf57b8452002-04-27 06:56:12 +000040// instruction may occupy. It is a simple class with value semantics.
Chris Lattner138a1242001-06-27 23:38:11 +000041//
Chris Lattner0dbfc052002-04-29 21:26:08 +000042namespace {
Chris Lattner138a1242001-06-27 23:38:11 +000043class InstVal {
44 enum {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000045 undefined, // This instruction has no known value
46 constant, // This instruction has a constant value
Chris Lattner138a1242001-06-27 23:38:11 +000047 // Range, // This instruction is known to fall within a range
Chris Lattnere9bb2df2001-12-03 22:26:30 +000048 overdefined // This instruction has an unknown value
49 } LatticeValue; // The current lattice position
50 Constant *ConstantVal; // If Constant value, the current value
Chris Lattner138a1242001-06-27 23:38:11 +000051public:
Chris Lattnere9bb2df2001-12-03 22:26:30 +000052 inline InstVal() : LatticeValue(undefined), ConstantVal(0) {}
Chris Lattner138a1242001-06-27 23:38:11 +000053
54 // markOverdefined - Return true if this is a new status to be in...
55 inline bool markOverdefined() {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000056 if (LatticeValue != overdefined) {
57 LatticeValue = overdefined;
Chris Lattner138a1242001-06-27 23:38:11 +000058 return true;
59 }
60 return false;
61 }
62
63 // markConstant - Return true if this is a new status for us...
Chris Lattnere9bb2df2001-12-03 22:26:30 +000064 inline bool markConstant(Constant *V) {
65 if (LatticeValue != constant) {
66 LatticeValue = constant;
Chris Lattner138a1242001-06-27 23:38:11 +000067 ConstantVal = V;
68 return true;
69 } else {
Chris Lattnerb70d82f2001-09-07 16:43:22 +000070 assert(ConstantVal == V && "Marking constant with different value");
Chris Lattner138a1242001-06-27 23:38:11 +000071 }
72 return false;
73 }
74
Chris Lattnere9bb2df2001-12-03 22:26:30 +000075 inline bool isUndefined() const { return LatticeValue == undefined; }
76 inline bool isConstant() const { return LatticeValue == constant; }
77 inline bool isOverdefined() const { return LatticeValue == overdefined; }
Chris Lattner138a1242001-06-27 23:38:11 +000078
Chris Lattnere9bb2df2001-12-03 22:26:30 +000079 inline Constant *getConstant() const { return ConstantVal; }
Chris Lattner138a1242001-06-27 23:38:11 +000080};
81
Chris Lattner0dbfc052002-04-29 21:26:08 +000082} // end anonymous namespace
Chris Lattner138a1242001-06-27 23:38:11 +000083
84
85//===----------------------------------------------------------------------===//
86// SCCP Class
87//
88// This class does all of the work of Sparse Conditional Constant Propogation.
Chris Lattner138a1242001-06-27 23:38:11 +000089//
Chris Lattner0dbfc052002-04-29 21:26:08 +000090namespace {
91class SCCP : public FunctionPass, public InstVisitor<SCCP> {
Chris Lattner697954c2002-01-20 22:54:45 +000092 std::set<BasicBlock*> BBExecutable;// The basic blocks that are executable
93 std::map<Value*, InstVal> ValueState; // The state each value is in...
Chris Lattner138a1242001-06-27 23:38:11 +000094
Chris Lattner59f0ce22002-05-02 21:18:01 +000095 std::set<Instruction*> InstWorkList;// The instruction work list
Chris Lattner697954c2002-01-20 22:54:45 +000096 std::vector<BasicBlock*> BBWorkList; // The BasicBlock work list
Chris Lattner138a1242001-06-27 23:38:11 +000097public:
98
Chris Lattner0dbfc052002-04-29 21:26:08 +000099 const char *getPassName() const {
100 return "Sparse Conditional Constant Propogation";
101 }
Chris Lattner138a1242001-06-27 23:38:11 +0000102
Chris Lattner0dbfc052002-04-29 21:26:08 +0000103 // runOnFunction - Run the Sparse Conditional Constant Propogation algorithm,
104 // and return true if the function was modified.
105 //
106 bool runOnFunction(Function *F);
107
108 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerb9a66342002-05-02 21:44:00 +0000109 AU.preservesCFG();
Chris Lattner0dbfc052002-04-29 21:26:08 +0000110 }
111
Chris Lattner138a1242001-06-27 23:38:11 +0000112
113 //===--------------------------------------------------------------------===//
114 // The implementation of this class
115 //
116private:
Chris Lattner2a632552002-04-18 15:13:15 +0000117 friend class InstVisitor<SCCP>; // Allow callbacks from visitor
Chris Lattner138a1242001-06-27 23:38:11 +0000118
119 // markValueOverdefined - Make a value be marked as "constant". If the value
120 // is not already a constant, add it to the instruction work list so that
121 // the users of the instruction are updated later.
122 //
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000123 inline bool markConstant(Instruction *I, Constant *V) {
Chris Lattner59f0ce22002-05-02 21:18:01 +0000124 DEBUG_SCCP(cerr << "markConstant: " << V << " = " << I);
125
Chris Lattner138a1242001-06-27 23:38:11 +0000126 if (ValueState[I].markConstant(V)) {
Chris Lattner59f0ce22002-05-02 21:18:01 +0000127 InstWorkList.insert(I);
Chris Lattner138a1242001-06-27 23:38:11 +0000128 return true;
129 }
130 return false;
131 }
132
133 // markValueOverdefined - Make a value be marked as "overdefined". If the
134 // value is not already overdefined, add it to the instruction work list so
135 // that the users of the instruction are updated later.
136 //
137 inline bool markOverdefined(Value *V) {
138 if (ValueState[V].markOverdefined()) {
Chris Lattner9636a912001-10-01 16:18:37 +0000139 if (Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner59f0ce22002-05-02 21:18:01 +0000140 DEBUG_SCCP(cerr << "markOverdefined: " << V);
141 InstWorkList.insert(I); // Only instructions go on the work list
Chris Lattner138a1242001-06-27 23:38:11 +0000142 }
143 return true;
144 }
145 return false;
146 }
147
148 // getValueState - Return the InstVal object that corresponds to the value.
149 // This function is neccesary because not all values should start out in the
Chris Lattner73e21422002-04-09 19:48:49 +0000150 // underdefined state... Argument's should be overdefined, and
Chris Lattner79df7c02002-03-26 18:01:55 +0000151 // constants should be marked as constants. If a value is not known to be an
Chris Lattner138a1242001-06-27 23:38:11 +0000152 // Instruction object, then use this accessor to get its value from the map.
153 //
154 inline InstVal &getValueState(Value *V) {
Chris Lattner697954c2002-01-20 22:54:45 +0000155 std::map<Value*, InstVal>::iterator I = ValueState.find(V);
Chris Lattner138a1242001-06-27 23:38:11 +0000156 if (I != ValueState.end()) return I->second; // Common case, in the map
157
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000158 if (Constant *CPV = dyn_cast<Constant>(V)) { // Constants are constant
Chris Lattner138a1242001-06-27 23:38:11 +0000159 ValueState[CPV].markConstant(CPV);
Chris Lattner73e21422002-04-09 19:48:49 +0000160 } else if (isa<Argument>(V)) { // Arguments are overdefined
Chris Lattner138a1242001-06-27 23:38:11 +0000161 ValueState[V].markOverdefined();
162 }
163 // All others are underdefined by default...
164 return ValueState[V];
165 }
166
167 // markExecutable - Mark a basic block as executable, adding it to the BB
168 // work list if it is not already executable...
169 //
170 void markExecutable(BasicBlock *BB) {
171 if (BBExecutable.count(BB)) return;
Chris Lattner59f0ce22002-05-02 21:18:01 +0000172 DEBUG_SCCP(cerr << "Marking BB Executable: " << BB);
Chris Lattner138a1242001-06-27 23:38:11 +0000173 BBExecutable.insert(BB); // Basic block is executable!
174 BBWorkList.push_back(BB); // Add the block to the work list!
175 }
176
Chris Lattner138a1242001-06-27 23:38:11 +0000177
Chris Lattner2a632552002-04-18 15:13:15 +0000178 // visit implementations - Something changed in this instruction... Either an
Chris Lattnercb056de2001-06-29 23:56:23 +0000179 // operand made a transition, or the instruction is newly executable. Change
180 // the value type of I to reflect these changes if appropriate.
181 //
Chris Lattner2a632552002-04-18 15:13:15 +0000182 void visitPHINode(PHINode *I);
183
184 // Terminators
185 void visitReturnInst(ReturnInst *I) { /*does not have an effect*/ }
Chris Lattnerb9a66342002-05-02 21:44:00 +0000186 void visitTerminatorInst(TerminatorInst *TI);
Chris Lattner2a632552002-04-18 15:13:15 +0000187
188 void visitUnaryOperator(Instruction *I);
189 void visitCastInst(CastInst *I) { visitUnaryOperator(I); }
190 void visitBinaryOperator(Instruction *I);
191 void visitShiftInst(ShiftInst *I) { visitBinaryOperator(I); }
192
193 // Instructions that cannot be folded away...
Chris Lattner59f0ce22002-05-02 21:18:01 +0000194 void visitStoreInst (Instruction *I) { /*returns void*/ }
Chris Lattner2a632552002-04-18 15:13:15 +0000195 void visitMemAccessInst (Instruction *I) { markOverdefined(I); }
196 void visitCallInst (Instruction *I) { markOverdefined(I); }
197 void visitInvokeInst (Instruction *I) { markOverdefined(I); }
198 void visitAllocationInst(Instruction *I) { markOverdefined(I); }
Chris Lattner59f0ce22002-05-02 21:18:01 +0000199 void visitFreeInst (Instruction *I) { /*returns void*/ }
Chris Lattner2a632552002-04-18 15:13:15 +0000200
201 void visitInstruction(Instruction *I) {
202 // If a new instruction is added to LLVM that we don't handle...
203 cerr << "SCCP: Don't know how to handle: " << I;
204 markOverdefined(I); // Just in case
205 }
Chris Lattnercb056de2001-06-29 23:56:23 +0000206
Chris Lattnerb9a66342002-05-02 21:44:00 +0000207 // getFeasibleSuccessors - Return a vector of booleans to indicate which
208 // successors are reachable from a given terminator instruction.
209 //
210 void getFeasibleSuccessors(TerminatorInst *I, std::vector<bool> &Succs);
211
Chris Lattner59f0ce22002-05-02 21:18:01 +0000212 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
213 // block to the 'To' basic block is currently feasible...
214 //
215 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To);
216
Chris Lattnercb056de2001-06-29 23:56:23 +0000217 // OperandChangedState - This method is invoked on all of the users of an
218 // instruction that was just changed state somehow.... Based on this
219 // information, we need to update the specified user of this instruction.
220 //
Chris Lattner59f0ce22002-05-02 21:18:01 +0000221 void OperandChangedState(User *U) {
222 // Only instructions use other variable values!
223 Instruction *I = cast<Instruction>(U);
224 if (!BBExecutable.count(I->getParent())) return;// Inst not executable yet!
225 visit(I);
226 }
Chris Lattnercb056de2001-06-29 23:56:23 +0000227};
Chris Lattner0dbfc052002-04-29 21:26:08 +0000228} // end anonymous namespace
229
230
231// createSCCPPass - This is the public interface to this file...
232//
233Pass *createSCCPPass() {
234 return new SCCP();
235}
236
Chris Lattner138a1242001-06-27 23:38:11 +0000237
238
239//===----------------------------------------------------------------------===//
240// SCCP Class Implementation
241
242
Chris Lattner0dbfc052002-04-29 21:26:08 +0000243// runOnFunction() - Run the Sparse Conditional Constant Propogation algorithm,
244// and return true if the function was modified.
Chris Lattner138a1242001-06-27 23:38:11 +0000245//
Chris Lattner0dbfc052002-04-29 21:26:08 +0000246bool SCCP::runOnFunction(Function *F) {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000247 // Mark the first block of the function as being executable...
Chris Lattner0dbfc052002-04-29 21:26:08 +0000248 markExecutable(F->front());
Chris Lattner138a1242001-06-27 23:38:11 +0000249
250 // Process the work lists until their are empty!
251 while (!BBWorkList.empty() || !InstWorkList.empty()) {
252 // Process the instruction work list...
253 while (!InstWorkList.empty()) {
Chris Lattner59f0ce22002-05-02 21:18:01 +0000254 Instruction *I = *InstWorkList.begin();
255 InstWorkList.erase(InstWorkList.begin());
Chris Lattner138a1242001-06-27 23:38:11 +0000256
Chris Lattner59f0ce22002-05-02 21:18:01 +0000257 DEBUG_SCCP(cerr << "\nPopped off I-WL: " << I);
Chris Lattner138a1242001-06-27 23:38:11 +0000258
259
260 // "I" got into the work list because it either made the transition from
261 // bottom to constant, or to Overdefined.
262 //
263 // Update all of the users of this instruction's value...
264 //
265 for_each(I->use_begin(), I->use_end(),
266 bind_obj(this, &SCCP::OperandChangedState));
267 }
268
269 // Process the basic block work list...
270 while (!BBWorkList.empty()) {
271 BasicBlock *BB = BBWorkList.back();
272 BBWorkList.pop_back();
273
Chris Lattner59f0ce22002-05-02 21:18:01 +0000274 DEBUG_SCCP(cerr << "\nPopped off BBWL: " << BB);
Chris Lattner138a1242001-06-27 23:38:11 +0000275
276 // If this block only has a single successor, mark it as executable as
277 // well... if not, terminate the do loop.
278 //
279 if (BB->getTerminator()->getNumSuccessors() == 1)
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000280 markExecutable(BB->getTerminator()->getSuccessor(0));
Chris Lattner138a1242001-06-27 23:38:11 +0000281
Chris Lattner2a632552002-04-18 15:13:15 +0000282 // Notify all instructions in this basic block that they are newly
283 // executable.
284 visit(BB);
Chris Lattner138a1242001-06-27 23:38:11 +0000285 }
286 }
287
Chris Lattner904ec282002-05-02 21:49:50 +0000288#if 0
Chris Lattner0dbfc052002-04-29 21:26:08 +0000289 for (Function::iterator BBI = F->begin(), BBEnd = F->end();
Chris Lattner79df7c02002-03-26 18:01:55 +0000290 BBI != BBEnd; ++BBI)
Chris Lattner138a1242001-06-27 23:38:11 +0000291 if (!BBExecutable.count(*BBI))
292 cerr << "BasicBlock Dead:" << *BBI;
293#endif
294
295
Chris Lattnerf57b8452002-04-27 06:56:12 +0000296 // Iterate over all of the instructions in a function, replacing them with
Chris Lattner138a1242001-06-27 23:38:11 +0000297 // constants if we have found them to be of constant values.
298 //
299 bool MadeChanges = false;
Chris Lattner0dbfc052002-04-29 21:26:08 +0000300 for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI) {
301 BasicBlock *BB = *FI;
Chris Lattner221d6882002-02-12 21:07:25 +0000302 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
303 Instruction *Inst = *BI;
304 InstVal &IV = ValueState[Inst];
305 if (IV.isConstant()) {
306 Constant *Const = IV.getConstant();
Chris Lattner59f0ce22002-05-02 21:18:01 +0000307 DEBUG_SCCP(cerr << "Constant: " << Inst << " is: " << Const);
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000308
Chris Lattner221d6882002-02-12 21:07:25 +0000309 // Replaces all of the uses of a variable with uses of the constant.
310 Inst->replaceAllUsesWith(Const);
Chris Lattner138a1242001-06-27 23:38:11 +0000311
Chris Lattner0e9c5152002-05-02 20:32:51 +0000312 // Remove the operator from the list of definitions... and delete it.
313 delete BB->getInstList().remove(BI);
Chris Lattner138a1242001-06-27 23:38:11 +0000314
Chris Lattner221d6882002-02-12 21:07:25 +0000315 // Hey, we just changed something!
316 MadeChanges = true;
Chris Lattnerb9a66342002-05-02 21:44:00 +0000317 } else {
318 ++BI;
Chris Lattner221d6882002-02-12 21:07:25 +0000319 }
Chris Lattner138a1242001-06-27 23:38:11 +0000320 }
321 }
322
Chris Lattner59f0ce22002-05-02 21:18:01 +0000323 // Reset state so that the next invocation will have empty data structures
Chris Lattner0dbfc052002-04-29 21:26:08 +0000324 BBExecutable.clear();
325 ValueState.clear();
326
Chris Lattnerb70d82f2001-09-07 16:43:22 +0000327 return MadeChanges;
Chris Lattner138a1242001-06-27 23:38:11 +0000328}
329
Chris Lattnerb9a66342002-05-02 21:44:00 +0000330
331// getFeasibleSuccessors - Return a vector of booleans to indicate which
332// successors are reachable from a given terminator instruction.
333//
334void SCCP::getFeasibleSuccessors(TerminatorInst *TI, std::vector<bool> &Succs) {
335 assert(Succs.size() == TI->getNumSuccessors() && "Succs vector wrong size!");
336 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
337 if (BI->isUnconditional()) {
338 Succs[0] = true;
339 } else {
340 InstVal &BCValue = getValueState(BI->getCondition());
341 if (BCValue.isOverdefined()) {
342 // Overdefined condition variables mean the branch could go either way.
343 Succs[0] = Succs[1] = true;
344 } else if (BCValue.isConstant()) {
345 // Constant condition variables mean the branch can only go a single way
346 Succs[BCValue.getConstant() == ConstantBool::False] = true;
347 }
348 }
349 } else if (InvokeInst *II = dyn_cast<InvokeInst>(TI)) {
350 // Invoke instructions successors are always executable.
351 Succs[0] = Succs[1] = true;
352 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
353 InstVal &SCValue = getValueState(SI->getCondition());
354 if (SCValue.isOverdefined()) { // Overdefined condition?
355 // All destinations are executable!
356 Succs.assign(TI->getNumSuccessors(), true);
357 } else if (SCValue.isConstant()) {
358 Constant *CPV = SCValue.getConstant();
359 // Make sure to skip the "default value" which isn't a value
360 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) {
361 if (SI->getSuccessorValue(i) == CPV) {// Found the right branch...
362 Succs[i] = true;
363 return;
364 }
365 }
366
367 // Constant value not equal to any of the branches... must execute
368 // default branch then...
369 Succs[0] = true;
370 }
371 } else {
372 cerr << "SCCP: Don't know how to handle: " << TI;
373 Succs.assign(TI->getNumSuccessors(), true);
374 }
375}
376
377
Chris Lattner59f0ce22002-05-02 21:18:01 +0000378// isEdgeFeasible - Return true if the control flow edge from the 'From' basic
379// block to the 'To' basic block is currently feasible...
380//
381bool SCCP::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
382 assert(BBExecutable.count(To) && "Dest should always be alive!");
383
384 // Make sure the source basic block is executable!!
385 if (!BBExecutable.count(From)) return false;
386
Chris Lattnerb9a66342002-05-02 21:44:00 +0000387 // Check to make sure this edge itself is actually feasible now...
388 TerminatorInst *FT = From->getTerminator();
389 std::vector<bool> SuccFeasible(FT->getNumSuccessors());
390 getFeasibleSuccessors(FT, SuccFeasible);
391
392 // Check all edges from From to To. If any are feasible, return true.
393 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
394 if (FT->getSuccessor(i) == To && SuccFeasible[i])
395 return true;
396
397 // Otherwise, none of the edges are actually feasible at this time...
398 return false;
Chris Lattner59f0ce22002-05-02 21:18:01 +0000399}
Chris Lattner138a1242001-06-27 23:38:11 +0000400
Chris Lattner2a632552002-04-18 15:13:15 +0000401// visit Implementations - Something changed in this instruction... Either an
Chris Lattner138a1242001-06-27 23:38:11 +0000402// operand made a transition, or the instruction is newly executable. Change
403// the value type of I to reflect these changes if appropriate. This method
404// makes sure to do the following actions:
405//
406// 1. If a phi node merges two constants in, and has conflicting value coming
407// from different branches, or if the PHI node merges in an overdefined
408// value, then the PHI node becomes overdefined.
409// 2. If a phi node merges only constants in, and they all agree on value, the
410// PHI node becomes a constant value equal to that.
411// 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
412// 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
413// 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
414// 6. If a conditional branch has a value that is constant, make the selected
415// destination executable
416// 7. If a conditional branch has a value that is overdefined, make all
417// successors executable.
418//
Chris Lattner138a1242001-06-27 23:38:11 +0000419
Chris Lattner2a632552002-04-18 15:13:15 +0000420void SCCP::visitPHINode(PHINode *PN) {
421 unsigned NumValues = PN->getNumIncomingValues(), i;
422 InstVal *OperandIV = 0;
Chris Lattner138a1242001-06-27 23:38:11 +0000423
Chris Lattner2a632552002-04-18 15:13:15 +0000424 // Look at all of the executable operands of the PHI node. If any of them
425 // are overdefined, the PHI becomes overdefined as well. If they are all
426 // constant, and they agree with each other, the PHI becomes the identical
427 // constant. If they are constant and don't agree, the PHI is overdefined.
428 // If there are no executable operands, the PHI remains undefined.
429 //
430 for (i = 0; i < NumValues; ++i) {
Chris Lattner59f0ce22002-05-02 21:18:01 +0000431 if (isEdgeFeasible(PN->getIncomingBlock(i), PN->getParent())) {
Chris Lattner2a632552002-04-18 15:13:15 +0000432 InstVal &IV = getValueState(PN->getIncomingValue(i));
433 if (IV.isUndefined()) continue; // Doesn't influence PHI node.
434 if (IV.isOverdefined()) { // PHI node becomes overdefined!
435 markOverdefined(PN);
436 return;
437 }
Chris Lattner138a1242001-06-27 23:38:11 +0000438
Chris Lattner2a632552002-04-18 15:13:15 +0000439 if (OperandIV == 0) { // Grab the first value...
440 OperandIV = &IV;
441 } else { // Another value is being merged in!
442 // There is already a reachable operand. If we conflict with it,
443 // then the PHI node becomes overdefined. If we agree with it, we
444 // can continue on.
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000445
Chris Lattner2a632552002-04-18 15:13:15 +0000446 // Check to see if there are two different constants merging...
447 if (IV.getConstant() != OperandIV->getConstant()) {
448 // Yes there is. This means the PHI node is not constant.
449 // You must be overdefined poor PHI.
450 //
451 markOverdefined(PN); // The PHI node now becomes overdefined
452 return; // I'm done analyzing you
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000453 }
Chris Lattner138a1242001-06-27 23:38:11 +0000454 }
455 }
Chris Lattner138a1242001-06-27 23:38:11 +0000456 }
457
Chris Lattner2a632552002-04-18 15:13:15 +0000458 // If we exited the loop, this means that the PHI node only has constant
459 // arguments that agree with each other(and OperandIV is a pointer to one
460 // of their InstVal's) or OperandIV is null because there are no defined
461 // incoming arguments. If this is the case, the PHI remains undefined.
Chris Lattner138a1242001-06-27 23:38:11 +0000462 //
Chris Lattner2a632552002-04-18 15:13:15 +0000463 if (OperandIV) {
464 assert(OperandIV->isConstant() && "Should only be here for constants!");
465 markConstant(PN, OperandIV->getConstant()); // Aquire operand value
Chris Lattner138a1242001-06-27 23:38:11 +0000466 }
Chris Lattner138a1242001-06-27 23:38:11 +0000467}
468
Chris Lattnerb9a66342002-05-02 21:44:00 +0000469void SCCP::visitTerminatorInst(TerminatorInst *TI) {
470 std::vector<bool> SuccFeasible(TI->getNumSuccessors());
471 getFeasibleSuccessors(TI, SuccFeasible);
Chris Lattner138a1242001-06-27 23:38:11 +0000472
Chris Lattnerb9a66342002-05-02 21:44:00 +0000473 // Mark all feasible successors executable...
474 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
475 if (SuccFeasible[i])
476 markExecutable(TI->getSuccessor(i));
Chris Lattner2a632552002-04-18 15:13:15 +0000477}
478
479void SCCP::visitUnaryOperator(Instruction *I) {
480 Value *V = I->getOperand(0);
481 InstVal &VState = getValueState(V);
482 if (VState.isOverdefined()) { // Inherit overdefinedness of operand
483 markOverdefined(I);
484 } else if (VState.isConstant()) { // Propogate constant value
485 Constant *Result = isa<CastInst>(I)
486 ? ConstantFoldCastInstruction(VState.getConstant(), I->getType())
487 : ConstantFoldUnaryInstruction(I->getOpcode(), VState.getConstant());
488
489 if (Result) {
490 // This instruction constant folds!
491 markConstant(I, Result);
492 } else {
493 markOverdefined(I); // Don't know how to fold this instruction. :(
494 }
495 }
496}
497
498// Handle BinaryOperators and Shift Instructions...
499void SCCP::visitBinaryOperator(Instruction *I) {
500 InstVal &V1State = getValueState(I->getOperand(0));
501 InstVal &V2State = getValueState(I->getOperand(1));
502 if (V1State.isOverdefined() || V2State.isOverdefined()) {
503 markOverdefined(I);
504 } else if (V1State.isConstant() && V2State.isConstant()) {
Chris Lattner4c1061f2002-05-06 03:01:37 +0000505 Constant *Result = 0;
506 if (isa<BinaryOperator>(I))
507 Result = ConstantFoldBinaryInstruction(I->getOpcode(),
508 V1State.getConstant(),
509 V2State.getConstant());
510 else if (isa<ShiftInst>(I))
511 Result = ConstantFoldShiftInstruction(I->getOpcode(),
512 V1State.getConstant(),
513 V2State.getConstant());
Chris Lattner2a632552002-04-18 15:13:15 +0000514 if (Result)
Chris Lattner0e9c5152002-05-02 20:32:51 +0000515 markConstant(I, Result); // This instruction constant folds!
Chris Lattner2a632552002-04-18 15:13:15 +0000516 else
517 markOverdefined(I); // Don't know how to fold this instruction. :(
518 }
519}