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