Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===- ConstantProp.cpp - Code to perform Constant Propogation ------------===// |
| 2 | // |
| 3 | // This file implements constant propogation and merging: |
| 4 | // |
| 5 | // Specifically, this: |
| 6 | // * Folds multiple identical constants in the constant pool together |
| 7 | // Note that if one is named and the other is not, that the result gets the |
| 8 | // original name. |
| 9 | // * Converts instructions like "add int %1, %2" into a direct def of %3 in |
| 10 | // the constant pool |
| 11 | // * Converts conditional branches on a constant boolean value into direct |
| 12 | // branches. |
| 13 | // * Converts phi nodes with one incoming def to the incoming def directly |
| 14 | // . Converts switch statements with one entry into a test & conditional |
| 15 | // branch |
| 16 | // . Converts switches on constant values into an unconditional branch. |
| 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 | |
| 24 | #include "llvm/Module.h" |
| 25 | #include "llvm/Method.h" |
| 26 | #include "llvm/BasicBlock.h" |
| 27 | #include "llvm/iTerminators.h" |
| 28 | #include "llvm/iOther.h" |
| 29 | #include "llvm/ConstPoolVals.h" |
| 30 | #include "llvm/ConstantPool.h" |
| 31 | #include "llvm/Opt/AllOpts.h" |
| 32 | #include "llvm/Opt/ConstantHandling.h" |
| 33 | |
| 34 | // Merge identical constant values in the constant pool. |
| 35 | // |
| 36 | // TODO: We can do better than this simplistic N^2 algorithm... |
| 37 | // |
Chris Lattner | 531450d | 2001-06-27 23:35:26 +0000 | [diff] [blame] | 38 | bool DoConstantPoolMerging(Method *M) { |
| 39 | return DoConstantPoolMerging(M->getConstantPool()); |
| 40 | } |
| 41 | |
| 42 | bool DoConstantPoolMerging(ConstantPool &CP) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 43 | bool Modified = false; |
| 44 | for (ConstantPool::plane_iterator PI = CP.begin(); PI != CP.end(); ++PI) { |
| 45 | for (ConstantPool::PlaneType::iterator I = (*PI)->begin(); |
Chris Lattner | 531450d | 2001-06-27 23:35:26 +0000 | [diff] [blame] | 46 | I != (*PI)->end(); ++I) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 47 | ConstPoolVal *C = *I; |
| 48 | |
| 49 | ConstantPool::PlaneType::iterator J = I; |
Chris Lattner | 531450d | 2001-06-27 23:35:26 +0000 | [diff] [blame] | 50 | for (++J; J != (*PI)->end(); ++J) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 51 | if (C->equals(*J)) { |
| 52 | Modified = true; |
| 53 | // Okay we know that *I == *J. So now we need to make all uses of *I |
| 54 | // point to *J. |
| 55 | // |
| 56 | C->replaceAllUsesWith(*J); |
| 57 | |
| 58 | (*PI)->remove(I); // Remove C from constant pool... |
| 59 | |
| 60 | if (C->hasName() && !(*J)->hasName()) // The merged constant inherits |
| 61 | (*J)->setName(C->getName()); // the old name... |
| 62 | |
| 63 | delete C; // Delete the constant itself. |
| 64 | break; // Break out of inner for loop |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | return Modified; |
| 70 | } |
| 71 | |
| 72 | inline static bool |
| 73 | ConstantFoldUnaryInst(Method *M, Method::inst_iterator &DI, |
| 74 | UnaryOperator *Op, ConstPoolVal *D) { |
Chris Lattner | 531450d | 2001-06-27 23:35:26 +0000 | [diff] [blame] | 75 | ConstPoolVal *ReplaceWith = |
| 76 | ConstantFoldUnaryInstruction(Op->getInstType(), D); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 77 | |
| 78 | if (!ReplaceWith) return false; // Nothing new to change... |
| 79 | |
| 80 | |
| 81 | // Add the new value to the constant pool... |
| 82 | M->getConstantPool().insert(ReplaceWith); |
| 83 | |
| 84 | // Replaces all of the uses of a variable with uses of the constant. |
| 85 | Op->replaceAllUsesWith(ReplaceWith); |
| 86 | |
| 87 | // Remove the operator from the list of definitions... |
| 88 | Op->getParent()->getInstList().remove(DI.getInstructionIterator()); |
| 89 | |
| 90 | // The new constant inherits the old name of the operator... |
| 91 | if (Op->hasName()) ReplaceWith->setName(Op->getName()); |
| 92 | |
| 93 | // Delete the operator now... |
| 94 | delete Op; |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | inline static bool |
| 99 | ConstantFoldBinaryInst(Method *M, Method::inst_iterator &DI, |
| 100 | BinaryOperator *Op, |
| 101 | ConstPoolVal *D1, ConstPoolVal *D2) { |
Chris Lattner | 531450d | 2001-06-27 23:35:26 +0000 | [diff] [blame] | 102 | ConstPoolVal *ReplaceWith = |
| 103 | ConstantFoldBinaryInstruction(Op->getInstType(), D1, D2); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 104 | if (!ReplaceWith) return false; // Nothing new to change... |
| 105 | |
| 106 | // Add the new value to the constant pool... |
| 107 | M->getConstantPool().insert(ReplaceWith); |
| 108 | |
| 109 | // Replaces all of the uses of a variable with uses of the constant. |
| 110 | Op->replaceAllUsesWith(ReplaceWith); |
| 111 | |
| 112 | // Remove the operator from the list of definitions... |
| 113 | Op->getParent()->getInstList().remove(DI.getInstructionIterator()); |
| 114 | |
| 115 | // The new constant inherits the old name of the operator... |
| 116 | if (Op->hasName()) ReplaceWith->setName(Op->getName()); |
| 117 | |
| 118 | // Delete the operator now... |
| 119 | delete Op; |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | inline static bool ConstantFoldTerminator(TerminatorInst *T) { |
| 124 | // Branch - See if we are conditional jumping on constant |
| 125 | if (T->getInstType() == Instruction::Br) { |
| 126 | BranchInst *BI = (BranchInst*)T; |
| 127 | if (!BI->isUnconditional() && // Are we branching on constant? |
Chris Lattner | 531450d | 2001-06-27 23:35:26 +0000 | [diff] [blame] | 128 | BI->getOperand(2)->isConstant()) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 129 | // YES. Change to unconditional branch... |
| 130 | ConstPoolBool *Cond = (ConstPoolBool*)BI->getOperand(2); |
| 131 | Value *Destination = BI->getOperand(Cond->getValue() ? 0 : 1); |
Chris Lattner | bca26a4 | 2001-06-29 05:23:10 +0000 | [diff] [blame^] | 132 | Value *OldDest = BI->getOperand(Cond->getValue() ? 1 : 0); |
| 133 | |
| 134 | //cerr << "Method: " << T->getParent()->getParent() << "\nRemoving branch from " << T->getParent() << "\n\nTo: " << OldDest << endl; |
| 135 | |
| 136 | // Let the basic block know that we are letting go of it. Based on this, |
| 137 | // it will adjust it's PHI nodes. |
| 138 | assert(T->getParent() && "Terminator not inserted in block!"); |
| 139 | OldDest->castBasicBlockAsserting()->removePredecessor(T->getParent()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 140 | |
| 141 | BI->setOperand(0, Destination); // Set the unconditional destination |
| 142 | BI->setOperand(1, 0); // Clear the conditional destination |
| 143 | BI->setOperand(2, 0); // Clear the condition... |
| 144 | return true; |
| 145 | } |
| 146 | } |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | // ConstantFoldInstruction - If an instruction references constants, try to fold |
| 151 | // them together... |
| 152 | // |
| 153 | inline static bool |
| 154 | ConstantFoldInstruction(Method *M, Method::inst_iterator &II) { |
| 155 | Instruction *Inst = *II; |
| 156 | if (Inst->isBinaryOp()) { |
Chris Lattner | 531450d | 2001-06-27 23:35:26 +0000 | [diff] [blame] | 157 | ConstPoolVal *D1 = Inst->getOperand(0)->castConstant(); |
| 158 | ConstPoolVal *D2 = Inst->getOperand(1)->castConstant(); |
| 159 | |
| 160 | if (D1 && D2) |
| 161 | return ConstantFoldBinaryInst(M, II, (BinaryOperator*)Inst, D1, D2); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 162 | |
| 163 | } else if (Inst->isUnaryOp()) { |
Chris Lattner | 531450d | 2001-06-27 23:35:26 +0000 | [diff] [blame] | 164 | ConstPoolVal *D = Inst->getOperand(0)->castConstant(); |
| 165 | if (D) return ConstantFoldUnaryInst(M, II, (UnaryOperator*)Inst, D); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 166 | } else if (Inst->isTerminator()) { |
| 167 | return ConstantFoldTerminator((TerminatorInst*)Inst); |
| 168 | |
Chris Lattner | 531450d | 2001-06-27 23:35:26 +0000 | [diff] [blame] | 169 | } else if (Inst->isPHINode()) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 170 | PHINode *PN = (PHINode*)Inst; // If it's a PHI node and only has one operand |
| 171 | // Then replace it directly with that operand. |
| 172 | assert(PN->getOperand(0) && "PHI Node must have at least one operand!"); |
| 173 | if (PN->getOperand(1) == 0) { // If the PHI Node has exactly 1 operand |
| 174 | Value *V = PN->getOperand(0); |
| 175 | PN->replaceAllUsesWith(V); // Replace all uses of this PHI |
| 176 | // Unlink from basic block |
| 177 | PN->getParent()->getInstList().remove(II.getInstructionIterator()); |
| 178 | if (PN->hasName()) V->setName(PN->getName()); // Inherit PHINode name |
| 179 | delete PN; // Finally, delete the node... |
| 180 | return true; |
| 181 | } |
| 182 | } |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | // DoConstPropPass - Propogate constants and do constant folding on instructions |
| 187 | // this returns true if something was changed, false if nothing was changed. |
| 188 | // |
| 189 | static bool DoConstPropPass(Method *M) { |
| 190 | bool SomethingChanged = false; |
| 191 | |
| 192 | #if 1 |
| 193 | Method::inst_iterator It = M->inst_begin(); |
| 194 | while (It != M->inst_end()) |
| 195 | if (ConstantFoldInstruction(M, It)) { |
| 196 | SomethingChanged = true; // If returned true, iter is already incremented |
| 197 | |
| 198 | // Incrementing the iterator in an unchecked manner could mess up the |
| 199 | // internals of 'It'. To make sure everything is happy, tell it we might |
| 200 | // have broken it. |
| 201 | It.resyncInstructionIterator(); |
| 202 | } else { |
| 203 | ++It; |
| 204 | } |
| 205 | #else |
Chris Lattner | 531450d | 2001-06-27 23:35:26 +0000 | [diff] [blame] | 206 | for (Method::iterator BBIt = M->begin(); BBIt != M->end(); ++BBIt) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 207 | BasicBlock *BB = *BBIt; |
| 208 | |
Chris Lattner | 531450d | 2001-06-27 23:35:26 +0000 | [diff] [blame] | 209 | reduce_apply_bool(BB->begin(), BB->end(), |
| 210 | bind1st(ConstantFoldInstruction, M)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 211 | } |
| 212 | #endif |
| 213 | return SomethingChanged; |
| 214 | } |
| 215 | |
| 216 | |
| 217 | // returns true on failure, false on success... |
| 218 | // |
| 219 | bool DoConstantPropogation(Method *M) { |
| 220 | bool Modified = false; |
| 221 | |
| 222 | // Fold constants until we make no progress... |
| 223 | while (DoConstPropPass(M)) Modified = true; |
| 224 | |
| 225 | // Merge identical constants last: this is important because we may have just |
| 226 | // introduced constants that already exist! |
| 227 | // |
Chris Lattner | 531450d | 2001-06-27 23:35:26 +0000 | [diff] [blame] | 228 | Modified |= DoConstantPoolMerging(M->getConstantPool()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 229 | |
| 230 | return Modified; |
| 231 | } |