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 | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 32 | // ... etc. |
Chris Lattner | bfb1d03 | 2003-07-23 21:41:57 +0000 | [diff] [blame] | 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 | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 38 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 39 | #include "llvm/Pass.h" |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 40 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 41 | #include "llvm/GlobalVariable.h" |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 42 | #include "llvm/Target/TargetData.h" |
| 43 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 44 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 45 | #include "llvm/Support/CallSite.h" |
| 46 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 60a6591 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 47 | #include "llvm/Support/InstIterator.h" |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 48 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 49 | #include "llvm/Support/PatternMatch.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 50 | #include "llvm/Support/Debug.h" |
| 51 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 52 | #include <algorithm> |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 53 | using namespace llvm; |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 54 | using namespace llvm::PatternMatch; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 56 | namespace { |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 57 | Statistic<> NumCombined ("instcombine", "Number of insts combined"); |
| 58 | Statistic<> NumConstProp("instcombine", "Number of constant folds"); |
| 59 | Statistic<> NumDeadInst ("instcombine", "Number of dead inst eliminated"); |
| 60 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 61 | class InstCombiner : public FunctionPass, |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 62 | public InstVisitor<InstCombiner, Instruction*> { |
| 63 | // Worklist of all of the instructions that need to be simplified. |
| 64 | std::vector<Instruction*> WorkList; |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 65 | TargetData *TD; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 67 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
| 68 | /// the instruction to the work lists because they might get more simplified |
| 69 | /// now. |
| 70 | /// |
| 71 | void AddUsersToWorkList(Instruction &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 72 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 73 | UI != UE; ++UI) |
| 74 | WorkList.push_back(cast<Instruction>(*UI)); |
| 75 | } |
| 76 | |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 77 | /// AddUsesToWorkList - When an instruction is simplified, add operands to |
| 78 | /// the work lists because they might get more simplified now. |
| 79 | /// |
| 80 | void AddUsesToWorkList(Instruction &I) { |
| 81 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 82 | if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) |
| 83 | WorkList.push_back(Op); |
| 84 | } |
| 85 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 86 | // removeFromWorkList - remove all instances of I from the worklist. |
| 87 | void removeFromWorkList(Instruction *I); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 88 | public: |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 89 | virtual bool runOnFunction(Function &F); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 90 | |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 91 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 92 | AU.addRequired<TargetData>(); |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 93 | AU.setPreservesCFG(); |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 96 | TargetData &getTargetData() const { return *TD; } |
| 97 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 98 | // Visitation implementation - Implement instruction combining for different |
| 99 | // instruction types. The semantics are as follows: |
| 100 | // Return Value: |
| 101 | // null - No change was made |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 102 | // I - Change was made, I is still valid, I may be dead though |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 103 | // otherwise - Change was made, replace I with returned instruction |
| 104 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 105 | Instruction *visitAdd(BinaryOperator &I); |
| 106 | Instruction *visitSub(BinaryOperator &I); |
| 107 | Instruction *visitMul(BinaryOperator &I); |
| 108 | Instruction *visitDiv(BinaryOperator &I); |
| 109 | Instruction *visitRem(BinaryOperator &I); |
| 110 | Instruction *visitAnd(BinaryOperator &I); |
| 111 | Instruction *visitOr (BinaryOperator &I); |
| 112 | Instruction *visitXor(BinaryOperator &I); |
| 113 | Instruction *visitSetCondInst(BinaryOperator &I); |
Reid Spencer | 279fa25 | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 114 | Instruction *visitSetCondInstWithCastAndConstant(BinaryOperator&I, |
| 115 | CastInst*LHSI, |
| 116 | ConstantInt* CI); |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 117 | Instruction *visitShiftInst(ShiftInst &I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 118 | Instruction *visitCastInst(CastInst &CI); |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 119 | Instruction *visitSelectInst(SelectInst &CI); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 120 | Instruction *visitCallInst(CallInst &CI); |
| 121 | Instruction *visitInvokeInst(InvokeInst &II); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 122 | Instruction *visitPHINode(PHINode &PN); |
| 123 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 124 | Instruction *visitAllocationInst(AllocationInst &AI); |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 125 | Instruction *visitFreeInst(FreeInst &FI); |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 126 | Instruction *visitLoadInst(LoadInst &LI); |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 127 | Instruction *visitBranchInst(BranchInst &BI); |
Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 128 | Instruction *visitSwitchInst(SwitchInst &SI); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 129 | |
| 130 | // visitInstruction - Specify what to return for unhandled instructions... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 131 | Instruction *visitInstruction(Instruction &I) { return 0; } |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 132 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 133 | private: |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 134 | Instruction *visitCallSite(CallSite CS); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 135 | bool transformConstExprCastCall(CallSite CS); |
| 136 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 137 | public: |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 138 | // InsertNewInstBefore - insert an instruction New before instruction Old |
| 139 | // in the program. Add the new instruction to the worklist. |
| 140 | // |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 141 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Chris Lattner | 65217ff | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 142 | assert(New && New->getParent() == 0 && |
| 143 | "New instruction already inserted into a basic block!"); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 144 | BasicBlock *BB = Old.getParent(); |
| 145 | BB->getInstList().insert(&Old, New); // Insert inst |
| 146 | WorkList.push_back(New); // Add to worklist |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 147 | return New; |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 150 | /// InsertCastBefore - Insert a cast of V to TY before the instruction POS. |
| 151 | /// This also adds the cast to the worklist. Finally, this returns the |
| 152 | /// cast. |
| 153 | Value *InsertCastBefore(Value *V, const Type *Ty, Instruction &Pos) { |
| 154 | if (V->getType() == Ty) return V; |
| 155 | |
| 156 | Instruction *C = new CastInst(V, Ty, V->getName(), &Pos); |
| 157 | WorkList.push_back(C); |
| 158 | return C; |
| 159 | } |
| 160 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 161 | // ReplaceInstUsesWith - This method is to be used when an instruction is |
| 162 | // found to be dead, replacable with another preexisting expression. Here |
| 163 | // we add all uses of I to the worklist, replace all uses of I with the new |
| 164 | // value, then return I, so that the inst combiner will know that I was |
| 165 | // modified. |
| 166 | // |
| 167 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 168 | AddUsersToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 8953b90 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 169 | if (&I != V) { |
| 170 | I.replaceAllUsesWith(V); |
| 171 | return &I; |
| 172 | } else { |
| 173 | // If we are replacing the instruction with itself, this must be in a |
| 174 | // segment of unreachable code, so just clobber the instruction. |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 175 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
Chris Lattner | 8953b90 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 176 | return &I; |
| 177 | } |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 178 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 179 | |
| 180 | // EraseInstFromFunction - When dealing with an instruction that has side |
| 181 | // effects or produces a void value, we can't rely on DCE to delete the |
| 182 | // instruction. Instead, visit methods should return the value returned by |
| 183 | // this function. |
| 184 | Instruction *EraseInstFromFunction(Instruction &I) { |
| 185 | assert(I.use_empty() && "Cannot erase instruction that is used!"); |
| 186 | AddUsesToWorkList(I); |
| 187 | removeFromWorkList(&I); |
Chris Lattner | 9530754 | 2004-11-18 21:41:39 +0000 | [diff] [blame] | 188 | I.eraseFromParent(); |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 189 | return 0; // Don't do anything with FI |
| 190 | } |
| 191 | |
| 192 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 193 | private: |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 194 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 195 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 196 | /// casts that are known to not do anything... |
| 197 | /// |
| 198 | Value *InsertOperandCastBefore(Value *V, const Type *DestTy, |
| 199 | Instruction *InsertBefore); |
| 200 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 201 | // SimplifyCommutative - This performs a few simplifications for commutative |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 202 | // operators. |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 203 | bool SimplifyCommutative(BinaryOperator &I); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 204 | |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 205 | |
| 206 | // FoldOpIntoPhi - Given a binary operator or cast instruction which has a |
| 207 | // PHI node as operand #0, see if we can fold the instruction into the PHI |
| 208 | // (which is only possible if all operands to the PHI are constants). |
| 209 | Instruction *FoldOpIntoPhi(Instruction &I); |
| 210 | |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 211 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 212 | // operator and they all are only used by the PHI, PHI together their |
| 213 | // inputs, and do the operation once, to the result of the PHI. |
| 214 | Instruction *FoldPHIArgOpIntoPHI(PHINode &PN); |
| 215 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 216 | Instruction *OptAndOp(Instruction *Op, ConstantIntegral *OpRHS, |
| 217 | ConstantIntegral *AndRHS, BinaryOperator &TheAnd); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 218 | |
| 219 | Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
| 220 | bool Inside, Instruction &IB); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 221 | }; |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 222 | |
Chris Lattner | c8b7092 | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 223 | RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions"); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 226 | // getComplexity: Assign a complexity or rank value to LLVM Values... |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 227 | // 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 228 | static unsigned getComplexity(Value *V) { |
| 229 | if (isa<Instruction>(V)) { |
| 230 | if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V)) |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 231 | return 3; |
| 232 | return 4; |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 233 | } |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 234 | if (isa<Argument>(V)) return 3; |
| 235 | return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2; |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 236 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 237 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 238 | // isOnlyUse - Return true if this instruction will be deleted if we stop using |
| 239 | // it. |
| 240 | static bool isOnlyUse(Value *V) { |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 241 | return V->hasOneUse() || isa<Constant>(V); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 244 | // getPromotedType - Return the specified type promoted as it would be to pass |
| 245 | // though a va_arg area... |
| 246 | static const Type *getPromotedType(const Type *Ty) { |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 247 | switch (Ty->getTypeID()) { |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 248 | case Type::SByteTyID: |
| 249 | case Type::ShortTyID: return Type::IntTy; |
| 250 | case Type::UByteTyID: |
| 251 | case Type::UShortTyID: return Type::UIntTy; |
| 252 | case Type::FloatTyID: return Type::DoubleTy; |
| 253 | default: return Ty; |
| 254 | } |
| 255 | } |
| 256 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 257 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 258 | // operators: |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 259 | // |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 260 | // 1. Order operands such that they are listed from right (least complex) to |
| 261 | // left (most complex). This puts constants before unary operators before |
| 262 | // binary operators. |
| 263 | // |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 264 | // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) |
| 265 | // 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] | 266 | // |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 267 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 268 | bool Changed = false; |
| 269 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) |
| 270 | Changed = !I.swapOperands(); |
| 271 | |
| 272 | if (!I.isAssociative()) return Changed; |
| 273 | Instruction::BinaryOps Opcode = I.getOpcode(); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 274 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) |
| 275 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { |
| 276 | if (isa<Constant>(I.getOperand(1))) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 277 | Constant *Folded = ConstantExpr::get(I.getOpcode(), |
| 278 | cast<Constant>(I.getOperand(1)), |
| 279 | cast<Constant>(Op->getOperand(1))); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 280 | I.setOperand(0, Op->getOperand(0)); |
| 281 | I.setOperand(1, Folded); |
| 282 | return true; |
| 283 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) |
| 284 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && |
| 285 | isOnlyUse(Op) && isOnlyUse(Op1)) { |
| 286 | Constant *C1 = cast<Constant>(Op->getOperand(1)); |
| 287 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); |
| 288 | |
| 289 | // 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] | 290 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 291 | Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0), |
| 292 | Op1->getOperand(0), |
| 293 | Op1->getName(), &I); |
| 294 | WorkList.push_back(New); |
| 295 | I.setOperand(0, New); |
| 296 | I.setOperand(1, Folded); |
| 297 | return true; |
| 298 | } |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 299 | } |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 300 | return Changed; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 301 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 302 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 303 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
| 304 | // if the LHS is a constant zero (which is the 'negate' form). |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 305 | // |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 306 | static inline Value *dyn_castNegVal(Value *V) { |
| 307 | if (BinaryOperator::isNeg(V)) |
| 308 | return BinaryOperator::getNegArgument(cast<BinaryOperator>(V)); |
| 309 | |
Chris Lattner | 9244df6 | 2003-04-30 22:19:10 +0000 | [diff] [blame] | 310 | // Constants can be considered to be negated values if they can be folded... |
| 311 | if (Constant *C = dyn_cast<Constant>(V)) |
Chris Lattner | 8f30caf | 2004-12-08 22:20:34 +0000 | [diff] [blame] | 312 | if (!isa<UndefValue>(C)) |
| 313 | return ConstantExpr::getNeg(C); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 314 | return 0; |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 317 | static inline Value *dyn_castNotVal(Value *V) { |
| 318 | if (BinaryOperator::isNot(V)) |
| 319 | return BinaryOperator::getNotArgument(cast<BinaryOperator>(V)); |
| 320 | |
| 321 | // Constants can be considered to be not'ed values... |
Chris Lattner | dd65d86 | 2003-04-30 22:34:06 +0000 | [diff] [blame] | 322 | if (ConstantIntegral *C = dyn_cast<ConstantIntegral>(V)) |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 323 | return ConstantExpr::getNot(C); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 324 | return 0; |
| 325 | } |
| 326 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 327 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
| 328 | // other computations (because it has a constant operand), return the |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 329 | // non-constant operand of the multiply, and set CST to point to the multiplier. |
| 330 | // Otherwise, return null. |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 331 | // |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 332 | static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) { |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 333 | if (V->hasOneUse() && V->getType()->isInteger()) |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 334 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 335 | if (I->getOpcode() == Instruction::Mul) |
Chris Lattner | 97013636 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 336 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 337 | return I->getOperand(0); |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 338 | if (I->getOpcode() == Instruction::Shl) |
Chris Lattner | 97013636 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 339 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) { |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 340 | // The multiplier is really 1 << CST. |
| 341 | Constant *One = ConstantInt::get(V->getType(), 1); |
| 342 | CST = cast<ConstantInt>(ConstantExpr::getShl(One, CST)); |
| 343 | return I->getOperand(0); |
| 344 | } |
| 345 | } |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 346 | return 0; |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 347 | } |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 348 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 349 | // Log2 - Calculate the log base 2 for the specified value if it is exactly a |
| 350 | // power of 2. |
| 351 | static unsigned Log2(uint64_t Val) { |
| 352 | assert(Val > 1 && "Values 0 and 1 should be handled elsewhere!"); |
| 353 | unsigned Count = 0; |
| 354 | while (Val != 1) { |
| 355 | if (Val & 1) return 0; // Multiple bits set? |
| 356 | Val >>= 1; |
| 357 | ++Count; |
| 358 | } |
| 359 | return Count; |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 362 | // AddOne, SubOne - Add or subtract a constant one from an integer constant... |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 363 | static ConstantInt *AddOne(ConstantInt *C) { |
| 364 | return cast<ConstantInt>(ConstantExpr::getAdd(C, |
| 365 | ConstantInt::get(C->getType(), 1))); |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 366 | } |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 367 | static ConstantInt *SubOne(ConstantInt *C) { |
| 368 | return cast<ConstantInt>(ConstantExpr::getSub(C, |
| 369 | ConstantInt::get(C->getType(), 1))); |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | // isTrueWhenEqual - Return true if the specified setcondinst instruction is |
| 373 | // true when both operands are equal... |
| 374 | // |
| 375 | static bool isTrueWhenEqual(Instruction &I) { |
| 376 | return I.getOpcode() == Instruction::SetEQ || |
| 377 | I.getOpcode() == Instruction::SetGE || |
| 378 | I.getOpcode() == Instruction::SetLE; |
| 379 | } |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 380 | |
| 381 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
| 382 | /// function is designed to check a chain of associative operators for a |
| 383 | /// potential to apply a certain optimization. Since the optimization may be |
| 384 | /// applicable if the expression was reassociated, this checks the chain, then |
| 385 | /// reassociates the expression as necessary to expose the optimization |
| 386 | /// opportunity. This makes use of a special Functor, which must define |
| 387 | /// 'shouldApply' and 'apply' methods. |
| 388 | /// |
| 389 | template<typename Functor> |
| 390 | Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { |
| 391 | unsigned Opcode = Root.getOpcode(); |
| 392 | Value *LHS = Root.getOperand(0); |
| 393 | |
| 394 | // Quick check, see if the immediate LHS matches... |
| 395 | if (F.shouldApply(LHS)) |
| 396 | return F.apply(Root); |
| 397 | |
| 398 | // Otherwise, if the LHS is not of the same opcode as the root, return. |
| 399 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 400 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 401 | // Should we apply this transform to the RHS? |
| 402 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); |
| 403 | |
| 404 | // If not to the RHS, check to see if we should apply to the LHS... |
| 405 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { |
| 406 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS |
| 407 | ShouldApply = true; |
| 408 | } |
| 409 | |
| 410 | // If the functor wants to apply the optimization to the RHS of LHSI, |
| 411 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) |
| 412 | if (ShouldApply) { |
| 413 | BasicBlock *BB = Root.getParent(); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 414 | |
| 415 | // Now all of the instructions are in the current basic block, go ahead |
| 416 | // and perform the reassociation. |
| 417 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); |
| 418 | |
| 419 | // First move the selected RHS to the LHS of the root... |
| 420 | Root.setOperand(0, LHSI->getOperand(1)); |
| 421 | |
| 422 | // Make what used to be the LHS of the root be the user of the root... |
| 423 | Value *ExtraOperand = TmpLHSI->getOperand(1); |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 424 | if (&Root == TmpLHSI) { |
Chris Lattner | 8953b90 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 425 | Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); |
| 426 | return 0; |
| 427 | } |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 428 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 429 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 430 | TmpLHSI->getParent()->getInstList().remove(TmpLHSI); |
| 431 | BasicBlock::iterator ARI = &Root; ++ARI; |
| 432 | BB->getInstList().insert(ARI, TmpLHSI); // Move TmpLHSI to after Root |
| 433 | ARI = Root; |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 434 | |
| 435 | // Now propagate the ExtraOperand down the chain of instructions until we |
| 436 | // get to LHSI. |
| 437 | while (TmpLHSI != LHSI) { |
| 438 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 439 | // Move the instruction to immediately before the chain we are |
| 440 | // constructing to avoid breaking dominance properties. |
| 441 | NextLHSI->getParent()->getInstList().remove(NextLHSI); |
| 442 | BB->getInstList().insert(ARI, NextLHSI); |
| 443 | ARI = NextLHSI; |
| 444 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 445 | Value *NextOp = NextLHSI->getOperand(1); |
| 446 | NextLHSI->setOperand(1, ExtraOperand); |
| 447 | TmpLHSI = NextLHSI; |
| 448 | ExtraOperand = NextOp; |
| 449 | } |
| 450 | |
| 451 | // Now that the instructions are reassociated, have the functor perform |
| 452 | // the transformation... |
| 453 | return F.apply(Root); |
| 454 | } |
| 455 | |
| 456 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
| 457 | } |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | |
| 462 | // AddRHS - Implements: X + X --> X << 1 |
| 463 | struct AddRHS { |
| 464 | Value *RHS; |
| 465 | AddRHS(Value *rhs) : RHS(rhs) {} |
| 466 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 467 | Instruction *apply(BinaryOperator &Add) const { |
| 468 | return new ShiftInst(Instruction::Shl, Add.getOperand(0), |
| 469 | ConstantInt::get(Type::UByteTy, 1)); |
| 470 | } |
| 471 | }; |
| 472 | |
| 473 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) |
| 474 | // iff C1&C2 == 0 |
| 475 | struct AddMaskingAnd { |
| 476 | Constant *C2; |
| 477 | AddMaskingAnd(Constant *c) : C2(c) {} |
| 478 | bool shouldApply(Value *LHS) const { |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 479 | ConstantInt *C1; |
| 480 | return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && |
| 481 | ConstantExpr::getAnd(C1, C2)->isNullValue(); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 482 | } |
| 483 | Instruction *apply(BinaryOperator &Add) const { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 484 | return BinaryOperator::createOr(Add.getOperand(0), Add.getOperand(1)); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 485 | } |
| 486 | }; |
| 487 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 488 | static Value *FoldOperationIntoSelectOperand(Instruction &BI, Value *SO, |
| 489 | InstCombiner *IC) { |
| 490 | // Figure out if the constant is the left or the right argument. |
| 491 | bool ConstIsRHS = isa<Constant>(BI.getOperand(1)); |
| 492 | Constant *ConstOperand = cast<Constant>(BI.getOperand(ConstIsRHS)); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 493 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 494 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
| 495 | if (ConstIsRHS) |
| 496 | return ConstantExpr::get(BI.getOpcode(), SOC, ConstOperand); |
| 497 | return ConstantExpr::get(BI.getOpcode(), ConstOperand, SOC); |
| 498 | } |
| 499 | |
| 500 | Value *Op0 = SO, *Op1 = ConstOperand; |
| 501 | if (!ConstIsRHS) |
| 502 | std::swap(Op0, Op1); |
| 503 | Instruction *New; |
| 504 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&BI)) |
| 505 | New = BinaryOperator::create(BO->getOpcode(), Op0, Op1); |
| 506 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(&BI)) |
| 507 | New = new ShiftInst(SI->getOpcode(), Op0, Op1); |
Chris Lattner | f9d9665 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 508 | else { |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 509 | assert(0 && "Unknown binary instruction type!"); |
Chris Lattner | f9d9665 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 510 | abort(); |
| 511 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 512 | return IC->InsertNewInstBefore(New, BI); |
| 513 | } |
| 514 | |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 515 | |
| 516 | /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI |
| 517 | /// node as operand #0, see if we can fold the instruction into the PHI (which |
| 518 | /// is only possible if all operands to the PHI are constants). |
| 519 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { |
| 520 | PHINode *PN = cast<PHINode>(I.getOperand(0)); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 521 | unsigned NumPHIValues = PN->getNumIncomingValues(); |
| 522 | if (!PN->hasOneUse() || NumPHIValues == 0 || |
| 523 | !isa<Constant>(PN->getIncomingValue(0))) return 0; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 524 | |
| 525 | // Check to see if all of the operands of the PHI are constants. If not, we |
| 526 | // cannot do the transformation. |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 527 | for (unsigned i = 1; i != NumPHIValues; ++i) |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 528 | if (!isa<Constant>(PN->getIncomingValue(i))) |
| 529 | return 0; |
| 530 | |
| 531 | // Okay, we can do the transformation: create the new PHI node. |
| 532 | PHINode *NewPN = new PHINode(I.getType(), I.getName()); |
| 533 | I.setName(""); |
| 534 | NewPN->op_reserve(PN->getNumOperands()); |
| 535 | InsertNewInstBefore(NewPN, *PN); |
| 536 | |
| 537 | // Next, add all of the operands to the PHI. |
| 538 | if (I.getNumOperands() == 2) { |
| 539 | Constant *C = cast<Constant>(I.getOperand(1)); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 540 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 541 | Constant *InV = cast<Constant>(PN->getIncomingValue(i)); |
| 542 | NewPN->addIncoming(ConstantExpr::get(I.getOpcode(), InV, C), |
| 543 | PN->getIncomingBlock(i)); |
| 544 | } |
| 545 | } else { |
| 546 | assert(isa<CastInst>(I) && "Unary op should be a cast!"); |
| 547 | const Type *RetTy = I.getType(); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 548 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 549 | Constant *InV = cast<Constant>(PN->getIncomingValue(i)); |
| 550 | NewPN->addIncoming(ConstantExpr::getCast(InV, RetTy), |
| 551 | PN->getIncomingBlock(i)); |
| 552 | } |
| 553 | } |
| 554 | return ReplaceInstUsesWith(I, NewPN); |
| 555 | } |
| 556 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 557 | // FoldBinOpIntoSelect - Given an instruction with a select as one operand and a |
| 558 | // constant as the other operand, try to fold the binary operator into the |
| 559 | // select arguments. |
| 560 | static Instruction *FoldBinOpIntoSelect(Instruction &BI, SelectInst *SI, |
| 561 | InstCombiner *IC) { |
| 562 | // Don't modify shared select instructions |
| 563 | if (!SI->hasOneUse()) return 0; |
| 564 | Value *TV = SI->getOperand(1); |
| 565 | Value *FV = SI->getOperand(2); |
| 566 | |
| 567 | if (isa<Constant>(TV) || isa<Constant>(FV)) { |
| 568 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(BI, TV, IC); |
| 569 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(BI, FV, IC); |
| 570 | |
| 571 | return new SelectInst(SI->getCondition(), SelectTrueVal, |
| 572 | SelectFalseVal); |
| 573 | } |
| 574 | return 0; |
| 575 | } |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 576 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 577 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 578 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 579 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 580 | |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 581 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 582 | // X + undef -> undef |
| 583 | if (isa<UndefValue>(RHS)) |
| 584 | return ReplaceInstUsesWith(I, RHS); |
| 585 | |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 586 | // X + 0 --> X |
| 587 | if (!I.getType()->isFloatingPoint() && // -0 + +0 = +0, so it's not a noop |
| 588 | RHSC->isNullValue()) |
| 589 | return ReplaceInstUsesWith(I, LHS); |
| 590 | |
| 591 | // X + (signbit) --> X ^ signbit |
| 592 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { |
| 593 | unsigned NumBits = CI->getType()->getPrimitiveSize()*8; |
| 594 | uint64_t Val = CI->getRawValue() & (1ULL << NumBits)-1; |
Chris Lattner | 33eb909 | 2004-11-05 04:45:43 +0000 | [diff] [blame] | 595 | if (Val == (1ULL << (NumBits-1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 596 | return BinaryOperator::createXor(LHS, RHS); |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 597 | } |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 598 | |
| 599 | if (isa<PHINode>(LHS)) |
| 600 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 601 | return NV; |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 602 | } |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 603 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 604 | // X + X --> X << 1 |
Robert Bocchino | 7b5b86c | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 605 | if (I.getType()->isInteger()) { |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 606 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; |
Robert Bocchino | 7b5b86c | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 607 | } |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 608 | |
Chris Lattner | 147e975 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 609 | // -A + B --> B - A |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 610 | if (Value *V = dyn_castNegVal(LHS)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 611 | return BinaryOperator::createSub(RHS, V); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 612 | |
| 613 | // A + -B --> A - B |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 614 | if (!isa<Constant>(RHS)) |
| 615 | if (Value *V = dyn_castNegVal(RHS)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 616 | return BinaryOperator::createSub(LHS, V); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 617 | |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 618 | ConstantInt *C2; |
| 619 | if (Value *X = dyn_castFoldableMul(LHS, C2)) { |
| 620 | if (X == RHS) // X*C + X --> X * (C+1) |
| 621 | return BinaryOperator::createMul(RHS, AddOne(C2)); |
| 622 | |
| 623 | // X*C1 + X*C2 --> X * (C1+C2) |
| 624 | ConstantInt *C1; |
| 625 | if (X == dyn_castFoldableMul(RHS, C1)) |
| 626 | return BinaryOperator::createMul(X, ConstantExpr::getAdd(C1, C2)); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | // X + X*C --> X * (C+1) |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 630 | if (dyn_castFoldableMul(RHS, C2) == LHS) |
| 631 | return BinaryOperator::createMul(LHS, AddOne(C2)); |
| 632 | |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 633 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 634 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 635 | if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 636 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) return R; |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 637 | |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 638 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 639 | Value *X; |
| 640 | if (match(LHS, m_Not(m_Value(X)))) { // ~X + C --> (C-1) - X |
| 641 | Constant *C= ConstantExpr::getSub(CRHS, ConstantInt::get(I.getType(), 1)); |
| 642 | return BinaryOperator::createSub(C, X); |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 643 | } |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 644 | |
Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 645 | // (X & FF00) + xx00 -> (X+xx00) & FF00 |
| 646 | if (LHS->hasOneUse() && match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) { |
| 647 | Constant *Anded = ConstantExpr::getAnd(CRHS, C2); |
| 648 | if (Anded == CRHS) { |
| 649 | // See if all bits from the first bit set in the Add RHS up are included |
| 650 | // in the mask. First, get the rightmost bit. |
| 651 | uint64_t AddRHSV = CRHS->getRawValue(); |
| 652 | |
| 653 | // Form a mask of all bits from the lowest bit added through the top. |
| 654 | uint64_t AddRHSHighBits = ~((AddRHSV & -AddRHSV)-1); |
| 655 | AddRHSHighBits &= (1ULL << C2->getType()->getPrimitiveSize()*8)-1; |
| 656 | |
| 657 | // See if the and mask includes all of these bits. |
| 658 | uint64_t AddRHSHighBitsAnd = AddRHSHighBits & C2->getRawValue(); |
| 659 | |
| 660 | if (AddRHSHighBits == AddRHSHighBitsAnd) { |
| 661 | // Okay, the xform is safe. Insert the new add pronto. |
| 662 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, CRHS, |
| 663 | LHS->getName()), I); |
| 664 | return BinaryOperator::createAnd(NewAdd, C2); |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 670 | // Try to fold constant add into select arguments. |
| 671 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) |
| 672 | if (Instruction *R = FoldBinOpIntoSelect(I, SI, this)) |
| 673 | return R; |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 676 | return Changed ? &I : 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Chris Lattner | bdb0ce0 | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 679 | // isSignBit - Return true if the value represented by the constant only has the |
| 680 | // highest order bit set. |
| 681 | static bool isSignBit(ConstantInt *CI) { |
| 682 | unsigned NumBits = CI->getType()->getPrimitiveSize()*8; |
| 683 | return (CI->getRawValue() & ~(-1LL << NumBits)) == (1ULL << (NumBits-1)); |
| 684 | } |
| 685 | |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 686 | static unsigned getTypeSizeInBits(const Type *Ty) { |
| 687 | return Ty == Type::BoolTy ? 1 : Ty->getPrimitiveSize()*8; |
| 688 | } |
| 689 | |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 690 | /// RemoveNoopCast - Strip off nonconverting casts from the value. |
| 691 | /// |
| 692 | static Value *RemoveNoopCast(Value *V) { |
| 693 | if (CastInst *CI = dyn_cast<CastInst>(V)) { |
| 694 | const Type *CTy = CI->getType(); |
| 695 | const Type *OpTy = CI->getOperand(0)->getType(); |
| 696 | if (CTy->isInteger() && OpTy->isInteger()) { |
| 697 | if (CTy->getPrimitiveSize() == OpTy->getPrimitiveSize()) |
| 698 | return RemoveNoopCast(CI->getOperand(0)); |
| 699 | } else if (isa<PointerType>(CTy) && isa<PointerType>(OpTy)) |
| 700 | return RemoveNoopCast(CI->getOperand(0)); |
| 701 | } |
| 702 | return V; |
| 703 | } |
| 704 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 705 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 706 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 707 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 708 | if (Op0 == Op1) // sub X, X -> 0 |
| 709 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 710 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 711 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 712 | if (Value *V = dyn_castNegVal(Op1)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 713 | return BinaryOperator::createAdd(Op0, V); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 714 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 715 | if (isa<UndefValue>(Op0)) |
| 716 | return ReplaceInstUsesWith(I, Op0); // undef - X -> undef |
| 717 | if (isa<UndefValue>(Op1)) |
| 718 | return ReplaceInstUsesWith(I, Op1); // X - undef -> undef |
| 719 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 720 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
| 721 | // Replace (-1 - A) with (~A)... |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 722 | if (C->isAllOnesValue()) |
| 723 | return BinaryOperator::createNot(Op1); |
Chris Lattner | ad3c495 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 724 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 725 | // C - ~X == X + (1+C) |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 726 | Value *X; |
| 727 | if (match(Op1, m_Not(m_Value(X)))) |
| 728 | return BinaryOperator::createAdd(X, |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 729 | ConstantExpr::getAdd(C, ConstantInt::get(I.getType(), 1))); |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 730 | // -((uint)X >> 31) -> ((int)X >> 31) |
| 731 | // -((int)X >> 31) -> ((uint)X >> 31) |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 732 | if (C->isNullValue()) { |
| 733 | Value *NoopCastedRHS = RemoveNoopCast(Op1); |
| 734 | if (ShiftInst *SI = dyn_cast<ShiftInst>(NoopCastedRHS)) |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 735 | if (SI->getOpcode() == Instruction::Shr) |
| 736 | if (ConstantUInt *CU = dyn_cast<ConstantUInt>(SI->getOperand(1))) { |
| 737 | const Type *NewTy; |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 738 | if (SI->getType()->isSigned()) |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 739 | NewTy = SI->getType()->getUnsignedVersion(); |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 740 | else |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 741 | NewTy = SI->getType()->getSignedVersion(); |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 742 | // 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] | 743 | if (CU->getValue() == SI->getType()->getPrimitiveSize()*8-1) { |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 744 | // Ok, the transformation is safe. Insert a cast of the incoming |
| 745 | // value, then the new shift, then the new cast. |
| 746 | Instruction *FirstCast = new CastInst(SI->getOperand(0), NewTy, |
| 747 | SI->getOperand(0)->getName()); |
| 748 | Value *InV = InsertNewInstBefore(FirstCast, I); |
| 749 | Instruction *NewShift = new ShiftInst(Instruction::Shr, FirstCast, |
| 750 | CU, SI->getName()); |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 751 | if (NewShift->getType() == I.getType()) |
| 752 | return NewShift; |
| 753 | else { |
| 754 | InV = InsertNewInstBefore(NewShift, I); |
| 755 | return new CastInst(NewShift, I.getType()); |
| 756 | } |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 757 | } |
| 758 | } |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 759 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 760 | |
| 761 | // Try to fold constant sub into select arguments. |
| 762 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 763 | if (Instruction *R = FoldBinOpIntoSelect(I, SI, this)) |
| 764 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 765 | |
| 766 | if (isa<PHINode>(Op0)) |
| 767 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 768 | return NV; |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 771 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 772 | if (Op1I->hasOneUse()) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 773 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 774 | // is not used by anyone else... |
| 775 | // |
Chris Lattner | c2f0aa5 | 2004-02-02 20:09:56 +0000 | [diff] [blame] | 776 | if (Op1I->getOpcode() == Instruction::Sub && |
| 777 | !Op1I->getType()->isFloatingPoint()) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 778 | // Swap the two operands of the subexpr... |
| 779 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 780 | Op1I->setOperand(0, IIOp1); |
| 781 | Op1I->setOperand(1, IIOp0); |
| 782 | |
| 783 | // Create the new top level add instruction... |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 784 | return BinaryOperator::createAdd(Op0, Op1); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 788 | // |
| 789 | if (Op1I->getOpcode() == Instruction::And && |
| 790 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 791 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 792 | |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 793 | Value *NewNot = |
| 794 | InsertNewInstBefore(BinaryOperator::createNot(OtherOp, "B.not"), I); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 795 | return BinaryOperator::createAnd(Op0, NewNot); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 796 | } |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 797 | |
Chris Lattner | 0aee4b7 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 798 | // -(X sdiv C) -> (X sdiv -C) |
| 799 | if (Op1I->getOpcode() == Instruction::Div) |
| 800 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0)) |
| 801 | if (CSI->getValue() == 0) |
| 802 | if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) |
| 803 | return BinaryOperator::createDiv(Op1I->getOperand(0), |
| 804 | ConstantExpr::getNeg(DivRHS)); |
| 805 | |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 806 | // X - X*C --> X * (1-C) |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 807 | ConstantInt *C2; |
| 808 | if (dyn_castFoldableMul(Op1I, C2) == Op0) { |
| 809 | Constant *CP1 = |
| 810 | ConstantExpr::getSub(ConstantInt::get(I.getType(), 1), C2); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 811 | return BinaryOperator::createMul(Op0, CP1); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 812 | } |
Chris Lattner | ad3c495 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 813 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 814 | |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 815 | |
| 816 | ConstantInt *C1; |
| 817 | if (Value *X = dyn_castFoldableMul(Op0, C1)) { |
| 818 | if (X == Op1) { // X*C - X --> X * (C-1) |
| 819 | Constant *CP1 = ConstantExpr::getSub(C1, ConstantInt::get(I.getType(),1)); |
| 820 | return BinaryOperator::createMul(Op1, CP1); |
| 821 | } |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 822 | |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 823 | ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2) |
| 824 | if (X == dyn_castFoldableMul(Op1, C2)) |
| 825 | return BinaryOperator::createMul(Op1, ConstantExpr::getSub(C1, C2)); |
| 826 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 827 | return 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 830 | /// isSignBitCheck - Given an exploded setcc instruction, return true if it is |
| 831 | /// really just returns true if the most significant (sign) bit is set. |
| 832 | static bool isSignBitCheck(unsigned Opcode, Value *LHS, ConstantInt *RHS) { |
| 833 | if (RHS->getType()->isSigned()) { |
| 834 | // True if source is LHS < 0 or LHS <= -1 |
| 835 | return Opcode == Instruction::SetLT && RHS->isNullValue() || |
| 836 | Opcode == Instruction::SetLE && RHS->isAllOnesValue(); |
| 837 | } else { |
| 838 | ConstantUInt *RHSC = cast<ConstantUInt>(RHS); |
| 839 | // True if source is LHS > 127 or LHS >= 128, where the constants depend on |
| 840 | // the size of the integer type. |
| 841 | if (Opcode == Instruction::SetGE) |
| 842 | return RHSC->getValue() == 1ULL<<(RHS->getType()->getPrimitiveSize()*8-1); |
| 843 | if (Opcode == Instruction::SetGT) |
| 844 | return RHSC->getValue() == |
| 845 | (1ULL << (RHS->getType()->getPrimitiveSize()*8-1))-1; |
| 846 | } |
| 847 | return false; |
| 848 | } |
| 849 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 850 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 851 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 852 | Value *Op0 = I.getOperand(0); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 853 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 854 | if (isa<UndefValue>(I.getOperand(1))) // undef * X -> 0 |
| 855 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 856 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 857 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 858 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 859 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 860 | |
| 861 | // ((X << C1)*C2) == (X * (C2 << C1)) |
| 862 | if (ShiftInst *SI = dyn_cast<ShiftInst>(Op0)) |
| 863 | if (SI->getOpcode() == Instruction::Shl) |
| 864 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 865 | return BinaryOperator::createMul(SI->getOperand(0), |
| 866 | ConstantExpr::getShl(CI, ShOp)); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 867 | |
Chris Lattner | cce81be | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 868 | if (CI->isNullValue()) |
| 869 | return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 |
| 870 | if (CI->equalsInt(1)) // X * 1 == X |
| 871 | return ReplaceInstUsesWith(I, Op0); |
| 872 | if (CI->isAllOnesValue()) // X * -1 == 0 - X |
Chris Lattner | 35236d8 | 2003-06-25 17:09:20 +0000 | [diff] [blame] | 873 | return BinaryOperator::createNeg(Op0, I.getName()); |
Chris Lattner | 31ba129 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 874 | |
Chris Lattner | cce81be | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 875 | int64_t Val = (int64_t)cast<ConstantInt>(CI)->getRawValue(); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 876 | if (uint64_t C = Log2(Val)) // Replace X*(2^C) with X << C |
| 877 | return new ShiftInst(Instruction::Shl, Op0, |
| 878 | ConstantUInt::get(Type::UByteTy, C)); |
Robert Bocchino | 7b5b86c | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 879 | } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 880 | if (Op1F->isNullValue()) |
| 881 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 31ba129 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 882 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 883 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 884 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) |
| 885 | if (Op1F->getValue() == 1.0) |
| 886 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
| 887 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 888 | |
| 889 | // Try to fold constant mul into select arguments. |
| 890 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 891 | if (Instruction *R = FoldBinOpIntoSelect(I, SI, this)) |
| 892 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 893 | |
| 894 | if (isa<PHINode>(Op0)) |
| 895 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 896 | return NV; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Chris Lattner | 934a64cf | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 899 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
| 900 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 901 | return BinaryOperator::createMul(Op0v, Op1v); |
Chris Lattner | 934a64cf | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 902 | |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 903 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 904 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 905 | // See if we can simplify things based on how the boolean was originally |
| 906 | // formed. |
| 907 | CastInst *BoolCast = 0; |
| 908 | if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(0))) |
| 909 | if (CI->getOperand(0)->getType() == Type::BoolTy) |
| 910 | BoolCast = CI; |
| 911 | if (!BoolCast) |
| 912 | if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(1))) |
| 913 | if (CI->getOperand(0)->getType() == Type::BoolTy) |
| 914 | BoolCast = CI; |
| 915 | if (BoolCast) { |
| 916 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(BoolCast->getOperand(0))) { |
| 917 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); |
| 918 | const Type *SCOpTy = SCIOp0->getType(); |
| 919 | |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 920 | // If the setcc is true iff the sign bit of X is set, then convert this |
| 921 | // multiply into a shift/and combination. |
| 922 | if (isa<ConstantInt>(SCIOp1) && |
| 923 | isSignBitCheck(SCI->getOpcode(), SCIOp0, cast<ConstantInt>(SCIOp1))) { |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 924 | // Shift the X value right to turn it into "all signbits". |
| 925 | Constant *Amt = ConstantUInt::get(Type::UByteTy, |
| 926 | SCOpTy->getPrimitiveSize()*8-1); |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 927 | if (SCIOp0->getType()->isUnsigned()) { |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 928 | const Type *NewTy = SCIOp0->getType()->getSignedVersion(); |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 929 | SCIOp0 = InsertNewInstBefore(new CastInst(SCIOp0, NewTy, |
| 930 | SCIOp0->getName()), I); |
| 931 | } |
| 932 | |
| 933 | Value *V = |
| 934 | InsertNewInstBefore(new ShiftInst(Instruction::Shr, SCIOp0, Amt, |
| 935 | BoolCast->getOperand(0)->getName()+ |
| 936 | ".mask"), I); |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 937 | |
| 938 | // If the multiply type is not the same as the source type, sign extend |
| 939 | // or truncate to the multiply type. |
| 940 | if (I.getType() != V->getType()) |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 941 | V = InsertNewInstBefore(new CastInst(V, I.getType(), V->getName()),I); |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 942 | |
| 943 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 944 | return BinaryOperator::createAnd(V, OtherOp); |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | } |
| 948 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 949 | return Changed ? &I : 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 950 | } |
| 951 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 952 | Instruction *InstCombiner::visitDiv(BinaryOperator &I) { |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 953 | if (isa<UndefValue>(I.getOperand(0))) // undef / X -> 0 |
| 954 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 955 | if (isa<UndefValue>(I.getOperand(1))) |
| 956 | return ReplaceInstUsesWith(I, I.getOperand(1)); // X / undef -> undef |
| 957 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 958 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) { |
Chris Lattner | e20c334 | 2004-04-26 14:01:59 +0000 | [diff] [blame] | 959 | // div X, 1 == X |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 960 | if (RHS->equalsInt(1)) |
| 961 | return ReplaceInstUsesWith(I, I.getOperand(0)); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 962 | |
Chris Lattner | e20c334 | 2004-04-26 14:01:59 +0000 | [diff] [blame] | 963 | // div X, -1 == -X |
| 964 | if (RHS->isAllOnesValue()) |
| 965 | return BinaryOperator::createNeg(I.getOperand(0)); |
| 966 | |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 967 | if (Instruction *LHS = dyn_cast<Instruction>(I.getOperand(0))) |
| 968 | if (LHS->getOpcode() == Instruction::Div) |
| 969 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 970 | // (X / C1) / C2 -> X / (C1*C2) |
| 971 | return BinaryOperator::createDiv(LHS->getOperand(0), |
| 972 | ConstantExpr::getMul(RHS, LHSRHS)); |
| 973 | } |
| 974 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 975 | // Check to see if this is an unsigned division with an exact power of 2, |
| 976 | // if so, convert to a right shift. |
| 977 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS)) |
| 978 | if (uint64_t Val = C->getValue()) // Don't break X / 0 |
| 979 | if (uint64_t C = Log2(Val)) |
| 980 | return new ShiftInst(Instruction::Shr, I.getOperand(0), |
| 981 | ConstantUInt::get(Type::UByteTy, C)); |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 982 | |
Chris Lattner | 4ad0835 | 2004-10-09 02:50:40 +0000 | [diff] [blame] | 983 | // -X/C -> X/-C |
| 984 | if (RHS->getType()->isSigned()) |
| 985 | if (Value *LHSNeg = dyn_castNegVal(I.getOperand(0))) |
| 986 | return BinaryOperator::createDiv(LHSNeg, ConstantExpr::getNeg(RHS)); |
| 987 | |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 988 | if (isa<PHINode>(I.getOperand(0)) && !RHS->isNullValue()) |
| 989 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 990 | return NV; |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | // 0 / X == 0, we don't need to preserve faults! |
| 994 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(I.getOperand(0))) |
| 995 | if (LHS->equalsInt(0)) |
| 996 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 997 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 998 | return 0; |
| 999 | } |
| 1000 | |
| 1001 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1002 | Instruction *InstCombiner::visitRem(BinaryOperator &I) { |
Chris Lattner | 7fd5f07 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 1003 | if (I.getType()->isSigned()) |
| 1004 | if (Value *RHSNeg = dyn_castNegVal(I.getOperand(1))) |
Chris Lattner | 98c6bdf | 2004-07-06 07:11:42 +0000 | [diff] [blame] | 1005 | if (!isa<ConstantSInt>(RHSNeg) || |
Chris Lattner | 8e72606 | 2004-08-09 21:05:48 +0000 | [diff] [blame] | 1006 | cast<ConstantSInt>(RHSNeg)->getValue() > 0) { |
Chris Lattner | 7fd5f07 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 1007 | // X % -Y -> X % Y |
| 1008 | AddUsesToWorkList(I); |
| 1009 | I.setOperand(1, RHSNeg); |
| 1010 | return &I; |
| 1011 | } |
| 1012 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1013 | if (isa<UndefValue>(I.getOperand(0))) // undef % X -> 0 |
| 1014 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1015 | if (isa<UndefValue>(I.getOperand(1))) |
| 1016 | return ReplaceInstUsesWith(I, I.getOperand(1)); // X % undef -> undef |
| 1017 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1018 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) { |
| 1019 | if (RHS->equalsInt(1)) // X % 1 == 0 |
| 1020 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1021 | |
| 1022 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 1023 | // if so, convert to a bitwise and. |
| 1024 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS)) |
| 1025 | 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] | 1026 | if (!(Val & (Val-1))) // Power of 2 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1027 | return BinaryOperator::createAnd(I.getOperand(0), |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1028 | ConstantUInt::get(I.getType(), Val-1)); |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1029 | if (isa<PHINode>(I.getOperand(0)) && !RHS->isNullValue()) |
| 1030 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1031 | return NV; |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | // 0 % X == 0, we don't need to preserve faults! |
| 1035 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(I.getOperand(0))) |
| 1036 | if (LHS->equalsInt(0)) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1037 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1038 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1039 | return 0; |
| 1040 | } |
| 1041 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1042 | // isMaxValueMinusOne - return true if this is Max-1 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1043 | static bool isMaxValueMinusOne(const ConstantInt *C) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1044 | if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) { |
| 1045 | // Calculate -1 casted to the right type... |
| 1046 | unsigned TypeBits = C->getType()->getPrimitiveSize()*8; |
| 1047 | uint64_t Val = ~0ULL; // All ones |
| 1048 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 1049 | return CU->getValue() == Val-1; |
| 1050 | } |
| 1051 | |
| 1052 | const ConstantSInt *CS = cast<ConstantSInt>(C); |
| 1053 | |
| 1054 | // Calculate 0111111111..11111 |
| 1055 | unsigned TypeBits = C->getType()->getPrimitiveSize()*8; |
| 1056 | int64_t Val = INT64_MAX; // All ones |
| 1057 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 1058 | return CS->getValue() == Val-1; |
| 1059 | } |
| 1060 | |
| 1061 | // isMinValuePlusOne - return true if this is Min+1 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1062 | static bool isMinValuePlusOne(const ConstantInt *C) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1063 | if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) |
| 1064 | return CU->getValue() == 1; |
| 1065 | |
| 1066 | const ConstantSInt *CS = cast<ConstantSInt>(C); |
| 1067 | |
| 1068 | // Calculate 1111111111000000000000 |
| 1069 | unsigned TypeBits = C->getType()->getPrimitiveSize()*8; |
| 1070 | int64_t Val = -1; // All ones |
| 1071 | Val <<= TypeBits-1; // Shift over to the right spot |
| 1072 | return CS->getValue() == Val+1; |
| 1073 | } |
| 1074 | |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1075 | // isOneBitSet - Return true if there is exactly one bit set in the specified |
| 1076 | // constant. |
| 1077 | static bool isOneBitSet(const ConstantInt *CI) { |
| 1078 | uint64_t V = CI->getRawValue(); |
| 1079 | return V && (V & (V-1)) == 0; |
| 1080 | } |
| 1081 | |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 1082 | #if 0 // Currently unused |
| 1083 | // isLowOnes - Return true if the constant is of the form 0+1+. |
| 1084 | static bool isLowOnes(const ConstantInt *CI) { |
| 1085 | uint64_t V = CI->getRawValue(); |
| 1086 | |
| 1087 | // There won't be bits set in parts that the type doesn't contain. |
| 1088 | V &= ConstantInt::getAllOnesValue(CI->getType())->getRawValue(); |
| 1089 | |
| 1090 | uint64_t U = V+1; // If it is low ones, this should be a power of two. |
| 1091 | return U && V && (U & V) == 0; |
| 1092 | } |
| 1093 | #endif |
| 1094 | |
| 1095 | // isHighOnes - Return true if the constant is of the form 1+0+. |
| 1096 | // This is the same as lowones(~X). |
| 1097 | static bool isHighOnes(const ConstantInt *CI) { |
| 1098 | uint64_t V = ~CI->getRawValue(); |
| 1099 | |
| 1100 | // There won't be bits set in parts that the type doesn't contain. |
| 1101 | V &= ConstantInt::getAllOnesValue(CI->getType())->getRawValue(); |
| 1102 | |
| 1103 | uint64_t U = V+1; // If it is low ones, this should be a power of two. |
| 1104 | return U && V && (U & V) == 0; |
| 1105 | } |
| 1106 | |
| 1107 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1108 | /// getSetCondCode - Encode a setcc opcode into a three bit mask. These bits |
| 1109 | /// are carefully arranged to allow folding of expressions such as: |
| 1110 | /// |
| 1111 | /// (A < B) | (A > B) --> (A != B) |
| 1112 | /// |
| 1113 | /// Bit value '4' represents that the comparison is true if A > B, bit value '2' |
| 1114 | /// represents that the comparison is true if A == B, and bit value '1' is true |
| 1115 | /// if A < B. |
| 1116 | /// |
| 1117 | static unsigned getSetCondCode(const SetCondInst *SCI) { |
| 1118 | switch (SCI->getOpcode()) { |
| 1119 | // False -> 0 |
| 1120 | case Instruction::SetGT: return 1; |
| 1121 | case Instruction::SetEQ: return 2; |
| 1122 | case Instruction::SetGE: return 3; |
| 1123 | case Instruction::SetLT: return 4; |
| 1124 | case Instruction::SetNE: return 5; |
| 1125 | case Instruction::SetLE: return 6; |
| 1126 | // True -> 7 |
| 1127 | default: |
| 1128 | assert(0 && "Invalid SetCC opcode!"); |
| 1129 | return 0; |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | /// getSetCCValue - This is the complement of getSetCondCode, which turns an |
| 1134 | /// opcode and two operands into either a constant true or false, or a brand new |
| 1135 | /// SetCC instruction. |
| 1136 | static Value *getSetCCValue(unsigned Opcode, Value *LHS, Value *RHS) { |
| 1137 | switch (Opcode) { |
| 1138 | case 0: return ConstantBool::False; |
| 1139 | case 1: return new SetCondInst(Instruction::SetGT, LHS, RHS); |
| 1140 | case 2: return new SetCondInst(Instruction::SetEQ, LHS, RHS); |
| 1141 | case 3: return new SetCondInst(Instruction::SetGE, LHS, RHS); |
| 1142 | case 4: return new SetCondInst(Instruction::SetLT, LHS, RHS); |
| 1143 | case 5: return new SetCondInst(Instruction::SetNE, LHS, RHS); |
| 1144 | case 6: return new SetCondInst(Instruction::SetLE, LHS, RHS); |
| 1145 | case 7: return ConstantBool::True; |
| 1146 | default: assert(0 && "Illegal SetCCCode!"); return 0; |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | // FoldSetCCLogical - Implements (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B) |
| 1151 | struct FoldSetCCLogical { |
| 1152 | InstCombiner &IC; |
| 1153 | Value *LHS, *RHS; |
| 1154 | FoldSetCCLogical(InstCombiner &ic, SetCondInst *SCI) |
| 1155 | : IC(ic), LHS(SCI->getOperand(0)), RHS(SCI->getOperand(1)) {} |
| 1156 | bool shouldApply(Value *V) const { |
| 1157 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(V)) |
| 1158 | return (SCI->getOperand(0) == LHS && SCI->getOperand(1) == RHS || |
| 1159 | SCI->getOperand(0) == RHS && SCI->getOperand(1) == LHS); |
| 1160 | return false; |
| 1161 | } |
| 1162 | Instruction *apply(BinaryOperator &Log) const { |
| 1163 | SetCondInst *SCI = cast<SetCondInst>(Log.getOperand(0)); |
| 1164 | if (SCI->getOperand(0) != LHS) { |
| 1165 | assert(SCI->getOperand(1) == LHS); |
| 1166 | SCI->swapOperands(); // Swap the LHS and RHS of the SetCC |
| 1167 | } |
| 1168 | |
| 1169 | unsigned LHSCode = getSetCondCode(SCI); |
| 1170 | unsigned RHSCode = getSetCondCode(cast<SetCondInst>(Log.getOperand(1))); |
| 1171 | unsigned Code; |
| 1172 | switch (Log.getOpcode()) { |
| 1173 | case Instruction::And: Code = LHSCode & RHSCode; break; |
| 1174 | case Instruction::Or: Code = LHSCode | RHSCode; break; |
| 1175 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; |
Chris Lattner | 2caaaba | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 1176 | default: assert(0 && "Illegal logical opcode!"); return 0; |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | Value *RV = getSetCCValue(Code, LHS, RHS); |
| 1180 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 1181 | return I; |
| 1182 | // Otherwise, it's a constant boolean value... |
| 1183 | return IC.ReplaceInstUsesWith(Log, RV); |
| 1184 | } |
| 1185 | }; |
| 1186 | |
| 1187 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1188 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
| 1189 | // the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'. Op is |
| 1190 | // guaranteed to be either a shift instruction or a binary operator. |
| 1191 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
| 1192 | ConstantIntegral *OpRHS, |
| 1193 | ConstantIntegral *AndRHS, |
| 1194 | BinaryOperator &TheAnd) { |
| 1195 | Value *X = Op->getOperand(0); |
Chris Lattner | fcf21a7 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 1196 | Constant *Together = 0; |
| 1197 | if (!isa<ShiftInst>(Op)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1198 | Together = ConstantExpr::getAnd(AndRHS, OpRHS); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1199 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1200 | switch (Op->getOpcode()) { |
| 1201 | case Instruction::Xor: |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1202 | if (Together->isNullValue()) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1203 | // (X ^ C1) & C2 --> (X & C2) iff (C1&C2) == 0 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1204 | return BinaryOperator::createAnd(X, AndRHS); |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1205 | } else if (Op->hasOneUse()) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1206 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
| 1207 | std::string OpName = Op->getName(); Op->setName(""); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1208 | Instruction *And = BinaryOperator::createAnd(X, AndRHS, OpName); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1209 | InsertNewInstBefore(And, TheAnd); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1210 | return BinaryOperator::createXor(And, Together); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1211 | } |
| 1212 | break; |
| 1213 | case Instruction::Or: |
| 1214 | // (X | C1) & C2 --> X & C2 iff C1 & C1 == 0 |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1215 | if (Together->isNullValue()) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1216 | return BinaryOperator::createAnd(X, AndRHS); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1217 | else { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1218 | if (Together == AndRHS) // (X | C) & C --> C |
| 1219 | return ReplaceInstUsesWith(TheAnd, AndRHS); |
| 1220 | |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1221 | if (Op->hasOneUse() && Together != OpRHS) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1222 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 |
| 1223 | std::string Op0Name = Op->getName(); Op->setName(""); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1224 | Instruction *Or = BinaryOperator::createOr(X, Together, Op0Name); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1225 | InsertNewInstBefore(Or, TheAnd); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1226 | return BinaryOperator::createAnd(Or, AndRHS); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1227 | } |
| 1228 | } |
| 1229 | break; |
| 1230 | case Instruction::Add: |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1231 | if (Op->hasOneUse()) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1232 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 1233 | // of the bit. First thing to check is to see if this AND is with a |
| 1234 | // single bit constant. |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1235 | uint64_t AndRHSV = cast<ConstantInt>(AndRHS)->getRawValue(); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1236 | |
| 1237 | // Clear bits that are not part of the constant. |
| 1238 | AndRHSV &= (1ULL << AndRHS->getType()->getPrimitiveSize()*8)-1; |
| 1239 | |
| 1240 | // If there is only one bit set... |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1241 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1242 | // Ok, at this point, we know that we are masking the result of the |
| 1243 | // ADD down to exactly one bit. If the constant we are adding has |
| 1244 | // no bits set below this bit, then we can eliminate the ADD. |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1245 | uint64_t AddRHS = cast<ConstantInt>(OpRHS)->getRawValue(); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1246 | |
| 1247 | // Check to see if any bits below the one bit set in AndRHSV are set. |
| 1248 | if ((AddRHS & (AndRHSV-1)) == 0) { |
| 1249 | // If not, the only thing that can effect the output of the AND is |
| 1250 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 1251 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 1252 | // no effect. |
| 1253 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop |
| 1254 | TheAnd.setOperand(0, X); |
| 1255 | return &TheAnd; |
| 1256 | } else { |
| 1257 | std::string Name = Op->getName(); Op->setName(""); |
| 1258 | // Pull the XOR out of the AND. |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1259 | Instruction *NewAnd = BinaryOperator::createAnd(X, AndRHS, Name); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1260 | InsertNewInstBefore(NewAnd, TheAnd); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1261 | return BinaryOperator::createXor(NewAnd, AndRHS); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1262 | } |
| 1263 | } |
| 1264 | } |
| 1265 | } |
| 1266 | break; |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1267 | |
| 1268 | case Instruction::Shl: { |
| 1269 | // We know that the AND will not produce any of the bits shifted in, so if |
| 1270 | // the anded constant includes them, clear them now! |
| 1271 | // |
| 1272 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1273 | Constant *ShlMask = ConstantExpr::getShl(AllOne, OpRHS); |
| 1274 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShlMask); |
| 1275 | |
| 1276 | if (CI == ShlMask) { // Masking out bits that the shift already masks |
| 1277 | return ReplaceInstUsesWith(TheAnd, Op); // No need for the and. |
| 1278 | } else if (CI != AndRHS) { // Reducing bits set in and. |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1279 | TheAnd.setOperand(1, CI); |
| 1280 | return &TheAnd; |
| 1281 | } |
| 1282 | break; |
| 1283 | } |
| 1284 | case Instruction::Shr: |
| 1285 | // We know that the AND will not produce any of the bits shifted in, so if |
| 1286 | // the anded constant includes them, clear them now! This only applies to |
| 1287 | // unsigned shifts, because a signed shr may bring in set bits! |
| 1288 | // |
| 1289 | if (AndRHS->getType()->isUnsigned()) { |
| 1290 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1291 | Constant *ShrMask = ConstantExpr::getShr(AllOne, OpRHS); |
| 1292 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask); |
| 1293 | |
| 1294 | if (CI == ShrMask) { // Masking out bits that the shift already masks. |
| 1295 | return ReplaceInstUsesWith(TheAnd, Op); |
| 1296 | } else if (CI != AndRHS) { |
| 1297 | TheAnd.setOperand(1, CI); // Reduce bits set in and cst. |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1298 | return &TheAnd; |
| 1299 | } |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1300 | } else { // Signed shr. |
| 1301 | // See if this is shifting in some sign extension, then masking it out |
| 1302 | // with an and. |
| 1303 | if (Op->hasOneUse()) { |
| 1304 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
| 1305 | Constant *ShrMask = ConstantExpr::getUShr(AllOne, OpRHS); |
| 1306 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask); |
Chris Lattner | 5c3c21e | 2004-10-22 04:53:16 +0000 | [diff] [blame] | 1307 | if (CI == AndRHS) { // Masking out bits shifted in. |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1308 | // Make the argument unsigned. |
| 1309 | Value *ShVal = Op->getOperand(0); |
| 1310 | ShVal = InsertCastBefore(ShVal, |
| 1311 | ShVal->getType()->getUnsignedVersion(), |
| 1312 | TheAnd); |
| 1313 | ShVal = InsertNewInstBefore(new ShiftInst(Instruction::Shr, ShVal, |
| 1314 | OpRHS, Op->getName()), |
| 1315 | TheAnd); |
Chris Lattner | 70c2039 | 2004-10-27 05:57:15 +0000 | [diff] [blame] | 1316 | Value *AndRHS2 = ConstantExpr::getCast(AndRHS, ShVal->getType()); |
| 1317 | ShVal = InsertNewInstBefore(BinaryOperator::createAnd(ShVal, AndRHS2, |
| 1318 | TheAnd.getName()), |
| 1319 | TheAnd); |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1320 | return new CastInst(ShVal, Op->getType()); |
| 1321 | } |
| 1322 | } |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1323 | } |
| 1324 | break; |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1325 | } |
| 1326 | return 0; |
| 1327 | } |
| 1328 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1329 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 1330 | /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is |
| 1331 | /// true, otherwise (V < Lo || V >= Hi). In pratice, we emit the more efficient |
| 1332 | /// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. IB is the location to |
| 1333 | /// insert new instructions. |
| 1334 | Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
| 1335 | bool Inside, Instruction &IB) { |
| 1336 | assert(cast<ConstantBool>(ConstantExpr::getSetLE(Lo, Hi))->getValue() && |
| 1337 | "Lo is not <= Hi in range emission code!"); |
| 1338 | if (Inside) { |
| 1339 | if (Lo == Hi) // Trivially false. |
| 1340 | return new SetCondInst(Instruction::SetNE, V, V); |
| 1341 | if (cast<ConstantIntegral>(Lo)->isMinValue()) |
| 1342 | return new SetCondInst(Instruction::SetLT, V, Hi); |
| 1343 | |
| 1344 | Constant *AddCST = ConstantExpr::getNeg(Lo); |
| 1345 | Instruction *Add = BinaryOperator::createAdd(V, AddCST,V->getName()+".off"); |
| 1346 | InsertNewInstBefore(Add, IB); |
| 1347 | // Convert to unsigned for the comparison. |
| 1348 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 1349 | Value *OffsetVal = InsertCastBefore(Add, UnsType, IB); |
| 1350 | AddCST = ConstantExpr::getAdd(AddCST, Hi); |
| 1351 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 1352 | return new SetCondInst(Instruction::SetLT, OffsetVal, AddCST); |
| 1353 | } |
| 1354 | |
| 1355 | if (Lo == Hi) // Trivially true. |
| 1356 | return new SetCondInst(Instruction::SetEQ, V, V); |
| 1357 | |
| 1358 | Hi = SubOne(cast<ConstantInt>(Hi)); |
| 1359 | if (cast<ConstantIntegral>(Lo)->isMinValue()) // V < 0 || V >= Hi ->'V > Hi-1' |
| 1360 | return new SetCondInst(Instruction::SetGT, V, Hi); |
| 1361 | |
| 1362 | // Emit X-Lo > Hi-Lo-1 |
| 1363 | Constant *AddCST = ConstantExpr::getNeg(Lo); |
| 1364 | Instruction *Add = BinaryOperator::createAdd(V, AddCST, V->getName()+".off"); |
| 1365 | InsertNewInstBefore(Add, IB); |
| 1366 | // Convert to unsigned for the comparison. |
| 1367 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 1368 | Value *OffsetVal = InsertCastBefore(Add, UnsType, IB); |
| 1369 | AddCST = ConstantExpr::getAdd(AddCST, Hi); |
| 1370 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 1371 | return new SetCondInst(Instruction::SetGT, OffsetVal, AddCST); |
| 1372 | } |
| 1373 | |
| 1374 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1375 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1376 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1377 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1378 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1379 | if (isa<UndefValue>(Op1)) // X & undef -> 0 |
| 1380 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1381 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1382 | // and X, X = X and X, 0 == 0 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1383 | if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) |
| 1384 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1385 | |
| 1386 | // and X, -1 == X |
Chris Lattner | 49b47ae | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 1387 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1388 | if (RHS->isAllOnesValue()) |
| 1389 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1390 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1391 | // Optimize a variety of ((val OP C1) & C2) combinations... |
| 1392 | if (isa<BinaryOperator>(Op0) || isa<ShiftInst>(Op0)) { |
| 1393 | Instruction *Op0I = cast<Instruction>(Op0); |
Chris Lattner | 33217db | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 1394 | Value *X = Op0I->getOperand(0); |
Chris Lattner | 16464b3 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 1395 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1396 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, RHS, I)) |
| 1397 | return Res; |
Chris Lattner | 33217db | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 1398 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1399 | |
| 1400 | // Try to fold constant and into select arguments. |
| 1401 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 1402 | if (Instruction *R = FoldBinOpIntoSelect(I, SI, this)) |
| 1403 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1404 | if (isa<PHINode>(Op0)) |
| 1405 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1406 | return NV; |
Chris Lattner | 49b47ae | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1409 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 1410 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1411 | |
Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 1412 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
| 1413 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1414 | |
Misha Brukman | 9c003d8 | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 1415 | // (~A & ~B) == (~(A | B)) - De Morgan's Law |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1416 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1417 | Instruction *Or = BinaryOperator::createOr(Op0NotVal, Op1NotVal, |
| 1418 | I.getName()+".demorgan"); |
Chris Lattner | 49b47ae | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 1419 | InsertNewInstBefore(Or, I); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1420 | return BinaryOperator::createNot(Or); |
| 1421 | } |
| 1422 | |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 1423 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(Op1)) { |
| 1424 | // (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B) |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1425 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 1426 | return R; |
| 1427 | |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 1428 | Value *LHSVal, *RHSVal; |
| 1429 | ConstantInt *LHSCst, *RHSCst; |
| 1430 | Instruction::BinaryOps LHSCC, RHSCC; |
| 1431 | if (match(Op0, m_SetCond(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 1432 | if (match(RHS, m_SetCond(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 1433 | if (LHSVal == RHSVal && // Found (X setcc C1) & (X setcc C2) |
| 1434 | // Set[GL]E X, CST is folded to Set[GL]T elsewhere. |
| 1435 | LHSCC != Instruction::SetGE && LHSCC != Instruction::SetLE && |
| 1436 | RHSCC != Instruction::SetGE && RHSCC != Instruction::SetLE) { |
| 1437 | // Ensure that the larger constant is on the RHS. |
| 1438 | Constant *Cmp = ConstantExpr::getSetGT(LHSCst, RHSCst); |
| 1439 | SetCondInst *LHS = cast<SetCondInst>(Op0); |
| 1440 | if (cast<ConstantBool>(Cmp)->getValue()) { |
| 1441 | std::swap(LHS, RHS); |
| 1442 | std::swap(LHSCst, RHSCst); |
| 1443 | std::swap(LHSCC, RHSCC); |
| 1444 | } |
| 1445 | |
| 1446 | // At this point, we know we have have two setcc instructions |
| 1447 | // comparing a value against two constants and and'ing the result |
| 1448 | // together. Because of the above check, we know that we only have |
| 1449 | // SetEQ, SetNE, SetLT, and SetGT here. We also know (from the |
| 1450 | // FoldSetCCLogical check above), that the two constants are not |
| 1451 | // equal. |
| 1452 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 1453 | |
| 1454 | switch (LHSCC) { |
| 1455 | default: assert(0 && "Unknown integer condition code!"); |
| 1456 | case Instruction::SetEQ: |
| 1457 | switch (RHSCC) { |
| 1458 | default: assert(0 && "Unknown integer condition code!"); |
| 1459 | case Instruction::SetEQ: // (X == 13 & X == 15) -> false |
| 1460 | case Instruction::SetGT: // (X == 13 & X > 15) -> false |
| 1461 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1462 | case Instruction::SetNE: // (X == 13 & X != 15) -> X == 13 |
| 1463 | case Instruction::SetLT: // (X == 13 & X < 15) -> X == 13 |
| 1464 | return ReplaceInstUsesWith(I, LHS); |
| 1465 | } |
| 1466 | case Instruction::SetNE: |
| 1467 | switch (RHSCC) { |
| 1468 | default: assert(0 && "Unknown integer condition code!"); |
| 1469 | case Instruction::SetLT: |
| 1470 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X < 14) -> X < 13 |
| 1471 | return new SetCondInst(Instruction::SetLT, LHSVal, LHSCst); |
| 1472 | break; // (X != 13 & X < 15) -> no change |
| 1473 | case Instruction::SetEQ: // (X != 13 & X == 15) -> X == 15 |
| 1474 | case Instruction::SetGT: // (X != 13 & X > 15) -> X > 15 |
| 1475 | return ReplaceInstUsesWith(I, RHS); |
| 1476 | case Instruction::SetNE: |
| 1477 | if (LHSCst == SubOne(RHSCst)) {// (X != 13 & X != 14) -> X-13 >u 1 |
| 1478 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 1479 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 1480 | LHSVal->getName()+".off"); |
| 1481 | InsertNewInstBefore(Add, I); |
| 1482 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 1483 | Value *OffsetVal = InsertCastBefore(Add, UnsType, I); |
| 1484 | AddCST = ConstantExpr::getSub(RHSCst, LHSCst); |
| 1485 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 1486 | return new SetCondInst(Instruction::SetGT, OffsetVal, AddCST); |
| 1487 | } |
| 1488 | break; // (X != 13 & X != 15) -> no change |
| 1489 | } |
| 1490 | break; |
| 1491 | case Instruction::SetLT: |
| 1492 | switch (RHSCC) { |
| 1493 | default: assert(0 && "Unknown integer condition code!"); |
| 1494 | case Instruction::SetEQ: // (X < 13 & X == 15) -> false |
| 1495 | case Instruction::SetGT: // (X < 13 & X > 15) -> false |
| 1496 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1497 | case Instruction::SetNE: // (X < 13 & X != 15) -> X < 13 |
| 1498 | case Instruction::SetLT: // (X < 13 & X < 15) -> X < 13 |
| 1499 | return ReplaceInstUsesWith(I, LHS); |
| 1500 | } |
| 1501 | case Instruction::SetGT: |
| 1502 | switch (RHSCC) { |
| 1503 | default: assert(0 && "Unknown integer condition code!"); |
| 1504 | case Instruction::SetEQ: // (X > 13 & X == 15) -> X > 13 |
| 1505 | return ReplaceInstUsesWith(I, LHS); |
| 1506 | case Instruction::SetGT: // (X > 13 & X > 15) -> X > 15 |
| 1507 | return ReplaceInstUsesWith(I, RHS); |
| 1508 | case Instruction::SetNE: |
| 1509 | if (RHSCst == AddOne(LHSCst)) // (X > 13 & X != 14) -> X > 14 |
| 1510 | return new SetCondInst(Instruction::SetGT, LHSVal, RHSCst); |
| 1511 | break; // (X > 13 & X != 15) -> no change |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 1512 | case Instruction::SetLT: // (X > 13 & X < 15) -> (X-14) <u 1 |
| 1513 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, true, I); |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 1514 | } |
| 1515 | } |
| 1516 | } |
| 1517 | } |
| 1518 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1519 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1522 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1523 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1524 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1525 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1526 | if (isa<UndefValue>(Op1)) |
| 1527 | return ReplaceInstUsesWith(I, // X | undef -> -1 |
| 1528 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 1529 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1530 | // or X, X = X or X, 0 == X |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1531 | if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) |
| 1532 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1533 | |
| 1534 | // or X, -1 == -1 |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1535 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1536 | if (RHS->isAllOnesValue()) |
| 1537 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1538 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1539 | ConstantInt *C1; Value *X; |
| 1540 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
| 1541 | if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { |
| 1542 | std::string Op0Name = Op0->getName(); Op0->setName(""); |
| 1543 | Instruction *Or = BinaryOperator::createOr(X, RHS, Op0Name); |
| 1544 | InsertNewInstBefore(Or, I); |
| 1545 | return BinaryOperator::createAnd(Or, ConstantExpr::getOr(RHS, C1)); |
| 1546 | } |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1547 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1548 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
| 1549 | if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { |
| 1550 | std::string Op0Name = Op0->getName(); Op0->setName(""); |
| 1551 | Instruction *Or = BinaryOperator::createOr(X, RHS, Op0Name); |
| 1552 | InsertNewInstBefore(Or, I); |
| 1553 | return BinaryOperator::createXor(Or, |
| 1554 | ConstantExpr::getAnd(C1, ConstantExpr::getNot(RHS))); |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1555 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1556 | |
| 1557 | // Try to fold constant and into select arguments. |
| 1558 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 1559 | if (Instruction *R = FoldBinOpIntoSelect(I, SI, this)) |
| 1560 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1561 | if (isa<PHINode>(Op0)) |
| 1562 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1563 | return NV; |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
Chris Lattner | 812aab7 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 1566 | // (A & C1)|(A & C2) == A & (C1|C2) |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1567 | Value *A, *B; ConstantInt *C1, *C2; |
| 1568 | if (match(Op0, m_And(m_Value(A), m_ConstantInt(C1))) && |
| 1569 | match(Op1, m_And(m_Value(B), m_ConstantInt(C2))) && A == B) |
| 1570 | return BinaryOperator::createAnd(A, ConstantExpr::getOr(C1, C2)); |
Chris Lattner | 812aab7 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 1571 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1572 | if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1 |
| 1573 | if (A == Op1) // ~A | A == -1 |
| 1574 | return ReplaceInstUsesWith(I, |
| 1575 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 1576 | } else { |
| 1577 | A = 0; |
| 1578 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1579 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1580 | if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B |
| 1581 | if (Op0 == B) |
| 1582 | return ReplaceInstUsesWith(I, |
| 1583 | ConstantIntegral::getAllOnesValue(I.getType())); |
Chris Lattner | 3e327a4 | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 1584 | |
Misha Brukman | 9c003d8 | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 1585 | // (~A | ~B) == (~(A & B)) - De Morgan's Law |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1586 | if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
| 1587 | Value *And = InsertNewInstBefore(BinaryOperator::createAnd(A, B, |
| 1588 | I.getName()+".demorgan"), I); |
| 1589 | return BinaryOperator::createNot(And); |
| 1590 | } |
Chris Lattner | 3e327a4 | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 1591 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1592 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1593 | // (setcc1 A, B) | (setcc2 A, B) --> (setcc3 A, B) |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 1594 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1))) { |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1595 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 1596 | return R; |
| 1597 | |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 1598 | Value *LHSVal, *RHSVal; |
| 1599 | ConstantInt *LHSCst, *RHSCst; |
| 1600 | Instruction::BinaryOps LHSCC, RHSCC; |
| 1601 | if (match(Op0, m_SetCond(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 1602 | if (match(RHS, m_SetCond(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 1603 | if (LHSVal == RHSVal && // Found (X setcc C1) | (X setcc C2) |
| 1604 | // Set[GL]E X, CST is folded to Set[GL]T elsewhere. |
| 1605 | LHSCC != Instruction::SetGE && LHSCC != Instruction::SetLE && |
| 1606 | RHSCC != Instruction::SetGE && RHSCC != Instruction::SetLE) { |
| 1607 | // Ensure that the larger constant is on the RHS. |
| 1608 | Constant *Cmp = ConstantExpr::getSetGT(LHSCst, RHSCst); |
| 1609 | SetCondInst *LHS = cast<SetCondInst>(Op0); |
| 1610 | if (cast<ConstantBool>(Cmp)->getValue()) { |
| 1611 | std::swap(LHS, RHS); |
| 1612 | std::swap(LHSCst, RHSCst); |
| 1613 | std::swap(LHSCC, RHSCC); |
| 1614 | } |
| 1615 | |
| 1616 | // At this point, we know we have have two setcc instructions |
| 1617 | // comparing a value against two constants and or'ing the result |
| 1618 | // together. Because of the above check, we know that we only have |
| 1619 | // SetEQ, SetNE, SetLT, and SetGT here. We also know (from the |
| 1620 | // FoldSetCCLogical check above), that the two constants are not |
| 1621 | // equal. |
| 1622 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 1623 | |
| 1624 | switch (LHSCC) { |
| 1625 | default: assert(0 && "Unknown integer condition code!"); |
| 1626 | case Instruction::SetEQ: |
| 1627 | switch (RHSCC) { |
| 1628 | default: assert(0 && "Unknown integer condition code!"); |
| 1629 | case Instruction::SetEQ: |
| 1630 | if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13 <u 2 |
| 1631 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 1632 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 1633 | LHSVal->getName()+".off"); |
| 1634 | InsertNewInstBefore(Add, I); |
| 1635 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 1636 | Value *OffsetVal = InsertCastBefore(Add, UnsType, I); |
| 1637 | AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst); |
| 1638 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 1639 | return new SetCondInst(Instruction::SetLT, OffsetVal, AddCST); |
| 1640 | } |
| 1641 | break; // (X == 13 | X == 15) -> no change |
| 1642 | |
| 1643 | case Instruction::SetGT: |
| 1644 | if (LHSCst == SubOne(RHSCst)) // (X == 13 | X > 14) -> X > 13 |
| 1645 | return new SetCondInst(Instruction::SetGT, LHSVal, LHSCst); |
| 1646 | break; // (X == 13 | X > 15) -> no change |
| 1647 | case Instruction::SetNE: // (X == 13 | X != 15) -> X != 15 |
| 1648 | case Instruction::SetLT: // (X == 13 | X < 15) -> X < 15 |
| 1649 | return ReplaceInstUsesWith(I, RHS); |
| 1650 | } |
| 1651 | break; |
| 1652 | case Instruction::SetNE: |
| 1653 | switch (RHSCC) { |
| 1654 | default: assert(0 && "Unknown integer condition code!"); |
| 1655 | case Instruction::SetLT: // (X != 13 | X < 15) -> X < 15 |
| 1656 | return ReplaceInstUsesWith(I, RHS); |
| 1657 | case Instruction::SetEQ: // (X != 13 | X == 15) -> X != 13 |
| 1658 | case Instruction::SetGT: // (X != 13 | X > 15) -> X != 13 |
| 1659 | return ReplaceInstUsesWith(I, LHS); |
| 1660 | case Instruction::SetNE: // (X != 13 | X != 15) -> true |
| 1661 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 1662 | } |
| 1663 | break; |
| 1664 | case Instruction::SetLT: |
| 1665 | switch (RHSCC) { |
| 1666 | default: assert(0 && "Unknown integer condition code!"); |
| 1667 | case Instruction::SetEQ: // (X < 13 | X == 14) -> no change |
| 1668 | break; |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 1669 | case Instruction::SetGT: // (X < 13 | X > 15) -> (X-13) > 2 |
| 1670 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false, I); |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 1671 | case Instruction::SetNE: // (X < 13 | X != 15) -> X != 15 |
| 1672 | case Instruction::SetLT: // (X < 13 | X < 15) -> X < 15 |
| 1673 | return ReplaceInstUsesWith(I, RHS); |
| 1674 | } |
| 1675 | break; |
| 1676 | case Instruction::SetGT: |
| 1677 | switch (RHSCC) { |
| 1678 | default: assert(0 && "Unknown integer condition code!"); |
| 1679 | case Instruction::SetEQ: // (X > 13 | X == 15) -> X > 13 |
| 1680 | case Instruction::SetGT: // (X > 13 | X > 15) -> X > 13 |
| 1681 | return ReplaceInstUsesWith(I, LHS); |
| 1682 | case Instruction::SetNE: // (X > 13 | X != 15) -> true |
| 1683 | case Instruction::SetLT: // (X > 13 | X < 15) -> true |
| 1684 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 1685 | } |
| 1686 | } |
| 1687 | } |
| 1688 | } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1689 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 1692 | // XorSelf - Implements: X ^ X --> 0 |
| 1693 | struct XorSelf { |
| 1694 | Value *RHS; |
| 1695 | XorSelf(Value *rhs) : RHS(rhs) {} |
| 1696 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 1697 | Instruction *apply(BinaryOperator &Xor) const { |
| 1698 | return &Xor; |
| 1699 | } |
| 1700 | }; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1701 | |
| 1702 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1703 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1704 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1705 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1706 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1707 | if (isa<UndefValue>(Op1)) |
| 1708 | return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef |
| 1709 | |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 1710 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
| 1711 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { |
| 1712 | assert(Result == &I && "AssociativeOpt didn't work?"); |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1713 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 1714 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1715 | |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1716 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1717 | // xor X, 0 == X |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1718 | if (RHS->isNullValue()) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1719 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1720 | |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1721 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 1722 | // xor (setcc A, B), true = not (setcc A, B) = setncc A, B |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1723 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(Op0I)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1724 | if (RHS == ConstantBool::True && SCI->hasOneUse()) |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 1725 | return new SetCondInst(SCI->getInverseCondition(), |
| 1726 | SCI->getOperand(0), SCI->getOperand(1)); |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 1727 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 1728 | // ~(c-X) == X-c-1 == X+(-c-1) |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1729 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
| 1730 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1731 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); |
| 1732 | Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1733 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1734 | return BinaryOperator::createAdd(Op0I->getOperand(1), ConstantRHS); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1735 | } |
Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 1736 | |
| 1737 | // ~(~X & Y) --> (X | ~Y) |
| 1738 | if (Op0I->getOpcode() == Instruction::And && RHS->isAllOnesValue()) { |
| 1739 | if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands(); |
| 1740 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { |
| 1741 | Instruction *NotY = |
| 1742 | BinaryOperator::createNot(Op0I->getOperand(1), |
| 1743 | Op0I->getOperand(1)->getName()+".not"); |
| 1744 | InsertNewInstBefore(NotY, I); |
| 1745 | return BinaryOperator::createOr(Op0NotVal, NotY); |
| 1746 | } |
| 1747 | } |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1748 | |
| 1749 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 1750 | switch (Op0I->getOpcode()) { |
| 1751 | case Instruction::Add: |
Chris Lattner | 0f68fa6 | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 1752 | // ~(X-c) --> (-c-1)-X |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1753 | if (RHS->isAllOnesValue()) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1754 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); |
| 1755 | return BinaryOperator::createSub( |
| 1756 | ConstantExpr::getSub(NegOp0CI, |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1757 | ConstantInt::get(I.getType(), 1)), |
Chris Lattner | 0f68fa6 | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 1758 | Op0I->getOperand(0)); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1759 | } |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 1760 | break; |
| 1761 | case Instruction::And: |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1762 | // (X & C1) ^ C2 --> (X & C1) | C2 iff (C1&C2) == 0 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1763 | if (ConstantExpr::getAnd(RHS, Op0CI)->isNullValue()) |
| 1764 | return BinaryOperator::createOr(Op0, RHS); |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 1765 | break; |
| 1766 | case Instruction::Or: |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1767 | // (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1768 | if (ConstantExpr::getAnd(RHS, Op0CI) == RHS) |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 1769 | return BinaryOperator::createAnd(Op0, ConstantExpr::getNot(RHS)); |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 1770 | break; |
| 1771 | default: break; |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1772 | } |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 1773 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1774 | |
| 1775 | // Try to fold constant and into select arguments. |
| 1776 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 1777 | if (Instruction *R = FoldBinOpIntoSelect(I, SI, this)) |
| 1778 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1779 | if (isa<PHINode>(Op0)) |
| 1780 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1781 | return NV; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1784 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1785 | if (X == Op1) |
| 1786 | return ReplaceInstUsesWith(I, |
| 1787 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 1788 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1789 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1790 | if (X == Op0) |
| 1791 | return ReplaceInstUsesWith(I, |
| 1792 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 1793 | |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1794 | if (Instruction *Op1I = dyn_cast<Instruction>(Op1)) |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 1795 | if (Op1I->getOpcode() == Instruction::Or) { |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1796 | if (Op1I->getOperand(0) == Op0) { // B^(B|A) == (A|B)^B |
| 1797 | cast<BinaryOperator>(Op1I)->swapOperands(); |
| 1798 | I.swapOperands(); |
| 1799 | std::swap(Op0, Op1); |
| 1800 | } else if (Op1I->getOperand(1) == Op0) { // B^(A|B) == (A|B)^B |
| 1801 | I.swapOperands(); |
| 1802 | std::swap(Op0, Op1); |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 1803 | } |
| 1804 | } else if (Op1I->getOpcode() == Instruction::Xor) { |
| 1805 | if (Op0 == Op1I->getOperand(0)) // A^(A^B) == B |
| 1806 | return ReplaceInstUsesWith(I, Op1I->getOperand(1)); |
| 1807 | else if (Op0 == Op1I->getOperand(1)) // A^(B^A) == B |
| 1808 | return ReplaceInstUsesWith(I, Op1I->getOperand(0)); |
| 1809 | } |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1810 | |
| 1811 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1812 | if (Op0I->getOpcode() == Instruction::Or && Op0I->hasOneUse()) { |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1813 | if (Op0I->getOperand(0) == Op1) // (B|A)^B == (A|B)^B |
| 1814 | cast<BinaryOperator>(Op0I)->swapOperands(); |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1815 | if (Op0I->getOperand(1) == Op1) { // (A|B)^B == A & ~B |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 1816 | Value *NotB = InsertNewInstBefore(BinaryOperator::createNot(Op1, |
| 1817 | Op1->getName()+".not"), I); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1818 | return BinaryOperator::createAnd(Op0I->getOperand(0), NotB); |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1819 | } |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 1820 | } else if (Op0I->getOpcode() == Instruction::Xor) { |
| 1821 | if (Op1 == Op0I->getOperand(0)) // (A^B)^A == B |
| 1822 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 1823 | else if (Op1 == Op0I->getOperand(1)) // (B^A)^A == B |
| 1824 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
Chris Lattner | 7aa2d47 | 2004-08-01 19:42:59 +0000 | [diff] [blame] | 1827 | // (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1828 | Value *A, *B; ConstantInt *C1, *C2; |
| 1829 | if (match(Op0, m_And(m_Value(A), m_ConstantInt(C1))) && |
| 1830 | match(Op1, m_And(m_Value(B), m_ConstantInt(C2))) && |
Chris Lattner | 7aa2d47 | 2004-08-01 19:42:59 +0000 | [diff] [blame] | 1831 | ConstantExpr::getAnd(C1, C2)->isNullValue()) |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1832 | return BinaryOperator::createOr(Op0, Op1); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 1833 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1834 | // (setcc1 A, B) ^ (setcc2 A, B) --> (setcc3 A, B) |
| 1835 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1))) |
| 1836 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 1837 | return R; |
| 1838 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1839 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 1842 | /// MulWithOverflow - Compute Result = In1*In2, returning true if the result |
| 1843 | /// overflowed for this type. |
| 1844 | static bool MulWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
| 1845 | ConstantInt *In2) { |
| 1846 | Result = cast<ConstantInt>(ConstantExpr::getMul(In1, In2)); |
| 1847 | return !In2->isNullValue() && ConstantExpr::getDiv(Result, In2) != In1; |
| 1848 | } |
| 1849 | |
| 1850 | static bool isPositive(ConstantInt *C) { |
| 1851 | return cast<ConstantSInt>(C)->getValue() >= 0; |
| 1852 | } |
| 1853 | |
| 1854 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result |
| 1855 | /// overflowed for this type. |
| 1856 | static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
| 1857 | ConstantInt *In2) { |
| 1858 | Result = cast<ConstantInt>(ConstantExpr::getAdd(In1, In2)); |
| 1859 | |
| 1860 | if (In1->getType()->isUnsigned()) |
| 1861 | return cast<ConstantUInt>(Result)->getValue() < |
| 1862 | cast<ConstantUInt>(In1)->getValue(); |
| 1863 | if (isPositive(In1) != isPositive(In2)) |
| 1864 | return false; |
| 1865 | if (isPositive(In1)) |
| 1866 | return cast<ConstantSInt>(Result)->getValue() < |
| 1867 | cast<ConstantSInt>(In1)->getValue(); |
| 1868 | return cast<ConstantSInt>(Result)->getValue() > |
| 1869 | cast<ConstantSInt>(In1)->getValue(); |
| 1870 | } |
| 1871 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1872 | Instruction *InstCombiner::visitSetCondInst(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1873 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1874 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1875 | const Type *Ty = Op0->getType(); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1876 | |
| 1877 | // setcc X, X |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1878 | if (Op0 == Op1) |
| 1879 | return ReplaceInstUsesWith(I, ConstantBool::get(isTrueWhenEqual(I))); |
Chris Lattner | 1fc23f3 | 2002-05-09 20:11:54 +0000 | [diff] [blame] | 1880 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1881 | if (isa<UndefValue>(Op1)) // X setcc undef -> undef |
| 1882 | return ReplaceInstUsesWith(I, UndefValue::get(Type::BoolTy)); |
| 1883 | |
Chris Lattner | 15ff1e1 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 1884 | // setcc <global/alloca*/null>, <global/alloca*/null> - Global/Stack value |
| 1885 | // addresses never equal each other! We already know that Op0 != Op1. |
| 1886 | if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) || |
| 1887 | isa<ConstantPointerNull>(Op0)) && |
| 1888 | (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) || |
| 1889 | isa<ConstantPointerNull>(Op1))) |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1890 | return ReplaceInstUsesWith(I, ConstantBool::get(!isTrueWhenEqual(I))); |
| 1891 | |
| 1892 | // setcc's with boolean values can always be turned into bitwise operations |
| 1893 | if (Ty == Type::BoolTy) { |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 1894 | switch (I.getOpcode()) { |
| 1895 | default: assert(0 && "Invalid setcc instruction!"); |
| 1896 | case Instruction::SetEQ: { // seteq bool %A, %B -> ~(A^B) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1897 | Instruction *Xor = BinaryOperator::createXor(Op0, Op1, I.getName()+"tmp"); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1898 | InsertNewInstBefore(Xor, I); |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 1899 | return BinaryOperator::createNot(Xor); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1900 | } |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 1901 | case Instruction::SetNE: |
| 1902 | return BinaryOperator::createXor(Op0, Op1); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1903 | |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 1904 | case Instruction::SetGT: |
| 1905 | std::swap(Op0, Op1); // Change setgt -> setlt |
| 1906 | // FALL THROUGH |
| 1907 | case Instruction::SetLT: { // setlt bool A, B -> ~X & Y |
| 1908 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 1909 | InsertNewInstBefore(Not, I); |
| 1910 | return BinaryOperator::createAnd(Not, Op1); |
| 1911 | } |
| 1912 | case Instruction::SetGE: |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1913 | std::swap(Op0, Op1); // Change setge -> setle |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 1914 | // FALL THROUGH |
| 1915 | case Instruction::SetLE: { // setle bool %A, %B -> ~A | B |
| 1916 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 1917 | InsertNewInstBefore(Not, I); |
| 1918 | return BinaryOperator::createOr(Not, Op1); |
| 1919 | } |
| 1920 | } |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
Chris Lattner | 2dd0174 | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 1923 | // See if we are doing a comparison between a constant and an instruction that |
| 1924 | // can be folded into the comparison. |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1925 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 1926 | // Check to see if we are comparing against the minimum or maximum value... |
| 1927 | if (CI->isMinValue()) { |
| 1928 | if (I.getOpcode() == Instruction::SetLT) // A < MIN -> FALSE |
| 1929 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1930 | if (I.getOpcode() == Instruction::SetGE) // A >= MIN -> TRUE |
| 1931 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 1932 | if (I.getOpcode() == Instruction::SetLE) // A <= MIN -> A == MIN |
| 1933 | return BinaryOperator::createSetEQ(Op0, Op1); |
| 1934 | if (I.getOpcode() == Instruction::SetGT) // A > MIN -> A != MIN |
| 1935 | return BinaryOperator::createSetNE(Op0, Op1); |
| 1936 | |
| 1937 | } else if (CI->isMaxValue()) { |
| 1938 | if (I.getOpcode() == Instruction::SetGT) // A > MAX -> FALSE |
| 1939 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1940 | if (I.getOpcode() == Instruction::SetLE) // A <= MAX -> TRUE |
| 1941 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 1942 | if (I.getOpcode() == Instruction::SetGE) // A >= MAX -> A == MAX |
| 1943 | return BinaryOperator::createSetEQ(Op0, Op1); |
| 1944 | if (I.getOpcode() == Instruction::SetLT) // A < MAX -> A != MAX |
| 1945 | return BinaryOperator::createSetNE(Op0, Op1); |
| 1946 | |
| 1947 | // Comparing against a value really close to min or max? |
| 1948 | } else if (isMinValuePlusOne(CI)) { |
| 1949 | if (I.getOpcode() == Instruction::SetLT) // A < MIN+1 -> A == MIN |
| 1950 | return BinaryOperator::createSetEQ(Op0, SubOne(CI)); |
| 1951 | if (I.getOpcode() == Instruction::SetGE) // A >= MIN-1 -> A != MIN |
| 1952 | return BinaryOperator::createSetNE(Op0, SubOne(CI)); |
| 1953 | |
| 1954 | } else if (isMaxValueMinusOne(CI)) { |
| 1955 | if (I.getOpcode() == Instruction::SetGT) // A > MAX-1 -> A == MAX |
| 1956 | return BinaryOperator::createSetEQ(Op0, AddOne(CI)); |
| 1957 | if (I.getOpcode() == Instruction::SetLE) // A <= MAX-1 -> A != MAX |
| 1958 | return BinaryOperator::createSetNE(Op0, AddOne(CI)); |
| 1959 | } |
| 1960 | |
| 1961 | // If we still have a setle or setge instruction, turn it into the |
| 1962 | // appropriate setlt or setgt instruction. Since the border cases have |
| 1963 | // already been handled above, this requires little checking. |
| 1964 | // |
| 1965 | if (I.getOpcode() == Instruction::SetLE) |
| 1966 | return BinaryOperator::createSetLT(Op0, AddOne(CI)); |
| 1967 | if (I.getOpcode() == Instruction::SetGE) |
| 1968 | return BinaryOperator::createSetGT(Op0, SubOne(CI)); |
| 1969 | |
Chris Lattner | e1e10e1 | 2004-05-25 06:32:08 +0000 | [diff] [blame] | 1970 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 1971 | switch (LHSI->getOpcode()) { |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1972 | case Instruction::PHI: |
| 1973 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1974 | return NV; |
| 1975 | break; |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 1976 | case Instruction::And: |
| 1977 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && |
| 1978 | LHSI->getOperand(0)->hasOneUse()) { |
| 1979 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare |
| 1980 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This |
| 1981 | // happens a LOT in code produced by the C front-end, for bitfield |
| 1982 | // access. |
| 1983 | ShiftInst *Shift = dyn_cast<ShiftInst>(LHSI->getOperand(0)); |
| 1984 | ConstantUInt *ShAmt; |
| 1985 | ShAmt = Shift ? dyn_cast<ConstantUInt>(Shift->getOperand(1)) : 0; |
| 1986 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); |
| 1987 | const Type *Ty = LHSI->getType(); |
| 1988 | |
| 1989 | // We can fold this as long as we can't shift unknown bits |
| 1990 | // into the mask. This can only happen with signed shift |
| 1991 | // rights, as they sign-extend. |
| 1992 | if (ShAmt) { |
| 1993 | bool CanFold = Shift->getOpcode() != Instruction::Shr || |
Chris Lattner | 6afc02f | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 1994 | Shift->getType()->isUnsigned(); |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 1995 | if (!CanFold) { |
| 1996 | // To test for the bad case of the signed shr, see if any |
| 1997 | // of the bits shifted in could be tested after the mask. |
| 1998 | Constant *OShAmt = ConstantUInt::get(Type::UByteTy, |
Chris Lattner | d8f5e2c | 2004-07-21 20:14:10 +0000 | [diff] [blame] | 1999 | Ty->getPrimitiveSize()*8-ShAmt->getValue()); |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2000 | Constant *ShVal = |
| 2001 | ConstantExpr::getShl(ConstantInt::getAllOnesValue(Ty), OShAmt); |
| 2002 | if (ConstantExpr::getAnd(ShVal, AndCST)->isNullValue()) |
| 2003 | CanFold = true; |
| 2004 | } |
| 2005 | |
| 2006 | if (CanFold) { |
Chris Lattner | 6afc02f | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 2007 | Constant *NewCst; |
| 2008 | if (Shift->getOpcode() == Instruction::Shl) |
| 2009 | NewCst = ConstantExpr::getUShr(CI, ShAmt); |
| 2010 | else |
| 2011 | NewCst = ConstantExpr::getShl(CI, ShAmt); |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2012 | |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2013 | // Check to see if we are shifting out any of the bits being |
| 2014 | // compared. |
| 2015 | if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != CI){ |
| 2016 | // If we shifted bits out, the fold is not going to work out. |
| 2017 | // As a special case, check to see if this means that the |
| 2018 | // result is always true or false now. |
| 2019 | if (I.getOpcode() == Instruction::SetEQ) |
| 2020 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2021 | if (I.getOpcode() == Instruction::SetNE) |
| 2022 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2023 | } else { |
| 2024 | I.setOperand(1, NewCst); |
Chris Lattner | 6afc02f | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 2025 | Constant *NewAndCST; |
| 2026 | if (Shift->getOpcode() == Instruction::Shl) |
| 2027 | NewAndCST = ConstantExpr::getUShr(AndCST, ShAmt); |
| 2028 | else |
| 2029 | NewAndCST = ConstantExpr::getShl(AndCST, ShAmt); |
| 2030 | LHSI->setOperand(1, NewAndCST); |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2031 | LHSI->setOperand(0, Shift->getOperand(0)); |
| 2032 | WorkList.push_back(Shift); // Shift is dead. |
| 2033 | AddUsesToWorkList(I); |
| 2034 | return &I; |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 2035 | } |
| 2036 | } |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2037 | } |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2038 | } |
| 2039 | break; |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2040 | |
Reid Spencer | 279fa25 | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 2041 | // (setcc (cast X to larger), CI) |
| 2042 | case Instruction::Cast: { |
| 2043 | Instruction* replacement = |
| 2044 | visitSetCondInstWithCastAndConstant(I,cast<CastInst>(LHSI),CI); |
| 2045 | if (replacement) |
| 2046 | return replacement; |
Chris Lattner | be7a69e | 2004-09-29 03:09:18 +0000 | [diff] [blame] | 2047 | break; |
| 2048 | } |
Reid Spencer | 279fa25 | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 2049 | |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2050 | case Instruction::Shl: // (setcc (shl X, ShAmt), CI) |
| 2051 | if (ConstantUInt *ShAmt = dyn_cast<ConstantUInt>(LHSI->getOperand(1))) { |
| 2052 | switch (I.getOpcode()) { |
| 2053 | default: break; |
| 2054 | case Instruction::SetEQ: |
| 2055 | case Instruction::SetNE: { |
| 2056 | // If we are comparing against bits always shifted out, the |
| 2057 | // comparison cannot succeed. |
| 2058 | Constant *Comp = |
| 2059 | ConstantExpr::getShl(ConstantExpr::getShr(CI, ShAmt), ShAmt); |
| 2060 | if (Comp != CI) {// Comparing against a bit that we know is zero. |
| 2061 | bool IsSetNE = I.getOpcode() == Instruction::SetNE; |
| 2062 | Constant *Cst = ConstantBool::get(IsSetNE); |
| 2063 | return ReplaceInstUsesWith(I, Cst); |
| 2064 | } |
| 2065 | |
| 2066 | if (LHSI->hasOneUse()) { |
| 2067 | // Otherwise strength reduce the shift into an and. |
| 2068 | unsigned ShAmtVal = ShAmt->getValue(); |
| 2069 | unsigned TypeBits = CI->getType()->getPrimitiveSize()*8; |
| 2070 | uint64_t Val = (1ULL << (TypeBits-ShAmtVal))-1; |
| 2071 | |
| 2072 | Constant *Mask; |
| 2073 | if (CI->getType()->isUnsigned()) { |
| 2074 | Mask = ConstantUInt::get(CI->getType(), Val); |
| 2075 | } else if (ShAmtVal != 0) { |
| 2076 | Mask = ConstantSInt::get(CI->getType(), Val); |
| 2077 | } else { |
| 2078 | Mask = ConstantInt::getAllOnesValue(CI->getType()); |
| 2079 | } |
| 2080 | |
| 2081 | Instruction *AndI = |
| 2082 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 2083 | Mask, LHSI->getName()+".mask"); |
| 2084 | Value *And = InsertNewInstBefore(AndI, I); |
| 2085 | return new SetCondInst(I.getOpcode(), And, |
| 2086 | ConstantExpr::getUShr(CI, ShAmt)); |
| 2087 | } |
| 2088 | } |
| 2089 | } |
| 2090 | } |
| 2091 | break; |
| 2092 | |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2093 | case Instruction::Shr: // (setcc (shr X, ShAmt), CI) |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2094 | if (ConstantUInt *ShAmt = dyn_cast<ConstantUInt>(LHSI->getOperand(1))) { |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2095 | switch (I.getOpcode()) { |
| 2096 | default: break; |
| 2097 | case Instruction::SetEQ: |
| 2098 | case Instruction::SetNE: { |
| 2099 | // If we are comparing against bits always shifted out, the |
| 2100 | // comparison cannot succeed. |
| 2101 | Constant *Comp = |
| 2102 | ConstantExpr::getShr(ConstantExpr::getShl(CI, ShAmt), ShAmt); |
| 2103 | |
| 2104 | if (Comp != CI) {// Comparing against a bit that we know is zero. |
| 2105 | bool IsSetNE = I.getOpcode() == Instruction::SetNE; |
| 2106 | Constant *Cst = ConstantBool::get(IsSetNE); |
| 2107 | return ReplaceInstUsesWith(I, Cst); |
| 2108 | } |
| 2109 | |
| 2110 | if (LHSI->hasOneUse() || CI->isNullValue()) { |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2111 | unsigned ShAmtVal = ShAmt->getValue(); |
| 2112 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2113 | // Otherwise strength reduce the shift into an and. |
| 2114 | uint64_t Val = ~0ULL; // All ones. |
| 2115 | Val <<= ShAmtVal; // Shift over to the right spot. |
| 2116 | |
| 2117 | Constant *Mask; |
| 2118 | if (CI->getType()->isUnsigned()) { |
| 2119 | unsigned TypeBits = CI->getType()->getPrimitiveSize()*8; |
| 2120 | Val &= (1ULL << TypeBits)-1; |
| 2121 | Mask = ConstantUInt::get(CI->getType(), Val); |
| 2122 | } else { |
| 2123 | Mask = ConstantSInt::get(CI->getType(), Val); |
| 2124 | } |
| 2125 | |
| 2126 | Instruction *AndI = |
| 2127 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 2128 | Mask, LHSI->getName()+".mask"); |
| 2129 | Value *And = InsertNewInstBefore(AndI, I); |
| 2130 | return new SetCondInst(I.getOpcode(), And, |
| 2131 | ConstantExpr::getShl(CI, ShAmt)); |
| 2132 | } |
| 2133 | break; |
| 2134 | } |
| 2135 | } |
| 2136 | } |
| 2137 | break; |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2138 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2139 | case Instruction::Div: |
| 2140 | // Fold: (div X, C1) op C2 -> range check |
| 2141 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
| 2142 | // Fold this div into the comparison, producing a range check. |
| 2143 | // Determine, based on the divide type, what the range is being |
| 2144 | // checked. If there is an overflow on the low or high side, remember |
| 2145 | // it, otherwise compute the range [low, hi) bounding the new value. |
| 2146 | bool LoOverflow = false, HiOverflow = 0; |
| 2147 | ConstantInt *LoBound = 0, *HiBound = 0; |
| 2148 | |
| 2149 | ConstantInt *Prod; |
| 2150 | bool ProdOV = MulWithOverflow(Prod, CI, DivRHS); |
| 2151 | |
Chris Lattner | a92af96 | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 2152 | Instruction::BinaryOps Opcode = I.getOpcode(); |
| 2153 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2154 | if (DivRHS->isNullValue()) { // Don't hack on divide by zeros. |
| 2155 | } else if (LHSI->getType()->isUnsigned()) { // udiv |
| 2156 | LoBound = Prod; |
| 2157 | LoOverflow = ProdOV; |
| 2158 | HiOverflow = ProdOV || AddWithOverflow(HiBound, LoBound, DivRHS); |
| 2159 | } else if (isPositive(DivRHS)) { // Divisor is > 0. |
| 2160 | if (CI->isNullValue()) { // (X / pos) op 0 |
| 2161 | // Can't overflow. |
| 2162 | LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS))); |
| 2163 | HiBound = DivRHS; |
| 2164 | } else if (isPositive(CI)) { // (X / pos) op pos |
| 2165 | LoBound = Prod; |
| 2166 | LoOverflow = ProdOV; |
| 2167 | HiOverflow = ProdOV || AddWithOverflow(HiBound, Prod, DivRHS); |
| 2168 | } else { // (X / pos) op neg |
| 2169 | Constant *DivRHSH = ConstantExpr::getNeg(SubOne(DivRHS)); |
| 2170 | LoOverflow = AddWithOverflow(LoBound, Prod, |
| 2171 | cast<ConstantInt>(DivRHSH)); |
| 2172 | HiBound = Prod; |
| 2173 | HiOverflow = ProdOV; |
| 2174 | } |
| 2175 | } else { // Divisor is < 0. |
| 2176 | if (CI->isNullValue()) { // (X / neg) op 0 |
| 2177 | LoBound = AddOne(DivRHS); |
| 2178 | HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS)); |
| 2179 | } else if (isPositive(CI)) { // (X / neg) op pos |
| 2180 | HiOverflow = LoOverflow = ProdOV; |
| 2181 | if (!LoOverflow) |
| 2182 | LoOverflow = AddWithOverflow(LoBound, Prod, AddOne(DivRHS)); |
| 2183 | HiBound = AddOne(Prod); |
| 2184 | } else { // (X / neg) op neg |
| 2185 | LoBound = Prod; |
| 2186 | LoOverflow = HiOverflow = ProdOV; |
| 2187 | HiBound = cast<ConstantInt>(ConstantExpr::getSub(Prod, DivRHS)); |
| 2188 | } |
Chris Lattner | 0b41e86 | 2004-10-08 19:15:44 +0000 | [diff] [blame] | 2189 | |
Chris Lattner | a92af96 | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 2190 | // Dividing by a negate swaps the condition. |
| 2191 | Opcode = SetCondInst::getSwappedCondition(Opcode); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | if (LoBound) { |
| 2195 | Value *X = LHSI->getOperand(0); |
Chris Lattner | a92af96 | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 2196 | switch (Opcode) { |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2197 | default: assert(0 && "Unhandled setcc opcode!"); |
| 2198 | case Instruction::SetEQ: |
| 2199 | if (LoOverflow && HiOverflow) |
| 2200 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2201 | else if (HiOverflow) |
| 2202 | return new SetCondInst(Instruction::SetGE, X, LoBound); |
| 2203 | else if (LoOverflow) |
| 2204 | return new SetCondInst(Instruction::SetLT, X, HiBound); |
| 2205 | else |
| 2206 | return InsertRangeTest(X, LoBound, HiBound, true, I); |
| 2207 | case Instruction::SetNE: |
| 2208 | if (LoOverflow && HiOverflow) |
| 2209 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2210 | else if (HiOverflow) |
| 2211 | return new SetCondInst(Instruction::SetLT, X, LoBound); |
| 2212 | else if (LoOverflow) |
| 2213 | return new SetCondInst(Instruction::SetGE, X, HiBound); |
| 2214 | else |
| 2215 | return InsertRangeTest(X, LoBound, HiBound, false, I); |
| 2216 | case Instruction::SetLT: |
| 2217 | if (LoOverflow) |
| 2218 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2219 | return new SetCondInst(Instruction::SetLT, X, LoBound); |
| 2220 | case Instruction::SetGT: |
| 2221 | if (HiOverflow) |
| 2222 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2223 | return new SetCondInst(Instruction::SetGE, X, HiBound); |
| 2224 | } |
| 2225 | } |
| 2226 | } |
| 2227 | break; |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2228 | case Instruction::Select: |
| 2229 | // If either operand of the select is a constant, we can fold the |
| 2230 | // comparison into the select arms, which will cause one to be |
| 2231 | // constant folded and the select turned into a bitwise or. |
| 2232 | Value *Op1 = 0, *Op2 = 0; |
| 2233 | if (LHSI->hasOneUse()) { |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2234 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
Chris Lattner | 2dd0174 | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 2235 | // Fold the known value into the constant operand. |
| 2236 | Op1 = ConstantExpr::get(I.getOpcode(), C, CI); |
| 2237 | // Insert a new SetCC of the other select operand. |
| 2238 | Op2 = InsertNewInstBefore(new SetCondInst(I.getOpcode(), |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2239 | LHSI->getOperand(2), CI, |
Chris Lattner | 2dd0174 | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 2240 | I.getName()), I); |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2241 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
Chris Lattner | 2dd0174 | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 2242 | // Fold the known value into the constant operand. |
| 2243 | Op2 = ConstantExpr::get(I.getOpcode(), C, CI); |
| 2244 | // Insert a new SetCC of the other select operand. |
| 2245 | Op1 = InsertNewInstBefore(new SetCondInst(I.getOpcode(), |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2246 | LHSI->getOperand(1), CI, |
Chris Lattner | 2dd0174 | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 2247 | I.getName()), I); |
| 2248 | } |
Chris Lattner | 2dd0174 | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 2249 | } |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2250 | |
| 2251 | if (Op1) |
| 2252 | return new SelectInst(LHSI->getOperand(0), Op1, Op2); |
| 2253 | break; |
| 2254 | } |
| 2255 | |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 2256 | // Simplify seteq and setne instructions... |
| 2257 | if (I.getOpcode() == Instruction::SetEQ || |
| 2258 | I.getOpcode() == Instruction::SetNE) { |
| 2259 | bool isSetNE = I.getOpcode() == Instruction::SetNE; |
| 2260 | |
Chris Lattner | cfbce7c | 2003-07-23 17:26:36 +0000 | [diff] [blame] | 2261 | // 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] | 2262 | // operand is a constant, simplify a bit. |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2263 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) { |
| 2264 | switch (BO->getOpcode()) { |
Chris Lattner | 23b47b6 | 2004-07-06 07:38:18 +0000 | [diff] [blame] | 2265 | case Instruction::Rem: |
| 2266 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. |
| 2267 | if (CI->isNullValue() && isa<ConstantSInt>(BO->getOperand(1)) && |
| 2268 | BO->hasOneUse() && |
| 2269 | cast<ConstantSInt>(BO->getOperand(1))->getValue() > 1) |
| 2270 | if (unsigned L2 = |
| 2271 | Log2(cast<ConstantSInt>(BO->getOperand(1))->getValue())) { |
| 2272 | const Type *UTy = BO->getType()->getUnsignedVersion(); |
| 2273 | Value *NewX = InsertNewInstBefore(new CastInst(BO->getOperand(0), |
| 2274 | UTy, "tmp"), I); |
| 2275 | Constant *RHSCst = ConstantUInt::get(UTy, 1ULL << L2); |
| 2276 | Value *NewRem =InsertNewInstBefore(BinaryOperator::createRem(NewX, |
| 2277 | RHSCst, BO->getName()), I); |
| 2278 | return BinaryOperator::create(I.getOpcode(), NewRem, |
| 2279 | Constant::getNullValue(UTy)); |
| 2280 | } |
| 2281 | break; |
| 2282 | |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2283 | case Instruction::Add: |
Chris Lattner | 6e07936 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 2284 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. |
| 2285 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | b121ae1 | 2004-09-21 21:35:23 +0000 | [diff] [blame] | 2286 | if (BO->hasOneUse()) |
| 2287 | return new SetCondInst(I.getOpcode(), BO->getOperand(0), |
| 2288 | ConstantExpr::getSub(CI, BOp1C)); |
Chris Lattner | 6e07936 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 2289 | } else if (CI->isNullValue()) { |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2290 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 2291 | // efficiently invertible, or if the add has just this one use. |
| 2292 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
Chris Lattner | 6e07936 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 2293 | |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2294 | if (Value *NegVal = dyn_castNegVal(BOp1)) |
| 2295 | return new SetCondInst(I.getOpcode(), BOp0, NegVal); |
| 2296 | else if (Value *NegVal = dyn_castNegVal(BOp0)) |
| 2297 | return new SetCondInst(I.getOpcode(), NegVal, BOp1); |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2298 | else if (BO->hasOneUse()) { |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2299 | Instruction *Neg = BinaryOperator::createNeg(BOp1, BO->getName()); |
| 2300 | BO->setName(""); |
| 2301 | InsertNewInstBefore(Neg, I); |
| 2302 | return new SetCondInst(I.getOpcode(), BOp0, Neg); |
| 2303 | } |
| 2304 | } |
| 2305 | break; |
| 2306 | case Instruction::Xor: |
| 2307 | // For the xor case, we can xor two constants together, eliminating |
| 2308 | // the explicit xor. |
| 2309 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) |
| 2310 | return BinaryOperator::create(I.getOpcode(), BO->getOperand(0), |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2311 | ConstantExpr::getXor(CI, BOC)); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2312 | |
| 2313 | // FALLTHROUGH |
| 2314 | case Instruction::Sub: |
| 2315 | // Replace (([sub|xor] A, B) != 0) with (A != B) |
| 2316 | if (CI->isNullValue()) |
| 2317 | return new SetCondInst(I.getOpcode(), BO->getOperand(0), |
| 2318 | BO->getOperand(1)); |
| 2319 | break; |
| 2320 | |
| 2321 | case Instruction::Or: |
| 2322 | // If bits are being or'd in that are not present in the constant we |
| 2323 | // are comparing against, then the comparison could never succeed! |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2324 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 2325 | Constant *NotCI = ConstantExpr::getNot(CI); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2326 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 2327 | return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE)); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2328 | } |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2329 | break; |
| 2330 | |
| 2331 | case Instruction::And: |
| 2332 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 2333 | // If bits are being compared against that are and'd out, then the |
| 2334 | // comparison can never succeed! |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 2335 | if (!ConstantExpr::getAnd(CI, |
| 2336 | ConstantExpr::getNot(BOC))->isNullValue()) |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 2337 | return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE)); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2338 | |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2339 | // If we have ((X & C) == C), turn it into ((X & C) != 0). |
Chris Lattner | ee59d4b | 2004-06-10 02:33:20 +0000 | [diff] [blame] | 2340 | if (CI == BOC && isOneBitSet(CI)) |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2341 | return new SetCondInst(isSetNE ? Instruction::SetEQ : |
| 2342 | Instruction::SetNE, Op0, |
| 2343 | Constant::getNullValue(CI->getType())); |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2344 | |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2345 | // Replace (and X, (1 << size(X)-1) != 0) with x < 0, converting X |
| 2346 | // to be a signed value as appropriate. |
| 2347 | if (isSignBit(BOC)) { |
| 2348 | Value *X = BO->getOperand(0); |
| 2349 | // If 'X' is not signed, insert a cast now... |
| 2350 | if (!BOC->getType()->isSigned()) { |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 2351 | const Type *DestTy = BOC->getType()->getSignedVersion(); |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2352 | X = InsertCastBefore(X, DestTy, I); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2353 | } |
| 2354 | return new SetCondInst(isSetNE ? Instruction::SetLT : |
| 2355 | Instruction::SetGE, X, |
| 2356 | Constant::getNullValue(X->getType())); |
| 2357 | } |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2358 | |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2359 | // ((X & ~7) == 0) --> X < 8 |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2360 | if (CI->isNullValue() && isHighOnes(BOC)) { |
| 2361 | Value *X = BO->getOperand(0); |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2362 | Constant *NegX = ConstantExpr::getNeg(BOC); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2363 | |
| 2364 | // If 'X' is signed, insert a cast now. |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2365 | if (NegX->getType()->isSigned()) { |
| 2366 | const Type *DestTy = NegX->getType()->getUnsignedVersion(); |
| 2367 | X = InsertCastBefore(X, DestTy, I); |
| 2368 | NegX = ConstantExpr::getCast(NegX, DestTy); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2369 | } |
| 2370 | |
| 2371 | return new SetCondInst(isSetNE ? Instruction::SetGE : |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2372 | Instruction::SetLT, X, NegX); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 2375 | } |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2376 | default: break; |
| 2377 | } |
| 2378 | } |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2379 | } else { // Not a SetEQ/SetNE |
| 2380 | // If the LHS is a cast from an integral value of the same size, |
| 2381 | if (CastInst *Cast = dyn_cast<CastInst>(Op0)) { |
| 2382 | Value *CastOp = Cast->getOperand(0); |
| 2383 | const Type *SrcTy = CastOp->getType(); |
| 2384 | unsigned SrcTySize = SrcTy->getPrimitiveSize(); |
| 2385 | if (SrcTy != Cast->getType() && SrcTy->isInteger() && |
| 2386 | SrcTySize == Cast->getType()->getPrimitiveSize()) { |
| 2387 | assert((SrcTy->isSigned() ^ Cast->getType()->isSigned()) && |
| 2388 | "Source and destination signednesses should differ!"); |
| 2389 | if (Cast->getType()->isSigned()) { |
| 2390 | // If this is a signed comparison, check for comparisons in the |
| 2391 | // vicinity of zero. |
| 2392 | if (I.getOpcode() == Instruction::SetLT && CI->isNullValue()) |
| 2393 | // X < 0 => x > 127 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2394 | return BinaryOperator::createSetGT(CastOp, |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2395 | ConstantUInt::get(SrcTy, (1ULL << (SrcTySize*8-1))-1)); |
| 2396 | else if (I.getOpcode() == Instruction::SetGT && |
| 2397 | cast<ConstantSInt>(CI)->getValue() == -1) |
| 2398 | // X > -1 => x < 128 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2399 | return BinaryOperator::createSetLT(CastOp, |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2400 | ConstantUInt::get(SrcTy, 1ULL << (SrcTySize*8-1))); |
| 2401 | } else { |
| 2402 | ConstantUInt *CUI = cast<ConstantUInt>(CI); |
| 2403 | if (I.getOpcode() == Instruction::SetLT && |
| 2404 | CUI->getValue() == 1ULL << (SrcTySize*8-1)) |
| 2405 | // X < 128 => X > -1 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2406 | return BinaryOperator::createSetGT(CastOp, |
| 2407 | ConstantSInt::get(SrcTy, -1)); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2408 | else if (I.getOpcode() == Instruction::SetGT && |
| 2409 | CUI->getValue() == (1ULL << (SrcTySize*8-1))-1) |
| 2410 | // X > 127 => X < 0 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2411 | return BinaryOperator::createSetLT(CastOp, |
| 2412 | Constant::getNullValue(SrcTy)); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2413 | } |
| 2414 | } |
| 2415 | } |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 2416 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 2419 | // Test to see if the operands of the setcc are casted versions of other |
| 2420 | // 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] | 2421 | if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
| 2422 | Value *CastOp0 = CI->getOperand(0); |
| 2423 | if (CastOp0->getType()->isLosslesslyConvertibleTo(CI->getType()) && |
Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 2424 | (isa<Constant>(Op1) || isa<CastInst>(Op1)) && |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 2425 | (I.getOpcode() == Instruction::SetEQ || |
| 2426 | I.getOpcode() == Instruction::SetNE)) { |
| 2427 | // We keep moving the cast from the left operand over to the right |
| 2428 | // operand, where it can often be eliminated completely. |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 2429 | Op0 = CastOp0; |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 2430 | |
| 2431 | // If operand #1 is a cast instruction, see if we can eliminate it as |
| 2432 | // well. |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 2433 | if (CastInst *CI2 = dyn_cast<CastInst>(Op1)) |
| 2434 | if (CI2->getOperand(0)->getType()->isLosslesslyConvertibleTo( |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 2435 | Op0->getType())) |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 2436 | Op1 = CI2->getOperand(0); |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 2437 | |
| 2438 | // If Op1 is a constant, we can fold the cast into the constant. |
| 2439 | if (Op1->getType() != Op0->getType()) |
| 2440 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
| 2441 | Op1 = ConstantExpr::getCast(Op1C, Op0->getType()); |
| 2442 | } else { |
| 2443 | // Otherwise, cast the RHS right before the setcc |
| 2444 | Op1 = new CastInst(Op1, Op0->getType(), Op1->getName()); |
| 2445 | InsertNewInstBefore(cast<Instruction>(Op1), I); |
| 2446 | } |
| 2447 | return BinaryOperator::create(I.getOpcode(), Op0, Op1); |
| 2448 | } |
| 2449 | |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 2450 | // Handle the special case of: setcc (cast bool to X), <cst> |
| 2451 | // This comes up when you have code like |
| 2452 | // int X = A < B; |
| 2453 | // if (X) ... |
| 2454 | // For generality, we handle any zero-extension of any operand comparison |
| 2455 | // with a constant. |
| 2456 | if (ConstantInt *ConstantRHS = dyn_cast<ConstantInt>(Op1)) { |
| 2457 | const Type *SrcTy = CastOp0->getType(); |
| 2458 | const Type *DestTy = Op0->getType(); |
| 2459 | if (SrcTy->getPrimitiveSize() < DestTy->getPrimitiveSize() && |
| 2460 | (SrcTy->isUnsigned() || SrcTy == Type::BoolTy)) { |
| 2461 | // Ok, we have an expansion of operand 0 into a new type. Get the |
| 2462 | // constant value, masink off bits which are not set in the RHS. These |
| 2463 | // could be set if the destination value is signed. |
| 2464 | uint64_t ConstVal = ConstantRHS->getRawValue(); |
| 2465 | ConstVal &= (1ULL << DestTy->getPrimitiveSize()*8)-1; |
| 2466 | |
| 2467 | // If the constant we are comparing it with has high bits set, which |
| 2468 | // don't exist in the original value, the values could never be equal, |
| 2469 | // because the source would be zero extended. |
| 2470 | unsigned SrcBits = |
| 2471 | SrcTy == Type::BoolTy ? 1 : SrcTy->getPrimitiveSize()*8; |
Chris Lattner | 7c94d11 | 2003-11-05 17:31:36 +0000 | [diff] [blame] | 2472 | bool HasSignBit = ConstVal & (1ULL << (DestTy->getPrimitiveSize()*8-1)); |
| 2473 | if (ConstVal & ~((1ULL << SrcBits)-1)) { |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 2474 | switch (I.getOpcode()) { |
| 2475 | default: assert(0 && "Unknown comparison type!"); |
| 2476 | case Instruction::SetEQ: |
| 2477 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2478 | case Instruction::SetNE: |
| 2479 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2480 | case Instruction::SetLT: |
| 2481 | case Instruction::SetLE: |
| 2482 | if (DestTy->isSigned() && HasSignBit) |
| 2483 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2484 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2485 | case Instruction::SetGT: |
| 2486 | case Instruction::SetGE: |
| 2487 | if (DestTy->isSigned() && HasSignBit) |
| 2488 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2489 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2490 | } |
| 2491 | } |
| 2492 | |
| 2493 | // Otherwise, we can replace the setcc with a setcc of the smaller |
| 2494 | // operand value. |
| 2495 | Op1 = ConstantExpr::getCast(cast<Constant>(Op1), SrcTy); |
| 2496 | return BinaryOperator::create(I.getOpcode(), CastOp0, Op1); |
| 2497 | } |
| 2498 | } |
| 2499 | } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2500 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2501 | } |
| 2502 | |
Reid Spencer | 279fa25 | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 2503 | // visitSetCondInstWithCastAndConstant - this method is part of the |
| 2504 | // visitSetCondInst method. It handles the situation where we have: |
| 2505 | // (setcc (cast X to larger), CI) |
| 2506 | // It tries to remove the cast and even the setcc if the CI value |
| 2507 | // and range of the cast allow it. |
| 2508 | Instruction * |
| 2509 | InstCombiner::visitSetCondInstWithCastAndConstant(BinaryOperator&I, |
| 2510 | CastInst* LHSI, |
| 2511 | ConstantInt* CI) { |
| 2512 | const Type *SrcTy = LHSI->getOperand(0)->getType(); |
| 2513 | const Type *DestTy = LHSI->getType(); |
| 2514 | if (SrcTy->isIntegral() && DestTy->isIntegral()) { |
| 2515 | unsigned SrcBits = SrcTy->getPrimitiveSize()*8; |
| 2516 | unsigned DestBits = DestTy->getPrimitiveSize()*8; |
| 2517 | if (SrcTy == Type::BoolTy) |
| 2518 | SrcBits = 1; |
| 2519 | if (DestTy == Type::BoolTy) |
| 2520 | DestBits = 1; |
| 2521 | if (SrcBits < DestBits) { |
| 2522 | // There are fewer bits in the source of the cast than in the result |
| 2523 | // of the cast. Any other case doesn't matter because the constant |
| 2524 | // value won't have changed due to sign extension. |
| 2525 | Constant *NewCst = ConstantExpr::getCast(CI, SrcTy); |
| 2526 | if (ConstantExpr::getCast(NewCst, DestTy) == CI) { |
| 2527 | // The constant value operand of the setCC before and after a |
| 2528 | // cast to the source type of the cast instruction is the same |
| 2529 | // value, so we just replace with the same setcc opcode, but |
| 2530 | // using the source value compared to the constant casted to the |
| 2531 | // source type. |
| 2532 | if (SrcTy->isSigned() && DestTy->isUnsigned()) { |
| 2533 | CastInst* Cst = new CastInst(LHSI->getOperand(0), |
| 2534 | SrcTy->getUnsignedVersion(), LHSI->getName()); |
| 2535 | InsertNewInstBefore(Cst,I); |
| 2536 | return new SetCondInst(I.getOpcode(), Cst, |
| 2537 | ConstantExpr::getCast(CI, SrcTy->getUnsignedVersion())); |
| 2538 | } |
| 2539 | return new SetCondInst(I.getOpcode(), LHSI->getOperand(0),NewCst); |
| 2540 | } |
| 2541 | // The constant value before and after a cast to the source type |
| 2542 | // is different, so various cases are possible depending on the |
| 2543 | // opcode and the signs of the types involved in the cast. |
| 2544 | switch (I.getOpcode()) { |
| 2545 | case Instruction::SetLT: { |
| 2546 | Constant* Max = ConstantIntegral::getMaxValue(SrcTy); |
| 2547 | Max = ConstantExpr::getCast(Max, DestTy); |
| 2548 | return ReplaceInstUsesWith(I, ConstantExpr::getSetLT(Max, CI)); |
| 2549 | } |
| 2550 | case Instruction::SetGT: { |
| 2551 | Constant* Min = ConstantIntegral::getMinValue(SrcTy); |
| 2552 | Min = ConstantExpr::getCast(Min, DestTy); |
| 2553 | return ReplaceInstUsesWith(I, ConstantExpr::getSetGT(Min, CI)); |
| 2554 | } |
| 2555 | case Instruction::SetEQ: |
| 2556 | // We're looking for equality, and we know the values are not |
| 2557 | // equal so replace with constant False. |
| 2558 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2559 | case Instruction::SetNE: |
| 2560 | // We're testing for inequality, and we know the values are not |
| 2561 | // equal so replace with constant True. |
| 2562 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2563 | case Instruction::SetLE: |
| 2564 | case Instruction::SetGE: |
| 2565 | assert(!"SetLE and SetGE should be handled elsewhere"); |
| 2566 | default: |
| 2567 | assert(!"unknown integer comparison"); |
| 2568 | } |
| 2569 | } |
| 2570 | } |
| 2571 | return 0; |
| 2572 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2573 | |
| 2574 | |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 2575 | Instruction *InstCombiner::visitShiftInst(ShiftInst &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2576 | assert(I.getOperand(1)->getType() == Type::UByteTy); |
| 2577 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 2578 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2579 | |
| 2580 | // shl X, 0 == X and shr X, 0 == X |
| 2581 | // shl 0, X == 0 and shr 0, X == 0 |
| 2582 | if (Op1 == Constant::getNullValue(Type::UByteTy) || |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2583 | Op0 == Constant::getNullValue(Op0->getType())) |
| 2584 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2585 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2586 | if (isa<UndefValue>(Op0)) { // undef >>s X -> undef |
| 2587 | if (!isLeftShift && I.getType()->isSigned()) |
Chris Lattner | 67f0545 | 2004-10-16 23:28:04 +0000 | [diff] [blame] | 2588 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2589 | else // undef << X -> 0 AND undef >>u X -> 0 |
| 2590 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2591 | } |
| 2592 | if (isa<UndefValue>(Op1)) { |
| 2593 | if (isLeftShift || I.getType()->isUnsigned()) |
| 2594 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2595 | else |
| 2596 | return ReplaceInstUsesWith(I, Op0); // X >>s undef -> X |
| 2597 | } |
| 2598 | |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 2599 | // shr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 2600 | if (!isLeftShift) |
| 2601 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0)) |
| 2602 | if (CSI->isAllOnesValue()) |
| 2603 | return ReplaceInstUsesWith(I, CSI); |
| 2604 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2605 | // Try to fold constant and into select arguments. |
| 2606 | if (isa<Constant>(Op0)) |
| 2607 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 2608 | if (Instruction *R = FoldBinOpIntoSelect(I, SI, this)) |
| 2609 | return R; |
| 2610 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2611 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) { |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 2612 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr |
| 2613 | // of a signed value. |
| 2614 | // |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 2615 | unsigned TypeBits = Op0->getType()->getPrimitiveSize()*8; |
Chris Lattner | f5ce254 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 2616 | if (CUI->getValue() >= TypeBits) { |
| 2617 | if (!Op0->getType()->isSigned() || isLeftShift) |
| 2618 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
| 2619 | else { |
| 2620 | I.setOperand(1, ConstantUInt::get(Type::UByteTy, TypeBits-1)); |
| 2621 | return &I; |
| 2622 | } |
| 2623 | } |
Chris Lattner | 55f3d94 | 2002-09-10 23:04:09 +0000 | [diff] [blame] | 2624 | |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2625 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 2626 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 2627 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 2628 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2629 | return BinaryOperator::createMul(BO->getOperand(0), |
| 2630 | ConstantExpr::getShl(BOOp, CUI)); |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2631 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2632 | // Try to fold constant and into select arguments. |
| 2633 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 2634 | if (Instruction *R = FoldBinOpIntoSelect(I, SI, this)) |
| 2635 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2636 | if (isa<PHINode>(Op0)) |
| 2637 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2638 | return NV; |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2639 | |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 2640 | // If the operand is an bitwise operator with a constant RHS, and the |
| 2641 | // 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] | 2642 | if (Op0->hasOneUse()) |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 2643 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) |
| 2644 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 2645 | bool isValid = true; // Valid only for And, Or, Xor |
| 2646 | bool highBitSet = false; // Transform if high bit of constant set? |
| 2647 | |
| 2648 | switch (Op0BO->getOpcode()) { |
| 2649 | default: isValid = false; break; // Do not perform transform! |
Chris Lattner | 44bd392 | 2004-10-08 03:46:20 +0000 | [diff] [blame] | 2650 | case Instruction::Add: |
| 2651 | isValid = isLeftShift; |
| 2652 | break; |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 2653 | case Instruction::Or: |
| 2654 | case Instruction::Xor: |
| 2655 | highBitSet = false; |
| 2656 | break; |
| 2657 | case Instruction::And: |
| 2658 | highBitSet = true; |
| 2659 | break; |
| 2660 | } |
| 2661 | |
| 2662 | // If this is a signed shift right, and the high bit is modified |
| 2663 | // by the logical operation, do not perform the transformation. |
| 2664 | // The highBitSet boolean indicates the value of the high bit of |
| 2665 | // the constant which would cause it to be modified for this |
| 2666 | // operation. |
| 2667 | // |
| 2668 | if (isValid && !isLeftShift && !I.getType()->isUnsigned()) { |
| 2669 | uint64_t Val = Op0C->getRawValue(); |
| 2670 | isValid = ((Val & (1 << (TypeBits-1))) != 0) == highBitSet; |
| 2671 | } |
| 2672 | |
| 2673 | if (isValid) { |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2674 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, CUI); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 2675 | |
| 2676 | Instruction *NewShift = |
| 2677 | new ShiftInst(I.getOpcode(), Op0BO->getOperand(0), CUI, |
| 2678 | Op0BO->getName()); |
| 2679 | Op0BO->setName(""); |
| 2680 | InsertNewInstBefore(NewShift, I); |
| 2681 | |
| 2682 | return BinaryOperator::create(Op0BO->getOpcode(), NewShift, |
| 2683 | NewRHS); |
| 2684 | } |
| 2685 | } |
| 2686 | |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 2687 | // 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] | 2688 | if (ShiftInst *Op0SI = dyn_cast<ShiftInst>(Op0)) |
Chris Lattner | ab780df | 2003-07-24 18:38:56 +0000 | [diff] [blame] | 2689 | if (ConstantUInt *ShiftAmt1C = |
| 2690 | dyn_cast<ConstantUInt>(Op0SI->getOperand(1))) { |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 2691 | unsigned ShiftAmt1 = ShiftAmt1C->getValue(); |
| 2692 | unsigned ShiftAmt2 = CUI->getValue(); |
| 2693 | |
| 2694 | // Check for (A << c1) << c2 and (A >> c1) >> c2 |
| 2695 | if (I.getOpcode() == Op0SI->getOpcode()) { |
| 2696 | unsigned Amt = ShiftAmt1+ShiftAmt2; // Fold into one big shift... |
Chris Lattner | f5ce254 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 2697 | if (Op0->getType()->getPrimitiveSize()*8 < Amt) |
| 2698 | Amt = Op0->getType()->getPrimitiveSize()*8; |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 2699 | return new ShiftInst(I.getOpcode(), Op0SI->getOperand(0), |
| 2700 | ConstantUInt::get(Type::UByteTy, Amt)); |
| 2701 | } |
| 2702 | |
Chris Lattner | ab780df | 2003-07-24 18:38:56 +0000 | [diff] [blame] | 2703 | // Check for (A << c1) >> c2 or visaversa. If we are dealing with |
| 2704 | // signed types, we can only support the (A >> c1) << c2 configuration, |
| 2705 | // 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] | 2706 | if (I.getType()->isUnsigned() || isLeftShift) { |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 2707 | // Calculate bitmask for what gets shifted off the edge... |
| 2708 | Constant *C = ConstantIntegral::getAllOnesValue(I.getType()); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 2709 | if (isLeftShift) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2710 | C = ConstantExpr::getShl(C, ShiftAmt1C); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 2711 | else |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2712 | C = ConstantExpr::getShr(C, ShiftAmt1C); |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 2713 | |
| 2714 | Instruction *Mask = |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2715 | BinaryOperator::createAnd(Op0SI->getOperand(0), C, |
| 2716 | Op0SI->getOperand(0)->getName()+".mask"); |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 2717 | InsertNewInstBefore(Mask, I); |
| 2718 | |
| 2719 | // Figure out what flavor of shift we should use... |
| 2720 | if (ShiftAmt1 == ShiftAmt2) |
| 2721 | return ReplaceInstUsesWith(I, Mask); // (A << c) >> c === A & c2 |
| 2722 | else if (ShiftAmt1 < ShiftAmt2) { |
| 2723 | return new ShiftInst(I.getOpcode(), Mask, |
| 2724 | ConstantUInt::get(Type::UByteTy, ShiftAmt2-ShiftAmt1)); |
| 2725 | } else { |
| 2726 | return new ShiftInst(Op0SI->getOpcode(), Mask, |
| 2727 | ConstantUInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2)); |
| 2728 | } |
| 2729 | } |
| 2730 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2731 | } |
Chris Lattner | 2e0fb39 | 2002-10-08 16:16:40 +0000 | [diff] [blame] | 2732 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2733 | return 0; |
| 2734 | } |
| 2735 | |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 2736 | enum CastType { |
| 2737 | Noop = 0, |
| 2738 | Truncate = 1, |
| 2739 | Signext = 2, |
| 2740 | Zeroext = 3 |
| 2741 | }; |
| 2742 | |
| 2743 | /// getCastType - In the future, we will split the cast instruction into these |
| 2744 | /// various types. Until then, we have to do the analysis here. |
| 2745 | static CastType getCastType(const Type *Src, const Type *Dest) { |
| 2746 | assert(Src->isIntegral() && Dest->isIntegral() && |
| 2747 | "Only works on integral types!"); |
| 2748 | unsigned SrcSize = Src->getPrimitiveSize()*8; |
| 2749 | if (Src == Type::BoolTy) SrcSize = 1; |
| 2750 | unsigned DestSize = Dest->getPrimitiveSize()*8; |
| 2751 | if (Dest == Type::BoolTy) DestSize = 1; |
| 2752 | |
| 2753 | if (SrcSize == DestSize) return Noop; |
| 2754 | if (SrcSize > DestSize) return Truncate; |
| 2755 | if (Src->isSigned()) return Signext; |
| 2756 | return Zeroext; |
| 2757 | } |
| 2758 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2759 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2760 | // isEliminableCastOfCast - Return true if it is valid to eliminate the CI |
| 2761 | // instruction. |
| 2762 | // |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 2763 | static inline bool isEliminableCastOfCast(const Type *SrcTy, const Type *MidTy, |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 2764 | const Type *DstTy, TargetData *TD) { |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2765 | |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 2766 | // It is legal to eliminate the instruction if casting A->B->A if the sizes |
| 2767 | // are identical and the bits don't get reinterpreted (for example |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 2768 | // int->float->int would not be allowed). |
Misha Brukman | e5838c4 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 2769 | if (SrcTy == DstTy && SrcTy->isLosslesslyConvertibleTo(MidTy)) |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 2770 | return true; |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2771 | |
Chris Lattner | 4fbad96 | 2004-07-21 04:27:24 +0000 | [diff] [blame] | 2772 | // If we are casting between pointer and integer types, treat pointers as |
| 2773 | // integers of the appropriate size for the code below. |
| 2774 | if (isa<PointerType>(SrcTy)) SrcTy = TD->getIntPtrType(); |
| 2775 | if (isa<PointerType>(MidTy)) MidTy = TD->getIntPtrType(); |
| 2776 | if (isa<PointerType>(DstTy)) DstTy = TD->getIntPtrType(); |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 2777 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2778 | // Allow free casting and conversion of sizes as long as the sign doesn't |
| 2779 | // change... |
Chris Lattner | b0b412e | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 2780 | if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral()) { |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 2781 | CastType FirstCast = getCastType(SrcTy, MidTy); |
| 2782 | CastType SecondCast = getCastType(MidTy, DstTy); |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 2783 | |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 2784 | // Capture the effect of these two casts. If the result is a legal cast, |
| 2785 | // the CastType is stored here, otherwise a special code is used. |
| 2786 | static const unsigned CastResult[] = { |
| 2787 | // First cast is noop |
| 2788 | 0, 1, 2, 3, |
| 2789 | // First cast is a truncate |
| 2790 | 1, 1, 4, 4, // trunc->extend is not safe to eliminate |
| 2791 | // First cast is a sign ext |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 2792 | 2, 5, 2, 4, // signext->zeroext never ok |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 2793 | // First cast is a zero ext |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 2794 | 3, 5, 3, 3, |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 2795 | }; |
| 2796 | |
| 2797 | unsigned Result = CastResult[FirstCast*4+SecondCast]; |
| 2798 | switch (Result) { |
| 2799 | default: assert(0 && "Illegal table value!"); |
| 2800 | case 0: |
| 2801 | case 1: |
| 2802 | case 2: |
| 2803 | case 3: |
| 2804 | // FIXME: in the future, when LLVM has explicit sign/zeroextends and |
| 2805 | // truncates, we could eliminate more casts. |
| 2806 | return (unsigned)getCastType(SrcTy, DstTy) == Result; |
| 2807 | case 4: |
| 2808 | return false; // Not possible to eliminate this here. |
| 2809 | case 5: |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 2810 | // Sign or zero extend followed by truncate is always ok if the result |
| 2811 | // is a truncate or noop. |
| 2812 | CastType ResultCast = getCastType(SrcTy, DstTy); |
| 2813 | if (ResultCast == Noop || ResultCast == Truncate) |
| 2814 | return true; |
| 2815 | // Otherwise we are still growing the value, we are only safe if the |
| 2816 | // result will match the sign/zeroextendness of the result. |
| 2817 | return ResultCast == FirstCast; |
Chris Lattner | 3732aca | 2002-08-15 16:15:25 +0000 | [diff] [blame] | 2818 | } |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 2819 | } |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2820 | return false; |
| 2821 | } |
| 2822 | |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 2823 | static bool ValueRequiresCast(const Value *V, const Type *Ty, TargetData *TD) { |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 2824 | if (V->getType() == Ty || isa<Constant>(V)) return false; |
| 2825 | if (const CastInst *CI = dyn_cast<CastInst>(V)) |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 2826 | if (isEliminableCastOfCast(CI->getOperand(0)->getType(), CI->getType(), Ty, |
| 2827 | TD)) |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 2828 | return false; |
| 2829 | return true; |
| 2830 | } |
| 2831 | |
| 2832 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 2833 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 2834 | /// casts that are known to not do anything... |
| 2835 | /// |
| 2836 | Value *InstCombiner::InsertOperandCastBefore(Value *V, const Type *DestTy, |
| 2837 | Instruction *InsertBefore) { |
| 2838 | if (V->getType() == DestTy) return V; |
| 2839 | if (Constant *C = dyn_cast<Constant>(V)) |
| 2840 | return ConstantExpr::getCast(C, DestTy); |
| 2841 | |
| 2842 | CastInst *CI = new CastInst(V, DestTy, V->getName()); |
| 2843 | InsertNewInstBefore(CI, *InsertBefore); |
| 2844 | return CI; |
| 2845 | } |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2846 | |
| 2847 | // CastInst simplification |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2848 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2849 | Instruction *InstCombiner::visitCastInst(CastInst &CI) { |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 2850 | Value *Src = CI.getOperand(0); |
| 2851 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2852 | // If the user is casting a value to the same type, eliminate this cast |
| 2853 | // instruction... |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 2854 | if (CI.getType() == Src->getType()) |
| 2855 | return ReplaceInstUsesWith(CI, Src); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2856 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2857 | if (isa<UndefValue>(Src)) // cast undef -> undef |
| 2858 | return ReplaceInstUsesWith(CI, UndefValue::get(CI.getType())); |
| 2859 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2860 | // If casting the result of another cast instruction, try to eliminate this |
| 2861 | // one! |
| 2862 | // |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 2863 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 2864 | if (isEliminableCastOfCast(CSrc->getOperand(0)->getType(), |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 2865 | CSrc->getType(), CI.getType(), TD)) { |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2866 | // This instruction now refers directly to the cast's src operand. This |
| 2867 | // has a good chance of making CSrc dead. |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2868 | CI.setOperand(0, CSrc->getOperand(0)); |
| 2869 | return &CI; |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2870 | } |
| 2871 | |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 2872 | // If this is an A->B->A cast, and we are dealing with integral types, try |
| 2873 | // to convert this into a logical 'and' instruction. |
| 2874 | // |
| 2875 | if (CSrc->getOperand(0)->getType() == CI.getType() && |
Chris Lattner | b0b412e | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 2876 | CI.getType()->isInteger() && CSrc->getType()->isInteger() && |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 2877 | CI.getType()->isUnsigned() && CSrc->getType()->isUnsigned() && |
| 2878 | CSrc->getType()->getPrimitiveSize() < CI.getType()->getPrimitiveSize()){ |
| 2879 | assert(CSrc->getType() != Type::ULongTy && |
| 2880 | "Cannot have type bigger than ulong!"); |
Chris Lattner | 196897c | 2003-05-26 23:41:32 +0000 | [diff] [blame] | 2881 | uint64_t AndValue = (1ULL << CSrc->getType()->getPrimitiveSize()*8)-1; |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 2882 | Constant *AndOp = ConstantUInt::get(CI.getType(), AndValue); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2883 | return BinaryOperator::createAnd(CSrc->getOperand(0), AndOp); |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 2884 | } |
| 2885 | } |
| 2886 | |
Chris Lattner | 0384165 | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 2887 | // If this is a cast to bool, turn it into the appropriate setne instruction. |
| 2888 | if (CI.getType() == Type::BoolTy) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2889 | return BinaryOperator::createSetNE(CI.getOperand(0), |
Chris Lattner | 0384165 | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 2890 | Constant::getNullValue(CI.getOperand(0)->getType())); |
| 2891 | |
Chris Lattner | d0d5160 | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 2892 | // If casting the result of a getelementptr instruction with no offset, turn |
| 2893 | // this into a cast of the original pointer! |
| 2894 | // |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 2895 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
Chris Lattner | d0d5160 | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 2896 | bool AllZeroOperands = true; |
| 2897 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) |
| 2898 | if (!isa<Constant>(GEP->getOperand(i)) || |
| 2899 | !cast<Constant>(GEP->getOperand(i))->isNullValue()) { |
| 2900 | AllZeroOperands = false; |
| 2901 | break; |
| 2902 | } |
| 2903 | if (AllZeroOperands) { |
| 2904 | CI.setOperand(0, GEP->getOperand(0)); |
| 2905 | return &CI; |
| 2906 | } |
| 2907 | } |
| 2908 | |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 2909 | // If we are casting a malloc or alloca to a pointer to a type of the same |
| 2910 | // size, rewrite the allocation instruction to allocate the "right" type. |
| 2911 | // |
| 2912 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) |
Chris Lattner | d4d987d | 2003-11-02 06:54:48 +0000 | [diff] [blame] | 2913 | if (AI->hasOneUse() && !AI->isArrayAllocation()) |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 2914 | if (const PointerType *PTy = dyn_cast<PointerType>(CI.getType())) { |
| 2915 | // Get the type really allocated and the type casted to... |
| 2916 | const Type *AllocElTy = AI->getAllocatedType(); |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 2917 | const Type *CastElTy = PTy->getElementType(); |
Chris Lattner | 9eb9ccd | 2004-07-06 19:28:42 +0000 | [diff] [blame] | 2918 | if (AllocElTy->isSized() && CastElTy->isSized()) { |
| 2919 | unsigned AllocElTySize = TD->getTypeSize(AllocElTy); |
| 2920 | unsigned CastElTySize = TD->getTypeSize(CastElTy); |
Chris Lattner | 7c94d11 | 2003-11-05 17:31:36 +0000 | [diff] [blame] | 2921 | |
Chris Lattner | 9eb9ccd | 2004-07-06 19:28:42 +0000 | [diff] [blame] | 2922 | // If the allocation is for an even multiple of the cast type size |
| 2923 | if (CastElTySize && (AllocElTySize % CastElTySize == 0)) { |
| 2924 | Value *Amt = ConstantUInt::get(Type::UIntTy, |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 2925 | AllocElTySize/CastElTySize); |
Chris Lattner | 9eb9ccd | 2004-07-06 19:28:42 +0000 | [diff] [blame] | 2926 | std::string Name = AI->getName(); AI->setName(""); |
| 2927 | AllocationInst *New; |
| 2928 | if (isa<MallocInst>(AI)) |
| 2929 | New = new MallocInst(CastElTy, Amt, Name); |
| 2930 | else |
| 2931 | New = new AllocaInst(CastElTy, Amt, Name); |
| 2932 | InsertNewInstBefore(New, *AI); |
| 2933 | return ReplaceInstUsesWith(CI, New); |
| 2934 | } |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 2935 | } |
| 2936 | } |
| 2937 | |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2938 | if (isa<PHINode>(Src)) |
| 2939 | if (Instruction *NV = FoldOpIntoPhi(CI)) |
| 2940 | return NV; |
| 2941 | |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 2942 | // If the source value is an instruction with only this use, we can attempt to |
| 2943 | // propagate the cast into the instruction. Also, only handle integral types |
| 2944 | // for now. |
| 2945 | if (Instruction *SrcI = dyn_cast<Instruction>(Src)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2946 | if (SrcI->hasOneUse() && Src->getType()->isIntegral() && |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 2947 | CI.getType()->isInteger()) { // Don't mess with casts to bool here |
| 2948 | const Type *DestTy = CI.getType(); |
| 2949 | unsigned SrcBitSize = getTypeSizeInBits(Src->getType()); |
| 2950 | unsigned DestBitSize = getTypeSizeInBits(DestTy); |
| 2951 | |
| 2952 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; |
| 2953 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; |
| 2954 | |
| 2955 | switch (SrcI->getOpcode()) { |
| 2956 | case Instruction::Add: |
| 2957 | case Instruction::Mul: |
| 2958 | case Instruction::And: |
| 2959 | case Instruction::Or: |
| 2960 | case Instruction::Xor: |
| 2961 | // If we are discarding information, or just changing the sign, rewrite. |
| 2962 | if (DestBitSize <= SrcBitSize && DestBitSize != 1) { |
| 2963 | // Don't insert two casts if they cannot be eliminated. We allow two |
| 2964 | // casts to be inserted if the sizes are the same. This could only be |
| 2965 | // converting signedness, which is a noop. |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 2966 | if (DestBitSize == SrcBitSize || !ValueRequiresCast(Op1, DestTy,TD) || |
| 2967 | !ValueRequiresCast(Op0, DestTy, TD)) { |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 2968 | Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI); |
| 2969 | Value *Op1c = InsertOperandCastBefore(Op1, DestTy, SrcI); |
| 2970 | return BinaryOperator::create(cast<BinaryOperator>(SrcI) |
| 2971 | ->getOpcode(), Op0c, Op1c); |
| 2972 | } |
| 2973 | } |
| 2974 | break; |
| 2975 | case Instruction::Shl: |
| 2976 | // Allow changing the sign of the source operand. Do not allow changing |
| 2977 | // the size of the shift, UNLESS the shift amount is a constant. We |
| 2978 | // mush not change variable sized shifts to a smaller size, because it |
| 2979 | // is undefined to shift more bits out than exist in the value. |
| 2980 | if (DestBitSize == SrcBitSize || |
| 2981 | (DestBitSize < SrcBitSize && isa<Constant>(Op1))) { |
| 2982 | Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI); |
| 2983 | return new ShiftInst(Instruction::Shl, Op0c, Op1); |
| 2984 | } |
| 2985 | break; |
| 2986 | } |
| 2987 | } |
| 2988 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2989 | return 0; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 2990 | } |
| 2991 | |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 2992 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
| 2993 | /// %C = or %A, %B |
| 2994 | /// %D = select %cond, %C, %A |
| 2995 | /// into: |
| 2996 | /// %C = select %cond, %B, 0 |
| 2997 | /// %D = or %A, %C |
| 2998 | /// |
| 2999 | /// Assuming that the specified instruction is an operand to the select, return |
| 3000 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 3001 | /// equal the other incoming value of the select. |
| 3002 | /// |
| 3003 | static unsigned GetSelectFoldableOperands(Instruction *I) { |
| 3004 | switch (I->getOpcode()) { |
| 3005 | case Instruction::Add: |
| 3006 | case Instruction::Mul: |
| 3007 | case Instruction::And: |
| 3008 | case Instruction::Or: |
| 3009 | case Instruction::Xor: |
| 3010 | return 3; // Can fold through either operand. |
| 3011 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 3012 | case Instruction::Shl: // Can only fold on the shift amount. |
| 3013 | case Instruction::Shr: |
| 3014 | return 1; |
| 3015 | default: |
| 3016 | return 0; // Cannot fold |
| 3017 | } |
| 3018 | } |
| 3019 | |
| 3020 | /// GetSelectFoldableConstant - For the same transformation as the previous |
| 3021 | /// function, return the identity constant that goes into the select. |
| 3022 | static Constant *GetSelectFoldableConstant(Instruction *I) { |
| 3023 | switch (I->getOpcode()) { |
| 3024 | default: assert(0 && "This cannot happen!"); abort(); |
| 3025 | case Instruction::Add: |
| 3026 | case Instruction::Sub: |
| 3027 | case Instruction::Or: |
| 3028 | case Instruction::Xor: |
| 3029 | return Constant::getNullValue(I->getType()); |
| 3030 | case Instruction::Shl: |
| 3031 | case Instruction::Shr: |
| 3032 | return Constant::getNullValue(Type::UByteTy); |
| 3033 | case Instruction::And: |
| 3034 | return ConstantInt::getAllOnesValue(I->getType()); |
| 3035 | case Instruction::Mul: |
| 3036 | return ConstantInt::get(I->getType(), 1); |
| 3037 | } |
| 3038 | } |
| 3039 | |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 3040 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 3041 | Value *CondVal = SI.getCondition(); |
| 3042 | Value *TrueVal = SI.getTrueValue(); |
| 3043 | Value *FalseVal = SI.getFalseValue(); |
| 3044 | |
| 3045 | // select true, X, Y -> X |
| 3046 | // select false, X, Y -> Y |
| 3047 | if (ConstantBool *C = dyn_cast<ConstantBool>(CondVal)) |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 3048 | if (C == ConstantBool::True) |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 3049 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 3050 | else { |
| 3051 | assert(C == ConstantBool::False); |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 3052 | return ReplaceInstUsesWith(SI, FalseVal); |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 3053 | } |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 3054 | |
| 3055 | // select C, X, X -> X |
| 3056 | if (TrueVal == FalseVal) |
| 3057 | return ReplaceInstUsesWith(SI, TrueVal); |
| 3058 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3059 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 3060 | return ReplaceInstUsesWith(SI, FalseVal); |
| 3061 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 3062 | return ReplaceInstUsesWith(SI, TrueVal); |
| 3063 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 3064 | if (isa<Constant>(TrueVal)) |
| 3065 | return ReplaceInstUsesWith(SI, TrueVal); |
| 3066 | else |
| 3067 | return ReplaceInstUsesWith(SI, FalseVal); |
| 3068 | } |
| 3069 | |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 3070 | if (SI.getType() == Type::BoolTy) |
| 3071 | if (ConstantBool *C = dyn_cast<ConstantBool>(TrueVal)) { |
| 3072 | if (C == ConstantBool::True) { |
| 3073 | // Change: A = select B, true, C --> A = or B, C |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3074 | return BinaryOperator::createOr(CondVal, FalseVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 3075 | } else { |
| 3076 | // Change: A = select B, false, C --> A = and !B, C |
| 3077 | Value *NotCond = |
| 3078 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 3079 | "not."+CondVal->getName()), SI); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3080 | return BinaryOperator::createAnd(NotCond, FalseVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 3081 | } |
| 3082 | } else if (ConstantBool *C = dyn_cast<ConstantBool>(FalseVal)) { |
| 3083 | if (C == ConstantBool::False) { |
| 3084 | // Change: A = select B, C, false --> A = and B, C |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3085 | return BinaryOperator::createAnd(CondVal, TrueVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 3086 | } else { |
| 3087 | // Change: A = select B, C, true --> A = or !B, C |
| 3088 | Value *NotCond = |
| 3089 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 3090 | "not."+CondVal->getName()), SI); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3091 | return BinaryOperator::createOr(NotCond, TrueVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 3092 | } |
| 3093 | } |
| 3094 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3095 | // Selecting between two integer constants? |
| 3096 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) |
| 3097 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { |
| 3098 | // select C, 1, 0 -> cast C to int |
| 3099 | if (FalseValC->isNullValue() && TrueValC->getRawValue() == 1) { |
| 3100 | return new CastInst(CondVal, SI.getType()); |
| 3101 | } else if (TrueValC->isNullValue() && FalseValC->getRawValue() == 1) { |
| 3102 | // select C, 0, 1 -> cast !C to int |
| 3103 | Value *NotCond = |
| 3104 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
Chris Lattner | cf7baf3 | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 3105 | "not."+CondVal->getName()), SI); |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3106 | return new CastInst(NotCond, SI.getType()); |
Chris Lattner | cf7baf3 | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 3107 | } |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3108 | |
| 3109 | // If one of the constants is zero (we know they can't both be) and we |
| 3110 | // have a setcc instruction with zero, and we have an 'and' with the |
| 3111 | // non-constant value, eliminate this whole mess. This corresponds to |
| 3112 | // cases like this: ((X & 27) ? 27 : 0) |
| 3113 | if (TrueValC->isNullValue() || FalseValC->isNullValue()) |
| 3114 | if (Instruction *IC = dyn_cast<Instruction>(SI.getCondition())) |
| 3115 | if ((IC->getOpcode() == Instruction::SetEQ || |
| 3116 | IC->getOpcode() == Instruction::SetNE) && |
| 3117 | isa<ConstantInt>(IC->getOperand(1)) && |
| 3118 | cast<Constant>(IC->getOperand(1))->isNullValue()) |
| 3119 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) |
| 3120 | if (ICA->getOpcode() == Instruction::And && |
| 3121 | isa<ConstantInt>(ICA->getOperand(1)) && |
| 3122 | (ICA->getOperand(1) == TrueValC || |
| 3123 | ICA->getOperand(1) == FalseValC) && |
| 3124 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { |
| 3125 | // Okay, now we know that everything is set up, we just don't |
| 3126 | // know whether we have a setne or seteq and whether the true or |
| 3127 | // false val is the zero. |
| 3128 | bool ShouldNotVal = !TrueValC->isNullValue(); |
| 3129 | ShouldNotVal ^= IC->getOpcode() == Instruction::SetNE; |
| 3130 | Value *V = ICA; |
| 3131 | if (ShouldNotVal) |
| 3132 | V = InsertNewInstBefore(BinaryOperator::create( |
| 3133 | Instruction::Xor, V, ICA->getOperand(1)), SI); |
| 3134 | return ReplaceInstUsesWith(SI, V); |
| 3135 | } |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 3136 | } |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 3137 | |
| 3138 | // See if we are selecting two values based on a comparison of the two values. |
| 3139 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(CondVal)) { |
| 3140 | if (SCI->getOperand(0) == TrueVal && SCI->getOperand(1) == FalseVal) { |
| 3141 | // Transform (X == Y) ? X : Y -> Y |
| 3142 | if (SCI->getOpcode() == Instruction::SetEQ) |
| 3143 | return ReplaceInstUsesWith(SI, FalseVal); |
| 3144 | // Transform (X != Y) ? X : Y -> X |
| 3145 | if (SCI->getOpcode() == Instruction::SetNE) |
| 3146 | return ReplaceInstUsesWith(SI, TrueVal); |
| 3147 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 3148 | |
| 3149 | } else if (SCI->getOperand(0) == FalseVal && SCI->getOperand(1) == TrueVal){ |
| 3150 | // Transform (X == Y) ? Y : X -> X |
| 3151 | if (SCI->getOpcode() == Instruction::SetEQ) |
Chris Lattner | 24cf020 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 3152 | return ReplaceInstUsesWith(SI, FalseVal); |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 3153 | // Transform (X != Y) ? Y : X -> Y |
| 3154 | if (SCI->getOpcode() == Instruction::SetNE) |
Chris Lattner | 24cf020 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 3155 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 3156 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 3157 | } |
| 3158 | } |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 3159 | |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 3160 | // See if we can fold the select into one of our operands. |
| 3161 | if (SI.getType()->isInteger()) { |
| 3162 | // See the comment above GetSelectFoldableOperands for a description of the |
| 3163 | // transformation we are doing here. |
| 3164 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) |
| 3165 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && |
| 3166 | !isa<Constant>(FalseVal)) |
| 3167 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { |
| 3168 | unsigned OpToFold = 0; |
| 3169 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 3170 | OpToFold = 1; |
| 3171 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
| 3172 | OpToFold = 2; |
| 3173 | } |
| 3174 | |
| 3175 | if (OpToFold) { |
| 3176 | Constant *C = GetSelectFoldableConstant(TVI); |
| 3177 | std::string Name = TVI->getName(); TVI->setName(""); |
| 3178 | Instruction *NewSel = |
| 3179 | new SelectInst(SI.getCondition(), TVI->getOperand(2-OpToFold), C, |
| 3180 | Name); |
| 3181 | InsertNewInstBefore(NewSel, SI); |
| 3182 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) |
| 3183 | return BinaryOperator::create(BO->getOpcode(), FalseVal, NewSel); |
| 3184 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(TVI)) |
| 3185 | return new ShiftInst(SI->getOpcode(), FalseVal, NewSel); |
| 3186 | else { |
| 3187 | assert(0 && "Unknown instruction!!"); |
| 3188 | } |
| 3189 | } |
| 3190 | } |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3191 | |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 3192 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) |
| 3193 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && |
| 3194 | !isa<Constant>(TrueVal)) |
| 3195 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { |
| 3196 | unsigned OpToFold = 0; |
| 3197 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 3198 | OpToFold = 1; |
| 3199 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
| 3200 | OpToFold = 2; |
| 3201 | } |
| 3202 | |
| 3203 | if (OpToFold) { |
| 3204 | Constant *C = GetSelectFoldableConstant(FVI); |
| 3205 | std::string Name = FVI->getName(); FVI->setName(""); |
| 3206 | Instruction *NewSel = |
| 3207 | new SelectInst(SI.getCondition(), C, FVI->getOperand(2-OpToFold), |
| 3208 | Name); |
| 3209 | InsertNewInstBefore(NewSel, SI); |
| 3210 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) |
| 3211 | return BinaryOperator::create(BO->getOpcode(), TrueVal, NewSel); |
| 3212 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(FVI)) |
| 3213 | return new ShiftInst(SI->getOpcode(), TrueVal, NewSel); |
| 3214 | else { |
| 3215 | assert(0 && "Unknown instruction!!"); |
| 3216 | } |
| 3217 | } |
| 3218 | } |
| 3219 | } |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 3220 | return 0; |
| 3221 | } |
| 3222 | |
| 3223 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 3224 | // CallInst simplification |
| 3225 | // |
| 3226 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 3227 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 3228 | // visitCallSite. |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 3229 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(&CI)) { |
| 3230 | bool Changed = false; |
| 3231 | |
| 3232 | // memmove/cpy/set of zero bytes is a noop. |
| 3233 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { |
| 3234 | if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); |
| 3235 | |
| 3236 | // FIXME: Increase alignment here. |
| 3237 | |
| 3238 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
| 3239 | if (CI->getRawValue() == 1) { |
| 3240 | // Replace the instruction with just byte operations. We would |
| 3241 | // transform other cases to loads/stores, but we don't know if |
| 3242 | // alignment is sufficient. |
| 3243 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 3244 | } |
| 3245 | |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 3246 | // If we have a memmove and the source operation is a constant global, |
| 3247 | // then the source and dest pointers can't alias, so we can change this |
| 3248 | // into a call to memcpy. |
| 3249 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) |
| 3250 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
| 3251 | if (GVSrc->isConstant()) { |
| 3252 | Module *M = CI.getParent()->getParent()->getParent(); |
| 3253 | Function *MemCpy = M->getOrInsertFunction("llvm.memcpy", |
| 3254 | CI.getCalledFunction()->getFunctionType()); |
| 3255 | CI.setOperand(0, MemCpy); |
| 3256 | Changed = true; |
| 3257 | } |
| 3258 | |
| 3259 | if (Changed) return &CI; |
Chris Lattner | 9530754 | 2004-11-18 21:41:39 +0000 | [diff] [blame] | 3260 | } else if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(&CI)) { |
| 3261 | // If this stoppoint is at the same source location as the previous |
| 3262 | // stoppoint in the chain, it is not needed. |
| 3263 | if (DbgStopPointInst *PrevSPI = |
| 3264 | dyn_cast<DbgStopPointInst>(SPI->getChain())) |
| 3265 | if (SPI->getLineNo() == PrevSPI->getLineNo() && |
| 3266 | SPI->getColNo() == PrevSPI->getColNo()) { |
| 3267 | SPI->replaceAllUsesWith(PrevSPI); |
| 3268 | return EraseInstFromFunction(CI); |
| 3269 | } |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 3270 | } |
| 3271 | |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 3272 | return visitCallSite(&CI); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | // InvokeInst simplification |
| 3276 | // |
| 3277 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 3278 | return visitCallSite(&II); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 3279 | } |
| 3280 | |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 3281 | // visitCallSite - Improvements for call and invoke instructions. |
| 3282 | // |
| 3283 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 3284 | bool Changed = false; |
| 3285 | |
| 3286 | // If the callee is a constexpr cast of a function, attempt to move the cast |
| 3287 | // to the arguments of the call/invoke. |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 3288 | if (transformConstExprCastCall(CS)) return 0; |
| 3289 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 3290 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3291 | |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 3292 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
| 3293 | // This instruction is not reachable, just remove it. We insert a store to |
| 3294 | // undef so that we know that this code is not reachable, despite the fact |
| 3295 | // that we can't modify the CFG here. |
| 3296 | new StoreInst(ConstantBool::True, |
| 3297 | UndefValue::get(PointerType::get(Type::BoolTy)), |
| 3298 | CS.getInstruction()); |
| 3299 | |
| 3300 | if (!CS.getInstruction()->use_empty()) |
| 3301 | CS.getInstruction()-> |
| 3302 | replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); |
| 3303 | |
| 3304 | if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { |
| 3305 | // Don't break the CFG, insert a dummy cond branch. |
| 3306 | new BranchInst(II->getNormalDest(), II->getUnwindDest(), |
| 3307 | ConstantBool::True, II); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3308 | } |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 3309 | return EraseInstFromFunction(*CS.getInstruction()); |
| 3310 | } |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3311 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 3312 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 3313 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 3314 | if (FTy->isVarArg()) { |
| 3315 | // See if we can optimize any arguments passed through the varargs area of |
| 3316 | // the call. |
| 3317 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
| 3318 | E = CS.arg_end(); I != E; ++I) |
| 3319 | if (CastInst *CI = dyn_cast<CastInst>(*I)) { |
| 3320 | // If this cast does not effect the value passed through the varargs |
| 3321 | // area, we can eliminate the use of the cast. |
| 3322 | Value *Op = CI->getOperand(0); |
| 3323 | if (CI->getType()->isLosslesslyConvertibleTo(Op->getType())) { |
| 3324 | *I = Op; |
| 3325 | Changed = true; |
| 3326 | } |
| 3327 | } |
| 3328 | } |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 3329 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 3330 | return Changed ? CS.getInstruction() : 0; |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 3331 | } |
| 3332 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 3333 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 3334 | // attempt to move the cast to the arguments of the call/invoke. |
| 3335 | // |
| 3336 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 3337 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 3338 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
Chris Lattner | f3edc49 | 2004-07-18 18:59:44 +0000 | [diff] [blame] | 3339 | if (CE->getOpcode() != Instruction::Cast || !isa<Function>(CE->getOperand(0))) |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 3340 | return false; |
Reid Spencer | 8743687 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 3341 | Function *Callee = cast<Function>(CE->getOperand(0)); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 3342 | Instruction *Caller = CS.getInstruction(); |
| 3343 | |
| 3344 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 3345 | // would cause a type conversion of one of our arguments, change this call to |
| 3346 | // be a direct call with arguments casted to the appropriate types. |
| 3347 | // |
| 3348 | const FunctionType *FT = Callee->getFunctionType(); |
| 3349 | const Type *OldRetTy = Caller->getType(); |
| 3350 | |
Chris Lattner | 1f7942f | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 3351 | // Check to see if we are changing the return type... |
| 3352 | if (OldRetTy != FT->getReturnType()) { |
| 3353 | if (Callee->isExternal() && |
| 3354 | !OldRetTy->isLosslesslyConvertibleTo(FT->getReturnType()) && |
| 3355 | !Caller->use_empty()) |
| 3356 | return false; // Cannot transform this return value... |
| 3357 | |
| 3358 | // If the callsite is an invoke instruction, and the return value is used by |
| 3359 | // a PHI node in a successor, we cannot change the return type of the call |
| 3360 | // because there is no place to put the cast instruction (without breaking |
| 3361 | // the critical edge). Bail out in this case. |
| 3362 | if (!Caller->use_empty()) |
| 3363 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 3364 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 3365 | UI != E; ++UI) |
| 3366 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 3367 | if (PN->getParent() == II->getNormalDest() || |
Chris Lattner | fae8ab3 | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 3368 | PN->getParent() == II->getUnwindDest()) |
Chris Lattner | 1f7942f | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 3369 | return false; |
| 3370 | } |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 3371 | |
| 3372 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 3373 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
| 3374 | |
| 3375 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 3376 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 3377 | const Type *ParamTy = FT->getParamType(i); |
| 3378 | bool isConvertible = (*AI)->getType()->isLosslesslyConvertibleTo(ParamTy); |
| 3379 | if (Callee->isExternal() && !isConvertible) return false; |
| 3380 | } |
| 3381 | |
| 3382 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
| 3383 | Callee->isExternal()) |
| 3384 | return false; // Do not delete arguments unless we have a function body... |
| 3385 | |
| 3386 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 3387 | // inserting cast instructions as necessary... |
| 3388 | std::vector<Value*> Args; |
| 3389 | Args.reserve(NumActualArgs); |
| 3390 | |
| 3391 | AI = CS.arg_begin(); |
| 3392 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 3393 | const Type *ParamTy = FT->getParamType(i); |
| 3394 | if ((*AI)->getType() == ParamTy) { |
| 3395 | Args.push_back(*AI); |
| 3396 | } else { |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 3397 | Args.push_back(InsertNewInstBefore(new CastInst(*AI, ParamTy, "tmp"), |
| 3398 | *Caller)); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 3399 | } |
| 3400 | } |
| 3401 | |
| 3402 | // If the function takes more arguments than the call was taking, add them |
| 3403 | // now... |
| 3404 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 3405 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 3406 | |
| 3407 | // If we are removing arguments to the function, emit an obnoxious warning... |
| 3408 | if (FT->getNumParams() < NumActualArgs) |
| 3409 | if (!FT->isVarArg()) { |
| 3410 | std::cerr << "WARNING: While resolving call to function '" |
| 3411 | << Callee->getName() << "' arguments were dropped!\n"; |
| 3412 | } else { |
| 3413 | // Add all of the arguments in their promoted form to the arg list... |
| 3414 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 3415 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 3416 | if (PTy != (*AI)->getType()) { |
| 3417 | // Must promote to pass through va_arg area! |
| 3418 | Instruction *Cast = new CastInst(*AI, PTy, "tmp"); |
| 3419 | InsertNewInstBefore(Cast, *Caller); |
| 3420 | Args.push_back(Cast); |
| 3421 | } else { |
| 3422 | Args.push_back(*AI); |
| 3423 | } |
| 3424 | } |
| 3425 | } |
| 3426 | |
| 3427 | if (FT->getReturnType() == Type::VoidTy) |
| 3428 | Caller->setName(""); // Void type should not have a name... |
| 3429 | |
| 3430 | Instruction *NC; |
| 3431 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Chris Lattner | fae8ab3 | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 3432 | NC = new InvokeInst(Callee, II->getNormalDest(), II->getUnwindDest(), |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 3433 | Args, Caller->getName(), Caller); |
| 3434 | } else { |
| 3435 | NC = new CallInst(Callee, Args, Caller->getName(), Caller); |
| 3436 | } |
| 3437 | |
| 3438 | // Insert a cast of the return type as necessary... |
| 3439 | Value *NV = NC; |
| 3440 | if (Caller->getType() != NV->getType() && !Caller->use_empty()) { |
| 3441 | if (NV->getType() != Type::VoidTy) { |
| 3442 | NV = NC = new CastInst(NC, Caller->getType(), "tmp"); |
Chris Lattner | 686767f | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 3443 | |
| 3444 | // If this is an invoke instruction, we should insert it after the first |
| 3445 | // non-phi, instruction in the normal successor block. |
| 3446 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 3447 | BasicBlock::iterator I = II->getNormalDest()->begin(); |
| 3448 | while (isa<PHINode>(I)) ++I; |
| 3449 | InsertNewInstBefore(NC, *I); |
| 3450 | } else { |
| 3451 | // Otherwise, it's a call, just insert cast right after the call instr |
| 3452 | InsertNewInstBefore(NC, *Caller); |
| 3453 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 3454 | AddUsersToWorkList(*Caller); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 3455 | } else { |
Chris Lattner | e29d634 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 3456 | NV = UndefValue::get(Caller->getType()); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 3457 | } |
| 3458 | } |
| 3459 | |
| 3460 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 3461 | Caller->replaceAllUsesWith(NV); |
| 3462 | Caller->getParent()->getInstList().erase(Caller); |
| 3463 | removeFromWorkList(Caller); |
| 3464 | return true; |
| 3465 | } |
| 3466 | |
| 3467 | |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 3468 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 3469 | // operator and they all are only used by the PHI, PHI together their |
| 3470 | // inputs, and do the operation once, to the result of the PHI. |
| 3471 | Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { |
| 3472 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
| 3473 | |
| 3474 | // Scan the instruction, looking for input operations that can be folded away. |
| 3475 | // If all input operands to the phi are the same instruction (e.g. a cast from |
| 3476 | // the same type or "+42") we can pull the operation through the PHI, reducing |
| 3477 | // code size and simplifying code. |
| 3478 | Constant *ConstantOp = 0; |
| 3479 | const Type *CastSrcTy = 0; |
| 3480 | if (isa<CastInst>(FirstInst)) { |
| 3481 | CastSrcTy = FirstInst->getOperand(0)->getType(); |
| 3482 | } else if (isa<BinaryOperator>(FirstInst) || isa<ShiftInst>(FirstInst)) { |
| 3483 | // Can fold binop or shift if the RHS is a constant. |
| 3484 | ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1)); |
| 3485 | if (ConstantOp == 0) return 0; |
| 3486 | } else { |
| 3487 | return 0; // Cannot fold this operation. |
| 3488 | } |
| 3489 | |
| 3490 | // Check to see if all arguments are the same operation. |
| 3491 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 3492 | if (!isa<Instruction>(PN.getIncomingValue(i))) return 0; |
| 3493 | Instruction *I = cast<Instruction>(PN.getIncomingValue(i)); |
| 3494 | if (!I->hasOneUse() || I->getOpcode() != FirstInst->getOpcode()) |
| 3495 | return 0; |
| 3496 | if (CastSrcTy) { |
| 3497 | if (I->getOperand(0)->getType() != CastSrcTy) |
| 3498 | return 0; // Cast operation must match. |
| 3499 | } else if (I->getOperand(1) != ConstantOp) { |
| 3500 | return 0; |
| 3501 | } |
| 3502 | } |
| 3503 | |
| 3504 | // Okay, they are all the same operation. Create a new PHI node of the |
| 3505 | // correct type, and PHI together all of the LHS's of the instructions. |
| 3506 | PHINode *NewPN = new PHINode(FirstInst->getOperand(0)->getType(), |
| 3507 | PN.getName()+".in"); |
| 3508 | NewPN->op_reserve(PN.getNumOperands()); |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 3509 | |
| 3510 | Value *InVal = FirstInst->getOperand(0); |
| 3511 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 3512 | |
| 3513 | // Add all operands to the new PHI. |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 3514 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 3515 | Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 3516 | if (NewInVal != InVal) |
| 3517 | InVal = 0; |
| 3518 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); |
| 3519 | } |
| 3520 | |
| 3521 | Value *PhiVal; |
| 3522 | if (InVal) { |
| 3523 | // The new PHI unions all of the same values together. This is really |
| 3524 | // common, so we handle it intelligently here for compile-time speed. |
| 3525 | PhiVal = InVal; |
| 3526 | delete NewPN; |
| 3527 | } else { |
| 3528 | InsertNewInstBefore(NewPN, PN); |
| 3529 | PhiVal = NewPN; |
| 3530 | } |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 3531 | |
| 3532 | // Insert and return the new operation. |
| 3533 | if (isa<CastInst>(FirstInst)) |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 3534 | return new CastInst(PhiVal, PN.getType()); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 3535 | else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 3536 | return BinaryOperator::create(BinOp->getOpcode(), PhiVal, ConstantOp); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 3537 | else |
| 3538 | return new ShiftInst(cast<ShiftInst>(FirstInst)->getOpcode(), |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 3539 | PhiVal, ConstantOp); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 3540 | } |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3541 | |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 3542 | // PHINode simplification |
| 3543 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3544 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Chris Lattner | e29d634 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 3545 | if (Value *V = hasConstantValue(&PN)) { |
| 3546 | // If V is an instruction, we have to be certain that it dominates PN. |
| 3547 | // However, because we don't have dom info, we can't do a perfect job. |
| 3548 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 3549 | // We know that the instruction dominates the PHI if there are no undef |
| 3550 | // values coming in. |
Chris Lattner | 3b92f17 | 2004-10-18 01:48:31 +0000 | [diff] [blame] | 3551 | if (I->getParent() != &I->getParent()->getParent()->front() || |
| 3552 | isa<InvokeInst>(I)) |
Chris Lattner | 107c15c | 2004-10-17 21:31:34 +0000 | [diff] [blame] | 3553 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 3554 | if (isa<UndefValue>(PN.getIncomingValue(i))) { |
| 3555 | V = 0; |
| 3556 | break; |
| 3557 | } |
Chris Lattner | e29d634 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 3558 | } |
| 3559 | |
| 3560 | if (V) |
| 3561 | return ReplaceInstUsesWith(PN, V); |
| 3562 | } |
Chris Lattner | 4db2d22 | 2004-02-16 05:07:08 +0000 | [diff] [blame] | 3563 | |
| 3564 | // If the only user of this instruction is a cast instruction, and all of the |
| 3565 | // incoming values are constants, change this PHI to merge together the casted |
| 3566 | // constants. |
| 3567 | if (PN.hasOneUse()) |
| 3568 | if (CastInst *CI = dyn_cast<CastInst>(PN.use_back())) |
| 3569 | if (CI->getType() != PN.getType()) { // noop casts will be folded |
| 3570 | bool AllConstant = true; |
| 3571 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 3572 | if (!isa<Constant>(PN.getIncomingValue(i))) { |
| 3573 | AllConstant = false; |
| 3574 | break; |
| 3575 | } |
| 3576 | if (AllConstant) { |
| 3577 | // Make a new PHI with all casted values. |
| 3578 | PHINode *New = new PHINode(CI->getType(), PN.getName(), &PN); |
| 3579 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 3580 | Constant *OldArg = cast<Constant>(PN.getIncomingValue(i)); |
| 3581 | New->addIncoming(ConstantExpr::getCast(OldArg, New->getType()), |
| 3582 | PN.getIncomingBlock(i)); |
| 3583 | } |
| 3584 | |
| 3585 | // Update the cast instruction. |
| 3586 | CI->setOperand(0, New); |
| 3587 | WorkList.push_back(CI); // revisit the cast instruction to fold. |
| 3588 | WorkList.push_back(New); // Make sure to revisit the new Phi |
| 3589 | return &PN; // PN is now dead! |
| 3590 | } |
| 3591 | } |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 3592 | |
| 3593 | // If all PHI operands are the same operation, pull them through the PHI, |
| 3594 | // reducing code size. |
| 3595 | if (isa<Instruction>(PN.getIncomingValue(0)) && |
| 3596 | PN.getIncomingValue(0)->hasOneUse()) |
| 3597 | if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) |
| 3598 | return Result; |
| 3599 | |
| 3600 | |
Chris Lattner | 91daeb5 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 3601 | return 0; |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 3602 | } |
| 3603 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 3604 | static Value *InsertSignExtendToPtrTy(Value *V, const Type *DTy, |
| 3605 | Instruction *InsertPoint, |
| 3606 | InstCombiner *IC) { |
| 3607 | unsigned PS = IC->getTargetData().getPointerSize(); |
| 3608 | const Type *VTy = V->getType(); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 3609 | if (!VTy->isSigned() && VTy->getPrimitiveSize() < PS) |
| 3610 | // We must insert a cast to ensure we sign-extend. |
| 3611 | V = IC->InsertNewInstBefore(new CastInst(V, VTy->getSignedVersion(), |
| 3612 | V->getName()), *InsertPoint); |
| 3613 | return IC->InsertNewInstBefore(new CastInst(V, DTy, V->getName()), |
| 3614 | *InsertPoint); |
| 3615 | } |
| 3616 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3617 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3618 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3619 | Value *PtrOp = GEP.getOperand(0); |
Chris Lattner | 471bd76 | 2003-05-22 19:07:21 +0000 | [diff] [blame] | 3620 | // Is it 'getelementptr %P, long 0' or 'getelementptr %P' |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3621 | // If so, eliminate the noop. |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 3622 | if (GEP.getNumOperands() == 1) |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3623 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 3624 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3625 | if (isa<UndefValue>(GEP.getOperand(0))) |
| 3626 | return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType())); |
| 3627 | |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 3628 | bool HasZeroPointerIndex = false; |
| 3629 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) |
| 3630 | HasZeroPointerIndex = C->isNullValue(); |
| 3631 | |
| 3632 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3633 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3634 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 3635 | // Eliminate unneeded casts for indices. |
| 3636 | bool MadeChange = false; |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 3637 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 3638 | for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI) |
| 3639 | if (isa<SequentialType>(*GTI)) { |
| 3640 | if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) { |
| 3641 | Value *Src = CI->getOperand(0); |
| 3642 | const Type *SrcTy = Src->getType(); |
| 3643 | const Type *DestTy = CI->getType(); |
| 3644 | if (Src->getType()->isInteger()) { |
| 3645 | if (SrcTy->getPrimitiveSize() == DestTy->getPrimitiveSize()) { |
| 3646 | // We can always eliminate a cast from ulong or long to the other. |
| 3647 | // We can always eliminate a cast from uint to int or the other on |
| 3648 | // 32-bit pointer platforms. |
| 3649 | if (DestTy->getPrimitiveSize() >= TD->getPointerSize()) { |
| 3650 | MadeChange = true; |
| 3651 | GEP.setOperand(i, Src); |
| 3652 | } |
| 3653 | } else if (SrcTy->getPrimitiveSize() < DestTy->getPrimitiveSize() && |
| 3654 | SrcTy->getPrimitiveSize() == 4) { |
| 3655 | // We can always eliminate a cast from int to [u]long. We can |
| 3656 | // eliminate a cast from uint to [u]long iff the target is a 32-bit |
| 3657 | // pointer target. |
| 3658 | if (SrcTy->isSigned() || |
| 3659 | SrcTy->getPrimitiveSize() >= TD->getPointerSize()) { |
| 3660 | MadeChange = true; |
| 3661 | GEP.setOperand(i, Src); |
| 3662 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 3663 | } |
| 3664 | } |
| 3665 | } |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 3666 | // If we are using a wider index than needed for this platform, shrink it |
| 3667 | // to what we need. If the incoming value needs a cast instruction, |
| 3668 | // insert it. This explicit cast can make subsequent optimizations more |
| 3669 | // obvious. |
| 3670 | Value *Op = GEP.getOperand(i); |
| 3671 | if (Op->getType()->getPrimitiveSize() > TD->getPointerSize()) |
Chris Lattner | 1e9ac1a | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 3672 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | 44d0b95 | 2004-07-20 01:48:15 +0000 | [diff] [blame] | 3673 | GEP.setOperand(i, ConstantExpr::getCast(C, |
| 3674 | TD->getIntPtrType()->getSignedVersion())); |
Chris Lattner | 1e9ac1a | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 3675 | MadeChange = true; |
| 3676 | } else { |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 3677 | Op = InsertNewInstBefore(new CastInst(Op, TD->getIntPtrType(), |
| 3678 | Op->getName()), GEP); |
| 3679 | GEP.setOperand(i, Op); |
| 3680 | MadeChange = true; |
| 3681 | } |
Chris Lattner | 44d0b95 | 2004-07-20 01:48:15 +0000 | [diff] [blame] | 3682 | |
| 3683 | // If this is a constant idx, make sure to canonicalize it to be a signed |
| 3684 | // operand, otherwise CSE and other optimizations are pessimized. |
| 3685 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op)) { |
| 3686 | GEP.setOperand(i, ConstantExpr::getCast(CUI, |
| 3687 | CUI->getType()->getSignedVersion())); |
| 3688 | MadeChange = true; |
| 3689 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 3690 | } |
| 3691 | if (MadeChange) return &GEP; |
| 3692 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 3693 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 3694 | // is a getelementptr instruction, combine the indices of the two |
| 3695 | // getelementptr instructions into a single instruction. |
| 3696 | // |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 3697 | std::vector<Value*> SrcGEPOperands; |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3698 | if (GetElementPtrInst *Src = dyn_cast<GetElementPtrInst>(PtrOp)) { |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 3699 | SrcGEPOperands.assign(Src->op_begin(), Src->op_end()); |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3700 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(PtrOp)) { |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 3701 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 3702 | SrcGEPOperands.assign(CE->op_begin(), CE->op_end()); |
| 3703 | } |
| 3704 | |
| 3705 | if (!SrcGEPOperands.empty()) { |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3706 | // Note that if our source is a gep chain itself that we wait for that |
| 3707 | // chain to be resolved before we perform this transformation. This |
| 3708 | // avoids us creating a TON of code in some cases. |
| 3709 | // |
| 3710 | if (isa<GetElementPtrInst>(SrcGEPOperands[0]) && |
| 3711 | cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2) |
| 3712 | return 0; // Wait until our source is folded to completion. |
| 3713 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 3714 | std::vector<Value *> Indices; |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3715 | |
| 3716 | // Find out whether the last index in the source GEP is a sequential idx. |
| 3717 | bool EndsWithSequential = false; |
| 3718 | for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)), |
| 3719 | E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I) |
Chris Lattner | 8ec5f88 | 2004-05-08 22:41:42 +0000 | [diff] [blame] | 3720 | EndsWithSequential = !isa<StructType>(*I); |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 3721 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 3722 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3723 | if (EndsWithSequential) { |
Chris Lattner | 235af56 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 3724 | // Replace: gep (gep %P, long B), long A, ... |
| 3725 | // With: T = long A+B; gep %P, T, ... |
| 3726 | // |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3727 | Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 3728 | if (SO1 == Constant::getNullValue(SO1->getType())) { |
| 3729 | Sum = GO1; |
| 3730 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { |
| 3731 | Sum = SO1; |
| 3732 | } else { |
| 3733 | // If they aren't the same type, convert both to an integer of the |
| 3734 | // target's pointer size. |
| 3735 | if (SO1->getType() != GO1->getType()) { |
| 3736 | if (Constant *SO1C = dyn_cast<Constant>(SO1)) { |
| 3737 | SO1 = ConstantExpr::getCast(SO1C, GO1->getType()); |
| 3738 | } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) { |
| 3739 | GO1 = ConstantExpr::getCast(GO1C, SO1->getType()); |
| 3740 | } else { |
| 3741 | unsigned PS = TD->getPointerSize(); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 3742 | if (SO1->getType()->getPrimitiveSize() == PS) { |
| 3743 | // Convert GO1 to SO1's type. |
| 3744 | GO1 = InsertSignExtendToPtrTy(GO1, SO1->getType(), &GEP, this); |
| 3745 | |
| 3746 | } else if (GO1->getType()->getPrimitiveSize() == PS) { |
| 3747 | // Convert SO1 to GO1's type. |
| 3748 | SO1 = InsertSignExtendToPtrTy(SO1, GO1->getType(), &GEP, this); |
| 3749 | } else { |
| 3750 | const Type *PT = TD->getIntPtrType(); |
| 3751 | SO1 = InsertSignExtendToPtrTy(SO1, PT, &GEP, this); |
| 3752 | GO1 = InsertSignExtendToPtrTy(GO1, PT, &GEP, this); |
| 3753 | } |
| 3754 | } |
| 3755 | } |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3756 | if (isa<Constant>(SO1) && isa<Constant>(GO1)) |
| 3757 | Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1)); |
| 3758 | else { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3759 | Sum = BinaryOperator::createAdd(SO1, GO1, PtrOp->getName()+".sum"); |
| 3760 | InsertNewInstBefore(cast<Instruction>(Sum), GEP); |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3761 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 3762 | } |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3763 | |
| 3764 | // Recycle the GEP we already have if possible. |
| 3765 | if (SrcGEPOperands.size() == 2) { |
| 3766 | GEP.setOperand(0, SrcGEPOperands[0]); |
| 3767 | GEP.setOperand(1, Sum); |
| 3768 | return &GEP; |
| 3769 | } else { |
| 3770 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 3771 | SrcGEPOperands.end()-1); |
| 3772 | Indices.push_back(Sum); |
| 3773 | Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end()); |
| 3774 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 3775 | } else if (isa<Constant>(*GEP.idx_begin()) && |
| 3776 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 3777 | SrcGEPOperands.size() != 1) { |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 3778 | // 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] | 3779 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 3780 | SrcGEPOperands.end()); |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 3781 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
| 3782 | } |
| 3783 | |
| 3784 | if (!Indices.empty()) |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 3785 | return new GetElementPtrInst(SrcGEPOperands[0], Indices, GEP.getName()); |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 3786 | |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3787 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) { |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 3788 | // GEP of global variable. If all of the indices for this GEP are |
| 3789 | // constants, we can promote this to a constexpr instead of an instruction. |
| 3790 | |
| 3791 | // Scan for nonconstants... |
| 3792 | std::vector<Constant*> Indices; |
| 3793 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
| 3794 | for (; I != E && isa<Constant>(*I); ++I) |
| 3795 | Indices.push_back(cast<Constant>(*I)); |
| 3796 | |
| 3797 | if (I == E) { // If they are all constants... |
Chris Lattner | f3edc49 | 2004-07-18 18:59:44 +0000 | [diff] [blame] | 3798 | Constant *CE = ConstantExpr::getGetElementPtr(GV, Indices); |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 3799 | |
| 3800 | // Replace all uses of the GEP with the new constexpr... |
| 3801 | return ReplaceInstUsesWith(GEP, CE); |
| 3802 | } |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3803 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(PtrOp)) { |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 3804 | if (CE->getOpcode() == Instruction::Cast) { |
| 3805 | if (HasZeroPointerIndex) { |
| 3806 | // transform: GEP (cast [10 x ubyte]* X to [0 x ubyte]*), long 0, ... |
| 3807 | // into : GEP [10 x ubyte]* X, long 0, ... |
| 3808 | // |
| 3809 | // This occurs when the program declares an array extern like "int X[];" |
| 3810 | // |
| 3811 | Constant *X = CE->getOperand(0); |
| 3812 | const PointerType *CPTy = cast<PointerType>(CE->getType()); |
| 3813 | if (const PointerType *XTy = dyn_cast<PointerType>(X->getType())) |
| 3814 | if (const ArrayType *XATy = |
| 3815 | dyn_cast<ArrayType>(XTy->getElementType())) |
| 3816 | if (const ArrayType *CATy = |
| 3817 | dyn_cast<ArrayType>(CPTy->getElementType())) |
| 3818 | if (CATy->getElementType() == XATy->getElementType()) { |
| 3819 | // At this point, we know that the cast source type is a pointer |
| 3820 | // to an array of the same type as the destination pointer |
| 3821 | // array. Because the array type is never stepped over (there |
| 3822 | // is a leading zero) we can fold the cast into this GEP. |
| 3823 | GEP.setOperand(0, X); |
| 3824 | return &GEP; |
| 3825 | } |
Chris Lattner | 14f3cdc | 2004-11-27 17:55:46 +0000 | [diff] [blame] | 3826 | } else if (GEP.getNumOperands() == 2) { |
| 3827 | // Transform things like: |
| 3828 | // %t = getelementptr ubyte* cast ([2 x sbyte]* %str to ubyte*), uint %V |
| 3829 | // into: %t1 = getelementptr [2 x sbyte*]* %str, int 0, uint %V; cast |
| 3830 | Constant *X = CE->getOperand(0); |
| 3831 | const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); |
| 3832 | const Type *ResElTy =cast<PointerType>(CE->getType())->getElementType(); |
| 3833 | if (isa<ArrayType>(SrcElTy) && |
| 3834 | TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()) == |
| 3835 | TD->getTypeSize(ResElTy)) { |
| 3836 | Value *V = InsertNewInstBefore( |
| 3837 | new GetElementPtrInst(X, Constant::getNullValue(Type::IntTy), |
| 3838 | GEP.getOperand(1), GEP.getName()), GEP); |
| 3839 | return new CastInst(V, GEP.getType()); |
| 3840 | } |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 3841 | } |
| 3842 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 3843 | } |
| 3844 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 3845 | return 0; |
| 3846 | } |
| 3847 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 3848 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
| 3849 | // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1 |
| 3850 | if (AI.isArrayAllocation()) // Check C != 1 |
| 3851 | if (const ConstantUInt *C = dyn_cast<ConstantUInt>(AI.getArraySize())) { |
| 3852 | const Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getValue()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 3853 | AllocationInst *New = 0; |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 3854 | |
| 3855 | // Create and insert the replacement instruction... |
| 3856 | if (isa<MallocInst>(AI)) |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 3857 | New = new MallocInst(NewTy, 0, AI.getName()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 3858 | else { |
| 3859 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 3860 | New = new AllocaInst(NewTy, 0, AI.getName()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 3861 | } |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 3862 | |
| 3863 | InsertNewInstBefore(New, AI); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 3864 | |
| 3865 | // Scan to the end of the allocation instructions, to skip over a block of |
| 3866 | // allocas if possible... |
| 3867 | // |
| 3868 | BasicBlock::iterator It = New; |
| 3869 | while (isa<AllocationInst>(*It)) ++It; |
| 3870 | |
| 3871 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 3872 | // insert our getelementptr instruction... |
| 3873 | // |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 3874 | std::vector<Value*> Idx(2, Constant::getNullValue(Type::IntTy)); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 3875 | Value *V = new GetElementPtrInst(New, Idx, New->getName()+".sub", It); |
| 3876 | |
| 3877 | // Now make everything use the getelementptr instead of the original |
| 3878 | // allocation. |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 3879 | return ReplaceInstUsesWith(AI, V); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3880 | } else if (isa<UndefValue>(AI.getArraySize())) { |
| 3881 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 3882 | } |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 3883 | |
| 3884 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. |
| 3885 | // Note that we only do this for alloca's, because malloc should allocate and |
| 3886 | // return a unique pointer, even for a zero byte allocation. |
Chris Lattner | 49df6ce | 2004-07-02 22:55:47 +0000 | [diff] [blame] | 3887 | if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() && |
| 3888 | TD->getTypeSize(AI.getAllocatedType()) == 0) |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 3889 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
| 3890 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 3891 | return 0; |
| 3892 | } |
| 3893 | |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 3894 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { |
| 3895 | Value *Op = FI.getOperand(0); |
| 3896 | |
| 3897 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X |
| 3898 | if (CastInst *CI = dyn_cast<CastInst>(Op)) |
| 3899 | if (isa<PointerType>(CI->getOperand(0)->getType())) { |
| 3900 | FI.setOperand(0, CI->getOperand(0)); |
| 3901 | return &FI; |
| 3902 | } |
| 3903 | |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 3904 | // free undef -> unreachable. |
| 3905 | if (isa<UndefValue>(Op)) { |
| 3906 | // Insert a new store to null because we cannot modify the CFG here. |
| 3907 | new StoreInst(ConstantBool::True, |
| 3908 | UndefValue::get(PointerType::get(Type::BoolTy)), &FI); |
| 3909 | return EraseInstFromFunction(FI); |
| 3910 | } |
| 3911 | |
Chris Lattner | f3a3660 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 3912 | // If we have 'free null' delete the instruction. This can happen in stl code |
| 3913 | // when lots of inlining happens. |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 3914 | if (isa<ConstantPointerNull>(Op)) |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 3915 | return EraseInstFromFunction(FI); |
Chris Lattner | f3a3660 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 3916 | |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 3917 | return 0; |
| 3918 | } |
| 3919 | |
| 3920 | |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 3921 | /// GetGEPGlobalInitializer - Given a constant, and a getelementptr |
| 3922 | /// constantexpr, return the constant value being addressed by the constant |
| 3923 | /// expression, or null if something is funny. |
| 3924 | /// |
| 3925 | static Constant *GetGEPGlobalInitializer(Constant *C, ConstantExpr *CE) { |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 3926 | if (CE->getOperand(1) != Constant::getNullValue(CE->getOperand(1)->getType())) |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 3927 | return 0; // Do not allow stepping over the value! |
| 3928 | |
| 3929 | // Loop over all of the operands, tracking down which value we are |
| 3930 | // addressing... |
Chris Lattner | ed79d8a | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 3931 | gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE); |
| 3932 | for (++I; I != E; ++I) |
| 3933 | if (const StructType *STy = dyn_cast<StructType>(*I)) { |
| 3934 | ConstantUInt *CU = cast<ConstantUInt>(I.getOperand()); |
| 3935 | assert(CU->getValue() < STy->getNumElements() && |
| 3936 | "Struct index out of range!"); |
| 3937 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { |
Alkis Evlogimenos | 8324372 | 2004-08-04 08:44:43 +0000 | [diff] [blame] | 3938 | C = CS->getOperand(CU->getValue()); |
Chris Lattner | ed79d8a | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 3939 | } else if (isa<ConstantAggregateZero>(C)) { |
| 3940 | C = Constant::getNullValue(STy->getElementType(CU->getValue())); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3941 | } else if (isa<UndefValue>(C)) { |
| 3942 | C = UndefValue::get(STy->getElementType(CU->getValue())); |
Chris Lattner | ed79d8a | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 3943 | } else { |
| 3944 | return 0; |
| 3945 | } |
| 3946 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand())) { |
| 3947 | const ArrayType *ATy = cast<ArrayType>(*I); |
| 3948 | if ((uint64_t)CI->getRawValue() >= ATy->getNumElements()) return 0; |
| 3949 | if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) |
Alkis Evlogimenos | 8324372 | 2004-08-04 08:44:43 +0000 | [diff] [blame] | 3950 | C = CA->getOperand(CI->getRawValue()); |
Chris Lattner | ed79d8a | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 3951 | else if (isa<ConstantAggregateZero>(C)) |
| 3952 | C = Constant::getNullValue(ATy->getElementType()); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3953 | else if (isa<UndefValue>(C)) |
| 3954 | C = UndefValue::get(ATy->getElementType()); |
Chris Lattner | ed79d8a | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 3955 | else |
| 3956 | return 0; |
| 3957 | } else { |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 3958 | return 0; |
Chris Lattner | ed79d8a | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 3959 | } |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 3960 | return C; |
| 3961 | } |
| 3962 | |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 3963 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI) { |
| 3964 | User *CI = cast<User>(LI.getOperand(0)); |
| 3965 | |
| 3966 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
| 3967 | if (const PointerType *SrcTy = |
| 3968 | dyn_cast<PointerType>(CI->getOperand(0)->getType())) { |
| 3969 | const Type *SrcPTy = SrcTy->getElementType(); |
| 3970 | if (SrcPTy->isSized() && DestPTy->isSized() && |
| 3971 | IC.getTargetData().getTypeSize(SrcPTy) == |
| 3972 | IC.getTargetData().getTypeSize(DestPTy) && |
| 3973 | (SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
| 3974 | (DestPTy->isInteger() || isa<PointerType>(DestPTy))) { |
| 3975 | // Okay, we are casting from one integer or pointer type to another of |
| 3976 | // the same size. Instead of casting the pointer before the load, cast |
| 3977 | // the result of the loaded value. |
| 3978 | Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CI->getOperand(0), |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 3979 | CI->getName(), |
| 3980 | LI.isVolatile()),LI); |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 3981 | // Now cast the result of the load. |
| 3982 | return new CastInst(NewLoad, LI.getType()); |
| 3983 | } |
| 3984 | } |
| 3985 | return 0; |
| 3986 | } |
| 3987 | |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 3988 | /// isSafeToLoadUnconditionally - Return true if we know that executing a load |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 3989 | /// from this value cannot trap. If it is not obviously safe to load from the |
| 3990 | /// specified pointer, we do a quick local scan of the basic block containing |
| 3991 | /// ScanFrom, to determine if the address is already accessed. |
| 3992 | static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) { |
| 3993 | // If it is an alloca or global variable, it is always safe to load from. |
| 3994 | if (isa<AllocaInst>(V) || isa<GlobalVariable>(V)) return true; |
| 3995 | |
| 3996 | // Otherwise, be a little bit agressive by scanning the local block where we |
| 3997 | // want to check to see if the pointer is already being loaded or stored |
Alkis Evlogimenos | d59cebf | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 3998 | // from/to. If so, the previous load or store would have already trapped, |
| 3999 | // so there is no harm doing an extra load (also, CSE will later eliminate |
| 4000 | // the load entirely). |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 4001 | BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); |
| 4002 | |
Alkis Evlogimenos | d59cebf | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 4003 | while (BBI != E) { |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 4004 | --BBI; |
| 4005 | |
| 4006 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 4007 | if (LI->getOperand(0) == V) return true; |
| 4008 | } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 4009 | if (SI->getOperand(1) == V) return true; |
| 4010 | |
Alkis Evlogimenos | d59cebf | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 4011 | } |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 4012 | return false; |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 4015 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 4016 | Value *Op = LI.getOperand(0); |
Chris Lattner | 7e8af38 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 4017 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4018 | if (Constant *C = dyn_cast<Constant>(Op)) { |
| 4019 | if ((C->isNullValue() || isa<UndefValue>(C)) && |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 4020 | !LI.isVolatile()) { // load null/undef -> undef |
| 4021 | // Insert a new store to null instruction before the load to indicate that |
| 4022 | // this code is not reachable. We do this instead of inserting an |
| 4023 | // unreachable instruction directly because we cannot modify the CFG. |
| 4024 | new StoreInst(UndefValue::get(LI.getType()), C, &LI); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4025 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 4026 | } |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 4027 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4028 | // Instcombine load (constant global) into the value loaded. |
| 4029 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) |
| 4030 | if (GV->isConstant() && !GV->isExternal()) |
| 4031 | return ReplaceInstUsesWith(LI, GV->getInitializer()); |
| 4032 | |
| 4033 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded. |
| 4034 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) |
| 4035 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 4036 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
| 4037 | if (GV->isConstant() && !GV->isExternal()) |
| 4038 | if (Constant *V = GetGEPGlobalInitializer(GV->getInitializer(), CE)) |
| 4039 | return ReplaceInstUsesWith(LI, V); |
| 4040 | } else if (CE->getOpcode() == Instruction::Cast) { |
| 4041 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 4042 | return Res; |
| 4043 | } |
| 4044 | } |
Chris Lattner | e228ee5 | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 4045 | |
| 4046 | // load (cast X) --> cast (load X) iff safe |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 4047 | if (CastInst *CI = dyn_cast<CastInst>(Op)) |
| 4048 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 4049 | return Res; |
Chris Lattner | e228ee5 | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 4050 | |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 4051 | if (!LI.isVolatile() && Op->hasOneUse()) { |
| 4052 | // Change select and PHI nodes to select values instead of addresses: this |
| 4053 | // helps alias analysis out a lot, allows many others simplifications, and |
| 4054 | // exposes redundancy in the code. |
| 4055 | // |
| 4056 | // Note that we cannot do the transformation unless we know that the |
| 4057 | // introduced loads cannot trap! Something like this is valid as long as |
| 4058 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 4059 | // but it would not be valid if we transformed it to load from null |
| 4060 | // unconditionally. |
| 4061 | // |
| 4062 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 4063 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 4064 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && |
| 4065 | isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 4066 | Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1), |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 4067 | SI->getOperand(1)->getName()+".val"), LI); |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 4068 | Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2), |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 4069 | SI->getOperand(2)->getName()+".val"), LI); |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 4070 | return new SelectInst(SI->getCondition(), V1, V2); |
| 4071 | } |
| 4072 | |
Chris Lattner | bdcf41a | 2004-09-23 15:46:00 +0000 | [diff] [blame] | 4073 | // load (select (cond, null, P)) -> load P |
| 4074 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) |
| 4075 | if (C->isNullValue()) { |
| 4076 | LI.setOperand(0, SI->getOperand(2)); |
| 4077 | return &LI; |
| 4078 | } |
| 4079 | |
| 4080 | // load (select (cond, P, null)) -> load P |
| 4081 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) |
| 4082 | if (C->isNullValue()) { |
| 4083 | LI.setOperand(0, SI->getOperand(1)); |
| 4084 | return &LI; |
| 4085 | } |
| 4086 | |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 4087 | } else if (PHINode *PN = dyn_cast<PHINode>(Op)) { |
| 4088 | // load (phi (&V1, &V2, &V3)) --> phi(load &V1, load &V2, load &V3) |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 4089 | bool Safe = PN->getParent() == LI.getParent(); |
| 4090 | |
| 4091 | // Scan all of the instructions between the PHI and the load to make |
| 4092 | // sure there are no instructions that might possibly alter the value |
| 4093 | // loaded from the PHI. |
| 4094 | if (Safe) { |
| 4095 | BasicBlock::iterator I = &LI; |
| 4096 | for (--I; !isa<PHINode>(I); --I) |
| 4097 | if (isa<StoreInst>(I) || isa<CallInst>(I)) { |
| 4098 | Safe = false; |
| 4099 | break; |
| 4100 | } |
| 4101 | } |
| 4102 | |
| 4103 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e && Safe; ++i) |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 4104 | if (!isSafeToLoadUnconditionally(PN->getIncomingValue(i), |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 4105 | PN->getIncomingBlock(i)->getTerminator())) |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 4106 | Safe = false; |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 4107 | |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 4108 | if (Safe) { |
| 4109 | // Create the PHI. |
| 4110 | PHINode *NewPN = new PHINode(LI.getType(), PN->getName()); |
| 4111 | InsertNewInstBefore(NewPN, *PN); |
| 4112 | std::map<BasicBlock*,Value*> LoadMap; // Don't insert duplicate loads |
| 4113 | |
| 4114 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 4115 | BasicBlock *BB = PN->getIncomingBlock(i); |
| 4116 | Value *&TheLoad = LoadMap[BB]; |
| 4117 | if (TheLoad == 0) { |
| 4118 | Value *InVal = PN->getIncomingValue(i); |
| 4119 | TheLoad = InsertNewInstBefore(new LoadInst(InVal, |
| 4120 | InVal->getName()+".val"), |
| 4121 | *BB->getTerminator()); |
| 4122 | } |
| 4123 | NewPN->addIncoming(TheLoad, BB); |
| 4124 | } |
| 4125 | return ReplaceInstUsesWith(LI, NewPN); |
| 4126 | } |
| 4127 | } |
| 4128 | } |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 4129 | return 0; |
| 4130 | } |
| 4131 | |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 4132 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 4133 | // 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] | 4134 | Value *X; |
| 4135 | BasicBlock *TrueDest; |
| 4136 | BasicBlock *FalseDest; |
| 4137 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && |
| 4138 | !isa<Constant>(X)) { |
| 4139 | // Swap Destinations and condition... |
| 4140 | BI.setCondition(X); |
| 4141 | BI.setSuccessor(0, FalseDest); |
| 4142 | BI.setSuccessor(1, TrueDest); |
| 4143 | return &BI; |
| 4144 | } |
| 4145 | |
| 4146 | // Cannonicalize setne -> seteq |
| 4147 | Instruction::BinaryOps Op; Value *Y; |
| 4148 | if (match(&BI, m_Br(m_SetCond(Op, m_Value(X), m_Value(Y)), |
| 4149 | TrueDest, FalseDest))) |
| 4150 | if ((Op == Instruction::SetNE || Op == Instruction::SetLE || |
| 4151 | Op == Instruction::SetGE) && BI.getCondition()->hasOneUse()) { |
| 4152 | SetCondInst *I = cast<SetCondInst>(BI.getCondition()); |
| 4153 | std::string Name = I->getName(); I->setName(""); |
| 4154 | Instruction::BinaryOps NewOpcode = SetCondInst::getInverseCondition(Op); |
| 4155 | Value *NewSCC = BinaryOperator::create(NewOpcode, X, Y, Name, I); |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 4156 | // Swap Destinations and condition... |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4157 | BI.setCondition(NewSCC); |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 4158 | BI.setSuccessor(0, FalseDest); |
| 4159 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4160 | removeFromWorkList(I); |
| 4161 | I->getParent()->getInstList().erase(I); |
| 4162 | WorkList.push_back(cast<Instruction>(NewSCC)); |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 4163 | return &BI; |
| 4164 | } |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4165 | |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 4166 | return 0; |
| 4167 | } |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 4168 | |
Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 4169 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
| 4170 | Value *Cond = SI.getCondition(); |
| 4171 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { |
| 4172 | if (I->getOpcode() == Instruction::Add) |
| 4173 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 4174 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' |
| 4175 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4176 | SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), |
Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 4177 | AddRHS)); |
| 4178 | SI.setOperand(0, I->getOperand(0)); |
| 4179 | WorkList.push_back(I); |
| 4180 | return &SI; |
| 4181 | } |
| 4182 | } |
| 4183 | return 0; |
| 4184 | } |
| 4185 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 4186 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 4187 | void InstCombiner::removeFromWorkList(Instruction *I) { |
| 4188 | WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I), |
| 4189 | WorkList.end()); |
| 4190 | } |
| 4191 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4192 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 4193 | bool Changed = false; |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 4194 | TD = &getAnalysis<TargetData>(); |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 4195 | |
Chris Lattner | b643a9e | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 4196 | for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) |
| 4197 | WorkList.push_back(&*i); |
Chris Lattner | 2d3a7a6 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 4198 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 4199 | |
| 4200 | while (!WorkList.empty()) { |
| 4201 | Instruction *I = WorkList.back(); // Get an instruction from the worklist |
| 4202 | WorkList.pop_back(); |
| 4203 | |
Misha Brukman | 632df28 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 4204 | // Check to see if we can DCE or ConstantPropagate the instruction... |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 4205 | // Check to see if we can DIE the instruction... |
| 4206 | if (isInstructionTriviallyDead(I)) { |
| 4207 | // Add operands to the worklist... |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 4208 | if (I->getNumOperands() < 4) |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 4209 | AddUsesToWorkList(*I); |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 4210 | ++NumDeadInst; |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 4211 | |
| 4212 | I->getParent()->getInstList().erase(I); |
| 4213 | removeFromWorkList(I); |
| 4214 | continue; |
| 4215 | } |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 4216 | |
Misha Brukman | 632df28 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 4217 | // Instruction isn't dead, see if we can constant propagate it... |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 4218 | if (Constant *C = ConstantFoldInstruction(I)) { |
Alkis Evlogimenos | a1291a0 | 2004-12-08 23:10:30 +0000 | [diff] [blame^] | 4219 | Value* Ptr = I->getOperand(0); |
Chris Lattner | 6580e09 | 2004-10-16 19:44:59 +0000 | [diff] [blame] | 4220 | if (isa<GetElementPtrInst>(I) && |
Alkis Evlogimenos | a1291a0 | 2004-12-08 23:10:30 +0000 | [diff] [blame^] | 4221 | cast<Constant>(Ptr)->isNullValue() && |
| 4222 | !isa<ConstantPointerNull>(C) && |
| 4223 | cast<PointerType>(Ptr->getType())->getElementType()->isSized()) { |
Chris Lattner | 6580e09 | 2004-10-16 19:44:59 +0000 | [diff] [blame] | 4224 | // If this is a constant expr gep that is effectively computing an |
| 4225 | // "offsetof", fold it into 'cast int X to T*' instead of 'gep 0, 0, 12' |
| 4226 | bool isFoldableGEP = true; |
| 4227 | for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i) |
| 4228 | if (!isa<ConstantInt>(I->getOperand(i))) |
| 4229 | isFoldableGEP = false; |
| 4230 | if (isFoldableGEP) { |
Alkis Evlogimenos | a1291a0 | 2004-12-08 23:10:30 +0000 | [diff] [blame^] | 4231 | uint64_t Offset = TD->getIndexedOffset(Ptr->getType(), |
Chris Lattner | 6580e09 | 2004-10-16 19:44:59 +0000 | [diff] [blame] | 4232 | std::vector<Value*>(I->op_begin()+1, I->op_end())); |
| 4233 | C = ConstantUInt::get(Type::ULongTy, Offset); |
Chris Lattner | 684c5c6 | 2004-10-16 19:46:33 +0000 | [diff] [blame] | 4234 | C = ConstantExpr::getCast(C, TD->getIntPtrType()); |
Chris Lattner | 6580e09 | 2004-10-16 19:44:59 +0000 | [diff] [blame] | 4235 | C = ConstantExpr::getCast(C, I->getType()); |
| 4236 | } |
| 4237 | } |
| 4238 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 4239 | // Add operands to the worklist... |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 4240 | AddUsesToWorkList(*I); |
Chris Lattner | c6509f4 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 4241 | ReplaceInstUsesWith(*I, C); |
| 4242 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 4243 | ++NumConstProp; |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 4244 | I->getParent()->getInstList().erase(I); |
Chris Lattner | 800aaaf | 2003-10-07 15:17:02 +0000 | [diff] [blame] | 4245 | removeFromWorkList(I); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 4246 | continue; |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 4247 | } |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 4248 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 4249 | // Now that we have an instruction, try combining it to simplify it... |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 4250 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 4251 | ++NumCombined; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 4252 | // Should we replace the old instruction with a new one? |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 4253 | if (Result != I) { |
Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 4254 | DEBUG(std::cerr << "IC: Old = " << *I |
| 4255 | << " New = " << *Result); |
| 4256 | |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 4257 | // Everything uses the new instruction now. |
| 4258 | I->replaceAllUsesWith(Result); |
| 4259 | |
| 4260 | // Push the new instruction and any users onto the worklist. |
| 4261 | WorkList.push_back(Result); |
| 4262 | AddUsersToWorkList(*Result); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 4263 | |
| 4264 | // Move the name to the new instruction first... |
| 4265 | std::string OldName = I->getName(); I->setName(""); |
Chris Lattner | 950fc78 | 2003-10-07 22:58:41 +0000 | [diff] [blame] | 4266 | Result->setName(OldName); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 4267 | |
| 4268 | // Insert the new instruction into the basic block... |
| 4269 | BasicBlock *InstParent = I->getParent(); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4270 | BasicBlock::iterator InsertPos = I; |
| 4271 | |
| 4272 | if (!isa<PHINode>(Result)) // If combining a PHI, don't insert |
| 4273 | while (isa<PHINode>(InsertPos)) // middle of a block of PHIs. |
| 4274 | ++InsertPos; |
| 4275 | |
| 4276 | InstParent->getInstList().insert(InsertPos, Result); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 4277 | |
Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 4278 | // Make sure that we reprocess all operands now that we reduced their |
| 4279 | // use counts. |
Chris Lattner | b643a9e | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 4280 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 4281 | if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i))) |
| 4282 | WorkList.push_back(OpI); |
| 4283 | |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 4284 | // Instructions can end up on the worklist more than once. Make sure |
| 4285 | // we do not process an instruction that has been deleted. |
| 4286 | removeFromWorkList(I); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 4287 | |
| 4288 | // Erase the old instruction. |
| 4289 | InstParent->getInstList().erase(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4290 | } else { |
Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 4291 | DEBUG(std::cerr << "IC: MOD = " << *I); |
| 4292 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 4293 | // If the instruction was modified, it's possible that it is now dead. |
| 4294 | // if so, remove it. |
Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 4295 | if (isInstructionTriviallyDead(I)) { |
| 4296 | // Make sure we process all operands now that we are reducing their |
| 4297 | // use counts. |
| 4298 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 4299 | if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i))) |
| 4300 | WorkList.push_back(OpI); |
| 4301 | |
| 4302 | // Instructions may end up in the worklist more than once. Erase all |
| 4303 | // occurrances of this instruction. |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 4304 | removeFromWorkList(I); |
Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 4305 | I->getParent()->getInstList().erase(I); |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 4306 | } else { |
| 4307 | WorkList.push_back(Result); |
| 4308 | AddUsersToWorkList(*Result); |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 4309 | } |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 4310 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 4311 | Changed = true; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 4312 | } |
| 4313 | } |
| 4314 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 4315 | return Changed; |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 4316 | } |
| 4317 | |
Brian Gaeke | 38b79e8 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 4318 | FunctionPass *llvm::createInstructionCombiningPass() { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 4319 | return new InstCombiner(); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 4320 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 4321 | |