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 | 471bd76 | 2003-05-22 19:07:21 +0000 | [diff] [blame] | 20 | #include "llvm/Instructions.h" |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 21 | #include "llvm/Pass.h" |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 22 | #include "llvm/Constants.h" |
| 23 | #include "llvm/ConstantHandling.h" |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 24 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 60a6591 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 25 | #include "llvm/Support/InstIterator.h" |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 26 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CallSite.h" |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 28 | #include "Support/Statistic.h" |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 29 | #include <algorithm> |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 31 | namespace { |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 32 | Statistic<> NumCombined ("instcombine", "Number of insts combined"); |
| 33 | Statistic<> NumConstProp("instcombine", "Number of constant folds"); |
| 34 | Statistic<> NumDeadInst ("instcombine", "Number of dead inst eliminated"); |
| 35 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 36 | class InstCombiner : public FunctionPass, |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 37 | public InstVisitor<InstCombiner, Instruction*> { |
| 38 | // Worklist of all of the instructions that need to be simplified. |
| 39 | std::vector<Instruction*> WorkList; |
| 40 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 41 | void AddUsesToWorkList(Instruction &I) { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 42 | // The instruction was simplified, add all users of the instruction to |
| 43 | // the work lists because they might get more simplified now... |
| 44 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 45 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 46 | UI != UE; ++UI) |
| 47 | WorkList.push_back(cast<Instruction>(*UI)); |
| 48 | } |
| 49 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 50 | // removeFromWorkList - remove all instances of I from the worklist. |
| 51 | void removeFromWorkList(Instruction *I); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 52 | public: |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 53 | virtual bool runOnFunction(Function &F); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 54 | |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 55 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 56 | AU.setPreservesCFG(); |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 59 | // Visitation implementation - Implement instruction combining for different |
| 60 | // instruction types. The semantics are as follows: |
| 61 | // Return Value: |
| 62 | // null - No change was made |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 63 | // 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] | 64 | // otherwise - Change was made, replace I with returned instruction |
| 65 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 66 | Instruction *visitAdd(BinaryOperator &I); |
| 67 | Instruction *visitSub(BinaryOperator &I); |
| 68 | Instruction *visitMul(BinaryOperator &I); |
| 69 | Instruction *visitDiv(BinaryOperator &I); |
| 70 | Instruction *visitRem(BinaryOperator &I); |
| 71 | Instruction *visitAnd(BinaryOperator &I); |
| 72 | Instruction *visitOr (BinaryOperator &I); |
| 73 | Instruction *visitXor(BinaryOperator &I); |
| 74 | Instruction *visitSetCondInst(BinaryOperator &I); |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 75 | Instruction *visitShiftInst(ShiftInst &I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 76 | Instruction *visitCastInst(CastInst &CI); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 77 | Instruction *visitCallInst(CallInst &CI); |
| 78 | Instruction *visitInvokeInst(InvokeInst &II); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 79 | Instruction *visitPHINode(PHINode &PN); |
| 80 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 81 | Instruction *visitAllocationInst(AllocationInst &AI); |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 82 | Instruction *visitBranchInst(BranchInst &BI); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 83 | |
| 84 | // visitInstruction - Specify what to return for unhandled instructions... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 85 | Instruction *visitInstruction(Instruction &I) { return 0; } |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 87 | private: |
| 88 | bool transformConstExprCastCall(CallSite CS); |
| 89 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 90 | // InsertNewInstBefore - insert an instruction New before instruction Old |
| 91 | // in the program. Add the new instruction to the worklist. |
| 92 | // |
| 93 | void InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Chris Lattner | 65217ff | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 94 | assert(New && New->getParent() == 0 && |
| 95 | "New instruction already inserted into a basic block!"); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 96 | BasicBlock *BB = Old.getParent(); |
| 97 | BB->getInstList().insert(&Old, New); // Insert inst |
| 98 | WorkList.push_back(New); // Add to worklist |
| 99 | } |
| 100 | |
| 101 | // ReplaceInstUsesWith - This method is to be used when an instruction is |
| 102 | // found to be dead, replacable with another preexisting expression. Here |
| 103 | // we add all uses of I to the worklist, replace all uses of I with the new |
| 104 | // value, then return I, so that the inst combiner will know that I was |
| 105 | // modified. |
| 106 | // |
| 107 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { |
| 108 | AddUsesToWorkList(I); // Add all modified instrs to worklist |
| 109 | I.replaceAllUsesWith(V); |
| 110 | return &I; |
| 111 | } |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 112 | |
| 113 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 114 | // operators... |
| 115 | bool SimplifyCommutative(BinaryOperator &I); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 116 | }; |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 117 | |
Chris Lattner | c8b7092 | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 118 | RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions"); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 121 | // getComplexity: Assign a complexity or rank value to LLVM Values... |
| 122 | // 0 -> Constant, 1 -> Other, 2 -> Argument, 2 -> Unary, 3 -> OtherInst |
| 123 | static unsigned getComplexity(Value *V) { |
| 124 | if (isa<Instruction>(V)) { |
| 125 | if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V)) |
| 126 | return 2; |
| 127 | return 3; |
| 128 | } |
| 129 | if (isa<Argument>(V)) return 2; |
| 130 | return isa<Constant>(V) ? 0 : 1; |
| 131 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 132 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 133 | // isOnlyUse - Return true if this instruction will be deleted if we stop using |
| 134 | // it. |
| 135 | static bool isOnlyUse(Value *V) { |
| 136 | return V->use_size() == 1 || isa<Constant>(V); |
| 137 | } |
| 138 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 139 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 140 | // operators: |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 141 | // |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 142 | // 1. Order operands such that they are listed from right (least complex) to |
| 143 | // left (most complex). This puts constants before unary operators before |
| 144 | // binary operators. |
| 145 | // |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 146 | // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) |
| 147 | // 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] | 148 | // |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 149 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 150 | bool Changed = false; |
| 151 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) |
| 152 | Changed = !I.swapOperands(); |
| 153 | |
| 154 | if (!I.isAssociative()) return Changed; |
| 155 | Instruction::BinaryOps Opcode = I.getOpcode(); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 156 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) |
| 157 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { |
| 158 | if (isa<Constant>(I.getOperand(1))) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 159 | Constant *Folded = ConstantExpr::get(I.getOpcode(), |
| 160 | cast<Constant>(I.getOperand(1)), |
| 161 | cast<Constant>(Op->getOperand(1))); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 162 | I.setOperand(0, Op->getOperand(0)); |
| 163 | I.setOperand(1, Folded); |
| 164 | return true; |
| 165 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) |
| 166 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && |
| 167 | isOnlyUse(Op) && isOnlyUse(Op1)) { |
| 168 | Constant *C1 = cast<Constant>(Op->getOperand(1)); |
| 169 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); |
| 170 | |
| 171 | // Fold (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2)) |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 172 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 173 | Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0), |
| 174 | Op1->getOperand(0), |
| 175 | Op1->getName(), &I); |
| 176 | WorkList.push_back(New); |
| 177 | I.setOperand(0, New); |
| 178 | I.setOperand(1, Folded); |
| 179 | return true; |
| 180 | } |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 181 | } |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 182 | return Changed; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 183 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 184 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 185 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
| 186 | // if the LHS is a constant zero (which is the 'negate' form). |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 187 | // |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 188 | static inline Value *dyn_castNegVal(Value *V) { |
| 189 | if (BinaryOperator::isNeg(V)) |
| 190 | return BinaryOperator::getNegArgument(cast<BinaryOperator>(V)); |
| 191 | |
Chris Lattner | 9244df6 | 2003-04-30 22:19:10 +0000 | [diff] [blame] | 192 | // Constants can be considered to be negated values if they can be folded... |
| 193 | if (Constant *C = dyn_cast<Constant>(V)) |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 194 | return ConstantExpr::get(Instruction::Sub, |
| 195 | Constant::getNullValue(V->getType()), C); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 196 | return 0; |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 199 | static inline Value *dyn_castNotVal(Value *V) { |
| 200 | if (BinaryOperator::isNot(V)) |
| 201 | return BinaryOperator::getNotArgument(cast<BinaryOperator>(V)); |
| 202 | |
| 203 | // Constants can be considered to be not'ed values... |
Chris Lattner | dd65d86 | 2003-04-30 22:34:06 +0000 | [diff] [blame] | 204 | if (ConstantIntegral *C = dyn_cast<ConstantIntegral>(V)) |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 205 | return ConstantExpr::get(Instruction::Xor, |
| 206 | ConstantIntegral::getAllOnesValue(C->getType()),C); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 210 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
| 211 | // other computations (because it has a constant operand), return the |
| 212 | // non-constant operand of the multiply. |
| 213 | // |
| 214 | static inline Value *dyn_castFoldableMul(Value *V) { |
| 215 | if (V->use_size() == 1 && V->getType()->isInteger()) |
| 216 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 217 | if (I->getOpcode() == Instruction::Mul) |
| 218 | if (isa<Constant>(I->getOperand(1))) |
| 219 | return I->getOperand(0); |
| 220 | return 0; |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 221 | } |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 222 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 223 | // dyn_castMaskingAnd - If this value is an And instruction masking a value with |
| 224 | // a constant, return the constant being anded with. |
| 225 | // |
| 226 | static inline Constant *dyn_castMaskingAnd(Value *V) { |
| 227 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 228 | if (I->getOpcode() == Instruction::And) |
| 229 | return dyn_cast<Constant>(I->getOperand(1)); |
| 230 | |
| 231 | // If this is a constant, it acts just like we were masking with it. |
| 232 | return dyn_cast<Constant>(V); |
| 233 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 234 | |
| 235 | // Log2 - Calculate the log base 2 for the specified value if it is exactly a |
| 236 | // power of 2. |
| 237 | static unsigned Log2(uint64_t Val) { |
| 238 | assert(Val > 1 && "Values 0 and 1 should be handled elsewhere!"); |
| 239 | unsigned Count = 0; |
| 240 | while (Val != 1) { |
| 241 | if (Val & 1) return 0; // Multiple bits set? |
| 242 | Val >>= 1; |
| 243 | ++Count; |
| 244 | } |
| 245 | return Count; |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 248 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 249 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 250 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 251 | |
| 252 | // Eliminate 'add int %X, 0' |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 253 | if (RHS == Constant::getNullValue(I.getType())) |
| 254 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 255 | |
Chris Lattner | 147e975 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 256 | // -A + B --> B - A |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 257 | if (Value *V = dyn_castNegVal(LHS)) |
Chris Lattner | 147e975 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 258 | return BinaryOperator::create(Instruction::Sub, RHS, V); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 259 | |
| 260 | // A + -B --> A - B |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 261 | if (!isa<Constant>(RHS)) |
| 262 | if (Value *V = dyn_castNegVal(RHS)) |
| 263 | return BinaryOperator::create(Instruction::Sub, LHS, V); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 264 | |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 265 | // X*C + X --> X * (C+1) |
| 266 | if (dyn_castFoldableMul(LHS) == RHS) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 267 | Constant *CP1 = |
| 268 | ConstantExpr::get(Instruction::Add, |
| 269 | cast<Constant>(cast<Instruction>(LHS)->getOperand(1)), |
| 270 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 271 | return BinaryOperator::create(Instruction::Mul, RHS, CP1); |
| 272 | } |
| 273 | |
| 274 | // X + X*C --> X * (C+1) |
| 275 | if (dyn_castFoldableMul(RHS) == LHS) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 276 | Constant *CP1 = |
| 277 | ConstantExpr::get(Instruction::Add, |
| 278 | cast<Constant>(cast<Instruction>(RHS)->getOperand(1)), |
| 279 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 280 | return BinaryOperator::create(Instruction::Mul, LHS, CP1); |
| 281 | } |
| 282 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 283 | // (A & C1)+(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 |
| 284 | if (Constant *C1 = dyn_castMaskingAnd(LHS)) |
| 285 | if (Constant *C2 = dyn_castMaskingAnd(RHS)) |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 286 | if (ConstantExpr::get(Instruction::And, C1, C2)->isNullValue()) |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 287 | return BinaryOperator::create(Instruction::Or, LHS, RHS); |
| 288 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 289 | return Changed ? &I : 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 292 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 293 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 294 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 295 | if (Op0 == Op1) // sub X, X -> 0 |
| 296 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 297 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 298 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 299 | if (Value *V = dyn_castNegVal(Op1)) |
Chris Lattner | 147e975 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 300 | return BinaryOperator::create(Instruction::Add, Op0, V); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 301 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 302 | // Replace (-1 - A) with (~A)... |
| 303 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) |
| 304 | if (C->isAllOnesValue()) |
| 305 | return BinaryOperator::createNot(Op1); |
Chris Lattner | ad3c495 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 306 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 307 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) |
| 308 | if (Op1I->use_size() == 1) { |
| 309 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 310 | // is not used by anyone else... |
| 311 | // |
| 312 | if (Op1I->getOpcode() == Instruction::Sub) { |
| 313 | // Swap the two operands of the subexpr... |
| 314 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 315 | Op1I->setOperand(0, IIOp1); |
| 316 | Op1I->setOperand(1, IIOp0); |
| 317 | |
| 318 | // Create the new top level add instruction... |
| 319 | return BinaryOperator::create(Instruction::Add, Op0, Op1); |
| 320 | } |
| 321 | |
| 322 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 323 | // |
| 324 | if (Op1I->getOpcode() == Instruction::And && |
| 325 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 326 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 327 | |
| 328 | Instruction *NewNot = BinaryOperator::createNot(OtherOp, "B.not", &I); |
| 329 | return BinaryOperator::create(Instruction::And, Op0, NewNot); |
| 330 | } |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 331 | |
| 332 | // X - X*C --> X * (1-C) |
| 333 | if (dyn_castFoldableMul(Op1I) == Op0) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 334 | Constant *CP1 = |
| 335 | ConstantExpr::get(Instruction::Sub, |
| 336 | ConstantInt::get(I.getType(), 1), |
| 337 | cast<Constant>(cast<Instruction>(Op1)->getOperand(1))); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 338 | assert(CP1 && "Couldn't constant fold 1-C?"); |
| 339 | return BinaryOperator::create(Instruction::Mul, Op0, CP1); |
| 340 | } |
Chris Lattner | ad3c495 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 341 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 342 | |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 343 | // X*C - X --> X * (C-1) |
| 344 | if (dyn_castFoldableMul(Op0) == Op1) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 345 | Constant *CP1 = |
| 346 | ConstantExpr::get(Instruction::Sub, |
| 347 | cast<Constant>(cast<Instruction>(Op0)->getOperand(1)), |
| 348 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 349 | assert(CP1 && "Couldn't constant fold C - 1?"); |
| 350 | return BinaryOperator::create(Instruction::Mul, Op1, CP1); |
| 351 | } |
| 352 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 353 | return 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 356 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 357 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 358 | Value *Op0 = I.getOperand(0); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 359 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 360 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 361 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 362 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
| 363 | const Type *Ty = CI->getType(); |
| 364 | uint64_t Val = Ty->isSigned() ? |
| 365 | (uint64_t)cast<ConstantSInt>(CI)->getValue() : |
| 366 | cast<ConstantUInt>(CI)->getValue(); |
| 367 | switch (Val) { |
| 368 | case 0: |
| 369 | return ReplaceInstUsesWith(I, Op1); // Eliminate 'mul double %X, 0' |
| 370 | case 1: |
| 371 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul int %X, 1' |
| 372 | case 2: // Convert 'mul int %X, 2' to 'add int %X, %X' |
| 373 | return BinaryOperator::create(Instruction::Add, Op0, Op0, I.getName()); |
| 374 | } |
Chris Lattner | 31ba129 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 375 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 376 | if (uint64_t C = Log2(Val)) // Replace X*(2^C) with X << C |
| 377 | return new ShiftInst(Instruction::Shl, Op0, |
| 378 | ConstantUInt::get(Type::UByteTy, C)); |
| 379 | } else { |
| 380 | ConstantFP *Op1F = cast<ConstantFP>(Op1); |
| 381 | if (Op1F->isNullValue()) |
| 382 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 31ba129 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 383 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 384 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 385 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) |
| 386 | if (Op1F->getValue() == 1.0) |
| 387 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
| 388 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Chris Lattner | 934a64cf | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 391 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
| 392 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) |
| 393 | return BinaryOperator::create(Instruction::Mul, Op0v, Op1v); |
| 394 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 395 | return Changed ? &I : 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 398 | Instruction *InstCombiner::visitDiv(BinaryOperator &I) { |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 399 | // div X, 1 == X |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 400 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) { |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 401 | if (RHS->equalsInt(1)) |
| 402 | return ReplaceInstUsesWith(I, I.getOperand(0)); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 403 | |
| 404 | // Check to see if this is an unsigned division with an exact power of 2, |
| 405 | // if so, convert to a right shift. |
| 406 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS)) |
| 407 | if (uint64_t Val = C->getValue()) // Don't break X / 0 |
| 408 | if (uint64_t C = Log2(Val)) |
| 409 | return new ShiftInst(Instruction::Shr, I.getOperand(0), |
| 410 | ConstantUInt::get(Type::UByteTy, C)); |
| 411 | } |
| 412 | |
| 413 | // 0 / X == 0, we don't need to preserve faults! |
| 414 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(I.getOperand(0))) |
| 415 | if (LHS->equalsInt(0)) |
| 416 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 417 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 422 | Instruction *InstCombiner::visitRem(BinaryOperator &I) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 423 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) { |
| 424 | if (RHS->equalsInt(1)) // X % 1 == 0 |
| 425 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 426 | |
| 427 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 428 | // if so, convert to a bitwise and. |
| 429 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS)) |
| 430 | if (uint64_t Val = C->getValue()) // Don't break X % 0 (divide by zero) |
| 431 | if (Log2(Val)) |
| 432 | return BinaryOperator::create(Instruction::And, I.getOperand(0), |
| 433 | ConstantUInt::get(I.getType(), Val-1)); |
| 434 | } |
| 435 | |
| 436 | // 0 % X == 0, we don't need to preserve faults! |
| 437 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(I.getOperand(0))) |
| 438 | if (LHS->equalsInt(0)) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 439 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 440 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 441 | return 0; |
| 442 | } |
| 443 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 444 | // isMaxValueMinusOne - return true if this is Max-1 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 445 | static bool isMaxValueMinusOne(const ConstantInt *C) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 446 | if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) { |
| 447 | // Calculate -1 casted to the right type... |
| 448 | unsigned TypeBits = C->getType()->getPrimitiveSize()*8; |
| 449 | uint64_t Val = ~0ULL; // All ones |
| 450 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 451 | return CU->getValue() == Val-1; |
| 452 | } |
| 453 | |
| 454 | const ConstantSInt *CS = cast<ConstantSInt>(C); |
| 455 | |
| 456 | // Calculate 0111111111..11111 |
| 457 | unsigned TypeBits = C->getType()->getPrimitiveSize()*8; |
| 458 | int64_t Val = INT64_MAX; // All ones |
| 459 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 460 | return CS->getValue() == Val-1; |
| 461 | } |
| 462 | |
| 463 | // isMinValuePlusOne - return true if this is Min+1 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 464 | static bool isMinValuePlusOne(const ConstantInt *C) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 465 | if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) |
| 466 | return CU->getValue() == 1; |
| 467 | |
| 468 | const ConstantSInt *CS = cast<ConstantSInt>(C); |
| 469 | |
| 470 | // Calculate 1111111111000000000000 |
| 471 | unsigned TypeBits = C->getType()->getPrimitiveSize()*8; |
| 472 | int64_t Val = -1; // All ones |
| 473 | Val <<= TypeBits-1; // Shift over to the right spot |
| 474 | return CS->getValue() == Val+1; |
| 475 | } |
| 476 | |
| 477 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 478 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 479 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 480 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 481 | |
| 482 | // and X, X = X and X, 0 == 0 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 483 | if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) |
| 484 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 485 | |
| 486 | // and X, -1 == X |
Chris Lattner | 8328263 | 2002-08-13 17:50:24 +0000 | [diff] [blame] | 487 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 488 | if (RHS->isAllOnesValue()) |
| 489 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 490 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 491 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 492 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 493 | |
| 494 | // (~A & ~B) == (~(A | B)) - Demorgan's Law |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 495 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 496 | Instruction *Or = BinaryOperator::create(Instruction::Or, Op0NotVal, |
| 497 | Op1NotVal,I.getName()+".demorgan", |
| 498 | &I); |
Chris Lattner | 3e327a4 | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 499 | WorkList.push_back(Or); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 500 | return BinaryOperator::createNot(Or); |
| 501 | } |
| 502 | |
| 503 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
| 504 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 65217ff | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 505 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 506 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | |
| 510 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 511 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 512 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 513 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 514 | |
| 515 | // or X, X = X or X, 0 == X |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 516 | if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) |
| 517 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 518 | |
| 519 | // or X, -1 == -1 |
Chris Lattner | 8328263 | 2002-08-13 17:50:24 +0000 | [diff] [blame] | 520 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 521 | if (RHS->isAllOnesValue()) |
| 522 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 523 | |
Chris Lattner | 3e327a4 | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 524 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 525 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 526 | |
Chris Lattner | 3e327a4 | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 527 | if (Op1 == Op0NotVal) // ~A | A == -1 |
| 528 | return ReplaceInstUsesWith(I, |
| 529 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 530 | |
| 531 | if (Op0 == Op1NotVal) // A | ~A == -1 |
| 532 | return ReplaceInstUsesWith(I, |
| 533 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 534 | |
| 535 | // (~A | ~B) == (~(A & B)) - Demorgan's Law |
| 536 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
| 537 | Instruction *And = BinaryOperator::create(Instruction::And, Op0NotVal, |
| 538 | Op1NotVal,I.getName()+".demorgan", |
| 539 | &I); |
| 540 | WorkList.push_back(And); |
| 541 | return BinaryOperator::createNot(And); |
| 542 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 543 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 544 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | |
| 548 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 549 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 550 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 551 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 552 | |
| 553 | // xor X, X = 0 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 554 | if (Op0 == Op1) |
| 555 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 556 | |
Chris Lattner | 8328263 | 2002-08-13 17:50:24 +0000 | [diff] [blame] | 557 | if (ConstantIntegral *Op1C = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 558 | // xor X, 0 == X |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 559 | if (Op1C->isNullValue()) |
| 560 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 561 | |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 562 | // Is this a "NOT" instruction? |
| 563 | if (Op1C->isAllOnesValue()) { |
| 564 | // xor (xor X, -1), -1 = not (not X) = X |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 565 | if (Value *X = dyn_castNotVal(Op0)) |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 566 | return ReplaceInstUsesWith(I, X); |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 567 | |
| 568 | // xor (setcc A, B), true = not (setcc A, B) = setncc A, B |
| 569 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(Op0)) |
| 570 | if (SCI->use_size() == 1) |
| 571 | return new SetCondInst(SCI->getInverseCondition(), |
| 572 | SCI->getOperand(0), SCI->getOperand(1)); |
| 573 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 576 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 577 | if (X == Op1) |
| 578 | return ReplaceInstUsesWith(I, |
| 579 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 580 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 581 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 582 | if (X == Op0) |
| 583 | return ReplaceInstUsesWith(I, |
| 584 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 585 | |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 586 | if (Instruction *Op1I = dyn_cast<Instruction>(Op1)) |
| 587 | if (Op1I->getOpcode() == Instruction::Or) |
| 588 | if (Op1I->getOperand(0) == Op0) { // B^(B|A) == (A|B)^B |
| 589 | cast<BinaryOperator>(Op1I)->swapOperands(); |
| 590 | I.swapOperands(); |
| 591 | std::swap(Op0, Op1); |
| 592 | } else if (Op1I->getOperand(1) == Op0) { // B^(A|B) == (A|B)^B |
| 593 | I.swapOperands(); |
| 594 | std::swap(Op0, Op1); |
| 595 | } |
| 596 | |
| 597 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) |
| 598 | if (Op0I->getOpcode() == Instruction::Or && Op0I->use_size() == 1) { |
| 599 | if (Op0I->getOperand(0) == Op1) // (B|A)^B == (A|B)^B |
| 600 | cast<BinaryOperator>(Op0I)->swapOperands(); |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 601 | if (Op0I->getOperand(1) == Op1) { // (A|B)^B == A & ~B |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 602 | Value *NotB = BinaryOperator::createNot(Op1, Op1->getName()+".not", &I); |
| 603 | WorkList.push_back(cast<Instruction>(NotB)); |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 604 | return BinaryOperator::create(Instruction::And, Op0I->getOperand(0), |
| 605 | NotB); |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 609 | // (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1^C2 == 0 |
| 610 | if (Constant *C1 = dyn_castMaskingAnd(Op0)) |
| 611 | if (Constant *C2 = dyn_castMaskingAnd(Op1)) |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 612 | if (ConstantExpr::get(Instruction::And, C1, C2)->isNullValue()) |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 613 | return BinaryOperator::create(Instruction::Or, Op0, Op1); |
| 614 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 615 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 618 | // AddOne, SubOne - Add or subtract a constant one from an integer constant... |
| 619 | static Constant *AddOne(ConstantInt *C) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 620 | Constant *Result = ConstantExpr::get(Instruction::Add, C, |
| 621 | ConstantInt::get(C->getType(), 1)); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 622 | assert(Result && "Constant folding integer addition failed!"); |
| 623 | return Result; |
| 624 | } |
| 625 | static Constant *SubOne(ConstantInt *C) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 626 | Constant *Result = ConstantExpr::get(Instruction::Sub, C, |
| 627 | ConstantInt::get(C->getType(), 1)); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 628 | assert(Result && "Constant folding integer addition failed!"); |
| 629 | return Result; |
| 630 | } |
| 631 | |
Chris Lattner | 1fc23f3 | 2002-05-09 20:11:54 +0000 | [diff] [blame] | 632 | // isTrueWhenEqual - Return true if the specified setcondinst instruction is |
| 633 | // true when both operands are equal... |
| 634 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 635 | static bool isTrueWhenEqual(Instruction &I) { |
| 636 | return I.getOpcode() == Instruction::SetEQ || |
| 637 | I.getOpcode() == Instruction::SetGE || |
| 638 | I.getOpcode() == Instruction::SetLE; |
Chris Lattner | 1fc23f3 | 2002-05-09 20:11:54 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 641 | Instruction *InstCombiner::visitSetCondInst(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 642 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 643 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 644 | const Type *Ty = Op0->getType(); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 645 | |
| 646 | // setcc X, X |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 647 | if (Op0 == Op1) |
| 648 | return ReplaceInstUsesWith(I, ConstantBool::get(isTrueWhenEqual(I))); |
Chris Lattner | 1fc23f3 | 2002-05-09 20:11:54 +0000 | [diff] [blame] | 649 | |
| 650 | // setcc <global*>, 0 - Global value addresses are never null! |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 651 | if (isa<GlobalValue>(Op0) && isa<ConstantPointerNull>(Op1)) |
| 652 | return ReplaceInstUsesWith(I, ConstantBool::get(!isTrueWhenEqual(I))); |
| 653 | |
| 654 | // setcc's with boolean values can always be turned into bitwise operations |
| 655 | if (Ty == Type::BoolTy) { |
| 656 | // If this is <, >, or !=, we can change this into a simple xor instruction |
| 657 | if (!isTrueWhenEqual(I)) |
| 658 | return BinaryOperator::create(Instruction::Xor, Op0, Op1, I.getName()); |
| 659 | |
| 660 | // Otherwise we need to make a temporary intermediate instruction and insert |
| 661 | // it into the instruction stream. This is what we are after: |
| 662 | // |
| 663 | // seteq bool %A, %B -> ~(A^B) |
| 664 | // setle bool %A, %B -> ~A | B |
| 665 | // setge bool %A, %B -> A | ~B |
| 666 | // |
| 667 | if (I.getOpcode() == Instruction::SetEQ) { // seteq case |
| 668 | Instruction *Xor = BinaryOperator::create(Instruction::Xor, Op0, Op1, |
| 669 | I.getName()+"tmp"); |
| 670 | InsertNewInstBefore(Xor, I); |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 671 | return BinaryOperator::createNot(Xor, I.getName()); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | // Handle the setXe cases... |
| 675 | assert(I.getOpcode() == Instruction::SetGE || |
| 676 | I.getOpcode() == Instruction::SetLE); |
| 677 | |
| 678 | if (I.getOpcode() == Instruction::SetGE) |
| 679 | std::swap(Op0, Op1); // Change setge -> setle |
| 680 | |
| 681 | // Now we just have the SetLE case. |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 682 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 683 | InsertNewInstBefore(Not, I); |
| 684 | return BinaryOperator::create(Instruction::Or, Not, Op1, I.getName()); |
| 685 | } |
| 686 | |
| 687 | // Check to see if we are doing one of many comparisons against constant |
| 688 | // integers at the end of their ranges... |
| 689 | // |
| 690 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 691 | if (CI->isNullValue()) { |
| 692 | if (I.getOpcode() == Instruction::SetNE) |
| 693 | return new CastInst(Op0, Type::BoolTy, I.getName()); |
| 694 | else if (I.getOpcode() == Instruction::SetEQ) { |
| 695 | // seteq X, 0 -> not (cast X to bool) |
| 696 | Instruction *Val = new CastInst(Op0, Type::BoolTy, I.getName()+".not"); |
| 697 | InsertNewInstBefore(Val, I); |
| 698 | return BinaryOperator::createNot(Val, I.getName()); |
| 699 | } |
| 700 | } |
Chris Lattner | 791ac1a | 2003-06-01 03:35:25 +0000 | [diff] [blame] | 701 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 702 | // 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] | 703 | if (CI->isMinValue()) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 704 | if (I.getOpcode() == Instruction::SetLT) // A < MIN -> FALSE |
| 705 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 706 | if (I.getOpcode() == Instruction::SetGE) // A >= MIN -> TRUE |
| 707 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 708 | if (I.getOpcode() == Instruction::SetLE) // A <= MIN -> A == MIN |
| 709 | return BinaryOperator::create(Instruction::SetEQ, Op0,Op1, I.getName()); |
| 710 | if (I.getOpcode() == Instruction::SetGT) // A > MIN -> A != MIN |
| 711 | return BinaryOperator::create(Instruction::SetNE, Op0,Op1, I.getName()); |
| 712 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 713 | } else if (CI->isMaxValue()) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 714 | if (I.getOpcode() == Instruction::SetGT) // A > MAX -> FALSE |
| 715 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 716 | if (I.getOpcode() == Instruction::SetLE) // A <= MAX -> TRUE |
| 717 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 718 | if (I.getOpcode() == Instruction::SetGE) // A >= MAX -> A == MAX |
| 719 | return BinaryOperator::create(Instruction::SetEQ, Op0,Op1, I.getName()); |
| 720 | if (I.getOpcode() == Instruction::SetLT) // A < MAX -> A != MAX |
| 721 | return BinaryOperator::create(Instruction::SetNE, Op0,Op1, I.getName()); |
| 722 | |
| 723 | // Comparing against a value really close to min or max? |
| 724 | } else if (isMinValuePlusOne(CI)) { |
| 725 | if (I.getOpcode() == Instruction::SetLT) // A < MIN+1 -> A == MIN |
| 726 | return BinaryOperator::create(Instruction::SetEQ, Op0, |
| 727 | SubOne(CI), I.getName()); |
| 728 | if (I.getOpcode() == Instruction::SetGE) // A >= MIN-1 -> A != MIN |
| 729 | return BinaryOperator::create(Instruction::SetNE, Op0, |
| 730 | SubOne(CI), I.getName()); |
| 731 | |
| 732 | } else if (isMaxValueMinusOne(CI)) { |
| 733 | if (I.getOpcode() == Instruction::SetGT) // A > MAX-1 -> A == MAX |
| 734 | return BinaryOperator::create(Instruction::SetEQ, Op0, |
| 735 | AddOne(CI), I.getName()); |
| 736 | if (I.getOpcode() == Instruction::SetLE) // A <= MAX-1 -> A != MAX |
| 737 | return BinaryOperator::create(Instruction::SetNE, Op0, |
| 738 | AddOne(CI), I.getName()); |
| 739 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 742 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | |
| 746 | |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 747 | Instruction *InstCombiner::visitShiftInst(ShiftInst &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 748 | assert(I.getOperand(1)->getType() == Type::UByteTy); |
| 749 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 750 | |
| 751 | // shl X, 0 == X and shr X, 0 == X |
| 752 | // shl 0, X == 0 and shr 0, X == 0 |
| 753 | if (Op1 == Constant::getNullValue(Type::UByteTy) || |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 754 | Op0 == Constant::getNullValue(Op0->getType())) |
| 755 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 756 | |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 757 | // If this is a shift of a shift, see if we can fold the two together... |
| 758 | if (ShiftInst *Op0SI = dyn_cast<ShiftInst>(Op0)) { |
| 759 | if (isa<Constant>(Op1) && isa<Constant>(Op0SI->getOperand(1))) { |
| 760 | ConstantUInt *ShiftAmt1C = cast<ConstantUInt>(Op0SI->getOperand(1)); |
| 761 | unsigned ShiftAmt1 = ShiftAmt1C->getValue(); |
| 762 | unsigned ShiftAmt2 = cast<ConstantUInt>(Op1)->getValue(); |
| 763 | |
| 764 | // Check for (A << c1) << c2 and (A >> c1) >> c2 |
| 765 | if (I.getOpcode() == Op0SI->getOpcode()) { |
| 766 | unsigned Amt = ShiftAmt1+ShiftAmt2; // Fold into one big shift... |
| 767 | return new ShiftInst(I.getOpcode(), Op0SI->getOperand(0), |
| 768 | ConstantUInt::get(Type::UByteTy, Amt)); |
| 769 | } |
| 770 | |
| 771 | if (I.getType()->isUnsigned()) { // Check for (A << c1) >> c2 or visaversa |
| 772 | // Calculate bitmask for what gets shifted off the edge... |
| 773 | Constant *C = ConstantIntegral::getAllOnesValue(I.getType()); |
| 774 | if (I.getOpcode() == Instruction::Shr) |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 775 | C = ConstantExpr::getShift(Instruction::Shr, C, ShiftAmt1C); |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 776 | else |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 777 | C = ConstantExpr::getShift(Instruction::Shl, C, ShiftAmt1C); |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 778 | |
| 779 | Instruction *Mask = |
| 780 | BinaryOperator::create(Instruction::And, Op0SI->getOperand(0), |
| 781 | C, Op0SI->getOperand(0)->getName()+".mask",&I); |
| 782 | WorkList.push_back(Mask); |
| 783 | |
| 784 | // Figure out what flavor of shift we should use... |
| 785 | if (ShiftAmt1 == ShiftAmt2) |
| 786 | return ReplaceInstUsesWith(I, Mask); // (A << c) >> c === A & c2 |
| 787 | else if (ShiftAmt1 < ShiftAmt2) { |
| 788 | return new ShiftInst(I.getOpcode(), Mask, |
| 789 | ConstantUInt::get(Type::UByteTy, ShiftAmt2-ShiftAmt1)); |
| 790 | } else { |
| 791 | return new ShiftInst(Op0SI->getOpcode(), Mask, |
| 792 | ConstantUInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2)); |
| 793 | } |
| 794 | } |
| 795 | } |
| 796 | } |
| 797 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 798 | // 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] | 799 | // a signed value. |
| 800 | // |
| 801 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) { |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 802 | unsigned TypeBits = Op0->getType()->getPrimitiveSize()*8; |
| 803 | if (CUI->getValue() >= TypeBits && |
| 804 | (!Op0->getType()->isSigned() || I.getOpcode() == Instruction::Shl)) |
| 805 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
Chris Lattner | 55f3d94 | 2002-09-10 23:04:09 +0000 | [diff] [blame] | 806 | |
| 807 | // Check to see if we are shifting left by 1. If so, turn it into an add |
| 808 | // instruction. |
| 809 | if (I.getOpcode() == Instruction::Shl && CUI->equalsInt(1)) |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 810 | // Convert 'shl int %X, 1' to 'add int %X, %X' |
Chris Lattner | 55f3d94 | 2002-09-10 23:04:09 +0000 | [diff] [blame] | 811 | return BinaryOperator::create(Instruction::Add, Op0, Op0, I.getName()); |
| 812 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 813 | } |
Chris Lattner | 2e0fb39 | 2002-10-08 16:16:40 +0000 | [diff] [blame] | 814 | |
| 815 | // shr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 816 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0)) |
| 817 | if (I.getOpcode() == Instruction::Shr && CSI->isAllOnesValue()) |
| 818 | return ReplaceInstUsesWith(I, CSI); |
| 819 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 824 | // isEliminableCastOfCast - Return true if it is valid to eliminate the CI |
| 825 | // instruction. |
| 826 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 827 | static inline bool isEliminableCastOfCast(const CastInst &CI, |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 828 | const CastInst *CSrc) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 829 | assert(CI.getOperand(0) == CSrc); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 830 | const Type *SrcTy = CSrc->getOperand(0)->getType(); |
| 831 | const Type *MidTy = CSrc->getType(); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 832 | const Type *DstTy = CI.getType(); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 833 | |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 834 | // It is legal to eliminate the instruction if casting A->B->A if the sizes |
| 835 | // are identical and the bits don't get reinterpreted (for example |
Chris Lattner | 0bb7591 | 2002-08-14 23:21:10 +0000 | [diff] [blame] | 836 | // int->float->int would not be allowed) |
Misha Brukman | e5838c4 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 837 | if (SrcTy == DstTy && SrcTy->isLosslesslyConvertibleTo(MidTy)) |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 838 | return true; |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 839 | |
| 840 | // Allow free casting and conversion of sizes as long as the sign doesn't |
| 841 | // change... |
Chris Lattner | b0b412e | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 842 | if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral()) { |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 843 | unsigned SrcSize = SrcTy->getPrimitiveSize(); |
| 844 | unsigned MidSize = MidTy->getPrimitiveSize(); |
| 845 | unsigned DstSize = DstTy->getPrimitiveSize(); |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 846 | |
Chris Lattner | 3732aca | 2002-08-15 16:15:25 +0000 | [diff] [blame] | 847 | // Cases where we are monotonically decreasing the size of the type are |
| 848 | // always ok, regardless of what sign changes are going on. |
| 849 | // |
Chris Lattner | 0bb7591 | 2002-08-14 23:21:10 +0000 | [diff] [blame] | 850 | if (SrcSize >= MidSize && MidSize >= DstSize) |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 851 | return true; |
Chris Lattner | 3732aca | 2002-08-15 16:15:25 +0000 | [diff] [blame] | 852 | |
Chris Lattner | 555518c | 2002-09-23 23:39:43 +0000 | [diff] [blame] | 853 | // Cases where the source and destination type are the same, but the middle |
| 854 | // type is bigger are noops. |
| 855 | // |
| 856 | if (SrcSize == DstSize && MidSize > SrcSize) |
| 857 | return true; |
| 858 | |
Chris Lattner | 3732aca | 2002-08-15 16:15:25 +0000 | [diff] [blame] | 859 | // If we are monotonically growing, things are more complex. |
| 860 | // |
| 861 | if (SrcSize <= MidSize && MidSize <= DstSize) { |
| 862 | // We have eight combinations of signedness to worry about. Here's the |
| 863 | // table: |
| 864 | static const int SignTable[8] = { |
| 865 | // CODE, SrcSigned, MidSigned, DstSigned, Comment |
| 866 | 1, // U U U Always ok |
| 867 | 1, // U U S Always ok |
| 868 | 3, // U S U Ok iff SrcSize != MidSize |
| 869 | 3, // U S S Ok iff SrcSize != MidSize |
| 870 | 0, // S U U Never ok |
| 871 | 2, // S U S Ok iff MidSize == DstSize |
| 872 | 1, // S S U Always ok |
| 873 | 1, // S S S Always ok |
| 874 | }; |
| 875 | |
| 876 | // Choose an action based on the current entry of the signtable that this |
| 877 | // cast of cast refers to... |
| 878 | unsigned Row = SrcTy->isSigned()*4+MidTy->isSigned()*2+DstTy->isSigned(); |
| 879 | switch (SignTable[Row]) { |
| 880 | case 0: return false; // Never ok |
| 881 | case 1: return true; // Always ok |
| 882 | case 2: return MidSize == DstSize; // Ok iff MidSize == DstSize |
| 883 | case 3: // Ok iff SrcSize != MidSize |
| 884 | return SrcSize != MidSize || SrcTy == Type::BoolTy; |
| 885 | default: assert(0 && "Bad entry in sign table!"); |
| 886 | } |
Chris Lattner | 3732aca | 2002-08-15 16:15:25 +0000 | [diff] [blame] | 887 | } |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 888 | } |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 889 | |
| 890 | // Otherwise, we cannot succeed. Specifically we do not want to allow things |
| 891 | // like: short -> ushort -> uint, because this can create wrong results if |
| 892 | // the input short is negative! |
| 893 | // |
| 894 | return false; |
| 895 | } |
| 896 | |
| 897 | |
| 898 | // CastInst simplification |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 899 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 900 | Instruction *InstCombiner::visitCastInst(CastInst &CI) { |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame^] | 901 | Value *Src = CI.getOperand(0); |
| 902 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 903 | // If the user is casting a value to the same type, eliminate this cast |
| 904 | // instruction... |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame^] | 905 | if (CI.getType() == Src->getType()) |
| 906 | return ReplaceInstUsesWith(CI, Src); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 907 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 908 | // If casting the result of another cast instruction, try to eliminate this |
| 909 | // one! |
| 910 | // |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame^] | 911 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 912 | if (isEliminableCastOfCast(CI, CSrc)) { |
| 913 | // This instruction now refers directly to the cast's src operand. This |
| 914 | // has a good chance of making CSrc dead. |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 915 | CI.setOperand(0, CSrc->getOperand(0)); |
| 916 | return &CI; |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 919 | // If this is an A->B->A cast, and we are dealing with integral types, try |
| 920 | // to convert this into a logical 'and' instruction. |
| 921 | // |
| 922 | if (CSrc->getOperand(0)->getType() == CI.getType() && |
Chris Lattner | b0b412e | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 923 | CI.getType()->isInteger() && CSrc->getType()->isInteger() && |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 924 | CI.getType()->isUnsigned() && CSrc->getType()->isUnsigned() && |
| 925 | CSrc->getType()->getPrimitiveSize() < CI.getType()->getPrimitiveSize()){ |
| 926 | assert(CSrc->getType() != Type::ULongTy && |
| 927 | "Cannot have type bigger than ulong!"); |
Chris Lattner | 196897c | 2003-05-26 23:41:32 +0000 | [diff] [blame] | 928 | uint64_t AndValue = (1ULL << CSrc->getType()->getPrimitiveSize()*8)-1; |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 929 | Constant *AndOp = ConstantUInt::get(CI.getType(), AndValue); |
| 930 | return BinaryOperator::create(Instruction::And, CSrc->getOperand(0), |
| 931 | AndOp); |
| 932 | } |
| 933 | } |
| 934 | |
Chris Lattner | d0d5160 | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 935 | // If casting the result of a getelementptr instruction with no offset, turn |
| 936 | // this into a cast of the original pointer! |
| 937 | // |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame^] | 938 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
Chris Lattner | d0d5160 | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 939 | bool AllZeroOperands = true; |
| 940 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) |
| 941 | if (!isa<Constant>(GEP->getOperand(i)) || |
| 942 | !cast<Constant>(GEP->getOperand(i))->isNullValue()) { |
| 943 | AllZeroOperands = false; |
| 944 | break; |
| 945 | } |
| 946 | if (AllZeroOperands) { |
| 947 | CI.setOperand(0, GEP->getOperand(0)); |
| 948 | return &CI; |
| 949 | } |
| 950 | } |
| 951 | |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame^] | 952 | // If this is a cast to bool (which is effectively a "!=0" test), then we can |
| 953 | // perform a few optimizations... |
| 954 | // |
| 955 | if (CI.getType() == Type::BoolTy) { |
| 956 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Src)) { |
| 957 | Value *Op0 = BO->getOperand(0), *Op1 = BO->getOperand(1); |
| 958 | |
| 959 | // Replace (cast (sub A, B) to bool) with (setne A, B) |
| 960 | if (BO->getOpcode() == Instruction::Sub) |
| 961 | return new SetCondInst(Instruction::SetNE, Op0, Op1); |
| 962 | |
| 963 | // Replace (cast (add A, B) to bool) with (setne A, -B) if B is |
| 964 | // efficiently invertible, or if the add has just this one use. |
| 965 | if (BO->getOpcode() == Instruction::Add) |
| 966 | if (Value *NegVal = dyn_castNegVal(Op1)) |
| 967 | return new SetCondInst(Instruction::SetNE, Op0, NegVal); |
| 968 | else if (Value *NegVal = dyn_castNegVal(Op0)) |
| 969 | return new SetCondInst(Instruction::SetNE, NegVal, Op1); |
| 970 | else if (BO->use_size() == 1) { |
| 971 | Instruction *Neg = BinaryOperator::createNeg(Op1, BO->getName()); |
| 972 | BO->setName(""); |
| 973 | InsertNewInstBefore(Neg, CI); |
| 974 | return new SetCondInst(Instruction::SetNE, Op0, Neg); |
| 975 | } |
| 976 | } |
| 977 | } |
| 978 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 979 | return 0; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 982 | // CallInst simplification |
| 983 | // |
| 984 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
| 985 | if (transformConstExprCastCall(&CI)) return 0; |
| 986 | return 0; |
| 987 | } |
| 988 | |
| 989 | // InvokeInst simplification |
| 990 | // |
| 991 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
| 992 | if (transformConstExprCastCall(&II)) return 0; |
| 993 | return 0; |
| 994 | } |
| 995 | |
| 996 | // getPromotedType - Return the specified type promoted as it would be to pass |
| 997 | // though a va_arg area... |
| 998 | static const Type *getPromotedType(const Type *Ty) { |
| 999 | switch (Ty->getPrimitiveID()) { |
| 1000 | case Type::SByteTyID: |
| 1001 | case Type::ShortTyID: return Type::IntTy; |
| 1002 | case Type::UByteTyID: |
| 1003 | case Type::UShortTyID: return Type::UIntTy; |
| 1004 | case Type::FloatTyID: return Type::DoubleTy; |
| 1005 | default: return Ty; |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 1010 | // attempt to move the cast to the arguments of the call/invoke. |
| 1011 | // |
| 1012 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 1013 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 1014 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
| 1015 | if (CE->getOpcode() != Instruction::Cast || |
| 1016 | !isa<ConstantPointerRef>(CE->getOperand(0))) |
| 1017 | return false; |
| 1018 | ConstantPointerRef *CPR = cast<ConstantPointerRef>(CE->getOperand(0)); |
| 1019 | if (!isa<Function>(CPR->getValue())) return false; |
| 1020 | Function *Callee = cast<Function>(CPR->getValue()); |
| 1021 | Instruction *Caller = CS.getInstruction(); |
| 1022 | |
| 1023 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 1024 | // would cause a type conversion of one of our arguments, change this call to |
| 1025 | // be a direct call with arguments casted to the appropriate types. |
| 1026 | // |
| 1027 | const FunctionType *FT = Callee->getFunctionType(); |
| 1028 | const Type *OldRetTy = Caller->getType(); |
| 1029 | |
| 1030 | if (Callee->isExternal() && |
| 1031 | !OldRetTy->isLosslesslyConvertibleTo(FT->getReturnType())) |
| 1032 | return false; // Cannot transform this return value... |
| 1033 | |
| 1034 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 1035 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
| 1036 | |
| 1037 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 1038 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 1039 | const Type *ParamTy = FT->getParamType(i); |
| 1040 | bool isConvertible = (*AI)->getType()->isLosslesslyConvertibleTo(ParamTy); |
| 1041 | if (Callee->isExternal() && !isConvertible) return false; |
| 1042 | } |
| 1043 | |
| 1044 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
| 1045 | Callee->isExternal()) |
| 1046 | return false; // Do not delete arguments unless we have a function body... |
| 1047 | |
| 1048 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 1049 | // inserting cast instructions as necessary... |
| 1050 | std::vector<Value*> Args; |
| 1051 | Args.reserve(NumActualArgs); |
| 1052 | |
| 1053 | AI = CS.arg_begin(); |
| 1054 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 1055 | const Type *ParamTy = FT->getParamType(i); |
| 1056 | if ((*AI)->getType() == ParamTy) { |
| 1057 | Args.push_back(*AI); |
| 1058 | } else { |
| 1059 | Instruction *Cast = new CastInst(*AI, ParamTy, "tmp"); |
| 1060 | InsertNewInstBefore(Cast, *Caller); |
| 1061 | Args.push_back(Cast); |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | // If the function takes more arguments than the call was taking, add them |
| 1066 | // now... |
| 1067 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 1068 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 1069 | |
| 1070 | // If we are removing arguments to the function, emit an obnoxious warning... |
| 1071 | if (FT->getNumParams() < NumActualArgs) |
| 1072 | if (!FT->isVarArg()) { |
| 1073 | std::cerr << "WARNING: While resolving call to function '" |
| 1074 | << Callee->getName() << "' arguments were dropped!\n"; |
| 1075 | } else { |
| 1076 | // Add all of the arguments in their promoted form to the arg list... |
| 1077 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 1078 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 1079 | if (PTy != (*AI)->getType()) { |
| 1080 | // Must promote to pass through va_arg area! |
| 1081 | Instruction *Cast = new CastInst(*AI, PTy, "tmp"); |
| 1082 | InsertNewInstBefore(Cast, *Caller); |
| 1083 | Args.push_back(Cast); |
| 1084 | } else { |
| 1085 | Args.push_back(*AI); |
| 1086 | } |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | if (FT->getReturnType() == Type::VoidTy) |
| 1091 | Caller->setName(""); // Void type should not have a name... |
| 1092 | |
| 1093 | Instruction *NC; |
| 1094 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 1095 | NC = new InvokeInst(Callee, II->getNormalDest(), II->getExceptionalDest(), |
| 1096 | Args, Caller->getName(), Caller); |
| 1097 | } else { |
| 1098 | NC = new CallInst(Callee, Args, Caller->getName(), Caller); |
| 1099 | } |
| 1100 | |
| 1101 | // Insert a cast of the return type as necessary... |
| 1102 | Value *NV = NC; |
| 1103 | if (Caller->getType() != NV->getType() && !Caller->use_empty()) { |
| 1104 | if (NV->getType() != Type::VoidTy) { |
| 1105 | NV = NC = new CastInst(NC, Caller->getType(), "tmp"); |
| 1106 | InsertNewInstBefore(NC, *Caller); |
| 1107 | AddUsesToWorkList(*Caller); |
| 1108 | } else { |
| 1109 | NV = Constant::getNullValue(Caller->getType()); |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 1114 | Caller->replaceAllUsesWith(NV); |
| 1115 | Caller->getParent()->getInstList().erase(Caller); |
| 1116 | removeFromWorkList(Caller); |
| 1117 | return true; |
| 1118 | } |
| 1119 | |
| 1120 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 1121 | |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 1122 | // PHINode simplification |
| 1123 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1124 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 1125 | // 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] | 1126 | if (PN.getNumIncomingValues() == 1) |
| 1127 | return ReplaceInstUsesWith(PN, PN.getIncomingValue(0)); |
Chris Lattner | 9cd1e66 | 2002-08-20 15:35:35 +0000 | [diff] [blame] | 1128 | |
| 1129 | // Otherwise if all of the incoming values are the same for the PHI, replace |
| 1130 | // the PHI node with the incoming value. |
| 1131 | // |
Chris Lattner | f6c0efa | 2002-08-22 20:22:01 +0000 | [diff] [blame] | 1132 | Value *InVal = 0; |
| 1133 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 1134 | if (PN.getIncomingValue(i) != &PN) // Not the PHI node itself... |
| 1135 | if (InVal && PN.getIncomingValue(i) != InVal) |
| 1136 | return 0; // Not the same, bail out. |
| 1137 | else |
| 1138 | InVal = PN.getIncomingValue(i); |
| 1139 | |
| 1140 | // The only case that could cause InVal to be null is if we have a PHI node |
| 1141 | // that only has entries for itself. In this case, there is no entry into the |
| 1142 | // loop, so kill the PHI. |
| 1143 | // |
| 1144 | if (InVal == 0) InVal = Constant::getNullValue(PN.getType()); |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 1145 | |
Chris Lattner | 9cd1e66 | 2002-08-20 15:35:35 +0000 | [diff] [blame] | 1146 | // All of the incoming values are the same, replace the PHI node now. |
| 1147 | return ReplaceInstUsesWith(PN, InVal); |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 1150 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1151 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 471bd76 | 2003-05-22 19:07:21 +0000 | [diff] [blame] | 1152 | // Is it 'getelementptr %P, long 0' or 'getelementptr %P' |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1153 | // If so, eliminate the noop. |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 1154 | if ((GEP.getNumOperands() == 2 && |
Chris Lattner | 136dab7 | 2002-09-11 01:21:33 +0000 | [diff] [blame] | 1155 | GEP.getOperand(1) == Constant::getNullValue(Type::LongTy)) || |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1156 | GEP.getNumOperands() == 1) |
| 1157 | return ReplaceInstUsesWith(GEP, GEP.getOperand(0)); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 1158 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 1159 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 1160 | // is a getelementptr instruction, combine the indices of the two |
| 1161 | // getelementptr instructions into a single instruction. |
| 1162 | // |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 1163 | if (GetElementPtrInst *Src = dyn_cast<GetElementPtrInst>(GEP.getOperand(0))) { |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 1164 | std::vector<Value *> Indices; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 1165 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 1166 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 471bd76 | 2003-05-22 19:07:21 +0000 | [diff] [blame] | 1167 | if (Src->getNumOperands() == 2 && isa<Constant>(Src->getOperand(1)) && |
| 1168 | isa<Constant>(GEP.getOperand(1))) { |
Chris Lattner | 235af56 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 1169 | // Replace: gep (gep %P, long C1), long C2, ... |
| 1170 | // With: gep %P, long (C1+C2), ... |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 1171 | Value *Sum = ConstantExpr::get(Instruction::Add, |
| 1172 | cast<Constant>(Src->getOperand(1)), |
| 1173 | cast<Constant>(GEP.getOperand(1))); |
Chris Lattner | 235af56 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 1174 | assert(Sum && "Constant folding of longs failed!?"); |
| 1175 | GEP.setOperand(0, Src->getOperand(0)); |
| 1176 | GEP.setOperand(1, Sum); |
| 1177 | AddUsesToWorkList(*Src); // Reduce use count of Src |
| 1178 | return &GEP; |
Chris Lattner | 471bd76 | 2003-05-22 19:07:21 +0000 | [diff] [blame] | 1179 | } else if (Src->getNumOperands() == 2) { |
Chris Lattner | 235af56 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 1180 | // Replace: gep (gep %P, long B), long A, ... |
| 1181 | // With: T = long A+B; gep %P, T, ... |
| 1182 | // |
| 1183 | Value *Sum = BinaryOperator::create(Instruction::Add, Src->getOperand(1), |
| 1184 | GEP.getOperand(1), |
| 1185 | Src->getName()+".sum", &GEP); |
| 1186 | GEP.setOperand(0, Src->getOperand(0)); |
| 1187 | GEP.setOperand(1, Sum); |
| 1188 | WorkList.push_back(cast<Instruction>(Sum)); |
| 1189 | return &GEP; |
Chris Lattner | 5d606a0 | 2002-11-04 16:43:32 +0000 | [diff] [blame] | 1190 | } else if (*GEP.idx_begin() == Constant::getNullValue(Type::LongTy) && |
Chris Lattner | a8339e3 | 2002-09-17 21:05:42 +0000 | [diff] [blame] | 1191 | Src->getNumOperands() != 1) { |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 1192 | // Otherwise we can do the fold if the first index of the GEP is a zero |
| 1193 | Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end()); |
| 1194 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
Chris Lattner | 5d606a0 | 2002-11-04 16:43:32 +0000 | [diff] [blame] | 1195 | } else if (Src->getOperand(Src->getNumOperands()-1) == |
| 1196 | Constant::getNullValue(Type::LongTy)) { |
| 1197 | // If the src gep ends with a constant array index, merge this get into |
| 1198 | // it, even if we have a non-zero array index. |
| 1199 | Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end()-1); |
| 1200 | Indices.insert(Indices.end(), GEP.idx_begin(), GEP.idx_end()); |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | if (!Indices.empty()) |
| 1204 | return new GetElementPtrInst(Src->getOperand(0), Indices, GEP.getName()); |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 1205 | |
| 1206 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(GEP.getOperand(0))) { |
| 1207 | // GEP of global variable. If all of the indices for this GEP are |
| 1208 | // constants, we can promote this to a constexpr instead of an instruction. |
| 1209 | |
| 1210 | // Scan for nonconstants... |
| 1211 | std::vector<Constant*> Indices; |
| 1212 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
| 1213 | for (; I != E && isa<Constant>(*I); ++I) |
| 1214 | Indices.push_back(cast<Constant>(*I)); |
| 1215 | |
| 1216 | if (I == E) { // If they are all constants... |
Chris Lattner | 46b3d30 | 2003-04-16 22:40:51 +0000 | [diff] [blame] | 1217 | Constant *CE = |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 1218 | ConstantExpr::getGetElementPtr(ConstantPointerRef::get(GV), Indices); |
| 1219 | |
| 1220 | // Replace all uses of the GEP with the new constexpr... |
| 1221 | return ReplaceInstUsesWith(GEP, CE); |
| 1222 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 1225 | return 0; |
| 1226 | } |
| 1227 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 1228 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
| 1229 | // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1 |
| 1230 | if (AI.isArrayAllocation()) // Check C != 1 |
| 1231 | if (const ConstantUInt *C = dyn_cast<ConstantUInt>(AI.getArraySize())) { |
| 1232 | const Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getValue()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 1233 | AllocationInst *New = 0; |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 1234 | |
| 1235 | // Create and insert the replacement instruction... |
| 1236 | if (isa<MallocInst>(AI)) |
| 1237 | New = new MallocInst(NewTy, 0, AI.getName(), &AI); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 1238 | else { |
| 1239 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 1240 | New = new AllocaInst(NewTy, 0, AI.getName(), &AI); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 1241 | } |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 1242 | |
| 1243 | // Scan to the end of the allocation instructions, to skip over a block of |
| 1244 | // allocas if possible... |
| 1245 | // |
| 1246 | BasicBlock::iterator It = New; |
| 1247 | while (isa<AllocationInst>(*It)) ++It; |
| 1248 | |
| 1249 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 1250 | // insert our getelementptr instruction... |
| 1251 | // |
| 1252 | std::vector<Value*> Idx(2, Constant::getNullValue(Type::LongTy)); |
| 1253 | Value *V = new GetElementPtrInst(New, Idx, New->getName()+".sub", It); |
| 1254 | |
| 1255 | // Now make everything use the getelementptr instead of the original |
| 1256 | // allocation. |
| 1257 | ReplaceInstUsesWith(AI, V); |
| 1258 | return &AI; |
| 1259 | } |
| 1260 | return 0; |
| 1261 | } |
| 1262 | |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 1263 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 1264 | // Change br (not X), label True, label False to: br X, label False, True |
Chris Lattner | 45789ac | 2003-06-05 20:12:51 +0000 | [diff] [blame] | 1265 | if (BI.isConditional() && !isa<Constant>(BI.getCondition())) |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 1266 | if (Value *V = dyn_castNotVal(BI.getCondition())) { |
| 1267 | BasicBlock *TrueDest = BI.getSuccessor(0); |
| 1268 | BasicBlock *FalseDest = BI.getSuccessor(1); |
| 1269 | // Swap Destinations and condition... |
| 1270 | BI.setCondition(V); |
| 1271 | BI.setSuccessor(0, FalseDest); |
| 1272 | BI.setSuccessor(1, TrueDest); |
| 1273 | return &BI; |
| 1274 | } |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 1275 | return 0; |
| 1276 | } |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 1277 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 1278 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 1279 | void InstCombiner::removeFromWorkList(Instruction *I) { |
| 1280 | WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I), |
| 1281 | WorkList.end()); |
| 1282 | } |
| 1283 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1284 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1285 | bool Changed = false; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 1286 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1287 | WorkList.insert(WorkList.end(), inst_begin(F), inst_end(F)); |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 1288 | |
| 1289 | while (!WorkList.empty()) { |
| 1290 | Instruction *I = WorkList.back(); // Get an instruction from the worklist |
| 1291 | WorkList.pop_back(); |
| 1292 | |
Misha Brukman | 632df28 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 1293 | // Check to see if we can DCE or ConstantPropagate the instruction... |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 1294 | // Check to see if we can DIE the instruction... |
| 1295 | if (isInstructionTriviallyDead(I)) { |
| 1296 | // Add operands to the worklist... |
| 1297 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 1298 | if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i))) |
| 1299 | WorkList.push_back(Op); |
| 1300 | |
| 1301 | ++NumDeadInst; |
| 1302 | BasicBlock::iterator BBI = I; |
| 1303 | if (dceInstruction(BBI)) { |
| 1304 | removeFromWorkList(I); |
| 1305 | continue; |
| 1306 | } |
| 1307 | } |
| 1308 | |
Misha Brukman | 632df28 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 1309 | // Instruction isn't dead, see if we can constant propagate it... |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 1310 | if (Constant *C = ConstantFoldInstruction(I)) { |
| 1311 | // Add operands to the worklist... |
| 1312 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 1313 | if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i))) |
| 1314 | WorkList.push_back(Op); |
Chris Lattner | c6509f4 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 1315 | ReplaceInstUsesWith(*I, C); |
| 1316 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 1317 | ++NumConstProp; |
| 1318 | BasicBlock::iterator BBI = I; |
| 1319 | if (dceInstruction(BBI)) { |
| 1320 | removeFromWorkList(I); |
| 1321 | continue; |
| 1322 | } |
| 1323 | } |
| 1324 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 1325 | // Now that we have an instruction, try combining it to simplify it... |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 1326 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 1327 | ++NumCombined; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1328 | // Should we replace the old instruction with a new one? |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 1329 | if (Result != I) { |
| 1330 | // Instructions can end up on the worklist more than once. Make sure |
| 1331 | // we do not process an instruction that has been deleted. |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 1332 | removeFromWorkList(I); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1333 | ReplaceInstWithInst(I, Result); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1334 | } else { |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 1335 | BasicBlock::iterator II = I; |
| 1336 | |
| 1337 | // If the instruction was modified, it's possible that it is now dead. |
| 1338 | // if so, remove it. |
| 1339 | if (dceInstruction(II)) { |
| 1340 | // Instructions may end up in the worklist more than once. Erase them |
| 1341 | // all. |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 1342 | removeFromWorkList(I); |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 1343 | Result = 0; |
| 1344 | } |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 1345 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1346 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 1347 | if (Result) { |
| 1348 | WorkList.push_back(Result); |
| 1349 | AddUsesToWorkList(*Result); |
| 1350 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1351 | Changed = true; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 1352 | } |
| 1353 | } |
| 1354 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1355 | return Changed; |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | Pass *createInstructionCombiningPass() { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1359 | return new InstCombiner(); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 1360 | } |