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