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