Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1 | //===- InstructionCombining.cpp - Combine multiple instructions -----------===// |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9 | // |
| 10 | // InstructionCombining - Combine instructions to form fewer, simple |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11 | // instructions. This pass does not modify the CFG This pass is where algebraic |
| 12 | // simplification happens. |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 13 | // |
| 14 | // This pass combines things like: |
| 15 | // %Y = add int 1, %X |
| 16 | // %Z = add int 1, %Y |
| 17 | // into: |
| 18 | // %Z = add int 2, %X |
| 19 | // |
| 20 | // This is a simple worklist driven algorithm. |
| 21 | // |
Chris Lattner | 216c7b8 | 2003-09-10 05:29:43 +0000 | [diff] [blame] | 22 | // This pass guarantees that the following canonicalizations are performed on |
Chris Lattner | bfb1d03 | 2003-07-23 21:41:57 +0000 | [diff] [blame] | 23 | // the program: |
| 24 | // 1. If a binary operator has a constant operand, it is moved to the RHS |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 25 | // 2. Bitwise operators with constant operands are always grouped so that |
| 26 | // shifts are performed first, then or's, then and's, then xor's. |
Chris Lattner | bfb1d03 | 2003-07-23 21:41:57 +0000 | [diff] [blame] | 27 | // 3. SetCC instructions are converted from <,>,<=,>= to ==,!= if possible |
| 28 | // 4. All SetCC instructions on boolean values are replaced with logical ops |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 29 | // 5. add X, X is represented as (X*2) => (X << 1) |
| 30 | // 6. Multiplies with a power-of-two constant argument are transformed into |
| 31 | // shifts. |
Chris Lattner | bfb1d03 | 2003-07-23 21:41:57 +0000 | [diff] [blame] | 32 | // N. This list is incomplete |
| 33 | // |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
| 35 | |
Chris Lattner | b4cfa7f | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 36 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 471bd76 | 2003-05-22 19:07:21 +0000 | [diff] [blame] | 37 | #include "llvm/Instructions.h" |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame^] | 38 | #include "llvm/Intrinsics.h" |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 39 | #include "llvm/Pass.h" |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 40 | #include "llvm/Constants.h" |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 41 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 42 | #include "llvm/GlobalVariable.h" |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetData.h" |
| 44 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 45 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 60a6591 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 46 | #include "llvm/Support/InstIterator.h" |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 47 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 48 | #include "llvm/Support/CallSite.h" |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 49 | #include "Support/Statistic.h" |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 50 | #include <algorithm> |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 51 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 53 | namespace { |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 54 | Statistic<> NumCombined ("instcombine", "Number of insts combined"); |
| 55 | Statistic<> NumConstProp("instcombine", "Number of constant folds"); |
| 56 | Statistic<> NumDeadInst ("instcombine", "Number of dead inst eliminated"); |
| 57 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 58 | class InstCombiner : public FunctionPass, |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 59 | public InstVisitor<InstCombiner, Instruction*> { |
| 60 | // Worklist of all of the instructions that need to be simplified. |
| 61 | std::vector<Instruction*> WorkList; |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 62 | TargetData *TD; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame^] | 64 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
| 65 | /// the instruction to the work lists because they might get more simplified |
| 66 | /// now. |
| 67 | /// |
| 68 | void AddUsersToWorkList(Instruction &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 69 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 70 | UI != UE; ++UI) |
| 71 | WorkList.push_back(cast<Instruction>(*UI)); |
| 72 | } |
| 73 | |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame^] | 74 | /// AddUsesToWorkList - When an instruction is simplified, add operands to |
| 75 | /// the work lists because they might get more simplified now. |
| 76 | /// |
| 77 | void AddUsesToWorkList(Instruction &I) { |
| 78 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 79 | if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) |
| 80 | WorkList.push_back(Op); |
| 81 | } |
| 82 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 83 | // removeFromWorkList - remove all instances of I from the worklist. |
| 84 | void removeFromWorkList(Instruction *I); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 85 | public: |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 86 | virtual bool runOnFunction(Function &F); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 87 | |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 88 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 89 | AU.addRequired<TargetData>(); |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 90 | AU.setPreservesCFG(); |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 93 | // Visitation implementation - Implement instruction combining for different |
| 94 | // instruction types. The semantics are as follows: |
| 95 | // Return Value: |
| 96 | // null - No change was made |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 97 | // 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] | 98 | // otherwise - Change was made, replace I with returned instruction |
| 99 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 100 | Instruction *visitAdd(BinaryOperator &I); |
| 101 | Instruction *visitSub(BinaryOperator &I); |
| 102 | Instruction *visitMul(BinaryOperator &I); |
| 103 | Instruction *visitDiv(BinaryOperator &I); |
| 104 | Instruction *visitRem(BinaryOperator &I); |
| 105 | Instruction *visitAnd(BinaryOperator &I); |
| 106 | Instruction *visitOr (BinaryOperator &I); |
| 107 | Instruction *visitXor(BinaryOperator &I); |
| 108 | Instruction *visitSetCondInst(BinaryOperator &I); |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 109 | Instruction *visitShiftInst(ShiftInst &I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 110 | Instruction *visitCastInst(CastInst &CI); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 111 | Instruction *visitCallInst(CallInst &CI); |
| 112 | Instruction *visitInvokeInst(InvokeInst &II); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 113 | Instruction *visitPHINode(PHINode &PN); |
| 114 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 115 | Instruction *visitAllocationInst(AllocationInst &AI); |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 116 | Instruction *visitFreeInst(FreeInst &FI); |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 117 | Instruction *visitLoadInst(LoadInst &LI); |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 118 | Instruction *visitBranchInst(BranchInst &BI); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 119 | |
| 120 | // visitInstruction - Specify what to return for unhandled instructions... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 121 | Instruction *visitInstruction(Instruction &I) { return 0; } |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 123 | private: |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 124 | Instruction *visitCallSite(CallSite CS); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 125 | bool transformConstExprCastCall(CallSite CS); |
| 126 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 127 | // InsertNewInstBefore - insert an instruction New before instruction Old |
| 128 | // in the program. Add the new instruction to the worklist. |
| 129 | // |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 130 | Value *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Chris Lattner | 65217ff | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 131 | assert(New && New->getParent() == 0 && |
| 132 | "New instruction already inserted into a basic block!"); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 133 | BasicBlock *BB = Old.getParent(); |
| 134 | BB->getInstList().insert(&Old, New); // Insert inst |
| 135 | WorkList.push_back(New); // Add to worklist |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 136 | return New; |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 139 | public: |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 140 | // ReplaceInstUsesWith - This method is to be used when an instruction is |
| 141 | // found to be dead, replacable with another preexisting expression. Here |
| 142 | // we add all uses of I to the worklist, replace all uses of I with the new |
| 143 | // value, then return I, so that the inst combiner will know that I was |
| 144 | // modified. |
| 145 | // |
| 146 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame^] | 147 | AddUsersToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 148 | I.replaceAllUsesWith(V); |
| 149 | return &I; |
| 150 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame^] | 151 | |
| 152 | // EraseInstFromFunction - When dealing with an instruction that has side |
| 153 | // effects or produces a void value, we can't rely on DCE to delete the |
| 154 | // instruction. Instead, visit methods should return the value returned by |
| 155 | // this function. |
| 156 | Instruction *EraseInstFromFunction(Instruction &I) { |
| 157 | assert(I.use_empty() && "Cannot erase instruction that is used!"); |
| 158 | AddUsesToWorkList(I); |
| 159 | removeFromWorkList(&I); |
| 160 | I.getParent()->getInstList().erase(&I); |
| 161 | return 0; // Don't do anything with FI |
| 162 | } |
| 163 | |
| 164 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 165 | private: |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 166 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 167 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 168 | /// casts that are known to not do anything... |
| 169 | /// |
| 170 | Value *InsertOperandCastBefore(Value *V, const Type *DestTy, |
| 171 | Instruction *InsertBefore); |
| 172 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 173 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 174 | // operators... |
| 175 | bool SimplifyCommutative(BinaryOperator &I); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 176 | |
| 177 | Instruction *OptAndOp(Instruction *Op, ConstantIntegral *OpRHS, |
| 178 | ConstantIntegral *AndRHS, BinaryOperator &TheAnd); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 179 | }; |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 180 | |
Chris Lattner | c8b7092 | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 181 | RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions"); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 184 | // getComplexity: Assign a complexity or rank value to LLVM Values... |
| 185 | // 0 -> Constant, 1 -> Other, 2 -> Argument, 2 -> Unary, 3 -> OtherInst |
| 186 | static unsigned getComplexity(Value *V) { |
| 187 | if (isa<Instruction>(V)) { |
| 188 | if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V)) |
| 189 | return 2; |
| 190 | return 3; |
| 191 | } |
| 192 | if (isa<Argument>(V)) return 2; |
| 193 | return isa<Constant>(V) ? 0 : 1; |
| 194 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 195 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 196 | // isOnlyUse - Return true if this instruction will be deleted if we stop using |
| 197 | // it. |
| 198 | static bool isOnlyUse(Value *V) { |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 199 | return V->hasOneUse() || isa<Constant>(V); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 202 | // getSignedIntegralType - Given an unsigned integral type, return the signed |
| 203 | // version of it that has the same size. |
| 204 | static const Type *getSignedIntegralType(const Type *Ty) { |
| 205 | switch (Ty->getPrimitiveID()) { |
| 206 | default: assert(0 && "Invalid unsigned integer type!"); abort(); |
| 207 | case Type::UByteTyID: return Type::SByteTy; |
| 208 | case Type::UShortTyID: return Type::ShortTy; |
| 209 | case Type::UIntTyID: return Type::IntTy; |
| 210 | case Type::ULongTyID: return Type::LongTy; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // getPromotedType - Return the specified type promoted as it would be to pass |
| 215 | // though a va_arg area... |
| 216 | static const Type *getPromotedType(const Type *Ty) { |
| 217 | switch (Ty->getPrimitiveID()) { |
| 218 | case Type::SByteTyID: |
| 219 | case Type::ShortTyID: return Type::IntTy; |
| 220 | case Type::UByteTyID: |
| 221 | case Type::UShortTyID: return Type::UIntTy; |
| 222 | case Type::FloatTyID: return Type::DoubleTy; |
| 223 | default: return Ty; |
| 224 | } |
| 225 | } |
| 226 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 227 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 228 | // operators: |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 229 | // |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 230 | // 1. Order operands such that they are listed from right (least complex) to |
| 231 | // left (most complex). This puts constants before unary operators before |
| 232 | // binary operators. |
| 233 | // |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 234 | // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) |
| 235 | // 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] | 236 | // |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 237 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 238 | bool Changed = false; |
| 239 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) |
| 240 | Changed = !I.swapOperands(); |
| 241 | |
| 242 | if (!I.isAssociative()) return Changed; |
| 243 | Instruction::BinaryOps Opcode = I.getOpcode(); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 244 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) |
| 245 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { |
| 246 | if (isa<Constant>(I.getOperand(1))) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 247 | Constant *Folded = ConstantExpr::get(I.getOpcode(), |
| 248 | cast<Constant>(I.getOperand(1)), |
| 249 | cast<Constant>(Op->getOperand(1))); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 250 | I.setOperand(0, Op->getOperand(0)); |
| 251 | I.setOperand(1, Folded); |
| 252 | return true; |
| 253 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) |
| 254 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && |
| 255 | isOnlyUse(Op) && isOnlyUse(Op1)) { |
| 256 | Constant *C1 = cast<Constant>(Op->getOperand(1)); |
| 257 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); |
| 258 | |
| 259 | // 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] | 260 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 261 | Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0), |
| 262 | Op1->getOperand(0), |
| 263 | Op1->getName(), &I); |
| 264 | WorkList.push_back(New); |
| 265 | I.setOperand(0, New); |
| 266 | I.setOperand(1, Folded); |
| 267 | return true; |
| 268 | } |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 269 | } |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 270 | return Changed; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 271 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 272 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 273 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
| 274 | // if the LHS is a constant zero (which is the 'negate' form). |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 275 | // |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 276 | static inline Value *dyn_castNegVal(Value *V) { |
| 277 | if (BinaryOperator::isNeg(V)) |
| 278 | return BinaryOperator::getNegArgument(cast<BinaryOperator>(V)); |
| 279 | |
Chris Lattner | 9244df6 | 2003-04-30 22:19:10 +0000 | [diff] [blame] | 280 | // Constants can be considered to be negated values if they can be folded... |
| 281 | if (Constant *C = dyn_cast<Constant>(V)) |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 282 | return ConstantExpr::get(Instruction::Sub, |
| 283 | Constant::getNullValue(V->getType()), C); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 284 | return 0; |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 287 | static Constant *NotConstant(Constant *C) { |
| 288 | return ConstantExpr::get(Instruction::Xor, C, |
| 289 | ConstantIntegral::getAllOnesValue(C->getType())); |
| 290 | } |
| 291 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 292 | static inline Value *dyn_castNotVal(Value *V) { |
| 293 | if (BinaryOperator::isNot(V)) |
| 294 | return BinaryOperator::getNotArgument(cast<BinaryOperator>(V)); |
| 295 | |
| 296 | // Constants can be considered to be not'ed values... |
Chris Lattner | dd65d86 | 2003-04-30 22:34:06 +0000 | [diff] [blame] | 297 | if (ConstantIntegral *C = dyn_cast<ConstantIntegral>(V)) |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 298 | return NotConstant(C); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 299 | return 0; |
| 300 | } |
| 301 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 302 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
| 303 | // other computations (because it has a constant operand), return the |
| 304 | // non-constant operand of the multiply. |
| 305 | // |
| 306 | static inline Value *dyn_castFoldableMul(Value *V) { |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 307 | if (V->hasOneUse() && V->getType()->isInteger()) |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 308 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 309 | if (I->getOpcode() == Instruction::Mul) |
| 310 | if (isa<Constant>(I->getOperand(1))) |
| 311 | return I->getOperand(0); |
| 312 | return 0; |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 313 | } |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 314 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 315 | // dyn_castMaskingAnd - If this value is an And instruction masking a value with |
| 316 | // a constant, return the constant being anded with. |
| 317 | // |
Chris Lattner | 01d5639 | 2003-08-12 19:17:27 +0000 | [diff] [blame] | 318 | template<class ValueType> |
| 319 | static inline Constant *dyn_castMaskingAnd(ValueType *V) { |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 320 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 321 | if (I->getOpcode() == Instruction::And) |
| 322 | return dyn_cast<Constant>(I->getOperand(1)); |
| 323 | |
| 324 | // If this is a constant, it acts just like we were masking with it. |
| 325 | return dyn_cast<Constant>(V); |
| 326 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 327 | |
| 328 | // Log2 - Calculate the log base 2 for the specified value if it is exactly a |
| 329 | // power of 2. |
| 330 | static unsigned Log2(uint64_t Val) { |
| 331 | assert(Val > 1 && "Values 0 and 1 should be handled elsewhere!"); |
| 332 | unsigned Count = 0; |
| 333 | while (Val != 1) { |
| 334 | if (Val & 1) return 0; // Multiple bits set? |
| 335 | Val >>= 1; |
| 336 | ++Count; |
| 337 | } |
| 338 | return Count; |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 341 | |
| 342 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
| 343 | /// function is designed to check a chain of associative operators for a |
| 344 | /// potential to apply a certain optimization. Since the optimization may be |
| 345 | /// applicable if the expression was reassociated, this checks the chain, then |
| 346 | /// reassociates the expression as necessary to expose the optimization |
| 347 | /// opportunity. This makes use of a special Functor, which must define |
| 348 | /// 'shouldApply' and 'apply' methods. |
| 349 | /// |
| 350 | template<typename Functor> |
| 351 | Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { |
| 352 | unsigned Opcode = Root.getOpcode(); |
| 353 | Value *LHS = Root.getOperand(0); |
| 354 | |
| 355 | // Quick check, see if the immediate LHS matches... |
| 356 | if (F.shouldApply(LHS)) |
| 357 | return F.apply(Root); |
| 358 | |
| 359 | // Otherwise, if the LHS is not of the same opcode as the root, return. |
| 360 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 361 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 362 | // Should we apply this transform to the RHS? |
| 363 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); |
| 364 | |
| 365 | // If not to the RHS, check to see if we should apply to the LHS... |
| 366 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { |
| 367 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS |
| 368 | ShouldApply = true; |
| 369 | } |
| 370 | |
| 371 | // If the functor wants to apply the optimization to the RHS of LHSI, |
| 372 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) |
| 373 | if (ShouldApply) { |
| 374 | BasicBlock *BB = Root.getParent(); |
| 375 | // All of the instructions have a single use and have no side-effects, |
| 376 | // because of this, we can pull them all into the current basic block. |
| 377 | if (LHSI->getParent() != BB) { |
| 378 | // Move all of the instructions from root to LHSI into the current |
| 379 | // block. |
| 380 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); |
| 381 | Instruction *LastUse = &Root; |
| 382 | while (TmpLHSI->getParent() == BB) { |
| 383 | LastUse = TmpLHSI; |
| 384 | TmpLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); |
| 385 | } |
| 386 | |
| 387 | // Loop over all of the instructions in other blocks, moving them into |
| 388 | // the current one. |
| 389 | Value *TmpLHS = TmpLHSI; |
| 390 | do { |
| 391 | TmpLHSI = cast<Instruction>(TmpLHS); |
| 392 | // Remove from current block... |
| 393 | TmpLHSI->getParent()->getInstList().remove(TmpLHSI); |
| 394 | // Insert before the last instruction... |
| 395 | BB->getInstList().insert(LastUse, TmpLHSI); |
| 396 | TmpLHS = TmpLHSI->getOperand(0); |
| 397 | } while (TmpLHSI != LHSI); |
| 398 | } |
| 399 | |
| 400 | // Now all of the instructions are in the current basic block, go ahead |
| 401 | // and perform the reassociation. |
| 402 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); |
| 403 | |
| 404 | // First move the selected RHS to the LHS of the root... |
| 405 | Root.setOperand(0, LHSI->getOperand(1)); |
| 406 | |
| 407 | // Make what used to be the LHS of the root be the user of the root... |
| 408 | Value *ExtraOperand = TmpLHSI->getOperand(1); |
| 409 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
| 410 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
| 411 | BB->getInstList().remove(&Root); // Remove root from the BB |
| 412 | BB->getInstList().insert(TmpLHSI, &Root); // Insert root before TmpLHSI |
| 413 | |
| 414 | // Now propagate the ExtraOperand down the chain of instructions until we |
| 415 | // get to LHSI. |
| 416 | while (TmpLHSI != LHSI) { |
| 417 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); |
| 418 | Value *NextOp = NextLHSI->getOperand(1); |
| 419 | NextLHSI->setOperand(1, ExtraOperand); |
| 420 | TmpLHSI = NextLHSI; |
| 421 | ExtraOperand = NextOp; |
| 422 | } |
| 423 | |
| 424 | // Now that the instructions are reassociated, have the functor perform |
| 425 | // the transformation... |
| 426 | return F.apply(Root); |
| 427 | } |
| 428 | |
| 429 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
| 430 | } |
| 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | |
| 435 | // AddRHS - Implements: X + X --> X << 1 |
| 436 | struct AddRHS { |
| 437 | Value *RHS; |
| 438 | AddRHS(Value *rhs) : RHS(rhs) {} |
| 439 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 440 | Instruction *apply(BinaryOperator &Add) const { |
| 441 | return new ShiftInst(Instruction::Shl, Add.getOperand(0), |
| 442 | ConstantInt::get(Type::UByteTy, 1)); |
| 443 | } |
| 444 | }; |
| 445 | |
| 446 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) |
| 447 | // iff C1&C2 == 0 |
| 448 | struct AddMaskingAnd { |
| 449 | Constant *C2; |
| 450 | AddMaskingAnd(Constant *c) : C2(c) {} |
| 451 | bool shouldApply(Value *LHS) const { |
| 452 | if (Constant *C1 = dyn_castMaskingAnd(LHS)) |
| 453 | return ConstantExpr::get(Instruction::And, C1, C2)->isNullValue(); |
| 454 | return false; |
| 455 | } |
| 456 | Instruction *apply(BinaryOperator &Add) const { |
| 457 | return BinaryOperator::create(Instruction::Or, Add.getOperand(0), |
| 458 | Add.getOperand(1)); |
| 459 | } |
| 460 | }; |
| 461 | |
| 462 | |
| 463 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 464 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 465 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 466 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 467 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 468 | // X + 0 --> X |
Chris Lattner | 8ee0593 | 2004-02-24 18:10:14 +0000 | [diff] [blame] | 469 | if (!I.getType()->isFloatingPoint() && // -0 + +0 = +0, so it's not a noop |
| 470 | RHS == Constant::getNullValue(I.getType())) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 471 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 472 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 473 | // X + X --> X << 1 |
| 474 | if (I.getType()->isInteger()) |
| 475 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 476 | |
Chris Lattner | 147e975 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 477 | // -A + B --> B - A |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 478 | if (Value *V = dyn_castNegVal(LHS)) |
Chris Lattner | 147e975 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 479 | return BinaryOperator::create(Instruction::Sub, RHS, V); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 480 | |
| 481 | // A + -B --> A - B |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 482 | if (!isa<Constant>(RHS)) |
| 483 | if (Value *V = dyn_castNegVal(RHS)) |
| 484 | return BinaryOperator::create(Instruction::Sub, LHS, V); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 485 | |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 486 | // X*C + X --> X * (C+1) |
| 487 | if (dyn_castFoldableMul(LHS) == RHS) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 488 | Constant *CP1 = |
| 489 | ConstantExpr::get(Instruction::Add, |
| 490 | cast<Constant>(cast<Instruction>(LHS)->getOperand(1)), |
| 491 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 492 | return BinaryOperator::create(Instruction::Mul, RHS, CP1); |
| 493 | } |
| 494 | |
| 495 | // X + X*C --> X * (C+1) |
| 496 | if (dyn_castFoldableMul(RHS) == LHS) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 497 | Constant *CP1 = |
| 498 | ConstantExpr::get(Instruction::Add, |
| 499 | cast<Constant>(cast<Instruction>(RHS)->getOperand(1)), |
| 500 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 501 | return BinaryOperator::create(Instruction::Mul, LHS, CP1); |
| 502 | } |
| 503 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 504 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
| 505 | if (Constant *C2 = dyn_castMaskingAnd(RHS)) |
| 506 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) return R; |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 507 | |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 508 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
| 509 | if (Instruction *ILHS = dyn_cast<Instruction>(LHS)) { |
| 510 | switch (ILHS->getOpcode()) { |
| 511 | case Instruction::Xor: |
| 512 | // ~X + C --> (C-1) - X |
| 513 | if (ConstantInt *XorRHS = dyn_cast<ConstantInt>(ILHS->getOperand(1))) |
| 514 | if (XorRHS->isAllOnesValue()) |
| 515 | return BinaryOperator::create(Instruction::Sub, |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 516 | ConstantExpr::get(Instruction::Sub, |
| 517 | CRHS, ConstantInt::get(I.getType(), 1)), |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 518 | ILHS->getOperand(0)); |
| 519 | break; |
| 520 | default: break; |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 525 | return Changed ? &I : 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Chris Lattner | bdb0ce0 | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 528 | // isSignBit - Return true if the value represented by the constant only has the |
| 529 | // highest order bit set. |
| 530 | static bool isSignBit(ConstantInt *CI) { |
| 531 | unsigned NumBits = CI->getType()->getPrimitiveSize()*8; |
| 532 | return (CI->getRawValue() & ~(-1LL << NumBits)) == (1ULL << (NumBits-1)); |
| 533 | } |
| 534 | |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 535 | static unsigned getTypeSizeInBits(const Type *Ty) { |
| 536 | return Ty == Type::BoolTy ? 1 : Ty->getPrimitiveSize()*8; |
| 537 | } |
| 538 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 539 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 540 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 541 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 542 | if (Op0 == Op1) // sub X, X -> 0 |
| 543 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 544 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 545 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 546 | if (Value *V = dyn_castNegVal(Op1)) |
Chris Lattner | 147e975 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 547 | return BinaryOperator::create(Instruction::Add, Op0, V); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 548 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 549 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
| 550 | // Replace (-1 - A) with (~A)... |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 551 | if (C->isAllOnesValue()) |
| 552 | return BinaryOperator::createNot(Op1); |
Chris Lattner | ad3c495 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 553 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 554 | // C - ~X == X + (1+C) |
| 555 | if (BinaryOperator::isNot(Op1)) |
| 556 | return BinaryOperator::create(Instruction::Add, |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 557 | BinaryOperator::getNotArgument(cast<BinaryOperator>(Op1)), |
| 558 | ConstantExpr::get(Instruction::Add, C, |
| 559 | ConstantInt::get(I.getType(), 1))); |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 562 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 563 | if (Op1I->hasOneUse()) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 564 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 565 | // is not used by anyone else... |
| 566 | // |
Chris Lattner | c2f0aa5 | 2004-02-02 20:09:56 +0000 | [diff] [blame] | 567 | if (Op1I->getOpcode() == Instruction::Sub && |
| 568 | !Op1I->getType()->isFloatingPoint()) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 569 | // Swap the two operands of the subexpr... |
| 570 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 571 | Op1I->setOperand(0, IIOp1); |
| 572 | Op1I->setOperand(1, IIOp0); |
| 573 | |
| 574 | // Create the new top level add instruction... |
| 575 | return BinaryOperator::create(Instruction::Add, Op0, Op1); |
| 576 | } |
| 577 | |
| 578 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 579 | // |
| 580 | if (Op1I->getOpcode() == Instruction::And && |
| 581 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 582 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 583 | |
| 584 | Instruction *NewNot = BinaryOperator::createNot(OtherOp, "B.not", &I); |
| 585 | return BinaryOperator::create(Instruction::And, Op0, NewNot); |
| 586 | } |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 587 | |
| 588 | // X - X*C --> X * (1-C) |
| 589 | if (dyn_castFoldableMul(Op1I) == Op0) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 590 | Constant *CP1 = |
| 591 | ConstantExpr::get(Instruction::Sub, |
| 592 | ConstantInt::get(I.getType(), 1), |
| 593 | cast<Constant>(cast<Instruction>(Op1)->getOperand(1))); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 594 | assert(CP1 && "Couldn't constant fold 1-C?"); |
| 595 | return BinaryOperator::create(Instruction::Mul, Op0, CP1); |
| 596 | } |
Chris Lattner | ad3c495 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 597 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 598 | |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 599 | // X*C - X --> X * (C-1) |
| 600 | if (dyn_castFoldableMul(Op0) == Op1) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 601 | Constant *CP1 = |
| 602 | ConstantExpr::get(Instruction::Sub, |
| 603 | cast<Constant>(cast<Instruction>(Op0)->getOperand(1)), |
| 604 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 605 | assert(CP1 && "Couldn't constant fold C - 1?"); |
| 606 | return BinaryOperator::create(Instruction::Mul, Op1, CP1); |
| 607 | } |
| 608 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 609 | return 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 612 | /// isSignBitCheck - Given an exploded setcc instruction, return true if it is |
| 613 | /// really just returns true if the most significant (sign) bit is set. |
| 614 | static bool isSignBitCheck(unsigned Opcode, Value *LHS, ConstantInt *RHS) { |
| 615 | if (RHS->getType()->isSigned()) { |
| 616 | // True if source is LHS < 0 or LHS <= -1 |
| 617 | return Opcode == Instruction::SetLT && RHS->isNullValue() || |
| 618 | Opcode == Instruction::SetLE && RHS->isAllOnesValue(); |
| 619 | } else { |
| 620 | ConstantUInt *RHSC = cast<ConstantUInt>(RHS); |
| 621 | // True if source is LHS > 127 or LHS >= 128, where the constants depend on |
| 622 | // the size of the integer type. |
| 623 | if (Opcode == Instruction::SetGE) |
| 624 | return RHSC->getValue() == 1ULL<<(RHS->getType()->getPrimitiveSize()*8-1); |
| 625 | if (Opcode == Instruction::SetGT) |
| 626 | return RHSC->getValue() == |
| 627 | (1ULL << (RHS->getType()->getPrimitiveSize()*8-1))-1; |
| 628 | } |
| 629 | return false; |
| 630 | } |
| 631 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 632 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 633 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 634 | Value *Op0 = I.getOperand(0); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 635 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 636 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 637 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 638 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 639 | |
| 640 | // ((X << C1)*C2) == (X * (C2 << C1)) |
| 641 | if (ShiftInst *SI = dyn_cast<ShiftInst>(Op0)) |
| 642 | if (SI->getOpcode() == Instruction::Shl) |
| 643 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
| 644 | return BinaryOperator::create(Instruction::Mul, SI->getOperand(0), |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 645 | ConstantExpr::get(Instruction::Shl, CI, ShOp)); |
| 646 | |
Chris Lattner | cce81be | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 647 | if (CI->isNullValue()) |
| 648 | return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 |
| 649 | if (CI->equalsInt(1)) // X * 1 == X |
| 650 | return ReplaceInstUsesWith(I, Op0); |
| 651 | if (CI->isAllOnesValue()) // X * -1 == 0 - X |
Chris Lattner | 35236d8 | 2003-06-25 17:09:20 +0000 | [diff] [blame] | 652 | return BinaryOperator::createNeg(Op0, I.getName()); |
Chris Lattner | 31ba129 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 653 | |
Chris Lattner | cce81be | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 654 | int64_t Val = (int64_t)cast<ConstantInt>(CI)->getRawValue(); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 655 | if (uint64_t C = Log2(Val)) // Replace X*(2^C) with X << C |
| 656 | return new ShiftInst(Instruction::Shl, Op0, |
| 657 | ConstantUInt::get(Type::UByteTy, C)); |
| 658 | } else { |
| 659 | ConstantFP *Op1F = cast<ConstantFP>(Op1); |
| 660 | if (Op1F->isNullValue()) |
| 661 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 31ba129 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 662 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 663 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 664 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) |
| 665 | if (Op1F->getValue() == 1.0) |
| 666 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
| 667 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Chris Lattner | 934a64cf | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 670 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
| 671 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) |
| 672 | return BinaryOperator::create(Instruction::Mul, Op0v, Op1v); |
| 673 | |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 674 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 675 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 676 | // See if we can simplify things based on how the boolean was originally |
| 677 | // formed. |
| 678 | CastInst *BoolCast = 0; |
| 679 | if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(0))) |
| 680 | if (CI->getOperand(0)->getType() == Type::BoolTy) |
| 681 | BoolCast = CI; |
| 682 | if (!BoolCast) |
| 683 | if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(1))) |
| 684 | if (CI->getOperand(0)->getType() == Type::BoolTy) |
| 685 | BoolCast = CI; |
| 686 | if (BoolCast) { |
| 687 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(BoolCast->getOperand(0))) { |
| 688 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); |
| 689 | const Type *SCOpTy = SCIOp0->getType(); |
| 690 | |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 691 | // If the setcc is true iff the sign bit of X is set, then convert this |
| 692 | // multiply into a shift/and combination. |
| 693 | if (isa<ConstantInt>(SCIOp1) && |
| 694 | isSignBitCheck(SCI->getOpcode(), SCIOp0, cast<ConstantInt>(SCIOp1))) { |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 695 | // Shift the X value right to turn it into "all signbits". |
| 696 | Constant *Amt = ConstantUInt::get(Type::UByteTy, |
| 697 | SCOpTy->getPrimitiveSize()*8-1); |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 698 | if (SCIOp0->getType()->isUnsigned()) { |
| 699 | const Type *NewTy = getSignedIntegralType(SCIOp0->getType()); |
| 700 | SCIOp0 = InsertNewInstBefore(new CastInst(SCIOp0, NewTy, |
| 701 | SCIOp0->getName()), I); |
| 702 | } |
| 703 | |
| 704 | Value *V = |
| 705 | InsertNewInstBefore(new ShiftInst(Instruction::Shr, SCIOp0, Amt, |
| 706 | BoolCast->getOperand(0)->getName()+ |
| 707 | ".mask"), I); |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 708 | |
| 709 | // If the multiply type is not the same as the source type, sign extend |
| 710 | // or truncate to the multiply type. |
| 711 | if (I.getType() != V->getType()) |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 712 | V = InsertNewInstBefore(new CastInst(V, I.getType(), V->getName()),I); |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 713 | |
| 714 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; |
| 715 | return BinaryOperator::create(Instruction::And, V, OtherOp); |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 720 | return Changed ? &I : 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 723 | Instruction *InstCombiner::visitDiv(BinaryOperator &I) { |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 724 | // div X, 1 == X |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 725 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) { |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 726 | if (RHS->equalsInt(1)) |
| 727 | return ReplaceInstUsesWith(I, I.getOperand(0)); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 728 | |
| 729 | // Check to see if this is an unsigned division with an exact power of 2, |
| 730 | // if so, convert to a right shift. |
| 731 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS)) |
| 732 | if (uint64_t Val = C->getValue()) // Don't break X / 0 |
| 733 | if (uint64_t C = Log2(Val)) |
| 734 | return new ShiftInst(Instruction::Shr, I.getOperand(0), |
| 735 | ConstantUInt::get(Type::UByteTy, C)); |
| 736 | } |
| 737 | |
| 738 | // 0 / X == 0, we don't need to preserve faults! |
| 739 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(I.getOperand(0))) |
| 740 | if (LHS->equalsInt(0)) |
| 741 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 742 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 747 | Instruction *InstCombiner::visitRem(BinaryOperator &I) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 748 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) { |
| 749 | if (RHS->equalsInt(1)) // X % 1 == 0 |
| 750 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 751 | |
| 752 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 753 | // if so, convert to a bitwise and. |
| 754 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS)) |
| 755 | if (uint64_t Val = C->getValue()) // Don't break X % 0 (divide by zero) |
| 756 | if (Log2(Val)) |
| 757 | return BinaryOperator::create(Instruction::And, I.getOperand(0), |
| 758 | ConstantUInt::get(I.getType(), Val-1)); |
| 759 | } |
| 760 | |
| 761 | // 0 % X == 0, we don't need to preserve faults! |
| 762 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(I.getOperand(0))) |
| 763 | if (LHS->equalsInt(0)) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 764 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 765 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 766 | return 0; |
| 767 | } |
| 768 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 769 | // isMaxValueMinusOne - return true if this is Max-1 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 770 | static bool isMaxValueMinusOne(const ConstantInt *C) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 771 | if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) { |
| 772 | // Calculate -1 casted to the right type... |
| 773 | unsigned TypeBits = C->getType()->getPrimitiveSize()*8; |
| 774 | uint64_t Val = ~0ULL; // All ones |
| 775 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 776 | return CU->getValue() == Val-1; |
| 777 | } |
| 778 | |
| 779 | const ConstantSInt *CS = cast<ConstantSInt>(C); |
| 780 | |
| 781 | // Calculate 0111111111..11111 |
| 782 | unsigned TypeBits = C->getType()->getPrimitiveSize()*8; |
| 783 | int64_t Val = INT64_MAX; // All ones |
| 784 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 785 | return CS->getValue() == Val-1; |
| 786 | } |
| 787 | |
| 788 | // isMinValuePlusOne - return true if this is Min+1 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 789 | static bool isMinValuePlusOne(const ConstantInt *C) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 790 | if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) |
| 791 | return CU->getValue() == 1; |
| 792 | |
| 793 | const ConstantSInt *CS = cast<ConstantSInt>(C); |
| 794 | |
| 795 | // Calculate 1111111111000000000000 |
| 796 | unsigned TypeBits = C->getType()->getPrimitiveSize()*8; |
| 797 | int64_t Val = -1; // All ones |
| 798 | Val <<= TypeBits-1; // Shift over to the right spot |
| 799 | return CS->getValue() == Val+1; |
| 800 | } |
| 801 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 802 | /// getSetCondCode - Encode a setcc opcode into a three bit mask. These bits |
| 803 | /// are carefully arranged to allow folding of expressions such as: |
| 804 | /// |
| 805 | /// (A < B) | (A > B) --> (A != B) |
| 806 | /// |
| 807 | /// Bit value '4' represents that the comparison is true if A > B, bit value '2' |
| 808 | /// represents that the comparison is true if A == B, and bit value '1' is true |
| 809 | /// if A < B. |
| 810 | /// |
| 811 | static unsigned getSetCondCode(const SetCondInst *SCI) { |
| 812 | switch (SCI->getOpcode()) { |
| 813 | // False -> 0 |
| 814 | case Instruction::SetGT: return 1; |
| 815 | case Instruction::SetEQ: return 2; |
| 816 | case Instruction::SetGE: return 3; |
| 817 | case Instruction::SetLT: return 4; |
| 818 | case Instruction::SetNE: return 5; |
| 819 | case Instruction::SetLE: return 6; |
| 820 | // True -> 7 |
| 821 | default: |
| 822 | assert(0 && "Invalid SetCC opcode!"); |
| 823 | return 0; |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | /// getSetCCValue - This is the complement of getSetCondCode, which turns an |
| 828 | /// opcode and two operands into either a constant true or false, or a brand new |
| 829 | /// SetCC instruction. |
| 830 | static Value *getSetCCValue(unsigned Opcode, Value *LHS, Value *RHS) { |
| 831 | switch (Opcode) { |
| 832 | case 0: return ConstantBool::False; |
| 833 | case 1: return new SetCondInst(Instruction::SetGT, LHS, RHS); |
| 834 | case 2: return new SetCondInst(Instruction::SetEQ, LHS, RHS); |
| 835 | case 3: return new SetCondInst(Instruction::SetGE, LHS, RHS); |
| 836 | case 4: return new SetCondInst(Instruction::SetLT, LHS, RHS); |
| 837 | case 5: return new SetCondInst(Instruction::SetNE, LHS, RHS); |
| 838 | case 6: return new SetCondInst(Instruction::SetLE, LHS, RHS); |
| 839 | case 7: return ConstantBool::True; |
| 840 | default: assert(0 && "Illegal SetCCCode!"); return 0; |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | // FoldSetCCLogical - Implements (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B) |
| 845 | struct FoldSetCCLogical { |
| 846 | InstCombiner &IC; |
| 847 | Value *LHS, *RHS; |
| 848 | FoldSetCCLogical(InstCombiner &ic, SetCondInst *SCI) |
| 849 | : IC(ic), LHS(SCI->getOperand(0)), RHS(SCI->getOperand(1)) {} |
| 850 | bool shouldApply(Value *V) const { |
| 851 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(V)) |
| 852 | return (SCI->getOperand(0) == LHS && SCI->getOperand(1) == RHS || |
| 853 | SCI->getOperand(0) == RHS && SCI->getOperand(1) == LHS); |
| 854 | return false; |
| 855 | } |
| 856 | Instruction *apply(BinaryOperator &Log) const { |
| 857 | SetCondInst *SCI = cast<SetCondInst>(Log.getOperand(0)); |
| 858 | if (SCI->getOperand(0) != LHS) { |
| 859 | assert(SCI->getOperand(1) == LHS); |
| 860 | SCI->swapOperands(); // Swap the LHS and RHS of the SetCC |
| 861 | } |
| 862 | |
| 863 | unsigned LHSCode = getSetCondCode(SCI); |
| 864 | unsigned RHSCode = getSetCondCode(cast<SetCondInst>(Log.getOperand(1))); |
| 865 | unsigned Code; |
| 866 | switch (Log.getOpcode()) { |
| 867 | case Instruction::And: Code = LHSCode & RHSCode; break; |
| 868 | case Instruction::Or: Code = LHSCode | RHSCode; break; |
| 869 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; |
Chris Lattner | 2caaaba | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 870 | default: assert(0 && "Illegal logical opcode!"); return 0; |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | Value *RV = getSetCCValue(Code, LHS, RHS); |
| 874 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 875 | return I; |
| 876 | // Otherwise, it's a constant boolean value... |
| 877 | return IC.ReplaceInstUsesWith(Log, RV); |
| 878 | } |
| 879 | }; |
| 880 | |
| 881 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 882 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
| 883 | // the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'. Op is |
| 884 | // guaranteed to be either a shift instruction or a binary operator. |
| 885 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
| 886 | ConstantIntegral *OpRHS, |
| 887 | ConstantIntegral *AndRHS, |
| 888 | BinaryOperator &TheAnd) { |
| 889 | Value *X = Op->getOperand(0); |
Chris Lattner | fcf21a7 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 890 | Constant *Together = 0; |
| 891 | if (!isa<ShiftInst>(Op)) |
| 892 | Together = ConstantExpr::get(Instruction::And, AndRHS, OpRHS); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 893 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 894 | switch (Op->getOpcode()) { |
| 895 | case Instruction::Xor: |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 896 | if (Together->isNullValue()) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 897 | // (X ^ C1) & C2 --> (X & C2) iff (C1&C2) == 0 |
| 898 | return BinaryOperator::create(Instruction::And, X, AndRHS); |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 899 | } else if (Op->hasOneUse()) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 900 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
| 901 | std::string OpName = Op->getName(); Op->setName(""); |
| 902 | Instruction *And = BinaryOperator::create(Instruction::And, |
| 903 | X, AndRHS, OpName); |
| 904 | InsertNewInstBefore(And, TheAnd); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 905 | return BinaryOperator::create(Instruction::Xor, And, Together); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 906 | } |
| 907 | break; |
| 908 | case Instruction::Or: |
| 909 | // (X | C1) & C2 --> X & C2 iff C1 & C1 == 0 |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 910 | if (Together->isNullValue()) |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 911 | return BinaryOperator::create(Instruction::And, X, AndRHS); |
| 912 | else { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 913 | if (Together == AndRHS) // (X | C) & C --> C |
| 914 | return ReplaceInstUsesWith(TheAnd, AndRHS); |
| 915 | |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 916 | if (Op->hasOneUse() && Together != OpRHS) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 917 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 |
| 918 | std::string Op0Name = Op->getName(); Op->setName(""); |
| 919 | Instruction *Or = BinaryOperator::create(Instruction::Or, X, |
| 920 | Together, Op0Name); |
| 921 | InsertNewInstBefore(Or, TheAnd); |
| 922 | return BinaryOperator::create(Instruction::And, Or, AndRHS); |
| 923 | } |
| 924 | } |
| 925 | break; |
| 926 | case Instruction::Add: |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 927 | if (Op->hasOneUse()) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 928 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 929 | // of the bit. First thing to check is to see if this AND is with a |
| 930 | // single bit constant. |
| 931 | unsigned long long AndRHSV = cast<ConstantInt>(AndRHS)->getRawValue(); |
| 932 | |
| 933 | // Clear bits that are not part of the constant. |
| 934 | AndRHSV &= (1ULL << AndRHS->getType()->getPrimitiveSize()*8)-1; |
| 935 | |
| 936 | // If there is only one bit set... |
| 937 | if ((AndRHSV & (AndRHSV-1)) == 0) { |
| 938 | // Ok, at this point, we know that we are masking the result of the |
| 939 | // ADD down to exactly one bit. If the constant we are adding has |
| 940 | // no bits set below this bit, then we can eliminate the ADD. |
| 941 | unsigned long long AddRHS = cast<ConstantInt>(OpRHS)->getRawValue(); |
| 942 | |
| 943 | // Check to see if any bits below the one bit set in AndRHSV are set. |
| 944 | if ((AddRHS & (AndRHSV-1)) == 0) { |
| 945 | // If not, the only thing that can effect the output of the AND is |
| 946 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 947 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 948 | // no effect. |
| 949 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop |
| 950 | TheAnd.setOperand(0, X); |
| 951 | return &TheAnd; |
| 952 | } else { |
| 953 | std::string Name = Op->getName(); Op->setName(""); |
| 954 | // Pull the XOR out of the AND. |
| 955 | Instruction *NewAnd = |
| 956 | BinaryOperator::create(Instruction::And, X, AndRHS, Name); |
| 957 | InsertNewInstBefore(NewAnd, TheAnd); |
| 958 | return BinaryOperator::create(Instruction::Xor, NewAnd, AndRHS); |
| 959 | } |
| 960 | } |
| 961 | } |
| 962 | } |
| 963 | break; |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 964 | |
| 965 | case Instruction::Shl: { |
| 966 | // We know that the AND will not produce any of the bits shifted in, so if |
| 967 | // the anded constant includes them, clear them now! |
| 968 | // |
| 969 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 970 | Constant *CI = ConstantExpr::get(Instruction::And, AndRHS, |
| 971 | ConstantExpr::get(Instruction::Shl, AllOne, OpRHS)); |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 972 | if (CI != AndRHS) { |
| 973 | TheAnd.setOperand(1, CI); |
| 974 | return &TheAnd; |
| 975 | } |
| 976 | break; |
| 977 | } |
| 978 | case Instruction::Shr: |
| 979 | // We know that the AND will not produce any of the bits shifted in, so if |
| 980 | // the anded constant includes them, clear them now! This only applies to |
| 981 | // unsigned shifts, because a signed shr may bring in set bits! |
| 982 | // |
| 983 | if (AndRHS->getType()->isUnsigned()) { |
| 984 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 985 | Constant *CI = ConstantExpr::get(Instruction::And, AndRHS, |
| 986 | ConstantExpr::get(Instruction::Shr, AllOne, OpRHS)); |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 987 | if (CI != AndRHS) { |
| 988 | TheAnd.setOperand(1, CI); |
| 989 | return &TheAnd; |
| 990 | } |
| 991 | } |
| 992 | break; |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 993 | } |
| 994 | return 0; |
| 995 | } |
| 996 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 997 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 998 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 999 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1000 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1001 | |
| 1002 | // and X, X = X and X, 0 == 0 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1003 | if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) |
| 1004 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1005 | |
| 1006 | // and X, -1 == X |
Chris Lattner | 49b47ae | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 1007 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1008 | if (RHS->isAllOnesValue()) |
| 1009 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1010 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1011 | // Optimize a variety of ((val OP C1) & C2) combinations... |
| 1012 | if (isa<BinaryOperator>(Op0) || isa<ShiftInst>(Op0)) { |
| 1013 | Instruction *Op0I = cast<Instruction>(Op0); |
Chris Lattner | 33217db | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 1014 | Value *X = Op0I->getOperand(0); |
Chris Lattner | 16464b3 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 1015 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1016 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, RHS, I)) |
| 1017 | return Res; |
Chris Lattner | 33217db | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 1018 | } |
Chris Lattner | 49b47ae | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1021 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 1022 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1023 | |
| 1024 | // (~A & ~B) == (~(A | B)) - Demorgan's Law |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1025 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1026 | Instruction *Or = BinaryOperator::create(Instruction::Or, Op0NotVal, |
Chris Lattner | 49b47ae | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 1027 | Op1NotVal,I.getName()+".demorgan"); |
| 1028 | InsertNewInstBefore(Or, I); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1029 | return BinaryOperator::createNot(Or); |
| 1030 | } |
| 1031 | |
| 1032 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
| 1033 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 65217ff | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 1034 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1035 | // (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B) |
| 1036 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1))) |
| 1037 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 1038 | return R; |
| 1039 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1040 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | |
| 1044 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1045 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1046 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1047 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1048 | |
| 1049 | // or X, X = X or X, 0 == X |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1050 | if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) |
| 1051 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1052 | |
| 1053 | // or X, -1 == -1 |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1054 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1055 | if (RHS->isAllOnesValue()) |
| 1056 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1057 | |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1058 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { |
| 1059 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
| 1060 | if (Op0I->getOpcode() == Instruction::And && isOnlyUse(Op0)) |
| 1061 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) { |
| 1062 | std::string Op0Name = Op0I->getName(); Op0I->setName(""); |
| 1063 | Instruction *Or = BinaryOperator::create(Instruction::Or, |
| 1064 | Op0I->getOperand(0), RHS, |
| 1065 | Op0Name); |
| 1066 | InsertNewInstBefore(Or, I); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1067 | return BinaryOperator::create(Instruction::And, Or, |
| 1068 | ConstantExpr::get(Instruction::Or, RHS, Op0CI)); |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
| 1072 | if (Op0I->getOpcode() == Instruction::Xor && isOnlyUse(Op0)) |
| 1073 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) { |
| 1074 | std::string Op0Name = Op0I->getName(); Op0I->setName(""); |
| 1075 | Instruction *Or = BinaryOperator::create(Instruction::Or, |
| 1076 | Op0I->getOperand(0), RHS, |
| 1077 | Op0Name); |
| 1078 | InsertNewInstBefore(Or, I); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1079 | return BinaryOperator::create(Instruction::Xor, Or, |
| 1080 | ConstantExpr::get(Instruction::And, Op0CI, |
| 1081 | NotConstant(RHS))); |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1082 | } |
| 1083 | } |
| 1084 | } |
| 1085 | |
Chris Lattner | 812aab7 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 1086 | // (A & C1)|(A & C2) == A & (C1|C2) |
Chris Lattner | 01d5639 | 2003-08-12 19:17:27 +0000 | [diff] [blame] | 1087 | if (Instruction *LHS = dyn_cast<BinaryOperator>(Op0)) |
| 1088 | if (Instruction *RHS = dyn_cast<BinaryOperator>(Op1)) |
| 1089 | if (LHS->getOperand(0) == RHS->getOperand(0)) |
| 1090 | if (Constant *C0 = dyn_castMaskingAnd(LHS)) |
| 1091 | if (Constant *C1 = dyn_castMaskingAnd(RHS)) |
| 1092 | return BinaryOperator::create(Instruction::And, LHS->getOperand(0), |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1093 | ConstantExpr::get(Instruction::Or, C0, C1)); |
Chris Lattner | 812aab7 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 1094 | |
Chris Lattner | 3e327a4 | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 1095 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 1096 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1097 | |
Chris Lattner | 3e327a4 | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 1098 | if (Op1 == Op0NotVal) // ~A | A == -1 |
| 1099 | return ReplaceInstUsesWith(I, |
| 1100 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 1101 | |
| 1102 | if (Op0 == Op1NotVal) // A | ~A == -1 |
| 1103 | return ReplaceInstUsesWith(I, |
| 1104 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 1105 | |
| 1106 | // (~A | ~B) == (~(A & B)) - Demorgan's Law |
| 1107 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
| 1108 | Instruction *And = BinaryOperator::create(Instruction::And, Op0NotVal, |
| 1109 | Op1NotVal,I.getName()+".demorgan", |
| 1110 | &I); |
| 1111 | WorkList.push_back(And); |
| 1112 | return BinaryOperator::createNot(And); |
| 1113 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1114 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1115 | // (setcc1 A, B) | (setcc2 A, B) --> (setcc3 A, B) |
| 1116 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1))) |
| 1117 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 1118 | return R; |
| 1119 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1120 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 1123 | // XorSelf - Implements: X ^ X --> 0 |
| 1124 | struct XorSelf { |
| 1125 | Value *RHS; |
| 1126 | XorSelf(Value *rhs) : RHS(rhs) {} |
| 1127 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 1128 | Instruction *apply(BinaryOperator &Xor) const { |
| 1129 | return &Xor; |
| 1130 | } |
| 1131 | }; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1132 | |
| 1133 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1134 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1135 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1136 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1137 | |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 1138 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
| 1139 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { |
| 1140 | assert(Result == &I && "AssociativeOpt didn't work?"); |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1141 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 1142 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1143 | |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1144 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1145 | // xor X, 0 == X |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1146 | if (RHS->isNullValue()) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1147 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1148 | |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1149 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 1150 | // xor (setcc A, B), true = not (setcc A, B) = setncc A, B |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1151 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(Op0I)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1152 | if (RHS == ConstantBool::True && SCI->hasOneUse()) |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 1153 | return new SetCondInst(SCI->getInverseCondition(), |
| 1154 | SCI->getOperand(0), SCI->getOperand(1)); |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 1155 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 1156 | // ~(c-X) == X-c-1 == X+(-c-1) |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1157 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
| 1158 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { |
| 1159 | Constant *NegOp0I0C = ConstantExpr::get(Instruction::Sub, |
| 1160 | Constant::getNullValue(Op0I0C->getType()), Op0I0C); |
| 1161 | Constant *ConstantRHS = ConstantExpr::get(Instruction::Sub, NegOp0I0C, |
| 1162 | ConstantInt::get(I.getType(), 1)); |
| 1163 | return BinaryOperator::create(Instruction::Add, Op0I->getOperand(1), |
| 1164 | ConstantRHS); |
| 1165 | } |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1166 | |
| 1167 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 1168 | switch (Op0I->getOpcode()) { |
| 1169 | case Instruction::Add: |
Chris Lattner | 0f68fa6 | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 1170 | // ~(X-c) --> (-c-1)-X |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1171 | if (RHS->isAllOnesValue()) { |
| 1172 | Constant *NegOp0CI = ConstantExpr::get(Instruction::Sub, |
| 1173 | Constant::getNullValue(Op0CI->getType()), Op0CI); |
Chris Lattner | 0f68fa6 | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 1174 | return BinaryOperator::create(Instruction::Sub, |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1175 | ConstantExpr::get(Instruction::Sub, NegOp0CI, |
| 1176 | ConstantInt::get(I.getType(), 1)), |
Chris Lattner | 0f68fa6 | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 1177 | Op0I->getOperand(0)); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1178 | } |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 1179 | break; |
| 1180 | case Instruction::And: |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1181 | // (X & C1) ^ C2 --> (X & C1) | C2 iff (C1&C2) == 0 |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1182 | if (ConstantExpr::get(Instruction::And, RHS, Op0CI)->isNullValue()) |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1183 | return BinaryOperator::create(Instruction::Or, Op0, RHS); |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 1184 | break; |
| 1185 | case Instruction::Or: |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1186 | // (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1187 | if (ConstantExpr::get(Instruction::And, RHS, Op0CI) == RHS) |
| 1188 | return BinaryOperator::create(Instruction::And, Op0, |
| 1189 | NotConstant(RHS)); |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 1190 | break; |
| 1191 | default: break; |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1192 | } |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 1193 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1196 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1197 | if (X == Op1) |
| 1198 | return ReplaceInstUsesWith(I, |
| 1199 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 1200 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1201 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1202 | if (X == Op0) |
| 1203 | return ReplaceInstUsesWith(I, |
| 1204 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 1205 | |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1206 | if (Instruction *Op1I = dyn_cast<Instruction>(Op1)) |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 1207 | if (Op1I->getOpcode() == Instruction::Or) { |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1208 | if (Op1I->getOperand(0) == Op0) { // B^(B|A) == (A|B)^B |
| 1209 | cast<BinaryOperator>(Op1I)->swapOperands(); |
| 1210 | I.swapOperands(); |
| 1211 | std::swap(Op0, Op1); |
| 1212 | } else if (Op1I->getOperand(1) == Op0) { // B^(A|B) == (A|B)^B |
| 1213 | I.swapOperands(); |
| 1214 | std::swap(Op0, Op1); |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 1215 | } |
| 1216 | } else if (Op1I->getOpcode() == Instruction::Xor) { |
| 1217 | if (Op0 == Op1I->getOperand(0)) // A^(A^B) == B |
| 1218 | return ReplaceInstUsesWith(I, Op1I->getOperand(1)); |
| 1219 | else if (Op0 == Op1I->getOperand(1)) // A^(B^A) == B |
| 1220 | return ReplaceInstUsesWith(I, Op1I->getOperand(0)); |
| 1221 | } |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1222 | |
| 1223 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1224 | if (Op0I->getOpcode() == Instruction::Or && Op0I->hasOneUse()) { |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1225 | if (Op0I->getOperand(0) == Op1) // (B|A)^B == (A|B)^B |
| 1226 | cast<BinaryOperator>(Op0I)->swapOperands(); |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1227 | if (Op0I->getOperand(1) == Op1) { // (A|B)^B == A & ~B |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1228 | Value *NotB = BinaryOperator::createNot(Op1, Op1->getName()+".not", &I); |
| 1229 | WorkList.push_back(cast<Instruction>(NotB)); |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1230 | return BinaryOperator::create(Instruction::And, Op0I->getOperand(0), |
| 1231 | NotB); |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1232 | } |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 1233 | } else if (Op0I->getOpcode() == Instruction::Xor) { |
| 1234 | if (Op1 == Op0I->getOperand(0)) // (A^B)^A == B |
| 1235 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 1236 | else if (Op1 == Op0I->getOperand(1)) // (B^A)^A == B |
| 1237 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 1240 | // (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1^C2 == 0 |
| 1241 | if (Constant *C1 = dyn_castMaskingAnd(Op0)) |
| 1242 | if (Constant *C2 = dyn_castMaskingAnd(Op1)) |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 1243 | if (ConstantExpr::get(Instruction::And, C1, C2)->isNullValue()) |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 1244 | return BinaryOperator::create(Instruction::Or, Op0, Op1); |
| 1245 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1246 | // (setcc1 A, B) ^ (setcc2 A, B) --> (setcc3 A, B) |
| 1247 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1))) |
| 1248 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 1249 | return R; |
| 1250 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1251 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1254 | // AddOne, SubOne - Add or subtract a constant one from an integer constant... |
| 1255 | static Constant *AddOne(ConstantInt *C) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 1256 | Constant *Result = ConstantExpr::get(Instruction::Add, C, |
| 1257 | ConstantInt::get(C->getType(), 1)); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1258 | assert(Result && "Constant folding integer addition failed!"); |
| 1259 | return Result; |
| 1260 | } |
| 1261 | static Constant *SubOne(ConstantInt *C) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 1262 | Constant *Result = ConstantExpr::get(Instruction::Sub, C, |
| 1263 | ConstantInt::get(C->getType(), 1)); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1264 | assert(Result && "Constant folding integer addition failed!"); |
| 1265 | return Result; |
| 1266 | } |
| 1267 | |
Chris Lattner | 1fc23f3 | 2002-05-09 20:11:54 +0000 | [diff] [blame] | 1268 | // isTrueWhenEqual - Return true if the specified setcondinst instruction is |
| 1269 | // true when both operands are equal... |
| 1270 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1271 | static bool isTrueWhenEqual(Instruction &I) { |
| 1272 | return I.getOpcode() == Instruction::SetEQ || |
| 1273 | I.getOpcode() == Instruction::SetGE || |
| 1274 | I.getOpcode() == Instruction::SetLE; |
Chris Lattner | 1fc23f3 | 2002-05-09 20:11:54 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1277 | Instruction *InstCombiner::visitSetCondInst(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1278 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1279 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1280 | const Type *Ty = Op0->getType(); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1281 | |
| 1282 | // setcc X, X |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1283 | if (Op0 == Op1) |
| 1284 | return ReplaceInstUsesWith(I, ConstantBool::get(isTrueWhenEqual(I))); |
Chris Lattner | 1fc23f3 | 2002-05-09 20:11:54 +0000 | [diff] [blame] | 1285 | |
Chris Lattner | d07283a | 2003-08-13 05:38:46 +0000 | [diff] [blame] | 1286 | // setcc <global/alloca*>, 0 - Global/Stack value addresses are never null! |
| 1287 | if (isa<ConstantPointerNull>(Op1) && |
| 1288 | (isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0))) |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1289 | return ReplaceInstUsesWith(I, ConstantBool::get(!isTrueWhenEqual(I))); |
| 1290 | |
Chris Lattner | d07283a | 2003-08-13 05:38:46 +0000 | [diff] [blame] | 1291 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1292 | // setcc's with boolean values can always be turned into bitwise operations |
| 1293 | if (Ty == Type::BoolTy) { |
| 1294 | // If this is <, >, or !=, we can change this into a simple xor instruction |
| 1295 | if (!isTrueWhenEqual(I)) |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 1296 | return BinaryOperator::create(Instruction::Xor, Op0, Op1); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1297 | |
| 1298 | // Otherwise we need to make a temporary intermediate instruction and insert |
| 1299 | // it into the instruction stream. This is what we are after: |
| 1300 | // |
| 1301 | // seteq bool %A, %B -> ~(A^B) |
| 1302 | // setle bool %A, %B -> ~A | B |
| 1303 | // setge bool %A, %B -> A | ~B |
| 1304 | // |
| 1305 | if (I.getOpcode() == Instruction::SetEQ) { // seteq case |
| 1306 | Instruction *Xor = BinaryOperator::create(Instruction::Xor, Op0, Op1, |
| 1307 | I.getName()+"tmp"); |
| 1308 | InsertNewInstBefore(Xor, I); |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 1309 | return BinaryOperator::createNot(Xor); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | // Handle the setXe cases... |
| 1313 | assert(I.getOpcode() == Instruction::SetGE || |
| 1314 | I.getOpcode() == Instruction::SetLE); |
| 1315 | |
| 1316 | if (I.getOpcode() == Instruction::SetGE) |
| 1317 | std::swap(Op0, Op1); // Change setge -> setle |
| 1318 | |
| 1319 | // Now we just have the SetLE case. |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 1320 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1321 | InsertNewInstBefore(Not, I); |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 1322 | return BinaryOperator::create(Instruction::Or, Not, Op1); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | // Check to see if we are doing one of many comparisons against constant |
| 1326 | // integers at the end of their ranges... |
| 1327 | // |
| 1328 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 1329 | // Simplify seteq and setne instructions... |
| 1330 | if (I.getOpcode() == Instruction::SetEQ || |
| 1331 | I.getOpcode() == Instruction::SetNE) { |
| 1332 | bool isSetNE = I.getOpcode() == Instruction::SetNE; |
| 1333 | |
Chris Lattner | cfbce7c | 2003-07-23 17:26:36 +0000 | [diff] [blame] | 1334 | // 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] | 1335 | // operand is a constant, simplify a bit. |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 1336 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) { |
| 1337 | switch (BO->getOpcode()) { |
| 1338 | case Instruction::Add: |
| 1339 | if (CI->isNullValue()) { |
| 1340 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 1341 | // efficiently invertible, or if the add has just this one use. |
| 1342 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
| 1343 | if (Value *NegVal = dyn_castNegVal(BOp1)) |
| 1344 | return new SetCondInst(I.getOpcode(), BOp0, NegVal); |
| 1345 | else if (Value *NegVal = dyn_castNegVal(BOp0)) |
| 1346 | return new SetCondInst(I.getOpcode(), NegVal, BOp1); |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1347 | else if (BO->hasOneUse()) { |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 1348 | Instruction *Neg = BinaryOperator::createNeg(BOp1, BO->getName()); |
| 1349 | BO->setName(""); |
| 1350 | InsertNewInstBefore(Neg, I); |
| 1351 | return new SetCondInst(I.getOpcode(), BOp0, Neg); |
| 1352 | } |
| 1353 | } |
| 1354 | break; |
| 1355 | case Instruction::Xor: |
| 1356 | // For the xor case, we can xor two constants together, eliminating |
| 1357 | // the explicit xor. |
| 1358 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) |
| 1359 | return BinaryOperator::create(I.getOpcode(), BO->getOperand(0), |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1360 | ConstantExpr::get(Instruction::Xor, CI, BOC)); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 1361 | |
| 1362 | // FALLTHROUGH |
| 1363 | case Instruction::Sub: |
| 1364 | // Replace (([sub|xor] A, B) != 0) with (A != B) |
| 1365 | if (CI->isNullValue()) |
| 1366 | return new SetCondInst(I.getOpcode(), BO->getOperand(0), |
| 1367 | BO->getOperand(1)); |
| 1368 | break; |
| 1369 | |
| 1370 | case Instruction::Or: |
| 1371 | // If bits are being or'd in that are not present in the constant we |
| 1372 | // are comparing against, then the comparison could never succeed! |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1373 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { |
| 1374 | Constant *NotCI = NotConstant(CI); |
| 1375 | if (!ConstantExpr::get(Instruction::And, BOC, NotCI)->isNullValue()) |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 1376 | return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE)); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1377 | } |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 1378 | break; |
| 1379 | |
| 1380 | case Instruction::And: |
| 1381 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 1382 | // If bits are being compared against that are and'd out, then the |
| 1383 | // comparison can never succeed! |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1384 | if (!ConstantExpr::get(Instruction::And, CI, |
| 1385 | NotConstant(BOC))->isNullValue()) |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 1386 | return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE)); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 1387 | |
| 1388 | // Replace (and X, (1 << size(X)-1) != 0) with x < 0, converting X |
| 1389 | // to be a signed value as appropriate. |
| 1390 | if (isSignBit(BOC)) { |
| 1391 | Value *X = BO->getOperand(0); |
| 1392 | // If 'X' is not signed, insert a cast now... |
| 1393 | if (!BOC->getType()->isSigned()) { |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 1394 | const Type *DestTy = getSignedIntegralType(BOC->getType()); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 1395 | CastInst *NewCI = new CastInst(X,DestTy,X->getName()+".signed"); |
| 1396 | InsertNewInstBefore(NewCI, I); |
| 1397 | X = NewCI; |
| 1398 | } |
| 1399 | return new SetCondInst(isSetNE ? Instruction::SetLT : |
| 1400 | Instruction::SetGE, X, |
| 1401 | Constant::getNullValue(X->getType())); |
| 1402 | } |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 1403 | } |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 1404 | default: break; |
| 1405 | } |
| 1406 | } |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 1407 | } else { // Not a SetEQ/SetNE |
| 1408 | // If the LHS is a cast from an integral value of the same size, |
| 1409 | if (CastInst *Cast = dyn_cast<CastInst>(Op0)) { |
| 1410 | Value *CastOp = Cast->getOperand(0); |
| 1411 | const Type *SrcTy = CastOp->getType(); |
| 1412 | unsigned SrcTySize = SrcTy->getPrimitiveSize(); |
| 1413 | if (SrcTy != Cast->getType() && SrcTy->isInteger() && |
| 1414 | SrcTySize == Cast->getType()->getPrimitiveSize()) { |
| 1415 | assert((SrcTy->isSigned() ^ Cast->getType()->isSigned()) && |
| 1416 | "Source and destination signednesses should differ!"); |
| 1417 | if (Cast->getType()->isSigned()) { |
| 1418 | // If this is a signed comparison, check for comparisons in the |
| 1419 | // vicinity of zero. |
| 1420 | if (I.getOpcode() == Instruction::SetLT && CI->isNullValue()) |
| 1421 | // X < 0 => x > 127 |
| 1422 | return BinaryOperator::create(Instruction::SetGT, CastOp, |
| 1423 | ConstantUInt::get(SrcTy, (1ULL << (SrcTySize*8-1))-1)); |
| 1424 | else if (I.getOpcode() == Instruction::SetGT && |
| 1425 | cast<ConstantSInt>(CI)->getValue() == -1) |
| 1426 | // X > -1 => x < 128 |
Chris Lattner | c40b9d7 | 2004-02-23 21:46:42 +0000 | [diff] [blame] | 1427 | return BinaryOperator::create(Instruction::SetLT, CastOp, |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 1428 | ConstantUInt::get(SrcTy, 1ULL << (SrcTySize*8-1))); |
| 1429 | } else { |
| 1430 | ConstantUInt *CUI = cast<ConstantUInt>(CI); |
| 1431 | if (I.getOpcode() == Instruction::SetLT && |
| 1432 | CUI->getValue() == 1ULL << (SrcTySize*8-1)) |
| 1433 | // X < 128 => X > -1 |
| 1434 | return BinaryOperator::create(Instruction::SetGT, CastOp, |
| 1435 | ConstantSInt::get(SrcTy, -1)); |
| 1436 | else if (I.getOpcode() == Instruction::SetGT && |
| 1437 | CUI->getValue() == (1ULL << (SrcTySize*8-1))-1) |
| 1438 | // X > 127 => X < 0 |
| 1439 | return BinaryOperator::create(Instruction::SetLT, CastOp, |
| 1440 | Constant::getNullValue(SrcTy)); |
| 1441 | } |
| 1442 | } |
| 1443 | } |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 1444 | } |
Chris Lattner | 791ac1a | 2003-06-01 03:35:25 +0000 | [diff] [blame] | 1445 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1446 | // 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] | 1447 | if (CI->isMinValue()) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1448 | if (I.getOpcode() == Instruction::SetLT) // A < MIN -> FALSE |
| 1449 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1450 | if (I.getOpcode() == Instruction::SetGE) // A >= MIN -> TRUE |
| 1451 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 1452 | if (I.getOpcode() == Instruction::SetLE) // A <= MIN -> A == MIN |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 1453 | return BinaryOperator::create(Instruction::SetEQ, Op0, Op1); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1454 | if (I.getOpcode() == Instruction::SetGT) // A > MIN -> A != MIN |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 1455 | return BinaryOperator::create(Instruction::SetNE, Op0, Op1); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1456 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1457 | } else if (CI->isMaxValue()) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1458 | if (I.getOpcode() == Instruction::SetGT) // A > MAX -> FALSE |
| 1459 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1460 | if (I.getOpcode() == Instruction::SetLE) // A <= MAX -> TRUE |
| 1461 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 1462 | if (I.getOpcode() == Instruction::SetGE) // A >= MAX -> A == MAX |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 1463 | return BinaryOperator::create(Instruction::SetEQ, Op0, Op1); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1464 | if (I.getOpcode() == Instruction::SetLT) // A < MAX -> A != MAX |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 1465 | return BinaryOperator::create(Instruction::SetNE, Op0, Op1); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1466 | |
| 1467 | // Comparing against a value really close to min or max? |
| 1468 | } else if (isMinValuePlusOne(CI)) { |
| 1469 | if (I.getOpcode() == Instruction::SetLT) // A < MIN+1 -> A == MIN |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 1470 | return BinaryOperator::create(Instruction::SetEQ, Op0, SubOne(CI)); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1471 | if (I.getOpcode() == Instruction::SetGE) // A >= MIN-1 -> A != MIN |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 1472 | return BinaryOperator::create(Instruction::SetNE, Op0, SubOne(CI)); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1473 | |
| 1474 | } else if (isMaxValueMinusOne(CI)) { |
| 1475 | if (I.getOpcode() == Instruction::SetGT) // A > MAX-1 -> A == MAX |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 1476 | return BinaryOperator::create(Instruction::SetEQ, Op0, AddOne(CI)); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1477 | if (I.getOpcode() == Instruction::SetLE) // A <= MAX-1 -> A != MAX |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 1478 | return BinaryOperator::create(Instruction::SetNE, Op0, AddOne(CI)); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1479 | } |
Chris Lattner | 5961114 | 2004-02-23 05:47:48 +0000 | [diff] [blame] | 1480 | |
| 1481 | // If we still have a setle or setge instruction, turn it into the |
| 1482 | // appropriate setlt or setgt instruction. Since the border cases have |
| 1483 | // already been handled above, this requires little checking. |
| 1484 | // |
| 1485 | if (I.getOpcode() == Instruction::SetLE) |
| 1486 | return BinaryOperator::create(Instruction::SetLT, Op0, AddOne(CI)); |
| 1487 | if (I.getOpcode() == Instruction::SetGE) |
| 1488 | return BinaryOperator::create(Instruction::SetGT, Op0, SubOne(CI)); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 1491 | // Test to see if the operands of the setcc are casted versions of other |
| 1492 | // values. If the cast can be stripped off both arguments, we do so now. |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 1493 | if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
| 1494 | Value *CastOp0 = CI->getOperand(0); |
| 1495 | if (CastOp0->getType()->isLosslesslyConvertibleTo(CI->getType()) && |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 1496 | !isa<Argument>(Op1) && |
| 1497 | (I.getOpcode() == Instruction::SetEQ || |
| 1498 | I.getOpcode() == Instruction::SetNE)) { |
| 1499 | // We keep moving the cast from the left operand over to the right |
| 1500 | // operand, where it can often be eliminated completely. |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 1501 | Op0 = CastOp0; |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 1502 | |
| 1503 | // If operand #1 is a cast instruction, see if we can eliminate it as |
| 1504 | // well. |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 1505 | if (CastInst *CI2 = dyn_cast<CastInst>(Op1)) |
| 1506 | if (CI2->getOperand(0)->getType()->isLosslesslyConvertibleTo( |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 1507 | Op0->getType())) |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 1508 | Op1 = CI2->getOperand(0); |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 1509 | |
| 1510 | // If Op1 is a constant, we can fold the cast into the constant. |
| 1511 | if (Op1->getType() != Op0->getType()) |
| 1512 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
| 1513 | Op1 = ConstantExpr::getCast(Op1C, Op0->getType()); |
| 1514 | } else { |
| 1515 | // Otherwise, cast the RHS right before the setcc |
| 1516 | Op1 = new CastInst(Op1, Op0->getType(), Op1->getName()); |
| 1517 | InsertNewInstBefore(cast<Instruction>(Op1), I); |
| 1518 | } |
| 1519 | return BinaryOperator::create(I.getOpcode(), Op0, Op1); |
| 1520 | } |
| 1521 | |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 1522 | // Handle the special case of: setcc (cast bool to X), <cst> |
| 1523 | // This comes up when you have code like |
| 1524 | // int X = A < B; |
| 1525 | // if (X) ... |
| 1526 | // For generality, we handle any zero-extension of any operand comparison |
| 1527 | // with a constant. |
| 1528 | if (ConstantInt *ConstantRHS = dyn_cast<ConstantInt>(Op1)) { |
| 1529 | const Type *SrcTy = CastOp0->getType(); |
| 1530 | const Type *DestTy = Op0->getType(); |
| 1531 | if (SrcTy->getPrimitiveSize() < DestTy->getPrimitiveSize() && |
| 1532 | (SrcTy->isUnsigned() || SrcTy == Type::BoolTy)) { |
| 1533 | // Ok, we have an expansion of operand 0 into a new type. Get the |
| 1534 | // constant value, masink off bits which are not set in the RHS. These |
| 1535 | // could be set if the destination value is signed. |
| 1536 | uint64_t ConstVal = ConstantRHS->getRawValue(); |
| 1537 | ConstVal &= (1ULL << DestTy->getPrimitiveSize()*8)-1; |
| 1538 | |
| 1539 | // If the constant we are comparing it with has high bits set, which |
| 1540 | // don't exist in the original value, the values could never be equal, |
| 1541 | // because the source would be zero extended. |
| 1542 | unsigned SrcBits = |
| 1543 | SrcTy == Type::BoolTy ? 1 : SrcTy->getPrimitiveSize()*8; |
Chris Lattner | 7c94d11 | 2003-11-05 17:31:36 +0000 | [diff] [blame] | 1544 | bool HasSignBit = ConstVal & (1ULL << (DestTy->getPrimitiveSize()*8-1)); |
| 1545 | if (ConstVal & ~((1ULL << SrcBits)-1)) { |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 1546 | switch (I.getOpcode()) { |
| 1547 | default: assert(0 && "Unknown comparison type!"); |
| 1548 | case Instruction::SetEQ: |
| 1549 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1550 | case Instruction::SetNE: |
| 1551 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 1552 | case Instruction::SetLT: |
| 1553 | case Instruction::SetLE: |
| 1554 | if (DestTy->isSigned() && HasSignBit) |
| 1555 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1556 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 1557 | case Instruction::SetGT: |
| 1558 | case Instruction::SetGE: |
| 1559 | if (DestTy->isSigned() && HasSignBit) |
| 1560 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 1561 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | // Otherwise, we can replace the setcc with a setcc of the smaller |
| 1566 | // operand value. |
| 1567 | Op1 = ConstantExpr::getCast(cast<Constant>(Op1), SrcTy); |
| 1568 | return BinaryOperator::create(I.getOpcode(), CastOp0, Op1); |
| 1569 | } |
| 1570 | } |
| 1571 | } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1572 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | |
| 1576 | |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 1577 | Instruction *InstCombiner::visitShiftInst(ShiftInst &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1578 | assert(I.getOperand(1)->getType() == Type::UByteTy); |
| 1579 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 1580 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1581 | |
| 1582 | // shl X, 0 == X and shr X, 0 == X |
| 1583 | // shl 0, X == 0 and shr 0, X == 0 |
| 1584 | if (Op1 == Constant::getNullValue(Type::UByteTy) || |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1585 | Op0 == Constant::getNullValue(Op0->getType())) |
| 1586 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1587 | |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 1588 | // shr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 1589 | if (!isLeftShift) |
| 1590 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0)) |
| 1591 | if (CSI->isAllOnesValue()) |
| 1592 | return ReplaceInstUsesWith(I, CSI); |
| 1593 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1594 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) { |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 1595 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr |
| 1596 | // of a signed value. |
| 1597 | // |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 1598 | unsigned TypeBits = Op0->getType()->getPrimitiveSize()*8; |
Chris Lattner | f5ce254 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 1599 | if (CUI->getValue() >= TypeBits) { |
| 1600 | if (!Op0->getType()->isSigned() || isLeftShift) |
| 1601 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
| 1602 | else { |
| 1603 | I.setOperand(1, ConstantUInt::get(Type::UByteTy, TypeBits-1)); |
| 1604 | return &I; |
| 1605 | } |
| 1606 | } |
Chris Lattner | 55f3d94 | 2002-09-10 23:04:09 +0000 | [diff] [blame] | 1607 | |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 1608 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 1609 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 1610 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 1611 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
| 1612 | return BinaryOperator::create(Instruction::Mul, BO->getOperand(0), |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1613 | ConstantExpr::get(Instruction::Shl, BOOp, CUI)); |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 1614 | |
| 1615 | |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 1616 | // If the operand is an bitwise operator with a constant RHS, and the |
| 1617 | // shift is the only use, we can pull it out of the shift. |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1618 | if (Op0->hasOneUse()) |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 1619 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) |
| 1620 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 1621 | bool isValid = true; // Valid only for And, Or, Xor |
| 1622 | bool highBitSet = false; // Transform if high bit of constant set? |
| 1623 | |
| 1624 | switch (Op0BO->getOpcode()) { |
| 1625 | default: isValid = false; break; // Do not perform transform! |
| 1626 | case Instruction::Or: |
| 1627 | case Instruction::Xor: |
| 1628 | highBitSet = false; |
| 1629 | break; |
| 1630 | case Instruction::And: |
| 1631 | highBitSet = true; |
| 1632 | break; |
| 1633 | } |
| 1634 | |
| 1635 | // If this is a signed shift right, and the high bit is modified |
| 1636 | // by the logical operation, do not perform the transformation. |
| 1637 | // The highBitSet boolean indicates the value of the high bit of |
| 1638 | // the constant which would cause it to be modified for this |
| 1639 | // operation. |
| 1640 | // |
| 1641 | if (isValid && !isLeftShift && !I.getType()->isUnsigned()) { |
| 1642 | uint64_t Val = Op0C->getRawValue(); |
| 1643 | isValid = ((Val & (1 << (TypeBits-1))) != 0) == highBitSet; |
| 1644 | } |
| 1645 | |
| 1646 | if (isValid) { |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1647 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, CUI); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 1648 | |
| 1649 | Instruction *NewShift = |
| 1650 | new ShiftInst(I.getOpcode(), Op0BO->getOperand(0), CUI, |
| 1651 | Op0BO->getName()); |
| 1652 | Op0BO->setName(""); |
| 1653 | InsertNewInstBefore(NewShift, I); |
| 1654 | |
| 1655 | return BinaryOperator::create(Op0BO->getOpcode(), NewShift, |
| 1656 | NewRHS); |
| 1657 | } |
| 1658 | } |
| 1659 | |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 1660 | // If this is a shift of a shift, see if we can fold the two together... |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 1661 | if (ShiftInst *Op0SI = dyn_cast<ShiftInst>(Op0)) |
Chris Lattner | ab780df | 2003-07-24 18:38:56 +0000 | [diff] [blame] | 1662 | if (ConstantUInt *ShiftAmt1C = |
| 1663 | dyn_cast<ConstantUInt>(Op0SI->getOperand(1))) { |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 1664 | unsigned ShiftAmt1 = ShiftAmt1C->getValue(); |
| 1665 | unsigned ShiftAmt2 = CUI->getValue(); |
| 1666 | |
| 1667 | // Check for (A << c1) << c2 and (A >> c1) >> c2 |
| 1668 | if (I.getOpcode() == Op0SI->getOpcode()) { |
| 1669 | unsigned Amt = ShiftAmt1+ShiftAmt2; // Fold into one big shift... |
Chris Lattner | f5ce254 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 1670 | if (Op0->getType()->getPrimitiveSize()*8 < Amt) |
| 1671 | Amt = Op0->getType()->getPrimitiveSize()*8; |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 1672 | return new ShiftInst(I.getOpcode(), Op0SI->getOperand(0), |
| 1673 | ConstantUInt::get(Type::UByteTy, Amt)); |
| 1674 | } |
| 1675 | |
Chris Lattner | ab780df | 2003-07-24 18:38:56 +0000 | [diff] [blame] | 1676 | // Check for (A << c1) >> c2 or visaversa. If we are dealing with |
| 1677 | // signed types, we can only support the (A >> c1) << c2 configuration, |
| 1678 | // because it can not turn an arbitrary bit of A into a sign bit. |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 1679 | if (I.getType()->isUnsigned() || isLeftShift) { |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 1680 | // Calculate bitmask for what gets shifted off the edge... |
| 1681 | Constant *C = ConstantIntegral::getAllOnesValue(I.getType()); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 1682 | if (isLeftShift) |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1683 | C = ConstantExpr::get(Instruction::Shl, C, ShiftAmt1C); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 1684 | else |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1685 | C = ConstantExpr::get(Instruction::Shr, C, ShiftAmt1C); |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 1686 | |
| 1687 | Instruction *Mask = |
| 1688 | BinaryOperator::create(Instruction::And, Op0SI->getOperand(0), |
| 1689 | C, Op0SI->getOperand(0)->getName()+".mask"); |
| 1690 | InsertNewInstBefore(Mask, I); |
| 1691 | |
| 1692 | // Figure out what flavor of shift we should use... |
| 1693 | if (ShiftAmt1 == ShiftAmt2) |
| 1694 | return ReplaceInstUsesWith(I, Mask); // (A << c) >> c === A & c2 |
| 1695 | else if (ShiftAmt1 < ShiftAmt2) { |
| 1696 | return new ShiftInst(I.getOpcode(), Mask, |
| 1697 | ConstantUInt::get(Type::UByteTy, ShiftAmt2-ShiftAmt1)); |
| 1698 | } else { |
| 1699 | return new ShiftInst(Op0SI->getOpcode(), Mask, |
| 1700 | ConstantUInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2)); |
| 1701 | } |
| 1702 | } |
| 1703 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1704 | } |
Chris Lattner | 2e0fb39 | 2002-10-08 16:16:40 +0000 | [diff] [blame] | 1705 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1706 | return 0; |
| 1707 | } |
| 1708 | |
| 1709 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 1710 | // isEliminableCastOfCast - Return true if it is valid to eliminate the CI |
| 1711 | // instruction. |
| 1712 | // |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 1713 | static inline bool isEliminableCastOfCast(const Type *SrcTy, const Type *MidTy, |
| 1714 | const Type *DstTy) { |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 1715 | |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 1716 | // It is legal to eliminate the instruction if casting A->B->A if the sizes |
| 1717 | // are identical and the bits don't get reinterpreted (for example |
Chris Lattner | 0bb7591 | 2002-08-14 23:21:10 +0000 | [diff] [blame] | 1718 | // int->float->int would not be allowed) |
Misha Brukman | e5838c4 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 1719 | if (SrcTy == DstTy && SrcTy->isLosslesslyConvertibleTo(MidTy)) |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 1720 | return true; |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 1721 | |
| 1722 | // Allow free casting and conversion of sizes as long as the sign doesn't |
| 1723 | // change... |
Chris Lattner | b0b412e | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 1724 | if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral()) { |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 1725 | unsigned SrcSize = SrcTy->getPrimitiveSize(); |
| 1726 | unsigned MidSize = MidTy->getPrimitiveSize(); |
| 1727 | unsigned DstSize = DstTy->getPrimitiveSize(); |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 1728 | |
Chris Lattner | 3732aca | 2002-08-15 16:15:25 +0000 | [diff] [blame] | 1729 | // Cases where we are monotonically decreasing the size of the type are |
| 1730 | // always ok, regardless of what sign changes are going on. |
| 1731 | // |
Chris Lattner | 0bb7591 | 2002-08-14 23:21:10 +0000 | [diff] [blame] | 1732 | if (SrcSize >= MidSize && MidSize >= DstSize) |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 1733 | return true; |
Chris Lattner | 3732aca | 2002-08-15 16:15:25 +0000 | [diff] [blame] | 1734 | |
Chris Lattner | 555518c | 2002-09-23 23:39:43 +0000 | [diff] [blame] | 1735 | // Cases where the source and destination type are the same, but the middle |
| 1736 | // type is bigger are noops. |
| 1737 | // |
| 1738 | if (SrcSize == DstSize && MidSize > SrcSize) |
| 1739 | return true; |
| 1740 | |
Chris Lattner | 3732aca | 2002-08-15 16:15:25 +0000 | [diff] [blame] | 1741 | // If we are monotonically growing, things are more complex. |
| 1742 | // |
| 1743 | if (SrcSize <= MidSize && MidSize <= DstSize) { |
| 1744 | // We have eight combinations of signedness to worry about. Here's the |
| 1745 | // table: |
| 1746 | static const int SignTable[8] = { |
| 1747 | // CODE, SrcSigned, MidSigned, DstSigned, Comment |
| 1748 | 1, // U U U Always ok |
| 1749 | 1, // U U S Always ok |
| 1750 | 3, // U S U Ok iff SrcSize != MidSize |
| 1751 | 3, // U S S Ok iff SrcSize != MidSize |
| 1752 | 0, // S U U Never ok |
| 1753 | 2, // S U S Ok iff MidSize == DstSize |
| 1754 | 1, // S S U Always ok |
| 1755 | 1, // S S S Always ok |
| 1756 | }; |
| 1757 | |
| 1758 | // Choose an action based on the current entry of the signtable that this |
| 1759 | // cast of cast refers to... |
| 1760 | unsigned Row = SrcTy->isSigned()*4+MidTy->isSigned()*2+DstTy->isSigned(); |
| 1761 | switch (SignTable[Row]) { |
| 1762 | case 0: return false; // Never ok |
| 1763 | case 1: return true; // Always ok |
| 1764 | case 2: return MidSize == DstSize; // Ok iff MidSize == DstSize |
| 1765 | case 3: // Ok iff SrcSize != MidSize |
| 1766 | return SrcSize != MidSize || SrcTy == Type::BoolTy; |
| 1767 | default: assert(0 && "Bad entry in sign table!"); |
| 1768 | } |
Chris Lattner | 3732aca | 2002-08-15 16:15:25 +0000 | [diff] [blame] | 1769 | } |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 1770 | } |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 1771 | |
| 1772 | // Otherwise, we cannot succeed. Specifically we do not want to allow things |
| 1773 | // like: short -> ushort -> uint, because this can create wrong results if |
| 1774 | // the input short is negative! |
| 1775 | // |
| 1776 | return false; |
| 1777 | } |
| 1778 | |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 1779 | static bool ValueRequiresCast(const Value *V, const Type *Ty) { |
| 1780 | if (V->getType() == Ty || isa<Constant>(V)) return false; |
| 1781 | if (const CastInst *CI = dyn_cast<CastInst>(V)) |
| 1782 | if (isEliminableCastOfCast(CI->getOperand(0)->getType(), CI->getType(), Ty)) |
| 1783 | return false; |
| 1784 | return true; |
| 1785 | } |
| 1786 | |
| 1787 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 1788 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 1789 | /// casts that are known to not do anything... |
| 1790 | /// |
| 1791 | Value *InstCombiner::InsertOperandCastBefore(Value *V, const Type *DestTy, |
| 1792 | Instruction *InsertBefore) { |
| 1793 | if (V->getType() == DestTy) return V; |
| 1794 | if (Constant *C = dyn_cast<Constant>(V)) |
| 1795 | return ConstantExpr::getCast(C, DestTy); |
| 1796 | |
| 1797 | CastInst *CI = new CastInst(V, DestTy, V->getName()); |
| 1798 | InsertNewInstBefore(CI, *InsertBefore); |
| 1799 | return CI; |
| 1800 | } |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 1801 | |
| 1802 | // CastInst simplification |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1803 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1804 | Instruction *InstCombiner::visitCastInst(CastInst &CI) { |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 1805 | Value *Src = CI.getOperand(0); |
| 1806 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 1807 | // If the user is casting a value to the same type, eliminate this cast |
| 1808 | // instruction... |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 1809 | if (CI.getType() == Src->getType()) |
| 1810 | return ReplaceInstUsesWith(CI, Src); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 1811 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 1812 | // If casting the result of another cast instruction, try to eliminate this |
| 1813 | // one! |
| 1814 | // |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 1815 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 1816 | if (isEliminableCastOfCast(CSrc->getOperand(0)->getType(), |
| 1817 | CSrc->getType(), CI.getType())) { |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 1818 | // This instruction now refers directly to the cast's src operand. This |
| 1819 | // has a good chance of making CSrc dead. |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1820 | CI.setOperand(0, CSrc->getOperand(0)); |
| 1821 | return &CI; |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 1824 | // If this is an A->B->A cast, and we are dealing with integral types, try |
| 1825 | // to convert this into a logical 'and' instruction. |
| 1826 | // |
| 1827 | if (CSrc->getOperand(0)->getType() == CI.getType() && |
Chris Lattner | b0b412e | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 1828 | CI.getType()->isInteger() && CSrc->getType()->isInteger() && |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 1829 | CI.getType()->isUnsigned() && CSrc->getType()->isUnsigned() && |
| 1830 | CSrc->getType()->getPrimitiveSize() < CI.getType()->getPrimitiveSize()){ |
| 1831 | assert(CSrc->getType() != Type::ULongTy && |
| 1832 | "Cannot have type bigger than ulong!"); |
Chris Lattner | 196897c | 2003-05-26 23:41:32 +0000 | [diff] [blame] | 1833 | uint64_t AndValue = (1ULL << CSrc->getType()->getPrimitiveSize()*8)-1; |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 1834 | Constant *AndOp = ConstantUInt::get(CI.getType(), AndValue); |
| 1835 | return BinaryOperator::create(Instruction::And, CSrc->getOperand(0), |
| 1836 | AndOp); |
| 1837 | } |
| 1838 | } |
| 1839 | |
Chris Lattner | d0d5160 | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 1840 | // If casting the result of a getelementptr instruction with no offset, turn |
| 1841 | // this into a cast of the original pointer! |
| 1842 | // |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 1843 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
Chris Lattner | d0d5160 | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 1844 | bool AllZeroOperands = true; |
| 1845 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) |
| 1846 | if (!isa<Constant>(GEP->getOperand(i)) || |
| 1847 | !cast<Constant>(GEP->getOperand(i))->isNullValue()) { |
| 1848 | AllZeroOperands = false; |
| 1849 | break; |
| 1850 | } |
| 1851 | if (AllZeroOperands) { |
| 1852 | CI.setOperand(0, GEP->getOperand(0)); |
| 1853 | return &CI; |
| 1854 | } |
| 1855 | } |
| 1856 | |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 1857 | // If we are casting a malloc or alloca to a pointer to a type of the same |
| 1858 | // size, rewrite the allocation instruction to allocate the "right" type. |
| 1859 | // |
| 1860 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) |
Chris Lattner | d4d987d | 2003-11-02 06:54:48 +0000 | [diff] [blame] | 1861 | if (AI->hasOneUse() && !AI->isArrayAllocation()) |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 1862 | if (const PointerType *PTy = dyn_cast<PointerType>(CI.getType())) { |
| 1863 | // Get the type really allocated and the type casted to... |
| 1864 | const Type *AllocElTy = AI->getAllocatedType(); |
| 1865 | unsigned AllocElTySize = TD->getTypeSize(AllocElTy); |
| 1866 | const Type *CastElTy = PTy->getElementType(); |
| 1867 | unsigned CastElTySize = TD->getTypeSize(CastElTy); |
Chris Lattner | 7c94d11 | 2003-11-05 17:31:36 +0000 | [diff] [blame] | 1868 | |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 1869 | // If the allocation is for an even multiple of the cast type size |
Chris Lattner | af78932 | 2003-11-03 01:29:41 +0000 | [diff] [blame] | 1870 | if (CastElTySize && (AllocElTySize % CastElTySize == 0)) { |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 1871 | Value *Amt = ConstantUInt::get(Type::UIntTy, |
| 1872 | AllocElTySize/CastElTySize); |
| 1873 | std::string Name = AI->getName(); AI->setName(""); |
| 1874 | AllocationInst *New; |
| 1875 | if (isa<MallocInst>(AI)) |
| 1876 | New = new MallocInst(CastElTy, Amt, Name); |
| 1877 | else |
| 1878 | New = new AllocaInst(CastElTy, Amt, Name); |
| 1879 | InsertNewInstBefore(New, CI); |
| 1880 | return ReplaceInstUsesWith(CI, New); |
| 1881 | } |
| 1882 | } |
| 1883 | |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 1884 | // If the source value is an instruction with only this use, we can attempt to |
| 1885 | // propagate the cast into the instruction. Also, only handle integral types |
| 1886 | // for now. |
| 1887 | if (Instruction *SrcI = dyn_cast<Instruction>(Src)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1888 | if (SrcI->hasOneUse() && Src->getType()->isIntegral() && |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 1889 | CI.getType()->isInteger()) { // Don't mess with casts to bool here |
| 1890 | const Type *DestTy = CI.getType(); |
| 1891 | unsigned SrcBitSize = getTypeSizeInBits(Src->getType()); |
| 1892 | unsigned DestBitSize = getTypeSizeInBits(DestTy); |
| 1893 | |
| 1894 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; |
| 1895 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; |
| 1896 | |
| 1897 | switch (SrcI->getOpcode()) { |
| 1898 | case Instruction::Add: |
| 1899 | case Instruction::Mul: |
| 1900 | case Instruction::And: |
| 1901 | case Instruction::Or: |
| 1902 | case Instruction::Xor: |
| 1903 | // If we are discarding information, or just changing the sign, rewrite. |
| 1904 | if (DestBitSize <= SrcBitSize && DestBitSize != 1) { |
| 1905 | // Don't insert two casts if they cannot be eliminated. We allow two |
| 1906 | // casts to be inserted if the sizes are the same. This could only be |
| 1907 | // converting signedness, which is a noop. |
| 1908 | if (DestBitSize == SrcBitSize || !ValueRequiresCast(Op1, DestTy) || |
| 1909 | !ValueRequiresCast(Op0, DestTy)) { |
| 1910 | Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI); |
| 1911 | Value *Op1c = InsertOperandCastBefore(Op1, DestTy, SrcI); |
| 1912 | return BinaryOperator::create(cast<BinaryOperator>(SrcI) |
| 1913 | ->getOpcode(), Op0c, Op1c); |
| 1914 | } |
| 1915 | } |
| 1916 | break; |
| 1917 | case Instruction::Shl: |
| 1918 | // Allow changing the sign of the source operand. Do not allow changing |
| 1919 | // the size of the shift, UNLESS the shift amount is a constant. We |
| 1920 | // mush not change variable sized shifts to a smaller size, because it |
| 1921 | // is undefined to shift more bits out than exist in the value. |
| 1922 | if (DestBitSize == SrcBitSize || |
| 1923 | (DestBitSize < SrcBitSize && isa<Constant>(Op1))) { |
| 1924 | Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI); |
| 1925 | return new ShiftInst(Instruction::Shl, Op0c, Op1); |
| 1926 | } |
| 1927 | break; |
| 1928 | } |
| 1929 | } |
| 1930 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1931 | return 0; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 1934 | // CallInst simplification |
| 1935 | // |
| 1936 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame^] | 1937 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 1938 | // visitCallSite. |
| 1939 | if (Function *F = CI.getCalledFunction()) |
| 1940 | switch (F->getIntrinsicID()) { |
| 1941 | case Intrinsic::memmove: |
| 1942 | case Intrinsic::memcpy: |
| 1943 | case Intrinsic::memset: |
| 1944 | // memmove/cpy/set of zero bytes is a noop. |
| 1945 | if (Constant *NumBytes = dyn_cast<Constant>(CI.getOperand(3))) { |
| 1946 | if (NumBytes->isNullValue()) |
| 1947 | return EraseInstFromFunction(CI); |
| 1948 | } |
| 1949 | break; |
| 1950 | default: |
| 1951 | break; |
| 1952 | } |
| 1953 | |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 1954 | return visitCallSite(&CI); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | // InvokeInst simplification |
| 1958 | // |
| 1959 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 1960 | return visitCallSite(&II); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 1963 | // visitCallSite - Improvements for call and invoke instructions. |
| 1964 | // |
| 1965 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 1966 | bool Changed = false; |
| 1967 | |
| 1968 | // If the callee is a constexpr cast of a function, attempt to move the cast |
| 1969 | // to the arguments of the call/invoke. |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 1970 | if (transformConstExprCastCall(CS)) return 0; |
| 1971 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 1972 | Value *Callee = CS.getCalledValue(); |
| 1973 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 1974 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 1975 | if (FTy->isVarArg()) { |
| 1976 | // See if we can optimize any arguments passed through the varargs area of |
| 1977 | // the call. |
| 1978 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
| 1979 | E = CS.arg_end(); I != E; ++I) |
| 1980 | if (CastInst *CI = dyn_cast<CastInst>(*I)) { |
| 1981 | // If this cast does not effect the value passed through the varargs |
| 1982 | // area, we can eliminate the use of the cast. |
| 1983 | Value *Op = CI->getOperand(0); |
| 1984 | if (CI->getType()->isLosslesslyConvertibleTo(Op->getType())) { |
| 1985 | *I = Op; |
| 1986 | Changed = true; |
| 1987 | } |
| 1988 | } |
| 1989 | } |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 1990 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 1991 | return Changed ? CS.getInstruction() : 0; |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 1994 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 1995 | // attempt to move the cast to the arguments of the call/invoke. |
| 1996 | // |
| 1997 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 1998 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 1999 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
| 2000 | if (CE->getOpcode() != Instruction::Cast || |
| 2001 | !isa<ConstantPointerRef>(CE->getOperand(0))) |
| 2002 | return false; |
| 2003 | ConstantPointerRef *CPR = cast<ConstantPointerRef>(CE->getOperand(0)); |
| 2004 | if (!isa<Function>(CPR->getValue())) return false; |
| 2005 | Function *Callee = cast<Function>(CPR->getValue()); |
| 2006 | Instruction *Caller = CS.getInstruction(); |
| 2007 | |
| 2008 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 2009 | // would cause a type conversion of one of our arguments, change this call to |
| 2010 | // be a direct call with arguments casted to the appropriate types. |
| 2011 | // |
| 2012 | const FunctionType *FT = Callee->getFunctionType(); |
| 2013 | const Type *OldRetTy = Caller->getType(); |
| 2014 | |
Chris Lattner | 1f7942f | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 2015 | // Check to see if we are changing the return type... |
| 2016 | if (OldRetTy != FT->getReturnType()) { |
| 2017 | if (Callee->isExternal() && |
| 2018 | !OldRetTy->isLosslesslyConvertibleTo(FT->getReturnType()) && |
| 2019 | !Caller->use_empty()) |
| 2020 | return false; // Cannot transform this return value... |
| 2021 | |
| 2022 | // If the callsite is an invoke instruction, and the return value is used by |
| 2023 | // a PHI node in a successor, we cannot change the return type of the call |
| 2024 | // because there is no place to put the cast instruction (without breaking |
| 2025 | // the critical edge). Bail out in this case. |
| 2026 | if (!Caller->use_empty()) |
| 2027 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 2028 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 2029 | UI != E; ++UI) |
| 2030 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 2031 | if (PN->getParent() == II->getNormalDest() || |
Chris Lattner | fae8ab3 | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 2032 | PN->getParent() == II->getUnwindDest()) |
Chris Lattner | 1f7942f | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 2033 | return false; |
| 2034 | } |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 2035 | |
| 2036 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 2037 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
| 2038 | |
| 2039 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 2040 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 2041 | const Type *ParamTy = FT->getParamType(i); |
| 2042 | bool isConvertible = (*AI)->getType()->isLosslesslyConvertibleTo(ParamTy); |
| 2043 | if (Callee->isExternal() && !isConvertible) return false; |
| 2044 | } |
| 2045 | |
| 2046 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
| 2047 | Callee->isExternal()) |
| 2048 | return false; // Do not delete arguments unless we have a function body... |
| 2049 | |
| 2050 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 2051 | // inserting cast instructions as necessary... |
| 2052 | std::vector<Value*> Args; |
| 2053 | Args.reserve(NumActualArgs); |
| 2054 | |
| 2055 | AI = CS.arg_begin(); |
| 2056 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 2057 | const Type *ParamTy = FT->getParamType(i); |
| 2058 | if ((*AI)->getType() == ParamTy) { |
| 2059 | Args.push_back(*AI); |
| 2060 | } else { |
| 2061 | Instruction *Cast = new CastInst(*AI, ParamTy, "tmp"); |
| 2062 | InsertNewInstBefore(Cast, *Caller); |
| 2063 | Args.push_back(Cast); |
| 2064 | } |
| 2065 | } |
| 2066 | |
| 2067 | // If the function takes more arguments than the call was taking, add them |
| 2068 | // now... |
| 2069 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 2070 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 2071 | |
| 2072 | // If we are removing arguments to the function, emit an obnoxious warning... |
| 2073 | if (FT->getNumParams() < NumActualArgs) |
| 2074 | if (!FT->isVarArg()) { |
| 2075 | std::cerr << "WARNING: While resolving call to function '" |
| 2076 | << Callee->getName() << "' arguments were dropped!\n"; |
| 2077 | } else { |
| 2078 | // Add all of the arguments in their promoted form to the arg list... |
| 2079 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 2080 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 2081 | if (PTy != (*AI)->getType()) { |
| 2082 | // Must promote to pass through va_arg area! |
| 2083 | Instruction *Cast = new CastInst(*AI, PTy, "tmp"); |
| 2084 | InsertNewInstBefore(Cast, *Caller); |
| 2085 | Args.push_back(Cast); |
| 2086 | } else { |
| 2087 | Args.push_back(*AI); |
| 2088 | } |
| 2089 | } |
| 2090 | } |
| 2091 | |
| 2092 | if (FT->getReturnType() == Type::VoidTy) |
| 2093 | Caller->setName(""); // Void type should not have a name... |
| 2094 | |
| 2095 | Instruction *NC; |
| 2096 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Chris Lattner | fae8ab3 | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 2097 | NC = new InvokeInst(Callee, II->getNormalDest(), II->getUnwindDest(), |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 2098 | Args, Caller->getName(), Caller); |
| 2099 | } else { |
| 2100 | NC = new CallInst(Callee, Args, Caller->getName(), Caller); |
| 2101 | } |
| 2102 | |
| 2103 | // Insert a cast of the return type as necessary... |
| 2104 | Value *NV = NC; |
| 2105 | if (Caller->getType() != NV->getType() && !Caller->use_empty()) { |
| 2106 | if (NV->getType() != Type::VoidTy) { |
| 2107 | NV = NC = new CastInst(NC, Caller->getType(), "tmp"); |
Chris Lattner | 686767f | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 2108 | |
| 2109 | // If this is an invoke instruction, we should insert it after the first |
| 2110 | // non-phi, instruction in the normal successor block. |
| 2111 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 2112 | BasicBlock::iterator I = II->getNormalDest()->begin(); |
| 2113 | while (isa<PHINode>(I)) ++I; |
| 2114 | InsertNewInstBefore(NC, *I); |
| 2115 | } else { |
| 2116 | // Otherwise, it's a call, just insert cast right after the call instr |
| 2117 | InsertNewInstBefore(NC, *Caller); |
| 2118 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame^] | 2119 | AddUsersToWorkList(*Caller); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 2120 | } else { |
| 2121 | NV = Constant::getNullValue(Caller->getType()); |
| 2122 | } |
| 2123 | } |
| 2124 | |
| 2125 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 2126 | Caller->replaceAllUsesWith(NV); |
| 2127 | Caller->getParent()->getInstList().erase(Caller); |
| 2128 | removeFromWorkList(Caller); |
| 2129 | return true; |
| 2130 | } |
| 2131 | |
| 2132 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2133 | |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 2134 | // PHINode simplification |
| 2135 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2136 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Chris Lattner | 91daeb5 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 2137 | if (Value *V = hasConstantValue(&PN)) |
| 2138 | return ReplaceInstUsesWith(PN, V); |
Chris Lattner | 4db2d22 | 2004-02-16 05:07:08 +0000 | [diff] [blame] | 2139 | |
| 2140 | // If the only user of this instruction is a cast instruction, and all of the |
| 2141 | // incoming values are constants, change this PHI to merge together the casted |
| 2142 | // constants. |
| 2143 | if (PN.hasOneUse()) |
| 2144 | if (CastInst *CI = dyn_cast<CastInst>(PN.use_back())) |
| 2145 | if (CI->getType() != PN.getType()) { // noop casts will be folded |
| 2146 | bool AllConstant = true; |
| 2147 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 2148 | if (!isa<Constant>(PN.getIncomingValue(i))) { |
| 2149 | AllConstant = false; |
| 2150 | break; |
| 2151 | } |
| 2152 | if (AllConstant) { |
| 2153 | // Make a new PHI with all casted values. |
| 2154 | PHINode *New = new PHINode(CI->getType(), PN.getName(), &PN); |
| 2155 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 2156 | Constant *OldArg = cast<Constant>(PN.getIncomingValue(i)); |
| 2157 | New->addIncoming(ConstantExpr::getCast(OldArg, New->getType()), |
| 2158 | PN.getIncomingBlock(i)); |
| 2159 | } |
| 2160 | |
| 2161 | // Update the cast instruction. |
| 2162 | CI->setOperand(0, New); |
| 2163 | WorkList.push_back(CI); // revisit the cast instruction to fold. |
| 2164 | WorkList.push_back(New); // Make sure to revisit the new Phi |
| 2165 | return &PN; // PN is now dead! |
| 2166 | } |
| 2167 | } |
Chris Lattner | 91daeb5 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 2168 | return 0; |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2171 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2172 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 471bd76 | 2003-05-22 19:07:21 +0000 | [diff] [blame] | 2173 | // Is it 'getelementptr %P, long 0' or 'getelementptr %P' |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2174 | // If so, eliminate the noop. |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 2175 | if (GEP.getNumOperands() == 1) |
| 2176 | return ReplaceInstUsesWith(GEP, GEP.getOperand(0)); |
| 2177 | |
| 2178 | bool HasZeroPointerIndex = false; |
| 2179 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) |
| 2180 | HasZeroPointerIndex = C->isNullValue(); |
| 2181 | |
| 2182 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2183 | return ReplaceInstUsesWith(GEP, GEP.getOperand(0)); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2184 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 2185 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 2186 | // is a getelementptr instruction, combine the indices of the two |
| 2187 | // getelementptr instructions into a single instruction. |
| 2188 | // |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 2189 | if (GetElementPtrInst *Src = dyn_cast<GetElementPtrInst>(GEP.getOperand(0))) { |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 2190 | std::vector<Value *> Indices; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 2191 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 2192 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 471bd76 | 2003-05-22 19:07:21 +0000 | [diff] [blame] | 2193 | if (Src->getNumOperands() == 2 && isa<Constant>(Src->getOperand(1)) && |
| 2194 | isa<Constant>(GEP.getOperand(1))) { |
Chris Lattner | 235af56 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 2195 | // Replace: gep (gep %P, long C1), long C2, ... |
| 2196 | // With: gep %P, long (C1+C2), ... |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 2197 | Value *Sum = ConstantExpr::get(Instruction::Add, |
| 2198 | cast<Constant>(Src->getOperand(1)), |
| 2199 | cast<Constant>(GEP.getOperand(1))); |
Chris Lattner | 235af56 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 2200 | assert(Sum && "Constant folding of longs failed!?"); |
| 2201 | GEP.setOperand(0, Src->getOperand(0)); |
| 2202 | GEP.setOperand(1, Sum); |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame^] | 2203 | AddUsersToWorkList(*Src); // Reduce use count of Src |
Chris Lattner | 235af56 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 2204 | return &GEP; |
Chris Lattner | 471bd76 | 2003-05-22 19:07:21 +0000 | [diff] [blame] | 2205 | } else if (Src->getNumOperands() == 2) { |
Chris Lattner | 235af56 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 2206 | // Replace: gep (gep %P, long B), long A, ... |
| 2207 | // With: T = long A+B; gep %P, T, ... |
| 2208 | // |
Chris Lattner | ae739ae | 2004-02-23 21:46:58 +0000 | [diff] [blame] | 2209 | // Note that if our source is a gep chain itself that we wait for that |
| 2210 | // chain to be resolved before we perform this transformation. This |
| 2211 | // avoids us creating a TON of code in some cases. |
| 2212 | // |
| 2213 | if (isa<GetElementPtrInst>(Src->getOperand(0)) && |
| 2214 | cast<Instruction>(Src->getOperand(0))->getNumOperands() == 2) |
| 2215 | return 0; // Wait until our source is folded to completion. |
| 2216 | |
Chris Lattner | 235af56 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 2217 | Value *Sum = BinaryOperator::create(Instruction::Add, Src->getOperand(1), |
| 2218 | GEP.getOperand(1), |
| 2219 | Src->getName()+".sum", &GEP); |
| 2220 | GEP.setOperand(0, Src->getOperand(0)); |
| 2221 | GEP.setOperand(1, Sum); |
| 2222 | WorkList.push_back(cast<Instruction>(Sum)); |
| 2223 | return &GEP; |
Chris Lattner | 5d606a0 | 2002-11-04 16:43:32 +0000 | [diff] [blame] | 2224 | } else if (*GEP.idx_begin() == Constant::getNullValue(Type::LongTy) && |
Chris Lattner | a8339e3 | 2002-09-17 21:05:42 +0000 | [diff] [blame] | 2225 | Src->getNumOperands() != 1) { |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 2226 | // Otherwise we can do the fold if the first index of the GEP is a zero |
| 2227 | Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end()); |
| 2228 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
Chris Lattner | 5d606a0 | 2002-11-04 16:43:32 +0000 | [diff] [blame] | 2229 | } else if (Src->getOperand(Src->getNumOperands()-1) == |
| 2230 | Constant::getNullValue(Type::LongTy)) { |
| 2231 | // If the src gep ends with a constant array index, merge this get into |
| 2232 | // it, even if we have a non-zero array index. |
| 2233 | Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end()-1); |
| 2234 | Indices.insert(Indices.end(), GEP.idx_begin(), GEP.idx_end()); |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 2235 | } |
| 2236 | |
| 2237 | if (!Indices.empty()) |
| 2238 | return new GetElementPtrInst(Src->getOperand(0), Indices, GEP.getName()); |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 2239 | |
| 2240 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(GEP.getOperand(0))) { |
| 2241 | // GEP of global variable. If all of the indices for this GEP are |
| 2242 | // constants, we can promote this to a constexpr instead of an instruction. |
| 2243 | |
| 2244 | // Scan for nonconstants... |
| 2245 | std::vector<Constant*> Indices; |
| 2246 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
| 2247 | for (; I != E && isa<Constant>(*I); ++I) |
| 2248 | Indices.push_back(cast<Constant>(*I)); |
| 2249 | |
| 2250 | if (I == E) { // If they are all constants... |
Chris Lattner | 46b3d30 | 2003-04-16 22:40:51 +0000 | [diff] [blame] | 2251 | Constant *CE = |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 2252 | ConstantExpr::getGetElementPtr(ConstantPointerRef::get(GV), Indices); |
| 2253 | |
| 2254 | // Replace all uses of the GEP with the new constexpr... |
| 2255 | return ReplaceInstUsesWith(GEP, CE); |
| 2256 | } |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 2257 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GEP.getOperand(0))) { |
| 2258 | if (CE->getOpcode() == Instruction::Cast) { |
| 2259 | if (HasZeroPointerIndex) { |
| 2260 | // transform: GEP (cast [10 x ubyte]* X to [0 x ubyte]*), long 0, ... |
| 2261 | // into : GEP [10 x ubyte]* X, long 0, ... |
| 2262 | // |
| 2263 | // This occurs when the program declares an array extern like "int X[];" |
| 2264 | // |
| 2265 | Constant *X = CE->getOperand(0); |
| 2266 | const PointerType *CPTy = cast<PointerType>(CE->getType()); |
| 2267 | if (const PointerType *XTy = dyn_cast<PointerType>(X->getType())) |
| 2268 | if (const ArrayType *XATy = |
| 2269 | dyn_cast<ArrayType>(XTy->getElementType())) |
| 2270 | if (const ArrayType *CATy = |
| 2271 | dyn_cast<ArrayType>(CPTy->getElementType())) |
| 2272 | if (CATy->getElementType() == XATy->getElementType()) { |
| 2273 | // At this point, we know that the cast source type is a pointer |
| 2274 | // to an array of the same type as the destination pointer |
| 2275 | // array. Because the array type is never stepped over (there |
| 2276 | // is a leading zero) we can fold the cast into this GEP. |
| 2277 | GEP.setOperand(0, X); |
| 2278 | return &GEP; |
| 2279 | } |
| 2280 | } |
| 2281 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 2284 | return 0; |
| 2285 | } |
| 2286 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 2287 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
| 2288 | // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1 |
| 2289 | if (AI.isArrayAllocation()) // Check C != 1 |
| 2290 | if (const ConstantUInt *C = dyn_cast<ConstantUInt>(AI.getArraySize())) { |
| 2291 | const Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getValue()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 2292 | AllocationInst *New = 0; |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 2293 | |
| 2294 | // Create and insert the replacement instruction... |
| 2295 | if (isa<MallocInst>(AI)) |
| 2296 | New = new MallocInst(NewTy, 0, AI.getName(), &AI); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 2297 | else { |
| 2298 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 2299 | New = new AllocaInst(NewTy, 0, AI.getName(), &AI); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 2300 | } |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 2301 | |
| 2302 | // Scan to the end of the allocation instructions, to skip over a block of |
| 2303 | // allocas if possible... |
| 2304 | // |
| 2305 | BasicBlock::iterator It = New; |
| 2306 | while (isa<AllocationInst>(*It)) ++It; |
| 2307 | |
| 2308 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 2309 | // insert our getelementptr instruction... |
| 2310 | // |
| 2311 | std::vector<Value*> Idx(2, Constant::getNullValue(Type::LongTy)); |
| 2312 | Value *V = new GetElementPtrInst(New, Idx, New->getName()+".sub", It); |
| 2313 | |
| 2314 | // Now make everything use the getelementptr instead of the original |
| 2315 | // allocation. |
| 2316 | ReplaceInstUsesWith(AI, V); |
| 2317 | return &AI; |
| 2318 | } |
| 2319 | return 0; |
| 2320 | } |
| 2321 | |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 2322 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { |
| 2323 | Value *Op = FI.getOperand(0); |
| 2324 | |
| 2325 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X |
| 2326 | if (CastInst *CI = dyn_cast<CastInst>(Op)) |
| 2327 | if (isa<PointerType>(CI->getOperand(0)->getType())) { |
| 2328 | FI.setOperand(0, CI->getOperand(0)); |
| 2329 | return &FI; |
| 2330 | } |
| 2331 | |
Chris Lattner | f3a3660 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 2332 | // If we have 'free null' delete the instruction. This can happen in stl code |
| 2333 | // when lots of inlining happens. |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame^] | 2334 | if (isa<ConstantPointerNull>(Op)) |
| 2335 | return EraseInstFromFunction(FI); |
Chris Lattner | f3a3660 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 2336 | |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 2337 | return 0; |
| 2338 | } |
| 2339 | |
| 2340 | |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 2341 | /// GetGEPGlobalInitializer - Given a constant, and a getelementptr |
| 2342 | /// constantexpr, return the constant value being addressed by the constant |
| 2343 | /// expression, or null if something is funny. |
| 2344 | /// |
| 2345 | static Constant *GetGEPGlobalInitializer(Constant *C, ConstantExpr *CE) { |
| 2346 | if (CE->getOperand(1) != Constant::getNullValue(Type::LongTy)) |
| 2347 | return 0; // Do not allow stepping over the value! |
| 2348 | |
| 2349 | // Loop over all of the operands, tracking down which value we are |
| 2350 | // addressing... |
| 2351 | for (unsigned i = 2, e = CE->getNumOperands(); i != e; ++i) |
| 2352 | if (ConstantUInt *CU = dyn_cast<ConstantUInt>(CE->getOperand(i))) { |
Chris Lattner | 76b2ff4 | 2004-02-15 05:55:15 +0000 | [diff] [blame] | 2353 | ConstantStruct *CS = dyn_cast<ConstantStruct>(C); |
| 2354 | if (CS == 0) return 0; |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 2355 | if (CU->getValue() >= CS->getValues().size()) return 0; |
| 2356 | C = cast<Constant>(CS->getValues()[CU->getValue()]); |
| 2357 | } else if (ConstantSInt *CS = dyn_cast<ConstantSInt>(CE->getOperand(i))) { |
Chris Lattner | 76b2ff4 | 2004-02-15 05:55:15 +0000 | [diff] [blame] | 2358 | ConstantArray *CA = dyn_cast<ConstantArray>(C); |
| 2359 | if (CA == 0) return 0; |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 2360 | if ((uint64_t)CS->getValue() >= CA->getValues().size()) return 0; |
| 2361 | C = cast<Constant>(CA->getValues()[CS->getValue()]); |
| 2362 | } else |
| 2363 | return 0; |
| 2364 | return C; |
| 2365 | } |
| 2366 | |
| 2367 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 2368 | Value *Op = LI.getOperand(0); |
Chris Lattner | 7e8af38 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 2369 | if (LI.isVolatile()) return 0; |
| 2370 | |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 2371 | if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Op)) |
| 2372 | Op = CPR->getValue(); |
| 2373 | |
| 2374 | // Instcombine load (constant global) into the value loaded... |
| 2375 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) |
Chris Lattner | bdb0ce0 | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 2376 | if (GV->isConstant() && !GV->isExternal()) |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 2377 | return ReplaceInstUsesWith(LI, GV->getInitializer()); |
| 2378 | |
| 2379 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded... |
| 2380 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) |
| 2381 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 2382 | if (ConstantPointerRef *G=dyn_cast<ConstantPointerRef>(CE->getOperand(0))) |
| 2383 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getValue())) |
Chris Lattner | bdb0ce0 | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 2384 | if (GV->isConstant() && !GV->isExternal()) |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 2385 | if (Constant *V = GetGEPGlobalInitializer(GV->getInitializer(), CE)) |
| 2386 | return ReplaceInstUsesWith(LI, V); |
| 2387 | return 0; |
| 2388 | } |
| 2389 | |
| 2390 | |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 2391 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 2392 | // Change br (not X), label True, label False to: br X, label False, True |
Chris Lattner | 4f7acca | 2004-02-27 06:27:46 +0000 | [diff] [blame] | 2393 | if (BI.isConditional() && !isa<Constant>(BI.getCondition())) { |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 2394 | if (Value *V = dyn_castNotVal(BI.getCondition())) { |
| 2395 | BasicBlock *TrueDest = BI.getSuccessor(0); |
| 2396 | BasicBlock *FalseDest = BI.getSuccessor(1); |
| 2397 | // Swap Destinations and condition... |
| 2398 | BI.setCondition(V); |
| 2399 | BI.setSuccessor(0, FalseDest); |
| 2400 | BI.setSuccessor(1, TrueDest); |
| 2401 | return &BI; |
Chris Lattner | 4f7acca | 2004-02-27 06:27:46 +0000 | [diff] [blame] | 2402 | } else if (SetCondInst *I = dyn_cast<SetCondInst>(BI.getCondition())) { |
| 2403 | // Cannonicalize setne -> seteq |
| 2404 | if ((I->getOpcode() == Instruction::SetNE || |
| 2405 | I->getOpcode() == Instruction::SetLE || |
| 2406 | I->getOpcode() == Instruction::SetGE) && I->hasOneUse()) { |
| 2407 | std::string Name = I->getName(); I->setName(""); |
| 2408 | Instruction::BinaryOps NewOpcode = |
| 2409 | SetCondInst::getInverseCondition(I->getOpcode()); |
| 2410 | Value *NewSCC = BinaryOperator::create(NewOpcode, I->getOperand(0), |
| 2411 | I->getOperand(1), Name, I); |
| 2412 | BasicBlock *TrueDest = BI.getSuccessor(0); |
| 2413 | BasicBlock *FalseDest = BI.getSuccessor(1); |
| 2414 | // Swap Destinations and condition... |
| 2415 | BI.setCondition(NewSCC); |
| 2416 | BI.setSuccessor(0, FalseDest); |
| 2417 | BI.setSuccessor(1, TrueDest); |
| 2418 | removeFromWorkList(I); |
| 2419 | I->getParent()->getInstList().erase(I); |
| 2420 | WorkList.push_back(cast<Instruction>(NewSCC)); |
| 2421 | return &BI; |
| 2422 | } |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 2423 | } |
Chris Lattner | 4f7acca | 2004-02-27 06:27:46 +0000 | [diff] [blame] | 2424 | } |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 2425 | return 0; |
| 2426 | } |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 2427 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 2428 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 2429 | void InstCombiner::removeFromWorkList(Instruction *I) { |
| 2430 | WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I), |
| 2431 | WorkList.end()); |
| 2432 | } |
| 2433 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2434 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2435 | bool Changed = false; |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 2436 | TD = &getAnalysis<TargetData>(); |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 2437 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2438 | WorkList.insert(WorkList.end(), inst_begin(F), inst_end(F)); |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 2439 | |
| 2440 | while (!WorkList.empty()) { |
| 2441 | Instruction *I = WorkList.back(); // Get an instruction from the worklist |
| 2442 | WorkList.pop_back(); |
| 2443 | |
Misha Brukman | 632df28 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 2444 | // Check to see if we can DCE or ConstantPropagate the instruction... |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 2445 | // Check to see if we can DIE the instruction... |
| 2446 | if (isInstructionTriviallyDead(I)) { |
| 2447 | // Add operands to the worklist... |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 2448 | if (I->getNumOperands() < 4) |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame^] | 2449 | AddUsesToWorkList(*I); |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 2450 | ++NumDeadInst; |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 2451 | |
| 2452 | I->getParent()->getInstList().erase(I); |
| 2453 | removeFromWorkList(I); |
| 2454 | continue; |
| 2455 | } |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 2456 | |
Misha Brukman | 632df28 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 2457 | // Instruction isn't dead, see if we can constant propagate it... |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 2458 | if (Constant *C = ConstantFoldInstruction(I)) { |
| 2459 | // Add operands to the worklist... |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame^] | 2460 | AddUsesToWorkList(*I); |
Chris Lattner | c6509f4 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 2461 | ReplaceInstUsesWith(*I, C); |
| 2462 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 2463 | ++NumConstProp; |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 2464 | I->getParent()->getInstList().erase(I); |
Chris Lattner | 800aaaf | 2003-10-07 15:17:02 +0000 | [diff] [blame] | 2465 | removeFromWorkList(I); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 2466 | continue; |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 2467 | } |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 2468 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 2469 | // Now that we have an instruction, try combining it to simplify it... |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 2470 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 2471 | ++NumCombined; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2472 | // Should we replace the old instruction with a new one? |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 2473 | if (Result != I) { |
| 2474 | // Instructions can end up on the worklist more than once. Make sure |
| 2475 | // we do not process an instruction that has been deleted. |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 2476 | removeFromWorkList(I); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 2477 | |
| 2478 | // Move the name to the new instruction first... |
| 2479 | std::string OldName = I->getName(); I->setName(""); |
Chris Lattner | 950fc78 | 2003-10-07 22:58:41 +0000 | [diff] [blame] | 2480 | Result->setName(OldName); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 2481 | |
| 2482 | // Insert the new instruction into the basic block... |
| 2483 | BasicBlock *InstParent = I->getParent(); |
| 2484 | InstParent->getInstList().insert(I, Result); |
| 2485 | |
| 2486 | // Everything uses the new instruction now... |
| 2487 | I->replaceAllUsesWith(Result); |
| 2488 | |
| 2489 | // Erase the old instruction. |
| 2490 | InstParent->getInstList().erase(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2491 | } else { |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 2492 | BasicBlock::iterator II = I; |
| 2493 | |
| 2494 | // If the instruction was modified, it's possible that it is now dead. |
| 2495 | // if so, remove it. |
| 2496 | if (dceInstruction(II)) { |
| 2497 | // Instructions may end up in the worklist more than once. Erase them |
| 2498 | // all. |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 2499 | removeFromWorkList(I); |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 2500 | Result = 0; |
| 2501 | } |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 2502 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2503 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 2504 | if (Result) { |
| 2505 | WorkList.push_back(Result); |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame^] | 2506 | AddUsersToWorkList(*Result); |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 2507 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2508 | Changed = true; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 2509 | } |
| 2510 | } |
| 2511 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2512 | return Changed; |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 2513 | } |
| 2514 | |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 2515 | Pass *llvm::createInstructionCombiningPass() { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2516 | return new InstCombiner(); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 2517 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 2518 | |