Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 1 | //===- InstructionCombining.cpp - Combine multiple instructions -------------=// |
| 2 | // |
| 3 | // InstructionCombining - Combine instructions to form fewer, simple |
| 4 | // instructions. This pass does not modify the CFG, and has a tendancy to |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5 | // make instructions dead, so a subsequent DIE pass is useful. This pass is |
| 6 | // where algebraic simplification happens. |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 7 | // |
| 8 | // This pass combines things like: |
| 9 | // %Y = add int 1, %X |
| 10 | // %Z = add int 1, %Y |
| 11 | // into: |
| 12 | // %Z = add int 2, %X |
| 13 | // |
| 14 | // This is a simple worklist driven algorithm. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Chris Lattner | 022103b | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 18 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 497c60c | 2002-05-07 18:12:18 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Chris Lattner | 968ddc9 | 2002-04-08 20:18:09 +0000 | [diff] [blame] | 20 | #include "llvm/ConstantHandling.h" |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 21 | #include "llvm/iMemory.h" |
Chris Lattner | 8d70cd9 | 2002-04-15 19:45:29 +0000 | [diff] [blame] | 22 | #include "llvm/iOther.h" |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 23 | #include "llvm/iPHINode.h" |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 24 | #include "llvm/iOperators.h" |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 25 | #include "llvm/Pass.h" |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 26 | #include "llvm/Support/InstIterator.h" |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 27 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 28 | #include "Support/StatisticReporter.h" |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 29 | #include <algorithm> |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 31 | static Statistic<> NumCombined("instcombine\t- Number of insts combined"); |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 32 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 33 | namespace { |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 34 | class InstCombiner : public FunctionPass, |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 35 | public InstVisitor<InstCombiner, Instruction*> { |
| 36 | // Worklist of all of the instructions that need to be simplified. |
| 37 | std::vector<Instruction*> WorkList; |
| 38 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 39 | void AddUsesToWorkList(Instruction &I) { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 40 | // The instruction was simplified, add all users of the instruction to |
| 41 | // the work lists because they might get more simplified now... |
| 42 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 43 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 44 | UI != UE; ++UI) |
| 45 | WorkList.push_back(cast<Instruction>(*UI)); |
| 46 | } |
| 47 | |
| 48 | public: |
Chris Lattner | 96c466b | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 49 | const char *getPassName() const { return "Instruction Combining"; } |
| 50 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 51 | virtual bool runOnFunction(Function &F); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 53 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 54 | AU.preservesCFG(); |
| 55 | } |
| 56 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 57 | // Visitation implementation - Implement instruction combining for different |
| 58 | // instruction types. The semantics are as follows: |
| 59 | // Return Value: |
| 60 | // null - No change was made |
| 61 | // I - Change was made, I is still valid |
| 62 | // otherwise - Change was made, replace I with returned instruction |
| 63 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 64 | Instruction *visitNot(UnaryOperator &I); |
| 65 | Instruction *visitAdd(BinaryOperator &I); |
| 66 | Instruction *visitSub(BinaryOperator &I); |
| 67 | Instruction *visitMul(BinaryOperator &I); |
| 68 | Instruction *visitDiv(BinaryOperator &I); |
| 69 | Instruction *visitRem(BinaryOperator &I); |
| 70 | Instruction *visitAnd(BinaryOperator &I); |
| 71 | Instruction *visitOr (BinaryOperator &I); |
| 72 | Instruction *visitXor(BinaryOperator &I); |
| 73 | Instruction *visitSetCondInst(BinaryOperator &I); |
| 74 | Instruction *visitShiftInst(Instruction &I); |
| 75 | Instruction *visitCastInst(CastInst &CI); |
| 76 | Instruction *visitPHINode(PHINode &PN); |
| 77 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
| 78 | Instruction *visitMemAccessInst(MemAccessInst &MAI); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 79 | |
| 80 | // visitInstruction - Specify what to return for unhandled instructions... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 81 | Instruction *visitInstruction(Instruction &I) { return 0; } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 82 | }; |
| 83 | } |
| 84 | |
| 85 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 86 | Instruction *InstCombiner::visitNot(UnaryOperator &I) { |
| 87 | if (I.use_empty()) return 0; // Don't fix dead instructions... |
Chris Lattner | ed3a550 | 2002-05-06 17:03:21 +0000 | [diff] [blame] | 88 | |
| 89 | // not (not X) = X |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 90 | if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(0))) |
Chris Lattner | ed3a550 | 2002-05-06 17:03:21 +0000 | [diff] [blame] | 91 | if (Op->getOpcode() == Instruction::Not) { |
| 92 | AddUsesToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 93 | I.replaceAllUsesWith(Op->getOperand(0)); |
| 94 | return &I; |
Chris Lattner | ed3a550 | 2002-05-06 17:03:21 +0000 | [diff] [blame] | 95 | } |
| 96 | return 0; |
| 97 | } |
| 98 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 99 | |
| 100 | // Make sure that this instruction has a constant on the right hand side if it |
| 101 | // has any constant arguments. If not, fix it an return true. |
| 102 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 103 | static bool SimplifyBinOp(BinaryOperator &I) { |
| 104 | if (isa<Constant>(I.getOperand(0)) && !isa<Constant>(I.getOperand(1))) |
| 105 | return !I.swapOperands(); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 106 | return false; |
| 107 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 108 | |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 109 | // dyn_castNegInst - Given a 'sub' instruction, return the RHS of the |
| 110 | // instruction if the LHS is a constant zero (which is the 'negate' form). |
| 111 | // |
| 112 | static inline Value *dyn_castNegInst(Value *V) { |
| 113 | Instruction *I = dyn_cast<Instruction>(V); |
| 114 | if (!I || I->getOpcode() != Instruction::Sub) return 0; |
| 115 | |
| 116 | if (I->getOperand(0) == Constant::getNullValue(I->getType())) |
| 117 | return I->getOperand(1); |
| 118 | return 0; |
| 119 | } |
| 120 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 121 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
| 122 | if (I.use_empty()) return 0; // Don't fix dead add instructions... |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 123 | bool Changed = SimplifyBinOp(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 124 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 125 | |
| 126 | // Eliminate 'add int %X, 0' |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 127 | if (RHS == Constant::getNullValue(I.getType())) { |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 128 | AddUsesToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 129 | I.replaceAllUsesWith(LHS); |
| 130 | return &I; |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Chris Lattner | 5c4afb9 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 133 | // -A + B --> B - A |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 134 | if (Value *V = dyn_castNegInst(LHS)) |
Chris Lattner | 5c4afb9 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 135 | return BinaryOperator::create(Instruction::Sub, RHS, V); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 136 | |
| 137 | // A + -B --> A - B |
| 138 | if (Value *V = dyn_castNegInst(RHS)) |
Chris Lattner | 5c4afb9 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 139 | return BinaryOperator::create(Instruction::Sub, LHS, V); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 140 | |
| 141 | // Simplify add instructions with a constant RHS... |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 142 | if (Constant *Op2 = dyn_cast<Constant>(RHS)) { |
| 143 | if (BinaryOperator *ILHS = dyn_cast<BinaryOperator>(LHS)) { |
| 144 | if (ILHS->getOpcode() == Instruction::Add && |
| 145 | isa<Constant>(ILHS->getOperand(1))) { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 146 | // Fold: |
| 147 | // %Y = add int %X, 1 |
| 148 | // %Z = add int %Y, 1 |
| 149 | // into: |
| 150 | // %Z = add int %X, 2 |
| 151 | // |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 152 | if (Constant *Val = *Op2 + *cast<Constant>(ILHS->getOperand(1))) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 153 | I.setOperand(0, ILHS->getOperand(0)); |
| 154 | I.setOperand(1, Val); |
| 155 | return &I; |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 156 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 161 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 164 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
| 165 | if (I.use_empty()) return 0; // Don't fix dead add instructions... |
| 166 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 167 | |
| 168 | if (Op0 == Op1) { // sub X, X -> 0 |
| 169 | AddUsesToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 170 | I.replaceAllUsesWith(Constant::getNullValue(I.getType())); |
| 171 | return &I; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 172 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 173 | |
| 174 | // If this is a subtract instruction with a constant RHS, convert it to an add |
| 175 | // instruction of a negative constant |
| 176 | // |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 177 | if (Constant *Op2 = dyn_cast<Constant>(Op1)) |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 178 | if (Constant *RHS = *Constant::getNullValue(I.getType()) - *Op2) // 0 - RHS |
| 179 | return BinaryOperator::create(Instruction::Add, Op0, RHS, I.getName()); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 180 | |
Chris Lattner | 5c4afb9 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 181 | // If this is a 'C = x-B', check to see if 'B = -A', so that C = x+A... |
| 182 | if (Value *V = dyn_castNegInst(Op1)) |
| 183 | return BinaryOperator::create(Instruction::Add, Op0, V); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 184 | |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 185 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression is |
| 186 | // not used by anyone else... |
| 187 | // |
| 188 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) |
Chris Lattner | 86c25fd | 2002-05-14 16:44:07 +0000 | [diff] [blame] | 189 | if (Op1I->use_size() == 1 && Op1I->getOpcode() == Instruction::Sub) { |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 190 | // Swap the two operands of the subexpr... |
| 191 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 192 | Op1I->setOperand(0, IIOp1); |
| 193 | Op1I->setOperand(1, IIOp0); |
| 194 | |
| 195 | // Create the new top level add instruction... |
| 196 | return BinaryOperator::create(Instruction::Add, Op0, Op1); |
| 197 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 198 | return 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 201 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
| 202 | if (I.use_empty()) return 0; // Don't fix dead instructions... |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 203 | bool Changed = SimplifyBinOp(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 204 | Value *Op1 = I.getOperand(0); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 205 | |
| 206 | // Simplify add instructions with a constant RHS... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 207 | if (Constant *Op2 = dyn_cast<Constant>(I.getOperand(1))) { |
| 208 | if (I.getType()->isIntegral() && cast<ConstantInt>(Op2)->equalsInt(1)){ |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 209 | // Eliminate 'mul int %X, 1' |
| 210 | AddUsesToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 211 | I.replaceAllUsesWith(Op1); |
| 212 | return &I; |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 213 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 214 | } else if (I.getType()->isIntegral() && |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 215 | cast<ConstantInt>(Op2)->equalsInt(2)) { |
| 216 | // Convert 'mul int %X, 2' to 'add int %X, %X' |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 217 | return BinaryOperator::create(Instruction::Add, Op1, Op1, I.getName()); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 218 | |
| 219 | } else if (Op2->isNullValue()) { |
| 220 | // Eliminate 'mul int %X, 0' |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 221 | AddUsesToWorkList(I); // Add all modified instrs to worklist |
| 222 | I.replaceAllUsesWith(Op2); // Set this value to zero directly |
| 223 | return &I; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 227 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 231 | Instruction *InstCombiner::visitDiv(BinaryOperator &I) { |
| 232 | if (I.use_empty()) return 0; // Don't fix dead instructions... |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 233 | |
| 234 | // div X, 1 == X |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 235 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 236 | if (RHS->equalsInt(1)) { |
| 237 | AddUsesToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 238 | I.replaceAllUsesWith(I.getOperand(0)); |
| 239 | return &I; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 240 | } |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 245 | Instruction *InstCombiner::visitRem(BinaryOperator &I) { |
| 246 | if (I.use_empty()) return 0; // Don't fix dead instructions... |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 247 | |
| 248 | // rem X, 1 == 0 |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 249 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 250 | if (RHS->equalsInt(1)) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 251 | AddUsesToWorkList(I); // Add all modified instrs to worklist |
| 252 | I.replaceAllUsesWith(Constant::getNullValue(I.getType())); |
| 253 | return &I; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 254 | } |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static Constant *getMaxValue(const Type *Ty) { |
| 259 | assert(Ty == Type::BoolTy || Ty->isIntegral()); |
| 260 | if (Ty == Type::BoolTy) |
| 261 | return ConstantBool::True; |
| 262 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 263 | if (Ty->isSigned()) |
Chris Lattner | d661937 | 2002-05-06 18:54:59 +0000 | [diff] [blame] | 264 | return ConstantSInt::get(Ty, -1); |
| 265 | else if (Ty->isUnsigned()) { |
| 266 | // Calculate -1 casted to the right type... |
| 267 | unsigned TypeBits = Ty->getPrimitiveSize()*8; |
| 268 | uint64_t Val = (uint64_t)-1LL; // All ones |
| 269 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 270 | return ConstantUInt::get(Ty, Val); |
Chris Lattner | d661937 | 2002-05-06 18:54:59 +0000 | [diff] [blame] | 271 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 276 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
| 277 | if (I.use_empty()) return 0; // Don't fix dead instructions... |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 278 | bool Changed = SimplifyBinOp(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 279 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 280 | |
| 281 | // and X, X = X and X, 0 == 0 |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 282 | if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) { |
| 283 | AddUsesToWorkList(I); // Add all modified instrs to worklist |
| 284 | I.replaceAllUsesWith(Op1); |
| 285 | return &I; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | // and X, -1 == X |
| 289 | if (Constant *RHS = dyn_cast<Constant>(Op1)) |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 290 | if (RHS == getMaxValue(I.getType())) { |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 291 | AddUsesToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 292 | I.replaceAllUsesWith(Op0); |
| 293 | return &I; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 296 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | |
| 300 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 301 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
| 302 | if (I.use_empty()) return 0; // Don't fix dead instructions... |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 303 | bool Changed = SimplifyBinOp(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 304 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 305 | |
| 306 | // or X, X = X or X, 0 == X |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 307 | if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) { |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 308 | AddUsesToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 309 | I.replaceAllUsesWith(Op0); |
| 310 | return &I; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | // or X, -1 == -1 |
| 314 | if (Constant *RHS = dyn_cast<Constant>(Op1)) |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 315 | if (RHS == getMaxValue(I.getType())) { |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 316 | AddUsesToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 317 | I.replaceAllUsesWith(Op1); |
| 318 | return &I; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 321 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | |
| 325 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 326 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
| 327 | if (I.use_empty()) return 0; // Don't fix dead instructions... |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 328 | bool Changed = SimplifyBinOp(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 329 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 330 | |
| 331 | // xor X, X = 0 |
| 332 | if (Op0 == Op1) { |
| 333 | AddUsesToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 334 | I.replaceAllUsesWith(Constant::getNullValue(I.getType())); |
| 335 | return &I; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | // xor X, 0 == X |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 339 | if (Op1 == Constant::getNullValue(I.getType())) { |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 340 | AddUsesToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 341 | I.replaceAllUsesWith(Op0); |
| 342 | return &I; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 345 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Chris Lattner | 53a5b57 | 2002-05-09 20:11:54 +0000 | [diff] [blame] | 348 | // isTrueWhenEqual - Return true if the specified setcondinst instruction is |
| 349 | // true when both operands are equal... |
| 350 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 351 | static bool isTrueWhenEqual(Instruction &I) { |
| 352 | return I.getOpcode() == Instruction::SetEQ || |
| 353 | I.getOpcode() == Instruction::SetGE || |
| 354 | I.getOpcode() == Instruction::SetLE; |
Chris Lattner | 53a5b57 | 2002-05-09 20:11:54 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 357 | Instruction *InstCombiner::visitSetCondInst(BinaryOperator &I) { |
| 358 | if (I.use_empty()) return 0; // Don't fix dead instructions... |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 359 | bool Changed = SimplifyBinOp(I); |
| 360 | |
| 361 | // setcc X, X |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 362 | if (I.getOperand(0) == I.getOperand(1)) { |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 363 | AddUsesToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 364 | I.replaceAllUsesWith(ConstantBool::get(isTrueWhenEqual(I))); |
| 365 | return &I; |
Chris Lattner | 53a5b57 | 2002-05-09 20:11:54 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | // setcc <global*>, 0 - Global value addresses are never null! |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 369 | if (isa<GlobalValue>(I.getOperand(0)) && |
| 370 | isa<ConstantPointerNull>(I.getOperand(1))) { |
Chris Lattner | 53a5b57 | 2002-05-09 20:11:54 +0000 | [diff] [blame] | 371 | AddUsesToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 372 | I.replaceAllUsesWith(ConstantBool::get(!isTrueWhenEqual(I))); |
| 373 | return &I; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 376 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | |
| 380 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 381 | Instruction *InstCombiner::visitShiftInst(Instruction &I) { |
| 382 | if (I.use_empty()) return 0; // Don't fix dead instructions... |
| 383 | assert(I.getOperand(1)->getType() == Type::UByteTy); |
| 384 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 385 | |
| 386 | // shl X, 0 == X and shr X, 0 == X |
| 387 | // shl 0, X == 0 and shr 0, X == 0 |
| 388 | if (Op1 == Constant::getNullValue(Type::UByteTy) || |
| 389 | Op0 == Constant::getNullValue(Op0->getType())) { |
| 390 | AddUsesToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 391 | I.replaceAllUsesWith(Op0); |
| 392 | return &I; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | // shl int X, 32 = 0 and shr sbyte Y, 9 = 0, ... just don't eliminate shr of |
| 396 | // a signed value. |
| 397 | // |
| 398 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) { |
| 399 | unsigned TypeBits = Op0->getType()->getPrimitiveSize()*8; |
| 400 | if (CUI->getValue() >= TypeBits && |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 401 | !(Op0->getType()->isSigned() && I.getOpcode() == Instruction::Shr)) { |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 402 | AddUsesToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 403 | I.replaceAllUsesWith(Constant::getNullValue(Op0->getType())); |
| 404 | return &I; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 411 | // isEliminableCastOfCast - Return true if it is valid to eliminate the CI |
| 412 | // instruction. |
| 413 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 414 | static inline bool isEliminableCastOfCast(const CastInst &CI, |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 415 | const CastInst *CSrc) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 416 | assert(CI.getOperand(0) == CSrc); |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 417 | const Type *SrcTy = CSrc->getOperand(0)->getType(); |
| 418 | const Type *MidTy = CSrc->getType(); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 419 | const Type *DstTy = CI.getType(); |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 420 | |
| 421 | // It is legal to eliminate the instruction if casting A->B->A |
| 422 | if (SrcTy == DstTy) return true; |
| 423 | |
| 424 | // Allow free casting and conversion of sizes as long as the sign doesn't |
| 425 | // change... |
| 426 | if (SrcTy->isSigned() == MidTy->isSigned() && |
| 427 | MidTy->isSigned() == DstTy->isSigned()) |
| 428 | return true; |
| 429 | |
| 430 | // Otherwise, we cannot succeed. Specifically we do not want to allow things |
| 431 | // like: short -> ushort -> uint, because this can create wrong results if |
| 432 | // the input short is negative! |
| 433 | // |
| 434 | return false; |
| 435 | } |
| 436 | |
| 437 | |
| 438 | // CastInst simplification |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 439 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 440 | Instruction *InstCombiner::visitCastInst(CastInst &CI) { |
| 441 | if (CI.use_empty()) return 0; // Don't fix dead instructions... |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 442 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 443 | // If the user is casting a value to the same type, eliminate this cast |
| 444 | // instruction... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 445 | if (CI.getType() == CI.getOperand(0)->getType() && !CI.use_empty()) { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 446 | AddUsesToWorkList(CI); // Add all modified instrs to worklist |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 447 | CI.replaceAllUsesWith(CI.getOperand(0)); |
| 448 | return &CI; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 449 | } |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 450 | |
| 451 | |
| 452 | // If casting the result of another cast instruction, try to eliminate this |
| 453 | // one! |
| 454 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 455 | if (CastInst *CSrc = dyn_cast<CastInst>(CI.getOperand(0))) |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 456 | if (isEliminableCastOfCast(CI, CSrc)) { |
| 457 | // This instruction now refers directly to the cast's src operand. This |
| 458 | // has a good chance of making CSrc dead. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 459 | CI.setOperand(0, CSrc->getOperand(0)); |
| 460 | return &CI; |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 463 | return 0; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 466 | |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 467 | // PHINode simplification |
| 468 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 469 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
| 470 | if (PN.use_empty()) return 0; // Don't fix dead instructions... |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 471 | |
| 472 | // If the PHI node only has one incoming value, eliminate the PHI node... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 473 | if (PN.getNumIncomingValues() == 1) { |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 474 | AddUsesToWorkList(PN); // Add all modified instrs to worklist |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 475 | PN.replaceAllUsesWith(PN.getIncomingValue(0)); |
| 476 | return &PN; |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 482 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 483 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 484 | // Is it getelementptr %P, uint 0 |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 485 | // If so, eliminate the noop. |
| 486 | if (GEP.getNumOperands() == 2 && !GEP.use_empty() && |
| 487 | GEP.getOperand(1) == Constant::getNullValue(Type::UIntTy)) { |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 488 | AddUsesToWorkList(GEP); // Add all modified instrs to worklist |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 489 | GEP.replaceAllUsesWith(GEP.getOperand(0)); |
| 490 | return &GEP; |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | return visitMemAccessInst(GEP); |
| 494 | } |
| 495 | |
| 496 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 497 | // Combine Indices - If the source pointer to this mem access instruction is a |
| 498 | // getelementptr instruction, combine the indices of the GEP into this |
| 499 | // instruction |
| 500 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 501 | Instruction *InstCombiner::visitMemAccessInst(MemAccessInst &MAI) { |
Chris Lattner | 43199a0 | 2002-07-10 22:37:17 +0000 | [diff] [blame^] | 502 | return 0; // DISABLE FOLDING. GEP is now the only MAI! |
| 503 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 504 | GetElementPtrInst *Src = |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 505 | dyn_cast<GetElementPtrInst>(MAI.getPointerOperand()); |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 506 | if (!Src) return 0; |
| 507 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 508 | std::vector<Value *> Indices; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 509 | |
| 510 | // Only special case we have to watch out for is pointer arithmetic on the |
| 511 | // 0th index of MAI. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 512 | unsigned FirstIdx = MAI.getFirstIndexOperandNumber(); |
| 513 | if (FirstIdx == MAI.getNumOperands() || |
| 514 | (FirstIdx == MAI.getNumOperands()-1 && |
| 515 | MAI.getOperand(FirstIdx) == ConstantUInt::get(Type::UIntTy, 0))) { |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 516 | // Replace the index list on this MAI with the index on the getelementptr |
| 517 | Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end()); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 518 | } else if (*MAI.idx_begin() == ConstantUInt::get(Type::UIntTy, 0)) { |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 519 | // Otherwise we can do the fold if the first index of the GEP is a zero |
| 520 | Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end()); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 521 | Indices.insert(Indices.end(), MAI.idx_begin()+1, MAI.idx_end()); |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | if (Indices.empty()) return 0; // Can't do the fold? |
| 525 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 526 | switch (MAI.getOpcode()) { |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 527 | case Instruction::GetElementPtr: |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 528 | return new GetElementPtrInst(Src->getOperand(0), Indices, MAI.getName()); |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 529 | case Instruction::Load: |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 530 | return new LoadInst(Src->getOperand(0), Indices, MAI.getName()); |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 531 | case Instruction::Store: |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 532 | return new StoreInst(MAI.getOperand(0), Src->getOperand(0), Indices); |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 533 | default: |
| 534 | assert(0 && "Unknown memaccessinst!"); |
| 535 | break; |
| 536 | } |
| 537 | abort(); |
| 538 | return 0; |
| 539 | } |
| 540 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 541 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 542 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 543 | bool Changed = false; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 544 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 545 | WorkList.insert(WorkList.end(), inst_begin(F), inst_end(F)); |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 546 | |
| 547 | while (!WorkList.empty()) { |
| 548 | Instruction *I = WorkList.back(); // Get an instruction from the worklist |
| 549 | WorkList.pop_back(); |
| 550 | |
| 551 | // Now that we have an instruction, try combining it to simplify it... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 552 | Instruction *Result = visit(*I); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 553 | if (Result) { |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 554 | ++NumCombined; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 555 | // Should we replace the old instruction with a new one? |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 556 | if (Result != I) { |
| 557 | // Instructions can end up on the worklist more than once. Make sure |
| 558 | // we do not process an instruction that has been deleted. |
| 559 | std::vector<Instruction*>::iterator It = std::find(WorkList.begin(), |
| 560 | WorkList.end(), I); |
| 561 | while (It != WorkList.end()) { |
| 562 | It = WorkList.erase(It); |
| 563 | It = std::find(It, WorkList.end(), I); |
| 564 | } |
| 565 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 566 | ReplaceInstWithInst(I, Result); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 567 | } else { |
| 568 | // FIXME: |
| 569 | // FIXME: |
| 570 | // FIXME: This should DCE the instruction to simplify the cases above. |
| 571 | // FIXME: |
| 572 | // FIXME: |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 573 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 574 | |
| 575 | WorkList.push_back(Result); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 576 | AddUsesToWorkList(*Result); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 577 | Changed = true; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 581 | return Changed; |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | Pass *createInstructionCombiningPass() { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 585 | return new InstCombiner(); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 586 | } |