Chris Lattner | ca08125 | 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 |
| 5 | // make instructions dead, so a subsequent DCE pass is useful. |
| 6 | // |
| 7 | // This pass combines things like: |
| 8 | // %Y = add int 1, %X |
| 9 | // %Z = add int 1, %Y |
| 10 | // into: |
| 11 | // %Z = add int 2, %X |
| 12 | // |
| 13 | // This is a simple worklist driven algorithm. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "llvm/Transforms/Scalar/InstructionCombining.h" |
Chris Lattner | 65b529f | 2002-04-08 20:18:09 +0000 | [diff] [blame] | 18 | #include "llvm/ConstantHandling.h" |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 19 | #include "llvm/Function.h" |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 20 | #include "llvm/iMemory.h" |
Chris Lattner | 3a60d04 | 2002-04-15 19:45:29 +0000 | [diff] [blame] | 21 | #include "llvm/iOther.h" |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 22 | #include "llvm/iOperators.h" |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 23 | #include "llvm/Pass.h" |
Chris Lattner | 60a6591 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 24 | #include "llvm/Support/InstIterator.h" |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 25 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | ee965ab | 2002-01-21 23:17:48 +0000 | [diff] [blame] | 26 | #include "../TransformInternals.h" |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 27 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 29 | namespace { |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 30 | class InstCombiner : public FunctionPass, |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 31 | public InstVisitor<InstCombiner, Instruction*> { |
| 32 | // Worklist of all of the instructions that need to be simplified. |
| 33 | std::vector<Instruction*> WorkList; |
| 34 | |
| 35 | void AddUsesToWorkList(Instruction *I) { |
| 36 | // The instruction was simplified, add all users of the instruction to |
| 37 | // the work lists because they might get more simplified now... |
| 38 | // |
| 39 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 40 | UI != UE; ++UI) |
| 41 | WorkList.push_back(cast<Instruction>(*UI)); |
| 42 | } |
| 43 | |
| 44 | public: |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame^] | 45 | const char *getPassName() const { return "Instruction Combining"; } |
| 46 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 47 | virtual bool runOnFunction(Function *F); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 48 | |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 49 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 50 | AU.preservesCFG(); |
| 51 | } |
| 52 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 53 | // Visitation implementation - Implement instruction combining for different |
| 54 | // instruction types. The semantics are as follows: |
| 55 | // Return Value: |
| 56 | // null - No change was made |
| 57 | // I - Change was made, I is still valid |
| 58 | // otherwise - Change was made, replace I with returned instruction |
| 59 | // |
| 60 | |
| 61 | Instruction *visitAdd(BinaryOperator *I); |
| 62 | Instruction *visitSub(BinaryOperator *I); |
| 63 | Instruction *visitMul(BinaryOperator *I); |
| 64 | Instruction *visitCastInst(CastInst *CI); |
| 65 | Instruction *visitMemAccessInst(MemAccessInst *MAI); |
| 66 | |
| 67 | // visitInstruction - Specify what to return for unhandled instructions... |
| 68 | Instruction *visitInstruction(Instruction *I) { return 0; } |
| 69 | }; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | |
| 74 | // Make sure that this instruction has a constant on the right hand side if it |
| 75 | // has any constant arguments. If not, fix it an return true. |
| 76 | // |
| 77 | static bool SimplifyBinOp(BinaryOperator *I) { |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 78 | if (isa<Constant>(I->getOperand(0)) && !isa<Constant>(I->getOperand(1))) |
| 79 | if (!I->swapOperands()) |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 80 | return true; |
| 81 | return false; |
| 82 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 83 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 84 | Instruction *InstCombiner::visitAdd(BinaryOperator *I) { |
| 85 | if (I->use_empty()) return 0; // Don't fix dead add instructions... |
| 86 | bool Changed = SimplifyBinOp(I); |
| 87 | Value *Op1 = I->getOperand(0); |
| 88 | |
| 89 | // Simplify add instructions with a constant RHS... |
| 90 | if (Constant *Op2 = dyn_cast<Constant>(I->getOperand(1))) { |
| 91 | // Eliminate 'add int %X, 0' |
| 92 | if (I->getType()->isIntegral() && Op2->isNullValue()) { |
| 93 | AddUsesToWorkList(I); // Add all modified instrs to worklist |
| 94 | I->replaceAllUsesWith(Op1); |
| 95 | return I; |
| 96 | } |
| 97 | |
| 98 | if (BinaryOperator *IOp1 = dyn_cast<BinaryOperator>(Op1)) { |
| 99 | Changed |= SimplifyBinOp(IOp1); |
| 100 | |
| 101 | if (IOp1->getOpcode() == Instruction::Add && |
| 102 | isa<Constant>(IOp1->getOperand(1))) { |
| 103 | // Fold: |
| 104 | // %Y = add int %X, 1 |
| 105 | // %Z = add int %Y, 1 |
| 106 | // into: |
| 107 | // %Z = add int %X, 2 |
| 108 | // |
| 109 | if (Constant *Val = *Op2 + *cast<Constant>(IOp1->getOperand(1))) { |
| 110 | I->setOperand(0, IOp1->getOperand(0)); |
| 111 | I->setOperand(1, Val); |
Chris Lattner | bee8662 | 2002-03-11 23:28:45 +0000 | [diff] [blame] | 112 | return I; |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 113 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 118 | return Changed ? I : 0; |
| 119 | } |
| 120 | |
| 121 | Instruction *InstCombiner::visitSub(BinaryOperator *I) { |
| 122 | if (I->use_empty()) return 0; // Don't fix dead add instructions... |
| 123 | bool Changed = SimplifyBinOp(I); |
| 124 | |
| 125 | // If this is a subtract instruction with a constant RHS, convert it to an add |
| 126 | // instruction of a negative constant |
| 127 | // |
| 128 | if (Constant *Op2 = dyn_cast<Constant>(I->getOperand(1))) |
| 129 | // Calculate 0 - RHS |
Chris Lattner | 2716b5e | 2002-04-27 02:25:14 +0000 | [diff] [blame] | 130 | if (Constant *RHS = *Constant::getNullValue(I->getType()) - *Op2) { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 131 | return BinaryOperator::create(Instruction::Add, I->getOperand(0), RHS, |
| 132 | I->getName()); |
| 133 | } |
| 134 | |
| 135 | return Changed ? I : 0; |
| 136 | } |
| 137 | |
| 138 | Instruction *InstCombiner::visitMul(BinaryOperator *I) { |
| 139 | if (I->use_empty()) return 0; // Don't fix dead add instructions... |
| 140 | bool Changed = SimplifyBinOp(I); |
| 141 | Value *Op1 = I->getOperand(0); |
| 142 | |
| 143 | // Simplify add instructions with a constant RHS... |
| 144 | if (Constant *Op2 = dyn_cast<Constant>(I->getOperand(1))) { |
| 145 | if (I->getType()->isIntegral() && cast<ConstantInt>(Op2)->equalsInt(1)){ |
| 146 | // Eliminate 'mul int %X, 1' |
| 147 | AddUsesToWorkList(I); // Add all modified instrs to worklist |
| 148 | I->replaceAllUsesWith(Op1); |
| 149 | return I; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return Changed ? I : 0; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | // CastInst simplification - If the user is casting a value to the same type, |
| 158 | // eliminate this cast instruction... |
| 159 | // |
| 160 | Instruction *InstCombiner::visitCastInst(CastInst *CI) { |
| 161 | if (CI->getType() == CI->getOperand(0)->getType() && !CI->use_empty()) { |
| 162 | AddUsesToWorkList(CI); // Add all modified instrs to worklist |
| 163 | CI->replaceAllUsesWith(CI->getOperand(0)); |
| 164 | return CI; |
| 165 | } |
| 166 | return 0; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | // Combine Indices - If the source pointer to this mem access instruction is a |
| 170 | // getelementptr instruction, combine the indices of the GEP into this |
| 171 | // instruction |
| 172 | // |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 173 | Instruction *InstCombiner::visitMemAccessInst(MemAccessInst *MAI) { |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 174 | GetElementPtrInst *Src = |
| 175 | dyn_cast<GetElementPtrInst>(MAI->getPointerOperand()); |
| 176 | if (!Src) return 0; |
| 177 | |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 178 | std::vector<Value *> Indices; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 179 | |
| 180 | // Only special case we have to watch out for is pointer arithmetic on the |
| 181 | // 0th index of MAI. |
| 182 | unsigned FirstIdx = MAI->getFirstIndexOperandNumber(); |
| 183 | if (FirstIdx == MAI->getNumOperands() || |
| 184 | (FirstIdx == MAI->getNumOperands()-1 && |
| 185 | MAI->getOperand(FirstIdx) == ConstantUInt::get(Type::UIntTy, 0))) { |
| 186 | // Replace the index list on this MAI with the index on the getelementptr |
| 187 | Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end()); |
| 188 | } else if (*MAI->idx_begin() == ConstantUInt::get(Type::UIntTy, 0)) { |
| 189 | // Otherwise we can do the fold if the first index of the GEP is a zero |
| 190 | Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end()); |
| 191 | Indices.insert(Indices.end(), MAI->idx_begin()+1, MAI->idx_end()); |
| 192 | } |
| 193 | |
| 194 | if (Indices.empty()) return 0; // Can't do the fold? |
| 195 | |
| 196 | switch (MAI->getOpcode()) { |
| 197 | case Instruction::GetElementPtr: |
| 198 | return new GetElementPtrInst(Src->getOperand(0), Indices, MAI->getName()); |
| 199 | case Instruction::Load: |
| 200 | return new LoadInst(Src->getOperand(0), Indices, MAI->getName()); |
| 201 | case Instruction::Store: |
Chris Lattner | f40379e | 2002-04-18 14:43:54 +0000 | [diff] [blame] | 202 | return new StoreInst(MAI->getOperand(0), Src->getOperand(0), Indices); |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 203 | default: |
| 204 | assert(0 && "Unknown memaccessinst!"); |
| 205 | break; |
| 206 | } |
| 207 | abort(); |
| 208 | return 0; |
| 209 | } |
| 210 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 211 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 212 | bool InstCombiner::runOnFunction(Function *F) { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 213 | bool Changed = false; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 214 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 215 | WorkList.insert(WorkList.end(), inst_begin(F), inst_end(F)); |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 216 | |
| 217 | while (!WorkList.empty()) { |
| 218 | Instruction *I = WorkList.back(); // Get an instruction from the worklist |
| 219 | WorkList.pop_back(); |
| 220 | |
| 221 | // Now that we have an instruction, try combining it to simplify it... |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 222 | Instruction *Result = visit(I); |
| 223 | if (Result) { |
| 224 | // Should we replace the old instruction with a new one? |
| 225 | if (Result != I) |
| 226 | ReplaceInstWithInst(I, Result); |
| 227 | |
| 228 | WorkList.push_back(Result); |
| 229 | AddUsesToWorkList(Result); |
| 230 | Changed = true; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 234 | return Changed; |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | Pass *createInstructionCombiningPass() { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 238 | return new InstCombiner(); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 239 | } |