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