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 | |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 24 | #include "llvm/Optimizations/ConstantProp.h" |
| 25 | #include "llvm/Optimizations/ConstantHandling.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 26 | #include "llvm/Module.h" |
| 27 | #include "llvm/Method.h" |
| 28 | #include "llvm/BasicBlock.h" |
| 29 | #include "llvm/iTerminators.h" |
Chris Lattner | 7061dc5 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 30 | #include "llvm/iPHINode.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 31 | #include "llvm/iOther.h" |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame^] | 32 | #include "llvm/ConstantVals.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 33 | |
| 34 | inline static bool |
Chris Lattner | faffb05 | 2001-11-26 18:57:12 +0000 | [diff] [blame] | 35 | ConstantFoldUnaryInst(BasicBlock *BB, BasicBlock::iterator &II, |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame^] | 36 | UnaryOperator *Op, Constant *D) { |
| 37 | Constant *ReplaceWith = |
Chris Lattner | a41f50d | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 38 | opt::ConstantFoldUnaryInstruction(Op->getOpcode(), D); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 39 | |
| 40 | if (!ReplaceWith) return false; // Nothing new to change... |
| 41 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 42 | // Replaces all of the uses of a variable with uses of the constant. |
| 43 | Op->replaceAllUsesWith(ReplaceWith); |
| 44 | |
| 45 | // Remove the operator from the list of definitions... |
Chris Lattner | faffb05 | 2001-11-26 18:57:12 +0000 | [diff] [blame] | 46 | Op->getParent()->getInstList().remove(II); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 47 | |
| 48 | // The new constant inherits the old name of the operator... |
Chris Lattner | 9b644cc | 2001-09-07 16:41:30 +0000 | [diff] [blame] | 49 | if (Op->hasName()) |
Chris Lattner | faffb05 | 2001-11-26 18:57:12 +0000 | [diff] [blame] | 50 | ReplaceWith->setName(Op->getName(), BB->getParent()->getSymbolTableSure()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 51 | |
| 52 | // Delete the operator now... |
| 53 | delete Op; |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | inline static bool |
Chris Lattner | faffb05 | 2001-11-26 18:57:12 +0000 | [diff] [blame] | 58 | ConstantFoldCast(BasicBlock *BB, BasicBlock::iterator &II, |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame^] | 59 | CastInst *CI, Constant *D) { |
| 60 | Constant *ReplaceWith = |
Chris Lattner | 37aabf2 | 2001-10-31 05:07:57 +0000 | [diff] [blame] | 61 | opt::ConstantFoldCastInstruction(D, CI->getType()); |
| 62 | |
| 63 | if (!ReplaceWith) return false; // Nothing new to change... |
| 64 | |
| 65 | // Replaces all of the uses of a variable with uses of the constant. |
| 66 | CI->replaceAllUsesWith(ReplaceWith); |
| 67 | |
| 68 | // Remove the cast from the list of definitions... |
Chris Lattner | faffb05 | 2001-11-26 18:57:12 +0000 | [diff] [blame] | 69 | CI->getParent()->getInstList().remove(II); |
Chris Lattner | 37aabf2 | 2001-10-31 05:07:57 +0000 | [diff] [blame] | 70 | |
| 71 | // The new constant inherits the old name of the cast... |
| 72 | if (CI->hasName()) |
Chris Lattner | faffb05 | 2001-11-26 18:57:12 +0000 | [diff] [blame] | 73 | ReplaceWith->setName(CI->getName(), BB->getParent()->getSymbolTableSure()); |
Chris Lattner | 37aabf2 | 2001-10-31 05:07:57 +0000 | [diff] [blame] | 74 | |
| 75 | // Delete the cast now... |
| 76 | delete CI; |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | inline static bool |
Chris Lattner | faffb05 | 2001-11-26 18:57:12 +0000 | [diff] [blame] | 81 | ConstantFoldBinaryInst(BasicBlock *BB, BasicBlock::iterator &II, |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 82 | BinaryOperator *Op, |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame^] | 83 | Constant *D1, Constant *D2) { |
| 84 | Constant *ReplaceWith = |
Chris Lattner | a41f50d | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 85 | opt::ConstantFoldBinaryInstruction(Op->getOpcode(), D1, D2); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 86 | if (!ReplaceWith) return false; // Nothing new to change... |
| 87 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 88 | // Replaces all of the uses of a variable with uses of the constant. |
| 89 | Op->replaceAllUsesWith(ReplaceWith); |
| 90 | |
| 91 | // Remove the operator from the list of definitions... |
Chris Lattner | faffb05 | 2001-11-26 18:57:12 +0000 | [diff] [blame] | 92 | Op->getParent()->getInstList().remove(II); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 93 | |
| 94 | // The new constant inherits the old name of the operator... |
Chris Lattner | 9b644cc | 2001-09-07 16:41:30 +0000 | [diff] [blame] | 95 | if (Op->hasName()) |
Chris Lattner | faffb05 | 2001-11-26 18:57:12 +0000 | [diff] [blame] | 96 | ReplaceWith->setName(Op->getName(), BB->getParent()->getSymbolTableSure()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 97 | |
| 98 | // Delete the operator now... |
| 99 | delete Op; |
| 100 | return true; |
| 101 | } |
| 102 | |
Chris Lattner | 2b05880 | 2001-06-29 23:56:58 +0000 | [diff] [blame] | 103 | // ConstantFoldTerminator - If a terminator instruction is predicated on a |
| 104 | // constant value, convert it into an unconditional branch to the constant |
| 105 | // destination. |
| 106 | // |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 107 | bool opt::ConstantFoldTerminator(TerminatorInst *T) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 108 | // Branch - See if we are conditional jumping on constant |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 109 | if (BranchInst *BI = dyn_cast<BranchInst>(T)) { |
Chris Lattner | 2b05880 | 2001-06-29 23:56:58 +0000 | [diff] [blame] | 110 | if (BI->isUnconditional()) return false; // Can't optimize uncond branch |
Chris Lattner | 9636a91 | 2001-10-01 16:18:37 +0000 | [diff] [blame] | 111 | BasicBlock *Dest1 = cast<BasicBlock>(BI->getOperand(0)); |
| 112 | BasicBlock *Dest2 = cast<BasicBlock>(BI->getOperand(1)); |
Chris Lattner | 2b05880 | 2001-06-29 23:56:58 +0000 | [diff] [blame] | 113 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame^] | 114 | if (ConstantBool *Cond = dyn_cast<ConstantBool>(BI->getCondition())) { |
Chris Lattner | 1d87bcf | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 115 | // Are we branching on constant? |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 116 | // YES. Change to unconditional branch... |
Chris Lattner | 2b05880 | 2001-06-29 23:56:58 +0000 | [diff] [blame] | 117 | BasicBlock *Destination = Cond->getValue() ? Dest1 : Dest2; |
| 118 | BasicBlock *OldDest = Cond->getValue() ? Dest2 : Dest1; |
Chris Lattner | bca26a4 | 2001-06-29 05:23:10 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 2b05880 | 2001-06-29 23:56:58 +0000 | [diff] [blame] | 120 | //cerr << "Method: " << T->getParent()->getParent() |
| 121 | // << "\nRemoving branch from " << T->getParent() |
| 122 | // << "\n\nTo: " << OldDest << endl; |
Chris Lattner | bca26a4 | 2001-06-29 05:23:10 +0000 | [diff] [blame] | 123 | |
| 124 | // Let the basic block know that we are letting go of it. Based on this, |
| 125 | // it will adjust it's PHI nodes. |
Chris Lattner | 2b05880 | 2001-06-29 23:56:58 +0000 | [diff] [blame] | 126 | assert(BI->getParent() && "Terminator not inserted in block!"); |
| 127 | OldDest->removePredecessor(BI->getParent()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 128 | |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 129 | // Set the unconditional destination, and change the insn to be an |
| 130 | // unconditional branch. |
| 131 | BI->setUnconditionalDest(Destination); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 132 | return true; |
Chris Lattner | 9b644cc | 2001-09-07 16:41:30 +0000 | [diff] [blame] | 133 | } |
| 134 | #if 0 |
| 135 | // FIXME: TODO: This doesn't work if the destination has PHI nodes with |
| 136 | // different incoming values on each branch! |
| 137 | // |
| 138 | else if (Dest2 == Dest1) { // Conditional branch to same location? |
Chris Lattner | 2b05880 | 2001-06-29 23:56:58 +0000 | [diff] [blame] | 139 | // This branch matches something like this: |
| 140 | // br bool %cond, label %Dest, label %Dest |
| 141 | // and changes it into: br label %Dest |
| 142 | |
| 143 | // Let the basic block know that we are letting go of one copy of it. |
| 144 | assert(BI->getParent() && "Terminator not inserted in block!"); |
| 145 | Dest1->removePredecessor(BI->getParent()); |
| 146 | |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 147 | // Change a conditional branch to unconditional. |
| 148 | BI->setUnconditionalDest(Dest1); |
Chris Lattner | 2b05880 | 2001-06-29 23:56:58 +0000 | [diff] [blame] | 149 | return true; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 150 | } |
Chris Lattner | 9b644cc | 2001-09-07 16:41:30 +0000 | [diff] [blame] | 151 | #endif |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 152 | } |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | // ConstantFoldInstruction - If an instruction references constants, try to fold |
| 157 | // them together... |
| 158 | // |
Chris Lattner | faffb05 | 2001-11-26 18:57:12 +0000 | [diff] [blame] | 159 | bool opt::ConstantPropogation::doConstantPropogation(BasicBlock *BB, |
| 160 | BasicBlock::iterator &II) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 161 | Instruction *Inst = *II; |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 162 | if (BinaryOperator *BInst = dyn_cast<BinaryOperator>(Inst)) { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame^] | 163 | Constant *D1 = dyn_cast<Constant>(Inst->getOperand(0)); |
| 164 | Constant *D2 = dyn_cast<Constant>(Inst->getOperand(1)); |
Chris Lattner | 531450d | 2001-06-27 23:35:26 +0000 | [diff] [blame] | 165 | |
| 166 | if (D1 && D2) |
Chris Lattner | faffb05 | 2001-11-26 18:57:12 +0000 | [diff] [blame] | 167 | return ConstantFoldBinaryInst(BB, II, cast<BinaryOperator>(Inst), D1, D2); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 168 | |
Chris Lattner | 37aabf2 | 2001-10-31 05:07:57 +0000 | [diff] [blame] | 169 | } else if (CastInst *CI = dyn_cast<CastInst>(Inst)) { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame^] | 170 | Constant *D = dyn_cast<Constant>(CI->getOperand(0)); |
Chris Lattner | faffb05 | 2001-11-26 18:57:12 +0000 | [diff] [blame] | 171 | if (D) return ConstantFoldCast(BB, II, CI, D); |
Chris Lattner | 37aabf2 | 2001-10-31 05:07:57 +0000 | [diff] [blame] | 172 | |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 173 | } else if (UnaryOperator *UInst = dyn_cast<UnaryOperator>(Inst)) { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame^] | 174 | Constant *D = dyn_cast<Constant>(UInst->getOperand(0)); |
Chris Lattner | faffb05 | 2001-11-26 18:57:12 +0000 | [diff] [blame] | 175 | if (D) return ConstantFoldUnaryInst(BB, II, UInst, D); |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 176 | } else if (TerminatorInst *TInst = dyn_cast<TerminatorInst>(Inst)) { |
| 177 | return opt::ConstantFoldTerminator(TInst); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 178 | |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 179 | } else if (PHINode *PN = dyn_cast<PHINode>(Inst)) { |
| 180 | // If it's a PHI node and only has one operand |
| 181 | // Then replace it directly with that operand. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 182 | assert(PN->getOperand(0) && "PHI Node must have at least one operand!"); |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 183 | if (PN->getNumOperands() == 1) { // If the PHI Node has exactly 1 operand |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 184 | Value *V = PN->getOperand(0); |
| 185 | PN->replaceAllUsesWith(V); // Replace all uses of this PHI |
| 186 | // Unlink from basic block |
Chris Lattner | faffb05 | 2001-11-26 18:57:12 +0000 | [diff] [blame] | 187 | PN->getParent()->getInstList().remove(II); |
Chris Lattner | 9b644cc | 2001-09-07 16:41:30 +0000 | [diff] [blame] | 188 | if (PN->hasName()) // Inherit PHINode name |
Chris Lattner | faffb05 | 2001-11-26 18:57:12 +0000 | [diff] [blame] | 189 | V->setName(PN->getName(), BB->getParent()->getSymbolTableSure()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 190 | delete PN; // Finally, delete the node... |
| 191 | return true; |
| 192 | } |
| 193 | } |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | // DoConstPropPass - Propogate constants and do constant folding on instructions |
| 198 | // this returns true if something was changed, false if nothing was changed. |
| 199 | // |
| 200 | static bool DoConstPropPass(Method *M) { |
| 201 | bool SomethingChanged = false; |
| 202 | |
Chris Lattner | faffb05 | 2001-11-26 18:57:12 +0000 | [diff] [blame] | 203 | for (Method::iterator BBI = M->begin(); BBI != M->end(); ++BBI) { |
| 204 | BasicBlock *BB = *BBI; |
| 205 | for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ) |
| 206 | if (opt::ConstantPropogation::doConstantPropogation(BB, I)) |
| 207 | SomethingChanged = true; |
| 208 | else |
| 209 | ++I; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 210 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 211 | return SomethingChanged; |
| 212 | } |
| 213 | |
| 214 | |
Chris Lattner | faffb05 | 2001-11-26 18:57:12 +0000 | [diff] [blame] | 215 | // returns whether or not the underlying method was modified |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 216 | // |
Chris Lattner | 5680ee6 | 2001-10-18 01:32:34 +0000 | [diff] [blame] | 217 | bool opt::ConstantPropogation::doConstantPropogation(Method *M) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 218 | bool Modified = false; |
| 219 | |
| 220 | // Fold constants until we make no progress... |
| 221 | while (DoConstPropPass(M)) Modified = true; |
| 222 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 223 | return Modified; |
| 224 | } |