blob: e4e1e7c42fa9e30d8fe80f24fa509a1e57248b1a [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 Lattner221d6882002-02-12 21:07:25 +0000291 // Remove the operator from the list of definitions...
292 BB->getInstList().remove(BI);
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000293
Chris Lattner221d6882002-02-12 21:07:25 +0000294 // The new constant inherits the old name of the operator...
295 if (Inst->hasName() && !Const->hasName())
Chris Lattner0dbfc052002-04-29 21:26:08 +0000296 Const->setName(Inst->getName(), F->getSymbolTableSure());
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000297
Chris Lattner221d6882002-02-12 21:07:25 +0000298 // Delete the operator now...
299 delete Inst;
Chris Lattner138a1242001-06-27 23:38:11 +0000300
Chris Lattner221d6882002-02-12 21:07:25 +0000301 // Hey, we just changed something!
302 MadeChanges = true;
303 } else if (TerminatorInst *TI = dyn_cast<TerminatorInst>(Inst)) {
Chris Lattner0fce76a2002-03-11 22:11:07 +0000304 MadeChanges |= ConstantFoldTerminator(BB, BI, TI);
Chris Lattner221d6882002-02-12 21:07:25 +0000305 }
Chris Lattner138a1242001-06-27 23:38:11 +0000306
Chris Lattner221d6882002-02-12 21:07:25 +0000307 ++BI;
Chris Lattner138a1242001-06-27 23:38:11 +0000308 }
309 }
310
Chris Lattner0dbfc052002-04-29 21:26:08 +0000311 // Reset state so that the next invokation will have empty data structures
312 BBExecutable.clear();
313 ValueState.clear();
314
Chris Lattner138a1242001-06-27 23:38:11 +0000315 // Merge identical constants last: this is important because we may have just
316 // introduced constants that already exist, and we don't want to pollute later
317 // stages with extraneous constants.
318 //
Chris Lattnerb70d82f2001-09-07 16:43:22 +0000319 return MadeChanges;
Chris Lattner138a1242001-06-27 23:38:11 +0000320}
321
322
Chris Lattner2a632552002-04-18 15:13:15 +0000323// visit Implementations - Something changed in this instruction... Either an
Chris Lattner138a1242001-06-27 23:38:11 +0000324// operand made a transition, or the instruction is newly executable. Change
325// the value type of I to reflect these changes if appropriate. This method
326// makes sure to do the following actions:
327//
328// 1. If a phi node merges two constants in, and has conflicting value coming
329// from different branches, or if the PHI node merges in an overdefined
330// value, then the PHI node becomes overdefined.
331// 2. If a phi node merges only constants in, and they all agree on value, the
332// PHI node becomes a constant value equal to that.
333// 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
334// 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
335// 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
336// 6. If a conditional branch has a value that is constant, make the selected
337// destination executable
338// 7. If a conditional branch has a value that is overdefined, make all
339// successors executable.
340//
Chris Lattner138a1242001-06-27 23:38:11 +0000341
Chris Lattner2a632552002-04-18 15:13:15 +0000342void SCCP::visitPHINode(PHINode *PN) {
343 unsigned NumValues = PN->getNumIncomingValues(), i;
344 InstVal *OperandIV = 0;
Chris Lattner138a1242001-06-27 23:38:11 +0000345
Chris Lattner2a632552002-04-18 15:13:15 +0000346 // Look at all of the executable operands of the PHI node. If any of them
347 // are overdefined, the PHI becomes overdefined as well. If they are all
348 // constant, and they agree with each other, the PHI becomes the identical
349 // constant. If they are constant and don't agree, the PHI is overdefined.
350 // If there are no executable operands, the PHI remains undefined.
351 //
352 for (i = 0; i < NumValues; ++i) {
353 if (BBExecutable.count(PN->getIncomingBlock(i))) {
354 InstVal &IV = getValueState(PN->getIncomingValue(i));
355 if (IV.isUndefined()) continue; // Doesn't influence PHI node.
356 if (IV.isOverdefined()) { // PHI node becomes overdefined!
357 markOverdefined(PN);
358 return;
359 }
Chris Lattner138a1242001-06-27 23:38:11 +0000360
Chris Lattner2a632552002-04-18 15:13:15 +0000361 if (OperandIV == 0) { // Grab the first value...
362 OperandIV = &IV;
363 } else { // Another value is being merged in!
364 // There is already a reachable operand. If we conflict with it,
365 // then the PHI node becomes overdefined. If we agree with it, we
366 // can continue on.
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000367
Chris Lattner2a632552002-04-18 15:13:15 +0000368 // Check to see if there are two different constants merging...
369 if (IV.getConstant() != OperandIV->getConstant()) {
370 // Yes there is. This means the PHI node is not constant.
371 // You must be overdefined poor PHI.
372 //
373 markOverdefined(PN); // The PHI node now becomes overdefined
374 return; // I'm done analyzing you
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000375 }
Chris Lattner138a1242001-06-27 23:38:11 +0000376 }
377 }
Chris Lattner138a1242001-06-27 23:38:11 +0000378 }
379
Chris Lattner2a632552002-04-18 15:13:15 +0000380 // If we exited the loop, this means that the PHI node only has constant
381 // arguments that agree with each other(and OperandIV is a pointer to one
382 // of their InstVal's) or OperandIV is null because there are no defined
383 // incoming arguments. If this is the case, the PHI remains undefined.
Chris Lattner138a1242001-06-27 23:38:11 +0000384 //
Chris Lattner2a632552002-04-18 15:13:15 +0000385 if (OperandIV) {
386 assert(OperandIV->isConstant() && "Should only be here for constants!");
387 markConstant(PN, OperandIV->getConstant()); // Aquire operand value
Chris Lattner138a1242001-06-27 23:38:11 +0000388 }
Chris Lattner138a1242001-06-27 23:38:11 +0000389}
390
Chris Lattner2a632552002-04-18 15:13:15 +0000391void SCCP::visitBranchInst(BranchInst *BI) {
392 if (BI->isUnconditional())
393 return; // Unconditional branches are already handled!
Chris Lattner138a1242001-06-27 23:38:11 +0000394
Chris Lattner2a632552002-04-18 15:13:15 +0000395 InstVal &BCValue = getValueState(BI->getCondition());
396 if (BCValue.isOverdefined()) {
397 // Overdefined condition variables mean the branch could go either way.
398 markExecutable(BI->getSuccessor(0));
399 markExecutable(BI->getSuccessor(1));
400 } else if (BCValue.isConstant()) {
401 // Constant condition variables mean the branch can only go a single way.
402 if (BCValue.getConstant() == ConstantBool::True)
403 markExecutable(BI->getSuccessor(0));
404 else
405 markExecutable(BI->getSuccessor(1));
406 }
407}
408
409void SCCP::visitSwitchInst(SwitchInst *SI) {
410 InstVal &SCValue = getValueState(SI->getCondition());
411 if (SCValue.isOverdefined()) { // Overdefined condition? All dests are exe
Chris Lattnerf2361c52002-04-27 03:15:45 +0000412 for(unsigned i = 0, E = SI->getNumSuccessors(); i != E; ++i)
413 markExecutable(SI->getSuccessor(i));
Chris Lattner2a632552002-04-18 15:13:15 +0000414 } else if (SCValue.isConstant()) {
415 Constant *CPV = SCValue.getConstant();
416 // Make sure to skip the "default value" which isn't a value
417 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) {
418 if (SI->getSuccessorValue(i) == CPV) {// Found the right branch...
419 markExecutable(SI->getSuccessor(i));
420 return;
421 }
422 }
423
424 // Constant value not equal to any of the branches... must execute
425 // default branch then...
426 markExecutable(SI->getDefaultDest());
427 }
428}
429
430void SCCP::visitUnaryOperator(Instruction *I) {
431 Value *V = I->getOperand(0);
432 InstVal &VState = getValueState(V);
433 if (VState.isOverdefined()) { // Inherit overdefinedness of operand
434 markOverdefined(I);
435 } else if (VState.isConstant()) { // Propogate constant value
436 Constant *Result = isa<CastInst>(I)
437 ? ConstantFoldCastInstruction(VState.getConstant(), I->getType())
438 : ConstantFoldUnaryInstruction(I->getOpcode(), VState.getConstant());
439
440 if (Result) {
441 // This instruction constant folds!
442 markConstant(I, Result);
443 } else {
444 markOverdefined(I); // Don't know how to fold this instruction. :(
445 }
446 }
447}
448
449// Handle BinaryOperators and Shift Instructions...
450void SCCP::visitBinaryOperator(Instruction *I) {
451 InstVal &V1State = getValueState(I->getOperand(0));
452 InstVal &V2State = getValueState(I->getOperand(1));
453 if (V1State.isOverdefined() || V2State.isOverdefined()) {
454 markOverdefined(I);
455 } else if (V1State.isConstant() && V2State.isConstant()) {
456 Constant *Result = ConstantFoldBinaryInstruction(I->getOpcode(),
457 V1State.getConstant(),
458 V2State.getConstant());
459 if (Result)
460 markConstant(I, Result); // This instruction constant fold!s
461 else
462 markOverdefined(I); // Don't know how to fold this instruction. :(
463 }
464}
Chris Lattner138a1242001-06-27 23:38:11 +0000465
466// OperandChangedState - This method is invoked on all of the users of an
467// instruction that was just changed state somehow.... Based on this
468// information, we need to update the specified user of this instruction.
469//
470void SCCP::OperandChangedState(User *U) {
471 // Only instructions use other variable values!
Chris Lattner9636a912001-10-01 16:18:37 +0000472 Instruction *I = cast<Instruction>(U);
Chris Lattner138a1242001-06-27 23:38:11 +0000473 if (!BBExecutable.count(I->getParent())) return; // Inst not executable yet!
474
Chris Lattner2a632552002-04-18 15:13:15 +0000475 visit(I);
Chris Lattner138a1242001-06-27 23:38:11 +0000476}