blob: ada670deb818f5516aa7657546c5f5419d997ae4 [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"
19#include "llvm/Transforms/Scalar/ConstantHandling.h"
Chris Lattner138a1242001-06-27 23:38:11 +000020#include "llvm/Method.h"
21#include "llvm/BasicBlock.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000022#include "llvm/ConstantVals.h"
Chris Lattner138a1242001-06-27 23:38:11 +000023#include "llvm/InstrTypes.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000024#include "llvm/iPHINode.h"
Chris Lattner3b7bfdb2001-07-14 06:11:51 +000025#include "llvm/iMemory.h"
Chris Lattner138a1242001-06-27 23:38:11 +000026#include "llvm/iTerminators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000027#include "llvm/iOther.h"
Chris Lattner0bd654a2001-07-08 21:18:49 +000028#include "llvm/Assembly/Writer.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000029#include "Support/STLExtras.h"
Chris Lattner138a1242001-06-27 23:38:11 +000030#include <algorithm>
31#include <map>
32#include <set>
Chris Lattner697954c2002-01-20 22:54:45 +000033#include <iostream>
34using std::cerr;
Chris Lattner138a1242001-06-27 23:38:11 +000035
Chris Lattner138a1242001-06-27 23:38:11 +000036// InstVal class - This class represents the different lattice values that an
37// instruction may occupy. It is a simple class with value semantics. The
38// potential constant value that is pointed to is owned by the constant pool
39// for the method being optimized.
40//
41class InstVal {
42 enum {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000043 undefined, // This instruction has no known value
44 constant, // This instruction has a constant value
Chris Lattner138a1242001-06-27 23:38:11 +000045 // Range, // This instruction is known to fall within a range
Chris Lattnere9bb2df2001-12-03 22:26:30 +000046 overdefined // This instruction has an unknown value
47 } LatticeValue; // The current lattice position
48 Constant *ConstantVal; // If Constant value, the current value
Chris Lattner138a1242001-06-27 23:38:11 +000049public:
Chris Lattnere9bb2df2001-12-03 22:26:30 +000050 inline InstVal() : LatticeValue(undefined), ConstantVal(0) {}
Chris Lattner138a1242001-06-27 23:38:11 +000051
52 // markOverdefined - Return true if this is a new status to be in...
53 inline bool markOverdefined() {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000054 if (LatticeValue != overdefined) {
55 LatticeValue = overdefined;
Chris Lattner138a1242001-06-27 23:38:11 +000056 return true;
57 }
58 return false;
59 }
60
61 // markConstant - Return true if this is a new status for us...
Chris Lattnere9bb2df2001-12-03 22:26:30 +000062 inline bool markConstant(Constant *V) {
63 if (LatticeValue != constant) {
64 LatticeValue = constant;
Chris Lattner138a1242001-06-27 23:38:11 +000065 ConstantVal = V;
66 return true;
67 } else {
Chris Lattnerb70d82f2001-09-07 16:43:22 +000068 assert(ConstantVal == V && "Marking constant with different value");
Chris Lattner138a1242001-06-27 23:38:11 +000069 }
70 return false;
71 }
72
Chris Lattnere9bb2df2001-12-03 22:26:30 +000073 inline bool isUndefined() const { return LatticeValue == undefined; }
74 inline bool isConstant() const { return LatticeValue == constant; }
75 inline bool isOverdefined() const { return LatticeValue == overdefined; }
Chris Lattner138a1242001-06-27 23:38:11 +000076
Chris Lattnere9bb2df2001-12-03 22:26:30 +000077 inline Constant *getConstant() const { return ConstantVal; }
Chris Lattner138a1242001-06-27 23:38:11 +000078};
79
80
81
82//===----------------------------------------------------------------------===//
83// SCCP Class
84//
85// This class does all of the work of Sparse Conditional Constant Propogation.
86// It's public interface consists of a constructor and a doSCCP() method.
87//
88class SCCP {
Chris Lattner697954c2002-01-20 22:54:45 +000089 Method *M; // The method that we are working on...
Chris Lattner138a1242001-06-27 23:38:11 +000090
Chris Lattner697954c2002-01-20 22:54:45 +000091 std::set<BasicBlock*> BBExecutable;// The basic blocks that are executable
92 std::map<Value*, InstVal> ValueState; // The state each value is in...
Chris Lattner138a1242001-06-27 23:38:11 +000093
Chris Lattner697954c2002-01-20 22:54:45 +000094 std::vector<Instruction*> InstWorkList;// The instruction work list
95 std::vector<BasicBlock*> BBWorkList; // The BasicBlock work list
Chris Lattner138a1242001-06-27 23:38:11 +000096
97 //===--------------------------------------------------------------------===//
98 // The public interface for this class
99 //
100public:
101
102 // SCCP Ctor - Save the method to operate on...
103 inline SCCP(Method *m) : M(m) {}
104
105 // doSCCP() - Run the Sparse Conditional Constant Propogation algorithm, and
106 // return true if the method was modified.
107 bool doSCCP();
108
109 //===--------------------------------------------------------------------===//
110 // The implementation of this class
111 //
112private:
113
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
144 // underdefined state... MethodArgument's should be overdefined, and constants
145 // should be marked as constants. If a value is not known to be an
146 // 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 Lattner1d87bcf2001-10-01 20:11:19 +0000154 } else if (isa<MethodArgument>(V)) { // MethodArgs 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 Lattnercb056de2001-06-29 23:56:23 +0000172 // UpdateInstruction - Something changed in this instruction... Either an
173 // operand made a transition, or the instruction is newly executable. Change
174 // the value type of I to reflect these changes if appropriate.
175 //
176 void UpdateInstruction(Instruction *I);
177
178 // OperandChangedState - This method is invoked on all of the users of an
179 // instruction that was just changed state somehow.... Based on this
180 // information, we need to update the specified user of this instruction.
181 //
182 void OperandChangedState(User *U);
183};
Chris Lattner138a1242001-06-27 23:38:11 +0000184
185
186//===----------------------------------------------------------------------===//
187// SCCP Class Implementation
188
189
190// doSCCP() - Run the Sparse Conditional Constant Propogation algorithm, and
191// return true if the method was modified.
192//
193bool SCCP::doSCCP() {
194 // Mark the first block of the method as being executable...
195 markExecutable(M->front());
196
197 // Process the work lists until their are empty!
198 while (!BBWorkList.empty() || !InstWorkList.empty()) {
199 // Process the instruction work list...
200 while (!InstWorkList.empty()) {
201 Instruction *I = InstWorkList.back();
202 InstWorkList.pop_back();
203
204 //cerr << "\nPopped off I-WL: " << I;
205
206
207 // "I" got into the work list because it either made the transition from
208 // bottom to constant, or to Overdefined.
209 //
210 // Update all of the users of this instruction's value...
211 //
212 for_each(I->use_begin(), I->use_end(),
213 bind_obj(this, &SCCP::OperandChangedState));
214 }
215
216 // Process the basic block work list...
217 while (!BBWorkList.empty()) {
218 BasicBlock *BB = BBWorkList.back();
219 BBWorkList.pop_back();
220
221 //cerr << "\nPopped off BBWL: " << BB;
222
223 // If this block only has a single successor, mark it as executable as
224 // well... if not, terminate the do loop.
225 //
226 if (BB->getTerminator()->getNumSuccessors() == 1)
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000227 markExecutable(BB->getTerminator()->getSuccessor(0));
Chris Lattner138a1242001-06-27 23:38:11 +0000228
229 // Loop over all of the instructions and notify them that they are newly
230 // executable...
231 for_each(BB->begin(), BB->end(),
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000232 bind_obj(this, &SCCP::UpdateInstruction));
Chris Lattner138a1242001-06-27 23:38:11 +0000233 }
234 }
235
236#if 0
237 for (Method::iterator BBI = M->begin(), BBEnd = M->end(); BBI != BBEnd; ++BBI)
238 if (!BBExecutable.count(*BBI))
239 cerr << "BasicBlock Dead:" << *BBI;
240#endif
241
242
243 // Iterate over all of the instructions in a method, replacing them with
244 // constants if we have found them to be of constant values.
245 //
246 bool MadeChanges = false;
Chris Lattner221d6882002-02-12 21:07:25 +0000247 for (Method::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI) {
248 BasicBlock *BB = *MI;
249 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
250 Instruction *Inst = *BI;
251 InstVal &IV = ValueState[Inst];
252 if (IV.isConstant()) {
253 Constant *Const = IV.getConstant();
254 // cerr << "Constant: " << Inst << " is: " << Const;
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000255
Chris Lattner221d6882002-02-12 21:07:25 +0000256 // Replaces all of the uses of a variable with uses of the constant.
257 Inst->replaceAllUsesWith(Const);
Chris Lattner138a1242001-06-27 23:38:11 +0000258
Chris Lattner221d6882002-02-12 21:07:25 +0000259 // Remove the operator from the list of definitions...
260 BB->getInstList().remove(BI);
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000261
Chris Lattner221d6882002-02-12 21:07:25 +0000262 // The new constant inherits the old name of the operator...
263 if (Inst->hasName() && !Const->hasName())
264 Const->setName(Inst->getName(), M->getSymbolTableSure());
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000265
Chris Lattner221d6882002-02-12 21:07:25 +0000266 // Delete the operator now...
267 delete Inst;
Chris Lattner138a1242001-06-27 23:38:11 +0000268
Chris Lattner221d6882002-02-12 21:07:25 +0000269 // Hey, we just changed something!
270 MadeChanges = true;
271 } else if (TerminatorInst *TI = dyn_cast<TerminatorInst>(Inst)) {
272 MadeChanges |= ConstantFoldTerminator(TI);
273 }
Chris Lattner138a1242001-06-27 23:38:11 +0000274
Chris Lattner221d6882002-02-12 21:07:25 +0000275 ++BI;
Chris Lattner138a1242001-06-27 23:38:11 +0000276 }
277 }
278
279 // Merge identical constants last: this is important because we may have just
280 // introduced constants that already exist, and we don't want to pollute later
281 // stages with extraneous constants.
282 //
Chris Lattnerb70d82f2001-09-07 16:43:22 +0000283 return MadeChanges;
Chris Lattner138a1242001-06-27 23:38:11 +0000284}
285
286
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000287// UpdateInstruction - Something changed in this instruction... Either an
Chris Lattner138a1242001-06-27 23:38:11 +0000288// operand made a transition, or the instruction is newly executable. Change
289// the value type of I to reflect these changes if appropriate. This method
290// makes sure to do the following actions:
291//
292// 1. If a phi node merges two constants in, and has conflicting value coming
293// from different branches, or if the PHI node merges in an overdefined
294// value, then the PHI node becomes overdefined.
295// 2. If a phi node merges only constants in, and they all agree on value, the
296// PHI node becomes a constant value equal to that.
297// 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
298// 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
299// 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
300// 6. If a conditional branch has a value that is constant, make the selected
301// destination executable
302// 7. If a conditional branch has a value that is overdefined, make all
303// successors executable.
304//
305void SCCP::UpdateInstruction(Instruction *I) {
306 InstVal &IValue = ValueState[I];
307 if (IValue.isOverdefined())
308 return; // If already overdefined, we aren't going to effect anything
309
Chris Lattnera41f50d2001-07-07 19:24:15 +0000310 switch (I->getOpcode()) {
Chris Lattner138a1242001-06-27 23:38:11 +0000311 //===-----------------------------------------------------------------===//
312 // Handle PHI nodes...
313 //
314 case Instruction::PHINode: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000315 PHINode *PN = cast<PHINode>(I);
Chris Lattner138a1242001-06-27 23:38:11 +0000316 unsigned NumValues = PN->getNumIncomingValues(), i;
317 InstVal *OperandIV = 0;
318
319 // Look at all of the executable operands of the PHI node. If any of them
320 // are overdefined, the PHI becomes overdefined as well. If they are all
321 // constant, and they agree with each other, the PHI becomes the identical
322 // constant. If they are constant and don't agree, the PHI is overdefined.
323 // If there are no executable operands, the PHI remains undefined.
324 //
325 for (i = 0; i < NumValues; ++i) {
326 if (BBExecutable.count(PN->getIncomingBlock(i))) {
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000327 InstVal &IV = getValueState(PN->getIncomingValue(i));
328 if (IV.isUndefined()) continue; // Doesn't influence PHI node.
329 if (IV.isOverdefined()) { // PHI node becomes overdefined!
330 markOverdefined(PN);
331 return;
332 }
Chris Lattner138a1242001-06-27 23:38:11 +0000333
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000334 if (OperandIV == 0) { // Grab the first value...
335 OperandIV = &IV;
336 } else { // Another value is being merged in!
337 // There is already a reachable operand. If we conflict with it,
338 // then the PHI node becomes overdefined. If we agree with it, we
339 // can continue on.
340
341 // Check to see if there are two different constants merging...
342 if (IV.getConstant() != OperandIV->getConstant()) {
343 // Yes there is. This means the PHI node is not constant.
344 // You must be overdefined poor PHI.
345 //
346 markOverdefined(I); // The PHI node now becomes overdefined
347 return; // I'm done analyzing you
348 }
349 }
Chris Lattner138a1242001-06-27 23:38:11 +0000350 }
351 }
352
353 // If we exited the loop, this means that the PHI node only has constant
354 // arguments that agree with each other(and OperandIV is a pointer to one
355 // of their InstVal's) or OperandIV is null because there are no defined
356 // incoming arguments. If this is the case, the PHI remains undefined.
357 //
358 if (OperandIV) {
359 assert(OperandIV->isConstant() && "Should only be here for constants!");
360 markConstant(I, OperandIV->getConstant()); // Aquire operand value
361 }
362 return;
363 }
364
365 //===-----------------------------------------------------------------===//
366 // Handle instructions that unconditionally provide overdefined values...
367 //
368 case Instruction::Malloc:
369 case Instruction::Free:
370 case Instruction::Alloca:
371 case Instruction::Load:
372 case Instruction::Store:
Chris Lattner93d39d22001-10-13 06:52:41 +0000373 // TODO: getfield
Chris Lattner138a1242001-06-27 23:38:11 +0000374 case Instruction::Call:
Chris Lattner93d39d22001-10-13 06:52:41 +0000375 case Instruction::Invoke:
Chris Lattner138a1242001-06-27 23:38:11 +0000376 markOverdefined(I); // Memory and call's are all overdefined
377 return;
378
379 //===-----------------------------------------------------------------===//
380 // Handle Terminator instructions...
381 //
382 case Instruction::Ret: return; // Method return doesn't affect anything
383 case Instruction::Br: { // Handle conditional branches...
Chris Lattnerb00c5822001-10-02 03:41:24 +0000384 BranchInst *BI = cast<BranchInst>(I);
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000385 if (BI->isUnconditional())
Chris Lattner138a1242001-06-27 23:38:11 +0000386 return; // Unconditional branches are already handled!
387
388 InstVal &BCValue = getValueState(BI->getCondition());
389 if (BCValue.isOverdefined()) {
390 // Overdefined condition variables mean the branch could go either way.
391 markExecutable(BI->getSuccessor(0));
392 markExecutable(BI->getSuccessor(1));
393 } else if (BCValue.isConstant()) {
394 // Constant condition variables mean the branch can only go a single way.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000395 ConstantBool *CPB = cast<ConstantBool>(BCValue.getConstant());
Chris Lattner138a1242001-06-27 23:38:11 +0000396 if (CPB->getValue()) // If the branch condition is TRUE...
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000397 markExecutable(BI->getSuccessor(0));
Chris Lattner138a1242001-06-27 23:38:11 +0000398 else // Else if the br cond is FALSE...
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000399 markExecutable(BI->getSuccessor(1));
Chris Lattner138a1242001-06-27 23:38:11 +0000400 }
401 return;
402 }
403
404 case Instruction::Switch: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000405 SwitchInst *SI = cast<SwitchInst>(I);
Chris Lattner138a1242001-06-27 23:38:11 +0000406 InstVal &SCValue = getValueState(SI->getCondition());
407 if (SCValue.isOverdefined()) { // Overdefined condition? All dests are exe
408 for(unsigned i = 0; BasicBlock *Succ = SI->getSuccessor(i); ++i)
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000409 markExecutable(Succ);
Chris Lattner138a1242001-06-27 23:38:11 +0000410 } else if (SCValue.isConstant()) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000411 Constant *CPV = SCValue.getConstant();
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000412 // Make sure to skip the "default value" which isn't a value
413 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) {
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000414 if (SI->getSuccessorValue(i) == CPV) {// Found the right branch...
415 markExecutable(SI->getSuccessor(i));
416 return;
417 }
Chris Lattner138a1242001-06-27 23:38:11 +0000418 }
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000419
420 // Constant value not equal to any of the branches... must execute
Chris Lattner138a1242001-06-27 23:38:11 +0000421 // default branch then...
422 markExecutable(SI->getDefaultDest());
423 }
424 return;
425 }
426
427 default: break; // Handle math operators as groups.
Chris Lattnera41f50d2001-07-07 19:24:15 +0000428 } // end switch(I->getOpcode())
Chris Lattner138a1242001-06-27 23:38:11 +0000429
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000430
Chris Lattner138a1242001-06-27 23:38:11 +0000431 //===-------------------------------------------------------------------===//
432 // Handle Unary instructions...
Chris Lattner3b7bfdb2001-07-14 06:11:51 +0000433 // Also treated as unary here, are cast instructions and getelementptr
434 // instructions on struct* operands.
Chris Lattner138a1242001-06-27 23:38:11 +0000435 //
Chris Lattnerb00c5822001-10-02 03:41:24 +0000436 if (isa<UnaryOperator>(I) || isa<CastInst>(I) ||
437 (isa<GetElementPtrInst>(I) &&
438 cast<GetElementPtrInst>(I)->isStructSelector())) {
Chris Lattner3b7bfdb2001-07-14 06:11:51 +0000439
Chris Lattner138a1242001-06-27 23:38:11 +0000440 Value *V = I->getOperand(0);
441 InstVal &VState = getValueState(V);
442 if (VState.isOverdefined()) { // Inherit overdefinedness of operand
443 markOverdefined(I);
444 } else if (VState.isConstant()) { // Propogate constant value
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000445 Constant *Result = isa<CastInst>(I)
Chris Lattner59b6b8e2002-01-21 23:17:48 +0000446 ? ConstantFoldCastInstruction(VState.getConstant(), I->getType())
447 : ConstantFoldUnaryInstruction(I->getOpcode(), VState.getConstant());
Chris Lattner138a1242001-06-27 23:38:11 +0000448
449 if (Result) {
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000450 // This instruction constant folds!
451 markConstant(I, Result);
Chris Lattner138a1242001-06-27 23:38:11 +0000452 } else {
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000453 markOverdefined(I); // Don't know how to fold this instruction. :(
Chris Lattner138a1242001-06-27 23:38:11 +0000454 }
455 }
456 return;
457 }
458
459 //===-----------------------------------------------------------------===//
460 // Handle Binary instructions...
461 //
Chris Lattnerb00c5822001-10-02 03:41:24 +0000462 if (isa<BinaryOperator>(I) || isa<ShiftInst>(I)) {
Chris Lattner138a1242001-06-27 23:38:11 +0000463 Value *V1 = I->getOperand(0);
464 Value *V2 = I->getOperand(1);
465
466 InstVal &V1State = getValueState(V1);
467 InstVal &V2State = getValueState(V2);
468 if (V1State.isOverdefined() || V2State.isOverdefined()) {
469 markOverdefined(I);
470 } else if (V1State.isConstant() && V2State.isConstant()) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000471 Constant *Result =
Chris Lattner59b6b8e2002-01-21 23:17:48 +0000472 ConstantFoldBinaryInstruction(I->getOpcode(),
473 V1State.getConstant(),
474 V2State.getConstant());
Chris Lattner138a1242001-06-27 23:38:11 +0000475 if (Result) {
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000476 // This instruction constant folds!
477 markConstant(I, Result);
Chris Lattner138a1242001-06-27 23:38:11 +0000478 } else {
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000479 markOverdefined(I); // Don't know how to fold this instruction. :(
Chris Lattner138a1242001-06-27 23:38:11 +0000480 }
481 }
482 return;
483 }
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000484
Chris Lattner138a1242001-06-27 23:38:11 +0000485 // Shouldn't get here... either the switch statement or one of the group
486 // handlers should have kicked in...
487 //
488 cerr << "SCCP: Don't know how to handle: " << I;
489 markOverdefined(I); // Just in case
490}
491
492
493
494// OperandChangedState - This method is invoked on all of the users of an
495// instruction that was just changed state somehow.... Based on this
496// information, we need to update the specified user of this instruction.
497//
498void SCCP::OperandChangedState(User *U) {
499 // Only instructions use other variable values!
Chris Lattner9636a912001-10-01 16:18:37 +0000500 Instruction *I = cast<Instruction>(U);
Chris Lattner138a1242001-06-27 23:38:11 +0000501 if (!BBExecutable.count(I->getParent())) return; // Inst not executable yet!
502
503 UpdateInstruction(I);
504}
505
506
Chris Lattner138a1242001-06-27 23:38:11 +0000507// DoSparseConditionalConstantProp - Use Sparse Conditional Constant Propogation
508// to prove whether a value is constant and whether blocks are used.
509//
Chris Lattner59b6b8e2002-01-21 23:17:48 +0000510bool SCCPPass::doSCCP(Method *M) {
Chris Lattner138a1242001-06-27 23:38:11 +0000511 SCCP S(M);
512 return S.doSCCP();
513}