blob: a5966e96a28802ab4e14b22a36a4ce8e09815764 [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
9// . Proves conditional branches constant, and unconditionalizes them
10// * 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 Lattner138a1242001-06-27 23:38:11 +000033// InstVal class - This class represents the different lattice values that an
Chris Lattnerf57b8452002-04-27 06:56:12 +000034// instruction may occupy. It is a simple class with value semantics.
Chris Lattner138a1242001-06-27 23:38:11 +000035//
Chris Lattner0dbfc052002-04-29 21:26:08 +000036namespace {
Chris Lattner138a1242001-06-27 23:38:11 +000037class InstVal {
38 enum {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000039 undefined, // This instruction has no known value
40 constant, // This instruction has a constant value
Chris Lattner138a1242001-06-27 23:38:11 +000041 // Range, // This instruction is known to fall within a range
Chris Lattnere9bb2df2001-12-03 22:26:30 +000042 overdefined // This instruction has an unknown value
43 } LatticeValue; // The current lattice position
44 Constant *ConstantVal; // If Constant value, the current value
Chris Lattner138a1242001-06-27 23:38:11 +000045public:
Chris Lattnere9bb2df2001-12-03 22:26:30 +000046 inline InstVal() : LatticeValue(undefined), ConstantVal(0) {}
Chris Lattner138a1242001-06-27 23:38:11 +000047
48 // markOverdefined - Return true if this is a new status to be in...
49 inline bool markOverdefined() {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000050 if (LatticeValue != overdefined) {
51 LatticeValue = overdefined;
Chris Lattner138a1242001-06-27 23:38:11 +000052 return true;
53 }
54 return false;
55 }
56
57 // markConstant - Return true if this is a new status for us...
Chris Lattnere9bb2df2001-12-03 22:26:30 +000058 inline bool markConstant(Constant *V) {
59 if (LatticeValue != constant) {
60 LatticeValue = constant;
Chris Lattner138a1242001-06-27 23:38:11 +000061 ConstantVal = V;
62 return true;
63 } else {
Chris Lattnerb70d82f2001-09-07 16:43:22 +000064 assert(ConstantVal == V && "Marking constant with different value");
Chris Lattner138a1242001-06-27 23:38:11 +000065 }
66 return false;
67 }
68
Chris Lattnere9bb2df2001-12-03 22:26:30 +000069 inline bool isUndefined() const { return LatticeValue == undefined; }
70 inline bool isConstant() const { return LatticeValue == constant; }
71 inline bool isOverdefined() const { return LatticeValue == overdefined; }
Chris Lattner138a1242001-06-27 23:38:11 +000072
Chris Lattnere9bb2df2001-12-03 22:26:30 +000073 inline Constant *getConstant() const { return ConstantVal; }
Chris Lattner138a1242001-06-27 23:38:11 +000074};
75
Chris Lattner0dbfc052002-04-29 21:26:08 +000076} // end anonymous namespace
Chris Lattner138a1242001-06-27 23:38:11 +000077
78
79//===----------------------------------------------------------------------===//
80// SCCP Class
81//
82// This class does all of the work of Sparse Conditional Constant Propogation.
Chris Lattner138a1242001-06-27 23:38:11 +000083//
Chris Lattner0dbfc052002-04-29 21:26:08 +000084namespace {
85class SCCP : public FunctionPass, public InstVisitor<SCCP> {
Chris Lattner697954c2002-01-20 22:54:45 +000086 std::set<BasicBlock*> BBExecutable;// The basic blocks that are executable
87 std::map<Value*, InstVal> ValueState; // The state each value is in...
Chris Lattner138a1242001-06-27 23:38:11 +000088
Chris Lattner697954c2002-01-20 22:54:45 +000089 std::vector<Instruction*> InstWorkList;// The instruction work list
90 std::vector<BasicBlock*> BBWorkList; // The BasicBlock work list
Chris Lattner138a1242001-06-27 23:38:11 +000091public:
92
Chris Lattner0dbfc052002-04-29 21:26:08 +000093 const char *getPassName() const {
94 return "Sparse Conditional Constant Propogation";
95 }
Chris Lattner138a1242001-06-27 23:38:11 +000096
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 //
100 bool runOnFunction(Function *F);
101
102 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
103 // FIXME: SCCP does not preserve the CFG because it folds terminators!
104 //AU.preservesCFG();
105 }
106
Chris Lattner138a1242001-06-27 23:38:11 +0000107
108 //===--------------------------------------------------------------------===//
109 // The implementation of this class
110 //
111private:
Chris Lattner2a632552002-04-18 15:13:15 +0000112 friend class InstVisitor<SCCP>; // Allow callbacks from visitor
Chris Lattner138a1242001-06-27 23:38:11 +0000113
114 // markValueOverdefined - Make a value be marked as "constant". If the value
115 // is not already a constant, add it to the instruction work list so that
116 // the users of the instruction are updated later.
117 //
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000118 inline bool markConstant(Instruction *I, Constant *V) {
Chris Lattner138a1242001-06-27 23:38:11 +0000119 //cerr << "markConstant: " << V << " = " << I;
120 if (ValueState[I].markConstant(V)) {
121 InstWorkList.push_back(I);
122 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 Lattner138a1242001-06-27 23:38:11 +0000134 //cerr << "markOverdefined: " << V;
135 InstWorkList.push_back(I); // Only instructions go on the work list
136 }
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;
166 //cerr << "Marking BB Executable: " << BB;
167 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 Lattner2a632552002-04-18 15:13:15 +0000176 void visitPHINode(PHINode *I);
177
178 // Terminators
179 void visitReturnInst(ReturnInst *I) { /*does not have an effect*/ }
180 void visitBranchInst(BranchInst *I);
181 void visitSwitchInst(SwitchInst *I);
182
183 void visitUnaryOperator(Instruction *I);
184 void visitCastInst(CastInst *I) { visitUnaryOperator(I); }
185 void visitBinaryOperator(Instruction *I);
186 void visitShiftInst(ShiftInst *I) { visitBinaryOperator(I); }
187
188 // Instructions that cannot be folded away...
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) { markOverdefined(I); }
194
195 void visitInstruction(Instruction *I) {
196 // If a new instruction is added to LLVM that we don't handle...
197 cerr << "SCCP: Don't know how to handle: " << I;
198 markOverdefined(I); // Just in case
199 }
Chris Lattnercb056de2001-06-29 23:56:23 +0000200
201 // OperandChangedState - This method is invoked on all of the users of an
202 // instruction that was just changed state somehow.... Based on this
203 // information, we need to update the specified user of this instruction.
204 //
205 void OperandChangedState(User *U);
206};
Chris Lattner0dbfc052002-04-29 21:26:08 +0000207} // end anonymous namespace
208
209
210// createSCCPPass - This is the public interface to this file...
211//
212Pass *createSCCPPass() {
213 return new SCCP();
214}
215
Chris Lattner138a1242001-06-27 23:38:11 +0000216
217
218//===----------------------------------------------------------------------===//
219// SCCP Class Implementation
220
221
Chris Lattner0dbfc052002-04-29 21:26:08 +0000222// runOnFunction() - Run the Sparse Conditional Constant Propogation algorithm,
223// and return true if the function was modified.
Chris Lattner138a1242001-06-27 23:38:11 +0000224//
Chris Lattner0dbfc052002-04-29 21:26:08 +0000225bool SCCP::runOnFunction(Function *F) {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000226 // Mark the first block of the function as being executable...
Chris Lattner0dbfc052002-04-29 21:26:08 +0000227 markExecutable(F->front());
Chris Lattner138a1242001-06-27 23:38:11 +0000228
229 // Process the work lists until their are empty!
230 while (!BBWorkList.empty() || !InstWorkList.empty()) {
231 // Process the instruction work list...
232 while (!InstWorkList.empty()) {
233 Instruction *I = InstWorkList.back();
234 InstWorkList.pop_back();
235
236 //cerr << "\nPopped off I-WL: " << I;
237
238
239 // "I" got into the work list because it either made the transition from
240 // bottom to constant, or to Overdefined.
241 //
242 // Update all of the users of this instruction's value...
243 //
244 for_each(I->use_begin(), I->use_end(),
245 bind_obj(this, &SCCP::OperandChangedState));
246 }
247
248 // Process the basic block work list...
249 while (!BBWorkList.empty()) {
250 BasicBlock *BB = BBWorkList.back();
251 BBWorkList.pop_back();
252
253 //cerr << "\nPopped off BBWL: " << BB;
254
255 // If this block only has a single successor, mark it as executable as
256 // well... if not, terminate the do loop.
257 //
258 if (BB->getTerminator()->getNumSuccessors() == 1)
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000259 markExecutable(BB->getTerminator()->getSuccessor(0));
Chris Lattner138a1242001-06-27 23:38:11 +0000260
Chris Lattner2a632552002-04-18 15:13:15 +0000261 // Notify all instructions in this basic block that they are newly
262 // executable.
263 visit(BB);
Chris Lattner138a1242001-06-27 23:38:11 +0000264 }
265 }
266
267#if 0
Chris Lattner0dbfc052002-04-29 21:26:08 +0000268 for (Function::iterator BBI = F->begin(), BBEnd = F->end();
Chris Lattner79df7c02002-03-26 18:01:55 +0000269 BBI != BBEnd; ++BBI)
Chris Lattner138a1242001-06-27 23:38:11 +0000270 if (!BBExecutable.count(*BBI))
271 cerr << "BasicBlock Dead:" << *BBI;
272#endif
273
274
Chris Lattnerf57b8452002-04-27 06:56:12 +0000275 // Iterate over all of the instructions in a function, replacing them with
Chris Lattner138a1242001-06-27 23:38:11 +0000276 // constants if we have found them to be of constant values.
277 //
278 bool MadeChanges = false;
Chris Lattner0dbfc052002-04-29 21:26:08 +0000279 for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI) {
280 BasicBlock *BB = *FI;
Chris Lattner221d6882002-02-12 21:07:25 +0000281 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
282 Instruction *Inst = *BI;
283 InstVal &IV = ValueState[Inst];
284 if (IV.isConstant()) {
285 Constant *Const = IV.getConstant();
286 // cerr << "Constant: " << Inst << " is: " << Const;
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000287
Chris Lattner221d6882002-02-12 21:07:25 +0000288 // Replaces all of the uses of a variable with uses of the constant.
289 Inst->replaceAllUsesWith(Const);
Chris Lattner138a1242001-06-27 23:38:11 +0000290
Chris Lattner0e9c5152002-05-02 20:32:51 +0000291 // Remove the operator from the list of definitions... and delete it.
292 delete BB->getInstList().remove(BI);
Chris Lattner138a1242001-06-27 23:38:11 +0000293
Chris Lattner221d6882002-02-12 21:07:25 +0000294 // Hey, we just changed something!
295 MadeChanges = true;
Chris Lattner0e9c5152002-05-02 20:32:51 +0000296
297 // Do NOT advance the iterator, skipping the next instruction...
298 continue;
299
Chris Lattner221d6882002-02-12 21:07:25 +0000300 } else if (TerminatorInst *TI = dyn_cast<TerminatorInst>(Inst)) {
Chris Lattner0fce76a2002-03-11 22:11:07 +0000301 MadeChanges |= ConstantFoldTerminator(BB, BI, TI);
Chris Lattner221d6882002-02-12 21:07:25 +0000302 }
Chris Lattner138a1242001-06-27 23:38:11 +0000303
Chris Lattner221d6882002-02-12 21:07:25 +0000304 ++BI;
Chris Lattner138a1242001-06-27 23:38:11 +0000305 }
306 }
307
Chris Lattner0dbfc052002-04-29 21:26:08 +0000308 // Reset state so that the next invokation will have empty data structures
309 BBExecutable.clear();
310 ValueState.clear();
311
Chris Lattner138a1242001-06-27 23:38:11 +0000312 // Merge identical constants last: this is important because we may have just
313 // introduced constants that already exist, and we don't want to pollute later
314 // stages with extraneous constants.
315 //
Chris Lattnerb70d82f2001-09-07 16:43:22 +0000316 return MadeChanges;
Chris Lattner138a1242001-06-27 23:38:11 +0000317}
318
319
Chris Lattner2a632552002-04-18 15:13:15 +0000320// visit Implementations - Something changed in this instruction... Either an
Chris Lattner138a1242001-06-27 23:38:11 +0000321// operand made a transition, or the instruction is newly executable. Change
322// the value type of I to reflect these changes if appropriate. This method
323// makes sure to do the following actions:
324//
325// 1. If a phi node merges two constants in, and has conflicting value coming
326// from different branches, or if the PHI node merges in an overdefined
327// value, then the PHI node becomes overdefined.
328// 2. If a phi node merges only constants in, and they all agree on value, the
329// PHI node becomes a constant value equal to that.
330// 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
331// 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
332// 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
333// 6. If a conditional branch has a value that is constant, make the selected
334// destination executable
335// 7. If a conditional branch has a value that is overdefined, make all
336// successors executable.
337//
Chris Lattner138a1242001-06-27 23:38:11 +0000338
Chris Lattner2a632552002-04-18 15:13:15 +0000339void SCCP::visitPHINode(PHINode *PN) {
340 unsigned NumValues = PN->getNumIncomingValues(), i;
341 InstVal *OperandIV = 0;
Chris Lattner138a1242001-06-27 23:38:11 +0000342
Chris Lattner2a632552002-04-18 15:13:15 +0000343 // Look at all of the executable operands of the PHI node. If any of them
344 // are overdefined, the PHI becomes overdefined as well. If they are all
345 // constant, and they agree with each other, the PHI becomes the identical
346 // constant. If they are constant and don't agree, the PHI is overdefined.
347 // If there are no executable operands, the PHI remains undefined.
348 //
349 for (i = 0; i < NumValues; ++i) {
350 if (BBExecutable.count(PN->getIncomingBlock(i))) {
351 InstVal &IV = getValueState(PN->getIncomingValue(i));
352 if (IV.isUndefined()) continue; // Doesn't influence PHI node.
353 if (IV.isOverdefined()) { // PHI node becomes overdefined!
354 markOverdefined(PN);
355 return;
356 }
Chris Lattner138a1242001-06-27 23:38:11 +0000357
Chris Lattner2a632552002-04-18 15:13:15 +0000358 if (OperandIV == 0) { // Grab the first value...
359 OperandIV = &IV;
360 } else { // Another value is being merged in!
361 // There is already a reachable operand. If we conflict with it,
362 // then the PHI node becomes overdefined. If we agree with it, we
363 // can continue on.
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000364
Chris Lattner2a632552002-04-18 15:13:15 +0000365 // Check to see if there are two different constants merging...
366 if (IV.getConstant() != OperandIV->getConstant()) {
367 // Yes there is. This means the PHI node is not constant.
368 // You must be overdefined poor PHI.
369 //
370 markOverdefined(PN); // The PHI node now becomes overdefined
371 return; // I'm done analyzing you
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000372 }
Chris Lattner138a1242001-06-27 23:38:11 +0000373 }
374 }
Chris Lattner138a1242001-06-27 23:38:11 +0000375 }
376
Chris Lattner2a632552002-04-18 15:13:15 +0000377 // If we exited the loop, this means that the PHI node only has constant
378 // arguments that agree with each other(and OperandIV is a pointer to one
379 // of their InstVal's) or OperandIV is null because there are no defined
380 // incoming arguments. If this is the case, the PHI remains undefined.
Chris Lattner138a1242001-06-27 23:38:11 +0000381 //
Chris Lattner2a632552002-04-18 15:13:15 +0000382 if (OperandIV) {
383 assert(OperandIV->isConstant() && "Should only be here for constants!");
384 markConstant(PN, OperandIV->getConstant()); // Aquire operand value
Chris Lattner138a1242001-06-27 23:38:11 +0000385 }
Chris Lattner138a1242001-06-27 23:38:11 +0000386}
387
Chris Lattner2a632552002-04-18 15:13:15 +0000388void SCCP::visitBranchInst(BranchInst *BI) {
389 if (BI->isUnconditional())
390 return; // Unconditional branches are already handled!
Chris Lattner138a1242001-06-27 23:38:11 +0000391
Chris Lattner2a632552002-04-18 15:13:15 +0000392 InstVal &BCValue = getValueState(BI->getCondition());
393 if (BCValue.isOverdefined()) {
394 // Overdefined condition variables mean the branch could go either way.
395 markExecutable(BI->getSuccessor(0));
396 markExecutable(BI->getSuccessor(1));
397 } else if (BCValue.isConstant()) {
398 // Constant condition variables mean the branch can only go a single way.
399 if (BCValue.getConstant() == ConstantBool::True)
400 markExecutable(BI->getSuccessor(0));
401 else
402 markExecutable(BI->getSuccessor(1));
403 }
404}
405
406void SCCP::visitSwitchInst(SwitchInst *SI) {
407 InstVal &SCValue = getValueState(SI->getCondition());
408 if (SCValue.isOverdefined()) { // Overdefined condition? All dests are exe
Chris Lattnerf2361c52002-04-27 03:15:45 +0000409 for(unsigned i = 0, E = SI->getNumSuccessors(); i != E; ++i)
410 markExecutable(SI->getSuccessor(i));
Chris Lattner2a632552002-04-18 15:13:15 +0000411 } else if (SCValue.isConstant()) {
412 Constant *CPV = SCValue.getConstant();
413 // Make sure to skip the "default value" which isn't a value
414 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) {
415 if (SI->getSuccessorValue(i) == CPV) {// Found the right branch...
416 markExecutable(SI->getSuccessor(i));
417 return;
418 }
419 }
420
421 // Constant value not equal to any of the branches... must execute
422 // default branch then...
423 markExecutable(SI->getDefaultDest());
424 }
425}
426
427void SCCP::visitUnaryOperator(Instruction *I) {
428 Value *V = I->getOperand(0);
429 InstVal &VState = getValueState(V);
430 if (VState.isOverdefined()) { // Inherit overdefinedness of operand
431 markOverdefined(I);
432 } else if (VState.isConstant()) { // Propogate constant value
433 Constant *Result = isa<CastInst>(I)
434 ? ConstantFoldCastInstruction(VState.getConstant(), I->getType())
435 : ConstantFoldUnaryInstruction(I->getOpcode(), VState.getConstant());
436
437 if (Result) {
438 // This instruction constant folds!
439 markConstant(I, Result);
440 } else {
441 markOverdefined(I); // Don't know how to fold this instruction. :(
442 }
443 }
444}
445
446// Handle BinaryOperators and Shift Instructions...
447void SCCP::visitBinaryOperator(Instruction *I) {
448 InstVal &V1State = getValueState(I->getOperand(0));
449 InstVal &V2State = getValueState(I->getOperand(1));
450 if (V1State.isOverdefined() || V2State.isOverdefined()) {
451 markOverdefined(I);
452 } else if (V1State.isConstant() && V2State.isConstant()) {
453 Constant *Result = ConstantFoldBinaryInstruction(I->getOpcode(),
454 V1State.getConstant(),
455 V2State.getConstant());
456 if (Result)
Chris Lattner0e9c5152002-05-02 20:32:51 +0000457 markConstant(I, Result); // This instruction constant folds!
Chris Lattner2a632552002-04-18 15:13:15 +0000458 else
459 markOverdefined(I); // Don't know how to fold this instruction. :(
460 }
461}
Chris Lattner138a1242001-06-27 23:38:11 +0000462
463// OperandChangedState - This method is invoked on all of the users of an
464// instruction that was just changed state somehow.... Based on this
465// information, we need to update the specified user of this instruction.
466//
467void SCCP::OperandChangedState(User *U) {
468 // Only instructions use other variable values!
Chris Lattner9636a912001-10-01 16:18:37 +0000469 Instruction *I = cast<Instruction>(U);
Chris Lattner138a1242001-06-27 23:38:11 +0000470 if (!BBExecutable.count(I->getParent())) return; // Inst not executable yet!
471
Chris Lattner2a632552002-04-18 15:13:15 +0000472 visit(I);
Chris Lattner138a1242001-06-27 23:38:11 +0000473}