Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 1 | //===- 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 Lattner | 2a88bb7 | 2002-08-30 23:39:00 +0000 | [diff] [blame] | 9 | // * Proves conditional branches to be unconditional |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 10 | // |
| 11 | // Notice that: |
| 12 | // * This pass has a habit of making definitions be dead. It is a good idea |
| 13 | // to to run a DCE pass sometime after running this pass. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | 022103b | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 968ddc9 | 2002-04-08 20:18:09 +0000 | [diff] [blame] | 18 | #include "llvm/ConstantHandling.h" |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 19 | #include "llvm/Function.h" |
Chris Lattner | 7061dc5 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 20 | #include "llvm/iPHINode.h" |
Chris Lattner | 3b7bfdb | 2001-07-14 06:11:51 +0000 | [diff] [blame] | 21 | #include "llvm/iMemory.h" |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 22 | #include "llvm/iTerminators.h" |
Chris Lattner | 7061dc5 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 23 | #include "llvm/iOther.h" |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 24 | #include "llvm/Pass.h" |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 25 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 26 | #include "Support/STLExtras.h" |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 27 | #include "Support/Statistic.h" |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 28 | #include <algorithm> |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 29 | #include <set> |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 30 | using std::cerr; |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 32 | // InstVal class - This class represents the different lattice values that an |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 33 | // instruction may occupy. It is a simple class with value semantics. |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 34 | // |
Chris Lattner | 0dbfc05 | 2002-04-29 21:26:08 +0000 | [diff] [blame] | 35 | namespace { |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 36 | Statistic<> NumInstRemoved("sccp", "Number of instructions removed"); |
| 37 | |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 38 | class InstVal { |
| 39 | enum { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 40 | undefined, // This instruction has no known value |
| 41 | constant, // This instruction has a constant value |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 42 | overdefined // This instruction has an unknown value |
| 43 | } LatticeValue; // The current lattice position |
| 44 | Constant *ConstantVal; // If Constant value, the current value |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 45 | public: |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 46 | inline InstVal() : LatticeValue(undefined), ConstantVal(0) {} |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 47 | |
| 48 | // markOverdefined - Return true if this is a new status to be in... |
| 49 | inline bool markOverdefined() { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 50 | if (LatticeValue != overdefined) { |
| 51 | LatticeValue = overdefined; |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 52 | return true; |
| 53 | } |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | // markConstant - Return true if this is a new status for us... |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 58 | inline bool markConstant(Constant *V) { |
| 59 | if (LatticeValue != constant) { |
| 60 | LatticeValue = constant; |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 61 | ConstantVal = V; |
| 62 | return true; |
| 63 | } else { |
Chris Lattner | b70d82f | 2001-09-07 16:43:22 +0000 | [diff] [blame] | 64 | assert(ConstantVal == V && "Marking constant with different value"); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 65 | } |
| 66 | return false; |
| 67 | } |
| 68 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 69 | inline bool isUndefined() const { return LatticeValue == undefined; } |
| 70 | inline bool isConstant() const { return LatticeValue == constant; } |
| 71 | inline bool isOverdefined() const { return LatticeValue == overdefined; } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 72 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 73 | inline Constant *getConstant() const { return ConstantVal; } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
Chris Lattner | 0dbfc05 | 2002-04-29 21:26:08 +0000 | [diff] [blame] | 76 | } // end anonymous namespace |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 77 | |
| 78 | |
| 79 | //===----------------------------------------------------------------------===// |
| 80 | // SCCP Class |
| 81 | // |
| 82 | // This class does all of the work of Sparse Conditional Constant Propogation. |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 83 | // |
Chris Lattner | 0dbfc05 | 2002-04-29 21:26:08 +0000 | [diff] [blame] | 84 | namespace { |
| 85 | class SCCP : public FunctionPass, public InstVisitor<SCCP> { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 86 | std::set<BasicBlock*> BBExecutable;// The basic blocks that are executable |
| 87 | std::map<Value*, InstVal> ValueState; // The state each value is in... |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 071d0ad | 2002-05-07 04:29:32 +0000 | [diff] [blame] | 89 | std::vector<Instruction*> InstWorkList;// The instruction work list |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 90 | std::vector<BasicBlock*> BBWorkList; // The BasicBlock work list |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 91 | public: |
| 92 | |
Chris Lattner | 0dbfc05 | 2002-04-29 21:26:08 +0000 | [diff] [blame] | 93 | // runOnFunction - Run the Sparse Conditional Constant Propogation algorithm, |
| 94 | // and return true if the function was modified. |
| 95 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 96 | bool runOnFunction(Function &F); |
Chris Lattner | 0dbfc05 | 2002-04-29 21:26:08 +0000 | [diff] [blame] | 97 | |
| 98 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | cb2610e | 2002-10-21 20:00:28 +0000 | [diff] [blame^] | 99 | AU.setPreservesCFG(); |
Chris Lattner | 0dbfc05 | 2002-04-29 21:26:08 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 102 | |
| 103 | //===--------------------------------------------------------------------===// |
| 104 | // The implementation of this class |
| 105 | // |
| 106 | private: |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 107 | friend class InstVisitor<SCCP>; // Allow callbacks from visitor |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 108 | |
| 109 | // markValueOverdefined - Make a value be marked as "constant". If the value |
| 110 | // is not already a constant, add it to the instruction work list so that |
| 111 | // the users of the instruction are updated later. |
| 112 | // |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 113 | inline bool markConstant(Instruction *I, Constant *V) { |
Chris Lattner | f016ea4 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 114 | DEBUG(cerr << "markConstant: " << V << " = " << I); |
Chris Lattner | 59f0ce2 | 2002-05-02 21:18:01 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 116 | if (ValueState[I].markConstant(V)) { |
Chris Lattner | 071d0ad | 2002-05-07 04:29:32 +0000 | [diff] [blame] | 117 | InstWorkList.push_back(I); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 118 | return true; |
| 119 | } |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | // markValueOverdefined - Make a value be marked as "overdefined". If the |
| 124 | // value is not already overdefined, add it to the instruction work list so |
| 125 | // that the users of the instruction are updated later. |
| 126 | // |
| 127 | inline bool markOverdefined(Value *V) { |
| 128 | if (ValueState[V].markOverdefined()) { |
Chris Lattner | 9636a91 | 2001-10-01 16:18:37 +0000 | [diff] [blame] | 129 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | f016ea4 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 130 | DEBUG(cerr << "markOverdefined: " << V); |
Chris Lattner | 071d0ad | 2002-05-07 04:29:32 +0000 | [diff] [blame] | 131 | InstWorkList.push_back(I); // Only instructions go on the work list |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 132 | } |
| 133 | return true; |
| 134 | } |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | // getValueState - Return the InstVal object that corresponds to the value. |
| 139 | // This function is neccesary because not all values should start out in the |
Chris Lattner | 73e2142 | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 140 | // underdefined state... Argument's should be overdefined, and |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 141 | // constants should be marked as constants. If a value is not known to be an |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 142 | // Instruction object, then use this accessor to get its value from the map. |
| 143 | // |
| 144 | inline InstVal &getValueState(Value *V) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 145 | std::map<Value*, InstVal>::iterator I = ValueState.find(V); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 146 | if (I != ValueState.end()) return I->second; // Common case, in the map |
| 147 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 148 | if (Constant *CPV = dyn_cast<Constant>(V)) { // Constants are constant |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 149 | ValueState[CPV].markConstant(CPV); |
Chris Lattner | 73e2142 | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 150 | } else if (isa<Argument>(V)) { // Arguments are overdefined |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 151 | ValueState[V].markOverdefined(); |
Chris Lattner | 2a88bb7 | 2002-08-30 23:39:00 +0000 | [diff] [blame] | 152 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 153 | // The address of a global is a constant... |
| 154 | ValueState[V].markConstant(ConstantPointerRef::get(GV)); |
| 155 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 156 | // All others are underdefined by default... |
| 157 | return ValueState[V]; |
| 158 | } |
| 159 | |
| 160 | // markExecutable - Mark a basic block as executable, adding it to the BB |
| 161 | // work list if it is not already executable... |
| 162 | // |
| 163 | void markExecutable(BasicBlock *BB) { |
| 164 | if (BBExecutable.count(BB)) return; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 165 | DEBUG(cerr << "Marking BB Executable: " << *BB); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 166 | BBExecutable.insert(BB); // Basic block is executable! |
| 167 | BBWorkList.push_back(BB); // Add the block to the work list! |
| 168 | } |
| 169 | |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 170 | |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 171 | // visit implementations - Something changed in this instruction... Either an |
Chris Lattner | cb056de | 2001-06-29 23:56:23 +0000 | [diff] [blame] | 172 | // operand made a transition, or the instruction is newly executable. Change |
| 173 | // the value type of I to reflect these changes if appropriate. |
| 174 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 175 | void visitPHINode(PHINode &I); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 176 | |
| 177 | // Terminators |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 178 | void visitReturnInst(ReturnInst &I) { /*does not have an effect*/ } |
| 179 | void visitTerminatorInst(TerminatorInst &TI); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 180 | |
Chris Lattner | b804760 | 2002-08-14 17:53:45 +0000 | [diff] [blame] | 181 | void visitCastInst(CastInst &I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 182 | void visitBinaryOperator(Instruction &I); |
| 183 | void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); } |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 184 | |
| 185 | // Instructions that cannot be folded away... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 186 | void visitStoreInst (Instruction &I) { /*returns void*/ } |
Chris Lattner | cc63f1c | 2002-08-22 23:37:20 +0000 | [diff] [blame] | 187 | void visitLoadInst (Instruction &I) { markOverdefined(&I); } |
Chris Lattner | 2a88bb7 | 2002-08-30 23:39:00 +0000 | [diff] [blame] | 188 | void visitGetElementPtrInst(GetElementPtrInst &I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 189 | void visitCallInst (Instruction &I) { markOverdefined(&I); } |
| 190 | void visitInvokeInst (Instruction &I) { markOverdefined(&I); } |
| 191 | void visitAllocationInst(Instruction &I) { markOverdefined(&I); } |
| 192 | void visitFreeInst (Instruction &I) { /*returns void*/ } |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 194 | void visitInstruction(Instruction &I) { |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 195 | // If a new instruction is added to LLVM that we don't handle... |
| 196 | cerr << "SCCP: Don't know how to handle: " << I; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 197 | markOverdefined(&I); // Just in case |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 198 | } |
Chris Lattner | cb056de | 2001-06-29 23:56:23 +0000 | [diff] [blame] | 199 | |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 200 | // getFeasibleSuccessors - Return a vector of booleans to indicate which |
| 201 | // successors are reachable from a given terminator instruction. |
| 202 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 203 | void getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs); |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 204 | |
Chris Lattner | 59f0ce2 | 2002-05-02 21:18:01 +0000 | [diff] [blame] | 205 | // isEdgeFeasible - Return true if the control flow edge from the 'From' basic |
| 206 | // block to the 'To' basic block is currently feasible... |
| 207 | // |
| 208 | bool isEdgeFeasible(BasicBlock *From, BasicBlock *To); |
| 209 | |
Chris Lattner | cb056de | 2001-06-29 23:56:23 +0000 | [diff] [blame] | 210 | // OperandChangedState - This method is invoked on all of the users of an |
| 211 | // instruction that was just changed state somehow.... Based on this |
| 212 | // information, we need to update the specified user of this instruction. |
| 213 | // |
Chris Lattner | 59f0ce2 | 2002-05-02 21:18:01 +0000 | [diff] [blame] | 214 | void OperandChangedState(User *U) { |
| 215 | // Only instructions use other variable values! |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 216 | Instruction &I = cast<Instruction>(*U); |
| 217 | if (!BBExecutable.count(I.getParent())) return;// Inst not executable yet! |
Chris Lattner | 59f0ce2 | 2002-05-02 21:18:01 +0000 | [diff] [blame] | 218 | visit(I); |
| 219 | } |
Chris Lattner | cb056de | 2001-06-29 23:56:23 +0000 | [diff] [blame] | 220 | }; |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 221 | |
Chris Lattner | a6275cc | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 222 | RegisterOpt<SCCP> X("sccp", "Sparse Conditional Constant Propogation"); |
Chris Lattner | 0dbfc05 | 2002-04-29 21:26:08 +0000 | [diff] [blame] | 223 | } // end anonymous namespace |
| 224 | |
| 225 | |
| 226 | // createSCCPPass - This is the public interface to this file... |
| 227 | // |
| 228 | Pass *createSCCPPass() { |
| 229 | return new SCCP(); |
| 230 | } |
| 231 | |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 232 | |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 233 | //===----------------------------------------------------------------------===// |
| 234 | // SCCP Class Implementation |
| 235 | |
| 236 | |
Chris Lattner | 0dbfc05 | 2002-04-29 21:26:08 +0000 | [diff] [blame] | 237 | // runOnFunction() - Run the Sparse Conditional Constant Propogation algorithm, |
| 238 | // and return true if the function was modified. |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 239 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 240 | bool SCCP::runOnFunction(Function &F) { |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 241 | // Mark the first block of the function as being executable... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 242 | markExecutable(&F.front()); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 243 | |
| 244 | // Process the work lists until their are empty! |
| 245 | while (!BBWorkList.empty() || !InstWorkList.empty()) { |
| 246 | // Process the instruction work list... |
| 247 | while (!InstWorkList.empty()) { |
Chris Lattner | 071d0ad | 2002-05-07 04:29:32 +0000 | [diff] [blame] | 248 | Instruction *I = InstWorkList.back(); |
| 249 | InstWorkList.pop_back(); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 250 | |
Chris Lattner | f016ea4 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 251 | DEBUG(cerr << "\nPopped off I-WL: " << I); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 252 | |
| 253 | |
| 254 | // "I" got into the work list because it either made the transition from |
| 255 | // bottom to constant, or to Overdefined. |
| 256 | // |
| 257 | // Update all of the users of this instruction's value... |
| 258 | // |
| 259 | for_each(I->use_begin(), I->use_end(), |
| 260 | bind_obj(this, &SCCP::OperandChangedState)); |
| 261 | } |
| 262 | |
| 263 | // Process the basic block work list... |
| 264 | while (!BBWorkList.empty()) { |
| 265 | BasicBlock *BB = BBWorkList.back(); |
| 266 | BBWorkList.pop_back(); |
| 267 | |
Chris Lattner | f016ea4 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 268 | DEBUG(cerr << "\nPopped off BBWL: " << BB); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 269 | |
| 270 | // If this block only has a single successor, mark it as executable as |
| 271 | // well... if not, terminate the do loop. |
| 272 | // |
| 273 | if (BB->getTerminator()->getNumSuccessors() == 1) |
Chris Lattner | 5b7d42b | 2001-11-26 18:57:38 +0000 | [diff] [blame] | 274 | markExecutable(BB->getTerminator()->getSuccessor(0)); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 276 | // Notify all instructions in this basic block that they are newly |
| 277 | // executable. |
| 278 | visit(BB); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Chris Lattner | f016ea4 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 282 | if (DebugFlag) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 283 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
| 284 | if (!BBExecutable.count(I)) |
Chris Lattner | f016ea4 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 285 | cerr << "BasicBlock Dead:" << *I; |
| 286 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 287 | |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 288 | // Iterate over all of the instructions in a function, replacing them with |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 289 | // constants if we have found them to be of constant values. |
| 290 | // |
| 291 | bool MadeChanges = false; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 292 | for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB) |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 293 | for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 294 | Instruction &Inst = *BI; |
| 295 | InstVal &IV = ValueState[&Inst]; |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 296 | if (IV.isConstant()) { |
| 297 | Constant *Const = IV.getConstant(); |
Chris Lattner | f016ea4 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 298 | DEBUG(cerr << "Constant: " << Const << " = " << Inst); |
Chris Lattner | 5b7d42b | 2001-11-26 18:57:38 +0000 | [diff] [blame] | 299 | |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 300 | // Replaces all of the uses of a variable with uses of the constant. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 301 | Inst.replaceAllUsesWith(Const); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 302 | |
Chris Lattner | 0e9c515 | 2002-05-02 20:32:51 +0000 | [diff] [blame] | 303 | // Remove the operator from the list of definitions... and delete it. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 304 | BI = BB->getInstList().erase(BI); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 305 | |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 306 | // Hey, we just changed something! |
| 307 | MadeChanges = true; |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 308 | ++NumInstRemoved; |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 309 | } else { |
| 310 | ++BI; |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 311 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 312 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 313 | |
Chris Lattner | 59f0ce2 | 2002-05-02 21:18:01 +0000 | [diff] [blame] | 314 | // Reset state so that the next invocation will have empty data structures |
Chris Lattner | 0dbfc05 | 2002-04-29 21:26:08 +0000 | [diff] [blame] | 315 | BBExecutable.clear(); |
| 316 | ValueState.clear(); |
| 317 | |
Chris Lattner | b70d82f | 2001-09-07 16:43:22 +0000 | [diff] [blame] | 318 | return MadeChanges; |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 321 | |
| 322 | // getFeasibleSuccessors - Return a vector of booleans to indicate which |
| 323 | // successors are reachable from a given terminator instruction. |
| 324 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 325 | void SCCP::getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs) { |
| 326 | assert(Succs.size() == TI.getNumSuccessors() && "Succs vector wrong size!"); |
| 327 | if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) { |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 328 | if (BI->isUnconditional()) { |
| 329 | Succs[0] = true; |
| 330 | } else { |
| 331 | InstVal &BCValue = getValueState(BI->getCondition()); |
| 332 | if (BCValue.isOverdefined()) { |
| 333 | // Overdefined condition variables mean the branch could go either way. |
| 334 | Succs[0] = Succs[1] = true; |
| 335 | } else if (BCValue.isConstant()) { |
| 336 | // Constant condition variables mean the branch can only go a single way |
| 337 | Succs[BCValue.getConstant() == ConstantBool::False] = true; |
| 338 | } |
| 339 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 340 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(&TI)) { |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 341 | // Invoke instructions successors are always executable. |
| 342 | Succs[0] = Succs[1] = true; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 343 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) { |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 344 | InstVal &SCValue = getValueState(SI->getCondition()); |
| 345 | if (SCValue.isOverdefined()) { // Overdefined condition? |
| 346 | // All destinations are executable! |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 347 | Succs.assign(TI.getNumSuccessors(), true); |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 348 | } else if (SCValue.isConstant()) { |
| 349 | Constant *CPV = SCValue.getConstant(); |
| 350 | // Make sure to skip the "default value" which isn't a value |
| 351 | for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) { |
| 352 | if (SI->getSuccessorValue(i) == CPV) {// Found the right branch... |
| 353 | Succs[i] = true; |
| 354 | return; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | // Constant value not equal to any of the branches... must execute |
| 359 | // default branch then... |
| 360 | Succs[0] = true; |
| 361 | } |
| 362 | } else { |
| 363 | cerr << "SCCP: Don't know how to handle: " << TI; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 364 | Succs.assign(TI.getNumSuccessors(), true); |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
| 368 | |
Chris Lattner | 59f0ce2 | 2002-05-02 21:18:01 +0000 | [diff] [blame] | 369 | // isEdgeFeasible - Return true if the control flow edge from the 'From' basic |
| 370 | // block to the 'To' basic block is currently feasible... |
| 371 | // |
| 372 | bool SCCP::isEdgeFeasible(BasicBlock *From, BasicBlock *To) { |
| 373 | assert(BBExecutable.count(To) && "Dest should always be alive!"); |
| 374 | |
| 375 | // Make sure the source basic block is executable!! |
| 376 | if (!BBExecutable.count(From)) return false; |
| 377 | |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 378 | // Check to make sure this edge itself is actually feasible now... |
| 379 | TerminatorInst *FT = From->getTerminator(); |
| 380 | std::vector<bool> SuccFeasible(FT->getNumSuccessors()); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 381 | getFeasibleSuccessors(*FT, SuccFeasible); |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 382 | |
| 383 | // Check all edges from From to To. If any are feasible, return true. |
| 384 | for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i) |
| 385 | if (FT->getSuccessor(i) == To && SuccFeasible[i]) |
| 386 | return true; |
| 387 | |
| 388 | // Otherwise, none of the edges are actually feasible at this time... |
| 389 | return false; |
Chris Lattner | 59f0ce2 | 2002-05-02 21:18:01 +0000 | [diff] [blame] | 390 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 391 | |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 392 | // visit Implementations - Something changed in this instruction... Either an |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 393 | // operand made a transition, or the instruction is newly executable. Change |
| 394 | // the value type of I to reflect these changes if appropriate. This method |
| 395 | // makes sure to do the following actions: |
| 396 | // |
| 397 | // 1. If a phi node merges two constants in, and has conflicting value coming |
| 398 | // from different branches, or if the PHI node merges in an overdefined |
| 399 | // value, then the PHI node becomes overdefined. |
| 400 | // 2. If a phi node merges only constants in, and they all agree on value, the |
| 401 | // PHI node becomes a constant value equal to that. |
| 402 | // 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant |
| 403 | // 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined |
| 404 | // 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined |
| 405 | // 6. If a conditional branch has a value that is constant, make the selected |
| 406 | // destination executable |
| 407 | // 7. If a conditional branch has a value that is overdefined, make all |
| 408 | // successors executable. |
| 409 | // |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 410 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 411 | void SCCP::visitPHINode(PHINode &PN) { |
| 412 | unsigned NumValues = PN.getNumIncomingValues(), i; |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 413 | InstVal *OperandIV = 0; |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 414 | |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 415 | // Look at all of the executable operands of the PHI node. If any of them |
| 416 | // are overdefined, the PHI becomes overdefined as well. If they are all |
| 417 | // constant, and they agree with each other, the PHI becomes the identical |
| 418 | // constant. If they are constant and don't agree, the PHI is overdefined. |
| 419 | // If there are no executable operands, the PHI remains undefined. |
| 420 | // |
| 421 | for (i = 0; i < NumValues; ++i) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 422 | if (isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent())) { |
| 423 | InstVal &IV = getValueState(PN.getIncomingValue(i)); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 424 | if (IV.isUndefined()) continue; // Doesn't influence PHI node. |
| 425 | if (IV.isOverdefined()) { // PHI node becomes overdefined! |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 426 | markOverdefined(&PN); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 427 | return; |
| 428 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 429 | |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 430 | if (OperandIV == 0) { // Grab the first value... |
| 431 | OperandIV = &IV; |
| 432 | } else { // Another value is being merged in! |
| 433 | // There is already a reachable operand. If we conflict with it, |
| 434 | // then the PHI node becomes overdefined. If we agree with it, we |
| 435 | // can continue on. |
Chris Lattner | 5b7d42b | 2001-11-26 18:57:38 +0000 | [diff] [blame] | 436 | |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 437 | // Check to see if there are two different constants merging... |
| 438 | if (IV.getConstant() != OperandIV->getConstant()) { |
| 439 | // Yes there is. This means the PHI node is not constant. |
| 440 | // You must be overdefined poor PHI. |
| 441 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 442 | markOverdefined(&PN); // The PHI node now becomes overdefined |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 443 | return; // I'm done analyzing you |
Chris Lattner | 5b7d42b | 2001-11-26 18:57:38 +0000 | [diff] [blame] | 444 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 445 | } |
| 446 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 449 | // If we exited the loop, this means that the PHI node only has constant |
| 450 | // arguments that agree with each other(and OperandIV is a pointer to one |
| 451 | // of their InstVal's) or OperandIV is null because there are no defined |
| 452 | // incoming arguments. If this is the case, the PHI remains undefined. |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 453 | // |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 454 | if (OperandIV) { |
| 455 | assert(OperandIV->isConstant() && "Should only be here for constants!"); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 456 | markConstant(&PN, OperandIV->getConstant()); // Aquire operand value |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 457 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 460 | void SCCP::visitTerminatorInst(TerminatorInst &TI) { |
| 461 | std::vector<bool> SuccFeasible(TI.getNumSuccessors()); |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 462 | getFeasibleSuccessors(TI, SuccFeasible); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 463 | |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 464 | // Mark all feasible successors executable... |
| 465 | for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i) |
Chris Lattner | 7f9412b | 2002-05-22 16:07:20 +0000 | [diff] [blame] | 466 | if (SuccFeasible[i]) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 467 | BasicBlock *Succ = TI.getSuccessor(i); |
Chris Lattner | 7f9412b | 2002-05-22 16:07:20 +0000 | [diff] [blame] | 468 | markExecutable(Succ); |
| 469 | |
| 470 | // Visit all of the PHI nodes that merge values from this block... |
| 471 | // Because this edge may be new executable, and PHI nodes that used to be |
| 472 | // constant now may not be. |
| 473 | // |
| 474 | for (BasicBlock::iterator I = Succ->begin(); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 475 | PHINode *PN = dyn_cast<PHINode>(&*I); ++I) |
| 476 | visitPHINode(*PN); |
Chris Lattner | 7f9412b | 2002-05-22 16:07:20 +0000 | [diff] [blame] | 477 | } |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Chris Lattner | b804760 | 2002-08-14 17:53:45 +0000 | [diff] [blame] | 480 | void SCCP::visitCastInst(CastInst &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 481 | Value *V = I.getOperand(0); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 482 | InstVal &VState = getValueState(V); |
| 483 | if (VState.isOverdefined()) { // Inherit overdefinedness of operand |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 484 | markOverdefined(&I); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 485 | } else if (VState.isConstant()) { // Propogate constant value |
Chris Lattner | b804760 | 2002-08-14 17:53:45 +0000 | [diff] [blame] | 486 | Constant *Result = |
| 487 | ConstantFoldCastInstruction(VState.getConstant(), I.getType()); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 488 | |
| 489 | if (Result) { |
| 490 | // This instruction constant folds! |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 491 | markConstant(&I, Result); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 492 | } else { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 493 | markOverdefined(&I); // Don't know how to fold this instruction. :( |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | // Handle BinaryOperators and Shift Instructions... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 499 | void SCCP::visitBinaryOperator(Instruction &I) { |
| 500 | InstVal &V1State = getValueState(I.getOperand(0)); |
| 501 | InstVal &V2State = getValueState(I.getOperand(1)); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 502 | if (V1State.isOverdefined() || V2State.isOverdefined()) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 503 | markOverdefined(&I); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 504 | } else if (V1State.isConstant() && V2State.isConstant()) { |
Chris Lattner | 4c1061f | 2002-05-06 03:01:37 +0000 | [diff] [blame] | 505 | Constant *Result = 0; |
| 506 | if (isa<BinaryOperator>(I)) |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 507 | Result = ConstantFoldBinaryInstruction(I.getOpcode(), |
Chris Lattner | 4c1061f | 2002-05-06 03:01:37 +0000 | [diff] [blame] | 508 | V1State.getConstant(), |
| 509 | V2State.getConstant()); |
| 510 | else if (isa<ShiftInst>(I)) |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 511 | Result = ConstantFoldShiftInstruction(I.getOpcode(), |
Chris Lattner | 4c1061f | 2002-05-06 03:01:37 +0000 | [diff] [blame] | 512 | V1State.getConstant(), |
| 513 | V2State.getConstant()); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 514 | if (Result) |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 515 | markConstant(&I, Result); // This instruction constant folds! |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 516 | else |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 517 | markOverdefined(&I); // Don't know how to fold this instruction. :( |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
Chris Lattner | 2a88bb7 | 2002-08-30 23:39:00 +0000 | [diff] [blame] | 520 | |
| 521 | // Handle getelementptr instructions... if all operands are constants then we |
| 522 | // can turn this into a getelementptr ConstantExpr. |
| 523 | // |
| 524 | void SCCP::visitGetElementPtrInst(GetElementPtrInst &I) { |
| 525 | std::vector<Constant*> Operands; |
| 526 | Operands.reserve(I.getNumOperands()); |
| 527 | |
| 528 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
| 529 | InstVal &State = getValueState(I.getOperand(i)); |
| 530 | if (State.isUndefined()) |
| 531 | return; // Operands are not resolved yet... |
| 532 | else if (State.isOverdefined()) { |
| 533 | markOverdefined(&I); |
| 534 | return; |
| 535 | } |
| 536 | assert(State.isConstant() && "Unknown state!"); |
| 537 | Operands.push_back(State.getConstant()); |
| 538 | } |
| 539 | |
| 540 | Constant *Ptr = Operands[0]; |
| 541 | Operands.erase(Operands.begin()); // Erase the pointer from idx list... |
| 542 | |
| 543 | markConstant(&I, ConstantExpr::getGetElementPtr(Ptr, Operands)); |
| 544 | } |