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