Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1 | //===- InstructionCombining.cpp - Combine multiple instructions -----------===// |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // InstructionCombining - Combine instructions to form fewer, simple |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 4 | // instructions. This pass does not modify the CFG This pass is where algebraic |
| 5 | // simplification happens. |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 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 | |
Chris Lattner | b4cfa7f | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 9b55e5a | 2002-05-07 18:12:18 +0000 | [diff] [blame] | 18 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 65b529f | 2002-04-08 20:18:09 +0000 | [diff] [blame] | 20 | #include "llvm/ConstantHandling.h" |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 21 | #include "llvm/iMemory.h" |
Chris Lattner | 3a60d04 | 2002-04-15 19:45:29 +0000 | [diff] [blame] | 22 | #include "llvm/iOther.h" |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 23 | #include "llvm/iPHINode.h" |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 24 | #include "llvm/iOperators.h" |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 25 | #include "llvm/Pass.h" |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 26 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 60a6591 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 27 | #include "llvm/Support/InstIterator.h" |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 28 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 29 | #include "Support/Statistic.h" |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 30 | #include <algorithm> |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 32 | namespace { |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 33 | Statistic<> NumCombined ("instcombine", "Number of insts combined"); |
| 34 | Statistic<> NumConstProp("instcombine", "Number of constant folds"); |
| 35 | Statistic<> NumDeadInst ("instcombine", "Number of dead inst eliminated"); |
| 36 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 37 | class InstCombiner : public FunctionPass, |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 38 | public InstVisitor<InstCombiner, Instruction*> { |
| 39 | // Worklist of all of the instructions that need to be simplified. |
| 40 | std::vector<Instruction*> WorkList; |
| 41 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 42 | void AddUsesToWorkList(Instruction &I) { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 43 | // The instruction was simplified, add all users of the instruction to |
| 44 | // the work lists because they might get more simplified now... |
| 45 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 46 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 47 | UI != UE; ++UI) |
| 48 | WorkList.push_back(cast<Instruction>(*UI)); |
| 49 | } |
| 50 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 51 | // removeFromWorkList - remove all instances of I from the worklist. |
| 52 | void removeFromWorkList(Instruction *I); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 53 | public: |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 54 | virtual bool runOnFunction(Function &F); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 55 | |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 56 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 57 | AU.setPreservesCFG(); |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 60 | // Visitation implementation - Implement instruction combining for different |
| 61 | // instruction types. The semantics are as follows: |
| 62 | // Return Value: |
| 63 | // null - No change was made |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 64 | // I - Change was made, I is still valid, I may be dead though |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 65 | // otherwise - Change was made, replace I with returned instruction |
| 66 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 67 | Instruction *visitAdd(BinaryOperator &I); |
| 68 | Instruction *visitSub(BinaryOperator &I); |
| 69 | Instruction *visitMul(BinaryOperator &I); |
| 70 | Instruction *visitDiv(BinaryOperator &I); |
| 71 | Instruction *visitRem(BinaryOperator &I); |
| 72 | Instruction *visitAnd(BinaryOperator &I); |
| 73 | Instruction *visitOr (BinaryOperator &I); |
| 74 | Instruction *visitXor(BinaryOperator &I); |
| 75 | Instruction *visitSetCondInst(BinaryOperator &I); |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame^] | 76 | Instruction *visitShiftInst(ShiftInst &I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 77 | Instruction *visitCastInst(CastInst &CI); |
| 78 | Instruction *visitPHINode(PHINode &PN); |
| 79 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 80 | Instruction *visitAllocationInst(AllocationInst &AI); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 81 | |
| 82 | // visitInstruction - Specify what to return for unhandled instructions... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 83 | Instruction *visitInstruction(Instruction &I) { return 0; } |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 84 | |
| 85 | // InsertNewInstBefore - insert an instruction New before instruction Old |
| 86 | // in the program. Add the new instruction to the worklist. |
| 87 | // |
| 88 | void InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Chris Lattner | 65217ff | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 89 | assert(New && New->getParent() == 0 && |
| 90 | "New instruction already inserted into a basic block!"); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 91 | BasicBlock *BB = Old.getParent(); |
| 92 | BB->getInstList().insert(&Old, New); // Insert inst |
| 93 | WorkList.push_back(New); // Add to worklist |
| 94 | } |
| 95 | |
| 96 | // ReplaceInstUsesWith - This method is to be used when an instruction is |
| 97 | // found to be dead, replacable with another preexisting expression. Here |
| 98 | // we add all uses of I to the worklist, replace all uses of I with the new |
| 99 | // value, then return I, so that the inst combiner will know that I was |
| 100 | // modified. |
| 101 | // |
| 102 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { |
| 103 | AddUsesToWorkList(I); // Add all modified instrs to worklist |
| 104 | I.replaceAllUsesWith(V); |
| 105 | return &I; |
| 106 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 107 | }; |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 108 | |
Chris Lattner | c8b7092 | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 109 | RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions"); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 113 | // Make sure that this instruction has a constant on the right hand side if it |
| 114 | // has any constant arguments. If not, fix it an return true. |
| 115 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 116 | static bool SimplifyBinOp(BinaryOperator &I) { |
| 117 | if (isa<Constant>(I.getOperand(0)) && !isa<Constant>(I.getOperand(1))) |
| 118 | return !I.swapOperands(); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 119 | return false; |
| 120 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 122 | // dyn_castNegInst - Given a 'sub' instruction, return the RHS of the |
| 123 | // instruction if the LHS is a constant zero (which is the 'negate' form). |
| 124 | // |
| 125 | static inline Value *dyn_castNegInst(Value *V) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 126 | return BinaryOperator::isNeg(V) ? |
| 127 | BinaryOperator::getNegArgument(cast<BinaryOperator>(V)) : 0; |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 130 | static inline Value *dyn_castNotInst(Value *V) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 131 | return BinaryOperator::isNot(V) ? |
| 132 | BinaryOperator::getNotArgument(cast<BinaryOperator>(V)) : 0; |
| 133 | } |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 134 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 135 | |
| 136 | // Log2 - Calculate the log base 2 for the specified value if it is exactly a |
| 137 | // power of 2. |
| 138 | static unsigned Log2(uint64_t Val) { |
| 139 | assert(Val > 1 && "Values 0 and 1 should be handled elsewhere!"); |
| 140 | unsigned Count = 0; |
| 141 | while (Val != 1) { |
| 142 | if (Val & 1) return 0; // Multiple bits set? |
| 143 | Val >>= 1; |
| 144 | ++Count; |
| 145 | } |
| 146 | return Count; |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 149 | static inline Value *dyn_castFoldableMul(Value *V) { |
| 150 | if (V->use_size() == 1 && V->getType()->isInteger()) |
| 151 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 152 | if (I->getOpcode() == Instruction::Mul) |
| 153 | if (isa<Constant>(I->getOperand(1))) |
| 154 | return I->getOperand(0); |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 159 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 160 | bool Changed = SimplifyBinOp(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 161 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 162 | |
| 163 | // Eliminate 'add int %X, 0' |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 164 | if (RHS == Constant::getNullValue(I.getType())) |
| 165 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 166 | |
Chris Lattner | 147e975 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 167 | // -A + B --> B - A |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 168 | if (Value *V = dyn_castNegInst(LHS)) |
Chris Lattner | 147e975 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 169 | return BinaryOperator::create(Instruction::Sub, RHS, V); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 170 | |
| 171 | // A + -B --> A - B |
| 172 | if (Value *V = dyn_castNegInst(RHS)) |
Chris Lattner | 147e975 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 173 | return BinaryOperator::create(Instruction::Sub, LHS, V); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 174 | |
| 175 | // Simplify add instructions with a constant RHS... |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 176 | if (Constant *Op2 = dyn_cast<Constant>(RHS)) { |
| 177 | if (BinaryOperator *ILHS = dyn_cast<BinaryOperator>(LHS)) { |
| 178 | if (ILHS->getOpcode() == Instruction::Add && |
| 179 | isa<Constant>(ILHS->getOperand(1))) { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 180 | // Fold: |
| 181 | // %Y = add int %X, 1 |
| 182 | // %Z = add int %Y, 1 |
| 183 | // into: |
| 184 | // %Z = add int %X, 2 |
| 185 | // |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 186 | if (Constant *Val = *Op2 + *cast<Constant>(ILHS->getOperand(1))) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 187 | I.setOperand(0, ILHS->getOperand(0)); |
| 188 | I.setOperand(1, Val); |
| 189 | return &I; |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 190 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 195 | // X*C + X --> X * (C+1) |
| 196 | if (dyn_castFoldableMul(LHS) == RHS) { |
| 197 | Constant *CP1 = *cast<Constant>(cast<Instruction>(LHS)->getOperand(1)) + |
| 198 | *ConstantInt::get(I.getType(), 1); |
| 199 | assert(CP1 && "Couldn't constant fold C + 1?"); |
| 200 | return BinaryOperator::create(Instruction::Mul, RHS, CP1); |
| 201 | } |
| 202 | |
| 203 | // X + X*C --> X * (C+1) |
| 204 | if (dyn_castFoldableMul(RHS) == LHS) { |
| 205 | Constant *CP1 = *cast<Constant>(cast<Instruction>(RHS)->getOperand(1)) + |
| 206 | *ConstantInt::get(I.getType(), 1); |
| 207 | assert(CP1 && "Couldn't constant fold C + 1?"); |
| 208 | return BinaryOperator::create(Instruction::Mul, LHS, CP1); |
| 209 | } |
| 210 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 211 | return Changed ? &I : 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 214 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 215 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 216 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 217 | if (Op0 == Op1) // sub X, X -> 0 |
| 218 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 219 | |
| 220 | // If this is a subtract instruction with a constant RHS, convert it to an add |
| 221 | // instruction of a negative constant |
| 222 | // |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 223 | if (Constant *Op2 = dyn_cast<Constant>(Op1)) |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 224 | if (Constant *RHS = *Constant::getNullValue(I.getType()) - *Op2) // 0 - RHS |
| 225 | return BinaryOperator::create(Instruction::Add, Op0, RHS, I.getName()); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 226 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 227 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | 147e975 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 228 | if (Value *V = dyn_castNegInst(Op1)) |
| 229 | return BinaryOperator::create(Instruction::Add, Op0, V); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 230 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 231 | // Replace (-1 - A) with (~A)... |
| 232 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) |
| 233 | if (C->isAllOnesValue()) |
| 234 | return BinaryOperator::createNot(Op1); |
Chris Lattner | ad3c495 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 236 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) |
| 237 | if (Op1I->use_size() == 1) { |
| 238 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 239 | // is not used by anyone else... |
| 240 | // |
| 241 | if (Op1I->getOpcode() == Instruction::Sub) { |
| 242 | // Swap the two operands of the subexpr... |
| 243 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 244 | Op1I->setOperand(0, IIOp1); |
| 245 | Op1I->setOperand(1, IIOp0); |
| 246 | |
| 247 | // Create the new top level add instruction... |
| 248 | return BinaryOperator::create(Instruction::Add, Op0, Op1); |
| 249 | } |
| 250 | |
| 251 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 252 | // |
| 253 | if (Op1I->getOpcode() == Instruction::And && |
| 254 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 255 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 256 | |
| 257 | Instruction *NewNot = BinaryOperator::createNot(OtherOp, "B.not", &I); |
| 258 | return BinaryOperator::create(Instruction::And, Op0, NewNot); |
| 259 | } |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 260 | |
| 261 | // X - X*C --> X * (1-C) |
| 262 | if (dyn_castFoldableMul(Op1I) == Op0) { |
| 263 | Constant *CP1 = *ConstantInt::get(I.getType(), 1) - |
| 264 | *cast<Constant>(cast<Instruction>(Op1)->getOperand(1)); |
| 265 | assert(CP1 && "Couldn't constant fold 1-C?"); |
| 266 | return BinaryOperator::create(Instruction::Mul, Op0, CP1); |
| 267 | } |
Chris Lattner | ad3c495 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 268 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 269 | |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 270 | // X*C - X --> X * (C-1) |
| 271 | if (dyn_castFoldableMul(Op0) == Op1) { |
| 272 | Constant *CP1 = *cast<Constant>(cast<Instruction>(Op0)->getOperand(1)) - |
| 273 | *ConstantInt::get(I.getType(), 1); |
| 274 | assert(CP1 && "Couldn't constant fold C - 1?"); |
| 275 | return BinaryOperator::create(Instruction::Mul, Op1, CP1); |
| 276 | } |
| 277 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 278 | return 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 281 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 282 | bool Changed = SimplifyBinOp(I); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 283 | Value *Op0 = I.getOperand(0); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 284 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 285 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 286 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 287 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
| 288 | const Type *Ty = CI->getType(); |
| 289 | uint64_t Val = Ty->isSigned() ? |
| 290 | (uint64_t)cast<ConstantSInt>(CI)->getValue() : |
| 291 | cast<ConstantUInt>(CI)->getValue(); |
| 292 | switch (Val) { |
| 293 | case 0: |
| 294 | return ReplaceInstUsesWith(I, Op1); // Eliminate 'mul double %X, 0' |
| 295 | case 1: |
| 296 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul int %X, 1' |
| 297 | case 2: // Convert 'mul int %X, 2' to 'add int %X, %X' |
| 298 | return BinaryOperator::create(Instruction::Add, Op0, Op0, I.getName()); |
| 299 | } |
Chris Lattner | 31ba129 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 300 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 301 | if (uint64_t C = Log2(Val)) // Replace X*(2^C) with X << C |
| 302 | return new ShiftInst(Instruction::Shl, Op0, |
| 303 | ConstantUInt::get(Type::UByteTy, C)); |
| 304 | } else { |
| 305 | ConstantFP *Op1F = cast<ConstantFP>(Op1); |
| 306 | if (Op1F->isNullValue()) |
| 307 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 31ba129 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 308 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 309 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 310 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) |
| 311 | if (Op1F->getValue() == 1.0) |
| 312 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
| 313 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 316 | return Changed ? &I : 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 319 | Instruction *InstCombiner::visitDiv(BinaryOperator &I) { |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 320 | // div X, 1 == X |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 321 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) { |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 322 | if (RHS->equalsInt(1)) |
| 323 | return ReplaceInstUsesWith(I, I.getOperand(0)); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 324 | |
| 325 | // Check to see if this is an unsigned division with an exact power of 2, |
| 326 | // if so, convert to a right shift. |
| 327 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS)) |
| 328 | if (uint64_t Val = C->getValue()) // Don't break X / 0 |
| 329 | if (uint64_t C = Log2(Val)) |
| 330 | return new ShiftInst(Instruction::Shr, I.getOperand(0), |
| 331 | ConstantUInt::get(Type::UByteTy, C)); |
| 332 | } |
| 333 | |
| 334 | // 0 / X == 0, we don't need to preserve faults! |
| 335 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(I.getOperand(0))) |
| 336 | if (LHS->equalsInt(0)) |
| 337 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 338 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 343 | Instruction *InstCombiner::visitRem(BinaryOperator &I) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 344 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) { |
| 345 | if (RHS->equalsInt(1)) // X % 1 == 0 |
| 346 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 347 | |
| 348 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 349 | // if so, convert to a bitwise and. |
| 350 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS)) |
| 351 | if (uint64_t Val = C->getValue()) // Don't break X % 0 (divide by zero) |
| 352 | if (Log2(Val)) |
| 353 | return BinaryOperator::create(Instruction::And, I.getOperand(0), |
| 354 | ConstantUInt::get(I.getType(), Val-1)); |
| 355 | } |
| 356 | |
| 357 | // 0 % X == 0, we don't need to preserve faults! |
| 358 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(I.getOperand(0))) |
| 359 | if (LHS->equalsInt(0)) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 360 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 361 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 362 | return 0; |
| 363 | } |
| 364 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 365 | // isMaxValueMinusOne - return true if this is Max-1 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 366 | static bool isMaxValueMinusOne(const ConstantInt *C) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 367 | if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) { |
| 368 | // Calculate -1 casted to the right type... |
| 369 | unsigned TypeBits = C->getType()->getPrimitiveSize()*8; |
| 370 | uint64_t Val = ~0ULL; // All ones |
| 371 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 372 | return CU->getValue() == Val-1; |
| 373 | } |
| 374 | |
| 375 | const ConstantSInt *CS = cast<ConstantSInt>(C); |
| 376 | |
| 377 | // Calculate 0111111111..11111 |
| 378 | unsigned TypeBits = C->getType()->getPrimitiveSize()*8; |
| 379 | int64_t Val = INT64_MAX; // All ones |
| 380 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 381 | return CS->getValue() == Val-1; |
| 382 | } |
| 383 | |
| 384 | // isMinValuePlusOne - return true if this is Min+1 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 385 | static bool isMinValuePlusOne(const ConstantInt *C) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 386 | if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) |
| 387 | return CU->getValue() == 1; |
| 388 | |
| 389 | const ConstantSInt *CS = cast<ConstantSInt>(C); |
| 390 | |
| 391 | // Calculate 1111111111000000000000 |
| 392 | unsigned TypeBits = C->getType()->getPrimitiveSize()*8; |
| 393 | int64_t Val = -1; // All ones |
| 394 | Val <<= TypeBits-1; // Shift over to the right spot |
| 395 | return CS->getValue() == Val+1; |
| 396 | } |
| 397 | |
| 398 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 399 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 400 | bool Changed = SimplifyBinOp(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 401 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 402 | |
| 403 | // and X, X = X and X, 0 == 0 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 404 | if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) |
| 405 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 406 | |
| 407 | // and X, -1 == X |
Chris Lattner | 8328263 | 2002-08-13 17:50:24 +0000 | [diff] [blame] | 408 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 409 | if (RHS->isAllOnesValue()) |
| 410 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 411 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 412 | Value *Op0NotVal = dyn_castNotInst(Op0); |
| 413 | Value *Op1NotVal = dyn_castNotInst(Op1); |
| 414 | |
| 415 | // (~A & ~B) == (~(A | B)) - Demorgan's Law |
| 416 | if (Op0->use_size() == 1 && Op1->use_size() == 1 && Op0NotVal && Op1NotVal) { |
| 417 | Instruction *Or = BinaryOperator::create(Instruction::Or, Op0NotVal, |
| 418 | Op1NotVal,I.getName()+".demorgan", |
| 419 | &I); |
| 420 | return BinaryOperator::createNot(Or); |
| 421 | } |
| 422 | |
| 423 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
| 424 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 65217ff | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 425 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 426 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | |
| 430 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 431 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 432 | bool Changed = SimplifyBinOp(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 433 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 434 | |
| 435 | // or X, X = X or X, 0 == X |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 436 | if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) |
| 437 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 438 | |
| 439 | // or X, -1 == -1 |
Chris Lattner | 8328263 | 2002-08-13 17:50:24 +0000 | [diff] [blame] | 440 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 441 | if (RHS->isAllOnesValue()) |
| 442 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 443 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 444 | if (Value *X = dyn_castNotInst(Op0)) // ~A | A == -1 |
| 445 | if (X == Op1) |
| 446 | return ReplaceInstUsesWith(I, |
| 447 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 448 | |
| 449 | if (Value *X = dyn_castNotInst(Op1)) // A | ~A == -1 |
| 450 | if (X == Op0) |
| 451 | return ReplaceInstUsesWith(I, |
| 452 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 453 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 454 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | |
| 458 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 459 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 460 | bool Changed = SimplifyBinOp(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 461 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 462 | |
| 463 | // xor X, X = 0 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 464 | if (Op0 == Op1) |
| 465 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 466 | |
Chris Lattner | 8328263 | 2002-08-13 17:50:24 +0000 | [diff] [blame] | 467 | if (ConstantIntegral *Op1C = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 468 | // xor X, 0 == X |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 469 | if (Op1C->isNullValue()) |
| 470 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 471 | |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 472 | // Is this a "NOT" instruction? |
| 473 | if (Op1C->isAllOnesValue()) { |
| 474 | // xor (xor X, -1), -1 = not (not X) = X |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 475 | if (Value *X = dyn_castNotInst(Op0)) |
| 476 | return ReplaceInstUsesWith(I, X); |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 477 | |
| 478 | // xor (setcc A, B), true = not (setcc A, B) = setncc A, B |
| 479 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(Op0)) |
| 480 | if (SCI->use_size() == 1) |
| 481 | return new SetCondInst(SCI->getInverseCondition(), |
| 482 | SCI->getOperand(0), SCI->getOperand(1)); |
| 483 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 486 | if (Value *X = dyn_castNotInst(Op0)) // ~A ^ A == -1 |
| 487 | if (X == Op1) |
| 488 | return ReplaceInstUsesWith(I, |
| 489 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 490 | |
| 491 | if (Value *X = dyn_castNotInst(Op1)) // A ^ ~A == -1 |
| 492 | if (X == Op0) |
| 493 | return ReplaceInstUsesWith(I, |
| 494 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 495 | |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 496 | |
| 497 | |
| 498 | if (Instruction *Op1I = dyn_cast<Instruction>(Op1)) |
| 499 | if (Op1I->getOpcode() == Instruction::Or) |
| 500 | if (Op1I->getOperand(0) == Op0) { // B^(B|A) == (A|B)^B |
| 501 | cast<BinaryOperator>(Op1I)->swapOperands(); |
| 502 | I.swapOperands(); |
| 503 | std::swap(Op0, Op1); |
| 504 | } else if (Op1I->getOperand(1) == Op0) { // B^(A|B) == (A|B)^B |
| 505 | I.swapOperands(); |
| 506 | std::swap(Op0, Op1); |
| 507 | } |
| 508 | |
| 509 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) |
| 510 | if (Op0I->getOpcode() == Instruction::Or && Op0I->use_size() == 1) { |
| 511 | if (Op0I->getOperand(0) == Op1) // (B|A)^B == (A|B)^B |
| 512 | cast<BinaryOperator>(Op0I)->swapOperands(); |
| 513 | if (Op0I->getOperand(1) == Op1) { // (A|B)^B == A & ~B |
| 514 | Value *NotB = BinaryOperator::createNot(Op1, Op1->getName()+".not", &I); |
| 515 | WorkList.push_back(cast<Instruction>(NotB)); |
| 516 | return BinaryOperator::create(Instruction::And, Op0I->getOperand(0), |
| 517 | NotB); |
| 518 | } |
| 519 | } |
| 520 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 521 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 524 | // AddOne, SubOne - Add or subtract a constant one from an integer constant... |
| 525 | static Constant *AddOne(ConstantInt *C) { |
| 526 | Constant *Result = *C + *ConstantInt::get(C->getType(), 1); |
| 527 | assert(Result && "Constant folding integer addition failed!"); |
| 528 | return Result; |
| 529 | } |
| 530 | static Constant *SubOne(ConstantInt *C) { |
| 531 | Constant *Result = *C - *ConstantInt::get(C->getType(), 1); |
| 532 | assert(Result && "Constant folding integer addition failed!"); |
| 533 | return Result; |
| 534 | } |
| 535 | |
Chris Lattner | 1fc23f3 | 2002-05-09 20:11:54 +0000 | [diff] [blame] | 536 | // isTrueWhenEqual - Return true if the specified setcondinst instruction is |
| 537 | // true when both operands are equal... |
| 538 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 539 | static bool isTrueWhenEqual(Instruction &I) { |
| 540 | return I.getOpcode() == Instruction::SetEQ || |
| 541 | I.getOpcode() == Instruction::SetGE || |
| 542 | I.getOpcode() == Instruction::SetLE; |
Chris Lattner | 1fc23f3 | 2002-05-09 20:11:54 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 545 | Instruction *InstCombiner::visitSetCondInst(BinaryOperator &I) { |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 546 | bool Changed = SimplifyBinOp(I); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 547 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 548 | const Type *Ty = Op0->getType(); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 549 | |
| 550 | // setcc X, X |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 551 | if (Op0 == Op1) |
| 552 | return ReplaceInstUsesWith(I, ConstantBool::get(isTrueWhenEqual(I))); |
Chris Lattner | 1fc23f3 | 2002-05-09 20:11:54 +0000 | [diff] [blame] | 553 | |
| 554 | // setcc <global*>, 0 - Global value addresses are never null! |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 555 | if (isa<GlobalValue>(Op0) && isa<ConstantPointerNull>(Op1)) |
| 556 | return ReplaceInstUsesWith(I, ConstantBool::get(!isTrueWhenEqual(I))); |
| 557 | |
| 558 | // setcc's with boolean values can always be turned into bitwise operations |
| 559 | if (Ty == Type::BoolTy) { |
| 560 | // If this is <, >, or !=, we can change this into a simple xor instruction |
| 561 | if (!isTrueWhenEqual(I)) |
| 562 | return BinaryOperator::create(Instruction::Xor, Op0, Op1, I.getName()); |
| 563 | |
| 564 | // Otherwise we need to make a temporary intermediate instruction and insert |
| 565 | // it into the instruction stream. This is what we are after: |
| 566 | // |
| 567 | // seteq bool %A, %B -> ~(A^B) |
| 568 | // setle bool %A, %B -> ~A | B |
| 569 | // setge bool %A, %B -> A | ~B |
| 570 | // |
| 571 | if (I.getOpcode() == Instruction::SetEQ) { // seteq case |
| 572 | Instruction *Xor = BinaryOperator::create(Instruction::Xor, Op0, Op1, |
| 573 | I.getName()+"tmp"); |
| 574 | InsertNewInstBefore(Xor, I); |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 575 | return BinaryOperator::createNot(Xor, I.getName()); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | // Handle the setXe cases... |
| 579 | assert(I.getOpcode() == Instruction::SetGE || |
| 580 | I.getOpcode() == Instruction::SetLE); |
| 581 | |
| 582 | if (I.getOpcode() == Instruction::SetGE) |
| 583 | std::swap(Op0, Op1); // Change setge -> setle |
| 584 | |
| 585 | // Now we just have the SetLE case. |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 586 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 587 | InsertNewInstBefore(Not, I); |
| 588 | return BinaryOperator::create(Instruction::Or, Not, Op1, I.getName()); |
| 589 | } |
| 590 | |
| 591 | // Check to see if we are doing one of many comparisons against constant |
| 592 | // integers at the end of their ranges... |
| 593 | // |
| 594 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
| 595 | // Check to see if we are comparing against the minimum or maximum value... |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 596 | if (CI->isMinValue()) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 597 | if (I.getOpcode() == Instruction::SetLT) // A < MIN -> FALSE |
| 598 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 599 | if (I.getOpcode() == Instruction::SetGE) // A >= MIN -> TRUE |
| 600 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 601 | if (I.getOpcode() == Instruction::SetLE) // A <= MIN -> A == MIN |
| 602 | return BinaryOperator::create(Instruction::SetEQ, Op0,Op1, I.getName()); |
| 603 | if (I.getOpcode() == Instruction::SetGT) // A > MIN -> A != MIN |
| 604 | return BinaryOperator::create(Instruction::SetNE, Op0,Op1, I.getName()); |
| 605 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 606 | } else if (CI->isMaxValue()) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 607 | if (I.getOpcode() == Instruction::SetGT) // A > MAX -> FALSE |
| 608 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 609 | if (I.getOpcode() == Instruction::SetLE) // A <= MAX -> TRUE |
| 610 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 611 | if (I.getOpcode() == Instruction::SetGE) // A >= MAX -> A == MAX |
| 612 | return BinaryOperator::create(Instruction::SetEQ, Op0,Op1, I.getName()); |
| 613 | if (I.getOpcode() == Instruction::SetLT) // A < MAX -> A != MAX |
| 614 | return BinaryOperator::create(Instruction::SetNE, Op0,Op1, I.getName()); |
| 615 | |
| 616 | // Comparing against a value really close to min or max? |
| 617 | } else if (isMinValuePlusOne(CI)) { |
| 618 | if (I.getOpcode() == Instruction::SetLT) // A < MIN+1 -> A == MIN |
| 619 | return BinaryOperator::create(Instruction::SetEQ, Op0, |
| 620 | SubOne(CI), I.getName()); |
| 621 | if (I.getOpcode() == Instruction::SetGE) // A >= MIN-1 -> A != MIN |
| 622 | return BinaryOperator::create(Instruction::SetNE, Op0, |
| 623 | SubOne(CI), I.getName()); |
| 624 | |
| 625 | } else if (isMaxValueMinusOne(CI)) { |
| 626 | if (I.getOpcode() == Instruction::SetGT) // A > MAX-1 -> A == MAX |
| 627 | return BinaryOperator::create(Instruction::SetEQ, Op0, |
| 628 | AddOne(CI), I.getName()); |
| 629 | if (I.getOpcode() == Instruction::SetLE) // A <= MAX-1 -> A != MAX |
| 630 | return BinaryOperator::create(Instruction::SetNE, Op0, |
| 631 | AddOne(CI), I.getName()); |
| 632 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 635 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | |
| 639 | |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame^] | 640 | Instruction *InstCombiner::visitShiftInst(ShiftInst &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 641 | assert(I.getOperand(1)->getType() == Type::UByteTy); |
| 642 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 643 | |
| 644 | // shl X, 0 == X and shr X, 0 == X |
| 645 | // shl 0, X == 0 and shr 0, X == 0 |
| 646 | if (Op1 == Constant::getNullValue(Type::UByteTy) || |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 647 | Op0 == Constant::getNullValue(Op0->getType())) |
| 648 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 649 | |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame^] | 650 | // If this is a shift of a shift, see if we can fold the two together... |
| 651 | if (ShiftInst *Op0SI = dyn_cast<ShiftInst>(Op0)) { |
| 652 | if (isa<Constant>(Op1) && isa<Constant>(Op0SI->getOperand(1))) { |
| 653 | ConstantUInt *ShiftAmt1C = cast<ConstantUInt>(Op0SI->getOperand(1)); |
| 654 | unsigned ShiftAmt1 = ShiftAmt1C->getValue(); |
| 655 | unsigned ShiftAmt2 = cast<ConstantUInt>(Op1)->getValue(); |
| 656 | |
| 657 | // Check for (A << c1) << c2 and (A >> c1) >> c2 |
| 658 | if (I.getOpcode() == Op0SI->getOpcode()) { |
| 659 | unsigned Amt = ShiftAmt1+ShiftAmt2; // Fold into one big shift... |
| 660 | return new ShiftInst(I.getOpcode(), Op0SI->getOperand(0), |
| 661 | ConstantUInt::get(Type::UByteTy, Amt)); |
| 662 | } |
| 663 | |
| 664 | if (I.getType()->isUnsigned()) { // Check for (A << c1) >> c2 or visaversa |
| 665 | // Calculate bitmask for what gets shifted off the edge... |
| 666 | Constant *C = ConstantIntegral::getAllOnesValue(I.getType()); |
| 667 | if (I.getOpcode() == Instruction::Shr) |
| 668 | C = *C >> *ShiftAmt1C; |
| 669 | else |
| 670 | C = *C << *ShiftAmt1C; |
| 671 | assert(C && "Couldn't constant fold shift expression?"); |
| 672 | |
| 673 | Instruction *Mask = |
| 674 | BinaryOperator::create(Instruction::And, Op0SI->getOperand(0), |
| 675 | C, Op0SI->getOperand(0)->getName()+".mask",&I); |
| 676 | WorkList.push_back(Mask); |
| 677 | |
| 678 | // Figure out what flavor of shift we should use... |
| 679 | if (ShiftAmt1 == ShiftAmt2) |
| 680 | return ReplaceInstUsesWith(I, Mask); // (A << c) >> c === A & c2 |
| 681 | else if (ShiftAmt1 < ShiftAmt2) { |
| 682 | return new ShiftInst(I.getOpcode(), Mask, |
| 683 | ConstantUInt::get(Type::UByteTy, ShiftAmt2-ShiftAmt1)); |
| 684 | } else { |
| 685 | return new ShiftInst(Op0SI->getOpcode(), Mask, |
| 686 | ConstantUInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2)); |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | } |
| 691 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 692 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr of |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 693 | // a signed value. |
| 694 | // |
| 695 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) { |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame^] | 696 | unsigned TypeBits = Op0->getType()->getPrimitiveSize()*8; |
| 697 | if (CUI->getValue() >= TypeBits && |
| 698 | (!Op0->getType()->isSigned() || I.getOpcode() == Instruction::Shl)) |
| 699 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
Chris Lattner | 55f3d94 | 2002-09-10 23:04:09 +0000 | [diff] [blame] | 700 | |
| 701 | // Check to see if we are shifting left by 1. If so, turn it into an add |
| 702 | // instruction. |
| 703 | if (I.getOpcode() == Instruction::Shl && CUI->equalsInt(1)) |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 704 | // Convert 'shl int %X, 1' to 'add int %X, %X' |
Chris Lattner | 55f3d94 | 2002-09-10 23:04:09 +0000 | [diff] [blame] | 705 | return BinaryOperator::create(Instruction::Add, Op0, Op0, I.getName()); |
| 706 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 707 | } |
Chris Lattner | 2e0fb39 | 2002-10-08 16:16:40 +0000 | [diff] [blame] | 708 | |
| 709 | // shr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 710 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0)) |
| 711 | if (I.getOpcode() == Instruction::Shr && CSI->isAllOnesValue()) |
| 712 | return ReplaceInstUsesWith(I, CSI); |
| 713 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 718 | // isEliminableCastOfCast - Return true if it is valid to eliminate the CI |
| 719 | // instruction. |
| 720 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 721 | static inline bool isEliminableCastOfCast(const CastInst &CI, |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 722 | const CastInst *CSrc) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 723 | assert(CI.getOperand(0) == CSrc); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 724 | const Type *SrcTy = CSrc->getOperand(0)->getType(); |
| 725 | const Type *MidTy = CSrc->getType(); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 726 | const Type *DstTy = CI.getType(); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 727 | |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 728 | // It is legal to eliminate the instruction if casting A->B->A if the sizes |
| 729 | // are identical and the bits don't get reinterpreted (for example |
Chris Lattner | 0bb7591 | 2002-08-14 23:21:10 +0000 | [diff] [blame] | 730 | // int->float->int would not be allowed) |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 731 | if (SrcTy == DstTy && SrcTy->isLosslesslyConvertableTo(MidTy)) |
| 732 | return true; |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 733 | |
| 734 | // Allow free casting and conversion of sizes as long as the sign doesn't |
| 735 | // change... |
Chris Lattner | b0b412e | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 736 | if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral()) { |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 737 | unsigned SrcSize = SrcTy->getPrimitiveSize(); |
| 738 | unsigned MidSize = MidTy->getPrimitiveSize(); |
| 739 | unsigned DstSize = DstTy->getPrimitiveSize(); |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 740 | |
Chris Lattner | 3732aca | 2002-08-15 16:15:25 +0000 | [diff] [blame] | 741 | // Cases where we are monotonically decreasing the size of the type are |
| 742 | // always ok, regardless of what sign changes are going on. |
| 743 | // |
Chris Lattner | 0bb7591 | 2002-08-14 23:21:10 +0000 | [diff] [blame] | 744 | if (SrcSize >= MidSize && MidSize >= DstSize) |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 745 | return true; |
Chris Lattner | 3732aca | 2002-08-15 16:15:25 +0000 | [diff] [blame] | 746 | |
Chris Lattner | 555518c | 2002-09-23 23:39:43 +0000 | [diff] [blame] | 747 | // Cases where the source and destination type are the same, but the middle |
| 748 | // type is bigger are noops. |
| 749 | // |
| 750 | if (SrcSize == DstSize && MidSize > SrcSize) |
| 751 | return true; |
| 752 | |
Chris Lattner | 3732aca | 2002-08-15 16:15:25 +0000 | [diff] [blame] | 753 | // If we are monotonically growing, things are more complex. |
| 754 | // |
| 755 | if (SrcSize <= MidSize && MidSize <= DstSize) { |
| 756 | // We have eight combinations of signedness to worry about. Here's the |
| 757 | // table: |
| 758 | static const int SignTable[8] = { |
| 759 | // CODE, SrcSigned, MidSigned, DstSigned, Comment |
| 760 | 1, // U U U Always ok |
| 761 | 1, // U U S Always ok |
| 762 | 3, // U S U Ok iff SrcSize != MidSize |
| 763 | 3, // U S S Ok iff SrcSize != MidSize |
| 764 | 0, // S U U Never ok |
| 765 | 2, // S U S Ok iff MidSize == DstSize |
| 766 | 1, // S S U Always ok |
| 767 | 1, // S S S Always ok |
| 768 | }; |
| 769 | |
| 770 | // Choose an action based on the current entry of the signtable that this |
| 771 | // cast of cast refers to... |
| 772 | unsigned Row = SrcTy->isSigned()*4+MidTy->isSigned()*2+DstTy->isSigned(); |
| 773 | switch (SignTable[Row]) { |
| 774 | case 0: return false; // Never ok |
| 775 | case 1: return true; // Always ok |
| 776 | case 2: return MidSize == DstSize; // Ok iff MidSize == DstSize |
| 777 | case 3: // Ok iff SrcSize != MidSize |
| 778 | return SrcSize != MidSize || SrcTy == Type::BoolTy; |
| 779 | default: assert(0 && "Bad entry in sign table!"); |
| 780 | } |
Chris Lattner | 3732aca | 2002-08-15 16:15:25 +0000 | [diff] [blame] | 781 | } |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 782 | } |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 783 | |
| 784 | // Otherwise, we cannot succeed. Specifically we do not want to allow things |
| 785 | // like: short -> ushort -> uint, because this can create wrong results if |
| 786 | // the input short is negative! |
| 787 | // |
| 788 | return false; |
| 789 | } |
| 790 | |
| 791 | |
| 792 | // CastInst simplification |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 793 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 794 | Instruction *InstCombiner::visitCastInst(CastInst &CI) { |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 795 | // If the user is casting a value to the same type, eliminate this cast |
| 796 | // instruction... |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 797 | if (CI.getType() == CI.getOperand(0)->getType()) |
| 798 | return ReplaceInstUsesWith(CI, CI.getOperand(0)); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 799 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 800 | // If casting the result of another cast instruction, try to eliminate this |
| 801 | // one! |
| 802 | // |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 803 | if (CastInst *CSrc = dyn_cast<CastInst>(CI.getOperand(0))) { |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 804 | if (isEliminableCastOfCast(CI, CSrc)) { |
| 805 | // This instruction now refers directly to the cast's src operand. This |
| 806 | // has a good chance of making CSrc dead. |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 807 | CI.setOperand(0, CSrc->getOperand(0)); |
| 808 | return &CI; |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 811 | // If this is an A->B->A cast, and we are dealing with integral types, try |
| 812 | // to convert this into a logical 'and' instruction. |
| 813 | // |
| 814 | if (CSrc->getOperand(0)->getType() == CI.getType() && |
Chris Lattner | b0b412e | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 815 | CI.getType()->isInteger() && CSrc->getType()->isInteger() && |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 816 | CI.getType()->isUnsigned() && CSrc->getType()->isUnsigned() && |
| 817 | CSrc->getType()->getPrimitiveSize() < CI.getType()->getPrimitiveSize()){ |
| 818 | assert(CSrc->getType() != Type::ULongTy && |
| 819 | "Cannot have type bigger than ulong!"); |
| 820 | unsigned AndValue = (1U << CSrc->getType()->getPrimitiveSize()*8)-1; |
| 821 | Constant *AndOp = ConstantUInt::get(CI.getType(), AndValue); |
| 822 | return BinaryOperator::create(Instruction::And, CSrc->getOperand(0), |
| 823 | AndOp); |
| 824 | } |
| 825 | } |
| 826 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 827 | return 0; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 830 | |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 831 | // PHINode simplification |
| 832 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 833 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 834 | // If the PHI node only has one incoming value, eliminate the PHI node... |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 835 | if (PN.getNumIncomingValues() == 1) |
| 836 | return ReplaceInstUsesWith(PN, PN.getIncomingValue(0)); |
Chris Lattner | 9cd1e66 | 2002-08-20 15:35:35 +0000 | [diff] [blame] | 837 | |
| 838 | // Otherwise if all of the incoming values are the same for the PHI, replace |
| 839 | // the PHI node with the incoming value. |
| 840 | // |
Chris Lattner | f6c0efa | 2002-08-22 20:22:01 +0000 | [diff] [blame] | 841 | Value *InVal = 0; |
| 842 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 843 | if (PN.getIncomingValue(i) != &PN) // Not the PHI node itself... |
| 844 | if (InVal && PN.getIncomingValue(i) != InVal) |
| 845 | return 0; // Not the same, bail out. |
| 846 | else |
| 847 | InVal = PN.getIncomingValue(i); |
| 848 | |
| 849 | // The only case that could cause InVal to be null is if we have a PHI node |
| 850 | // that only has entries for itself. In this case, there is no entry into the |
| 851 | // loop, so kill the PHI. |
| 852 | // |
| 853 | if (InVal == 0) InVal = Constant::getNullValue(PN.getType()); |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 854 | |
Chris Lattner | 9cd1e66 | 2002-08-20 15:35:35 +0000 | [diff] [blame] | 855 | // All of the incoming values are the same, replace the PHI node now. |
| 856 | return ReplaceInstUsesWith(PN, InVal); |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 859 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 860 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 861 | // Is it 'getelementptr %P, uint 0' or 'getelementptr %P' |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 862 | // If so, eliminate the noop. |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 863 | if ((GEP.getNumOperands() == 2 && |
Chris Lattner | 136dab7 | 2002-09-11 01:21:33 +0000 | [diff] [blame] | 864 | GEP.getOperand(1) == Constant::getNullValue(Type::LongTy)) || |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 865 | GEP.getNumOperands() == 1) |
| 866 | return ReplaceInstUsesWith(GEP, GEP.getOperand(0)); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 867 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 868 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 869 | // is a getelementptr instruction, combine the indices of the two |
| 870 | // getelementptr instructions into a single instruction. |
| 871 | // |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 872 | if (GetElementPtrInst *Src = dyn_cast<GetElementPtrInst>(GEP.getOperand(0))) { |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 873 | std::vector<Value *> Indices; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 874 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 875 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 235af56 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 876 | if (Src->getNumOperands() == 2 && isa<Constant>(Src->getOperand(1)) && |
| 877 | isa<Constant>(GEP.getOperand(1))) { |
| 878 | // Replace: gep (gep %P, long C1), long C2, ... |
| 879 | // With: gep %P, long (C1+C2), ... |
| 880 | Value *Sum = *cast<Constant>(Src->getOperand(1)) + |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 881 | *cast<Constant>(GEP.getOperand(1)); |
Chris Lattner | 235af56 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 882 | assert(Sum && "Constant folding of longs failed!?"); |
| 883 | GEP.setOperand(0, Src->getOperand(0)); |
| 884 | GEP.setOperand(1, Sum); |
| 885 | AddUsesToWorkList(*Src); // Reduce use count of Src |
| 886 | return &GEP; |
| 887 | } else if (Src->getNumOperands() == 2 && Src->use_size() == 1) { |
| 888 | // Replace: gep (gep %P, long B), long A, ... |
| 889 | // With: T = long A+B; gep %P, T, ... |
| 890 | // |
| 891 | Value *Sum = BinaryOperator::create(Instruction::Add, Src->getOperand(1), |
| 892 | GEP.getOperand(1), |
| 893 | Src->getName()+".sum", &GEP); |
| 894 | GEP.setOperand(0, Src->getOperand(0)); |
| 895 | GEP.setOperand(1, Sum); |
| 896 | WorkList.push_back(cast<Instruction>(Sum)); |
| 897 | return &GEP; |
Chris Lattner | 5d606a0 | 2002-11-04 16:43:32 +0000 | [diff] [blame] | 898 | } else if (*GEP.idx_begin() == Constant::getNullValue(Type::LongTy) && |
Chris Lattner | a8339e3 | 2002-09-17 21:05:42 +0000 | [diff] [blame] | 899 | Src->getNumOperands() != 1) { |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 900 | // Otherwise we can do the fold if the first index of the GEP is a zero |
| 901 | Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end()); |
| 902 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
Chris Lattner | 5d606a0 | 2002-11-04 16:43:32 +0000 | [diff] [blame] | 903 | } else if (Src->getOperand(Src->getNumOperands()-1) == |
| 904 | Constant::getNullValue(Type::LongTy)) { |
| 905 | // If the src gep ends with a constant array index, merge this get into |
| 906 | // it, even if we have a non-zero array index. |
| 907 | Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end()-1); |
| 908 | Indices.insert(Indices.end(), GEP.idx_begin(), GEP.idx_end()); |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | if (!Indices.empty()) |
| 912 | return new GetElementPtrInst(Src->getOperand(0), Indices, GEP.getName()); |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 913 | |
| 914 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(GEP.getOperand(0))) { |
| 915 | // GEP of global variable. If all of the indices for this GEP are |
| 916 | // constants, we can promote this to a constexpr instead of an instruction. |
| 917 | |
| 918 | // Scan for nonconstants... |
| 919 | std::vector<Constant*> Indices; |
| 920 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
| 921 | for (; I != E && isa<Constant>(*I); ++I) |
| 922 | Indices.push_back(cast<Constant>(*I)); |
| 923 | |
| 924 | if (I == E) { // If they are all constants... |
| 925 | ConstantExpr *CE = |
| 926 | ConstantExpr::getGetElementPtr(ConstantPointerRef::get(GV), Indices); |
| 927 | |
| 928 | // Replace all uses of the GEP with the new constexpr... |
| 929 | return ReplaceInstUsesWith(GEP, CE); |
| 930 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 931 | } |
| 932 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 933 | return 0; |
| 934 | } |
| 935 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 936 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
| 937 | // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1 |
| 938 | if (AI.isArrayAllocation()) // Check C != 1 |
| 939 | if (const ConstantUInt *C = dyn_cast<ConstantUInt>(AI.getArraySize())) { |
| 940 | const Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getValue()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 941 | AllocationInst *New = 0; |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 942 | |
| 943 | // Create and insert the replacement instruction... |
| 944 | if (isa<MallocInst>(AI)) |
| 945 | New = new MallocInst(NewTy, 0, AI.getName(), &AI); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 946 | else { |
| 947 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 948 | New = new AllocaInst(NewTy, 0, AI.getName(), &AI); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 949 | } |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 950 | |
| 951 | // Scan to the end of the allocation instructions, to skip over a block of |
| 952 | // allocas if possible... |
| 953 | // |
| 954 | BasicBlock::iterator It = New; |
| 955 | while (isa<AllocationInst>(*It)) ++It; |
| 956 | |
| 957 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 958 | // insert our getelementptr instruction... |
| 959 | // |
| 960 | std::vector<Value*> Idx(2, Constant::getNullValue(Type::LongTy)); |
| 961 | Value *V = new GetElementPtrInst(New, Idx, New->getName()+".sub", It); |
| 962 | |
| 963 | // Now make everything use the getelementptr instead of the original |
| 964 | // allocation. |
| 965 | ReplaceInstUsesWith(AI, V); |
| 966 | return &AI; |
| 967 | } |
| 968 | return 0; |
| 969 | } |
| 970 | |
| 971 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 972 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 973 | void InstCombiner::removeFromWorkList(Instruction *I) { |
| 974 | WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I), |
| 975 | WorkList.end()); |
| 976 | } |
| 977 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 978 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 979 | bool Changed = false; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 980 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 981 | WorkList.insert(WorkList.end(), inst_begin(F), inst_end(F)); |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 982 | |
| 983 | while (!WorkList.empty()) { |
| 984 | Instruction *I = WorkList.back(); // Get an instruction from the worklist |
| 985 | WorkList.pop_back(); |
| 986 | |
Misha Brukman | 632df28 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 987 | // Check to see if we can DCE or ConstantPropagate the instruction... |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 988 | // Check to see if we can DIE the instruction... |
| 989 | if (isInstructionTriviallyDead(I)) { |
| 990 | // Add operands to the worklist... |
| 991 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 992 | if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i))) |
| 993 | WorkList.push_back(Op); |
| 994 | |
| 995 | ++NumDeadInst; |
| 996 | BasicBlock::iterator BBI = I; |
| 997 | if (dceInstruction(BBI)) { |
| 998 | removeFromWorkList(I); |
| 999 | continue; |
| 1000 | } |
| 1001 | } |
| 1002 | |
Misha Brukman | 632df28 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 1003 | // Instruction isn't dead, see if we can constant propagate it... |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 1004 | if (Constant *C = ConstantFoldInstruction(I)) { |
| 1005 | // Add operands to the worklist... |
| 1006 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 1007 | if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i))) |
| 1008 | WorkList.push_back(Op); |
Chris Lattner | c6509f4 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 1009 | ReplaceInstUsesWith(*I, C); |
| 1010 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 1011 | ++NumConstProp; |
| 1012 | BasicBlock::iterator BBI = I; |
| 1013 | if (dceInstruction(BBI)) { |
| 1014 | removeFromWorkList(I); |
| 1015 | continue; |
| 1016 | } |
| 1017 | } |
| 1018 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 1019 | // Now that we have an instruction, try combining it to simplify it... |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 1020 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 1021 | ++NumCombined; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1022 | // Should we replace the old instruction with a new one? |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 1023 | if (Result != I) { |
| 1024 | // Instructions can end up on the worklist more than once. Make sure |
| 1025 | // we do not process an instruction that has been deleted. |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 1026 | removeFromWorkList(I); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1027 | ReplaceInstWithInst(I, Result); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1028 | } else { |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 1029 | BasicBlock::iterator II = I; |
| 1030 | |
| 1031 | // If the instruction was modified, it's possible that it is now dead. |
| 1032 | // if so, remove it. |
| 1033 | if (dceInstruction(II)) { |
| 1034 | // Instructions may end up in the worklist more than once. Erase them |
| 1035 | // all. |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 1036 | removeFromWorkList(I); |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 1037 | Result = 0; |
| 1038 | } |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 1039 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1040 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 1041 | if (Result) { |
| 1042 | WorkList.push_back(Result); |
| 1043 | AddUsesToWorkList(*Result); |
| 1044 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1045 | Changed = true; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 1046 | } |
| 1047 | } |
| 1048 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1049 | return Changed; |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | Pass *createInstructionCombiningPass() { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1053 | return new InstCombiner(); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 1054 | } |