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