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