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