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: |
Chris Lattner | dd1a86d | 2004-05-04 15:19:33 +0000 | [diff] [blame] | 15 | // %Y = add int %X, 1 |
| 16 | // %Z = add int %Y, 1 |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 17 | // into: |
Chris Lattner | dd1a86d | 2004-05-04 15:19:33 +0000 | [diff] [blame] | 18 | // %Z = add int %X, 2 |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 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 | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 36 | #define DEBUG_TYPE "instcombine" |
Chris Lattner | b4cfa7f | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 37 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 471bd76 | 2003-05-22 19:07:21 +0000 | [diff] [blame] | 38 | #include "llvm/Instructions.h" |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 39 | #include "llvm/Intrinsics.h" |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 40 | #include "llvm/Pass.h" |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 41 | #include "llvm/Constants.h" |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 42 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 43 | #include "llvm/GlobalVariable.h" |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetData.h" |
| 45 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 46 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 47 | #include "llvm/Support/CallSite.h" |
| 48 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 60a6591 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 49 | #include "llvm/Support/InstIterator.h" |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 50 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 51 | #include "Support/Debug.h" |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 52 | #include "Support/Statistic.h" |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 53 | #include <algorithm> |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 54 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 56 | namespace { |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 57 | Statistic<> NumCombined ("instcombine", "Number of insts combined"); |
| 58 | Statistic<> NumConstProp("instcombine", "Number of constant folds"); |
| 59 | Statistic<> NumDeadInst ("instcombine", "Number of dead inst eliminated"); |
| 60 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 61 | class InstCombiner : public FunctionPass, |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 62 | public InstVisitor<InstCombiner, Instruction*> { |
| 63 | // Worklist of all of the instructions that need to be simplified. |
| 64 | std::vector<Instruction*> WorkList; |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 65 | TargetData *TD; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 67 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
| 68 | /// the instruction to the work lists because they might get more simplified |
| 69 | /// now. |
| 70 | /// |
| 71 | void AddUsersToWorkList(Instruction &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 72 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 73 | UI != UE; ++UI) |
| 74 | WorkList.push_back(cast<Instruction>(*UI)); |
| 75 | } |
| 76 | |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 77 | /// AddUsesToWorkList - When an instruction is simplified, add operands to |
| 78 | /// the work lists because they might get more simplified now. |
| 79 | /// |
| 80 | void AddUsesToWorkList(Instruction &I) { |
| 81 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 82 | if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) |
| 83 | WorkList.push_back(Op); |
| 84 | } |
| 85 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 86 | // removeFromWorkList - remove all instances of I from the worklist. |
| 87 | void removeFromWorkList(Instruction *I); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 88 | public: |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 89 | virtual bool runOnFunction(Function &F); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 90 | |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 91 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 92 | AU.addRequired<TargetData>(); |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 93 | AU.setPreservesCFG(); |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 96 | TargetData &getTargetData() const { return *TD; } |
| 97 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 98 | // Visitation implementation - Implement instruction combining for different |
| 99 | // instruction types. The semantics are as follows: |
| 100 | // Return Value: |
| 101 | // null - No change was made |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 102 | // 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] | 103 | // otherwise - Change was made, replace I with returned instruction |
| 104 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 105 | Instruction *visitAdd(BinaryOperator &I); |
| 106 | Instruction *visitSub(BinaryOperator &I); |
| 107 | Instruction *visitMul(BinaryOperator &I); |
| 108 | Instruction *visitDiv(BinaryOperator &I); |
| 109 | Instruction *visitRem(BinaryOperator &I); |
| 110 | Instruction *visitAnd(BinaryOperator &I); |
| 111 | Instruction *visitOr (BinaryOperator &I); |
| 112 | Instruction *visitXor(BinaryOperator &I); |
| 113 | Instruction *visitSetCondInst(BinaryOperator &I); |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 114 | Instruction *visitShiftInst(ShiftInst &I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 115 | Instruction *visitCastInst(CastInst &CI); |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 116 | Instruction *visitSelectInst(SelectInst &CI); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 117 | Instruction *visitCallInst(CallInst &CI); |
| 118 | Instruction *visitInvokeInst(InvokeInst &II); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 119 | Instruction *visitPHINode(PHINode &PN); |
| 120 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 121 | Instruction *visitAllocationInst(AllocationInst &AI); |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 122 | Instruction *visitFreeInst(FreeInst &FI); |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 123 | Instruction *visitLoadInst(LoadInst &LI); |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 124 | Instruction *visitBranchInst(BranchInst &BI); |
Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 125 | Instruction *visitSwitchInst(SwitchInst &SI); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 126 | |
| 127 | // visitInstruction - Specify what to return for unhandled instructions... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 128 | Instruction *visitInstruction(Instruction &I) { return 0; } |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 129 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 130 | private: |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 131 | Instruction *visitCallSite(CallSite CS); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 132 | bool transformConstExprCastCall(CallSite CS); |
| 133 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 134 | public: |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 135 | // InsertNewInstBefore - insert an instruction New before instruction Old |
| 136 | // in the program. Add the new instruction to the worklist. |
| 137 | // |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 138 | Value *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Chris Lattner | 65217ff | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 139 | assert(New && New->getParent() == 0 && |
| 140 | "New instruction already inserted into a basic block!"); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 141 | BasicBlock *BB = Old.getParent(); |
| 142 | BB->getInstList().insert(&Old, New); // Insert inst |
| 143 | WorkList.push_back(New); // Add to worklist |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 144 | return New; |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // ReplaceInstUsesWith - This method is to be used when an instruction is |
| 148 | // found to be dead, replacable with another preexisting expression. Here |
| 149 | // we add all uses of I to the worklist, replace all uses of I with the new |
| 150 | // value, then return I, so that the inst combiner will know that I was |
| 151 | // modified. |
| 152 | // |
| 153 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 154 | AddUsersToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 8953b90 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 155 | if (&I != V) { |
| 156 | I.replaceAllUsesWith(V); |
| 157 | return &I; |
| 158 | } else { |
| 159 | // If we are replacing the instruction with itself, this must be in a |
| 160 | // segment of unreachable code, so just clobber the instruction. |
| 161 | I.replaceAllUsesWith(Constant::getNullValue(I.getType())); |
| 162 | return &I; |
| 163 | } |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 164 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 165 | |
| 166 | // EraseInstFromFunction - When dealing with an instruction that has side |
| 167 | // effects or produces a void value, we can't rely on DCE to delete the |
| 168 | // instruction. Instead, visit methods should return the value returned by |
| 169 | // this function. |
| 170 | Instruction *EraseInstFromFunction(Instruction &I) { |
| 171 | assert(I.use_empty() && "Cannot erase instruction that is used!"); |
| 172 | AddUsesToWorkList(I); |
| 173 | removeFromWorkList(&I); |
| 174 | I.getParent()->getInstList().erase(&I); |
| 175 | return 0; // Don't do anything with FI |
| 176 | } |
| 177 | |
| 178 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 179 | private: |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 180 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 181 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 182 | /// casts that are known to not do anything... |
| 183 | /// |
| 184 | Value *InsertOperandCastBefore(Value *V, const Type *DestTy, |
| 185 | Instruction *InsertBefore); |
| 186 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 187 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 188 | // operators... |
| 189 | bool SimplifyCommutative(BinaryOperator &I); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 190 | |
| 191 | Instruction *OptAndOp(Instruction *Op, ConstantIntegral *OpRHS, |
| 192 | ConstantIntegral *AndRHS, BinaryOperator &TheAnd); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 193 | }; |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 194 | |
Chris Lattner | c8b7092 | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 195 | RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions"); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 198 | // getComplexity: Assign a complexity or rank value to LLVM Values... |
| 199 | // 0 -> Constant, 1 -> Other, 2 -> Argument, 2 -> Unary, 3 -> OtherInst |
| 200 | static unsigned getComplexity(Value *V) { |
| 201 | if (isa<Instruction>(V)) { |
| 202 | if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V)) |
| 203 | return 2; |
| 204 | return 3; |
| 205 | } |
| 206 | if (isa<Argument>(V)) return 2; |
| 207 | return isa<Constant>(V) ? 0 : 1; |
| 208 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 209 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 210 | // isOnlyUse - Return true if this instruction will be deleted if we stop using |
| 211 | // it. |
| 212 | static bool isOnlyUse(Value *V) { |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 213 | return V->hasOneUse() || isa<Constant>(V); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 216 | // getPromotedType - Return the specified type promoted as it would be to pass |
| 217 | // though a va_arg area... |
| 218 | static const Type *getPromotedType(const Type *Ty) { |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 219 | switch (Ty->getTypeID()) { |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 220 | case Type::SByteTyID: |
| 221 | case Type::ShortTyID: return Type::IntTy; |
| 222 | case Type::UByteTyID: |
| 223 | case Type::UShortTyID: return Type::UIntTy; |
| 224 | case Type::FloatTyID: return Type::DoubleTy; |
| 225 | default: return Ty; |
| 226 | } |
| 227 | } |
| 228 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 229 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 230 | // operators: |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 231 | // |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 232 | // 1. Order operands such that they are listed from right (least complex) to |
| 233 | // left (most complex). This puts constants before unary operators before |
| 234 | // binary operators. |
| 235 | // |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 236 | // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) |
| 237 | // 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] | 238 | // |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 239 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 240 | bool Changed = false; |
| 241 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) |
| 242 | Changed = !I.swapOperands(); |
| 243 | |
| 244 | if (!I.isAssociative()) return Changed; |
| 245 | Instruction::BinaryOps Opcode = I.getOpcode(); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 246 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) |
| 247 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { |
| 248 | if (isa<Constant>(I.getOperand(1))) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 249 | Constant *Folded = ConstantExpr::get(I.getOpcode(), |
| 250 | cast<Constant>(I.getOperand(1)), |
| 251 | cast<Constant>(Op->getOperand(1))); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 252 | I.setOperand(0, Op->getOperand(0)); |
| 253 | I.setOperand(1, Folded); |
| 254 | return true; |
| 255 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) |
| 256 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && |
| 257 | isOnlyUse(Op) && isOnlyUse(Op1)) { |
| 258 | Constant *C1 = cast<Constant>(Op->getOperand(1)); |
| 259 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); |
| 260 | |
| 261 | // 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] | 262 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 263 | Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0), |
| 264 | Op1->getOperand(0), |
| 265 | Op1->getName(), &I); |
| 266 | WorkList.push_back(New); |
| 267 | I.setOperand(0, New); |
| 268 | I.setOperand(1, Folded); |
| 269 | return true; |
| 270 | } |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 271 | } |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 272 | return Changed; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 273 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 274 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 275 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
| 276 | // if the LHS is a constant zero (which is the 'negate' form). |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 277 | // |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 278 | static inline Value *dyn_castNegVal(Value *V) { |
| 279 | if (BinaryOperator::isNeg(V)) |
| 280 | return BinaryOperator::getNegArgument(cast<BinaryOperator>(V)); |
| 281 | |
Chris Lattner | 9244df6 | 2003-04-30 22:19:10 +0000 | [diff] [blame] | 282 | // Constants can be considered to be negated values if they can be folded... |
| 283 | if (Constant *C = dyn_cast<Constant>(V)) |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 284 | return ConstantExpr::getNeg(C); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 285 | return 0; |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 288 | static inline Value *dyn_castNotVal(Value *V) { |
| 289 | if (BinaryOperator::isNot(V)) |
| 290 | return BinaryOperator::getNotArgument(cast<BinaryOperator>(V)); |
| 291 | |
| 292 | // Constants can be considered to be not'ed values... |
Chris Lattner | dd65d86 | 2003-04-30 22:34:06 +0000 | [diff] [blame] | 293 | if (ConstantIntegral *C = dyn_cast<ConstantIntegral>(V)) |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 294 | return ConstantExpr::getNot(C); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 295 | return 0; |
| 296 | } |
| 297 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 298 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
| 299 | // other computations (because it has a constant operand), return the |
| 300 | // non-constant operand of the multiply. |
| 301 | // |
| 302 | static inline Value *dyn_castFoldableMul(Value *V) { |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 303 | if (V->hasOneUse() && V->getType()->isInteger()) |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 304 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 305 | if (I->getOpcode() == Instruction::Mul) |
| 306 | if (isa<Constant>(I->getOperand(1))) |
| 307 | return I->getOperand(0); |
| 308 | return 0; |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 309 | } |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 310 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 311 | // dyn_castMaskingAnd - If this value is an And instruction masking a value with |
| 312 | // a constant, return the constant being anded with. |
| 313 | // |
Chris Lattner | 01d5639 | 2003-08-12 19:17:27 +0000 | [diff] [blame] | 314 | template<class ValueType> |
| 315 | static inline Constant *dyn_castMaskingAnd(ValueType *V) { |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 316 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 317 | if (I->getOpcode() == Instruction::And) |
| 318 | return dyn_cast<Constant>(I->getOperand(1)); |
| 319 | |
| 320 | // If this is a constant, it acts just like we were masking with it. |
| 321 | return dyn_cast<Constant>(V); |
| 322 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 323 | |
| 324 | // Log2 - Calculate the log base 2 for the specified value if it is exactly a |
| 325 | // power of 2. |
| 326 | static unsigned Log2(uint64_t Val) { |
| 327 | assert(Val > 1 && "Values 0 and 1 should be handled elsewhere!"); |
| 328 | unsigned Count = 0; |
| 329 | while (Val != 1) { |
| 330 | if (Val & 1) return 0; // Multiple bits set? |
| 331 | Val >>= 1; |
| 332 | ++Count; |
| 333 | } |
| 334 | return Count; |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 337 | |
| 338 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
| 339 | /// function is designed to check a chain of associative operators for a |
| 340 | /// potential to apply a certain optimization. Since the optimization may be |
| 341 | /// applicable if the expression was reassociated, this checks the chain, then |
| 342 | /// reassociates the expression as necessary to expose the optimization |
| 343 | /// opportunity. This makes use of a special Functor, which must define |
| 344 | /// 'shouldApply' and 'apply' methods. |
| 345 | /// |
| 346 | template<typename Functor> |
| 347 | Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { |
| 348 | unsigned Opcode = Root.getOpcode(); |
| 349 | Value *LHS = Root.getOperand(0); |
| 350 | |
| 351 | // Quick check, see if the immediate LHS matches... |
| 352 | if (F.shouldApply(LHS)) |
| 353 | return F.apply(Root); |
| 354 | |
| 355 | // Otherwise, if the LHS is not of the same opcode as the root, return. |
| 356 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 357 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 358 | // Should we apply this transform to the RHS? |
| 359 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); |
| 360 | |
| 361 | // If not to the RHS, check to see if we should apply to the LHS... |
| 362 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { |
| 363 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS |
| 364 | ShouldApply = true; |
| 365 | } |
| 366 | |
| 367 | // If the functor wants to apply the optimization to the RHS of LHSI, |
| 368 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) |
| 369 | if (ShouldApply) { |
| 370 | BasicBlock *BB = Root.getParent(); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 371 | |
| 372 | // Now all of the instructions are in the current basic block, go ahead |
| 373 | // and perform the reassociation. |
| 374 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); |
| 375 | |
| 376 | // First move the selected RHS to the LHS of the root... |
| 377 | Root.setOperand(0, LHSI->getOperand(1)); |
| 378 | |
| 379 | // Make what used to be the LHS of the root be the user of the root... |
| 380 | Value *ExtraOperand = TmpLHSI->getOperand(1); |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 381 | if (&Root == TmpLHSI) { |
Chris Lattner | 8953b90 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 382 | Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); |
| 383 | return 0; |
| 384 | } |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 385 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 386 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 387 | TmpLHSI->getParent()->getInstList().remove(TmpLHSI); |
| 388 | BasicBlock::iterator ARI = &Root; ++ARI; |
| 389 | BB->getInstList().insert(ARI, TmpLHSI); // Move TmpLHSI to after Root |
| 390 | ARI = Root; |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 391 | |
| 392 | // Now propagate the ExtraOperand down the chain of instructions until we |
| 393 | // get to LHSI. |
| 394 | while (TmpLHSI != LHSI) { |
| 395 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 396 | // Move the instruction to immediately before the chain we are |
| 397 | // constructing to avoid breaking dominance properties. |
| 398 | NextLHSI->getParent()->getInstList().remove(NextLHSI); |
| 399 | BB->getInstList().insert(ARI, NextLHSI); |
| 400 | ARI = NextLHSI; |
| 401 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 402 | Value *NextOp = NextLHSI->getOperand(1); |
| 403 | NextLHSI->setOperand(1, ExtraOperand); |
| 404 | TmpLHSI = NextLHSI; |
| 405 | ExtraOperand = NextOp; |
| 406 | } |
| 407 | |
| 408 | // Now that the instructions are reassociated, have the functor perform |
| 409 | // the transformation... |
| 410 | return F.apply(Root); |
| 411 | } |
| 412 | |
| 413 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
| 414 | } |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | |
| 419 | // AddRHS - Implements: X + X --> X << 1 |
| 420 | struct AddRHS { |
| 421 | Value *RHS; |
| 422 | AddRHS(Value *rhs) : RHS(rhs) {} |
| 423 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 424 | Instruction *apply(BinaryOperator &Add) const { |
| 425 | return new ShiftInst(Instruction::Shl, Add.getOperand(0), |
| 426 | ConstantInt::get(Type::UByteTy, 1)); |
| 427 | } |
| 428 | }; |
| 429 | |
| 430 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) |
| 431 | // iff C1&C2 == 0 |
| 432 | struct AddMaskingAnd { |
| 433 | Constant *C2; |
| 434 | AddMaskingAnd(Constant *c) : C2(c) {} |
| 435 | bool shouldApply(Value *LHS) const { |
| 436 | if (Constant *C1 = dyn_castMaskingAnd(LHS)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 437 | return ConstantExpr::getAnd(C1, C2)->isNullValue(); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 438 | return false; |
| 439 | } |
| 440 | Instruction *apply(BinaryOperator &Add) const { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 441 | return BinaryOperator::createOr(Add.getOperand(0), Add.getOperand(1)); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 442 | } |
| 443 | }; |
| 444 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 445 | static Value *FoldOperationIntoSelectOperand(Instruction &BI, Value *SO, |
| 446 | InstCombiner *IC) { |
| 447 | // Figure out if the constant is the left or the right argument. |
| 448 | bool ConstIsRHS = isa<Constant>(BI.getOperand(1)); |
| 449 | Constant *ConstOperand = cast<Constant>(BI.getOperand(ConstIsRHS)); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 450 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 451 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
| 452 | if (ConstIsRHS) |
| 453 | return ConstantExpr::get(BI.getOpcode(), SOC, ConstOperand); |
| 454 | return ConstantExpr::get(BI.getOpcode(), ConstOperand, SOC); |
| 455 | } |
| 456 | |
| 457 | Value *Op0 = SO, *Op1 = ConstOperand; |
| 458 | if (!ConstIsRHS) |
| 459 | std::swap(Op0, Op1); |
| 460 | Instruction *New; |
| 461 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&BI)) |
| 462 | New = BinaryOperator::create(BO->getOpcode(), Op0, Op1); |
| 463 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(&BI)) |
| 464 | New = new ShiftInst(SI->getOpcode(), Op0, Op1); |
Chris Lattner | f9d9665 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 465 | else { |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 466 | assert(0 && "Unknown binary instruction type!"); |
Chris Lattner | f9d9665 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 467 | abort(); |
| 468 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 469 | return IC->InsertNewInstBefore(New, BI); |
| 470 | } |
| 471 | |
| 472 | // FoldBinOpIntoSelect - Given an instruction with a select as one operand and a |
| 473 | // constant as the other operand, try to fold the binary operator into the |
| 474 | // select arguments. |
| 475 | static Instruction *FoldBinOpIntoSelect(Instruction &BI, SelectInst *SI, |
| 476 | InstCombiner *IC) { |
| 477 | // Don't modify shared select instructions |
| 478 | if (!SI->hasOneUse()) return 0; |
| 479 | Value *TV = SI->getOperand(1); |
| 480 | Value *FV = SI->getOperand(2); |
| 481 | |
| 482 | if (isa<Constant>(TV) || isa<Constant>(FV)) { |
| 483 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(BI, TV, IC); |
| 484 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(BI, FV, IC); |
| 485 | |
| 486 | return new SelectInst(SI->getCondition(), SelectTrueVal, |
| 487 | SelectFalseVal); |
| 488 | } |
| 489 | return 0; |
| 490 | } |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 491 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 492 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 493 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 494 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 495 | |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 496 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 497 | // X + 0 --> X |
| 498 | if (!I.getType()->isFloatingPoint() && // -0 + +0 = +0, so it's not a noop |
| 499 | RHSC->isNullValue()) |
| 500 | return ReplaceInstUsesWith(I, LHS); |
| 501 | |
| 502 | // X + (signbit) --> X ^ signbit |
| 503 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { |
| 504 | unsigned NumBits = CI->getType()->getPrimitiveSize()*8; |
| 505 | uint64_t Val = CI->getRawValue() & (1ULL << NumBits)-1; |
| 506 | if (Val == (1ULL << NumBits-1)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 507 | return BinaryOperator::createXor(LHS, RHS); |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 508 | } |
| 509 | } |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 510 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 511 | // X + X --> X << 1 |
| 512 | if (I.getType()->isInteger()) |
| 513 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 514 | |
Chris Lattner | 147e975 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 515 | // -A + B --> B - A |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 516 | if (Value *V = dyn_castNegVal(LHS)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 517 | return BinaryOperator::createSub(RHS, V); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 518 | |
| 519 | // A + -B --> A - B |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 520 | if (!isa<Constant>(RHS)) |
| 521 | if (Value *V = dyn_castNegVal(RHS)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 522 | return BinaryOperator::createSub(LHS, V); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 523 | |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 524 | // X*C + X --> X * (C+1) |
| 525 | if (dyn_castFoldableMul(LHS) == RHS) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 526 | Constant *CP1 = |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 527 | ConstantExpr::getAdd( |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 528 | cast<Constant>(cast<Instruction>(LHS)->getOperand(1)), |
| 529 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 530 | return BinaryOperator::createMul(RHS, CP1); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | // X + X*C --> X * (C+1) |
| 534 | if (dyn_castFoldableMul(RHS) == LHS) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 535 | Constant *CP1 = |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 536 | ConstantExpr::getAdd( |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 537 | cast<Constant>(cast<Instruction>(RHS)->getOperand(1)), |
| 538 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 539 | return BinaryOperator::createMul(LHS, CP1); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 542 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
| 543 | if (Constant *C2 = dyn_castMaskingAnd(RHS)) |
| 544 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) return R; |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 545 | |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 546 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
| 547 | if (Instruction *ILHS = dyn_cast<Instruction>(LHS)) { |
| 548 | switch (ILHS->getOpcode()) { |
| 549 | case Instruction::Xor: |
| 550 | // ~X + C --> (C-1) - X |
| 551 | if (ConstantInt *XorRHS = dyn_cast<ConstantInt>(ILHS->getOperand(1))) |
| 552 | if (XorRHS->isAllOnesValue()) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 553 | return BinaryOperator::createSub(ConstantExpr::getSub(CRHS, |
| 554 | ConstantInt::get(I.getType(), 1)), |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 555 | ILHS->getOperand(0)); |
| 556 | break; |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 557 | case Instruction::Select: |
| 558 | // Try to fold constant add into select arguments. |
| 559 | if (Instruction *R = FoldBinOpIntoSelect(I,cast<SelectInst>(ILHS),this)) |
| 560 | return R; |
| 561 | |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 562 | default: break; |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 567 | return Changed ? &I : 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Chris Lattner | bdb0ce0 | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 570 | // isSignBit - Return true if the value represented by the constant only has the |
| 571 | // highest order bit set. |
| 572 | static bool isSignBit(ConstantInt *CI) { |
| 573 | unsigned NumBits = CI->getType()->getPrimitiveSize()*8; |
| 574 | return (CI->getRawValue() & ~(-1LL << NumBits)) == (1ULL << (NumBits-1)); |
| 575 | } |
| 576 | |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 577 | static unsigned getTypeSizeInBits(const Type *Ty) { |
| 578 | return Ty == Type::BoolTy ? 1 : Ty->getPrimitiveSize()*8; |
| 579 | } |
| 580 | |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 581 | /// RemoveNoopCast - Strip off nonconverting casts from the value. |
| 582 | /// |
| 583 | static Value *RemoveNoopCast(Value *V) { |
| 584 | if (CastInst *CI = dyn_cast<CastInst>(V)) { |
| 585 | const Type *CTy = CI->getType(); |
| 586 | const Type *OpTy = CI->getOperand(0)->getType(); |
| 587 | if (CTy->isInteger() && OpTy->isInteger()) { |
| 588 | if (CTy->getPrimitiveSize() == OpTy->getPrimitiveSize()) |
| 589 | return RemoveNoopCast(CI->getOperand(0)); |
| 590 | } else if (isa<PointerType>(CTy) && isa<PointerType>(OpTy)) |
| 591 | return RemoveNoopCast(CI->getOperand(0)); |
| 592 | } |
| 593 | return V; |
| 594 | } |
| 595 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 596 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 597 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 598 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 599 | if (Op0 == Op1) // sub X, X -> 0 |
| 600 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 601 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 602 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 603 | if (Value *V = dyn_castNegVal(Op1)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 604 | return BinaryOperator::createAdd(Op0, V); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 605 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 606 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
| 607 | // Replace (-1 - A) with (~A)... |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 608 | if (C->isAllOnesValue()) |
| 609 | return BinaryOperator::createNot(Op1); |
Chris Lattner | ad3c495 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 610 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 611 | // C - ~X == X + (1+C) |
| 612 | if (BinaryOperator::isNot(Op1)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 613 | return BinaryOperator::createAdd( |
| 614 | BinaryOperator::getNotArgument(cast<BinaryOperator>(Op1)), |
| 615 | ConstantExpr::getAdd(C, ConstantInt::get(I.getType(), 1))); |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 616 | // -((uint)X >> 31) -> ((int)X >> 31) |
| 617 | // -((int)X >> 31) -> ((uint)X >> 31) |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 618 | if (C->isNullValue()) { |
| 619 | Value *NoopCastedRHS = RemoveNoopCast(Op1); |
| 620 | if (ShiftInst *SI = dyn_cast<ShiftInst>(NoopCastedRHS)) |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 621 | if (SI->getOpcode() == Instruction::Shr) |
| 622 | if (ConstantUInt *CU = dyn_cast<ConstantUInt>(SI->getOperand(1))) { |
| 623 | const Type *NewTy; |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 624 | if (SI->getType()->isSigned()) |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 625 | NewTy = SI->getType()->getUnsignedVersion(); |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 626 | else |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 627 | NewTy = SI->getType()->getSignedVersion(); |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 628 | // Check to see if we are shifting out everything but the sign bit. |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 629 | if (CU->getValue() == SI->getType()->getPrimitiveSize()*8-1) { |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 630 | // Ok, the transformation is safe. Insert a cast of the incoming |
| 631 | // value, then the new shift, then the new cast. |
| 632 | Instruction *FirstCast = new CastInst(SI->getOperand(0), NewTy, |
| 633 | SI->getOperand(0)->getName()); |
| 634 | Value *InV = InsertNewInstBefore(FirstCast, I); |
| 635 | Instruction *NewShift = new ShiftInst(Instruction::Shr, FirstCast, |
| 636 | CU, SI->getName()); |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 637 | if (NewShift->getType() == I.getType()) |
| 638 | return NewShift; |
| 639 | else { |
| 640 | InV = InsertNewInstBefore(NewShift, I); |
| 641 | return new CastInst(NewShift, I.getType()); |
| 642 | } |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 643 | } |
| 644 | } |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 645 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 646 | |
| 647 | // Try to fold constant sub into select arguments. |
| 648 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 649 | if (Instruction *R = FoldBinOpIntoSelect(I, SI, this)) |
| 650 | return R; |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 653 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 654 | if (Op1I->hasOneUse()) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 655 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 656 | // is not used by anyone else... |
| 657 | // |
Chris Lattner | c2f0aa5 | 2004-02-02 20:09:56 +0000 | [diff] [blame] | 658 | if (Op1I->getOpcode() == Instruction::Sub && |
| 659 | !Op1I->getType()->isFloatingPoint()) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 660 | // Swap the two operands of the subexpr... |
| 661 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 662 | Op1I->setOperand(0, IIOp1); |
| 663 | Op1I->setOperand(1, IIOp0); |
| 664 | |
| 665 | // Create the new top level add instruction... |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 666 | return BinaryOperator::createAdd(Op0, Op1); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 670 | // |
| 671 | if (Op1I->getOpcode() == Instruction::And && |
| 672 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 673 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 674 | |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 675 | Value *NewNot = |
| 676 | InsertNewInstBefore(BinaryOperator::createNot(OtherOp, "B.not"), I); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 677 | return BinaryOperator::createAnd(Op0, NewNot); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 678 | } |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 679 | |
| 680 | // X - X*C --> X * (1-C) |
| 681 | if (dyn_castFoldableMul(Op1I) == Op0) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 682 | Constant *CP1 = |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 683 | ConstantExpr::getSub(ConstantInt::get(I.getType(), 1), |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 684 | cast<Constant>(cast<Instruction>(Op1)->getOperand(1))); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 685 | assert(CP1 && "Couldn't constant fold 1-C?"); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 686 | return BinaryOperator::createMul(Op0, CP1); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 687 | } |
Chris Lattner | ad3c495 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 688 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 689 | |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 690 | // X*C - X --> X * (C-1) |
| 691 | if (dyn_castFoldableMul(Op0) == Op1) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 692 | Constant *CP1 = |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 693 | ConstantExpr::getSub(cast<Constant>(cast<Instruction>(Op0)->getOperand(1)), |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 694 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 695 | assert(CP1 && "Couldn't constant fold C - 1?"); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 696 | return BinaryOperator::createMul(Op1, CP1); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 699 | return 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 702 | /// isSignBitCheck - Given an exploded setcc instruction, return true if it is |
| 703 | /// really just returns true if the most significant (sign) bit is set. |
| 704 | static bool isSignBitCheck(unsigned Opcode, Value *LHS, ConstantInt *RHS) { |
| 705 | if (RHS->getType()->isSigned()) { |
| 706 | // True if source is LHS < 0 or LHS <= -1 |
| 707 | return Opcode == Instruction::SetLT && RHS->isNullValue() || |
| 708 | Opcode == Instruction::SetLE && RHS->isAllOnesValue(); |
| 709 | } else { |
| 710 | ConstantUInt *RHSC = cast<ConstantUInt>(RHS); |
| 711 | // True if source is LHS > 127 or LHS >= 128, where the constants depend on |
| 712 | // the size of the integer type. |
| 713 | if (Opcode == Instruction::SetGE) |
| 714 | return RHSC->getValue() == 1ULL<<(RHS->getType()->getPrimitiveSize()*8-1); |
| 715 | if (Opcode == Instruction::SetGT) |
| 716 | return RHSC->getValue() == |
| 717 | (1ULL << (RHS->getType()->getPrimitiveSize()*8-1))-1; |
| 718 | } |
| 719 | return false; |
| 720 | } |
| 721 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 722 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 723 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 724 | Value *Op0 = I.getOperand(0); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 725 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 726 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 727 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 728 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 729 | |
| 730 | // ((X << C1)*C2) == (X * (C2 << C1)) |
| 731 | if (ShiftInst *SI = dyn_cast<ShiftInst>(Op0)) |
| 732 | if (SI->getOpcode() == Instruction::Shl) |
| 733 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 734 | return BinaryOperator::createMul(SI->getOperand(0), |
| 735 | ConstantExpr::getShl(CI, ShOp)); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 736 | |
Chris Lattner | cce81be | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 737 | if (CI->isNullValue()) |
| 738 | return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 |
| 739 | if (CI->equalsInt(1)) // X * 1 == X |
| 740 | return ReplaceInstUsesWith(I, Op0); |
| 741 | if (CI->isAllOnesValue()) // X * -1 == 0 - X |
Chris Lattner | 35236d8 | 2003-06-25 17:09:20 +0000 | [diff] [blame] | 742 | return BinaryOperator::createNeg(Op0, I.getName()); |
Chris Lattner | 31ba129 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 743 | |
Chris Lattner | cce81be | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 744 | int64_t Val = (int64_t)cast<ConstantInt>(CI)->getRawValue(); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 745 | if (uint64_t C = Log2(Val)) // Replace X*(2^C) with X << C |
| 746 | return new ShiftInst(Instruction::Shl, Op0, |
| 747 | ConstantUInt::get(Type::UByteTy, C)); |
| 748 | } else { |
| 749 | ConstantFP *Op1F = cast<ConstantFP>(Op1); |
| 750 | if (Op1F->isNullValue()) |
| 751 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 31ba129 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 752 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 753 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 754 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) |
| 755 | if (Op1F->getValue() == 1.0) |
| 756 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
| 757 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 758 | |
| 759 | // Try to fold constant mul into select arguments. |
| 760 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 761 | if (Instruction *R = FoldBinOpIntoSelect(I, SI, this)) |
| 762 | return R; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Chris Lattner | 934a64cf | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 765 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
| 766 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 767 | return BinaryOperator::createMul(Op0v, Op1v); |
Chris Lattner | 934a64cf | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 768 | |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 769 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 770 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 771 | // See if we can simplify things based on how the boolean was originally |
| 772 | // formed. |
| 773 | CastInst *BoolCast = 0; |
| 774 | if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(0))) |
| 775 | if (CI->getOperand(0)->getType() == Type::BoolTy) |
| 776 | BoolCast = CI; |
| 777 | if (!BoolCast) |
| 778 | if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(1))) |
| 779 | if (CI->getOperand(0)->getType() == Type::BoolTy) |
| 780 | BoolCast = CI; |
| 781 | if (BoolCast) { |
| 782 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(BoolCast->getOperand(0))) { |
| 783 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); |
| 784 | const Type *SCOpTy = SCIOp0->getType(); |
| 785 | |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 786 | // If the setcc is true iff the sign bit of X is set, then convert this |
| 787 | // multiply into a shift/and combination. |
| 788 | if (isa<ConstantInt>(SCIOp1) && |
| 789 | isSignBitCheck(SCI->getOpcode(), SCIOp0, cast<ConstantInt>(SCIOp1))) { |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 790 | // Shift the X value right to turn it into "all signbits". |
| 791 | Constant *Amt = ConstantUInt::get(Type::UByteTy, |
| 792 | SCOpTy->getPrimitiveSize()*8-1); |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 793 | if (SCIOp0->getType()->isUnsigned()) { |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 794 | const Type *NewTy = SCIOp0->getType()->getSignedVersion(); |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 795 | SCIOp0 = InsertNewInstBefore(new CastInst(SCIOp0, NewTy, |
| 796 | SCIOp0->getName()), I); |
| 797 | } |
| 798 | |
| 799 | Value *V = |
| 800 | InsertNewInstBefore(new ShiftInst(Instruction::Shr, SCIOp0, Amt, |
| 801 | BoolCast->getOperand(0)->getName()+ |
| 802 | ".mask"), I); |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 803 | |
| 804 | // If the multiply type is not the same as the source type, sign extend |
| 805 | // or truncate to the multiply type. |
| 806 | if (I.getType() != V->getType()) |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 807 | V = InsertNewInstBefore(new CastInst(V, I.getType(), V->getName()),I); |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 808 | |
| 809 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 810 | return BinaryOperator::createAnd(V, OtherOp); |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | } |
| 814 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 815 | return Changed ? &I : 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 818 | Instruction *InstCombiner::visitDiv(BinaryOperator &I) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 819 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) { |
Chris Lattner | e20c334 | 2004-04-26 14:01:59 +0000 | [diff] [blame] | 820 | // div X, 1 == X |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 821 | if (RHS->equalsInt(1)) |
| 822 | return ReplaceInstUsesWith(I, I.getOperand(0)); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 823 | |
Chris Lattner | e20c334 | 2004-04-26 14:01:59 +0000 | [diff] [blame] | 824 | // div X, -1 == -X |
| 825 | if (RHS->isAllOnesValue()) |
| 826 | return BinaryOperator::createNeg(I.getOperand(0)); |
| 827 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 828 | // Check to see if this is an unsigned division with an exact power of 2, |
| 829 | // if so, convert to a right shift. |
| 830 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS)) |
| 831 | if (uint64_t Val = C->getValue()) // Don't break X / 0 |
| 832 | if (uint64_t C = Log2(Val)) |
| 833 | return new ShiftInst(Instruction::Shr, I.getOperand(0), |
| 834 | ConstantUInt::get(Type::UByteTy, C)); |
| 835 | } |
| 836 | |
| 837 | // 0 / X == 0, we don't need to preserve faults! |
| 838 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(I.getOperand(0))) |
| 839 | if (LHS->equalsInt(0)) |
| 840 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 841 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 842 | return 0; |
| 843 | } |
| 844 | |
| 845 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 846 | Instruction *InstCombiner::visitRem(BinaryOperator &I) { |
Chris Lattner | 7fd5f07 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 847 | if (I.getType()->isSigned()) |
| 848 | if (Value *RHSNeg = dyn_castNegVal(I.getOperand(1))) |
Chris Lattner | 98c6bdf | 2004-07-06 07:11:42 +0000 | [diff] [blame] | 849 | if (!isa<ConstantSInt>(RHSNeg) || |
| 850 | cast<ConstantSInt>(RHSNeg)->getValue() >= 0) { |
Chris Lattner | 7fd5f07 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 851 | // X % -Y -> X % Y |
| 852 | AddUsesToWorkList(I); |
| 853 | I.setOperand(1, RHSNeg); |
| 854 | return &I; |
| 855 | } |
| 856 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 857 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) { |
| 858 | if (RHS->equalsInt(1)) // X % 1 == 0 |
| 859 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 860 | |
| 861 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 862 | // if so, convert to a bitwise and. |
| 863 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS)) |
| 864 | if (uint64_t Val = C->getValue()) // Don't break X % 0 (divide by zero) |
Chris Lattner | d9e5813 | 2004-05-07 15:35:56 +0000 | [diff] [blame] | 865 | if (!(Val & (Val-1))) // Power of 2 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 866 | return BinaryOperator::createAnd(I.getOperand(0), |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 867 | ConstantUInt::get(I.getType(), Val-1)); |
| 868 | } |
| 869 | |
| 870 | // 0 % X == 0, we don't need to preserve faults! |
| 871 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(I.getOperand(0))) |
| 872 | if (LHS->equalsInt(0)) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 873 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 874 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 875 | return 0; |
| 876 | } |
| 877 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 878 | // isMaxValueMinusOne - return true if this is Max-1 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 879 | static bool isMaxValueMinusOne(const ConstantInt *C) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 880 | if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) { |
| 881 | // Calculate -1 casted to the right type... |
| 882 | unsigned TypeBits = C->getType()->getPrimitiveSize()*8; |
| 883 | uint64_t Val = ~0ULL; // All ones |
| 884 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 885 | return CU->getValue() == Val-1; |
| 886 | } |
| 887 | |
| 888 | const ConstantSInt *CS = cast<ConstantSInt>(C); |
| 889 | |
| 890 | // Calculate 0111111111..11111 |
| 891 | unsigned TypeBits = C->getType()->getPrimitiveSize()*8; |
| 892 | int64_t Val = INT64_MAX; // All ones |
| 893 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 894 | return CS->getValue() == Val-1; |
| 895 | } |
| 896 | |
| 897 | // isMinValuePlusOne - return true if this is Min+1 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 898 | static bool isMinValuePlusOne(const ConstantInt *C) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 899 | if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) |
| 900 | return CU->getValue() == 1; |
| 901 | |
| 902 | const ConstantSInt *CS = cast<ConstantSInt>(C); |
| 903 | |
| 904 | // Calculate 1111111111000000000000 |
| 905 | unsigned TypeBits = C->getType()->getPrimitiveSize()*8; |
| 906 | int64_t Val = -1; // All ones |
| 907 | Val <<= TypeBits-1; // Shift over to the right spot |
| 908 | return CS->getValue() == Val+1; |
| 909 | } |
| 910 | |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 911 | // isOneBitSet - Return true if there is exactly one bit set in the specified |
| 912 | // constant. |
| 913 | static bool isOneBitSet(const ConstantInt *CI) { |
| 914 | uint64_t V = CI->getRawValue(); |
| 915 | return V && (V & (V-1)) == 0; |
| 916 | } |
| 917 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 918 | /// getSetCondCode - Encode a setcc opcode into a three bit mask. These bits |
| 919 | /// are carefully arranged to allow folding of expressions such as: |
| 920 | /// |
| 921 | /// (A < B) | (A > B) --> (A != B) |
| 922 | /// |
| 923 | /// Bit value '4' represents that the comparison is true if A > B, bit value '2' |
| 924 | /// represents that the comparison is true if A == B, and bit value '1' is true |
| 925 | /// if A < B. |
| 926 | /// |
| 927 | static unsigned getSetCondCode(const SetCondInst *SCI) { |
| 928 | switch (SCI->getOpcode()) { |
| 929 | // False -> 0 |
| 930 | case Instruction::SetGT: return 1; |
| 931 | case Instruction::SetEQ: return 2; |
| 932 | case Instruction::SetGE: return 3; |
| 933 | case Instruction::SetLT: return 4; |
| 934 | case Instruction::SetNE: return 5; |
| 935 | case Instruction::SetLE: return 6; |
| 936 | // True -> 7 |
| 937 | default: |
| 938 | assert(0 && "Invalid SetCC opcode!"); |
| 939 | return 0; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | /// getSetCCValue - This is the complement of getSetCondCode, which turns an |
| 944 | /// opcode and two operands into either a constant true or false, or a brand new |
| 945 | /// SetCC instruction. |
| 946 | static Value *getSetCCValue(unsigned Opcode, Value *LHS, Value *RHS) { |
| 947 | switch (Opcode) { |
| 948 | case 0: return ConstantBool::False; |
| 949 | case 1: return new SetCondInst(Instruction::SetGT, LHS, RHS); |
| 950 | case 2: return new SetCondInst(Instruction::SetEQ, LHS, RHS); |
| 951 | case 3: return new SetCondInst(Instruction::SetGE, LHS, RHS); |
| 952 | case 4: return new SetCondInst(Instruction::SetLT, LHS, RHS); |
| 953 | case 5: return new SetCondInst(Instruction::SetNE, LHS, RHS); |
| 954 | case 6: return new SetCondInst(Instruction::SetLE, LHS, RHS); |
| 955 | case 7: return ConstantBool::True; |
| 956 | default: assert(0 && "Illegal SetCCCode!"); return 0; |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | // FoldSetCCLogical - Implements (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B) |
| 961 | struct FoldSetCCLogical { |
| 962 | InstCombiner &IC; |
| 963 | Value *LHS, *RHS; |
| 964 | FoldSetCCLogical(InstCombiner &ic, SetCondInst *SCI) |
| 965 | : IC(ic), LHS(SCI->getOperand(0)), RHS(SCI->getOperand(1)) {} |
| 966 | bool shouldApply(Value *V) const { |
| 967 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(V)) |
| 968 | return (SCI->getOperand(0) == LHS && SCI->getOperand(1) == RHS || |
| 969 | SCI->getOperand(0) == RHS && SCI->getOperand(1) == LHS); |
| 970 | return false; |
| 971 | } |
| 972 | Instruction *apply(BinaryOperator &Log) const { |
| 973 | SetCondInst *SCI = cast<SetCondInst>(Log.getOperand(0)); |
| 974 | if (SCI->getOperand(0) != LHS) { |
| 975 | assert(SCI->getOperand(1) == LHS); |
| 976 | SCI->swapOperands(); // Swap the LHS and RHS of the SetCC |
| 977 | } |
| 978 | |
| 979 | unsigned LHSCode = getSetCondCode(SCI); |
| 980 | unsigned RHSCode = getSetCondCode(cast<SetCondInst>(Log.getOperand(1))); |
| 981 | unsigned Code; |
| 982 | switch (Log.getOpcode()) { |
| 983 | case Instruction::And: Code = LHSCode & RHSCode; break; |
| 984 | case Instruction::Or: Code = LHSCode | RHSCode; break; |
| 985 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; |
Chris Lattner | 2caaaba | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 986 | default: assert(0 && "Illegal logical opcode!"); return 0; |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | Value *RV = getSetCCValue(Code, LHS, RHS); |
| 990 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 991 | return I; |
| 992 | // Otherwise, it's a constant boolean value... |
| 993 | return IC.ReplaceInstUsesWith(Log, RV); |
| 994 | } |
| 995 | }; |
| 996 | |
| 997 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 998 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
| 999 | // the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'. Op is |
| 1000 | // guaranteed to be either a shift instruction or a binary operator. |
| 1001 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
| 1002 | ConstantIntegral *OpRHS, |
| 1003 | ConstantIntegral *AndRHS, |
| 1004 | BinaryOperator &TheAnd) { |
| 1005 | Value *X = Op->getOperand(0); |
Chris Lattner | fcf21a7 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 1006 | Constant *Together = 0; |
| 1007 | if (!isa<ShiftInst>(Op)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1008 | Together = ConstantExpr::getAnd(AndRHS, OpRHS); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1009 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1010 | switch (Op->getOpcode()) { |
| 1011 | case Instruction::Xor: |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1012 | if (Together->isNullValue()) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1013 | // (X ^ C1) & C2 --> (X & C2) iff (C1&C2) == 0 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1014 | return BinaryOperator::createAnd(X, AndRHS); |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1015 | } else if (Op->hasOneUse()) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1016 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
| 1017 | std::string OpName = Op->getName(); Op->setName(""); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1018 | Instruction *And = BinaryOperator::createAnd(X, AndRHS, OpName); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1019 | InsertNewInstBefore(And, TheAnd); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1020 | return BinaryOperator::createXor(And, Together); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1021 | } |
| 1022 | break; |
| 1023 | case Instruction::Or: |
| 1024 | // (X | C1) & C2 --> X & C2 iff C1 & C1 == 0 |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1025 | if (Together->isNullValue()) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1026 | return BinaryOperator::createAnd(X, AndRHS); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1027 | else { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1028 | if (Together == AndRHS) // (X | C) & C --> C |
| 1029 | return ReplaceInstUsesWith(TheAnd, AndRHS); |
| 1030 | |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1031 | if (Op->hasOneUse() && Together != OpRHS) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1032 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 |
| 1033 | std::string Op0Name = Op->getName(); Op->setName(""); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1034 | Instruction *Or = BinaryOperator::createOr(X, Together, Op0Name); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1035 | InsertNewInstBefore(Or, TheAnd); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1036 | return BinaryOperator::createAnd(Or, AndRHS); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1037 | } |
| 1038 | } |
| 1039 | break; |
| 1040 | case Instruction::Add: |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1041 | if (Op->hasOneUse()) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1042 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 1043 | // of the bit. First thing to check is to see if this AND is with a |
| 1044 | // single bit constant. |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1045 | uint64_t AndRHSV = cast<ConstantInt>(AndRHS)->getRawValue(); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1046 | |
| 1047 | // Clear bits that are not part of the constant. |
| 1048 | AndRHSV &= (1ULL << AndRHS->getType()->getPrimitiveSize()*8)-1; |
| 1049 | |
| 1050 | // If there is only one bit set... |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1051 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1052 | // Ok, at this point, we know that we are masking the result of the |
| 1053 | // ADD down to exactly one bit. If the constant we are adding has |
| 1054 | // no bits set below this bit, then we can eliminate the ADD. |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1055 | uint64_t AddRHS = cast<ConstantInt>(OpRHS)->getRawValue(); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1056 | |
| 1057 | // Check to see if any bits below the one bit set in AndRHSV are set. |
| 1058 | if ((AddRHS & (AndRHSV-1)) == 0) { |
| 1059 | // If not, the only thing that can effect the output of the AND is |
| 1060 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 1061 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 1062 | // no effect. |
| 1063 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop |
| 1064 | TheAnd.setOperand(0, X); |
| 1065 | return &TheAnd; |
| 1066 | } else { |
| 1067 | std::string Name = Op->getName(); Op->setName(""); |
| 1068 | // Pull the XOR out of the AND. |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1069 | Instruction *NewAnd = BinaryOperator::createAnd(X, AndRHS, Name); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1070 | InsertNewInstBefore(NewAnd, TheAnd); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1071 | return BinaryOperator::createXor(NewAnd, AndRHS); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | } |
| 1076 | break; |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1077 | |
| 1078 | case Instruction::Shl: { |
| 1079 | // We know that the AND will not produce any of the bits shifted in, so if |
| 1080 | // the anded constant includes them, clear them now! |
| 1081 | // |
| 1082 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1083 | Constant *CI = ConstantExpr::getAnd(AndRHS, |
| 1084 | ConstantExpr::getShl(AllOne, OpRHS)); |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1085 | if (CI != AndRHS) { |
| 1086 | TheAnd.setOperand(1, CI); |
| 1087 | return &TheAnd; |
| 1088 | } |
| 1089 | break; |
| 1090 | } |
| 1091 | case Instruction::Shr: |
| 1092 | // We know that the AND will not produce any of the bits shifted in, so if |
| 1093 | // the anded constant includes them, clear them now! This only applies to |
| 1094 | // unsigned shifts, because a signed shr may bring in set bits! |
| 1095 | // |
| 1096 | if (AndRHS->getType()->isUnsigned()) { |
| 1097 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1098 | Constant *CI = ConstantExpr::getAnd(AndRHS, |
| 1099 | ConstantExpr::getShr(AllOne, OpRHS)); |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1100 | if (CI != AndRHS) { |
| 1101 | TheAnd.setOperand(1, CI); |
| 1102 | return &TheAnd; |
| 1103 | } |
| 1104 | } |
| 1105 | break; |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1106 | } |
| 1107 | return 0; |
| 1108 | } |
| 1109 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1110 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1111 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1112 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1113 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1114 | |
| 1115 | // and X, X = X and X, 0 == 0 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1116 | if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) |
| 1117 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1118 | |
| 1119 | // and X, -1 == X |
Chris Lattner | 49b47ae | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 1120 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1121 | if (RHS->isAllOnesValue()) |
| 1122 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1123 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1124 | // Optimize a variety of ((val OP C1) & C2) combinations... |
| 1125 | if (isa<BinaryOperator>(Op0) || isa<ShiftInst>(Op0)) { |
| 1126 | Instruction *Op0I = cast<Instruction>(Op0); |
Chris Lattner | 33217db | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 1127 | Value *X = Op0I->getOperand(0); |
Chris Lattner | 16464b3 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 1128 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1129 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, RHS, I)) |
| 1130 | return Res; |
Chris Lattner | 33217db | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 1131 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1132 | |
| 1133 | // Try to fold constant and into select arguments. |
| 1134 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 1135 | if (Instruction *R = FoldBinOpIntoSelect(I, SI, this)) |
| 1136 | return R; |
Chris Lattner | 49b47ae | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1139 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 1140 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1141 | |
Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 1142 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
| 1143 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1144 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1145 | // (~A & ~B) == (~(A | B)) - Demorgan's Law |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1146 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1147 | Instruction *Or = BinaryOperator::createOr(Op0NotVal, Op1NotVal, |
| 1148 | I.getName()+".demorgan"); |
Chris Lattner | 49b47ae | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 1149 | InsertNewInstBefore(Or, I); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1150 | return BinaryOperator::createNot(Or); |
| 1151 | } |
| 1152 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1153 | // (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B) |
| 1154 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1))) |
| 1155 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 1156 | return R; |
| 1157 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1158 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | |
| 1162 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1163 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1164 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1165 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1166 | |
| 1167 | // or X, X = X or X, 0 == X |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1168 | if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) |
| 1169 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1170 | |
| 1171 | // or X, -1 == -1 |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1172 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1173 | if (RHS->isAllOnesValue()) |
| 1174 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1175 | |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1176 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { |
| 1177 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
| 1178 | if (Op0I->getOpcode() == Instruction::And && isOnlyUse(Op0)) |
| 1179 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) { |
| 1180 | std::string Op0Name = Op0I->getName(); Op0I->setName(""); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1181 | Instruction *Or = BinaryOperator::createOr(Op0I->getOperand(0), RHS, |
| 1182 | Op0Name); |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1183 | InsertNewInstBefore(Or, I); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1184 | return BinaryOperator::createAnd(Or, ConstantExpr::getOr(RHS, Op0CI)); |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
| 1188 | if (Op0I->getOpcode() == Instruction::Xor && isOnlyUse(Op0)) |
| 1189 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) { |
| 1190 | std::string Op0Name = Op0I->getName(); Op0I->setName(""); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1191 | Instruction *Or = BinaryOperator::createOr(Op0I->getOperand(0), RHS, |
| 1192 | Op0Name); |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1193 | InsertNewInstBefore(Or, I); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1194 | return BinaryOperator::createXor(Or, |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 1195 | ConstantExpr::getAnd(Op0CI, |
| 1196 | ConstantExpr::getNot(RHS))); |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1197 | } |
| 1198 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1199 | |
| 1200 | // Try to fold constant and into select arguments. |
| 1201 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 1202 | if (Instruction *R = FoldBinOpIntoSelect(I, SI, this)) |
| 1203 | return R; |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
Chris Lattner | 812aab7 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 1206 | // (A & C1)|(A & C2) == A & (C1|C2) |
Chris Lattner | 01d5639 | 2003-08-12 19:17:27 +0000 | [diff] [blame] | 1207 | if (Instruction *LHS = dyn_cast<BinaryOperator>(Op0)) |
| 1208 | if (Instruction *RHS = dyn_cast<BinaryOperator>(Op1)) |
| 1209 | if (LHS->getOperand(0) == RHS->getOperand(0)) |
| 1210 | if (Constant *C0 = dyn_castMaskingAnd(LHS)) |
| 1211 | if (Constant *C1 = dyn_castMaskingAnd(RHS)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1212 | return BinaryOperator::createAnd(LHS->getOperand(0), |
| 1213 | ConstantExpr::getOr(C0, C1)); |
Chris Lattner | 812aab7 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 1214 | |
Chris Lattner | 3e327a4 | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 1215 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 1216 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1217 | |
Chris Lattner | 3e327a4 | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 1218 | if (Op1 == Op0NotVal) // ~A | A == -1 |
| 1219 | return ReplaceInstUsesWith(I, |
| 1220 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 1221 | |
| 1222 | if (Op0 == Op1NotVal) // A | ~A == -1 |
| 1223 | return ReplaceInstUsesWith(I, |
| 1224 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 1225 | |
| 1226 | // (~A | ~B) == (~(A & B)) - Demorgan's Law |
| 1227 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 1228 | Value *And = InsertNewInstBefore( |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1229 | BinaryOperator::createAnd(Op0NotVal, |
| 1230 | Op1NotVal,I.getName()+".demorgan"),I); |
Chris Lattner | 3e327a4 | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 1231 | return BinaryOperator::createNot(And); |
| 1232 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1233 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1234 | // (setcc1 A, B) | (setcc2 A, B) --> (setcc3 A, B) |
| 1235 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1))) |
| 1236 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 1237 | return R; |
| 1238 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1239 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 1242 | // XorSelf - Implements: X ^ X --> 0 |
| 1243 | struct XorSelf { |
| 1244 | Value *RHS; |
| 1245 | XorSelf(Value *rhs) : RHS(rhs) {} |
| 1246 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 1247 | Instruction *apply(BinaryOperator &Xor) const { |
| 1248 | return &Xor; |
| 1249 | } |
| 1250 | }; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1251 | |
| 1252 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1253 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1254 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1255 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1256 | |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 1257 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
| 1258 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { |
| 1259 | assert(Result == &I && "AssociativeOpt didn't work?"); |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1260 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 1261 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1262 | |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1263 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1264 | // xor X, 0 == X |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1265 | if (RHS->isNullValue()) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1266 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1267 | |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1268 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 1269 | // xor (setcc A, B), true = not (setcc A, B) = setncc A, B |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1270 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(Op0I)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1271 | if (RHS == ConstantBool::True && SCI->hasOneUse()) |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 1272 | return new SetCondInst(SCI->getInverseCondition(), |
| 1273 | SCI->getOperand(0), SCI->getOperand(1)); |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 1274 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 1275 | // ~(c-X) == X-c-1 == X+(-c-1) |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1276 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
| 1277 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1278 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); |
| 1279 | Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1280 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1281 | return BinaryOperator::createAdd(Op0I->getOperand(1), ConstantRHS); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1282 | } |
Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 1283 | |
| 1284 | // ~(~X & Y) --> (X | ~Y) |
| 1285 | if (Op0I->getOpcode() == Instruction::And && RHS->isAllOnesValue()) { |
| 1286 | if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands(); |
| 1287 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { |
| 1288 | Instruction *NotY = |
| 1289 | BinaryOperator::createNot(Op0I->getOperand(1), |
| 1290 | Op0I->getOperand(1)->getName()+".not"); |
| 1291 | InsertNewInstBefore(NotY, I); |
| 1292 | return BinaryOperator::createOr(Op0NotVal, NotY); |
| 1293 | } |
| 1294 | } |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1295 | |
| 1296 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 1297 | switch (Op0I->getOpcode()) { |
| 1298 | case Instruction::Add: |
Chris Lattner | 0f68fa6 | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 1299 | // ~(X-c) --> (-c-1)-X |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1300 | if (RHS->isAllOnesValue()) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1301 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); |
| 1302 | return BinaryOperator::createSub( |
| 1303 | ConstantExpr::getSub(NegOp0CI, |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1304 | ConstantInt::get(I.getType(), 1)), |
Chris Lattner | 0f68fa6 | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 1305 | Op0I->getOperand(0)); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1306 | } |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 1307 | break; |
| 1308 | case Instruction::And: |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1309 | // (X & C1) ^ C2 --> (X & C1) | C2 iff (C1&C2) == 0 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1310 | if (ConstantExpr::getAnd(RHS, Op0CI)->isNullValue()) |
| 1311 | return BinaryOperator::createOr(Op0, RHS); |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 1312 | break; |
| 1313 | case Instruction::Or: |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1314 | // (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1315 | if (ConstantExpr::getAnd(RHS, Op0CI) == RHS) |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 1316 | return BinaryOperator::createAnd(Op0, ConstantExpr::getNot(RHS)); |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 1317 | break; |
| 1318 | default: break; |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1319 | } |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 1320 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1321 | |
| 1322 | // Try to fold constant and into select arguments. |
| 1323 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 1324 | if (Instruction *R = FoldBinOpIntoSelect(I, SI, this)) |
| 1325 | return R; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1328 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1329 | if (X == Op1) |
| 1330 | return ReplaceInstUsesWith(I, |
| 1331 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 1332 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1333 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1334 | if (X == Op0) |
| 1335 | return ReplaceInstUsesWith(I, |
| 1336 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 1337 | |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1338 | if (Instruction *Op1I = dyn_cast<Instruction>(Op1)) |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 1339 | if (Op1I->getOpcode() == Instruction::Or) { |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1340 | if (Op1I->getOperand(0) == Op0) { // B^(B|A) == (A|B)^B |
| 1341 | cast<BinaryOperator>(Op1I)->swapOperands(); |
| 1342 | I.swapOperands(); |
| 1343 | std::swap(Op0, Op1); |
| 1344 | } else if (Op1I->getOperand(1) == Op0) { // B^(A|B) == (A|B)^B |
| 1345 | I.swapOperands(); |
| 1346 | std::swap(Op0, Op1); |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 1347 | } |
| 1348 | } else if (Op1I->getOpcode() == Instruction::Xor) { |
| 1349 | if (Op0 == Op1I->getOperand(0)) // A^(A^B) == B |
| 1350 | return ReplaceInstUsesWith(I, Op1I->getOperand(1)); |
| 1351 | else if (Op0 == Op1I->getOperand(1)) // A^(B^A) == B |
| 1352 | return ReplaceInstUsesWith(I, Op1I->getOperand(0)); |
| 1353 | } |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1354 | |
| 1355 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1356 | if (Op0I->getOpcode() == Instruction::Or && Op0I->hasOneUse()) { |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1357 | if (Op0I->getOperand(0) == Op1) // (B|A)^B == (A|B)^B |
| 1358 | cast<BinaryOperator>(Op0I)->swapOperands(); |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1359 | if (Op0I->getOperand(1) == Op1) { // (A|B)^B == A & ~B |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 1360 | Value *NotB = InsertNewInstBefore(BinaryOperator::createNot(Op1, |
| 1361 | Op1->getName()+".not"), I); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1362 | return BinaryOperator::createAnd(Op0I->getOperand(0), NotB); |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1363 | } |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 1364 | } else if (Op0I->getOpcode() == Instruction::Xor) { |
| 1365 | if (Op1 == Op0I->getOperand(0)) // (A^B)^A == B |
| 1366 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 1367 | else if (Op1 == Op0I->getOperand(1)) // (B^A)^A == B |
| 1368 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 1371 | // (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1^C2 == 0 |
| 1372 | if (Constant *C1 = dyn_castMaskingAnd(Op0)) |
| 1373 | if (Constant *C2 = dyn_castMaskingAnd(Op1)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1374 | if (ConstantExpr::getAnd(C1, C2)->isNullValue()) |
| 1375 | return BinaryOperator::createOr(Op0, Op1); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 1376 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1377 | // (setcc1 A, B) ^ (setcc2 A, B) --> (setcc3 A, B) |
| 1378 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1))) |
| 1379 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 1380 | return R; |
| 1381 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1382 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1385 | // AddOne, SubOne - Add or subtract a constant one from an integer constant... |
| 1386 | static Constant *AddOne(ConstantInt *C) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1387 | return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1)); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1388 | } |
| 1389 | static Constant *SubOne(ConstantInt *C) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1390 | return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1)); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
Chris Lattner | 1fc23f3 | 2002-05-09 20:11:54 +0000 | [diff] [blame] | 1393 | // isTrueWhenEqual - Return true if the specified setcondinst instruction is |
| 1394 | // true when both operands are equal... |
| 1395 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1396 | static bool isTrueWhenEqual(Instruction &I) { |
| 1397 | return I.getOpcode() == Instruction::SetEQ || |
| 1398 | I.getOpcode() == Instruction::SetGE || |
| 1399 | I.getOpcode() == Instruction::SetLE; |
Chris Lattner | 1fc23f3 | 2002-05-09 20:11:54 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1402 | Instruction *InstCombiner::visitSetCondInst(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1403 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1404 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1405 | const Type *Ty = Op0->getType(); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1406 | |
| 1407 | // setcc X, X |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1408 | if (Op0 == Op1) |
| 1409 | return ReplaceInstUsesWith(I, ConstantBool::get(isTrueWhenEqual(I))); |
Chris Lattner | 1fc23f3 | 2002-05-09 20:11:54 +0000 | [diff] [blame] | 1410 | |
Chris Lattner | d07283a | 2003-08-13 05:38:46 +0000 | [diff] [blame] | 1411 | // setcc <global/alloca*>, 0 - Global/Stack value addresses are never null! |
| 1412 | if (isa<ConstantPointerNull>(Op1) && |
| 1413 | (isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0))) |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1414 | return ReplaceInstUsesWith(I, ConstantBool::get(!isTrueWhenEqual(I))); |
| 1415 | |
Chris Lattner | d07283a | 2003-08-13 05:38:46 +0000 | [diff] [blame] | 1416 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1417 | // setcc's with boolean values can always be turned into bitwise operations |
| 1418 | if (Ty == Type::BoolTy) { |
| 1419 | // If this is <, >, or !=, we can change this into a simple xor instruction |
| 1420 | if (!isTrueWhenEqual(I)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1421 | return BinaryOperator::createXor(Op0, Op1); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1422 | |
| 1423 | // Otherwise we need to make a temporary intermediate instruction and insert |
| 1424 | // it into the instruction stream. This is what we are after: |
| 1425 | // |
| 1426 | // seteq bool %A, %B -> ~(A^B) |
| 1427 | // setle bool %A, %B -> ~A | B |
| 1428 | // setge bool %A, %B -> A | ~B |
| 1429 | // |
| 1430 | if (I.getOpcode() == Instruction::SetEQ) { // seteq case |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1431 | Instruction *Xor = BinaryOperator::createXor(Op0, Op1, I.getName()+"tmp"); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1432 | InsertNewInstBefore(Xor, I); |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 1433 | return BinaryOperator::createNot(Xor); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | // Handle the setXe cases... |
| 1437 | assert(I.getOpcode() == Instruction::SetGE || |
| 1438 | I.getOpcode() == Instruction::SetLE); |
| 1439 | |
| 1440 | if (I.getOpcode() == Instruction::SetGE) |
| 1441 | std::swap(Op0, Op1); // Change setge -> setle |
| 1442 | |
| 1443 | // Now we just have the SetLE case. |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 1444 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1445 | InsertNewInstBefore(Not, I); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1446 | return BinaryOperator::createOr(Not, Op1); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
Chris Lattner | 2dd0174 | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 1449 | // See if we are doing a comparison between a constant and an instruction that |
| 1450 | // can be folded into the comparison. |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1451 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | e1e10e1 | 2004-05-25 06:32:08 +0000 | [diff] [blame] | 1452 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
Chris Lattner | 2dd0174 | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 1453 | if (LHSI->hasOneUse()) |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1454 | switch (LHSI->getOpcode()) { |
| 1455 | case Instruction::And: |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 1456 | if (isa<ConstantInt>(LHSI->getOperand(1)) && |
| 1457 | LHSI->getOperand(0)->hasOneUse()) { |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1458 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare |
| 1459 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This |
| 1460 | // happens a LOT in code produced by the C front-end, for bitfield |
| 1461 | // access. |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 1462 | ShiftInst *Shift = dyn_cast<ShiftInst>(LHSI->getOperand(0)); |
| 1463 | ConstantUInt *ShAmt; |
| 1464 | ShAmt = Shift ? dyn_cast<ConstantUInt>(Shift->getOperand(1)) : 0; |
| 1465 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); |
| 1466 | const Type *Ty = LHSI->getType(); |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1467 | |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 1468 | // We can fold this as long as we can't shift unknown bits |
| 1469 | // into the mask. This can only happen with signed shift |
| 1470 | // rights, as they sign-extend. |
| 1471 | if (ShAmt) { |
| 1472 | bool CanFold = Shift->getOpcode() != Instruction::Shr || |
| 1473 | Shift->getType()->isUnsigned(); |
Chris Lattner | d8f5e2c | 2004-07-21 20:14:10 +0000 | [diff] [blame] | 1474 | if (!CanFold) { |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 1475 | // To test for the bad case of the signed shr, see if any |
| 1476 | // of the bits shifted in could be tested after the mask. |
Chris Lattner | d8f5e2c | 2004-07-21 20:14:10 +0000 | [diff] [blame] | 1477 | Constant *OShAmt = ConstantUInt::get(Type::UByteTy, |
| 1478 | Ty->getPrimitiveSize()*8-ShAmt->getValue()); |
| 1479 | Constant *ShVal = |
| 1480 | ConstantExpr::getShl(ConstantInt::getAllOnesValue(Ty), OShAmt); |
| 1481 | if (ConstantExpr::getAnd(ShVal, AndCST)->isNullValue()) |
| 1482 | CanFold = true; |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | if (CanFold) { |
| 1486 | unsigned ShiftOp = Shift->getOpcode() == Instruction::Shl |
| 1487 | ? Instruction::Shr : Instruction::Shl; |
Chris Lattner | d8f5e2c | 2004-07-21 20:14:10 +0000 | [diff] [blame] | 1488 | Constant *NewCst = ConstantExpr::get(ShiftOp, CI, ShAmt); |
| 1489 | |
| 1490 | // Check to see if we are shifting out any of the bits being |
| 1491 | // compared. |
| 1492 | if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != CI){ |
| 1493 | // If we shifted bits out, the fold is not going to work out. |
| 1494 | // As a special case, check to see if this means that the |
| 1495 | // result is always true or false now. |
| 1496 | if (I.getOpcode() == Instruction::SetEQ) |
| 1497 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1498 | if (I.getOpcode() == Instruction::SetNE) |
| 1499 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 1500 | } else { |
| 1501 | I.setOperand(1, NewCst); |
| 1502 | LHSI->setOperand(1, ConstantExpr::get(ShiftOp, AndCST,ShAmt)); |
| 1503 | LHSI->setOperand(0, Shift->getOperand(0)); |
| 1504 | WorkList.push_back(Shift); // Shift is dead. |
| 1505 | AddUsesToWorkList(I); |
| 1506 | return &I; |
| 1507 | } |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 1508 | } |
| 1509 | } |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1510 | } |
| 1511 | break; |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1512 | case Instruction::Div: |
| 1513 | if (0 && isa<ConstantInt>(LHSI->getOperand(1))) { |
| 1514 | std::cerr << "COULD FOLD: " << *LHSI; |
| 1515 | std::cerr << "COULD FOLD: " << I << "\n"; |
| 1516 | } |
| 1517 | break; |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1518 | case Instruction::Select: |
Chris Lattner | 2dd0174 | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 1519 | // If either operand of the select is a constant, we can fold the |
| 1520 | // comparison into the select arms, which will cause one to be |
| 1521 | // constant folded and the select turned into a bitwise or. |
| 1522 | Value *Op1 = 0, *Op2 = 0; |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1523 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
Chris Lattner | 2dd0174 | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 1524 | // Fold the known value into the constant operand. |
| 1525 | Op1 = ConstantExpr::get(I.getOpcode(), C, CI); |
| 1526 | // Insert a new SetCC of the other select operand. |
| 1527 | Op2 = InsertNewInstBefore(new SetCondInst(I.getOpcode(), |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1528 | LHSI->getOperand(2), CI, |
Chris Lattner | 2dd0174 | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 1529 | I.getName()), I); |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1530 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
Chris Lattner | 2dd0174 | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 1531 | // Fold the known value into the constant operand. |
| 1532 | Op2 = ConstantExpr::get(I.getOpcode(), C, CI); |
| 1533 | // Insert a new SetCC of the other select operand. |
| 1534 | Op1 = InsertNewInstBefore(new SetCondInst(I.getOpcode(), |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1535 | LHSI->getOperand(1), CI, |
Chris Lattner | 2dd0174 | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 1536 | I.getName()), I); |
| 1537 | } |
| 1538 | |
| 1539 | if (Op1) |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1540 | return new SelectInst(LHSI->getOperand(0), Op1, Op2); |
| 1541 | break; |
Chris Lattner | 2dd0174 | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 1542 | } |
Chris Lattner | e1e10e1 | 2004-05-25 06:32:08 +0000 | [diff] [blame] | 1543 | |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 1544 | // Simplify seteq and setne instructions... |
| 1545 | if (I.getOpcode() == Instruction::SetEQ || |
| 1546 | I.getOpcode() == Instruction::SetNE) { |
| 1547 | bool isSetNE = I.getOpcode() == Instruction::SetNE; |
| 1548 | |
Chris Lattner | cfbce7c | 2003-07-23 17:26:36 +0000 | [diff] [blame] | 1549 | // 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] | 1550 | // operand is a constant, simplify a bit. |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 1551 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) { |
| 1552 | switch (BO->getOpcode()) { |
Chris Lattner | 23b47b6 | 2004-07-06 07:38:18 +0000 | [diff] [blame] | 1553 | case Instruction::Rem: |
| 1554 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. |
| 1555 | if (CI->isNullValue() && isa<ConstantSInt>(BO->getOperand(1)) && |
| 1556 | BO->hasOneUse() && |
| 1557 | cast<ConstantSInt>(BO->getOperand(1))->getValue() > 1) |
| 1558 | if (unsigned L2 = |
| 1559 | Log2(cast<ConstantSInt>(BO->getOperand(1))->getValue())) { |
| 1560 | const Type *UTy = BO->getType()->getUnsignedVersion(); |
| 1561 | Value *NewX = InsertNewInstBefore(new CastInst(BO->getOperand(0), |
| 1562 | UTy, "tmp"), I); |
| 1563 | Constant *RHSCst = ConstantUInt::get(UTy, 1ULL << L2); |
| 1564 | Value *NewRem =InsertNewInstBefore(BinaryOperator::createRem(NewX, |
| 1565 | RHSCst, BO->getName()), I); |
| 1566 | return BinaryOperator::create(I.getOpcode(), NewRem, |
| 1567 | Constant::getNullValue(UTy)); |
| 1568 | } |
| 1569 | break; |
| 1570 | |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 1571 | case Instruction::Add: |
Chris Lattner | 6e07936 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 1572 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. |
| 1573 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 1574 | return new SetCondInst(I.getOpcode(), BO->getOperand(0), |
| 1575 | ConstantExpr::getSub(CI, BOp1C)); |
| 1576 | } else if (CI->isNullValue()) { |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 1577 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 1578 | // efficiently invertible, or if the add has just this one use. |
| 1579 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
Chris Lattner | 6e07936 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 1580 | |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 1581 | if (Value *NegVal = dyn_castNegVal(BOp1)) |
| 1582 | return new SetCondInst(I.getOpcode(), BOp0, NegVal); |
| 1583 | else if (Value *NegVal = dyn_castNegVal(BOp0)) |
| 1584 | return new SetCondInst(I.getOpcode(), NegVal, BOp1); |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1585 | else if (BO->hasOneUse()) { |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 1586 | Instruction *Neg = BinaryOperator::createNeg(BOp1, BO->getName()); |
| 1587 | BO->setName(""); |
| 1588 | InsertNewInstBefore(Neg, I); |
| 1589 | return new SetCondInst(I.getOpcode(), BOp0, Neg); |
| 1590 | } |
| 1591 | } |
| 1592 | break; |
| 1593 | case Instruction::Xor: |
| 1594 | // For the xor case, we can xor two constants together, eliminating |
| 1595 | // the explicit xor. |
| 1596 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) |
| 1597 | return BinaryOperator::create(I.getOpcode(), BO->getOperand(0), |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1598 | ConstantExpr::getXor(CI, BOC)); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 1599 | |
| 1600 | // FALLTHROUGH |
| 1601 | case Instruction::Sub: |
| 1602 | // Replace (([sub|xor] A, B) != 0) with (A != B) |
| 1603 | if (CI->isNullValue()) |
| 1604 | return new SetCondInst(I.getOpcode(), BO->getOperand(0), |
| 1605 | BO->getOperand(1)); |
| 1606 | break; |
| 1607 | |
| 1608 | case Instruction::Or: |
| 1609 | // If bits are being or'd in that are not present in the constant we |
| 1610 | // are comparing against, then the comparison could never succeed! |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1611 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 1612 | Constant *NotCI = ConstantExpr::getNot(CI); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1613 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 1614 | return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE)); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1615 | } |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 1616 | break; |
| 1617 | |
| 1618 | case Instruction::And: |
| 1619 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 1620 | // If bits are being compared against that are and'd out, then the |
| 1621 | // comparison can never succeed! |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 1622 | if (!ConstantExpr::getAnd(CI, |
| 1623 | ConstantExpr::getNot(BOC))->isNullValue()) |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 1624 | return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE)); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 1625 | |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1626 | // If we have ((X & C) == C), turn it into ((X & C) != 0). |
Chris Lattner | ee59d4b | 2004-06-10 02:33:20 +0000 | [diff] [blame] | 1627 | if (CI == BOC && isOneBitSet(CI)) |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1628 | return new SetCondInst(isSetNE ? Instruction::SetEQ : |
| 1629 | Instruction::SetNE, Op0, |
| 1630 | Constant::getNullValue(CI->getType())); |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1631 | |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 1632 | // Replace (and X, (1 << size(X)-1) != 0) with x < 0, converting X |
| 1633 | // to be a signed value as appropriate. |
| 1634 | if (isSignBit(BOC)) { |
| 1635 | Value *X = BO->getOperand(0); |
| 1636 | // If 'X' is not signed, insert a cast now... |
| 1637 | if (!BOC->getType()->isSigned()) { |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 1638 | const Type *DestTy = BOC->getType()->getSignedVersion(); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 1639 | CastInst *NewCI = new CastInst(X,DestTy,X->getName()+".signed"); |
| 1640 | InsertNewInstBefore(NewCI, I); |
| 1641 | X = NewCI; |
| 1642 | } |
| 1643 | return new SetCondInst(isSetNE ? Instruction::SetLT : |
| 1644 | Instruction::SetGE, X, |
| 1645 | Constant::getNullValue(X->getType())); |
| 1646 | } |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 1647 | } |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 1648 | default: break; |
| 1649 | } |
| 1650 | } |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 1651 | } else { // Not a SetEQ/SetNE |
| 1652 | // If the LHS is a cast from an integral value of the same size, |
| 1653 | if (CastInst *Cast = dyn_cast<CastInst>(Op0)) { |
| 1654 | Value *CastOp = Cast->getOperand(0); |
| 1655 | const Type *SrcTy = CastOp->getType(); |
| 1656 | unsigned SrcTySize = SrcTy->getPrimitiveSize(); |
| 1657 | if (SrcTy != Cast->getType() && SrcTy->isInteger() && |
| 1658 | SrcTySize == Cast->getType()->getPrimitiveSize()) { |
| 1659 | assert((SrcTy->isSigned() ^ Cast->getType()->isSigned()) && |
| 1660 | "Source and destination signednesses should differ!"); |
| 1661 | if (Cast->getType()->isSigned()) { |
| 1662 | // If this is a signed comparison, check for comparisons in the |
| 1663 | // vicinity of zero. |
| 1664 | if (I.getOpcode() == Instruction::SetLT && CI->isNullValue()) |
| 1665 | // X < 0 => x > 127 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1666 | return BinaryOperator::createSetGT(CastOp, |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 1667 | ConstantUInt::get(SrcTy, (1ULL << (SrcTySize*8-1))-1)); |
| 1668 | else if (I.getOpcode() == Instruction::SetGT && |
| 1669 | cast<ConstantSInt>(CI)->getValue() == -1) |
| 1670 | // X > -1 => x < 128 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1671 | return BinaryOperator::createSetLT(CastOp, |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 1672 | ConstantUInt::get(SrcTy, 1ULL << (SrcTySize*8-1))); |
| 1673 | } else { |
| 1674 | ConstantUInt *CUI = cast<ConstantUInt>(CI); |
| 1675 | if (I.getOpcode() == Instruction::SetLT && |
| 1676 | CUI->getValue() == 1ULL << (SrcTySize*8-1)) |
| 1677 | // X < 128 => X > -1 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1678 | return BinaryOperator::createSetGT(CastOp, |
| 1679 | ConstantSInt::get(SrcTy, -1)); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 1680 | else if (I.getOpcode() == Instruction::SetGT && |
| 1681 | CUI->getValue() == (1ULL << (SrcTySize*8-1))-1) |
| 1682 | // X > 127 => X < 0 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1683 | return BinaryOperator::createSetLT(CastOp, |
| 1684 | Constant::getNullValue(SrcTy)); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 1685 | } |
| 1686 | } |
| 1687 | } |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 1688 | } |
Chris Lattner | 791ac1a | 2003-06-01 03:35:25 +0000 | [diff] [blame] | 1689 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1690 | // 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] | 1691 | if (CI->isMinValue()) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1692 | if (I.getOpcode() == Instruction::SetLT) // A < MIN -> FALSE |
| 1693 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1694 | if (I.getOpcode() == Instruction::SetGE) // A >= MIN -> TRUE |
| 1695 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 1696 | if (I.getOpcode() == Instruction::SetLE) // A <= MIN -> A == MIN |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1697 | return BinaryOperator::createSetEQ(Op0, Op1); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1698 | if (I.getOpcode() == Instruction::SetGT) // A > MIN -> A != MIN |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1699 | return BinaryOperator::createSetNE(Op0, Op1); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1700 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1701 | } else if (CI->isMaxValue()) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1702 | if (I.getOpcode() == Instruction::SetGT) // A > MAX -> FALSE |
| 1703 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1704 | if (I.getOpcode() == Instruction::SetLE) // A <= MAX -> TRUE |
| 1705 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 1706 | if (I.getOpcode() == Instruction::SetGE) // A >= MAX -> A == MAX |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1707 | return BinaryOperator::createSetEQ(Op0, Op1); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1708 | if (I.getOpcode() == Instruction::SetLT) // A < MAX -> A != MAX |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1709 | return BinaryOperator::createSetNE(Op0, Op1); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1710 | |
| 1711 | // Comparing against a value really close to min or max? |
| 1712 | } else if (isMinValuePlusOne(CI)) { |
| 1713 | if (I.getOpcode() == Instruction::SetLT) // A < MIN+1 -> A == MIN |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1714 | return BinaryOperator::createSetEQ(Op0, SubOne(CI)); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1715 | if (I.getOpcode() == Instruction::SetGE) // A >= MIN-1 -> A != MIN |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1716 | return BinaryOperator::createSetNE(Op0, SubOne(CI)); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1717 | |
| 1718 | } else if (isMaxValueMinusOne(CI)) { |
| 1719 | if (I.getOpcode() == Instruction::SetGT) // A > MAX-1 -> A == MAX |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1720 | return BinaryOperator::createSetEQ(Op0, AddOne(CI)); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1721 | if (I.getOpcode() == Instruction::SetLE) // A <= MAX-1 -> A != MAX |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1722 | return BinaryOperator::createSetNE(Op0, AddOne(CI)); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1723 | } |
Chris Lattner | 5961114 | 2004-02-23 05:47:48 +0000 | [diff] [blame] | 1724 | |
| 1725 | // If we still have a setle or setge instruction, turn it into the |
| 1726 | // appropriate setlt or setgt instruction. Since the border cases have |
| 1727 | // already been handled above, this requires little checking. |
| 1728 | // |
| 1729 | if (I.getOpcode() == Instruction::SetLE) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1730 | return BinaryOperator::createSetLT(Op0, AddOne(CI)); |
Chris Lattner | 5961114 | 2004-02-23 05:47:48 +0000 | [diff] [blame] | 1731 | if (I.getOpcode() == Instruction::SetGE) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1732 | return BinaryOperator::createSetGT(Op0, SubOne(CI)); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 1735 | // Test to see if the operands of the setcc are casted versions of other |
| 1736 | // 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] | 1737 | if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
| 1738 | Value *CastOp0 = CI->getOperand(0); |
| 1739 | if (CastOp0->getType()->isLosslesslyConvertibleTo(CI->getType()) && |
Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 1740 | (isa<Constant>(Op1) || isa<CastInst>(Op1)) && |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 1741 | (I.getOpcode() == Instruction::SetEQ || |
| 1742 | I.getOpcode() == Instruction::SetNE)) { |
| 1743 | // We keep moving the cast from the left operand over to the right |
| 1744 | // operand, where it can often be eliminated completely. |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 1745 | Op0 = CastOp0; |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 1746 | |
| 1747 | // If operand #1 is a cast instruction, see if we can eliminate it as |
| 1748 | // well. |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 1749 | if (CastInst *CI2 = dyn_cast<CastInst>(Op1)) |
| 1750 | if (CI2->getOperand(0)->getType()->isLosslesslyConvertibleTo( |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 1751 | Op0->getType())) |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 1752 | Op1 = CI2->getOperand(0); |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 1753 | |
| 1754 | // If Op1 is a constant, we can fold the cast into the constant. |
| 1755 | if (Op1->getType() != Op0->getType()) |
| 1756 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
| 1757 | Op1 = ConstantExpr::getCast(Op1C, Op0->getType()); |
| 1758 | } else { |
| 1759 | // Otherwise, cast the RHS right before the setcc |
| 1760 | Op1 = new CastInst(Op1, Op0->getType(), Op1->getName()); |
| 1761 | InsertNewInstBefore(cast<Instruction>(Op1), I); |
| 1762 | } |
| 1763 | return BinaryOperator::create(I.getOpcode(), Op0, Op1); |
| 1764 | } |
| 1765 | |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 1766 | // Handle the special case of: setcc (cast bool to X), <cst> |
| 1767 | // This comes up when you have code like |
| 1768 | // int X = A < B; |
| 1769 | // if (X) ... |
| 1770 | // For generality, we handle any zero-extension of any operand comparison |
| 1771 | // with a constant. |
| 1772 | if (ConstantInt *ConstantRHS = dyn_cast<ConstantInt>(Op1)) { |
| 1773 | const Type *SrcTy = CastOp0->getType(); |
| 1774 | const Type *DestTy = Op0->getType(); |
| 1775 | if (SrcTy->getPrimitiveSize() < DestTy->getPrimitiveSize() && |
| 1776 | (SrcTy->isUnsigned() || SrcTy == Type::BoolTy)) { |
| 1777 | // Ok, we have an expansion of operand 0 into a new type. Get the |
| 1778 | // constant value, masink off bits which are not set in the RHS. These |
| 1779 | // could be set if the destination value is signed. |
| 1780 | uint64_t ConstVal = ConstantRHS->getRawValue(); |
| 1781 | ConstVal &= (1ULL << DestTy->getPrimitiveSize()*8)-1; |
| 1782 | |
| 1783 | // If the constant we are comparing it with has high bits set, which |
| 1784 | // don't exist in the original value, the values could never be equal, |
| 1785 | // because the source would be zero extended. |
| 1786 | unsigned SrcBits = |
| 1787 | SrcTy == Type::BoolTy ? 1 : SrcTy->getPrimitiveSize()*8; |
Chris Lattner | 7c94d11 | 2003-11-05 17:31:36 +0000 | [diff] [blame] | 1788 | bool HasSignBit = ConstVal & (1ULL << (DestTy->getPrimitiveSize()*8-1)); |
| 1789 | if (ConstVal & ~((1ULL << SrcBits)-1)) { |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 1790 | switch (I.getOpcode()) { |
| 1791 | default: assert(0 && "Unknown comparison type!"); |
| 1792 | case Instruction::SetEQ: |
| 1793 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1794 | case Instruction::SetNE: |
| 1795 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 1796 | case Instruction::SetLT: |
| 1797 | case Instruction::SetLE: |
| 1798 | if (DestTy->isSigned() && HasSignBit) |
| 1799 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1800 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 1801 | case Instruction::SetGT: |
| 1802 | case Instruction::SetGE: |
| 1803 | if (DestTy->isSigned() && HasSignBit) |
| 1804 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 1805 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | // Otherwise, we can replace the setcc with a setcc of the smaller |
| 1810 | // operand value. |
| 1811 | Op1 = ConstantExpr::getCast(cast<Constant>(Op1), SrcTy); |
| 1812 | return BinaryOperator::create(I.getOpcode(), CastOp0, Op1); |
| 1813 | } |
| 1814 | } |
| 1815 | } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1816 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | |
| 1820 | |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 1821 | Instruction *InstCombiner::visitShiftInst(ShiftInst &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1822 | assert(I.getOperand(1)->getType() == Type::UByteTy); |
| 1823 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 1824 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1825 | |
| 1826 | // shl X, 0 == X and shr X, 0 == X |
| 1827 | // shl 0, X == 0 and shr 0, X == 0 |
| 1828 | if (Op1 == Constant::getNullValue(Type::UByteTy) || |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1829 | Op0 == Constant::getNullValue(Op0->getType())) |
| 1830 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1831 | |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 1832 | // shr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 1833 | if (!isLeftShift) |
| 1834 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0)) |
| 1835 | if (CSI->isAllOnesValue()) |
| 1836 | return ReplaceInstUsesWith(I, CSI); |
| 1837 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1838 | // Try to fold constant and into select arguments. |
| 1839 | if (isa<Constant>(Op0)) |
| 1840 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 1841 | if (Instruction *R = FoldBinOpIntoSelect(I, SI, this)) |
| 1842 | return R; |
| 1843 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1844 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) { |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 1845 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr |
| 1846 | // of a signed value. |
| 1847 | // |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 1848 | unsigned TypeBits = Op0->getType()->getPrimitiveSize()*8; |
Chris Lattner | f5ce254 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 1849 | if (CUI->getValue() >= TypeBits) { |
| 1850 | if (!Op0->getType()->isSigned() || isLeftShift) |
| 1851 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
| 1852 | else { |
| 1853 | I.setOperand(1, ConstantUInt::get(Type::UByteTy, TypeBits-1)); |
| 1854 | return &I; |
| 1855 | } |
| 1856 | } |
Chris Lattner | 55f3d94 | 2002-09-10 23:04:09 +0000 | [diff] [blame] | 1857 | |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 1858 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 1859 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 1860 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 1861 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1862 | return BinaryOperator::createMul(BO->getOperand(0), |
| 1863 | ConstantExpr::getShl(BOOp, CUI)); |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 1864 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1865 | // Try to fold constant and into select arguments. |
| 1866 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 1867 | if (Instruction *R = FoldBinOpIntoSelect(I, SI, this)) |
| 1868 | return R; |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 1869 | |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 1870 | // If the operand is an bitwise operator with a constant RHS, and the |
| 1871 | // 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] | 1872 | if (Op0->hasOneUse()) |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 1873 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) |
| 1874 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 1875 | bool isValid = true; // Valid only for And, Or, Xor |
| 1876 | bool highBitSet = false; // Transform if high bit of constant set? |
| 1877 | |
| 1878 | switch (Op0BO->getOpcode()) { |
| 1879 | default: isValid = false; break; // Do not perform transform! |
| 1880 | case Instruction::Or: |
| 1881 | case Instruction::Xor: |
| 1882 | highBitSet = false; |
| 1883 | break; |
| 1884 | case Instruction::And: |
| 1885 | highBitSet = true; |
| 1886 | break; |
| 1887 | } |
| 1888 | |
| 1889 | // If this is a signed shift right, and the high bit is modified |
| 1890 | // by the logical operation, do not perform the transformation. |
| 1891 | // The highBitSet boolean indicates the value of the high bit of |
| 1892 | // the constant which would cause it to be modified for this |
| 1893 | // operation. |
| 1894 | // |
| 1895 | if (isValid && !isLeftShift && !I.getType()->isUnsigned()) { |
| 1896 | uint64_t Val = Op0C->getRawValue(); |
| 1897 | isValid = ((Val & (1 << (TypeBits-1))) != 0) == highBitSet; |
| 1898 | } |
| 1899 | |
| 1900 | if (isValid) { |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1901 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, CUI); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 1902 | |
| 1903 | Instruction *NewShift = |
| 1904 | new ShiftInst(I.getOpcode(), Op0BO->getOperand(0), CUI, |
| 1905 | Op0BO->getName()); |
| 1906 | Op0BO->setName(""); |
| 1907 | InsertNewInstBefore(NewShift, I); |
| 1908 | |
| 1909 | return BinaryOperator::create(Op0BO->getOpcode(), NewShift, |
| 1910 | NewRHS); |
| 1911 | } |
| 1912 | } |
| 1913 | |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 1914 | // 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] | 1915 | if (ShiftInst *Op0SI = dyn_cast<ShiftInst>(Op0)) |
Chris Lattner | ab780df | 2003-07-24 18:38:56 +0000 | [diff] [blame] | 1916 | if (ConstantUInt *ShiftAmt1C = |
| 1917 | dyn_cast<ConstantUInt>(Op0SI->getOperand(1))) { |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 1918 | unsigned ShiftAmt1 = ShiftAmt1C->getValue(); |
| 1919 | unsigned ShiftAmt2 = CUI->getValue(); |
| 1920 | |
| 1921 | // Check for (A << c1) << c2 and (A >> c1) >> c2 |
| 1922 | if (I.getOpcode() == Op0SI->getOpcode()) { |
| 1923 | unsigned Amt = ShiftAmt1+ShiftAmt2; // Fold into one big shift... |
Chris Lattner | f5ce254 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 1924 | if (Op0->getType()->getPrimitiveSize()*8 < Amt) |
| 1925 | Amt = Op0->getType()->getPrimitiveSize()*8; |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 1926 | return new ShiftInst(I.getOpcode(), Op0SI->getOperand(0), |
| 1927 | ConstantUInt::get(Type::UByteTy, Amt)); |
| 1928 | } |
| 1929 | |
Chris Lattner | ab780df | 2003-07-24 18:38:56 +0000 | [diff] [blame] | 1930 | // Check for (A << c1) >> c2 or visaversa. If we are dealing with |
| 1931 | // signed types, we can only support the (A >> c1) << c2 configuration, |
| 1932 | // 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] | 1933 | if (I.getType()->isUnsigned() || isLeftShift) { |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 1934 | // Calculate bitmask for what gets shifted off the edge... |
| 1935 | Constant *C = ConstantIntegral::getAllOnesValue(I.getType()); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 1936 | if (isLeftShift) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1937 | C = ConstantExpr::getShl(C, ShiftAmt1C); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 1938 | else |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1939 | C = ConstantExpr::getShr(C, ShiftAmt1C); |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 1940 | |
| 1941 | Instruction *Mask = |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1942 | BinaryOperator::createAnd(Op0SI->getOperand(0), C, |
| 1943 | Op0SI->getOperand(0)->getName()+".mask"); |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 1944 | InsertNewInstBefore(Mask, I); |
| 1945 | |
| 1946 | // Figure out what flavor of shift we should use... |
| 1947 | if (ShiftAmt1 == ShiftAmt2) |
| 1948 | return ReplaceInstUsesWith(I, Mask); // (A << c) >> c === A & c2 |
| 1949 | else if (ShiftAmt1 < ShiftAmt2) { |
| 1950 | return new ShiftInst(I.getOpcode(), Mask, |
| 1951 | ConstantUInt::get(Type::UByteTy, ShiftAmt2-ShiftAmt1)); |
| 1952 | } else { |
| 1953 | return new ShiftInst(Op0SI->getOpcode(), Mask, |
| 1954 | ConstantUInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2)); |
| 1955 | } |
| 1956 | } |
| 1957 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1958 | } |
Chris Lattner | 2e0fb39 | 2002-10-08 16:16:40 +0000 | [diff] [blame] | 1959 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1960 | return 0; |
| 1961 | } |
| 1962 | |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 1963 | enum CastType { |
| 1964 | Noop = 0, |
| 1965 | Truncate = 1, |
| 1966 | Signext = 2, |
| 1967 | Zeroext = 3 |
| 1968 | }; |
| 1969 | |
| 1970 | /// getCastType - In the future, we will split the cast instruction into these |
| 1971 | /// various types. Until then, we have to do the analysis here. |
| 1972 | static CastType getCastType(const Type *Src, const Type *Dest) { |
| 1973 | assert(Src->isIntegral() && Dest->isIntegral() && |
| 1974 | "Only works on integral types!"); |
| 1975 | unsigned SrcSize = Src->getPrimitiveSize()*8; |
| 1976 | if (Src == Type::BoolTy) SrcSize = 1; |
| 1977 | unsigned DestSize = Dest->getPrimitiveSize()*8; |
| 1978 | if (Dest == Type::BoolTy) DestSize = 1; |
| 1979 | |
| 1980 | if (SrcSize == DestSize) return Noop; |
| 1981 | if (SrcSize > DestSize) return Truncate; |
| 1982 | if (Src->isSigned()) return Signext; |
| 1983 | return Zeroext; |
| 1984 | } |
| 1985 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1986 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 1987 | // isEliminableCastOfCast - Return true if it is valid to eliminate the CI |
| 1988 | // instruction. |
| 1989 | // |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 1990 | static inline bool isEliminableCastOfCast(const Type *SrcTy, const Type *MidTy, |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 1991 | const Type *DstTy, TargetData *TD) { |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 1992 | |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 1993 | // It is legal to eliminate the instruction if casting A->B->A if the sizes |
| 1994 | // are identical and the bits don't get reinterpreted (for example |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 1995 | // int->float->int would not be allowed). |
Misha Brukman | e5838c4 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 1996 | if (SrcTy == DstTy && SrcTy->isLosslesslyConvertibleTo(MidTy)) |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 1997 | return true; |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 1998 | |
Chris Lattner | 4fbad96 | 2004-07-21 04:27:24 +0000 | [diff] [blame] | 1999 | // If we are casting between pointer and integer types, treat pointers as |
| 2000 | // integers of the appropriate size for the code below. |
| 2001 | if (isa<PointerType>(SrcTy)) SrcTy = TD->getIntPtrType(); |
| 2002 | if (isa<PointerType>(MidTy)) MidTy = TD->getIntPtrType(); |
| 2003 | if (isa<PointerType>(DstTy)) DstTy = TD->getIntPtrType(); |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 2004 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2005 | // Allow free casting and conversion of sizes as long as the sign doesn't |
| 2006 | // change... |
Chris Lattner | b0b412e | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 2007 | if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral()) { |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 2008 | CastType FirstCast = getCastType(SrcTy, MidTy); |
| 2009 | CastType SecondCast = getCastType(MidTy, DstTy); |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 2010 | |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 2011 | // Capture the effect of these two casts. If the result is a legal cast, |
| 2012 | // the CastType is stored here, otherwise a special code is used. |
| 2013 | static const unsigned CastResult[] = { |
| 2014 | // First cast is noop |
| 2015 | 0, 1, 2, 3, |
| 2016 | // First cast is a truncate |
| 2017 | 1, 1, 4, 4, // trunc->extend is not safe to eliminate |
| 2018 | // First cast is a sign ext |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 2019 | 2, 5, 2, 4, // signext->zeroext never ok |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 2020 | // First cast is a zero ext |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 2021 | 3, 5, 3, 3, |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 2022 | }; |
| 2023 | |
| 2024 | unsigned Result = CastResult[FirstCast*4+SecondCast]; |
| 2025 | switch (Result) { |
| 2026 | default: assert(0 && "Illegal table value!"); |
| 2027 | case 0: |
| 2028 | case 1: |
| 2029 | case 2: |
| 2030 | case 3: |
| 2031 | // FIXME: in the future, when LLVM has explicit sign/zeroextends and |
| 2032 | // truncates, we could eliminate more casts. |
| 2033 | return (unsigned)getCastType(SrcTy, DstTy) == Result; |
| 2034 | case 4: |
| 2035 | return false; // Not possible to eliminate this here. |
| 2036 | case 5: |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 2037 | // Sign or zero extend followed by truncate is always ok if the result |
| 2038 | // is a truncate or noop. |
| 2039 | CastType ResultCast = getCastType(SrcTy, DstTy); |
| 2040 | if (ResultCast == Noop || ResultCast == Truncate) |
| 2041 | return true; |
| 2042 | // Otherwise we are still growing the value, we are only safe if the |
| 2043 | // result will match the sign/zeroextendness of the result. |
| 2044 | return ResultCast == FirstCast; |
Chris Lattner | 3732aca | 2002-08-15 16:15:25 +0000 | [diff] [blame] | 2045 | } |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 2046 | } |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2047 | return false; |
| 2048 | } |
| 2049 | |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 2050 | static bool ValueRequiresCast(const Value *V, const Type *Ty, TargetData *TD) { |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 2051 | if (V->getType() == Ty || isa<Constant>(V)) return false; |
| 2052 | if (const CastInst *CI = dyn_cast<CastInst>(V)) |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 2053 | if (isEliminableCastOfCast(CI->getOperand(0)->getType(), CI->getType(), Ty, |
| 2054 | TD)) |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 2055 | return false; |
| 2056 | return true; |
| 2057 | } |
| 2058 | |
| 2059 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 2060 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 2061 | /// casts that are known to not do anything... |
| 2062 | /// |
| 2063 | Value *InstCombiner::InsertOperandCastBefore(Value *V, const Type *DestTy, |
| 2064 | Instruction *InsertBefore) { |
| 2065 | if (V->getType() == DestTy) return V; |
| 2066 | if (Constant *C = dyn_cast<Constant>(V)) |
| 2067 | return ConstantExpr::getCast(C, DestTy); |
| 2068 | |
| 2069 | CastInst *CI = new CastInst(V, DestTy, V->getName()); |
| 2070 | InsertNewInstBefore(CI, *InsertBefore); |
| 2071 | return CI; |
| 2072 | } |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2073 | |
| 2074 | // CastInst simplification |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2075 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2076 | Instruction *InstCombiner::visitCastInst(CastInst &CI) { |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 2077 | Value *Src = CI.getOperand(0); |
| 2078 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2079 | // If the user is casting a value to the same type, eliminate this cast |
| 2080 | // instruction... |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 2081 | if (CI.getType() == Src->getType()) |
| 2082 | return ReplaceInstUsesWith(CI, Src); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2083 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2084 | // If casting the result of another cast instruction, try to eliminate this |
| 2085 | // one! |
| 2086 | // |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 2087 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 2088 | if (isEliminableCastOfCast(CSrc->getOperand(0)->getType(), |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 2089 | CSrc->getType(), CI.getType(), TD)) { |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2090 | // This instruction now refers directly to the cast's src operand. This |
| 2091 | // has a good chance of making CSrc dead. |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2092 | CI.setOperand(0, CSrc->getOperand(0)); |
| 2093 | return &CI; |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 2096 | // If this is an A->B->A cast, and we are dealing with integral types, try |
| 2097 | // to convert this into a logical 'and' instruction. |
| 2098 | // |
| 2099 | if (CSrc->getOperand(0)->getType() == CI.getType() && |
Chris Lattner | b0b412e | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 2100 | CI.getType()->isInteger() && CSrc->getType()->isInteger() && |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 2101 | CI.getType()->isUnsigned() && CSrc->getType()->isUnsigned() && |
| 2102 | CSrc->getType()->getPrimitiveSize() < CI.getType()->getPrimitiveSize()){ |
| 2103 | assert(CSrc->getType() != Type::ULongTy && |
| 2104 | "Cannot have type bigger than ulong!"); |
Chris Lattner | 196897c | 2003-05-26 23:41:32 +0000 | [diff] [blame] | 2105 | uint64_t AndValue = (1ULL << CSrc->getType()->getPrimitiveSize()*8)-1; |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 2106 | Constant *AndOp = ConstantUInt::get(CI.getType(), AndValue); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2107 | return BinaryOperator::createAnd(CSrc->getOperand(0), AndOp); |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 2108 | } |
| 2109 | } |
| 2110 | |
Chris Lattner | 0384165 | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 2111 | // If this is a cast to bool, turn it into the appropriate setne instruction. |
| 2112 | if (CI.getType() == Type::BoolTy) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2113 | return BinaryOperator::createSetNE(CI.getOperand(0), |
Chris Lattner | 0384165 | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 2114 | Constant::getNullValue(CI.getOperand(0)->getType())); |
| 2115 | |
Chris Lattner | d0d5160 | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 2116 | // If casting the result of a getelementptr instruction with no offset, turn |
| 2117 | // this into a cast of the original pointer! |
| 2118 | // |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 2119 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
Chris Lattner | d0d5160 | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 2120 | bool AllZeroOperands = true; |
| 2121 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) |
| 2122 | if (!isa<Constant>(GEP->getOperand(i)) || |
| 2123 | !cast<Constant>(GEP->getOperand(i))->isNullValue()) { |
| 2124 | AllZeroOperands = false; |
| 2125 | break; |
| 2126 | } |
| 2127 | if (AllZeroOperands) { |
| 2128 | CI.setOperand(0, GEP->getOperand(0)); |
| 2129 | return &CI; |
| 2130 | } |
| 2131 | } |
| 2132 | |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 2133 | // If we are casting a malloc or alloca to a pointer to a type of the same |
| 2134 | // size, rewrite the allocation instruction to allocate the "right" type. |
| 2135 | // |
| 2136 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) |
Chris Lattner | d4d987d | 2003-11-02 06:54:48 +0000 | [diff] [blame] | 2137 | if (AI->hasOneUse() && !AI->isArrayAllocation()) |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 2138 | if (const PointerType *PTy = dyn_cast<PointerType>(CI.getType())) { |
| 2139 | // Get the type really allocated and the type casted to... |
| 2140 | const Type *AllocElTy = AI->getAllocatedType(); |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 2141 | const Type *CastElTy = PTy->getElementType(); |
Chris Lattner | 9eb9ccd | 2004-07-06 19:28:42 +0000 | [diff] [blame] | 2142 | if (AllocElTy->isSized() && CastElTy->isSized()) { |
| 2143 | unsigned AllocElTySize = TD->getTypeSize(AllocElTy); |
| 2144 | unsigned CastElTySize = TD->getTypeSize(CastElTy); |
Chris Lattner | 7c94d11 | 2003-11-05 17:31:36 +0000 | [diff] [blame] | 2145 | |
Chris Lattner | 9eb9ccd | 2004-07-06 19:28:42 +0000 | [diff] [blame] | 2146 | // If the allocation is for an even multiple of the cast type size |
| 2147 | if (CastElTySize && (AllocElTySize % CastElTySize == 0)) { |
| 2148 | Value *Amt = ConstantUInt::get(Type::UIntTy, |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 2149 | AllocElTySize/CastElTySize); |
Chris Lattner | 9eb9ccd | 2004-07-06 19:28:42 +0000 | [diff] [blame] | 2150 | std::string Name = AI->getName(); AI->setName(""); |
| 2151 | AllocationInst *New; |
| 2152 | if (isa<MallocInst>(AI)) |
| 2153 | New = new MallocInst(CastElTy, Amt, Name); |
| 2154 | else |
| 2155 | New = new AllocaInst(CastElTy, Amt, Name); |
| 2156 | InsertNewInstBefore(New, *AI); |
| 2157 | return ReplaceInstUsesWith(CI, New); |
| 2158 | } |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 2159 | } |
| 2160 | } |
| 2161 | |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 2162 | // If the source value is an instruction with only this use, we can attempt to |
| 2163 | // propagate the cast into the instruction. Also, only handle integral types |
| 2164 | // for now. |
| 2165 | if (Instruction *SrcI = dyn_cast<Instruction>(Src)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2166 | if (SrcI->hasOneUse() && Src->getType()->isIntegral() && |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 2167 | CI.getType()->isInteger()) { // Don't mess with casts to bool here |
| 2168 | const Type *DestTy = CI.getType(); |
| 2169 | unsigned SrcBitSize = getTypeSizeInBits(Src->getType()); |
| 2170 | unsigned DestBitSize = getTypeSizeInBits(DestTy); |
| 2171 | |
| 2172 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; |
| 2173 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; |
| 2174 | |
| 2175 | switch (SrcI->getOpcode()) { |
| 2176 | case Instruction::Add: |
| 2177 | case Instruction::Mul: |
| 2178 | case Instruction::And: |
| 2179 | case Instruction::Or: |
| 2180 | case Instruction::Xor: |
| 2181 | // If we are discarding information, or just changing the sign, rewrite. |
| 2182 | if (DestBitSize <= SrcBitSize && DestBitSize != 1) { |
| 2183 | // Don't insert two casts if they cannot be eliminated. We allow two |
| 2184 | // casts to be inserted if the sizes are the same. This could only be |
| 2185 | // converting signedness, which is a noop. |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 2186 | if (DestBitSize == SrcBitSize || !ValueRequiresCast(Op1, DestTy,TD) || |
| 2187 | !ValueRequiresCast(Op0, DestTy, TD)) { |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 2188 | Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI); |
| 2189 | Value *Op1c = InsertOperandCastBefore(Op1, DestTy, SrcI); |
| 2190 | return BinaryOperator::create(cast<BinaryOperator>(SrcI) |
| 2191 | ->getOpcode(), Op0c, Op1c); |
| 2192 | } |
| 2193 | } |
| 2194 | break; |
| 2195 | case Instruction::Shl: |
| 2196 | // Allow changing the sign of the source operand. Do not allow changing |
| 2197 | // the size of the shift, UNLESS the shift amount is a constant. We |
| 2198 | // mush not change variable sized shifts to a smaller size, because it |
| 2199 | // is undefined to shift more bits out than exist in the value. |
| 2200 | if (DestBitSize == SrcBitSize || |
| 2201 | (DestBitSize < SrcBitSize && isa<Constant>(Op1))) { |
| 2202 | Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI); |
| 2203 | return new ShiftInst(Instruction::Shl, Op0c, Op1); |
| 2204 | } |
| 2205 | break; |
| 2206 | } |
| 2207 | } |
| 2208 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2209 | return 0; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 2212 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
| 2213 | /// %C = or %A, %B |
| 2214 | /// %D = select %cond, %C, %A |
| 2215 | /// into: |
| 2216 | /// %C = select %cond, %B, 0 |
| 2217 | /// %D = or %A, %C |
| 2218 | /// |
| 2219 | /// Assuming that the specified instruction is an operand to the select, return |
| 2220 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 2221 | /// equal the other incoming value of the select. |
| 2222 | /// |
| 2223 | static unsigned GetSelectFoldableOperands(Instruction *I) { |
| 2224 | switch (I->getOpcode()) { |
| 2225 | case Instruction::Add: |
| 2226 | case Instruction::Mul: |
| 2227 | case Instruction::And: |
| 2228 | case Instruction::Or: |
| 2229 | case Instruction::Xor: |
| 2230 | return 3; // Can fold through either operand. |
| 2231 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 2232 | case Instruction::Shl: // Can only fold on the shift amount. |
| 2233 | case Instruction::Shr: |
| 2234 | return 1; |
| 2235 | default: |
| 2236 | return 0; // Cannot fold |
| 2237 | } |
| 2238 | } |
| 2239 | |
| 2240 | /// GetSelectFoldableConstant - For the same transformation as the previous |
| 2241 | /// function, return the identity constant that goes into the select. |
| 2242 | static Constant *GetSelectFoldableConstant(Instruction *I) { |
| 2243 | switch (I->getOpcode()) { |
| 2244 | default: assert(0 && "This cannot happen!"); abort(); |
| 2245 | case Instruction::Add: |
| 2246 | case Instruction::Sub: |
| 2247 | case Instruction::Or: |
| 2248 | case Instruction::Xor: |
| 2249 | return Constant::getNullValue(I->getType()); |
| 2250 | case Instruction::Shl: |
| 2251 | case Instruction::Shr: |
| 2252 | return Constant::getNullValue(Type::UByteTy); |
| 2253 | case Instruction::And: |
| 2254 | return ConstantInt::getAllOnesValue(I->getType()); |
| 2255 | case Instruction::Mul: |
| 2256 | return ConstantInt::get(I->getType(), 1); |
| 2257 | } |
| 2258 | } |
| 2259 | |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 2260 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 2261 | Value *CondVal = SI.getCondition(); |
| 2262 | Value *TrueVal = SI.getTrueValue(); |
| 2263 | Value *FalseVal = SI.getFalseValue(); |
| 2264 | |
| 2265 | // select true, X, Y -> X |
| 2266 | // select false, X, Y -> Y |
| 2267 | if (ConstantBool *C = dyn_cast<ConstantBool>(CondVal)) |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 2268 | if (C == ConstantBool::True) |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 2269 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 2270 | else { |
| 2271 | assert(C == ConstantBool::False); |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 2272 | return ReplaceInstUsesWith(SI, FalseVal); |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 2273 | } |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 2274 | |
| 2275 | // select C, X, X -> X |
| 2276 | if (TrueVal == FalseVal) |
| 2277 | return ReplaceInstUsesWith(SI, TrueVal); |
| 2278 | |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 2279 | if (SI.getType() == Type::BoolTy) |
| 2280 | if (ConstantBool *C = dyn_cast<ConstantBool>(TrueVal)) { |
| 2281 | if (C == ConstantBool::True) { |
| 2282 | // Change: A = select B, true, C --> A = or B, C |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2283 | return BinaryOperator::createOr(CondVal, FalseVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 2284 | } else { |
| 2285 | // Change: A = select B, false, C --> A = and !B, C |
| 2286 | Value *NotCond = |
| 2287 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 2288 | "not."+CondVal->getName()), SI); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2289 | return BinaryOperator::createAnd(NotCond, FalseVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 2290 | } |
| 2291 | } else if (ConstantBool *C = dyn_cast<ConstantBool>(FalseVal)) { |
| 2292 | if (C == ConstantBool::False) { |
| 2293 | // Change: A = select B, C, false --> A = and B, C |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2294 | return BinaryOperator::createAnd(CondVal, TrueVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 2295 | } else { |
| 2296 | // Change: A = select B, C, true --> A = or !B, C |
| 2297 | Value *NotCond = |
| 2298 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 2299 | "not."+CondVal->getName()), SI); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2300 | return BinaryOperator::createOr(NotCond, TrueVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 2301 | } |
| 2302 | } |
| 2303 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2304 | // Selecting between two integer constants? |
| 2305 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) |
| 2306 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { |
| 2307 | // select C, 1, 0 -> cast C to int |
| 2308 | if (FalseValC->isNullValue() && TrueValC->getRawValue() == 1) { |
| 2309 | return new CastInst(CondVal, SI.getType()); |
| 2310 | } else if (TrueValC->isNullValue() && FalseValC->getRawValue() == 1) { |
| 2311 | // select C, 0, 1 -> cast !C to int |
| 2312 | Value *NotCond = |
| 2313 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
Chris Lattner | cf7baf3 | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 2314 | "not."+CondVal->getName()), SI); |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2315 | return new CastInst(NotCond, SI.getType()); |
Chris Lattner | cf7baf3 | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 2316 | } |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2317 | |
| 2318 | // If one of the constants is zero (we know they can't both be) and we |
| 2319 | // have a setcc instruction with zero, and we have an 'and' with the |
| 2320 | // non-constant value, eliminate this whole mess. This corresponds to |
| 2321 | // cases like this: ((X & 27) ? 27 : 0) |
| 2322 | if (TrueValC->isNullValue() || FalseValC->isNullValue()) |
| 2323 | if (Instruction *IC = dyn_cast<Instruction>(SI.getCondition())) |
| 2324 | if ((IC->getOpcode() == Instruction::SetEQ || |
| 2325 | IC->getOpcode() == Instruction::SetNE) && |
| 2326 | isa<ConstantInt>(IC->getOperand(1)) && |
| 2327 | cast<Constant>(IC->getOperand(1))->isNullValue()) |
| 2328 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) |
| 2329 | if (ICA->getOpcode() == Instruction::And && |
| 2330 | isa<ConstantInt>(ICA->getOperand(1)) && |
| 2331 | (ICA->getOperand(1) == TrueValC || |
| 2332 | ICA->getOperand(1) == FalseValC) && |
| 2333 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { |
| 2334 | // Okay, now we know that everything is set up, we just don't |
| 2335 | // know whether we have a setne or seteq and whether the true or |
| 2336 | // false val is the zero. |
| 2337 | bool ShouldNotVal = !TrueValC->isNullValue(); |
| 2338 | ShouldNotVal ^= IC->getOpcode() == Instruction::SetNE; |
| 2339 | Value *V = ICA; |
| 2340 | if (ShouldNotVal) |
| 2341 | V = InsertNewInstBefore(BinaryOperator::create( |
| 2342 | Instruction::Xor, V, ICA->getOperand(1)), SI); |
| 2343 | return ReplaceInstUsesWith(SI, V); |
| 2344 | } |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 2345 | } |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 2346 | |
| 2347 | // See if we are selecting two values based on a comparison of the two values. |
| 2348 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(CondVal)) { |
| 2349 | if (SCI->getOperand(0) == TrueVal && SCI->getOperand(1) == FalseVal) { |
| 2350 | // Transform (X == Y) ? X : Y -> Y |
| 2351 | if (SCI->getOpcode() == Instruction::SetEQ) |
| 2352 | return ReplaceInstUsesWith(SI, FalseVal); |
| 2353 | // Transform (X != Y) ? X : Y -> X |
| 2354 | if (SCI->getOpcode() == Instruction::SetNE) |
| 2355 | return ReplaceInstUsesWith(SI, TrueVal); |
| 2356 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 2357 | |
| 2358 | } else if (SCI->getOperand(0) == FalseVal && SCI->getOperand(1) == TrueVal){ |
| 2359 | // Transform (X == Y) ? Y : X -> X |
| 2360 | if (SCI->getOpcode() == Instruction::SetEQ) |
Chris Lattner | 24cf020 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 2361 | return ReplaceInstUsesWith(SI, FalseVal); |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 2362 | // Transform (X != Y) ? Y : X -> Y |
| 2363 | if (SCI->getOpcode() == Instruction::SetNE) |
Chris Lattner | 24cf020 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 2364 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 2365 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 2366 | } |
| 2367 | } |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 2368 | |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 2369 | // See if we can fold the select into one of our operands. |
| 2370 | if (SI.getType()->isInteger()) { |
| 2371 | // See the comment above GetSelectFoldableOperands for a description of the |
| 2372 | // transformation we are doing here. |
| 2373 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) |
| 2374 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && |
| 2375 | !isa<Constant>(FalseVal)) |
| 2376 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { |
| 2377 | unsigned OpToFold = 0; |
| 2378 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 2379 | OpToFold = 1; |
| 2380 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
| 2381 | OpToFold = 2; |
| 2382 | } |
| 2383 | |
| 2384 | if (OpToFold) { |
| 2385 | Constant *C = GetSelectFoldableConstant(TVI); |
| 2386 | std::string Name = TVI->getName(); TVI->setName(""); |
| 2387 | Instruction *NewSel = |
| 2388 | new SelectInst(SI.getCondition(), TVI->getOperand(2-OpToFold), C, |
| 2389 | Name); |
| 2390 | InsertNewInstBefore(NewSel, SI); |
| 2391 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) |
| 2392 | return BinaryOperator::create(BO->getOpcode(), FalseVal, NewSel); |
| 2393 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(TVI)) |
| 2394 | return new ShiftInst(SI->getOpcode(), FalseVal, NewSel); |
| 2395 | else { |
| 2396 | assert(0 && "Unknown instruction!!"); |
| 2397 | } |
| 2398 | } |
| 2399 | } |
| 2400 | |
| 2401 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) |
| 2402 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && |
| 2403 | !isa<Constant>(TrueVal)) |
| 2404 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { |
| 2405 | unsigned OpToFold = 0; |
| 2406 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 2407 | OpToFold = 1; |
| 2408 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
| 2409 | OpToFold = 2; |
| 2410 | } |
| 2411 | |
| 2412 | if (OpToFold) { |
| 2413 | Constant *C = GetSelectFoldableConstant(FVI); |
| 2414 | std::string Name = FVI->getName(); FVI->setName(""); |
| 2415 | Instruction *NewSel = |
| 2416 | new SelectInst(SI.getCondition(), C, FVI->getOperand(2-OpToFold), |
| 2417 | Name); |
| 2418 | InsertNewInstBefore(NewSel, SI); |
| 2419 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) |
| 2420 | return BinaryOperator::create(BO->getOpcode(), TrueVal, NewSel); |
| 2421 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(FVI)) |
| 2422 | return new ShiftInst(SI->getOpcode(), TrueVal, NewSel); |
| 2423 | else { |
| 2424 | assert(0 && "Unknown instruction!!"); |
| 2425 | } |
| 2426 | } |
| 2427 | } |
| 2428 | } |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 2429 | return 0; |
| 2430 | } |
| 2431 | |
| 2432 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 2433 | // CallInst simplification |
| 2434 | // |
| 2435 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 2436 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 2437 | // visitCallSite. |
| 2438 | if (Function *F = CI.getCalledFunction()) |
| 2439 | switch (F->getIntrinsicID()) { |
| 2440 | case Intrinsic::memmove: |
| 2441 | case Intrinsic::memcpy: |
| 2442 | case Intrinsic::memset: |
| 2443 | // memmove/cpy/set of zero bytes is a noop. |
| 2444 | if (Constant *NumBytes = dyn_cast<Constant>(CI.getOperand(3))) { |
| 2445 | if (NumBytes->isNullValue()) |
| 2446 | return EraseInstFromFunction(CI); |
| 2447 | } |
| 2448 | break; |
| 2449 | default: |
| 2450 | break; |
| 2451 | } |
| 2452 | |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 2453 | return visitCallSite(&CI); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
| 2456 | // InvokeInst simplification |
| 2457 | // |
| 2458 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 2459 | return visitCallSite(&II); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 2462 | // visitCallSite - Improvements for call and invoke instructions. |
| 2463 | // |
| 2464 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 2465 | bool Changed = false; |
| 2466 | |
| 2467 | // If the callee is a constexpr cast of a function, attempt to move the cast |
| 2468 | // to the arguments of the call/invoke. |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 2469 | if (transformConstExprCastCall(CS)) return 0; |
| 2470 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 2471 | Value *Callee = CS.getCalledValue(); |
| 2472 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 2473 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 2474 | if (FTy->isVarArg()) { |
| 2475 | // See if we can optimize any arguments passed through the varargs area of |
| 2476 | // the call. |
| 2477 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
| 2478 | E = CS.arg_end(); I != E; ++I) |
| 2479 | if (CastInst *CI = dyn_cast<CastInst>(*I)) { |
| 2480 | // If this cast does not effect the value passed through the varargs |
| 2481 | // area, we can eliminate the use of the cast. |
| 2482 | Value *Op = CI->getOperand(0); |
| 2483 | if (CI->getType()->isLosslesslyConvertibleTo(Op->getType())) { |
| 2484 | *I = Op; |
| 2485 | Changed = true; |
| 2486 | } |
| 2487 | } |
| 2488 | } |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 2489 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 2490 | return Changed ? CS.getInstruction() : 0; |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 2491 | } |
| 2492 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 2493 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 2494 | // attempt to move the cast to the arguments of the call/invoke. |
| 2495 | // |
| 2496 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 2497 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 2498 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
Chris Lattner | f3edc49 | 2004-07-18 18:59:44 +0000 | [diff] [blame] | 2499 | if (CE->getOpcode() != Instruction::Cast || !isa<Function>(CE->getOperand(0))) |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 2500 | return false; |
Reid Spencer | 8743687 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 2501 | Function *Callee = cast<Function>(CE->getOperand(0)); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 2502 | Instruction *Caller = CS.getInstruction(); |
| 2503 | |
| 2504 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 2505 | // would cause a type conversion of one of our arguments, change this call to |
| 2506 | // be a direct call with arguments casted to the appropriate types. |
| 2507 | // |
| 2508 | const FunctionType *FT = Callee->getFunctionType(); |
| 2509 | const Type *OldRetTy = Caller->getType(); |
| 2510 | |
Chris Lattner | 1f7942f | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 2511 | // Check to see if we are changing the return type... |
| 2512 | if (OldRetTy != FT->getReturnType()) { |
| 2513 | if (Callee->isExternal() && |
| 2514 | !OldRetTy->isLosslesslyConvertibleTo(FT->getReturnType()) && |
| 2515 | !Caller->use_empty()) |
| 2516 | return false; // Cannot transform this return value... |
| 2517 | |
| 2518 | // If the callsite is an invoke instruction, and the return value is used by |
| 2519 | // a PHI node in a successor, we cannot change the return type of the call |
| 2520 | // because there is no place to put the cast instruction (without breaking |
| 2521 | // the critical edge). Bail out in this case. |
| 2522 | if (!Caller->use_empty()) |
| 2523 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 2524 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 2525 | UI != E; ++UI) |
| 2526 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 2527 | if (PN->getParent() == II->getNormalDest() || |
Chris Lattner | fae8ab3 | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 2528 | PN->getParent() == II->getUnwindDest()) |
Chris Lattner | 1f7942f | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 2529 | return false; |
| 2530 | } |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 2531 | |
| 2532 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 2533 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
| 2534 | |
| 2535 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 2536 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 2537 | const Type *ParamTy = FT->getParamType(i); |
| 2538 | bool isConvertible = (*AI)->getType()->isLosslesslyConvertibleTo(ParamTy); |
| 2539 | if (Callee->isExternal() && !isConvertible) return false; |
| 2540 | } |
| 2541 | |
| 2542 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
| 2543 | Callee->isExternal()) |
| 2544 | return false; // Do not delete arguments unless we have a function body... |
| 2545 | |
| 2546 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 2547 | // inserting cast instructions as necessary... |
| 2548 | std::vector<Value*> Args; |
| 2549 | Args.reserve(NumActualArgs); |
| 2550 | |
| 2551 | AI = CS.arg_begin(); |
| 2552 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 2553 | const Type *ParamTy = FT->getParamType(i); |
| 2554 | if ((*AI)->getType() == ParamTy) { |
| 2555 | Args.push_back(*AI); |
| 2556 | } else { |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 2557 | Args.push_back(InsertNewInstBefore(new CastInst(*AI, ParamTy, "tmp"), |
| 2558 | *Caller)); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 2559 | } |
| 2560 | } |
| 2561 | |
| 2562 | // If the function takes more arguments than the call was taking, add them |
| 2563 | // now... |
| 2564 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 2565 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 2566 | |
| 2567 | // If we are removing arguments to the function, emit an obnoxious warning... |
| 2568 | if (FT->getNumParams() < NumActualArgs) |
| 2569 | if (!FT->isVarArg()) { |
| 2570 | std::cerr << "WARNING: While resolving call to function '" |
| 2571 | << Callee->getName() << "' arguments were dropped!\n"; |
| 2572 | } else { |
| 2573 | // Add all of the arguments in their promoted form to the arg list... |
| 2574 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 2575 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 2576 | if (PTy != (*AI)->getType()) { |
| 2577 | // Must promote to pass through va_arg area! |
| 2578 | Instruction *Cast = new CastInst(*AI, PTy, "tmp"); |
| 2579 | InsertNewInstBefore(Cast, *Caller); |
| 2580 | Args.push_back(Cast); |
| 2581 | } else { |
| 2582 | Args.push_back(*AI); |
| 2583 | } |
| 2584 | } |
| 2585 | } |
| 2586 | |
| 2587 | if (FT->getReturnType() == Type::VoidTy) |
| 2588 | Caller->setName(""); // Void type should not have a name... |
| 2589 | |
| 2590 | Instruction *NC; |
| 2591 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Chris Lattner | fae8ab3 | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 2592 | NC = new InvokeInst(Callee, II->getNormalDest(), II->getUnwindDest(), |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 2593 | Args, Caller->getName(), Caller); |
| 2594 | } else { |
| 2595 | NC = new CallInst(Callee, Args, Caller->getName(), Caller); |
| 2596 | } |
| 2597 | |
| 2598 | // Insert a cast of the return type as necessary... |
| 2599 | Value *NV = NC; |
| 2600 | if (Caller->getType() != NV->getType() && !Caller->use_empty()) { |
| 2601 | if (NV->getType() != Type::VoidTy) { |
| 2602 | NV = NC = new CastInst(NC, Caller->getType(), "tmp"); |
Chris Lattner | 686767f | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 2603 | |
| 2604 | // If this is an invoke instruction, we should insert it after the first |
| 2605 | // non-phi, instruction in the normal successor block. |
| 2606 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 2607 | BasicBlock::iterator I = II->getNormalDest()->begin(); |
| 2608 | while (isa<PHINode>(I)) ++I; |
| 2609 | InsertNewInstBefore(NC, *I); |
| 2610 | } else { |
| 2611 | // Otherwise, it's a call, just insert cast right after the call instr |
| 2612 | InsertNewInstBefore(NC, *Caller); |
| 2613 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 2614 | AddUsersToWorkList(*Caller); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 2615 | } else { |
| 2616 | NV = Constant::getNullValue(Caller->getType()); |
| 2617 | } |
| 2618 | } |
| 2619 | |
| 2620 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 2621 | Caller->replaceAllUsesWith(NV); |
| 2622 | Caller->getParent()->getInstList().erase(Caller); |
| 2623 | removeFromWorkList(Caller); |
| 2624 | return true; |
| 2625 | } |
| 2626 | |
| 2627 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2628 | |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 2629 | // PHINode simplification |
| 2630 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2631 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Chris Lattner | 91daeb5 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 2632 | if (Value *V = hasConstantValue(&PN)) |
| 2633 | return ReplaceInstUsesWith(PN, V); |
Chris Lattner | 4db2d22 | 2004-02-16 05:07:08 +0000 | [diff] [blame] | 2634 | |
| 2635 | // If the only user of this instruction is a cast instruction, and all of the |
| 2636 | // incoming values are constants, change this PHI to merge together the casted |
| 2637 | // constants. |
| 2638 | if (PN.hasOneUse()) |
| 2639 | if (CastInst *CI = dyn_cast<CastInst>(PN.use_back())) |
| 2640 | if (CI->getType() != PN.getType()) { // noop casts will be folded |
| 2641 | bool AllConstant = true; |
| 2642 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 2643 | if (!isa<Constant>(PN.getIncomingValue(i))) { |
| 2644 | AllConstant = false; |
| 2645 | break; |
| 2646 | } |
| 2647 | if (AllConstant) { |
| 2648 | // Make a new PHI with all casted values. |
| 2649 | PHINode *New = new PHINode(CI->getType(), PN.getName(), &PN); |
| 2650 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 2651 | Constant *OldArg = cast<Constant>(PN.getIncomingValue(i)); |
| 2652 | New->addIncoming(ConstantExpr::getCast(OldArg, New->getType()), |
| 2653 | PN.getIncomingBlock(i)); |
| 2654 | } |
| 2655 | |
| 2656 | // Update the cast instruction. |
| 2657 | CI->setOperand(0, New); |
| 2658 | WorkList.push_back(CI); // revisit the cast instruction to fold. |
| 2659 | WorkList.push_back(New); // Make sure to revisit the new Phi |
| 2660 | return &PN; // PN is now dead! |
| 2661 | } |
| 2662 | } |
Chris Lattner | 91daeb5 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 2663 | return 0; |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 2664 | } |
| 2665 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 2666 | static Value *InsertSignExtendToPtrTy(Value *V, const Type *DTy, |
| 2667 | Instruction *InsertPoint, |
| 2668 | InstCombiner *IC) { |
| 2669 | unsigned PS = IC->getTargetData().getPointerSize(); |
| 2670 | const Type *VTy = V->getType(); |
| 2671 | Instruction *Cast; |
| 2672 | if (!VTy->isSigned() && VTy->getPrimitiveSize() < PS) |
| 2673 | // We must insert a cast to ensure we sign-extend. |
| 2674 | V = IC->InsertNewInstBefore(new CastInst(V, VTy->getSignedVersion(), |
| 2675 | V->getName()), *InsertPoint); |
| 2676 | return IC->InsertNewInstBefore(new CastInst(V, DTy, V->getName()), |
| 2677 | *InsertPoint); |
| 2678 | } |
| 2679 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2680 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2681 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 2682 | Value *PtrOp = GEP.getOperand(0); |
Chris Lattner | 471bd76 | 2003-05-22 19:07:21 +0000 | [diff] [blame] | 2683 | // Is it 'getelementptr %P, long 0' or 'getelementptr %P' |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2684 | // If so, eliminate the noop. |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 2685 | if (GEP.getNumOperands() == 1) |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 2686 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 2687 | |
| 2688 | bool HasZeroPointerIndex = false; |
| 2689 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) |
| 2690 | HasZeroPointerIndex = C->isNullValue(); |
| 2691 | |
| 2692 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 2693 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2694 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 2695 | // Eliminate unneeded casts for indices. |
| 2696 | bool MadeChange = false; |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 2697 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 2698 | for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI) |
| 2699 | if (isa<SequentialType>(*GTI)) { |
| 2700 | if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) { |
| 2701 | Value *Src = CI->getOperand(0); |
| 2702 | const Type *SrcTy = Src->getType(); |
| 2703 | const Type *DestTy = CI->getType(); |
| 2704 | if (Src->getType()->isInteger()) { |
| 2705 | if (SrcTy->getPrimitiveSize() == DestTy->getPrimitiveSize()) { |
| 2706 | // We can always eliminate a cast from ulong or long to the other. |
| 2707 | // We can always eliminate a cast from uint to int or the other on |
| 2708 | // 32-bit pointer platforms. |
| 2709 | if (DestTy->getPrimitiveSize() >= TD->getPointerSize()) { |
| 2710 | MadeChange = true; |
| 2711 | GEP.setOperand(i, Src); |
| 2712 | } |
| 2713 | } else if (SrcTy->getPrimitiveSize() < DestTy->getPrimitiveSize() && |
| 2714 | SrcTy->getPrimitiveSize() == 4) { |
| 2715 | // We can always eliminate a cast from int to [u]long. We can |
| 2716 | // eliminate a cast from uint to [u]long iff the target is a 32-bit |
| 2717 | // pointer target. |
| 2718 | if (SrcTy->isSigned() || |
| 2719 | SrcTy->getPrimitiveSize() >= TD->getPointerSize()) { |
| 2720 | MadeChange = true; |
| 2721 | GEP.setOperand(i, Src); |
| 2722 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 2723 | } |
| 2724 | } |
| 2725 | } |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 2726 | // If we are using a wider index than needed for this platform, shrink it |
| 2727 | // to what we need. If the incoming value needs a cast instruction, |
| 2728 | // insert it. This explicit cast can make subsequent optimizations more |
| 2729 | // obvious. |
| 2730 | Value *Op = GEP.getOperand(i); |
| 2731 | if (Op->getType()->getPrimitiveSize() > TD->getPointerSize()) |
Chris Lattner | 1e9ac1a | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 2732 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | 44d0b95 | 2004-07-20 01:48:15 +0000 | [diff] [blame] | 2733 | GEP.setOperand(i, ConstantExpr::getCast(C, |
| 2734 | TD->getIntPtrType()->getSignedVersion())); |
Chris Lattner | 1e9ac1a | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 2735 | MadeChange = true; |
| 2736 | } else { |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 2737 | Op = InsertNewInstBefore(new CastInst(Op, TD->getIntPtrType(), |
| 2738 | Op->getName()), GEP); |
| 2739 | GEP.setOperand(i, Op); |
| 2740 | MadeChange = true; |
| 2741 | } |
Chris Lattner | 44d0b95 | 2004-07-20 01:48:15 +0000 | [diff] [blame] | 2742 | |
| 2743 | // If this is a constant idx, make sure to canonicalize it to be a signed |
| 2744 | // operand, otherwise CSE and other optimizations are pessimized. |
| 2745 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op)) { |
| 2746 | GEP.setOperand(i, ConstantExpr::getCast(CUI, |
| 2747 | CUI->getType()->getSignedVersion())); |
| 2748 | MadeChange = true; |
| 2749 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 2750 | } |
| 2751 | if (MadeChange) return &GEP; |
| 2752 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 2753 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 2754 | // is a getelementptr instruction, combine the indices of the two |
| 2755 | // getelementptr instructions into a single instruction. |
| 2756 | // |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 2757 | std::vector<Value*> SrcGEPOperands; |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 2758 | if (GetElementPtrInst *Src = dyn_cast<GetElementPtrInst>(PtrOp)) { |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 2759 | SrcGEPOperands.assign(Src->op_begin(), Src->op_end()); |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 2760 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(PtrOp)) { |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 2761 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 2762 | SrcGEPOperands.assign(CE->op_begin(), CE->op_end()); |
| 2763 | } |
| 2764 | |
| 2765 | if (!SrcGEPOperands.empty()) { |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 2766 | // Note that if our source is a gep chain itself that we wait for that |
| 2767 | // chain to be resolved before we perform this transformation. This |
| 2768 | // avoids us creating a TON of code in some cases. |
| 2769 | // |
| 2770 | if (isa<GetElementPtrInst>(SrcGEPOperands[0]) && |
| 2771 | cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2) |
| 2772 | return 0; // Wait until our source is folded to completion. |
| 2773 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 2774 | std::vector<Value *> Indices; |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 2775 | |
| 2776 | // Find out whether the last index in the source GEP is a sequential idx. |
| 2777 | bool EndsWithSequential = false; |
| 2778 | for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)), |
| 2779 | E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I) |
Chris Lattner | 8ec5f88 | 2004-05-08 22:41:42 +0000 | [diff] [blame] | 2780 | EndsWithSequential = !isa<StructType>(*I); |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 2781 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 2782 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 2783 | if (EndsWithSequential) { |
Chris Lattner | 235af56 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 2784 | // Replace: gep (gep %P, long B), long A, ... |
| 2785 | // With: T = long A+B; gep %P, T, ... |
| 2786 | // |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 2787 | Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 2788 | if (SO1 == Constant::getNullValue(SO1->getType())) { |
| 2789 | Sum = GO1; |
| 2790 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { |
| 2791 | Sum = SO1; |
| 2792 | } else { |
| 2793 | // If they aren't the same type, convert both to an integer of the |
| 2794 | // target's pointer size. |
| 2795 | if (SO1->getType() != GO1->getType()) { |
| 2796 | if (Constant *SO1C = dyn_cast<Constant>(SO1)) { |
| 2797 | SO1 = ConstantExpr::getCast(SO1C, GO1->getType()); |
| 2798 | } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) { |
| 2799 | GO1 = ConstantExpr::getCast(GO1C, SO1->getType()); |
| 2800 | } else { |
| 2801 | unsigned PS = TD->getPointerSize(); |
| 2802 | Instruction *Cast; |
| 2803 | if (SO1->getType()->getPrimitiveSize() == PS) { |
| 2804 | // Convert GO1 to SO1's type. |
| 2805 | GO1 = InsertSignExtendToPtrTy(GO1, SO1->getType(), &GEP, this); |
| 2806 | |
| 2807 | } else if (GO1->getType()->getPrimitiveSize() == PS) { |
| 2808 | // Convert SO1 to GO1's type. |
| 2809 | SO1 = InsertSignExtendToPtrTy(SO1, GO1->getType(), &GEP, this); |
| 2810 | } else { |
| 2811 | const Type *PT = TD->getIntPtrType(); |
| 2812 | SO1 = InsertSignExtendToPtrTy(SO1, PT, &GEP, this); |
| 2813 | GO1 = InsertSignExtendToPtrTy(GO1, PT, &GEP, this); |
| 2814 | } |
| 2815 | } |
| 2816 | } |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 2817 | if (isa<Constant>(SO1) && isa<Constant>(GO1)) |
| 2818 | Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1)); |
| 2819 | else { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2820 | Sum = BinaryOperator::createAdd(SO1, GO1, PtrOp->getName()+".sum"); |
| 2821 | InsertNewInstBefore(cast<Instruction>(Sum), GEP); |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 2822 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 2823 | } |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 2824 | |
| 2825 | // Recycle the GEP we already have if possible. |
| 2826 | if (SrcGEPOperands.size() == 2) { |
| 2827 | GEP.setOperand(0, SrcGEPOperands[0]); |
| 2828 | GEP.setOperand(1, Sum); |
| 2829 | return &GEP; |
| 2830 | } else { |
| 2831 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 2832 | SrcGEPOperands.end()-1); |
| 2833 | Indices.push_back(Sum); |
| 2834 | Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end()); |
| 2835 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 2836 | } else if (isa<Constant>(*GEP.idx_begin()) && |
| 2837 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 2838 | SrcGEPOperands.size() != 1) { |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 2839 | // Otherwise we can do the fold if the first index of the GEP is a zero |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 2840 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 2841 | SrcGEPOperands.end()); |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 2842 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
| 2843 | } |
| 2844 | |
| 2845 | if (!Indices.empty()) |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 2846 | return new GetElementPtrInst(SrcGEPOperands[0], Indices, GEP.getName()); |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 2847 | |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 2848 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) { |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 2849 | // GEP of global variable. If all of the indices for this GEP are |
| 2850 | // constants, we can promote this to a constexpr instead of an instruction. |
| 2851 | |
| 2852 | // Scan for nonconstants... |
| 2853 | std::vector<Constant*> Indices; |
| 2854 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
| 2855 | for (; I != E && isa<Constant>(*I); ++I) |
| 2856 | Indices.push_back(cast<Constant>(*I)); |
| 2857 | |
| 2858 | if (I == E) { // If they are all constants... |
Chris Lattner | f3edc49 | 2004-07-18 18:59:44 +0000 | [diff] [blame] | 2859 | Constant *CE = ConstantExpr::getGetElementPtr(GV, Indices); |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 2860 | |
| 2861 | // Replace all uses of the GEP with the new constexpr... |
| 2862 | return ReplaceInstUsesWith(GEP, CE); |
| 2863 | } |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 2864 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(PtrOp)) { |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 2865 | if (CE->getOpcode() == Instruction::Cast) { |
| 2866 | if (HasZeroPointerIndex) { |
| 2867 | // transform: GEP (cast [10 x ubyte]* X to [0 x ubyte]*), long 0, ... |
| 2868 | // into : GEP [10 x ubyte]* X, long 0, ... |
| 2869 | // |
| 2870 | // This occurs when the program declares an array extern like "int X[];" |
| 2871 | // |
| 2872 | Constant *X = CE->getOperand(0); |
| 2873 | const PointerType *CPTy = cast<PointerType>(CE->getType()); |
| 2874 | if (const PointerType *XTy = dyn_cast<PointerType>(X->getType())) |
| 2875 | if (const ArrayType *XATy = |
| 2876 | dyn_cast<ArrayType>(XTy->getElementType())) |
| 2877 | if (const ArrayType *CATy = |
| 2878 | dyn_cast<ArrayType>(CPTy->getElementType())) |
| 2879 | if (CATy->getElementType() == XATy->getElementType()) { |
| 2880 | // At this point, we know that the cast source type is a pointer |
| 2881 | // to an array of the same type as the destination pointer |
| 2882 | // array. Because the array type is never stepped over (there |
| 2883 | // is a leading zero) we can fold the cast into this GEP. |
| 2884 | GEP.setOperand(0, X); |
| 2885 | return &GEP; |
| 2886 | } |
| 2887 | } |
| 2888 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 2889 | } |
| 2890 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 2891 | return 0; |
| 2892 | } |
| 2893 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 2894 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
| 2895 | // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1 |
| 2896 | if (AI.isArrayAllocation()) // Check C != 1 |
| 2897 | if (const ConstantUInt *C = dyn_cast<ConstantUInt>(AI.getArraySize())) { |
| 2898 | const Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getValue()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 2899 | AllocationInst *New = 0; |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 2900 | |
| 2901 | // Create and insert the replacement instruction... |
| 2902 | if (isa<MallocInst>(AI)) |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 2903 | New = new MallocInst(NewTy, 0, AI.getName()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 2904 | else { |
| 2905 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 2906 | New = new AllocaInst(NewTy, 0, AI.getName()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 2907 | } |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 2908 | |
| 2909 | InsertNewInstBefore(New, AI); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 2910 | |
| 2911 | // Scan to the end of the allocation instructions, to skip over a block of |
| 2912 | // allocas if possible... |
| 2913 | // |
| 2914 | BasicBlock::iterator It = New; |
| 2915 | while (isa<AllocationInst>(*It)) ++It; |
| 2916 | |
| 2917 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 2918 | // insert our getelementptr instruction... |
| 2919 | // |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 2920 | std::vector<Value*> Idx(2, Constant::getNullValue(Type::IntTy)); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 2921 | Value *V = new GetElementPtrInst(New, Idx, New->getName()+".sub", It); |
| 2922 | |
| 2923 | // Now make everything use the getelementptr instead of the original |
| 2924 | // allocation. |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 2925 | return ReplaceInstUsesWith(AI, V); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 2926 | } |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 2927 | |
| 2928 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. |
| 2929 | // Note that we only do this for alloca's, because malloc should allocate and |
| 2930 | // return a unique pointer, even for a zero byte allocation. |
Chris Lattner | 49df6ce | 2004-07-02 22:55:47 +0000 | [diff] [blame] | 2931 | if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() && |
| 2932 | TD->getTypeSize(AI.getAllocatedType()) == 0) |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 2933 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
| 2934 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 2935 | return 0; |
| 2936 | } |
| 2937 | |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 2938 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { |
| 2939 | Value *Op = FI.getOperand(0); |
| 2940 | |
| 2941 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X |
| 2942 | if (CastInst *CI = dyn_cast<CastInst>(Op)) |
| 2943 | if (isa<PointerType>(CI->getOperand(0)->getType())) { |
| 2944 | FI.setOperand(0, CI->getOperand(0)); |
| 2945 | return &FI; |
| 2946 | } |
| 2947 | |
Chris Lattner | f3a3660 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 2948 | // If we have 'free null' delete the instruction. This can happen in stl code |
| 2949 | // when lots of inlining happens. |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 2950 | if (isa<ConstantPointerNull>(Op)) |
| 2951 | return EraseInstFromFunction(FI); |
Chris Lattner | f3a3660 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 2952 | |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 2953 | return 0; |
| 2954 | } |
| 2955 | |
| 2956 | |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 2957 | /// GetGEPGlobalInitializer - Given a constant, and a getelementptr |
| 2958 | /// constantexpr, return the constant value being addressed by the constant |
| 2959 | /// expression, or null if something is funny. |
| 2960 | /// |
| 2961 | static Constant *GetGEPGlobalInitializer(Constant *C, ConstantExpr *CE) { |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 2962 | if (CE->getOperand(1) != Constant::getNullValue(CE->getOperand(1)->getType())) |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 2963 | return 0; // Do not allow stepping over the value! |
| 2964 | |
| 2965 | // Loop over all of the operands, tracking down which value we are |
| 2966 | // addressing... |
Chris Lattner | ed79d8a | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 2967 | gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE); |
| 2968 | for (++I; I != E; ++I) |
| 2969 | if (const StructType *STy = dyn_cast<StructType>(*I)) { |
| 2970 | ConstantUInt *CU = cast<ConstantUInt>(I.getOperand()); |
| 2971 | assert(CU->getValue() < STy->getNumElements() && |
| 2972 | "Struct index out of range!"); |
| 2973 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { |
| 2974 | C = cast<Constant>(CS->getValues()[CU->getValue()]); |
| 2975 | } else if (isa<ConstantAggregateZero>(C)) { |
| 2976 | C = Constant::getNullValue(STy->getElementType(CU->getValue())); |
| 2977 | } else { |
| 2978 | return 0; |
| 2979 | } |
| 2980 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand())) { |
| 2981 | const ArrayType *ATy = cast<ArrayType>(*I); |
| 2982 | if ((uint64_t)CI->getRawValue() >= ATy->getNumElements()) return 0; |
| 2983 | if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) |
| 2984 | C = cast<Constant>(CA->getValues()[CI->getRawValue()]); |
| 2985 | else if (isa<ConstantAggregateZero>(C)) |
| 2986 | C = Constant::getNullValue(ATy->getElementType()); |
| 2987 | else |
| 2988 | return 0; |
| 2989 | } else { |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 2990 | return 0; |
Chris Lattner | ed79d8a | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 2991 | } |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 2992 | return C; |
| 2993 | } |
| 2994 | |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 2995 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI) { |
| 2996 | User *CI = cast<User>(LI.getOperand(0)); |
| 2997 | |
| 2998 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
| 2999 | if (const PointerType *SrcTy = |
| 3000 | dyn_cast<PointerType>(CI->getOperand(0)->getType())) { |
| 3001 | const Type *SrcPTy = SrcTy->getElementType(); |
| 3002 | if (SrcPTy->isSized() && DestPTy->isSized() && |
| 3003 | IC.getTargetData().getTypeSize(SrcPTy) == |
| 3004 | IC.getTargetData().getTypeSize(DestPTy) && |
| 3005 | (SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
| 3006 | (DestPTy->isInteger() || isa<PointerType>(DestPTy))) { |
| 3007 | // Okay, we are casting from one integer or pointer type to another of |
| 3008 | // the same size. Instead of casting the pointer before the load, cast |
| 3009 | // the result of the loaded value. |
| 3010 | Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CI->getOperand(0), |
| 3011 | CI->getName()), LI); |
| 3012 | // Now cast the result of the load. |
| 3013 | return new CastInst(NewLoad, LI.getType()); |
| 3014 | } |
| 3015 | } |
| 3016 | return 0; |
| 3017 | } |
| 3018 | |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 3019 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 3020 | Value *Op = LI.getOperand(0); |
Chris Lattner | 7e8af38 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 3021 | if (LI.isVolatile()) return 0; |
| 3022 | |
Chris Lattner | 6679e46 | 2004-04-14 03:28:36 +0000 | [diff] [blame] | 3023 | if (Constant *C = dyn_cast<Constant>(Op)) |
| 3024 | if (C->isNullValue()) // load null -> 0 |
| 3025 | return ReplaceInstUsesWith(LI, Constant::getNullValue(LI.getType())); |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 3026 | |
| 3027 | // Instcombine load (constant global) into the value loaded... |
| 3028 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) |
Chris Lattner | bdb0ce0 | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 3029 | if (GV->isConstant() && !GV->isExternal()) |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 3030 | return ReplaceInstUsesWith(LI, GV->getInitializer()); |
| 3031 | |
| 3032 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded... |
| 3033 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 3034 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
Reid Spencer | 8743687 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 3035 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
| 3036 | if (GV->isConstant() && !GV->isExternal()) |
| 3037 | if (Constant *V = GetGEPGlobalInitializer(GV->getInitializer(), CE)) |
| 3038 | return ReplaceInstUsesWith(LI, V); |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 3039 | } else if (CE->getOpcode() == Instruction::Cast) { |
| 3040 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 3041 | return Res; |
| 3042 | } |
Chris Lattner | e228ee5 | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 3043 | |
| 3044 | // load (cast X) --> cast (load X) iff safe |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 3045 | if (CastInst *CI = dyn_cast<CastInst>(Op)) |
| 3046 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 3047 | return Res; |
Chris Lattner | e228ee5 | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 3048 | |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 3049 | return 0; |
| 3050 | } |
| 3051 | |
| 3052 | |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 3053 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 3054 | // 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] | 3055 | if (BI.isConditional() && !isa<Constant>(BI.getCondition())) { |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 3056 | if (Value *V = dyn_castNotVal(BI.getCondition())) { |
| 3057 | BasicBlock *TrueDest = BI.getSuccessor(0); |
| 3058 | BasicBlock *FalseDest = BI.getSuccessor(1); |
| 3059 | // Swap Destinations and condition... |
| 3060 | BI.setCondition(V); |
| 3061 | BI.setSuccessor(0, FalseDest); |
| 3062 | BI.setSuccessor(1, TrueDest); |
| 3063 | return &BI; |
Chris Lattner | 4f7acca | 2004-02-27 06:27:46 +0000 | [diff] [blame] | 3064 | } else if (SetCondInst *I = dyn_cast<SetCondInst>(BI.getCondition())) { |
| 3065 | // Cannonicalize setne -> seteq |
| 3066 | if ((I->getOpcode() == Instruction::SetNE || |
| 3067 | I->getOpcode() == Instruction::SetLE || |
| 3068 | I->getOpcode() == Instruction::SetGE) && I->hasOneUse()) { |
| 3069 | std::string Name = I->getName(); I->setName(""); |
| 3070 | Instruction::BinaryOps NewOpcode = |
| 3071 | SetCondInst::getInverseCondition(I->getOpcode()); |
| 3072 | Value *NewSCC = BinaryOperator::create(NewOpcode, I->getOperand(0), |
| 3073 | I->getOperand(1), Name, I); |
| 3074 | BasicBlock *TrueDest = BI.getSuccessor(0); |
| 3075 | BasicBlock *FalseDest = BI.getSuccessor(1); |
| 3076 | // Swap Destinations and condition... |
| 3077 | BI.setCondition(NewSCC); |
| 3078 | BI.setSuccessor(0, FalseDest); |
| 3079 | BI.setSuccessor(1, TrueDest); |
| 3080 | removeFromWorkList(I); |
| 3081 | I->getParent()->getInstList().erase(I); |
| 3082 | WorkList.push_back(cast<Instruction>(NewSCC)); |
| 3083 | return &BI; |
| 3084 | } |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 3085 | } |
Chris Lattner | 4f7acca | 2004-02-27 06:27:46 +0000 | [diff] [blame] | 3086 | } |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 3087 | return 0; |
| 3088 | } |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 3089 | |
Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 3090 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
| 3091 | Value *Cond = SI.getCondition(); |
| 3092 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { |
| 3093 | if (I->getOpcode() == Instruction::Add) |
| 3094 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 3095 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' |
| 3096 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) |
| 3097 | SI.setOperand(i, ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), |
| 3098 | AddRHS)); |
| 3099 | SI.setOperand(0, I->getOperand(0)); |
| 3100 | WorkList.push_back(I); |
| 3101 | return &SI; |
| 3102 | } |
| 3103 | } |
| 3104 | return 0; |
| 3105 | } |
| 3106 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 3107 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 3108 | void InstCombiner::removeFromWorkList(Instruction *I) { |
| 3109 | WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I), |
| 3110 | WorkList.end()); |
| 3111 | } |
| 3112 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3113 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 3114 | bool Changed = false; |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 3115 | TD = &getAnalysis<TargetData>(); |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 3116 | |
Chris Lattner | b643a9e | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 3117 | for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) |
| 3118 | WorkList.push_back(&*i); |
Chris Lattner | 2d3a7a6 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 3119 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 3120 | |
| 3121 | while (!WorkList.empty()) { |
| 3122 | Instruction *I = WorkList.back(); // Get an instruction from the worklist |
| 3123 | WorkList.pop_back(); |
| 3124 | |
Misha Brukman | 632df28 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 3125 | // Check to see if we can DCE or ConstantPropagate the instruction... |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 3126 | // Check to see if we can DIE the instruction... |
| 3127 | if (isInstructionTriviallyDead(I)) { |
| 3128 | // Add operands to the worklist... |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 3129 | if (I->getNumOperands() < 4) |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 3130 | AddUsesToWorkList(*I); |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 3131 | ++NumDeadInst; |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 3132 | |
| 3133 | I->getParent()->getInstList().erase(I); |
| 3134 | removeFromWorkList(I); |
| 3135 | continue; |
| 3136 | } |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 3137 | |
Misha Brukman | 632df28 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 3138 | // Instruction isn't dead, see if we can constant propagate it... |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 3139 | if (Constant *C = ConstantFoldInstruction(I)) { |
| 3140 | // Add operands to the worklist... |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 3141 | AddUsesToWorkList(*I); |
Chris Lattner | c6509f4 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 3142 | ReplaceInstUsesWith(*I, C); |
| 3143 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 3144 | ++NumConstProp; |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 3145 | I->getParent()->getInstList().erase(I); |
Chris Lattner | 800aaaf | 2003-10-07 15:17:02 +0000 | [diff] [blame] | 3146 | removeFromWorkList(I); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 3147 | continue; |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 3148 | } |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 3149 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 3150 | // Now that we have an instruction, try combining it to simplify it... |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 3151 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 3152 | ++NumCombined; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 3153 | // Should we replace the old instruction with a new one? |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 3154 | if (Result != I) { |
Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 3155 | DEBUG(std::cerr << "IC: Old = " << *I |
| 3156 | << " New = " << *Result); |
| 3157 | |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 3158 | // Everything uses the new instruction now. |
| 3159 | I->replaceAllUsesWith(Result); |
| 3160 | |
| 3161 | // Push the new instruction and any users onto the worklist. |
| 3162 | WorkList.push_back(Result); |
| 3163 | AddUsersToWorkList(*Result); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 3164 | |
| 3165 | // Move the name to the new instruction first... |
| 3166 | std::string OldName = I->getName(); I->setName(""); |
Chris Lattner | 950fc78 | 2003-10-07 22:58:41 +0000 | [diff] [blame] | 3167 | Result->setName(OldName); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 3168 | |
| 3169 | // Insert the new instruction into the basic block... |
| 3170 | BasicBlock *InstParent = I->getParent(); |
| 3171 | InstParent->getInstList().insert(I, Result); |
| 3172 | |
Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 3173 | // Make sure that we reprocess all operands now that we reduced their |
| 3174 | // use counts. |
Chris Lattner | b643a9e | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 3175 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 3176 | if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i))) |
| 3177 | WorkList.push_back(OpI); |
| 3178 | |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 3179 | // Instructions can end up on the worklist more than once. Make sure |
| 3180 | // we do not process an instruction that has been deleted. |
| 3181 | removeFromWorkList(I); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 3182 | |
| 3183 | // Erase the old instruction. |
| 3184 | InstParent->getInstList().erase(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3185 | } else { |
Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 3186 | DEBUG(std::cerr << "IC: MOD = " << *I); |
| 3187 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 3188 | // If the instruction was modified, it's possible that it is now dead. |
| 3189 | // if so, remove it. |
Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 3190 | if (isInstructionTriviallyDead(I)) { |
| 3191 | // Make sure we process all operands now that we are reducing their |
| 3192 | // use counts. |
| 3193 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 3194 | if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i))) |
| 3195 | WorkList.push_back(OpI); |
| 3196 | |
| 3197 | // Instructions may end up in the worklist more than once. Erase all |
| 3198 | // occurrances of this instruction. |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 3199 | removeFromWorkList(I); |
Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 3200 | I->getParent()->getInstList().erase(I); |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 3201 | } else { |
| 3202 | WorkList.push_back(Result); |
| 3203 | AddUsersToWorkList(*Result); |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 3204 | } |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 3205 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 3206 | Changed = true; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 3207 | } |
| 3208 | } |
| 3209 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 3210 | return Changed; |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 3211 | } |
| 3212 | |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 3213 | Pass *llvm::createInstructionCombiningPass() { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 3214 | return new InstCombiner(); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 3215 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 3216 | |