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