Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1 | //===- InstructionCombining.cpp - Combine multiple instructions -----------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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" |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Debug.h" |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 47 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 48 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 49 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 50 | #include "llvm/Support/PatternMatch.h" |
Chris Lattner | 4ed40f7 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 51 | #include "llvm/ADT/DepthFirstIterator.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 52 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 53 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 54 | #include <algorithm> |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 55 | using namespace llvm; |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 56 | using namespace llvm::PatternMatch; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 57 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 58 | namespace { |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 59 | Statistic<> NumCombined ("instcombine", "Number of insts combined"); |
| 60 | Statistic<> NumConstProp("instcombine", "Number of constant folds"); |
| 61 | Statistic<> NumDeadInst ("instcombine", "Number of dead inst eliminated"); |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 62 | Statistic<> NumSunkInst ("instcombine", "Number of instructions sunk"); |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 63 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 64 | class InstCombiner : public FunctionPass, |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 65 | public InstVisitor<InstCombiner, Instruction*> { |
| 66 | // Worklist of all of the instructions that need to be simplified. |
| 67 | std::vector<Instruction*> WorkList; |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 68 | TargetData *TD; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 70 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
| 71 | /// the instruction to the work lists because they might get more simplified |
| 72 | /// now. |
| 73 | /// |
| 74 | void AddUsersToWorkList(Instruction &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 75 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 76 | UI != UE; ++UI) |
| 77 | WorkList.push_back(cast<Instruction>(*UI)); |
| 78 | } |
| 79 | |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 80 | /// AddUsesToWorkList - When an instruction is simplified, add operands to |
| 81 | /// the work lists because they might get more simplified now. |
| 82 | /// |
| 83 | void AddUsesToWorkList(Instruction &I) { |
| 84 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 85 | if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) |
| 86 | WorkList.push_back(Op); |
| 87 | } |
| 88 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 89 | // removeFromWorkList - remove all instances of I from the worklist. |
| 90 | void removeFromWorkList(Instruction *I); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 91 | public: |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 92 | virtual bool runOnFunction(Function &F); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 93 | |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 94 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 95 | AU.addRequired<TargetData>(); |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 96 | AU.setPreservesCFG(); |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 99 | TargetData &getTargetData() const { return *TD; } |
| 100 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 101 | // Visitation implementation - Implement instruction combining for different |
| 102 | // instruction types. The semantics are as follows: |
| 103 | // Return Value: |
| 104 | // null - No change was made |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 105 | // 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] | 106 | // otherwise - Change was made, replace I with returned instruction |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 107 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 108 | Instruction *visitAdd(BinaryOperator &I); |
| 109 | Instruction *visitSub(BinaryOperator &I); |
| 110 | Instruction *visitMul(BinaryOperator &I); |
| 111 | Instruction *visitDiv(BinaryOperator &I); |
| 112 | Instruction *visitRem(BinaryOperator &I); |
| 113 | Instruction *visitAnd(BinaryOperator &I); |
| 114 | Instruction *visitOr (BinaryOperator &I); |
| 115 | Instruction *visitXor(BinaryOperator &I); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 116 | Instruction *visitSetCondInst(SetCondInst &I); |
| 117 | Instruction *visitSetCondInstWithCastAndCast(SetCondInst &SCI); |
| 118 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 119 | Instruction *FoldGEPSetCC(User *GEPLHS, Value *RHS, |
| 120 | Instruction::BinaryOps Cond, Instruction &I); |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 121 | Instruction *visitShiftInst(ShiftInst &I); |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 122 | Instruction *FoldShiftByConstant(Value *Op0, ConstantUInt *Op1, |
| 123 | ShiftInst &I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 124 | Instruction *visitCastInst(CastInst &CI); |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 125 | Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 126 | Instruction *FI); |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 127 | Instruction *visitSelectInst(SelectInst &CI); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 128 | Instruction *visitCallInst(CallInst &CI); |
| 129 | Instruction *visitInvokeInst(InvokeInst &II); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 130 | Instruction *visitPHINode(PHINode &PN); |
| 131 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 132 | Instruction *visitAllocationInst(AllocationInst &AI); |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 133 | Instruction *visitFreeInst(FreeInst &FI); |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 134 | Instruction *visitLoadInst(LoadInst &LI); |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 135 | Instruction *visitStoreInst(StoreInst &SI); |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 136 | Instruction *visitBranchInst(BranchInst &BI); |
Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 137 | Instruction *visitSwitchInst(SwitchInst &SI); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 138 | |
| 139 | // visitInstruction - Specify what to return for unhandled instructions... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 140 | Instruction *visitInstruction(Instruction &I) { return 0; } |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 142 | private: |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 143 | Instruction *visitCallSite(CallSite CS); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 144 | bool transformConstExprCastCall(CallSite CS); |
| 145 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 146 | public: |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 147 | // InsertNewInstBefore - insert an instruction New before instruction Old |
| 148 | // in the program. Add the new instruction to the worklist. |
| 149 | // |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 150 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Chris Lattner | 65217ff | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 151 | assert(New && New->getParent() == 0 && |
| 152 | "New instruction already inserted into a basic block!"); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 153 | BasicBlock *BB = Old.getParent(); |
| 154 | BB->getInstList().insert(&Old, New); // Insert inst |
| 155 | WorkList.push_back(New); // Add to worklist |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 156 | return New; |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 159 | /// InsertCastBefore - Insert a cast of V to TY before the instruction POS. |
| 160 | /// This also adds the cast to the worklist. Finally, this returns the |
| 161 | /// cast. |
| 162 | Value *InsertCastBefore(Value *V, const Type *Ty, Instruction &Pos) { |
| 163 | if (V->getType() == Ty) return V; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 164 | |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 165 | Instruction *C = new CastInst(V, Ty, V->getName(), &Pos); |
| 166 | WorkList.push_back(C); |
| 167 | return C; |
| 168 | } |
| 169 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 170 | // ReplaceInstUsesWith - This method is to be used when an instruction is |
| 171 | // found to be dead, replacable with another preexisting expression. Here |
| 172 | // we add all uses of I to the worklist, replace all uses of I with the new |
| 173 | // value, then return I, so that the inst combiner will know that I was |
| 174 | // modified. |
| 175 | // |
| 176 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 177 | AddUsersToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 8953b90 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 178 | if (&I != V) { |
| 179 | I.replaceAllUsesWith(V); |
| 180 | return &I; |
| 181 | } else { |
| 182 | // If we are replacing the instruction with itself, this must be in a |
| 183 | // segment of unreachable code, so just clobber the instruction. |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 184 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
Chris Lattner | 8953b90 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 185 | return &I; |
| 186 | } |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 187 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 188 | |
| 189 | // EraseInstFromFunction - When dealing with an instruction that has side |
| 190 | // effects or produces a void value, we can't rely on DCE to delete the |
| 191 | // instruction. Instead, visit methods should return the value returned by |
| 192 | // this function. |
| 193 | Instruction *EraseInstFromFunction(Instruction &I) { |
| 194 | assert(I.use_empty() && "Cannot erase instruction that is used!"); |
| 195 | AddUsesToWorkList(I); |
| 196 | removeFromWorkList(&I); |
Chris Lattner | 9530754 | 2004-11-18 21:41:39 +0000 | [diff] [blame] | 197 | I.eraseFromParent(); |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 198 | return 0; // Don't do anything with FI |
| 199 | } |
| 200 | |
| 201 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 202 | private: |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 203 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 204 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 205 | /// casts that are known to not do anything... |
| 206 | /// |
| 207 | Value *InsertOperandCastBefore(Value *V, const Type *DestTy, |
| 208 | Instruction *InsertBefore); |
| 209 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 210 | // SimplifyCommutative - This performs a few simplifications for commutative |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 211 | // operators. |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 212 | bool SimplifyCommutative(BinaryOperator &I); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 213 | |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 214 | |
| 215 | // FoldOpIntoPhi - Given a binary operator or cast instruction which has a |
| 216 | // PHI node as operand #0, see if we can fold the instruction into the PHI |
| 217 | // (which is only possible if all operands to the PHI are constants). |
| 218 | Instruction *FoldOpIntoPhi(Instruction &I); |
| 219 | |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 220 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 221 | // operator and they all are only used by the PHI, PHI together their |
| 222 | // inputs, and do the operation once, to the result of the PHI. |
| 223 | Instruction *FoldPHIArgOpIntoPHI(PHINode &PN); |
| 224 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 225 | Instruction *OptAndOp(Instruction *Op, ConstantIntegral *OpRHS, |
| 226 | ConstantIntegral *AndRHS, BinaryOperator &TheAnd); |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 227 | |
| 228 | Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantIntegral *Mask, |
| 229 | bool isSub, Instruction &I); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 230 | Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
| 231 | bool Inside, Instruction &IB); |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 232 | Instruction *PromoteCastOfAllocation(CastInst &CI, AllocationInst &AI); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 233 | }; |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 234 | |
Chris Lattner | c8b7092 | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 235 | RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions"); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 238 | // getComplexity: Assign a complexity or rank value to LLVM Values... |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 239 | // 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 240 | static unsigned getComplexity(Value *V) { |
| 241 | if (isa<Instruction>(V)) { |
| 242 | if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V)) |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 243 | return 3; |
| 244 | return 4; |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 245 | } |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 246 | if (isa<Argument>(V)) return 3; |
| 247 | return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2; |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 248 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 249 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 250 | // isOnlyUse - Return true if this instruction will be deleted if we stop using |
| 251 | // it. |
| 252 | static bool isOnlyUse(Value *V) { |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 253 | return V->hasOneUse() || isa<Constant>(V); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 256 | // getPromotedType - Return the specified type promoted as it would be to pass |
| 257 | // though a va_arg area... |
| 258 | static const Type *getPromotedType(const Type *Ty) { |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 259 | switch (Ty->getTypeID()) { |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 260 | case Type::SByteTyID: |
| 261 | case Type::ShortTyID: return Type::IntTy; |
| 262 | case Type::UByteTyID: |
| 263 | case Type::UShortTyID: return Type::UIntTy; |
| 264 | case Type::FloatTyID: return Type::DoubleTy; |
| 265 | default: return Ty; |
| 266 | } |
| 267 | } |
| 268 | |
Chris Lattner | 567b81f | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 269 | /// isCast - If the specified operand is a CastInst or a constant expr cast, |
| 270 | /// return the operand value, otherwise return null. |
| 271 | static Value *isCast(Value *V) { |
| 272 | if (CastInst *I = dyn_cast<CastInst>(V)) |
| 273 | return I->getOperand(0); |
| 274 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 275 | if (CE->getOpcode() == Instruction::Cast) |
| 276 | return CE->getOperand(0); |
| 277 | return 0; |
| 278 | } |
| 279 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 280 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 281 | // operators: |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 282 | // |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 283 | // 1. Order operands such that they are listed from right (least complex) to |
| 284 | // left (most complex). This puts constants before unary operators before |
| 285 | // binary operators. |
| 286 | // |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 287 | // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) |
| 288 | // 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] | 289 | // |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 290 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 291 | bool Changed = false; |
| 292 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) |
| 293 | Changed = !I.swapOperands(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 294 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 295 | if (!I.isAssociative()) return Changed; |
| 296 | Instruction::BinaryOps Opcode = I.getOpcode(); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 297 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) |
| 298 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { |
| 299 | if (isa<Constant>(I.getOperand(1))) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 300 | Constant *Folded = ConstantExpr::get(I.getOpcode(), |
| 301 | cast<Constant>(I.getOperand(1)), |
| 302 | cast<Constant>(Op->getOperand(1))); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 303 | I.setOperand(0, Op->getOperand(0)); |
| 304 | I.setOperand(1, Folded); |
| 305 | return true; |
| 306 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) |
| 307 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && |
| 308 | isOnlyUse(Op) && isOnlyUse(Op1)) { |
| 309 | Constant *C1 = cast<Constant>(Op->getOperand(1)); |
| 310 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); |
| 311 | |
| 312 | // 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] | 313 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 314 | Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0), |
| 315 | Op1->getOperand(0), |
| 316 | Op1->getName(), &I); |
| 317 | WorkList.push_back(New); |
| 318 | I.setOperand(0, New); |
| 319 | I.setOperand(1, Folded); |
| 320 | return true; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 321 | } |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 322 | } |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 323 | return Changed; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 324 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 325 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 326 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
| 327 | // if the LHS is a constant zero (which is the 'negate' form). |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 328 | // |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 329 | static inline Value *dyn_castNegVal(Value *V) { |
| 330 | if (BinaryOperator::isNeg(V)) |
Chris Lattner | d6f636a | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 331 | return BinaryOperator::getNegArgument(V); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 332 | |
Chris Lattner | 9ad0d55 | 2004-12-14 20:08:06 +0000 | [diff] [blame] | 333 | // Constants can be considered to be negated values if they can be folded. |
| 334 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
| 335 | return ConstantExpr::getNeg(C); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 336 | return 0; |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 339 | static inline Value *dyn_castNotVal(Value *V) { |
| 340 | if (BinaryOperator::isNot(V)) |
Chris Lattner | d6f636a | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 341 | return BinaryOperator::getNotArgument(V); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 342 | |
| 343 | // Constants can be considered to be not'ed values... |
Chris Lattner | dd65d86 | 2003-04-30 22:34:06 +0000 | [diff] [blame] | 344 | if (ConstantIntegral *C = dyn_cast<ConstantIntegral>(V)) |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 345 | return ConstantExpr::getNot(C); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 346 | return 0; |
| 347 | } |
| 348 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 349 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
| 350 | // other computations (because it has a constant operand), return the |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 351 | // non-constant operand of the multiply, and set CST to point to the multiplier. |
| 352 | // Otherwise, return null. |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 353 | // |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 354 | static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) { |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 355 | if (V->hasOneUse() && V->getType()->isInteger()) |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 356 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 357 | if (I->getOpcode() == Instruction::Mul) |
Chris Lattner | 97013636 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 358 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 359 | return I->getOperand(0); |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 360 | if (I->getOpcode() == Instruction::Shl) |
Chris Lattner | 97013636 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 361 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) { |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 362 | // The multiplier is really 1 << CST. |
| 363 | Constant *One = ConstantInt::get(V->getType(), 1); |
| 364 | CST = cast<ConstantInt>(ConstantExpr::getShl(One, CST)); |
| 365 | return I->getOperand(0); |
| 366 | } |
| 367 | } |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 368 | return 0; |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 369 | } |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 370 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 371 | /// dyn_castGetElementPtr - If this is a getelementptr instruction or constant |
| 372 | /// expression, return it. |
| 373 | static User *dyn_castGetElementPtr(Value *V) { |
| 374 | if (isa<GetElementPtrInst>(V)) return cast<User>(V); |
| 375 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 376 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 377 | return cast<User>(V); |
| 378 | return false; |
| 379 | } |
| 380 | |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 381 | // AddOne, SubOne - Add or subtract a constant one from an integer constant... |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 382 | static ConstantInt *AddOne(ConstantInt *C) { |
| 383 | return cast<ConstantInt>(ConstantExpr::getAdd(C, |
| 384 | ConstantInt::get(C->getType(), 1))); |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 385 | } |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 386 | static ConstantInt *SubOne(ConstantInt *C) { |
| 387 | return cast<ConstantInt>(ConstantExpr::getSub(C, |
| 388 | ConstantInt::get(C->getType(), 1))); |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 391 | /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use |
| 392 | /// this predicate to simplify operations downstream. V and Mask are known to |
| 393 | /// be the same type. |
Chris Lattner | 09efd4e | 2005-10-31 18:35:52 +0000 | [diff] [blame] | 394 | static bool MaskedValueIsZero(Value *V, ConstantIntegral *Mask, |
| 395 | unsigned Depth = 0) { |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 396 | // Note, we cannot consider 'undef' to be "IsZero" here. The problem is that |
| 397 | // we cannot optimize based on the assumption that it is zero without changing |
| 398 | // to to an explicit zero. If we don't change it to zero, other code could |
| 399 | // optimized based on the contradictory assumption that it is non-zero. |
| 400 | // Because instcombine aggressively folds operations with undef args anyway, |
| 401 | // this won't lose us code quality. |
| 402 | if (Mask->isNullValue()) |
| 403 | return true; |
| 404 | if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V)) |
| 405 | return ConstantExpr::getAnd(CI, Mask)->isNullValue(); |
Chris Lattner | 09efd4e | 2005-10-31 18:35:52 +0000 | [diff] [blame] | 406 | |
| 407 | if (Depth == 6) return false; // Limit search depth. |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 408 | |
| 409 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 410 | switch (I->getOpcode()) { |
Chris Lattner | 62010c4 | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 411 | case Instruction::And: |
| 412 | // (X & C1) & C2 == 0 iff C1 & C2 == 0. |
Chris Lattner | 03b9eb5 | 2005-10-09 22:08:50 +0000 | [diff] [blame] | 413 | if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(I->getOperand(1))) { |
| 414 | ConstantIntegral *C1C2 = |
| 415 | cast<ConstantIntegral>(ConstantExpr::getAnd(CI, Mask)); |
Chris Lattner | 09efd4e | 2005-10-31 18:35:52 +0000 | [diff] [blame] | 416 | if (MaskedValueIsZero(I->getOperand(0), C1C2, Depth+1)) |
Chris Lattner | 62010c4 | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 417 | return true; |
Chris Lattner | 03b9eb5 | 2005-10-09 22:08:50 +0000 | [diff] [blame] | 418 | } |
| 419 | // If either the LHS or the RHS are MaskedValueIsZero, the result is zero. |
Chris Lattner | 09efd4e | 2005-10-31 18:35:52 +0000 | [diff] [blame] | 420 | return MaskedValueIsZero(I->getOperand(1), Mask, Depth+1) || |
| 421 | MaskedValueIsZero(I->getOperand(0), Mask, Depth+1); |
Chris Lattner | 62010c4 | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 422 | case Instruction::Or: |
Chris Lattner | 03b9eb5 | 2005-10-09 22:08:50 +0000 | [diff] [blame] | 423 | case Instruction::Xor: |
Chris Lattner | 62010c4 | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 424 | // If the LHS and the RHS are MaskedValueIsZero, the result is also zero. |
Chris Lattner | 09efd4e | 2005-10-31 18:35:52 +0000 | [diff] [blame] | 425 | return MaskedValueIsZero(I->getOperand(1), Mask, Depth+1) && |
| 426 | MaskedValueIsZero(I->getOperand(0), Mask, Depth+1); |
Chris Lattner | 62010c4 | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 427 | case Instruction::Select: |
| 428 | // If the T and F values are MaskedValueIsZero, the result is also zero. |
Chris Lattner | 09efd4e | 2005-10-31 18:35:52 +0000 | [diff] [blame] | 429 | return MaskedValueIsZero(I->getOperand(2), Mask, Depth+1) && |
| 430 | MaskedValueIsZero(I->getOperand(1), Mask, Depth+1); |
Chris Lattner | 62010c4 | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 431 | case Instruction::Cast: { |
| 432 | const Type *SrcTy = I->getOperand(0)->getType(); |
| 433 | if (SrcTy == Type::BoolTy) |
| 434 | return (Mask->getRawValue() & 1) == 0; |
| 435 | |
| 436 | if (SrcTy->isInteger()) { |
| 437 | // (cast <ty> X to int) & C2 == 0 iff <ty> could not have contained C2. |
| 438 | if (SrcTy->isUnsigned() && // Only handle zero ext. |
| 439 | ConstantExpr::getCast(Mask, SrcTy)->isNullValue()) |
| 440 | return true; |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 441 | |
Chris Lattner | 62010c4 | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 442 | // If this is a noop cast, recurse. |
| 443 | if ((SrcTy->isSigned() && SrcTy->getUnsignedVersion() == I->getType())|| |
| 444 | SrcTy->getSignedVersion() == I->getType()) { |
| 445 | Constant *NewMask = |
| 446 | ConstantExpr::getCast(Mask, I->getOperand(0)->getType()); |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 447 | return MaskedValueIsZero(I->getOperand(0), |
Chris Lattner | 09efd4e | 2005-10-31 18:35:52 +0000 | [diff] [blame] | 448 | cast<ConstantIntegral>(NewMask), Depth+1); |
Chris Lattner | 62010c4 | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | break; |
| 452 | } |
| 453 | case Instruction::Shl: |
| 454 | // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 |
| 455 | if (ConstantUInt *SA = dyn_cast<ConstantUInt>(I->getOperand(1))) |
| 456 | return MaskedValueIsZero(I->getOperand(0), |
Chris Lattner | 09efd4e | 2005-10-31 18:35:52 +0000 | [diff] [blame] | 457 | cast<ConstantIntegral>(ConstantExpr::getUShr(Mask, SA)), |
| 458 | Depth+1); |
Chris Lattner | 62010c4 | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 459 | break; |
| 460 | case Instruction::Shr: |
| 461 | // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
| 462 | if (ConstantUInt *SA = dyn_cast<ConstantUInt>(I->getOperand(1))) |
| 463 | if (I->getType()->isUnsigned()) { |
| 464 | Constant *C1 = ConstantIntegral::getAllOnesValue(I->getType()); |
| 465 | C1 = ConstantExpr::getShr(C1, SA); |
| 466 | C1 = ConstantExpr::getAnd(C1, Mask); |
| 467 | if (C1->isNullValue()) |
| 468 | return true; |
| 469 | } |
| 470 | break; |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
| 474 | return false; |
| 475 | } |
| 476 | |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 477 | // isTrueWhenEqual - Return true if the specified setcondinst instruction is |
| 478 | // true when both operands are equal... |
| 479 | // |
| 480 | static bool isTrueWhenEqual(Instruction &I) { |
| 481 | return I.getOpcode() == Instruction::SetEQ || |
| 482 | I.getOpcode() == Instruction::SetGE || |
| 483 | I.getOpcode() == Instruction::SetLE; |
| 484 | } |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 485 | |
| 486 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
| 487 | /// function is designed to check a chain of associative operators for a |
| 488 | /// potential to apply a certain optimization. Since the optimization may be |
| 489 | /// applicable if the expression was reassociated, this checks the chain, then |
| 490 | /// reassociates the expression as necessary to expose the optimization |
| 491 | /// opportunity. This makes use of a special Functor, which must define |
| 492 | /// 'shouldApply' and 'apply' methods. |
| 493 | /// |
| 494 | template<typename Functor> |
| 495 | Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { |
| 496 | unsigned Opcode = Root.getOpcode(); |
| 497 | Value *LHS = Root.getOperand(0); |
| 498 | |
| 499 | // Quick check, see if the immediate LHS matches... |
| 500 | if (F.shouldApply(LHS)) |
| 501 | return F.apply(Root); |
| 502 | |
| 503 | // Otherwise, if the LHS is not of the same opcode as the root, return. |
| 504 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 505 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 506 | // Should we apply this transform to the RHS? |
| 507 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); |
| 508 | |
| 509 | // If not to the RHS, check to see if we should apply to the LHS... |
| 510 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { |
| 511 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS |
| 512 | ShouldApply = true; |
| 513 | } |
| 514 | |
| 515 | // If the functor wants to apply the optimization to the RHS of LHSI, |
| 516 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) |
| 517 | if (ShouldApply) { |
| 518 | BasicBlock *BB = Root.getParent(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 519 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 520 | // Now all of the instructions are in the current basic block, go ahead |
| 521 | // and perform the reassociation. |
| 522 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); |
| 523 | |
| 524 | // First move the selected RHS to the LHS of the root... |
| 525 | Root.setOperand(0, LHSI->getOperand(1)); |
| 526 | |
| 527 | // Make what used to be the LHS of the root be the user of the root... |
| 528 | Value *ExtraOperand = TmpLHSI->getOperand(1); |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 529 | if (&Root == TmpLHSI) { |
Chris Lattner | 8953b90 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 530 | Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); |
| 531 | return 0; |
| 532 | } |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 533 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 534 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 535 | TmpLHSI->getParent()->getInstList().remove(TmpLHSI); |
| 536 | BasicBlock::iterator ARI = &Root; ++ARI; |
| 537 | BB->getInstList().insert(ARI, TmpLHSI); // Move TmpLHSI to after Root |
| 538 | ARI = Root; |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 539 | |
| 540 | // Now propagate the ExtraOperand down the chain of instructions until we |
| 541 | // get to LHSI. |
| 542 | while (TmpLHSI != LHSI) { |
| 543 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 544 | // Move the instruction to immediately before the chain we are |
| 545 | // constructing to avoid breaking dominance properties. |
| 546 | NextLHSI->getParent()->getInstList().remove(NextLHSI); |
| 547 | BB->getInstList().insert(ARI, NextLHSI); |
| 548 | ARI = NextLHSI; |
| 549 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 550 | Value *NextOp = NextLHSI->getOperand(1); |
| 551 | NextLHSI->setOperand(1, ExtraOperand); |
| 552 | TmpLHSI = NextLHSI; |
| 553 | ExtraOperand = NextOp; |
| 554 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 555 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 556 | // Now that the instructions are reassociated, have the functor perform |
| 557 | // the transformation... |
| 558 | return F.apply(Root); |
| 559 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 560 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 561 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
| 562 | } |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | |
| 567 | // AddRHS - Implements: X + X --> X << 1 |
| 568 | struct AddRHS { |
| 569 | Value *RHS; |
| 570 | AddRHS(Value *rhs) : RHS(rhs) {} |
| 571 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 572 | Instruction *apply(BinaryOperator &Add) const { |
| 573 | return new ShiftInst(Instruction::Shl, Add.getOperand(0), |
| 574 | ConstantInt::get(Type::UByteTy, 1)); |
| 575 | } |
| 576 | }; |
| 577 | |
| 578 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) |
| 579 | // iff C1&C2 == 0 |
| 580 | struct AddMaskingAnd { |
| 581 | Constant *C2; |
| 582 | AddMaskingAnd(Constant *c) : C2(c) {} |
| 583 | bool shouldApply(Value *LHS) const { |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 584 | ConstantInt *C1; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 585 | return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 586 | ConstantExpr::getAnd(C1, C2)->isNullValue(); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 587 | } |
| 588 | Instruction *apply(BinaryOperator &Add) const { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 589 | return BinaryOperator::createOr(Add.getOperand(0), Add.getOperand(1)); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 590 | } |
| 591 | }; |
| 592 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 593 | static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 594 | InstCombiner *IC) { |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 595 | if (isa<CastInst>(I)) { |
| 596 | if (Constant *SOC = dyn_cast<Constant>(SO)) |
| 597 | return ConstantExpr::getCast(SOC, I.getType()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 598 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 599 | return IC->InsertNewInstBefore(new CastInst(SO, I.getType(), |
| 600 | SO->getName() + ".cast"), I); |
| 601 | } |
| 602 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 603 | // Figure out if the constant is the left or the right argument. |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 604 | bool ConstIsRHS = isa<Constant>(I.getOperand(1)); |
| 605 | Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS)); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 606 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 607 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
| 608 | if (ConstIsRHS) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 609 | return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand); |
| 610 | return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC); |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | Value *Op0 = SO, *Op1 = ConstOperand; |
| 614 | if (!ConstIsRHS) |
| 615 | std::swap(Op0, Op1); |
| 616 | Instruction *New; |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 617 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| 618 | New = BinaryOperator::create(BO->getOpcode(), Op0, Op1,SO->getName()+".op"); |
| 619 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(&I)) |
| 620 | New = new ShiftInst(SI->getOpcode(), Op0, Op1, SO->getName()+".sh"); |
Chris Lattner | f9d9665 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 621 | else { |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 622 | assert(0 && "Unknown binary instruction type!"); |
Chris Lattner | f9d9665 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 623 | abort(); |
| 624 | } |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 625 | return IC->InsertNewInstBefore(New, I); |
| 626 | } |
| 627 | |
| 628 | // FoldOpIntoSelect - Given an instruction with a select as one operand and a |
| 629 | // constant as the other operand, try to fold the binary operator into the |
| 630 | // select arguments. This also works for Cast instructions, which obviously do |
| 631 | // not have a second operand. |
| 632 | static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI, |
| 633 | InstCombiner *IC) { |
| 634 | // Don't modify shared select instructions |
| 635 | if (!SI->hasOneUse()) return 0; |
| 636 | Value *TV = SI->getOperand(1); |
| 637 | Value *FV = SI->getOperand(2); |
| 638 | |
| 639 | if (isa<Constant>(TV) || isa<Constant>(FV)) { |
Chris Lattner | 374e659 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 640 | // Bool selects with constant operands can be folded to logical ops. |
| 641 | if (SI->getType() == Type::BoolTy) return 0; |
| 642 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 643 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC); |
| 644 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC); |
| 645 | |
| 646 | return new SelectInst(SI->getCondition(), SelectTrueVal, |
| 647 | SelectFalseVal); |
| 648 | } |
| 649 | return 0; |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 652 | |
| 653 | /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI |
| 654 | /// node as operand #0, see if we can fold the instruction into the PHI (which |
| 655 | /// is only possible if all operands to the PHI are constants). |
| 656 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { |
| 657 | PHINode *PN = cast<PHINode>(I.getOperand(0)); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 658 | unsigned NumPHIValues = PN->getNumIncomingValues(); |
| 659 | if (!PN->hasOneUse() || NumPHIValues == 0 || |
| 660 | !isa<Constant>(PN->getIncomingValue(0))) return 0; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 661 | |
| 662 | // Check to see if all of the operands of the PHI are constants. If not, we |
| 663 | // cannot do the transformation. |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 664 | for (unsigned i = 1; i != NumPHIValues; ++i) |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 665 | if (!isa<Constant>(PN->getIncomingValue(i))) |
| 666 | return 0; |
| 667 | |
| 668 | // Okay, we can do the transformation: create the new PHI node. |
| 669 | PHINode *NewPN = new PHINode(I.getType(), I.getName()); |
| 670 | I.setName(""); |
Chris Lattner | d8e2018 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 671 | NewPN->reserveOperandSpace(PN->getNumOperands()/2); |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 672 | InsertNewInstBefore(NewPN, *PN); |
| 673 | |
| 674 | // Next, add all of the operands to the PHI. |
| 675 | if (I.getNumOperands() == 2) { |
| 676 | Constant *C = cast<Constant>(I.getOperand(1)); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 677 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 678 | Constant *InV = cast<Constant>(PN->getIncomingValue(i)); |
| 679 | NewPN->addIncoming(ConstantExpr::get(I.getOpcode(), InV, C), |
| 680 | PN->getIncomingBlock(i)); |
| 681 | } |
| 682 | } else { |
| 683 | assert(isa<CastInst>(I) && "Unary op should be a cast!"); |
| 684 | const Type *RetTy = I.getType(); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 685 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 686 | Constant *InV = cast<Constant>(PN->getIncomingValue(i)); |
| 687 | NewPN->addIncoming(ConstantExpr::getCast(InV, RetTy), |
| 688 | PN->getIncomingBlock(i)); |
| 689 | } |
| 690 | } |
| 691 | return ReplaceInstUsesWith(I, NewPN); |
| 692 | } |
| 693 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 694 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 695 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 696 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 697 | |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 698 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 699 | // X + undef -> undef |
| 700 | if (isa<UndefValue>(RHS)) |
| 701 | return ReplaceInstUsesWith(I, RHS); |
| 702 | |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 703 | // X + 0 --> X |
Chris Lattner | 7fde91e | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 704 | if (!I.getType()->isFloatingPoint()) { // NOTE: -0 + +0 = +0. |
| 705 | if (RHSC->isNullValue()) |
| 706 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | da1b152 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 707 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
| 708 | if (CFP->isExactlyValue(-0.0)) |
| 709 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 7fde91e | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 710 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 711 | |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 712 | // X + (signbit) --> X ^ signbit |
| 713 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 714 | unsigned NumBits = CI->getType()->getPrimitiveSizeInBits(); |
Andrew Lenharth | 6622955 | 2005-11-02 18:35:40 +0000 | [diff] [blame] | 715 | uint64_t Val = CI->getRawValue() & (~0ULL >> (64- NumBits)); |
Chris Lattner | 33eb909 | 2004-11-05 04:45:43 +0000 | [diff] [blame] | 716 | if (Val == (1ULL << (NumBits-1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 717 | return BinaryOperator::createXor(LHS, RHS); |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 718 | } |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 719 | |
| 720 | if (isa<PHINode>(LHS)) |
| 721 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 722 | return NV; |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 723 | |
Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 724 | ConstantInt *XorRHS = 0; |
| 725 | Value *XorLHS = 0; |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 726 | if (match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) { |
| 727 | unsigned TySizeBits = I.getType()->getPrimitiveSizeInBits(); |
| 728 | int64_t RHSSExt = cast<ConstantInt>(RHSC)->getSExtValue(); |
| 729 | uint64_t RHSZExt = cast<ConstantInt>(RHSC)->getZExtValue(); |
| 730 | |
| 731 | uint64_t C0080Val = 1ULL << 31; |
| 732 | int64_t CFF80Val = -C0080Val; |
| 733 | unsigned Size = 32; |
| 734 | do { |
| 735 | if (TySizeBits > Size) { |
| 736 | bool Found = false; |
| 737 | // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext. |
| 738 | // If we have ADD(XOR(AND(X, 0xFF), 0xF..F80), 0x80), it's a sext. |
| 739 | if (RHSSExt == CFF80Val) { |
| 740 | if (XorRHS->getZExtValue() == C0080Val) |
| 741 | Found = true; |
| 742 | } else if (RHSZExt == C0080Val) { |
| 743 | if (XorRHS->getSExtValue() == CFF80Val) |
| 744 | Found = true; |
| 745 | } |
| 746 | if (Found) { |
| 747 | // This is a sign extend if the top bits are known zero. |
| 748 | Constant *Mask = ConstantInt::getAllOnesValue(XorLHS->getType()); |
| 749 | Mask = ConstantExpr::getShl(Mask, |
| 750 | ConstantInt::get(Type::UByteTy, 64-TySizeBits-Size)); |
| 751 | if (!MaskedValueIsZero(XorLHS, cast<ConstantInt>(Mask))) |
| 752 | Size = 0; // Not a sign ext, but can't be any others either. |
| 753 | goto FoundSExt; |
| 754 | } |
| 755 | } |
| 756 | Size >>= 1; |
| 757 | C0080Val >>= Size; |
| 758 | CFF80Val >>= Size; |
| 759 | } while (Size >= 8); |
| 760 | |
| 761 | FoundSExt: |
| 762 | const Type *MiddleType = 0; |
| 763 | switch (Size) { |
| 764 | default: break; |
| 765 | case 32: MiddleType = Type::IntTy; break; |
| 766 | case 16: MiddleType = Type::ShortTy; break; |
| 767 | case 8: MiddleType = Type::SByteTy; break; |
| 768 | } |
| 769 | if (MiddleType) { |
| 770 | Instruction *NewTrunc = new CastInst(XorLHS, MiddleType, "sext"); |
| 771 | InsertNewInstBefore(NewTrunc, I); |
| 772 | return new CastInst(NewTrunc, I.getType()); |
| 773 | } |
| 774 | } |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 775 | } |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 776 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 777 | // X + X --> X << 1 |
Robert Bocchino | 7b5b86c | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 778 | if (I.getType()->isInteger()) { |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 779 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; |
Chris Lattner | 4706046 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 780 | |
| 781 | if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) { |
| 782 | if (RHSI->getOpcode() == Instruction::Sub) |
| 783 | if (LHS == RHSI->getOperand(1)) // A + (B - A) --> B |
| 784 | return ReplaceInstUsesWith(I, RHSI->getOperand(0)); |
| 785 | } |
| 786 | if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) { |
| 787 | if (LHSI->getOpcode() == Instruction::Sub) |
| 788 | if (RHS == LHSI->getOperand(1)) // (B - A) + A --> B |
| 789 | return ReplaceInstUsesWith(I, LHSI->getOperand(0)); |
| 790 | } |
Robert Bocchino | 7b5b86c | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 791 | } |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 792 | |
Chris Lattner | 147e975 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 793 | // -A + B --> B - A |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 794 | if (Value *V = dyn_castNegVal(LHS)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 795 | return BinaryOperator::createSub(RHS, V); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 796 | |
| 797 | // A + -B --> A - B |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 798 | if (!isa<Constant>(RHS)) |
| 799 | if (Value *V = dyn_castNegVal(RHS)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 800 | return BinaryOperator::createSub(LHS, V); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 801 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 802 | |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 803 | ConstantInt *C2; |
| 804 | if (Value *X = dyn_castFoldableMul(LHS, C2)) { |
| 805 | if (X == RHS) // X*C + X --> X * (C+1) |
| 806 | return BinaryOperator::createMul(RHS, AddOne(C2)); |
| 807 | |
| 808 | // X*C1 + X*C2 --> X * (C1+C2) |
| 809 | ConstantInt *C1; |
| 810 | if (X == dyn_castFoldableMul(RHS, C1)) |
| 811 | return BinaryOperator::createMul(X, ConstantExpr::getAdd(C1, C2)); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | // X + X*C --> X * (C+1) |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 815 | if (dyn_castFoldableMul(RHS, C2) == LHS) |
| 816 | return BinaryOperator::createMul(LHS, AddOne(C2)); |
| 817 | |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 818 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 819 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 820 | if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 821 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) return R; |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 822 | |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 823 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 824 | Value *X = 0; |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 825 | if (match(LHS, m_Not(m_Value(X)))) { // ~X + C --> (C-1) - X |
| 826 | Constant *C= ConstantExpr::getSub(CRHS, ConstantInt::get(I.getType(), 1)); |
| 827 | return BinaryOperator::createSub(C, X); |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 828 | } |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 829 | |
Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 830 | // (X & FF00) + xx00 -> (X+xx00) & FF00 |
| 831 | if (LHS->hasOneUse() && match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) { |
| 832 | Constant *Anded = ConstantExpr::getAnd(CRHS, C2); |
| 833 | if (Anded == CRHS) { |
| 834 | // See if all bits from the first bit set in the Add RHS up are included |
| 835 | // in the mask. First, get the rightmost bit. |
| 836 | uint64_t AddRHSV = CRHS->getRawValue(); |
| 837 | |
| 838 | // Form a mask of all bits from the lowest bit added through the top. |
| 839 | uint64_t AddRHSHighBits = ~((AddRHSV & -AddRHSV)-1); |
Chris Lattner | 2f1457f | 2005-04-24 17:46:05 +0000 | [diff] [blame] | 840 | AddRHSHighBits &= ~0ULL >> (64-C2->getType()->getPrimitiveSizeInBits()); |
Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 841 | |
| 842 | // See if the and mask includes all of these bits. |
| 843 | uint64_t AddRHSHighBitsAnd = AddRHSHighBits & C2->getRawValue(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 844 | |
Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 845 | if (AddRHSHighBits == AddRHSHighBitsAnd) { |
| 846 | // Okay, the xform is safe. Insert the new add pronto. |
| 847 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, CRHS, |
| 848 | LHS->getName()), I); |
| 849 | return BinaryOperator::createAnd(NewAdd, C2); |
| 850 | } |
| 851 | } |
| 852 | } |
| 853 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 854 | // Try to fold constant add into select arguments. |
| 855 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 856 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 857 | return R; |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 860 | return Changed ? &I : 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Chris Lattner | bdb0ce0 | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 863 | // isSignBit - Return true if the value represented by the constant only has the |
| 864 | // highest order bit set. |
| 865 | static bool isSignBit(ConstantInt *CI) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 866 | unsigned NumBits = CI->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 2f1457f | 2005-04-24 17:46:05 +0000 | [diff] [blame] | 867 | return (CI->getRawValue() & (~0ULL >> (64-NumBits))) == (1ULL << (NumBits-1)); |
Chris Lattner | bdb0ce0 | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 870 | /// RemoveNoopCast - Strip off nonconverting casts from the value. |
| 871 | /// |
| 872 | static Value *RemoveNoopCast(Value *V) { |
| 873 | if (CastInst *CI = dyn_cast<CastInst>(V)) { |
| 874 | const Type *CTy = CI->getType(); |
| 875 | const Type *OpTy = CI->getOperand(0)->getType(); |
| 876 | if (CTy->isInteger() && OpTy->isInteger()) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 877 | if (CTy->getPrimitiveSizeInBits() == OpTy->getPrimitiveSizeInBits()) |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 878 | return RemoveNoopCast(CI->getOperand(0)); |
| 879 | } else if (isa<PointerType>(CTy) && isa<PointerType>(OpTy)) |
| 880 | return RemoveNoopCast(CI->getOperand(0)); |
| 881 | } |
| 882 | return V; |
| 883 | } |
| 884 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 885 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 886 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 887 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 888 | if (Op0 == Op1) // sub X, X -> 0 |
| 889 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 890 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 891 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 892 | if (Value *V = dyn_castNegVal(Op1)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 893 | return BinaryOperator::createAdd(Op0, V); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 894 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 895 | if (isa<UndefValue>(Op0)) |
| 896 | return ReplaceInstUsesWith(I, Op0); // undef - X -> undef |
| 897 | if (isa<UndefValue>(Op1)) |
| 898 | return ReplaceInstUsesWith(I, Op1); // X - undef -> undef |
| 899 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 900 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
| 901 | // Replace (-1 - A) with (~A)... |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 902 | if (C->isAllOnesValue()) |
| 903 | return BinaryOperator::createNot(Op1); |
Chris Lattner | ad3c495 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 904 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 905 | // C - ~X == X + (1+C) |
Reid Spencer | 4fdd96c | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 906 | Value *X = 0; |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 907 | if (match(Op1, m_Not(m_Value(X)))) |
| 908 | return BinaryOperator::createAdd(X, |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 909 | ConstantExpr::getAdd(C, ConstantInt::get(I.getType(), 1))); |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 910 | // -((uint)X >> 31) -> ((int)X >> 31) |
| 911 | // -((int)X >> 31) -> ((uint)X >> 31) |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 912 | if (C->isNullValue()) { |
| 913 | Value *NoopCastedRHS = RemoveNoopCast(Op1); |
| 914 | if (ShiftInst *SI = dyn_cast<ShiftInst>(NoopCastedRHS)) |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 915 | if (SI->getOpcode() == Instruction::Shr) |
| 916 | if (ConstantUInt *CU = dyn_cast<ConstantUInt>(SI->getOperand(1))) { |
| 917 | const Type *NewTy; |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 918 | if (SI->getType()->isSigned()) |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 919 | NewTy = SI->getType()->getUnsignedVersion(); |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 920 | else |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 921 | NewTy = SI->getType()->getSignedVersion(); |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 922 | // Check to see if we are shifting out everything but the sign bit. |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 923 | if (CU->getValue() == SI->getType()->getPrimitiveSizeInBits()-1) { |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 924 | // Ok, the transformation is safe. Insert a cast of the incoming |
| 925 | // value, then the new shift, then the new cast. |
| 926 | Instruction *FirstCast = new CastInst(SI->getOperand(0), NewTy, |
| 927 | SI->getOperand(0)->getName()); |
| 928 | Value *InV = InsertNewInstBefore(FirstCast, I); |
| 929 | Instruction *NewShift = new ShiftInst(Instruction::Shr, FirstCast, |
| 930 | CU, SI->getName()); |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 931 | if (NewShift->getType() == I.getType()) |
| 932 | return NewShift; |
| 933 | else { |
| 934 | InV = InsertNewInstBefore(NewShift, I); |
| 935 | return new CastInst(NewShift, I.getType()); |
| 936 | } |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 937 | } |
| 938 | } |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 939 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 940 | |
| 941 | // Try to fold constant sub into select arguments. |
| 942 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 943 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 944 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 945 | |
| 946 | if (isa<PHINode>(Op0)) |
| 947 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 948 | return NV; |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 951 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
| 952 | if (Op1I->getOpcode() == Instruction::Add && |
| 953 | !Op0->getType()->isFloatingPoint()) { |
Chris Lattner | c7f3c1a | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 954 | if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 955 | return BinaryOperator::createNeg(Op1I->getOperand(1), I.getName()); |
Chris Lattner | c7f3c1a | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 956 | else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 957 | return BinaryOperator::createNeg(Op1I->getOperand(0), I.getName()); |
Chris Lattner | c7f3c1a | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 958 | else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) { |
| 959 | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1))) |
| 960 | // C1-(X+C2) --> (C1-C2)-X |
| 961 | return BinaryOperator::createSub(ConstantExpr::getSub(CI1, CI2), |
| 962 | Op1I->getOperand(0)); |
| 963 | } |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 966 | if (Op1I->hasOneUse()) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 967 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 968 | // is not used by anyone else... |
| 969 | // |
Chris Lattner | c2f0aa5 | 2004-02-02 20:09:56 +0000 | [diff] [blame] | 970 | if (Op1I->getOpcode() == Instruction::Sub && |
| 971 | !Op1I->getType()->isFloatingPoint()) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 972 | // Swap the two operands of the subexpr... |
| 973 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 974 | Op1I->setOperand(0, IIOp1); |
| 975 | Op1I->setOperand(1, IIOp0); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 976 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 977 | // Create the new top level add instruction... |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 978 | return BinaryOperator::createAdd(Op0, Op1); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 982 | // |
| 983 | if (Op1I->getOpcode() == Instruction::And && |
| 984 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 985 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 986 | |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 987 | Value *NewNot = |
| 988 | InsertNewInstBefore(BinaryOperator::createNot(OtherOp, "B.not"), I); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 989 | return BinaryOperator::createAnd(Op0, NewNot); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 990 | } |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 991 | |
Chris Lattner | 0aee4b7 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 992 | // -(X sdiv C) -> (X sdiv -C) |
| 993 | if (Op1I->getOpcode() == Instruction::Div) |
| 994 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0)) |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 995 | if (CSI->isNullValue()) |
Chris Lattner | 0aee4b7 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 996 | if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 997 | return BinaryOperator::createDiv(Op1I->getOperand(0), |
Chris Lattner | 0aee4b7 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 998 | ConstantExpr::getNeg(DivRHS)); |
| 999 | |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1000 | // X - X*C --> X * (1-C) |
Reid Spencer | 4fdd96c | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 1001 | ConstantInt *C2 = 0; |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1002 | if (dyn_castFoldableMul(Op1I, C2) == Op0) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1003 | Constant *CP1 = |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1004 | ConstantExpr::getSub(ConstantInt::get(I.getType(), 1), C2); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1005 | return BinaryOperator::createMul(Op0, CP1); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1006 | } |
Chris Lattner | ad3c495 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 1007 | } |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 1008 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1009 | |
Chris Lattner | 4706046 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 1010 | if (!Op0->getType()->isFloatingPoint()) |
| 1011 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 1012 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 1013 | if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X |
| 1014 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 1015 | else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X |
| 1016 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 4706046 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 1017 | } else if (Op0I->getOpcode() == Instruction::Sub) { |
| 1018 | if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y |
| 1019 | return BinaryOperator::createNeg(Op0I->getOperand(1), I.getName()); |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 1020 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1021 | |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1022 | ConstantInt *C1; |
| 1023 | if (Value *X = dyn_castFoldableMul(Op0, C1)) { |
| 1024 | if (X == Op1) { // X*C - X --> X * (C-1) |
| 1025 | Constant *CP1 = ConstantExpr::getSub(C1, ConstantInt::get(I.getType(),1)); |
| 1026 | return BinaryOperator::createMul(Op1, CP1); |
| 1027 | } |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1028 | |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1029 | ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2) |
| 1030 | if (X == dyn_castFoldableMul(Op1, C2)) |
| 1031 | return BinaryOperator::createMul(Op1, ConstantExpr::getSub(C1, C2)); |
| 1032 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1033 | return 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 1036 | /// isSignBitCheck - Given an exploded setcc instruction, return true if it is |
| 1037 | /// really just returns true if the most significant (sign) bit is set. |
| 1038 | static bool isSignBitCheck(unsigned Opcode, Value *LHS, ConstantInt *RHS) { |
| 1039 | if (RHS->getType()->isSigned()) { |
| 1040 | // True if source is LHS < 0 or LHS <= -1 |
| 1041 | return Opcode == Instruction::SetLT && RHS->isNullValue() || |
| 1042 | Opcode == Instruction::SetLE && RHS->isAllOnesValue(); |
| 1043 | } else { |
| 1044 | ConstantUInt *RHSC = cast<ConstantUInt>(RHS); |
| 1045 | // True if source is LHS > 127 or LHS >= 128, where the constants depend on |
| 1046 | // the size of the integer type. |
| 1047 | if (Opcode == Instruction::SetGE) |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1048 | return RHSC->getValue() == |
| 1049 | 1ULL << (RHS->getType()->getPrimitiveSizeInBits()-1); |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 1050 | if (Opcode == Instruction::SetGT) |
| 1051 | return RHSC->getValue() == |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1052 | (1ULL << (RHS->getType()->getPrimitiveSizeInBits()-1))-1; |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 1053 | } |
| 1054 | return false; |
| 1055 | } |
| 1056 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1057 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1058 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1059 | Value *Op0 = I.getOperand(0); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1060 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1061 | if (isa<UndefValue>(I.getOperand(1))) // undef * X -> 0 |
| 1062 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1063 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1064 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1065 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 1066 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 1067 | |
| 1068 | // ((X << C1)*C2) == (X * (C2 << C1)) |
| 1069 | if (ShiftInst *SI = dyn_cast<ShiftInst>(Op0)) |
| 1070 | if (SI->getOpcode() == Instruction::Shl) |
| 1071 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1072 | return BinaryOperator::createMul(SI->getOperand(0), |
| 1073 | ConstantExpr::getShl(CI, ShOp)); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1074 | |
Chris Lattner | cce81be | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 1075 | if (CI->isNullValue()) |
| 1076 | return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 |
| 1077 | if (CI->equalsInt(1)) // X * 1 == X |
| 1078 | return ReplaceInstUsesWith(I, Op0); |
| 1079 | if (CI->isAllOnesValue()) // X * -1 == 0 - X |
Chris Lattner | 35236d8 | 2003-06-25 17:09:20 +0000 | [diff] [blame] | 1080 | return BinaryOperator::createNeg(Op0, I.getName()); |
Chris Lattner | 31ba129 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 1081 | |
Chris Lattner | cce81be | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 1082 | int64_t Val = (int64_t)cast<ConstantInt>(CI)->getRawValue(); |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 1083 | if (isPowerOf2_64(Val)) { // Replace X*(2^C) with X << C |
| 1084 | uint64_t C = Log2_64(Val); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1085 | return new ShiftInst(Instruction::Shl, Op0, |
| 1086 | ConstantUInt::get(Type::UByteTy, C)); |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 1087 | } |
Robert Bocchino | 7b5b86c | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 1088 | } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1089 | if (Op1F->isNullValue()) |
| 1090 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 31ba129 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 1091 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1092 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 1093 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) |
| 1094 | if (Op1F->getValue() == 1.0) |
| 1095 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
| 1096 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1097 | |
| 1098 | // Try to fold constant mul into select arguments. |
| 1099 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1100 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1101 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1102 | |
| 1103 | if (isa<PHINode>(Op0)) |
| 1104 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1105 | return NV; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
Chris Lattner | 934a64cf | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 1108 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
| 1109 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1110 | return BinaryOperator::createMul(Op0v, Op1v); |
Chris Lattner | 934a64cf | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 1111 | |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 1112 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 1113 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 1114 | // See if we can simplify things based on how the boolean was originally |
| 1115 | // formed. |
| 1116 | CastInst *BoolCast = 0; |
| 1117 | if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(0))) |
| 1118 | if (CI->getOperand(0)->getType() == Type::BoolTy) |
| 1119 | BoolCast = CI; |
| 1120 | if (!BoolCast) |
| 1121 | if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(1))) |
| 1122 | if (CI->getOperand(0)->getType() == Type::BoolTy) |
| 1123 | BoolCast = CI; |
| 1124 | if (BoolCast) { |
| 1125 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(BoolCast->getOperand(0))) { |
| 1126 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); |
| 1127 | const Type *SCOpTy = SCIOp0->getType(); |
| 1128 | |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 1129 | // If the setcc is true iff the sign bit of X is set, then convert this |
| 1130 | // multiply into a shift/and combination. |
| 1131 | if (isa<ConstantInt>(SCIOp1) && |
| 1132 | isSignBitCheck(SCI->getOpcode(), SCIOp0, cast<ConstantInt>(SCIOp1))) { |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 1133 | // Shift the X value right to turn it into "all signbits". |
| 1134 | Constant *Amt = ConstantUInt::get(Type::UByteTy, |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1135 | SCOpTy->getPrimitiveSizeInBits()-1); |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 1136 | if (SCIOp0->getType()->isUnsigned()) { |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 1137 | const Type *NewTy = SCIOp0->getType()->getSignedVersion(); |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 1138 | SCIOp0 = InsertNewInstBefore(new CastInst(SCIOp0, NewTy, |
| 1139 | SCIOp0->getName()), I); |
| 1140 | } |
| 1141 | |
| 1142 | Value *V = |
| 1143 | InsertNewInstBefore(new ShiftInst(Instruction::Shr, SCIOp0, Amt, |
| 1144 | BoolCast->getOperand(0)->getName()+ |
| 1145 | ".mask"), I); |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 1146 | |
| 1147 | // If the multiply type is not the same as the source type, sign extend |
| 1148 | // or truncate to the multiply type. |
| 1149 | if (I.getType() != V->getType()) |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 1150 | V = InsertNewInstBefore(new CastInst(V, I.getType(), V->getName()),I); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1151 | |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 1152 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1153 | return BinaryOperator::createAnd(V, OtherOp); |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1158 | return Changed ? &I : 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1161 | Instruction *InstCombiner::visitDiv(BinaryOperator &I) { |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1162 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1163 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1164 | if (isa<UndefValue>(Op0)) // undef / X -> 0 |
| 1165 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1166 | if (isa<UndefValue>(Op1)) |
| 1167 | return ReplaceInstUsesWith(I, Op1); // X / undef -> undef |
| 1168 | |
| 1169 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | e20c334 | 2004-04-26 14:01:59 +0000 | [diff] [blame] | 1170 | // div X, 1 == X |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1171 | if (RHS->equalsInt(1)) |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1172 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1173 | |
Chris Lattner | e20c334 | 2004-04-26 14:01:59 +0000 | [diff] [blame] | 1174 | // div X, -1 == -X |
| 1175 | if (RHS->isAllOnesValue()) |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1176 | return BinaryOperator::createNeg(Op0); |
Chris Lattner | e20c334 | 2004-04-26 14:01:59 +0000 | [diff] [blame] | 1177 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1178 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 1179 | if (LHS->getOpcode() == Instruction::Div) |
| 1180 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 1181 | // (X / C1) / C2 -> X / (C1*C2) |
| 1182 | return BinaryOperator::createDiv(LHS->getOperand(0), |
| 1183 | ConstantExpr::getMul(RHS, LHSRHS)); |
| 1184 | } |
| 1185 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1186 | // Check to see if this is an unsigned division with an exact power of 2, |
| 1187 | // if so, convert to a right shift. |
| 1188 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS)) |
| 1189 | if (uint64_t Val = C->getValue()) // Don't break X / 0 |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 1190 | if (isPowerOf2_64(Val)) { |
| 1191 | uint64_t C = Log2_64(Val); |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1192 | return new ShiftInst(Instruction::Shr, Op0, |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1193 | ConstantUInt::get(Type::UByteTy, C)); |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 1194 | } |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1195 | |
Chris Lattner | 4ad0835 | 2004-10-09 02:50:40 +0000 | [diff] [blame] | 1196 | // -X/C -> X/-C |
| 1197 | if (RHS->getType()->isSigned()) |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1198 | if (Value *LHSNeg = dyn_castNegVal(Op0)) |
Chris Lattner | 4ad0835 | 2004-10-09 02:50:40 +0000 | [diff] [blame] | 1199 | return BinaryOperator::createDiv(LHSNeg, ConstantExpr::getNeg(RHS)); |
| 1200 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1201 | if (!RHS->isNullValue()) { |
| 1202 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1203 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1204 | return R; |
| 1205 | if (isa<PHINode>(Op0)) |
| 1206 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1207 | return NV; |
| 1208 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1211 | // If this is 'udiv X, (Cond ? C1, C2)' where C1&C2 are powers of two, |
| 1212 | // transform this into: '(Cond ? (udiv X, C1) : (udiv X, C2))'. |
| 1213 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 1214 | if (ConstantUInt *STO = dyn_cast<ConstantUInt>(SI->getOperand(1))) |
| 1215 | if (ConstantUInt *SFO = dyn_cast<ConstantUInt>(SI->getOperand(2))) { |
| 1216 | if (STO->getValue() == 0) { // Couldn't be this argument. |
| 1217 | I.setOperand(1, SFO); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1218 | return &I; |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1219 | } else if (SFO->getValue() == 0) { |
Chris Lattner | 89dc4f1 | 2005-06-16 04:55:52 +0000 | [diff] [blame] | 1220 | I.setOperand(1, STO); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1221 | return &I; |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Chris Lattner | 4236261 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 1224 | uint64_t TVA = STO->getValue(), FVA = SFO->getValue(); |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 1225 | if (isPowerOf2_64(TVA) && isPowerOf2_64(FVA)) { |
| 1226 | unsigned TSA = Log2_64(TVA), FSA = Log2_64(FVA); |
Chris Lattner | 4236261 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 1227 | Constant *TC = ConstantUInt::get(Type::UByteTy, TSA); |
| 1228 | Instruction *TSI = new ShiftInst(Instruction::Shr, Op0, |
| 1229 | TC, SI->getName()+".t"); |
| 1230 | TSI = InsertNewInstBefore(TSI, I); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1231 | |
Chris Lattner | 4236261 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 1232 | Constant *FC = ConstantUInt::get(Type::UByteTy, FSA); |
| 1233 | Instruction *FSI = new ShiftInst(Instruction::Shr, Op0, |
| 1234 | FC, SI->getName()+".f"); |
| 1235 | FSI = InsertNewInstBefore(FSI, I); |
| 1236 | return new SelectInst(SI->getOperand(0), TSI, FSI); |
| 1237 | } |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1238 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1239 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1240 | // 0 / X == 0, we don't need to preserve faults! |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1241 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1242 | if (LHS->equalsInt(0)) |
| 1243 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1244 | |
Chris Lattner | dd0c174 | 2005-11-05 07:40:31 +0000 | [diff] [blame] | 1245 | if (I.getType()->isSigned()) { |
| 1246 | // If the top bits of both operands are zero (i.e. we can prove they are |
| 1247 | // unsigned inputs), turn this into a udiv. |
| 1248 | ConstantIntegral *MaskV = ConstantSInt::getMinValue(I.getType()); |
| 1249 | if (MaskedValueIsZero(Op1, MaskV) && MaskedValueIsZero(Op0, MaskV)) { |
| 1250 | const Type *NTy = Op0->getType()->getUnsignedVersion(); |
| 1251 | Instruction *LHS = new CastInst(Op0, NTy, Op0->getName()); |
| 1252 | InsertNewInstBefore(LHS, I); |
| 1253 | Value *RHS; |
| 1254 | if (Constant *R = dyn_cast<Constant>(Op1)) |
| 1255 | RHS = ConstantExpr::getCast(R, NTy); |
| 1256 | else |
| 1257 | RHS = InsertNewInstBefore(new CastInst(Op1, NTy, Op1->getName()), I); |
| 1258 | Instruction *Div = BinaryOperator::createDiv(LHS, RHS, I.getName()); |
| 1259 | InsertNewInstBefore(Div, I); |
| 1260 | return new CastInst(Div, I.getType()); |
| 1261 | } |
| 1262 | } |
| 1263 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1264 | return 0; |
| 1265 | } |
| 1266 | |
| 1267 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1268 | Instruction *InstCombiner::visitRem(BinaryOperator &I) { |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1269 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | e9ff0ea | 2005-11-05 07:28:37 +0000 | [diff] [blame] | 1270 | if (I.getType()->isSigned()) { |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1271 | if (Value *RHSNeg = dyn_castNegVal(Op1)) |
Chris Lattner | 98c6bdf | 2004-07-06 07:11:42 +0000 | [diff] [blame] | 1272 | if (!isa<ConstantSInt>(RHSNeg) || |
Chris Lattner | 8e72606 | 2004-08-09 21:05:48 +0000 | [diff] [blame] | 1273 | cast<ConstantSInt>(RHSNeg)->getValue() > 0) { |
Chris Lattner | 7fd5f07 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 1274 | // X % -Y -> X % Y |
| 1275 | AddUsesToWorkList(I); |
| 1276 | I.setOperand(1, RHSNeg); |
| 1277 | return &I; |
| 1278 | } |
Chris Lattner | e9ff0ea | 2005-11-05 07:28:37 +0000 | [diff] [blame] | 1279 | |
| 1280 | // If the top bits of both operands are zero (i.e. we can prove they are |
| 1281 | // unsigned inputs), turn this into a urem. |
| 1282 | ConstantIntegral *MaskV = ConstantSInt::getMinValue(I.getType()); |
| 1283 | if (MaskedValueIsZero(Op1, MaskV) && MaskedValueIsZero(Op0, MaskV)) { |
| 1284 | const Type *NTy = Op0->getType()->getUnsignedVersion(); |
| 1285 | Instruction *LHS = new CastInst(Op0, NTy, Op0->getName()); |
| 1286 | InsertNewInstBefore(LHS, I); |
| 1287 | Value *RHS; |
| 1288 | if (Constant *R = dyn_cast<Constant>(Op1)) |
| 1289 | RHS = ConstantExpr::getCast(R, NTy); |
| 1290 | else |
| 1291 | RHS = InsertNewInstBefore(new CastInst(Op1, NTy, Op1->getName()), I); |
| 1292 | Instruction *Rem = BinaryOperator::createRem(LHS, RHS, I.getName()); |
| 1293 | InsertNewInstBefore(Rem, I); |
| 1294 | return new CastInst(Rem, I.getType()); |
| 1295 | } |
| 1296 | } |
Chris Lattner | 7fd5f07 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 1297 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1298 | if (isa<UndefValue>(Op0)) // undef % X -> 0 |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1299 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1300 | if (isa<UndefValue>(Op1)) |
| 1301 | return ReplaceInstUsesWith(I, Op1); // X % undef -> undef |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1302 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1303 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1304 | if (RHS->equalsInt(1)) // X % 1 == 0 |
| 1305 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1306 | |
| 1307 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 1308 | // if so, convert to a bitwise and. |
| 1309 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS)) |
| 1310 | 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] | 1311 | if (!(Val & (Val-1))) // Power of 2 |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1312 | return BinaryOperator::createAnd(Op0, |
| 1313 | ConstantUInt::get(I.getType(), Val-1)); |
| 1314 | |
| 1315 | if (!RHS->isNullValue()) { |
| 1316 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1317 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1318 | return R; |
| 1319 | if (isa<PHINode>(Op0)) |
| 1320 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1321 | return NV; |
| 1322 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1325 | // If this is 'urem X, (Cond ? C1, C2)' where C1&C2 are powers of two, |
| 1326 | // transform this into: '(Cond ? (urem X, C1) : (urem X, C2))'. |
| 1327 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 1328 | if (ConstantUInt *STO = dyn_cast<ConstantUInt>(SI->getOperand(1))) |
| 1329 | if (ConstantUInt *SFO = dyn_cast<ConstantUInt>(SI->getOperand(2))) { |
| 1330 | if (STO->getValue() == 0) { // Couldn't be this argument. |
| 1331 | I.setOperand(1, SFO); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1332 | return &I; |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1333 | } else if (SFO->getValue() == 0) { |
| 1334 | I.setOperand(1, STO); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1335 | return &I; |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | if (!(STO->getValue() & (STO->getValue()-1)) && |
| 1339 | !(SFO->getValue() & (SFO->getValue()-1))) { |
| 1340 | Value *TrueAnd = InsertNewInstBefore(BinaryOperator::createAnd(Op0, |
| 1341 | SubOne(STO), SI->getName()+".t"), I); |
| 1342 | Value *FalseAnd = InsertNewInstBefore(BinaryOperator::createAnd(Op0, |
| 1343 | SubOne(SFO), SI->getName()+".f"), I); |
| 1344 | return new SelectInst(SI->getOperand(0), TrueAnd, FalseAnd); |
| 1345 | } |
| 1346 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1347 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1348 | // 0 % X == 0, we don't need to preserve faults! |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1349 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1350 | if (LHS->equalsInt(0)) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1351 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1352 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1353 | return 0; |
| 1354 | } |
| 1355 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1356 | // isMaxValueMinusOne - return true if this is Max-1 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1357 | static bool isMaxValueMinusOne(const ConstantInt *C) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1358 | if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) { |
| 1359 | // Calculate -1 casted to the right type... |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1360 | unsigned TypeBits = C->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1361 | uint64_t Val = ~0ULL; // All ones |
| 1362 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 1363 | return CU->getValue() == Val-1; |
| 1364 | } |
| 1365 | |
| 1366 | const ConstantSInt *CS = cast<ConstantSInt>(C); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1367 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1368 | // Calculate 0111111111..11111 |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1369 | unsigned TypeBits = C->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1370 | int64_t Val = INT64_MAX; // All ones |
| 1371 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 1372 | return CS->getValue() == Val-1; |
| 1373 | } |
| 1374 | |
| 1375 | // isMinValuePlusOne - return true if this is Min+1 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1376 | static bool isMinValuePlusOne(const ConstantInt *C) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1377 | if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) |
| 1378 | return CU->getValue() == 1; |
| 1379 | |
| 1380 | const ConstantSInt *CS = cast<ConstantSInt>(C); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1381 | |
| 1382 | // Calculate 1111111111000000000000 |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1383 | unsigned TypeBits = C->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1384 | int64_t Val = -1; // All ones |
| 1385 | Val <<= TypeBits-1; // Shift over to the right spot |
| 1386 | return CS->getValue() == Val+1; |
| 1387 | } |
| 1388 | |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1389 | // isOneBitSet - Return true if there is exactly one bit set in the specified |
| 1390 | // constant. |
| 1391 | static bool isOneBitSet(const ConstantInt *CI) { |
| 1392 | uint64_t V = CI->getRawValue(); |
| 1393 | return V && (V & (V-1)) == 0; |
| 1394 | } |
| 1395 | |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 1396 | #if 0 // Currently unused |
| 1397 | // isLowOnes - Return true if the constant is of the form 0+1+. |
| 1398 | static bool isLowOnes(const ConstantInt *CI) { |
| 1399 | uint64_t V = CI->getRawValue(); |
| 1400 | |
| 1401 | // There won't be bits set in parts that the type doesn't contain. |
| 1402 | V &= ConstantInt::getAllOnesValue(CI->getType())->getRawValue(); |
| 1403 | |
| 1404 | uint64_t U = V+1; // If it is low ones, this should be a power of two. |
| 1405 | return U && V && (U & V) == 0; |
| 1406 | } |
| 1407 | #endif |
| 1408 | |
| 1409 | // isHighOnes - Return true if the constant is of the form 1+0+. |
| 1410 | // This is the same as lowones(~X). |
| 1411 | static bool isHighOnes(const ConstantInt *CI) { |
| 1412 | uint64_t V = ~CI->getRawValue(); |
Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 1413 | if (~V == 0) return false; // 0's does not match "1+" |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 1414 | |
| 1415 | // There won't be bits set in parts that the type doesn't contain. |
| 1416 | V &= ConstantInt::getAllOnesValue(CI->getType())->getRawValue(); |
| 1417 | |
| 1418 | uint64_t U = V+1; // If it is low ones, this should be a power of two. |
| 1419 | return U && V && (U & V) == 0; |
| 1420 | } |
| 1421 | |
| 1422 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1423 | /// getSetCondCode - Encode a setcc opcode into a three bit mask. These bits |
| 1424 | /// are carefully arranged to allow folding of expressions such as: |
| 1425 | /// |
| 1426 | /// (A < B) | (A > B) --> (A != B) |
| 1427 | /// |
| 1428 | /// Bit value '4' represents that the comparison is true if A > B, bit value '2' |
| 1429 | /// represents that the comparison is true if A == B, and bit value '1' is true |
| 1430 | /// if A < B. |
| 1431 | /// |
| 1432 | static unsigned getSetCondCode(const SetCondInst *SCI) { |
| 1433 | switch (SCI->getOpcode()) { |
| 1434 | // False -> 0 |
| 1435 | case Instruction::SetGT: return 1; |
| 1436 | case Instruction::SetEQ: return 2; |
| 1437 | case Instruction::SetGE: return 3; |
| 1438 | case Instruction::SetLT: return 4; |
| 1439 | case Instruction::SetNE: return 5; |
| 1440 | case Instruction::SetLE: return 6; |
| 1441 | // True -> 7 |
| 1442 | default: |
| 1443 | assert(0 && "Invalid SetCC opcode!"); |
| 1444 | return 0; |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | /// getSetCCValue - This is the complement of getSetCondCode, which turns an |
| 1449 | /// opcode and two operands into either a constant true or false, or a brand new |
| 1450 | /// SetCC instruction. |
| 1451 | static Value *getSetCCValue(unsigned Opcode, Value *LHS, Value *RHS) { |
| 1452 | switch (Opcode) { |
| 1453 | case 0: return ConstantBool::False; |
| 1454 | case 1: return new SetCondInst(Instruction::SetGT, LHS, RHS); |
| 1455 | case 2: return new SetCondInst(Instruction::SetEQ, LHS, RHS); |
| 1456 | case 3: return new SetCondInst(Instruction::SetGE, LHS, RHS); |
| 1457 | case 4: return new SetCondInst(Instruction::SetLT, LHS, RHS); |
| 1458 | case 5: return new SetCondInst(Instruction::SetNE, LHS, RHS); |
| 1459 | case 6: return new SetCondInst(Instruction::SetLE, LHS, RHS); |
| 1460 | case 7: return ConstantBool::True; |
| 1461 | default: assert(0 && "Illegal SetCCCode!"); return 0; |
| 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | // FoldSetCCLogical - Implements (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B) |
| 1466 | struct FoldSetCCLogical { |
| 1467 | InstCombiner &IC; |
| 1468 | Value *LHS, *RHS; |
| 1469 | FoldSetCCLogical(InstCombiner &ic, SetCondInst *SCI) |
| 1470 | : IC(ic), LHS(SCI->getOperand(0)), RHS(SCI->getOperand(1)) {} |
| 1471 | bool shouldApply(Value *V) const { |
| 1472 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(V)) |
| 1473 | return (SCI->getOperand(0) == LHS && SCI->getOperand(1) == RHS || |
| 1474 | SCI->getOperand(0) == RHS && SCI->getOperand(1) == LHS); |
| 1475 | return false; |
| 1476 | } |
| 1477 | Instruction *apply(BinaryOperator &Log) const { |
| 1478 | SetCondInst *SCI = cast<SetCondInst>(Log.getOperand(0)); |
| 1479 | if (SCI->getOperand(0) != LHS) { |
| 1480 | assert(SCI->getOperand(1) == LHS); |
| 1481 | SCI->swapOperands(); // Swap the LHS and RHS of the SetCC |
| 1482 | } |
| 1483 | |
| 1484 | unsigned LHSCode = getSetCondCode(SCI); |
| 1485 | unsigned RHSCode = getSetCondCode(cast<SetCondInst>(Log.getOperand(1))); |
| 1486 | unsigned Code; |
| 1487 | switch (Log.getOpcode()) { |
| 1488 | case Instruction::And: Code = LHSCode & RHSCode; break; |
| 1489 | case Instruction::Or: Code = LHSCode | RHSCode; break; |
| 1490 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; |
Chris Lattner | 2caaaba | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 1491 | default: assert(0 && "Illegal logical opcode!"); return 0; |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | Value *RV = getSetCCValue(Code, LHS, RHS); |
| 1495 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 1496 | return I; |
| 1497 | // Otherwise, it's a constant boolean value... |
| 1498 | return IC.ReplaceInstUsesWith(Log, RV); |
| 1499 | } |
| 1500 | }; |
| 1501 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1502 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
| 1503 | // the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'. Op is |
| 1504 | // guaranteed to be either a shift instruction or a binary operator. |
| 1505 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
| 1506 | ConstantIntegral *OpRHS, |
| 1507 | ConstantIntegral *AndRHS, |
| 1508 | BinaryOperator &TheAnd) { |
| 1509 | Value *X = Op->getOperand(0); |
Chris Lattner | fcf21a7 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 1510 | Constant *Together = 0; |
| 1511 | if (!isa<ShiftInst>(Op)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1512 | Together = ConstantExpr::getAnd(AndRHS, OpRHS); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1513 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1514 | switch (Op->getOpcode()) { |
| 1515 | case Instruction::Xor: |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1516 | if (Op->hasOneUse()) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1517 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
| 1518 | std::string OpName = Op->getName(); Op->setName(""); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1519 | Instruction *And = BinaryOperator::createAnd(X, AndRHS, OpName); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1520 | InsertNewInstBefore(And, TheAnd); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1521 | return BinaryOperator::createXor(And, Together); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1522 | } |
| 1523 | break; |
| 1524 | case Instruction::Or: |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1525 | if (Together == AndRHS) // (X | C) & C --> C |
| 1526 | return ReplaceInstUsesWith(TheAnd, AndRHS); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1527 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1528 | if (Op->hasOneUse() && Together != OpRHS) { |
| 1529 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 |
| 1530 | std::string Op0Name = Op->getName(); Op->setName(""); |
| 1531 | Instruction *Or = BinaryOperator::createOr(X, Together, Op0Name); |
| 1532 | InsertNewInstBefore(Or, TheAnd); |
| 1533 | return BinaryOperator::createAnd(Or, AndRHS); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1534 | } |
| 1535 | break; |
| 1536 | case Instruction::Add: |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1537 | if (Op->hasOneUse()) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1538 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 1539 | // of the bit. First thing to check is to see if this AND is with a |
| 1540 | // single bit constant. |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1541 | uint64_t AndRHSV = cast<ConstantInt>(AndRHS)->getRawValue(); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1542 | |
| 1543 | // Clear bits that are not part of the constant. |
Chris Lattner | 2f1457f | 2005-04-24 17:46:05 +0000 | [diff] [blame] | 1544 | AndRHSV &= ~0ULL >> (64-AndRHS->getType()->getPrimitiveSizeInBits()); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1545 | |
| 1546 | // If there is only one bit set... |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1547 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1548 | // Ok, at this point, we know that we are masking the result of the |
| 1549 | // ADD down to exactly one bit. If the constant we are adding has |
| 1550 | // no bits set below this bit, then we can eliminate the ADD. |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1551 | uint64_t AddRHS = cast<ConstantInt>(OpRHS)->getRawValue(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1552 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1553 | // Check to see if any bits below the one bit set in AndRHSV are set. |
| 1554 | if ((AddRHS & (AndRHSV-1)) == 0) { |
| 1555 | // If not, the only thing that can effect the output of the AND is |
| 1556 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 1557 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 1558 | // no effect. |
| 1559 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop |
| 1560 | TheAnd.setOperand(0, X); |
| 1561 | return &TheAnd; |
| 1562 | } else { |
| 1563 | std::string Name = Op->getName(); Op->setName(""); |
| 1564 | // Pull the XOR out of the AND. |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1565 | Instruction *NewAnd = BinaryOperator::createAnd(X, AndRHS, Name); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1566 | InsertNewInstBefore(NewAnd, TheAnd); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1567 | return BinaryOperator::createXor(NewAnd, AndRHS); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1568 | } |
| 1569 | } |
| 1570 | } |
| 1571 | } |
| 1572 | break; |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1573 | |
| 1574 | case Instruction::Shl: { |
| 1575 | // We know that the AND will not produce any of the bits shifted in, so if |
| 1576 | // the anded constant includes them, clear them now! |
| 1577 | // |
| 1578 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1579 | Constant *ShlMask = ConstantExpr::getShl(AllOne, OpRHS); |
| 1580 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShlMask); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1581 | |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1582 | if (CI == ShlMask) { // Masking out bits that the shift already masks |
| 1583 | return ReplaceInstUsesWith(TheAnd, Op); // No need for the and. |
| 1584 | } else if (CI != AndRHS) { // Reducing bits set in and. |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1585 | TheAnd.setOperand(1, CI); |
| 1586 | return &TheAnd; |
| 1587 | } |
| 1588 | break; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1589 | } |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1590 | case Instruction::Shr: |
| 1591 | // We know that the AND will not produce any of the bits shifted in, so if |
| 1592 | // the anded constant includes them, clear them now! This only applies to |
| 1593 | // unsigned shifts, because a signed shr may bring in set bits! |
| 1594 | // |
| 1595 | if (AndRHS->getType()->isUnsigned()) { |
| 1596 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1597 | Constant *ShrMask = ConstantExpr::getShr(AllOne, OpRHS); |
| 1598 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask); |
| 1599 | |
| 1600 | if (CI == ShrMask) { // Masking out bits that the shift already masks. |
| 1601 | return ReplaceInstUsesWith(TheAnd, Op); |
| 1602 | } else if (CI != AndRHS) { |
| 1603 | TheAnd.setOperand(1, CI); // Reduce bits set in and cst. |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1604 | return &TheAnd; |
| 1605 | } |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1606 | } else { // Signed shr. |
| 1607 | // See if this is shifting in some sign extension, then masking it out |
| 1608 | // with an and. |
| 1609 | if (Op->hasOneUse()) { |
| 1610 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
| 1611 | Constant *ShrMask = ConstantExpr::getUShr(AllOne, OpRHS); |
| 1612 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask); |
Chris Lattner | 5c3c21e | 2004-10-22 04:53:16 +0000 | [diff] [blame] | 1613 | if (CI == AndRHS) { // Masking out bits shifted in. |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1614 | // Make the argument unsigned. |
| 1615 | Value *ShVal = Op->getOperand(0); |
| 1616 | ShVal = InsertCastBefore(ShVal, |
| 1617 | ShVal->getType()->getUnsignedVersion(), |
| 1618 | TheAnd); |
| 1619 | ShVal = InsertNewInstBefore(new ShiftInst(Instruction::Shr, ShVal, |
| 1620 | OpRHS, Op->getName()), |
| 1621 | TheAnd); |
Chris Lattner | 70c2039 | 2004-10-27 05:57:15 +0000 | [diff] [blame] | 1622 | Value *AndRHS2 = ConstantExpr::getCast(AndRHS, ShVal->getType()); |
| 1623 | ShVal = InsertNewInstBefore(BinaryOperator::createAnd(ShVal, AndRHS2, |
| 1624 | TheAnd.getName()), |
| 1625 | TheAnd); |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1626 | return new CastInst(ShVal, Op->getType()); |
| 1627 | } |
| 1628 | } |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1629 | } |
| 1630 | break; |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1631 | } |
| 1632 | return 0; |
| 1633 | } |
| 1634 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1635 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 1636 | /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is |
| 1637 | /// true, otherwise (V < Lo || V >= Hi). In pratice, we emit the more efficient |
| 1638 | /// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. IB is the location to |
| 1639 | /// insert new instructions. |
| 1640 | Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
| 1641 | bool Inside, Instruction &IB) { |
| 1642 | assert(cast<ConstantBool>(ConstantExpr::getSetLE(Lo, Hi))->getValue() && |
| 1643 | "Lo is not <= Hi in range emission code!"); |
| 1644 | if (Inside) { |
| 1645 | if (Lo == Hi) // Trivially false. |
| 1646 | return new SetCondInst(Instruction::SetNE, V, V); |
| 1647 | if (cast<ConstantIntegral>(Lo)->isMinValue()) |
| 1648 | return new SetCondInst(Instruction::SetLT, V, Hi); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1649 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 1650 | Constant *AddCST = ConstantExpr::getNeg(Lo); |
| 1651 | Instruction *Add = BinaryOperator::createAdd(V, AddCST,V->getName()+".off"); |
| 1652 | InsertNewInstBefore(Add, IB); |
| 1653 | // Convert to unsigned for the comparison. |
| 1654 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 1655 | Value *OffsetVal = InsertCastBefore(Add, UnsType, IB); |
| 1656 | AddCST = ConstantExpr::getAdd(AddCST, Hi); |
| 1657 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 1658 | return new SetCondInst(Instruction::SetLT, OffsetVal, AddCST); |
| 1659 | } |
| 1660 | |
| 1661 | if (Lo == Hi) // Trivially true. |
| 1662 | return new SetCondInst(Instruction::SetEQ, V, V); |
| 1663 | |
| 1664 | Hi = SubOne(cast<ConstantInt>(Hi)); |
| 1665 | if (cast<ConstantIntegral>(Lo)->isMinValue()) // V < 0 || V >= Hi ->'V > Hi-1' |
| 1666 | return new SetCondInst(Instruction::SetGT, V, Hi); |
| 1667 | |
| 1668 | // Emit X-Lo > Hi-Lo-1 |
| 1669 | Constant *AddCST = ConstantExpr::getNeg(Lo); |
| 1670 | Instruction *Add = BinaryOperator::createAdd(V, AddCST, V->getName()+".off"); |
| 1671 | InsertNewInstBefore(Add, IB); |
| 1672 | // Convert to unsigned for the comparison. |
| 1673 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 1674 | Value *OffsetVal = InsertCastBefore(Add, UnsType, IB); |
| 1675 | AddCST = ConstantExpr::getAdd(AddCST, Hi); |
| 1676 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 1677 | return new SetCondInst(Instruction::SetGT, OffsetVal, AddCST); |
| 1678 | } |
| 1679 | |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 1680 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 1681 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 1682 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 1683 | // not, since all 1s are not contiguous. |
| 1684 | static bool isRunOfOnes(ConstantIntegral *Val, unsigned &MB, unsigned &ME) { |
| 1685 | uint64_t V = Val->getRawValue(); |
| 1686 | if (!isShiftedMask_64(V)) return false; |
| 1687 | |
| 1688 | // look for the first zero bit after the run of ones |
| 1689 | MB = 64-CountLeadingZeros_64((V - 1) ^ V); |
| 1690 | // look for the first non-zero bit |
| 1691 | ME = 64-CountLeadingZeros_64(V); |
| 1692 | return true; |
| 1693 | } |
| 1694 | |
| 1695 | |
| 1696 | |
| 1697 | /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask, |
| 1698 | /// where isSub determines whether the operator is a sub. If we can fold one of |
| 1699 | /// the following xforms: |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 1700 | /// |
| 1701 | /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask |
| 1702 | /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 1703 | /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 1704 | /// |
| 1705 | /// return (A +/- B). |
| 1706 | /// |
| 1707 | Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS, |
| 1708 | ConstantIntegral *Mask, bool isSub, |
| 1709 | Instruction &I) { |
| 1710 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
| 1711 | if (!LHSI || LHSI->getNumOperands() != 2 || |
| 1712 | !isa<ConstantInt>(LHSI->getOperand(1))) return 0; |
| 1713 | |
| 1714 | ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1)); |
| 1715 | |
| 1716 | switch (LHSI->getOpcode()) { |
| 1717 | default: return 0; |
| 1718 | case Instruction::And: |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 1719 | if (ConstantExpr::getAnd(N, Mask) == Mask) { |
| 1720 | // If the AndRHS is a power of two minus one (0+1+), this is simple. |
| 1721 | if ((Mask->getRawValue() & Mask->getRawValue()+1) == 0) |
| 1722 | break; |
| 1723 | |
| 1724 | // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+ |
| 1725 | // part, we don't need any explicit masks to take them out of A. If that |
| 1726 | // is all N is, ignore it. |
| 1727 | unsigned MB, ME; |
| 1728 | if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive |
| 1729 | Constant *Mask = ConstantInt::getAllOnesValue(RHS->getType()); |
| 1730 | Mask = ConstantExpr::getUShr(Mask, |
| 1731 | ConstantInt::get(Type::UByteTy, |
| 1732 | (64-MB+1))); |
| 1733 | if (MaskedValueIsZero(RHS, cast<ConstantIntegral>(Mask))) |
| 1734 | break; |
| 1735 | } |
| 1736 | } |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 1737 | return 0; |
| 1738 | case Instruction::Or: |
| 1739 | case Instruction::Xor: |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 1740 | // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0 |
| 1741 | if ((Mask->getRawValue() & Mask->getRawValue()+1) == 0 && |
| 1742 | ConstantExpr::getAnd(N, Mask)->isNullValue()) |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 1743 | break; |
| 1744 | return 0; |
| 1745 | } |
| 1746 | |
| 1747 | Instruction *New; |
| 1748 | if (isSub) |
| 1749 | New = BinaryOperator::createSub(LHSI->getOperand(0), RHS, "fold"); |
| 1750 | else |
| 1751 | New = BinaryOperator::createAdd(LHSI->getOperand(0), RHS, "fold"); |
| 1752 | return InsertNewInstBefore(New, I); |
| 1753 | } |
| 1754 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1755 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1756 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1757 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1758 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1759 | if (isa<UndefValue>(Op1)) // X & undef -> 0 |
| 1760 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1761 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1762 | // and X, X = X |
| 1763 | if (Op0 == Op1) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1764 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1765 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1766 | if (ConstantIntegral *AndRHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1767 | // and X, -1 == X |
| 1768 | if (AndRHS->isAllOnesValue()) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1769 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 38a1b00 | 2005-10-26 17:18:16 +0000 | [diff] [blame] | 1770 | |
| 1771 | // and (and X, c1), c2 -> and (x, c1&c2). Handle this case here, before |
| 1772 | // calling MaskedValueIsZero, to avoid inefficient cases where we traipse |
| 1773 | // through many levels of ands. |
| 1774 | { |
Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 1775 | Value *X = 0; ConstantInt *C1 = 0; |
Chris Lattner | 38a1b00 | 2005-10-26 17:18:16 +0000 | [diff] [blame] | 1776 | if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1)))) |
| 1777 | return BinaryOperator::createAnd(X, ConstantExpr::getAnd(C1, AndRHS)); |
| 1778 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1779 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1780 | if (MaskedValueIsZero(Op0, AndRHS)) // LHS & RHS == 0 |
| 1781 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1782 | |
| 1783 | // If the mask is not masking out any bits, there is no reason to do the |
| 1784 | // and in the first place. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1785 | ConstantIntegral *NotAndRHS = |
Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1786 | cast<ConstantIntegral>(ConstantExpr::getNot(AndRHS)); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1787 | if (MaskedValueIsZero(Op0, NotAndRHS)) |
Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1788 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1789 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1790 | // Optimize a variety of ((val OP C1) & C2) combinations... |
| 1791 | if (isa<BinaryOperator>(Op0) || isa<ShiftInst>(Op0)) { |
| 1792 | Instruction *Op0I = cast<Instruction>(Op0); |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1793 | Value *Op0LHS = Op0I->getOperand(0); |
| 1794 | Value *Op0RHS = Op0I->getOperand(1); |
| 1795 | switch (Op0I->getOpcode()) { |
| 1796 | case Instruction::Xor: |
| 1797 | case Instruction::Or: |
| 1798 | // (X ^ V) & C2 --> (X & C2) iff (V & C2) == 0 |
| 1799 | // (X | V) & C2 --> (X & C2) iff (V & C2) == 0 |
| 1800 | if (MaskedValueIsZero(Op0LHS, AndRHS)) |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1801 | return BinaryOperator::createAnd(Op0RHS, AndRHS); |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1802 | if (MaskedValueIsZero(Op0RHS, AndRHS)) |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1803 | return BinaryOperator::createAnd(Op0LHS, AndRHS); |
Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1804 | |
| 1805 | // If the mask is only needed on one incoming arm, push it up. |
| 1806 | if (Op0I->hasOneUse()) { |
| 1807 | if (MaskedValueIsZero(Op0LHS, NotAndRHS)) { |
| 1808 | // Not masking anything out for the LHS, move to RHS. |
| 1809 | Instruction *NewRHS = BinaryOperator::createAnd(Op0RHS, AndRHS, |
| 1810 | Op0RHS->getName()+".masked"); |
| 1811 | InsertNewInstBefore(NewRHS, I); |
| 1812 | return BinaryOperator::create( |
| 1813 | cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1814 | } |
Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1815 | if (!isa<Constant>(NotAndRHS) && |
| 1816 | MaskedValueIsZero(Op0RHS, NotAndRHS)) { |
| 1817 | // Not masking anything out for the RHS, move to LHS. |
| 1818 | Instruction *NewLHS = BinaryOperator::createAnd(Op0LHS, AndRHS, |
| 1819 | Op0LHS->getName()+".masked"); |
| 1820 | InsertNewInstBefore(NewLHS, I); |
| 1821 | return BinaryOperator::create( |
| 1822 | cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS); |
| 1823 | } |
| 1824 | } |
| 1825 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1826 | break; |
| 1827 | case Instruction::And: |
| 1828 | // (X & V) & C2 --> 0 iff (V & C2) == 0 |
| 1829 | if (MaskedValueIsZero(Op0LHS, AndRHS) || |
| 1830 | MaskedValueIsZero(Op0RHS, AndRHS)) |
| 1831 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1832 | break; |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 1833 | case Instruction::Add: |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 1834 | // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS. |
| 1835 | // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 1836 | // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 1837 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I)) |
| 1838 | return BinaryOperator::createAnd(V, AndRHS); |
| 1839 | if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I)) |
| 1840 | return BinaryOperator::createAnd(V, AndRHS); // Add commutes |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 1841 | break; |
| 1842 | |
| 1843 | case Instruction::Sub: |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 1844 | // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS. |
| 1845 | // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 1846 | // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 1847 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I)) |
| 1848 | return BinaryOperator::createAnd(V, AndRHS); |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 1849 | break; |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
Chris Lattner | 16464b3 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 1852 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1853 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1854 | return Res; |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1855 | } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
| 1856 | const Type *SrcTy = CI->getOperand(0)->getType(); |
| 1857 | |
Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 1858 | // If this is an integer truncation or change from signed-to-unsigned, and |
| 1859 | // if the source is an and/or with immediate, transform it. This |
| 1860 | // frequently occurs for bitfield accesses. |
| 1861 | if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) { |
| 1862 | if (SrcTy->getPrimitiveSizeInBits() >= |
| 1863 | I.getType()->getPrimitiveSizeInBits() && |
| 1864 | CastOp->getNumOperands() == 2) |
| 1865 | if (ConstantInt *AndCI =dyn_cast<ConstantInt>(CastOp->getOperand(1))) |
| 1866 | if (CastOp->getOpcode() == Instruction::And) { |
| 1867 | // Change: and (cast (and X, C1) to T), C2 |
| 1868 | // into : and (cast X to T), trunc(C1)&C2 |
| 1869 | // This will folds the two ands together, which may allow other |
| 1870 | // simplifications. |
| 1871 | Instruction *NewCast = |
| 1872 | new CastInst(CastOp->getOperand(0), I.getType(), |
| 1873 | CastOp->getName()+".shrunk"); |
| 1874 | NewCast = InsertNewInstBefore(NewCast, I); |
| 1875 | |
| 1876 | Constant *C3=ConstantExpr::getCast(AndCI, I.getType());//trunc(C1) |
| 1877 | C3 = ConstantExpr::getAnd(C3, AndRHS); // trunc(C1)&C2 |
| 1878 | return BinaryOperator::createAnd(NewCast, C3); |
| 1879 | } else if (CastOp->getOpcode() == Instruction::Or) { |
| 1880 | // Change: and (cast (or X, C1) to T), C2 |
| 1881 | // into : trunc(C1)&C2 iff trunc(C1)&C2 == C2 |
| 1882 | Constant *C3=ConstantExpr::getCast(AndCI, I.getType());//trunc(C1) |
| 1883 | if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS) // trunc(C1)&C2 |
| 1884 | return ReplaceInstUsesWith(I, AndRHS); |
| 1885 | } |
| 1886 | } |
| 1887 | |
| 1888 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1889 | // If this is an integer sign or zero extension instruction. |
| 1890 | if (SrcTy->isIntegral() && |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1891 | SrcTy->getPrimitiveSizeInBits() < |
| 1892 | CI->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1893 | |
| 1894 | if (SrcTy->isUnsigned()) { |
| 1895 | // See if this and is clearing out bits that are known to be zero |
| 1896 | // anyway (due to the zero extension). |
| 1897 | Constant *Mask = ConstantIntegral::getAllOnesValue(SrcTy); |
| 1898 | Mask = ConstantExpr::getZeroExtend(Mask, CI->getType()); |
| 1899 | Constant *Result = ConstantExpr::getAnd(Mask, AndRHS); |
| 1900 | if (Result == Mask) // The "and" isn't doing anything, remove it. |
| 1901 | return ReplaceInstUsesWith(I, CI); |
| 1902 | if (Result != AndRHS) { // Reduce the and RHS constant. |
| 1903 | I.setOperand(1, Result); |
| 1904 | return &I; |
| 1905 | } |
| 1906 | |
| 1907 | } else { |
| 1908 | if (CI->hasOneUse() && SrcTy->isInteger()) { |
| 1909 | // We can only do this if all of the sign bits brought in are masked |
| 1910 | // out. Compute this by first getting 0000011111, then inverting |
| 1911 | // it. |
| 1912 | Constant *Mask = ConstantIntegral::getAllOnesValue(SrcTy); |
| 1913 | Mask = ConstantExpr::getZeroExtend(Mask, CI->getType()); |
| 1914 | Mask = ConstantExpr::getNot(Mask); // 1's in the new bits. |
| 1915 | if (ConstantExpr::getAnd(Mask, AndRHS)->isNullValue()) { |
| 1916 | // If the and is clearing all of the sign bits, change this to a |
| 1917 | // zero extension cast. To do this, cast the cast input to |
| 1918 | // unsigned, then to the requested size. |
| 1919 | Value *CastOp = CI->getOperand(0); |
| 1920 | Instruction *NC = |
| 1921 | new CastInst(CastOp, CastOp->getType()->getUnsignedVersion(), |
| 1922 | CI->getName()+".uns"); |
| 1923 | NC = InsertNewInstBefore(NC, I); |
| 1924 | // Finally, insert a replacement for CI. |
| 1925 | NC = new CastInst(NC, CI->getType(), CI->getName()); |
| 1926 | CI->setName(""); |
| 1927 | NC = InsertNewInstBefore(NC, I); |
| 1928 | WorkList.push_back(CI); // Delete CI later. |
| 1929 | I.setOperand(0, NC); |
| 1930 | return &I; // The AND operand was modified. |
| 1931 | } |
| 1932 | } |
| 1933 | } |
| 1934 | } |
Chris Lattner | 33217db | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 1935 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1936 | |
| 1937 | // Try to fold constant and into select arguments. |
| 1938 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1939 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1940 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1941 | if (isa<PHINode>(Op0)) |
| 1942 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1943 | return NV; |
Chris Lattner | 49b47ae | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1946 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 1947 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1948 | |
Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 1949 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
| 1950 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1951 | |
Misha Brukman | 9c003d8 | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 1952 | // (~A & ~B) == (~(A | B)) - De Morgan's Law |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1953 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1954 | Instruction *Or = BinaryOperator::createOr(Op0NotVal, Op1NotVal, |
| 1955 | I.getName()+".demorgan"); |
Chris Lattner | 49b47ae | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 1956 | InsertNewInstBefore(Or, I); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1957 | return BinaryOperator::createNot(Or); |
| 1958 | } |
| 1959 | |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 1960 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(Op1)) { |
| 1961 | // (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B) |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1962 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 1963 | return R; |
| 1964 | |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 1965 | Value *LHSVal, *RHSVal; |
| 1966 | ConstantInt *LHSCst, *RHSCst; |
| 1967 | Instruction::BinaryOps LHSCC, RHSCC; |
| 1968 | if (match(Op0, m_SetCond(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 1969 | if (match(RHS, m_SetCond(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 1970 | if (LHSVal == RHSVal && // Found (X setcc C1) & (X setcc C2) |
| 1971 | // Set[GL]E X, CST is folded to Set[GL]T elsewhere. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1972 | LHSCC != Instruction::SetGE && LHSCC != Instruction::SetLE && |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 1973 | RHSCC != Instruction::SetGE && RHSCC != Instruction::SetLE) { |
| 1974 | // Ensure that the larger constant is on the RHS. |
| 1975 | Constant *Cmp = ConstantExpr::getSetGT(LHSCst, RHSCst); |
| 1976 | SetCondInst *LHS = cast<SetCondInst>(Op0); |
| 1977 | if (cast<ConstantBool>(Cmp)->getValue()) { |
| 1978 | std::swap(LHS, RHS); |
| 1979 | std::swap(LHSCst, RHSCst); |
| 1980 | std::swap(LHSCC, RHSCC); |
| 1981 | } |
| 1982 | |
| 1983 | // At this point, we know we have have two setcc instructions |
| 1984 | // comparing a value against two constants and and'ing the result |
| 1985 | // together. Because of the above check, we know that we only have |
| 1986 | // SetEQ, SetNE, SetLT, and SetGT here. We also know (from the |
| 1987 | // FoldSetCCLogical check above), that the two constants are not |
| 1988 | // equal. |
| 1989 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 1990 | |
| 1991 | switch (LHSCC) { |
| 1992 | default: assert(0 && "Unknown integer condition code!"); |
| 1993 | case Instruction::SetEQ: |
| 1994 | switch (RHSCC) { |
| 1995 | default: assert(0 && "Unknown integer condition code!"); |
| 1996 | case Instruction::SetEQ: // (X == 13 & X == 15) -> false |
| 1997 | case Instruction::SetGT: // (X == 13 & X > 15) -> false |
| 1998 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1999 | case Instruction::SetNE: // (X == 13 & X != 15) -> X == 13 |
| 2000 | case Instruction::SetLT: // (X == 13 & X < 15) -> X == 13 |
| 2001 | return ReplaceInstUsesWith(I, LHS); |
| 2002 | } |
| 2003 | case Instruction::SetNE: |
| 2004 | switch (RHSCC) { |
| 2005 | default: assert(0 && "Unknown integer condition code!"); |
| 2006 | case Instruction::SetLT: |
| 2007 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X < 14) -> X < 13 |
| 2008 | return new SetCondInst(Instruction::SetLT, LHSVal, LHSCst); |
| 2009 | break; // (X != 13 & X < 15) -> no change |
| 2010 | case Instruction::SetEQ: // (X != 13 & X == 15) -> X == 15 |
| 2011 | case Instruction::SetGT: // (X != 13 & X > 15) -> X > 15 |
| 2012 | return ReplaceInstUsesWith(I, RHS); |
| 2013 | case Instruction::SetNE: |
| 2014 | if (LHSCst == SubOne(RHSCst)) {// (X != 13 & X != 14) -> X-13 >u 1 |
| 2015 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 2016 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 2017 | LHSVal->getName()+".off"); |
| 2018 | InsertNewInstBefore(Add, I); |
| 2019 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 2020 | Value *OffsetVal = InsertCastBefore(Add, UnsType, I); |
| 2021 | AddCST = ConstantExpr::getSub(RHSCst, LHSCst); |
| 2022 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 2023 | return new SetCondInst(Instruction::SetGT, OffsetVal, AddCST); |
| 2024 | } |
| 2025 | break; // (X != 13 & X != 15) -> no change |
| 2026 | } |
| 2027 | break; |
| 2028 | case Instruction::SetLT: |
| 2029 | switch (RHSCC) { |
| 2030 | default: assert(0 && "Unknown integer condition code!"); |
| 2031 | case Instruction::SetEQ: // (X < 13 & X == 15) -> false |
| 2032 | case Instruction::SetGT: // (X < 13 & X > 15) -> false |
| 2033 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2034 | case Instruction::SetNE: // (X < 13 & X != 15) -> X < 13 |
| 2035 | case Instruction::SetLT: // (X < 13 & X < 15) -> X < 13 |
| 2036 | return ReplaceInstUsesWith(I, LHS); |
| 2037 | } |
| 2038 | case Instruction::SetGT: |
| 2039 | switch (RHSCC) { |
| 2040 | default: assert(0 && "Unknown integer condition code!"); |
| 2041 | case Instruction::SetEQ: // (X > 13 & X == 15) -> X > 13 |
| 2042 | return ReplaceInstUsesWith(I, LHS); |
| 2043 | case Instruction::SetGT: // (X > 13 & X > 15) -> X > 15 |
| 2044 | return ReplaceInstUsesWith(I, RHS); |
| 2045 | case Instruction::SetNE: |
| 2046 | if (RHSCst == AddOne(LHSCst)) // (X > 13 & X != 14) -> X > 14 |
| 2047 | return new SetCondInst(Instruction::SetGT, LHSVal, RHSCst); |
| 2048 | break; // (X > 13 & X != 15) -> no change |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2049 | case Instruction::SetLT: // (X > 13 & X < 15) -> (X-14) <u 1 |
| 2050 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, true, I); |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 2051 | } |
| 2052 | } |
| 2053 | } |
| 2054 | } |
| 2055 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2056 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2059 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2060 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2061 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2062 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2063 | if (isa<UndefValue>(Op1)) |
| 2064 | return ReplaceInstUsesWith(I, // X | undef -> -1 |
| 2065 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 2066 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2067 | // or X, X = X or X, 0 == X |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2068 | if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) |
| 2069 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2070 | |
| 2071 | // or X, -1 == -1 |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 2072 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2073 | // If X is known to only contain bits that already exist in RHS, just |
| 2074 | // replace this instruction with RHS directly. |
| 2075 | if (MaskedValueIsZero(Op0, |
| 2076 | cast<ConstantIntegral>(ConstantExpr::getNot(RHS)))) |
| 2077 | return ReplaceInstUsesWith(I, RHS); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2078 | |
Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2079 | ConstantInt *C1 = 0; Value *X = 0; |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2080 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
| 2081 | if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { |
Chris Lattner | b62f508 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 2082 | Instruction *Or = BinaryOperator::createOr(X, RHS, Op0->getName()); |
| 2083 | Op0->setName(""); |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2084 | InsertNewInstBefore(Or, I); |
| 2085 | return BinaryOperator::createAnd(Or, ConstantExpr::getOr(RHS, C1)); |
| 2086 | } |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 2087 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2088 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
| 2089 | if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { |
| 2090 | std::string Op0Name = Op0->getName(); Op0->setName(""); |
| 2091 | Instruction *Or = BinaryOperator::createOr(X, RHS, Op0Name); |
| 2092 | InsertNewInstBefore(Or, I); |
| 2093 | return BinaryOperator::createXor(Or, |
| 2094 | ConstantExpr::getAnd(C1, ConstantExpr::getNot(RHS))); |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 2095 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2096 | |
| 2097 | // Try to fold constant and into select arguments. |
| 2098 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2099 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2100 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2101 | if (isa<PHINode>(Op0)) |
| 2102 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2103 | return NV; |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2106 | Value *A = 0, *B = 0; |
| 2107 | ConstantInt *C1 = 0, *C2 = 0; |
Chris Lattner | 4294cec | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 2108 | |
| 2109 | if (match(Op0, m_And(m_Value(A), m_Value(B)))) |
| 2110 | if (A == Op1 || B == Op1) // (A & ?) | A --> A |
| 2111 | return ReplaceInstUsesWith(I, Op1); |
| 2112 | if (match(Op1, m_And(m_Value(A), m_Value(B)))) |
| 2113 | if (A == Op0 || B == Op0) // A | (A & ?) --> A |
| 2114 | return ReplaceInstUsesWith(I, Op0); |
| 2115 | |
Chris Lattner | b62f508 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 2116 | // (X^C)|Y -> (X|Y)^C iff Y&C == 0 |
| 2117 | if (Op0->hasOneUse() && match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) && |
| 2118 | MaskedValueIsZero(Op1, C1)) { |
| 2119 | Instruction *NOr = BinaryOperator::createOr(A, Op1, Op0->getName()); |
| 2120 | Op0->setName(""); |
| 2121 | return BinaryOperator::createXor(InsertNewInstBefore(NOr, I), C1); |
| 2122 | } |
| 2123 | |
| 2124 | // Y|(X^C) -> (X|Y)^C iff Y&C == 0 |
| 2125 | if (Op1->hasOneUse() && match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) && |
| 2126 | MaskedValueIsZero(Op0, C1)) { |
| 2127 | Instruction *NOr = BinaryOperator::createOr(A, Op0, Op1->getName()); |
| 2128 | Op0->setName(""); |
| 2129 | return BinaryOperator::createXor(InsertNewInstBefore(NOr, I), C1); |
| 2130 | } |
| 2131 | |
Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 2132 | // (A & C1)|(B & C2) |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2133 | if (match(Op0, m_And(m_Value(A), m_ConstantInt(C1))) && |
Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 2134 | match(Op1, m_And(m_Value(B), m_ConstantInt(C2)))) { |
| 2135 | |
| 2136 | if (A == B) // (A & C1)|(A & C2) == A & (C1|C2) |
| 2137 | return BinaryOperator::createAnd(A, ConstantExpr::getOr(C1, C2)); |
| 2138 | |
| 2139 | |
Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 2140 | // If we have: ((V + N) & C1) | (V & C2) |
| 2141 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 2142 | // replace with V+N. |
| 2143 | if (C1 == ConstantExpr::getNot(C2)) { |
Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2144 | Value *V1 = 0, *V2 = 0; |
Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 2145 | if ((C2->getRawValue() & (C2->getRawValue()+1)) == 0 && // C2 == 0+1+ |
| 2146 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { |
| 2147 | // Add commutes, try both ways. |
| 2148 | if (V1 == B && MaskedValueIsZero(V2, C2)) |
| 2149 | return ReplaceInstUsesWith(I, A); |
| 2150 | if (V2 == B && MaskedValueIsZero(V1, C2)) |
| 2151 | return ReplaceInstUsesWith(I, A); |
| 2152 | } |
| 2153 | // Or commutes, try both ways. |
| 2154 | if ((C1->getRawValue() & (C1->getRawValue()+1)) == 0 && |
| 2155 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { |
| 2156 | // Add commutes, try both ways. |
| 2157 | if (V1 == A && MaskedValueIsZero(V2, C1)) |
| 2158 | return ReplaceInstUsesWith(I, B); |
| 2159 | if (V2 == A && MaskedValueIsZero(V1, C1)) |
| 2160 | return ReplaceInstUsesWith(I, B); |
Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 2161 | } |
| 2162 | } |
| 2163 | } |
Chris Lattner | 812aab7 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 2164 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2165 | if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1 |
| 2166 | if (A == Op1) // ~A | A == -1 |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2167 | return ReplaceInstUsesWith(I, |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2168 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 2169 | } else { |
| 2170 | A = 0; |
| 2171 | } |
Chris Lattner | 4294cec | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 2172 | // Note, A is still live here! |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2173 | if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B |
| 2174 | if (Op0 == B) |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2175 | return ReplaceInstUsesWith(I, |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2176 | ConstantIntegral::getAllOnesValue(I.getType())); |
Chris Lattner | 3e327a4 | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 2177 | |
Misha Brukman | 9c003d8 | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 2178 | // (~A | ~B) == (~(A & B)) - De Morgan's Law |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2179 | if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
| 2180 | Value *And = InsertNewInstBefore(BinaryOperator::createAnd(A, B, |
| 2181 | I.getName()+".demorgan"), I); |
| 2182 | return BinaryOperator::createNot(And); |
| 2183 | } |
Chris Lattner | 3e327a4 | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 2184 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2185 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2186 | // (setcc1 A, B) | (setcc2 A, B) --> (setcc3 A, B) |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2187 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1))) { |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2188 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 2189 | return R; |
| 2190 | |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2191 | Value *LHSVal, *RHSVal; |
| 2192 | ConstantInt *LHSCst, *RHSCst; |
| 2193 | Instruction::BinaryOps LHSCC, RHSCC; |
| 2194 | if (match(Op0, m_SetCond(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 2195 | if (match(RHS, m_SetCond(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 2196 | if (LHSVal == RHSVal && // Found (X setcc C1) | (X setcc C2) |
| 2197 | // Set[GL]E X, CST is folded to Set[GL]T elsewhere. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2198 | LHSCC != Instruction::SetGE && LHSCC != Instruction::SetLE && |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2199 | RHSCC != Instruction::SetGE && RHSCC != Instruction::SetLE) { |
| 2200 | // Ensure that the larger constant is on the RHS. |
| 2201 | Constant *Cmp = ConstantExpr::getSetGT(LHSCst, RHSCst); |
| 2202 | SetCondInst *LHS = cast<SetCondInst>(Op0); |
| 2203 | if (cast<ConstantBool>(Cmp)->getValue()) { |
| 2204 | std::swap(LHS, RHS); |
| 2205 | std::swap(LHSCst, RHSCst); |
| 2206 | std::swap(LHSCC, RHSCC); |
| 2207 | } |
| 2208 | |
| 2209 | // At this point, we know we have have two setcc instructions |
| 2210 | // comparing a value against two constants and or'ing the result |
| 2211 | // together. Because of the above check, we know that we only have |
| 2212 | // SetEQ, SetNE, SetLT, and SetGT here. We also know (from the |
| 2213 | // FoldSetCCLogical check above), that the two constants are not |
| 2214 | // equal. |
| 2215 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 2216 | |
| 2217 | switch (LHSCC) { |
| 2218 | default: assert(0 && "Unknown integer condition code!"); |
| 2219 | case Instruction::SetEQ: |
| 2220 | switch (RHSCC) { |
| 2221 | default: assert(0 && "Unknown integer condition code!"); |
| 2222 | case Instruction::SetEQ: |
| 2223 | if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13 <u 2 |
| 2224 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 2225 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 2226 | LHSVal->getName()+".off"); |
| 2227 | InsertNewInstBefore(Add, I); |
| 2228 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 2229 | Value *OffsetVal = InsertCastBefore(Add, UnsType, I); |
| 2230 | AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst); |
| 2231 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 2232 | return new SetCondInst(Instruction::SetLT, OffsetVal, AddCST); |
| 2233 | } |
| 2234 | break; // (X == 13 | X == 15) -> no change |
| 2235 | |
Chris Lattner | 5c21946 | 2005-04-19 06:04:18 +0000 | [diff] [blame] | 2236 | case Instruction::SetGT: // (X == 13 | X > 14) -> no change |
| 2237 | break; |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2238 | case Instruction::SetNE: // (X == 13 | X != 15) -> X != 15 |
| 2239 | case Instruction::SetLT: // (X == 13 | X < 15) -> X < 15 |
| 2240 | return ReplaceInstUsesWith(I, RHS); |
| 2241 | } |
| 2242 | break; |
| 2243 | case Instruction::SetNE: |
| 2244 | switch (RHSCC) { |
| 2245 | default: assert(0 && "Unknown integer condition code!"); |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2246 | case Instruction::SetEQ: // (X != 13 | X == 15) -> X != 13 |
| 2247 | case Instruction::SetGT: // (X != 13 | X > 15) -> X != 13 |
| 2248 | return ReplaceInstUsesWith(I, LHS); |
| 2249 | case Instruction::SetNE: // (X != 13 | X != 15) -> true |
Chris Lattner | 2ceb6ee | 2005-06-17 03:59:17 +0000 | [diff] [blame] | 2250 | case Instruction::SetLT: // (X != 13 | X < 15) -> true |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2251 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2252 | } |
| 2253 | break; |
| 2254 | case Instruction::SetLT: |
| 2255 | switch (RHSCC) { |
| 2256 | default: assert(0 && "Unknown integer condition code!"); |
| 2257 | case Instruction::SetEQ: // (X < 13 | X == 14) -> no change |
| 2258 | break; |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2259 | case Instruction::SetGT: // (X < 13 | X > 15) -> (X-13) > 2 |
| 2260 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false, I); |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2261 | case Instruction::SetNE: // (X < 13 | X != 15) -> X != 15 |
| 2262 | case Instruction::SetLT: // (X < 13 | X < 15) -> X < 15 |
| 2263 | return ReplaceInstUsesWith(I, RHS); |
| 2264 | } |
| 2265 | break; |
| 2266 | case Instruction::SetGT: |
| 2267 | switch (RHSCC) { |
| 2268 | default: assert(0 && "Unknown integer condition code!"); |
| 2269 | case Instruction::SetEQ: // (X > 13 | X == 15) -> X > 13 |
| 2270 | case Instruction::SetGT: // (X > 13 | X > 15) -> X > 13 |
| 2271 | return ReplaceInstUsesWith(I, LHS); |
| 2272 | case Instruction::SetNE: // (X > 13 | X != 15) -> true |
| 2273 | case Instruction::SetLT: // (X > 13 | X < 15) -> true |
| 2274 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2275 | } |
| 2276 | } |
| 2277 | } |
| 2278 | } |
Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 2279 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2280 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 2283 | // XorSelf - Implements: X ^ X --> 0 |
| 2284 | struct XorSelf { |
| 2285 | Value *RHS; |
| 2286 | XorSelf(Value *rhs) : RHS(rhs) {} |
| 2287 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 2288 | Instruction *apply(BinaryOperator &Xor) const { |
| 2289 | return &Xor; |
| 2290 | } |
| 2291 | }; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2292 | |
| 2293 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2294 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2295 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2296 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2297 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2298 | if (isa<UndefValue>(Op1)) |
| 2299 | return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef |
| 2300 | |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 2301 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
| 2302 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { |
| 2303 | assert(Result == &I && "AssociativeOpt didn't work?"); |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2304 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 2305 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2306 | |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2307 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2308 | // xor X, 0 == X |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2309 | if (RHS->isNullValue()) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2310 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2311 | |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2312 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 2313 | // xor (setcc A, B), true = not (setcc A, B) = setncc A, B |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2314 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(Op0I)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2315 | if (RHS == ConstantBool::True && SCI->hasOneUse()) |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 2316 | return new SetCondInst(SCI->getInverseCondition(), |
| 2317 | SCI->getOperand(0), SCI->getOperand(1)); |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 2318 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2319 | // ~(c-X) == X-c-1 == X+(-c-1) |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2320 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
| 2321 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2322 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); |
| 2323 | Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2324 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2325 | return BinaryOperator::createAdd(Op0I->getOperand(1), ConstantRHS); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2326 | } |
Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 2327 | |
| 2328 | // ~(~X & Y) --> (X | ~Y) |
| 2329 | if (Op0I->getOpcode() == Instruction::And && RHS->isAllOnesValue()) { |
| 2330 | if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands(); |
| 2331 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { |
| 2332 | Instruction *NotY = |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2333 | BinaryOperator::createNot(Op0I->getOperand(1), |
Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 2334 | Op0I->getOperand(1)->getName()+".not"); |
| 2335 | InsertNewInstBefore(NotY, I); |
| 2336 | return BinaryOperator::createOr(Op0NotVal, NotY); |
| 2337 | } |
| 2338 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2339 | |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2340 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 2341 | switch (Op0I->getOpcode()) { |
| 2342 | case Instruction::Add: |
Chris Lattner | 0f68fa6 | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 2343 | // ~(X-c) --> (-c-1)-X |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2344 | if (RHS->isAllOnesValue()) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2345 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); |
| 2346 | return BinaryOperator::createSub( |
| 2347 | ConstantExpr::getSub(NegOp0CI, |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2348 | ConstantInt::get(I.getType(), 1)), |
Chris Lattner | 0f68fa6 | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 2349 | Op0I->getOperand(0)); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2350 | } |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 2351 | break; |
| 2352 | case Instruction::And: |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2353 | // (X & C1) ^ C2 --> (X & C1) | C2 iff (C1&C2) == 0 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2354 | if (ConstantExpr::getAnd(RHS, Op0CI)->isNullValue()) |
| 2355 | return BinaryOperator::createOr(Op0, RHS); |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 2356 | break; |
| 2357 | case Instruction::Or: |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2358 | // (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2359 | if (ConstantExpr::getAnd(RHS, Op0CI) == RHS) |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 2360 | return BinaryOperator::createAnd(Op0, ConstantExpr::getNot(RHS)); |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 2361 | break; |
| 2362 | default: break; |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2363 | } |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 2364 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2365 | |
| 2366 | // Try to fold constant and into select arguments. |
| 2367 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2368 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2369 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2370 | if (isa<PHINode>(Op0)) |
| 2371 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2372 | return NV; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2375 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2376 | if (X == Op1) |
| 2377 | return ReplaceInstUsesWith(I, |
| 2378 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 2379 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2380 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2381 | if (X == Op0) |
| 2382 | return ReplaceInstUsesWith(I, |
| 2383 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 2384 | |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2385 | if (Instruction *Op1I = dyn_cast<Instruction>(Op1)) |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 2386 | if (Op1I->getOpcode() == Instruction::Or) { |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2387 | if (Op1I->getOperand(0) == Op0) { // B^(B|A) == (A|B)^B |
| 2388 | cast<BinaryOperator>(Op1I)->swapOperands(); |
| 2389 | I.swapOperands(); |
| 2390 | std::swap(Op0, Op1); |
| 2391 | } else if (Op1I->getOperand(1) == Op0) { // B^(A|B) == (A|B)^B |
| 2392 | I.swapOperands(); |
| 2393 | std::swap(Op0, Op1); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2394 | } |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 2395 | } else if (Op1I->getOpcode() == Instruction::Xor) { |
| 2396 | if (Op0 == Op1I->getOperand(0)) // A^(A^B) == B |
| 2397 | return ReplaceInstUsesWith(I, Op1I->getOperand(1)); |
| 2398 | else if (Op0 == Op1I->getOperand(1)) // A^(B^A) == B |
| 2399 | return ReplaceInstUsesWith(I, Op1I->getOperand(0)); |
| 2400 | } |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2401 | |
| 2402 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2403 | if (Op0I->getOpcode() == Instruction::Or && Op0I->hasOneUse()) { |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2404 | if (Op0I->getOperand(0) == Op1) // (B|A)^B == (A|B)^B |
| 2405 | cast<BinaryOperator>(Op0I)->swapOperands(); |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2406 | if (Op0I->getOperand(1) == Op1) { // (A|B)^B == A & ~B |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 2407 | Value *NotB = InsertNewInstBefore(BinaryOperator::createNot(Op1, |
| 2408 | Op1->getName()+".not"), I); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2409 | return BinaryOperator::createAnd(Op0I->getOperand(0), NotB); |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2410 | } |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 2411 | } else if (Op0I->getOpcode() == Instruction::Xor) { |
| 2412 | if (Op1 == Op0I->getOperand(0)) // (A^B)^A == B |
| 2413 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 2414 | else if (Op1 == Op0I->getOperand(1)) // (B^A)^A == B |
| 2415 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
Chris Lattner | 7aa2d47 | 2004-08-01 19:42:59 +0000 | [diff] [blame] | 2418 | // (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2419 | ConstantInt *C1 = 0, *C2 = 0; |
| 2420 | if (match(Op0, m_And(m_Value(), m_ConstantInt(C1))) && |
| 2421 | match(Op1, m_And(m_Value(), m_ConstantInt(C2))) && |
Chris Lattner | 7aa2d47 | 2004-08-01 19:42:59 +0000 | [diff] [blame] | 2422 | ConstantExpr::getAnd(C1, C2)->isNullValue()) |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2423 | return BinaryOperator::createOr(Op0, Op1); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 2424 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2425 | // (setcc1 A, B) ^ (setcc2 A, B) --> (setcc3 A, B) |
| 2426 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1))) |
| 2427 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 2428 | return R; |
| 2429 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2430 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2431 | } |
| 2432 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2433 | /// MulWithOverflow - Compute Result = In1*In2, returning true if the result |
| 2434 | /// overflowed for this type. |
| 2435 | static bool MulWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
| 2436 | ConstantInt *In2) { |
| 2437 | Result = cast<ConstantInt>(ConstantExpr::getMul(In1, In2)); |
| 2438 | return !In2->isNullValue() && ConstantExpr::getDiv(Result, In2) != In1; |
| 2439 | } |
| 2440 | |
| 2441 | static bool isPositive(ConstantInt *C) { |
| 2442 | return cast<ConstantSInt>(C)->getValue() >= 0; |
| 2443 | } |
| 2444 | |
| 2445 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result |
| 2446 | /// overflowed for this type. |
| 2447 | static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
| 2448 | ConstantInt *In2) { |
| 2449 | Result = cast<ConstantInt>(ConstantExpr::getAdd(In1, In2)); |
| 2450 | |
| 2451 | if (In1->getType()->isUnsigned()) |
| 2452 | return cast<ConstantUInt>(Result)->getValue() < |
| 2453 | cast<ConstantUInt>(In1)->getValue(); |
| 2454 | if (isPositive(In1) != isPositive(In2)) |
| 2455 | return false; |
| 2456 | if (isPositive(In1)) |
| 2457 | return cast<ConstantSInt>(Result)->getValue() < |
| 2458 | cast<ConstantSInt>(In1)->getValue(); |
| 2459 | return cast<ConstantSInt>(Result)->getValue() > |
| 2460 | cast<ConstantSInt>(In1)->getValue(); |
| 2461 | } |
| 2462 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2463 | /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the |
| 2464 | /// code necessary to compute the offset from the base pointer (without adding |
| 2465 | /// in the base pointer). Return the result as a signed integer of intptr size. |
| 2466 | static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { |
| 2467 | TargetData &TD = IC.getTargetData(); |
| 2468 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 2469 | const Type *UIntPtrTy = TD.getIntPtrType(); |
| 2470 | const Type *SIntPtrTy = UIntPtrTy->getSignedVersion(); |
| 2471 | Value *Result = Constant::getNullValue(SIntPtrTy); |
| 2472 | |
| 2473 | // Build a mask for high order bits. |
| 2474 | uint64_t PtrSizeMask = ~0ULL; |
| 2475 | PtrSizeMask >>= 64-(TD.getPointerSize()*8); |
| 2476 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2477 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { |
| 2478 | Value *Op = GEP->getOperand(i); |
Chris Lattner | d35d210 | 2005-01-13 23:26:48 +0000 | [diff] [blame] | 2479 | uint64_t Size = TD.getTypeSize(GTI.getIndexedType()) & PtrSizeMask; |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2480 | Constant *Scale = ConstantExpr::getCast(ConstantUInt::get(UIntPtrTy, Size), |
| 2481 | SIntPtrTy); |
| 2482 | if (Constant *OpC = dyn_cast<Constant>(Op)) { |
| 2483 | if (!OpC->isNullValue()) { |
Chris Lattner | 4cb9fa3 | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 2484 | OpC = ConstantExpr::getCast(OpC, SIntPtrTy); |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2485 | Scale = ConstantExpr::getMul(OpC, Scale); |
| 2486 | if (Constant *RC = dyn_cast<Constant>(Result)) |
| 2487 | Result = ConstantExpr::getAdd(RC, Scale); |
| 2488 | else { |
| 2489 | // Emit an add instruction. |
| 2490 | Result = IC.InsertNewInstBefore( |
| 2491 | BinaryOperator::createAdd(Result, Scale, |
| 2492 | GEP->getName()+".offs"), I); |
| 2493 | } |
| 2494 | } |
| 2495 | } else { |
Chris Lattner | 7aa41cf | 2005-01-14 17:17:59 +0000 | [diff] [blame] | 2496 | // Convert to correct type. |
| 2497 | Op = IC.InsertNewInstBefore(new CastInst(Op, SIntPtrTy, |
| 2498 | Op->getName()+".c"), I); |
| 2499 | if (Size != 1) |
Chris Lattner | 4cb9fa3 | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 2500 | // We'll let instcombine(mul) convert this to a shl if possible. |
| 2501 | Op = IC.InsertNewInstBefore(BinaryOperator::createMul(Op, Scale, |
| 2502 | GEP->getName()+".idx"), I); |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2503 | |
| 2504 | // Emit an add instruction. |
Chris Lattner | 4cb9fa3 | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 2505 | Result = IC.InsertNewInstBefore(BinaryOperator::createAdd(Op, Result, |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2506 | GEP->getName()+".offs"), I); |
| 2507 | } |
| 2508 | } |
| 2509 | return Result; |
| 2510 | } |
| 2511 | |
| 2512 | /// FoldGEPSetCC - Fold comparisons between a GEP instruction and something |
| 2513 | /// else. At this point we know that the GEP is on the LHS of the comparison. |
| 2514 | Instruction *InstCombiner::FoldGEPSetCC(User *GEPLHS, Value *RHS, |
| 2515 | Instruction::BinaryOps Cond, |
| 2516 | Instruction &I) { |
| 2517 | assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!"); |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2518 | |
| 2519 | if (CastInst *CI = dyn_cast<CastInst>(RHS)) |
| 2520 | if (isa<PointerType>(CI->getOperand(0)->getType())) |
| 2521 | RHS = CI->getOperand(0); |
| 2522 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2523 | Value *PtrBase = GEPLHS->getOperand(0); |
| 2524 | if (PtrBase == RHS) { |
| 2525 | // As an optimization, we don't actually have to compute the actual value of |
| 2526 | // OFFSET if this is a seteq or setne comparison, just return whether each |
| 2527 | // index is zero or not. |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2528 | if (Cond == Instruction::SetEQ || Cond == Instruction::SetNE) { |
| 2529 | Instruction *InVal = 0; |
Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 2530 | gep_type_iterator GTI = gep_type_begin(GEPLHS); |
| 2531 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i, ++GTI) { |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2532 | bool EmitIt = true; |
| 2533 | if (Constant *C = dyn_cast<Constant>(GEPLHS->getOperand(i))) { |
| 2534 | if (isa<UndefValue>(C)) // undef index -> undef. |
| 2535 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); |
| 2536 | if (C->isNullValue()) |
| 2537 | EmitIt = false; |
Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 2538 | else if (TD->getTypeSize(GTI.getIndexedType()) == 0) { |
| 2539 | EmitIt = false; // This is indexing into a zero sized array? |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2540 | } else if (isa<ConstantInt>(C)) |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2541 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
| 2542 | ConstantBool::get(Cond == Instruction::SetNE)); |
| 2543 | } |
| 2544 | |
| 2545 | if (EmitIt) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2546 | Instruction *Comp = |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2547 | new SetCondInst(Cond, GEPLHS->getOperand(i), |
| 2548 | Constant::getNullValue(GEPLHS->getOperand(i)->getType())); |
| 2549 | if (InVal == 0) |
| 2550 | InVal = Comp; |
| 2551 | else { |
| 2552 | InVal = InsertNewInstBefore(InVal, I); |
| 2553 | InsertNewInstBefore(Comp, I); |
| 2554 | if (Cond == Instruction::SetNE) // True if any are unequal |
| 2555 | InVal = BinaryOperator::createOr(InVal, Comp); |
| 2556 | else // True if all are equal |
| 2557 | InVal = BinaryOperator::createAnd(InVal, Comp); |
| 2558 | } |
| 2559 | } |
| 2560 | } |
| 2561 | |
| 2562 | if (InVal) |
| 2563 | return InVal; |
| 2564 | else |
| 2565 | ReplaceInstUsesWith(I, // No comparison is needed here, all indexes = 0 |
| 2566 | ConstantBool::get(Cond == Instruction::SetEQ)); |
| 2567 | } |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2568 | |
| 2569 | // Only lower this if the setcc is the only user of the GEP or if we expect |
| 2570 | // the result to fold to a constant! |
| 2571 | if (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) { |
| 2572 | // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). |
| 2573 | Value *Offset = EmitGEPOffset(GEPLHS, I, *this); |
| 2574 | return new SetCondInst(Cond, Offset, |
| 2575 | Constant::getNullValue(Offset->getType())); |
| 2576 | } |
| 2577 | } else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) { |
Chris Lattner | a21bf8d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 2578 | // If the base pointers are different, but the indices are the same, just |
| 2579 | // compare the base pointer. |
| 2580 | if (PtrBase != GEPRHS->getOperand(0)) { |
| 2581 | bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands(); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2582 | IndicesTheSame &= GEPLHS->getOperand(0)->getType() == |
Chris Lattner | bd43b9d | 2005-04-26 14:40:41 +0000 | [diff] [blame] | 2583 | GEPRHS->getOperand(0)->getType(); |
Chris Lattner | a21bf8d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 2584 | if (IndicesTheSame) |
| 2585 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 2586 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 2587 | IndicesTheSame = false; |
| 2588 | break; |
| 2589 | } |
| 2590 | |
| 2591 | // If all indices are the same, just compare the base pointers. |
| 2592 | if (IndicesTheSame) |
| 2593 | return new SetCondInst(Cond, GEPLHS->getOperand(0), |
| 2594 | GEPRHS->getOperand(0)); |
| 2595 | |
| 2596 | // Otherwise, the base pointers are different and the indices are |
| 2597 | // different, bail out. |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2598 | return 0; |
Chris Lattner | a21bf8d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 2599 | } |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2600 | |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2601 | // If one of the GEPs has all zero indices, recurse. |
| 2602 | bool AllZeros = true; |
| 2603 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 2604 | if (!isa<Constant>(GEPLHS->getOperand(i)) || |
| 2605 | !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) { |
| 2606 | AllZeros = false; |
| 2607 | break; |
| 2608 | } |
| 2609 | if (AllZeros) |
| 2610 | return FoldGEPSetCC(GEPRHS, GEPLHS->getOperand(0), |
| 2611 | SetCondInst::getSwappedCondition(Cond), I); |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 2612 | |
| 2613 | // If the other GEP has all zero indices, recurse. |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2614 | AllZeros = true; |
| 2615 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 2616 | if (!isa<Constant>(GEPRHS->getOperand(i)) || |
| 2617 | !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) { |
| 2618 | AllZeros = false; |
| 2619 | break; |
| 2620 | } |
| 2621 | if (AllZeros) |
| 2622 | return FoldGEPSetCC(GEPLHS, GEPRHS->getOperand(0), Cond, I); |
| 2623 | |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 2624 | if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) { |
| 2625 | // If the GEPs only differ by one index, compare it. |
| 2626 | unsigned NumDifferences = 0; // Keep track of # differences. |
| 2627 | unsigned DiffOperand = 0; // The operand that differs. |
| 2628 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 2629 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2630 | if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() != |
| 2631 | GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | fc4429e | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 2632 | // Irreconcilable differences. |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 2633 | NumDifferences = 2; |
| 2634 | break; |
| 2635 | } else { |
| 2636 | if (NumDifferences++) break; |
| 2637 | DiffOperand = i; |
| 2638 | } |
| 2639 | } |
| 2640 | |
| 2641 | if (NumDifferences == 0) // SAME GEP? |
| 2642 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
| 2643 | ConstantBool::get(Cond == Instruction::SetEQ)); |
| 2644 | else if (NumDifferences == 1) { |
Chris Lattner | fc4429e | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 2645 | Value *LHSV = GEPLHS->getOperand(DiffOperand); |
| 2646 | Value *RHSV = GEPRHS->getOperand(DiffOperand); |
Chris Lattner | 247aef8 | 2005-07-18 23:07:33 +0000 | [diff] [blame] | 2647 | |
| 2648 | // Convert the operands to signed values to make sure to perform a |
| 2649 | // signed comparison. |
| 2650 | const Type *NewTy = LHSV->getType()->getSignedVersion(); |
| 2651 | if (LHSV->getType() != NewTy) |
| 2652 | LHSV = InsertNewInstBefore(new CastInst(LHSV, NewTy, |
| 2653 | LHSV->getName()), I); |
| 2654 | if (RHSV->getType() != NewTy) |
| 2655 | RHSV = InsertNewInstBefore(new CastInst(RHSV, NewTy, |
| 2656 | RHSV->getName()), I); |
| 2657 | return new SetCondInst(Cond, LHSV, RHSV); |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 2658 | } |
| 2659 | } |
| 2660 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2661 | // Only lower this if the setcc is the only user of the GEP or if we expect |
| 2662 | // the result to fold to a constant! |
| 2663 | if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) && |
| 2664 | (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) { |
| 2665 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) |
| 2666 | Value *L = EmitGEPOffset(GEPLHS, I, *this); |
| 2667 | Value *R = EmitGEPOffset(GEPRHS, I, *this); |
| 2668 | return new SetCondInst(Cond, L, R); |
| 2669 | } |
| 2670 | } |
| 2671 | return 0; |
| 2672 | } |
| 2673 | |
| 2674 | |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2675 | Instruction *InstCombiner::visitSetCondInst(SetCondInst &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2676 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2677 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2678 | const Type *Ty = Op0->getType(); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2679 | |
| 2680 | // setcc X, X |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2681 | if (Op0 == Op1) |
| 2682 | return ReplaceInstUsesWith(I, ConstantBool::get(isTrueWhenEqual(I))); |
Chris Lattner | 1fc23f3 | 2002-05-09 20:11:54 +0000 | [diff] [blame] | 2683 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2684 | if (isa<UndefValue>(Op1)) // X setcc undef -> undef |
| 2685 | return ReplaceInstUsesWith(I, UndefValue::get(Type::BoolTy)); |
| 2686 | |
Chris Lattner | 15ff1e1 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 2687 | // setcc <global/alloca*/null>, <global/alloca*/null> - Global/Stack value |
| 2688 | // addresses never equal each other! We already know that Op0 != Op1. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2689 | if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) || |
| 2690 | isa<ConstantPointerNull>(Op0)) && |
| 2691 | (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) || |
Chris Lattner | 15ff1e1 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 2692 | isa<ConstantPointerNull>(Op1))) |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2693 | return ReplaceInstUsesWith(I, ConstantBool::get(!isTrueWhenEqual(I))); |
| 2694 | |
| 2695 | // setcc's with boolean values can always be turned into bitwise operations |
| 2696 | if (Ty == Type::BoolTy) { |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 2697 | switch (I.getOpcode()) { |
| 2698 | default: assert(0 && "Invalid setcc instruction!"); |
| 2699 | case Instruction::SetEQ: { // seteq bool %A, %B -> ~(A^B) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2700 | Instruction *Xor = BinaryOperator::createXor(Op0, Op1, I.getName()+"tmp"); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2701 | InsertNewInstBefore(Xor, I); |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 2702 | return BinaryOperator::createNot(Xor); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2703 | } |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 2704 | case Instruction::SetNE: |
| 2705 | return BinaryOperator::createXor(Op0, Op1); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2706 | |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 2707 | case Instruction::SetGT: |
| 2708 | std::swap(Op0, Op1); // Change setgt -> setlt |
| 2709 | // FALL THROUGH |
| 2710 | case Instruction::SetLT: { // setlt bool A, B -> ~X & Y |
| 2711 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 2712 | InsertNewInstBefore(Not, I); |
| 2713 | return BinaryOperator::createAnd(Not, Op1); |
| 2714 | } |
| 2715 | case Instruction::SetGE: |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2716 | std::swap(Op0, Op1); // Change setge -> setle |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 2717 | // FALL THROUGH |
| 2718 | case Instruction::SetLE: { // setle bool %A, %B -> ~A | B |
| 2719 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 2720 | InsertNewInstBefore(Not, I); |
| 2721 | return BinaryOperator::createOr(Not, Op1); |
| 2722 | } |
| 2723 | } |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2724 | } |
| 2725 | |
Chris Lattner | 2dd0174 | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 2726 | // See if we are doing a comparison between a constant and an instruction that |
| 2727 | // can be folded into the comparison. |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2728 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2729 | // Check to see if we are comparing against the minimum or maximum value... |
| 2730 | if (CI->isMinValue()) { |
| 2731 | if (I.getOpcode() == Instruction::SetLT) // A < MIN -> FALSE |
| 2732 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2733 | if (I.getOpcode() == Instruction::SetGE) // A >= MIN -> TRUE |
| 2734 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2735 | if (I.getOpcode() == Instruction::SetLE) // A <= MIN -> A == MIN |
| 2736 | return BinaryOperator::createSetEQ(Op0, Op1); |
| 2737 | if (I.getOpcode() == Instruction::SetGT) // A > MIN -> A != MIN |
| 2738 | return BinaryOperator::createSetNE(Op0, Op1); |
| 2739 | |
| 2740 | } else if (CI->isMaxValue()) { |
| 2741 | if (I.getOpcode() == Instruction::SetGT) // A > MAX -> FALSE |
| 2742 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2743 | if (I.getOpcode() == Instruction::SetLE) // A <= MAX -> TRUE |
| 2744 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2745 | if (I.getOpcode() == Instruction::SetGE) // A >= MAX -> A == MAX |
| 2746 | return BinaryOperator::createSetEQ(Op0, Op1); |
| 2747 | if (I.getOpcode() == Instruction::SetLT) // A < MAX -> A != MAX |
| 2748 | return BinaryOperator::createSetNE(Op0, Op1); |
| 2749 | |
| 2750 | // Comparing against a value really close to min or max? |
| 2751 | } else if (isMinValuePlusOne(CI)) { |
| 2752 | if (I.getOpcode() == Instruction::SetLT) // A < MIN+1 -> A == MIN |
| 2753 | return BinaryOperator::createSetEQ(Op0, SubOne(CI)); |
| 2754 | if (I.getOpcode() == Instruction::SetGE) // A >= MIN-1 -> A != MIN |
| 2755 | return BinaryOperator::createSetNE(Op0, SubOne(CI)); |
| 2756 | |
| 2757 | } else if (isMaxValueMinusOne(CI)) { |
| 2758 | if (I.getOpcode() == Instruction::SetGT) // A > MAX-1 -> A == MAX |
| 2759 | return BinaryOperator::createSetEQ(Op0, AddOne(CI)); |
| 2760 | if (I.getOpcode() == Instruction::SetLE) // A <= MAX-1 -> A != MAX |
| 2761 | return BinaryOperator::createSetNE(Op0, AddOne(CI)); |
| 2762 | } |
| 2763 | |
| 2764 | // If we still have a setle or setge instruction, turn it into the |
| 2765 | // appropriate setlt or setgt instruction. Since the border cases have |
| 2766 | // already been handled above, this requires little checking. |
| 2767 | // |
| 2768 | if (I.getOpcode() == Instruction::SetLE) |
| 2769 | return BinaryOperator::createSetLT(Op0, AddOne(CI)); |
| 2770 | if (I.getOpcode() == Instruction::SetGE) |
| 2771 | return BinaryOperator::createSetGT(Op0, SubOne(CI)); |
| 2772 | |
Chris Lattner | e1e10e1 | 2004-05-25 06:32:08 +0000 | [diff] [blame] | 2773 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2774 | switch (LHSI->getOpcode()) { |
| 2775 | case Instruction::And: |
| 2776 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && |
| 2777 | LHSI->getOperand(0)->hasOneUse()) { |
| 2778 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare |
| 2779 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This |
| 2780 | // happens a LOT in code produced by the C front-end, for bitfield |
| 2781 | // access. |
| 2782 | ShiftInst *Shift = dyn_cast<ShiftInst>(LHSI->getOperand(0)); |
| 2783 | ConstantUInt *ShAmt; |
| 2784 | ShAmt = Shift ? dyn_cast<ConstantUInt>(Shift->getOperand(1)) : 0; |
| 2785 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); |
| 2786 | const Type *Ty = LHSI->getType(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2787 | |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2788 | // We can fold this as long as we can't shift unknown bits |
| 2789 | // into the mask. This can only happen with signed shift |
| 2790 | // rights, as they sign-extend. |
| 2791 | if (ShAmt) { |
| 2792 | bool CanFold = Shift->getOpcode() != Instruction::Shr || |
Chris Lattner | 6afc02f | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 2793 | Shift->getType()->isUnsigned(); |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2794 | if (!CanFold) { |
| 2795 | // To test for the bad case of the signed shr, see if any |
| 2796 | // of the bits shifted in could be tested after the mask. |
Chris Lattner | c53cb9d | 2005-06-17 01:29:28 +0000 | [diff] [blame] | 2797 | int ShAmtVal = Ty->getPrimitiveSizeInBits()-ShAmt->getValue(); |
| 2798 | if (ShAmtVal < 0) ShAmtVal = 0; // Out of range shift. |
| 2799 | |
| 2800 | Constant *OShAmt = ConstantUInt::get(Type::UByteTy, ShAmtVal); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2801 | Constant *ShVal = |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2802 | ConstantExpr::getShl(ConstantInt::getAllOnesValue(Ty), OShAmt); |
| 2803 | if (ConstantExpr::getAnd(ShVal, AndCST)->isNullValue()) |
| 2804 | CanFold = true; |
| 2805 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2806 | |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2807 | if (CanFold) { |
Chris Lattner | 6afc02f | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 2808 | Constant *NewCst; |
| 2809 | if (Shift->getOpcode() == Instruction::Shl) |
| 2810 | NewCst = ConstantExpr::getUShr(CI, ShAmt); |
| 2811 | else |
| 2812 | NewCst = ConstantExpr::getShl(CI, ShAmt); |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2813 | |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2814 | // Check to see if we are shifting out any of the bits being |
| 2815 | // compared. |
| 2816 | if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != CI){ |
| 2817 | // If we shifted bits out, the fold is not going to work out. |
| 2818 | // As a special case, check to see if this means that the |
| 2819 | // result is always true or false now. |
| 2820 | if (I.getOpcode() == Instruction::SetEQ) |
| 2821 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2822 | if (I.getOpcode() == Instruction::SetNE) |
| 2823 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2824 | } else { |
| 2825 | I.setOperand(1, NewCst); |
Chris Lattner | 6afc02f | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 2826 | Constant *NewAndCST; |
| 2827 | if (Shift->getOpcode() == Instruction::Shl) |
| 2828 | NewAndCST = ConstantExpr::getUShr(AndCST, ShAmt); |
| 2829 | else |
| 2830 | NewAndCST = ConstantExpr::getShl(AndCST, ShAmt); |
| 2831 | LHSI->setOperand(1, NewAndCST); |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2832 | LHSI->setOperand(0, Shift->getOperand(0)); |
| 2833 | WorkList.push_back(Shift); // Shift is dead. |
| 2834 | AddUsesToWorkList(I); |
| 2835 | return &I; |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 2836 | } |
| 2837 | } |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2838 | } |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2839 | } |
| 2840 | break; |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2841 | |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2842 | case Instruction::Shl: // (setcc (shl X, ShAmt), CI) |
| 2843 | if (ConstantUInt *ShAmt = dyn_cast<ConstantUInt>(LHSI->getOperand(1))) { |
| 2844 | switch (I.getOpcode()) { |
| 2845 | default: break; |
| 2846 | case Instruction::SetEQ: |
| 2847 | case Instruction::SetNE: { |
Chris Lattner | 19b57f5 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 2848 | unsigned TypeBits = CI->getType()->getPrimitiveSizeInBits(); |
| 2849 | |
| 2850 | // Check that the shift amount is in range. If not, don't perform |
| 2851 | // undefined shifts. When the shift is visited it will be |
| 2852 | // simplified. |
| 2853 | if (ShAmt->getValue() >= TypeBits) |
| 2854 | break; |
| 2855 | |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2856 | // If we are comparing against bits always shifted out, the |
| 2857 | // comparison cannot succeed. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2858 | Constant *Comp = |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2859 | ConstantExpr::getShl(ConstantExpr::getShr(CI, ShAmt), ShAmt); |
| 2860 | if (Comp != CI) {// Comparing against a bit that we know is zero. |
| 2861 | bool IsSetNE = I.getOpcode() == Instruction::SetNE; |
| 2862 | Constant *Cst = ConstantBool::get(IsSetNE); |
| 2863 | return ReplaceInstUsesWith(I, Cst); |
| 2864 | } |
| 2865 | |
| 2866 | if (LHSI->hasOneUse()) { |
| 2867 | // Otherwise strength reduce the shift into an and. |
Chris Lattner | fdfe3e49 | 2005-01-08 19:42:22 +0000 | [diff] [blame] | 2868 | unsigned ShAmtVal = (unsigned)ShAmt->getValue(); |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2869 | uint64_t Val = (1ULL << (TypeBits-ShAmtVal))-1; |
| 2870 | |
| 2871 | Constant *Mask; |
| 2872 | if (CI->getType()->isUnsigned()) { |
| 2873 | Mask = ConstantUInt::get(CI->getType(), Val); |
| 2874 | } else if (ShAmtVal != 0) { |
| 2875 | Mask = ConstantSInt::get(CI->getType(), Val); |
| 2876 | } else { |
| 2877 | Mask = ConstantInt::getAllOnesValue(CI->getType()); |
| 2878 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2879 | |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2880 | Instruction *AndI = |
| 2881 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 2882 | Mask, LHSI->getName()+".mask"); |
| 2883 | Value *And = InsertNewInstBefore(AndI, I); |
| 2884 | return new SetCondInst(I.getOpcode(), And, |
| 2885 | ConstantExpr::getUShr(CI, ShAmt)); |
| 2886 | } |
| 2887 | } |
| 2888 | } |
| 2889 | } |
| 2890 | break; |
| 2891 | |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2892 | case Instruction::Shr: // (setcc (shr X, ShAmt), CI) |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2893 | if (ConstantUInt *ShAmt = dyn_cast<ConstantUInt>(LHSI->getOperand(1))) { |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2894 | switch (I.getOpcode()) { |
| 2895 | default: break; |
| 2896 | case Instruction::SetEQ: |
| 2897 | case Instruction::SetNE: { |
Chris Lattner | 19b57f5 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 2898 | |
| 2899 | // Check that the shift amount is in range. If not, don't perform |
| 2900 | // undefined shifts. When the shift is visited it will be |
| 2901 | // simplified. |
Chris Lattner | 104002b | 2005-06-16 01:52:07 +0000 | [diff] [blame] | 2902 | unsigned TypeBits = CI->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 19b57f5 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 2903 | if (ShAmt->getValue() >= TypeBits) |
| 2904 | break; |
| 2905 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2906 | // If we are comparing against bits always shifted out, the |
| 2907 | // comparison cannot succeed. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2908 | Constant *Comp = |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2909 | ConstantExpr::getShr(ConstantExpr::getShl(CI, ShAmt), ShAmt); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2910 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2911 | if (Comp != CI) {// Comparing against a bit that we know is zero. |
| 2912 | bool IsSetNE = I.getOpcode() == Instruction::SetNE; |
| 2913 | Constant *Cst = ConstantBool::get(IsSetNE); |
| 2914 | return ReplaceInstUsesWith(I, Cst); |
| 2915 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2916 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2917 | if (LHSI->hasOneUse() || CI->isNullValue()) { |
Chris Lattner | fdfe3e49 | 2005-01-08 19:42:22 +0000 | [diff] [blame] | 2918 | unsigned ShAmtVal = (unsigned)ShAmt->getValue(); |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2919 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2920 | // Otherwise strength reduce the shift into an and. |
| 2921 | uint64_t Val = ~0ULL; // All ones. |
| 2922 | Val <<= ShAmtVal; // Shift over to the right spot. |
| 2923 | |
| 2924 | Constant *Mask; |
| 2925 | if (CI->getType()->isUnsigned()) { |
Chris Lattner | 2f1457f | 2005-04-24 17:46:05 +0000 | [diff] [blame] | 2926 | Val &= ~0ULL >> (64-TypeBits); |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2927 | Mask = ConstantUInt::get(CI->getType(), Val); |
| 2928 | } else { |
| 2929 | Mask = ConstantSInt::get(CI->getType(), Val); |
| 2930 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2931 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2932 | Instruction *AndI = |
| 2933 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 2934 | Mask, LHSI->getName()+".mask"); |
| 2935 | Value *And = InsertNewInstBefore(AndI, I); |
| 2936 | return new SetCondInst(I.getOpcode(), And, |
| 2937 | ConstantExpr::getShl(CI, ShAmt)); |
| 2938 | } |
| 2939 | break; |
| 2940 | } |
| 2941 | } |
| 2942 | } |
| 2943 | break; |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2944 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2945 | case Instruction::Div: |
| 2946 | // Fold: (div X, C1) op C2 -> range check |
| 2947 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
| 2948 | // Fold this div into the comparison, producing a range check. |
| 2949 | // Determine, based on the divide type, what the range is being |
| 2950 | // checked. If there is an overflow on the low or high side, remember |
| 2951 | // it, otherwise compute the range [low, hi) bounding the new value. |
| 2952 | bool LoOverflow = false, HiOverflow = 0; |
| 2953 | ConstantInt *LoBound = 0, *HiBound = 0; |
| 2954 | |
| 2955 | ConstantInt *Prod; |
| 2956 | bool ProdOV = MulWithOverflow(Prod, CI, DivRHS); |
| 2957 | |
Chris Lattner | a92af96 | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 2958 | Instruction::BinaryOps Opcode = I.getOpcode(); |
| 2959 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2960 | if (DivRHS->isNullValue()) { // Don't hack on divide by zeros. |
| 2961 | } else if (LHSI->getType()->isUnsigned()) { // udiv |
| 2962 | LoBound = Prod; |
| 2963 | LoOverflow = ProdOV; |
| 2964 | HiOverflow = ProdOV || AddWithOverflow(HiBound, LoBound, DivRHS); |
| 2965 | } else if (isPositive(DivRHS)) { // Divisor is > 0. |
| 2966 | if (CI->isNullValue()) { // (X / pos) op 0 |
| 2967 | // Can't overflow. |
| 2968 | LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS))); |
| 2969 | HiBound = DivRHS; |
| 2970 | } else if (isPositive(CI)) { // (X / pos) op pos |
| 2971 | LoBound = Prod; |
| 2972 | LoOverflow = ProdOV; |
| 2973 | HiOverflow = ProdOV || AddWithOverflow(HiBound, Prod, DivRHS); |
| 2974 | } else { // (X / pos) op neg |
| 2975 | Constant *DivRHSH = ConstantExpr::getNeg(SubOne(DivRHS)); |
| 2976 | LoOverflow = AddWithOverflow(LoBound, Prod, |
| 2977 | cast<ConstantInt>(DivRHSH)); |
| 2978 | HiBound = Prod; |
| 2979 | HiOverflow = ProdOV; |
| 2980 | } |
| 2981 | } else { // Divisor is < 0. |
| 2982 | if (CI->isNullValue()) { // (X / neg) op 0 |
| 2983 | LoBound = AddOne(DivRHS); |
| 2984 | HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS)); |
Chris Lattner | 73bcba5 | 2005-06-17 02:05:55 +0000 | [diff] [blame] | 2985 | if (HiBound == DivRHS) |
| 2986 | LoBound = 0; // - INTMIN = INTMIN |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2987 | } else if (isPositive(CI)) { // (X / neg) op pos |
| 2988 | HiOverflow = LoOverflow = ProdOV; |
| 2989 | if (!LoOverflow) |
| 2990 | LoOverflow = AddWithOverflow(LoBound, Prod, AddOne(DivRHS)); |
| 2991 | HiBound = AddOne(Prod); |
| 2992 | } else { // (X / neg) op neg |
| 2993 | LoBound = Prod; |
| 2994 | LoOverflow = HiOverflow = ProdOV; |
| 2995 | HiBound = cast<ConstantInt>(ConstantExpr::getSub(Prod, DivRHS)); |
| 2996 | } |
Chris Lattner | 0b41e86 | 2004-10-08 19:15:44 +0000 | [diff] [blame] | 2997 | |
Chris Lattner | a92af96 | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 2998 | // Dividing by a negate swaps the condition. |
| 2999 | Opcode = SetCondInst::getSwappedCondition(Opcode); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
| 3002 | if (LoBound) { |
| 3003 | Value *X = LHSI->getOperand(0); |
Chris Lattner | a92af96 | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 3004 | switch (Opcode) { |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3005 | default: assert(0 && "Unhandled setcc opcode!"); |
| 3006 | case Instruction::SetEQ: |
| 3007 | if (LoOverflow && HiOverflow) |
| 3008 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 3009 | else if (HiOverflow) |
| 3010 | return new SetCondInst(Instruction::SetGE, X, LoBound); |
| 3011 | else if (LoOverflow) |
| 3012 | return new SetCondInst(Instruction::SetLT, X, HiBound); |
| 3013 | else |
| 3014 | return InsertRangeTest(X, LoBound, HiBound, true, I); |
| 3015 | case Instruction::SetNE: |
| 3016 | if (LoOverflow && HiOverflow) |
| 3017 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 3018 | else if (HiOverflow) |
| 3019 | return new SetCondInst(Instruction::SetLT, X, LoBound); |
| 3020 | else if (LoOverflow) |
| 3021 | return new SetCondInst(Instruction::SetGE, X, HiBound); |
| 3022 | else |
| 3023 | return InsertRangeTest(X, LoBound, HiBound, false, I); |
| 3024 | case Instruction::SetLT: |
| 3025 | if (LoOverflow) |
| 3026 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 3027 | return new SetCondInst(Instruction::SetLT, X, LoBound); |
| 3028 | case Instruction::SetGT: |
| 3029 | if (HiOverflow) |
| 3030 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 3031 | return new SetCondInst(Instruction::SetGE, X, HiBound); |
| 3032 | } |
| 3033 | } |
| 3034 | } |
| 3035 | break; |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 3036 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3037 | |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 3038 | // Simplify seteq and setne instructions... |
| 3039 | if (I.getOpcode() == Instruction::SetEQ || |
| 3040 | I.getOpcode() == Instruction::SetNE) { |
| 3041 | bool isSetNE = I.getOpcode() == Instruction::SetNE; |
| 3042 | |
Chris Lattner | cfbce7c | 2003-07-23 17:26:36 +0000 | [diff] [blame] | 3043 | // 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] | 3044 | // operand is a constant, simplify a bit. |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3045 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) { |
| 3046 | switch (BO->getOpcode()) { |
Chris Lattner | 23b47b6 | 2004-07-06 07:38:18 +0000 | [diff] [blame] | 3047 | case Instruction::Rem: |
| 3048 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. |
| 3049 | if (CI->isNullValue() && isa<ConstantSInt>(BO->getOperand(1)) && |
| 3050 | BO->hasOneUse() && |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 3051 | cast<ConstantSInt>(BO->getOperand(1))->getValue() > 1) { |
| 3052 | int64_t V = cast<ConstantSInt>(BO->getOperand(1))->getValue(); |
| 3053 | if (isPowerOf2_64(V)) { |
| 3054 | unsigned L2 = Log2_64(V); |
Chris Lattner | 23b47b6 | 2004-07-06 07:38:18 +0000 | [diff] [blame] | 3055 | const Type *UTy = BO->getType()->getUnsignedVersion(); |
| 3056 | Value *NewX = InsertNewInstBefore(new CastInst(BO->getOperand(0), |
| 3057 | UTy, "tmp"), I); |
| 3058 | Constant *RHSCst = ConstantUInt::get(UTy, 1ULL << L2); |
| 3059 | Value *NewRem =InsertNewInstBefore(BinaryOperator::createRem(NewX, |
| 3060 | RHSCst, BO->getName()), I); |
| 3061 | return BinaryOperator::create(I.getOpcode(), NewRem, |
| 3062 | Constant::getNullValue(UTy)); |
| 3063 | } |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 3064 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3065 | break; |
Chris Lattner | 23b47b6 | 2004-07-06 07:38:18 +0000 | [diff] [blame] | 3066 | |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3067 | case Instruction::Add: |
Chris Lattner | 6e07936 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 3068 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. |
| 3069 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | b121ae1 | 2004-09-21 21:35:23 +0000 | [diff] [blame] | 3070 | if (BO->hasOneUse()) |
| 3071 | return new SetCondInst(I.getOpcode(), BO->getOperand(0), |
| 3072 | ConstantExpr::getSub(CI, BOp1C)); |
Chris Lattner | 6e07936 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 3073 | } else if (CI->isNullValue()) { |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3074 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 3075 | // efficiently invertible, or if the add has just this one use. |
| 3076 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3077 | |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3078 | if (Value *NegVal = dyn_castNegVal(BOp1)) |
| 3079 | return new SetCondInst(I.getOpcode(), BOp0, NegVal); |
| 3080 | else if (Value *NegVal = dyn_castNegVal(BOp0)) |
| 3081 | return new SetCondInst(I.getOpcode(), NegVal, BOp1); |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 3082 | else if (BO->hasOneUse()) { |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3083 | Instruction *Neg = BinaryOperator::createNeg(BOp1, BO->getName()); |
| 3084 | BO->setName(""); |
| 3085 | InsertNewInstBefore(Neg, I); |
| 3086 | return new SetCondInst(I.getOpcode(), BOp0, Neg); |
| 3087 | } |
| 3088 | } |
| 3089 | break; |
| 3090 | case Instruction::Xor: |
| 3091 | // For the xor case, we can xor two constants together, eliminating |
| 3092 | // the explicit xor. |
| 3093 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) |
| 3094 | return BinaryOperator::create(I.getOpcode(), BO->getOperand(0), |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3095 | ConstantExpr::getXor(CI, BOC)); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3096 | |
| 3097 | // FALLTHROUGH |
| 3098 | case Instruction::Sub: |
| 3099 | // Replace (([sub|xor] A, B) != 0) with (A != B) |
| 3100 | if (CI->isNullValue()) |
| 3101 | return new SetCondInst(I.getOpcode(), BO->getOperand(0), |
| 3102 | BO->getOperand(1)); |
| 3103 | break; |
| 3104 | |
| 3105 | case Instruction::Or: |
| 3106 | // If bits are being or'd in that are not present in the constant we |
| 3107 | // are comparing against, then the comparison could never succeed! |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3108 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 3109 | Constant *NotCI = ConstantExpr::getNot(CI); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3110 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 3111 | return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE)); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3112 | } |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3113 | break; |
| 3114 | |
| 3115 | case Instruction::And: |
| 3116 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 3117 | // If bits are being compared against that are and'd out, then the |
| 3118 | // comparison can never succeed! |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 3119 | if (!ConstantExpr::getAnd(CI, |
| 3120 | ConstantExpr::getNot(BOC))->isNullValue()) |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 3121 | return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE)); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3122 | |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3123 | // If we have ((X & C) == C), turn it into ((X & C) != 0). |
Chris Lattner | ee59d4b | 2004-06-10 02:33:20 +0000 | [diff] [blame] | 3124 | if (CI == BOC && isOneBitSet(CI)) |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3125 | return new SetCondInst(isSetNE ? Instruction::SetEQ : |
| 3126 | Instruction::SetNE, Op0, |
| 3127 | Constant::getNullValue(CI->getType())); |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3128 | |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3129 | // Replace (and X, (1 << size(X)-1) != 0) with x < 0, converting X |
| 3130 | // to be a signed value as appropriate. |
| 3131 | if (isSignBit(BOC)) { |
| 3132 | Value *X = BO->getOperand(0); |
| 3133 | // If 'X' is not signed, insert a cast now... |
| 3134 | if (!BOC->getType()->isSigned()) { |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 3135 | const Type *DestTy = BOC->getType()->getSignedVersion(); |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 3136 | X = InsertCastBefore(X, DestTy, I); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3137 | } |
| 3138 | return new SetCondInst(isSetNE ? Instruction::SetLT : |
| 3139 | Instruction::SetGE, X, |
| 3140 | Constant::getNullValue(X->getType())); |
| 3141 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3142 | |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 3143 | // ((X & ~7) == 0) --> X < 8 |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3144 | if (CI->isNullValue() && isHighOnes(BOC)) { |
| 3145 | Value *X = BO->getOperand(0); |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 3146 | Constant *NegX = ConstantExpr::getNeg(BOC); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3147 | |
| 3148 | // If 'X' is signed, insert a cast now. |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 3149 | if (NegX->getType()->isSigned()) { |
| 3150 | const Type *DestTy = NegX->getType()->getUnsignedVersion(); |
| 3151 | X = InsertCastBefore(X, DestTy, I); |
| 3152 | NegX = ConstantExpr::getCast(NegX, DestTy); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3153 | } |
| 3154 | |
| 3155 | return new SetCondInst(isSetNE ? Instruction::SetGE : |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 3156 | Instruction::SetLT, X, NegX); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3157 | } |
| 3158 | |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 3159 | } |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3160 | default: break; |
| 3161 | } |
| 3162 | } |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3163 | } else { // Not a SetEQ/SetNE |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3164 | // If the LHS is a cast from an integral value of the same size, |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3165 | if (CastInst *Cast = dyn_cast<CastInst>(Op0)) { |
| 3166 | Value *CastOp = Cast->getOperand(0); |
| 3167 | const Type *SrcTy = CastOp->getType(); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3168 | unsigned SrcTySize = SrcTy->getPrimitiveSizeInBits(); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3169 | if (SrcTy != Cast->getType() && SrcTy->isInteger() && |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3170 | SrcTySize == Cast->getType()->getPrimitiveSizeInBits()) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3171 | assert((SrcTy->isSigned() ^ Cast->getType()->isSigned()) && |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3172 | "Source and destination signednesses should differ!"); |
| 3173 | if (Cast->getType()->isSigned()) { |
| 3174 | // If this is a signed comparison, check for comparisons in the |
| 3175 | // vicinity of zero. |
| 3176 | if (I.getOpcode() == Instruction::SetLT && CI->isNullValue()) |
| 3177 | // X < 0 => x > 127 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3178 | return BinaryOperator::createSetGT(CastOp, |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3179 | ConstantUInt::get(SrcTy, (1ULL << (SrcTySize-1))-1)); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3180 | else if (I.getOpcode() == Instruction::SetGT && |
| 3181 | cast<ConstantSInt>(CI)->getValue() == -1) |
| 3182 | // X > -1 => x < 128 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3183 | return BinaryOperator::createSetLT(CastOp, |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3184 | ConstantUInt::get(SrcTy, 1ULL << (SrcTySize-1))); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3185 | } else { |
| 3186 | ConstantUInt *CUI = cast<ConstantUInt>(CI); |
| 3187 | if (I.getOpcode() == Instruction::SetLT && |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3188 | CUI->getValue() == 1ULL << (SrcTySize-1)) |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3189 | // X < 128 => X > -1 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3190 | return BinaryOperator::createSetGT(CastOp, |
| 3191 | ConstantSInt::get(SrcTy, -1)); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3192 | else if (I.getOpcode() == Instruction::SetGT && |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3193 | CUI->getValue() == (1ULL << (SrcTySize-1))-1) |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3194 | // X > 127 => X < 0 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3195 | return BinaryOperator::createSetLT(CastOp, |
| 3196 | Constant::getNullValue(SrcTy)); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3197 | } |
| 3198 | } |
| 3199 | } |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 3200 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3201 | } |
| 3202 | |
Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 3203 | // Handle setcc with constant RHS's that can be integer, FP or pointer. |
| 3204 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 3205 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 3206 | switch (LHSI->getOpcode()) { |
Chris Lattner | a816eee | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 3207 | case Instruction::GetElementPtr: |
| 3208 | if (RHSC->isNullValue()) { |
| 3209 | // Transform setcc GEP P, int 0, int 0, int 0, null -> setcc P, null |
| 3210 | bool isAllZeros = true; |
| 3211 | for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i) |
| 3212 | if (!isa<Constant>(LHSI->getOperand(i)) || |
| 3213 | !cast<Constant>(LHSI->getOperand(i))->isNullValue()) { |
| 3214 | isAllZeros = false; |
| 3215 | break; |
| 3216 | } |
| 3217 | if (isAllZeros) |
| 3218 | return new SetCondInst(I.getOpcode(), LHSI->getOperand(0), |
| 3219 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
| 3220 | } |
| 3221 | break; |
| 3222 | |
Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 3223 | case Instruction::PHI: |
| 3224 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3225 | return NV; |
| 3226 | break; |
| 3227 | case Instruction::Select: |
| 3228 | // If either operand of the select is a constant, we can fold the |
| 3229 | // comparison into the select arms, which will cause one to be |
| 3230 | // constant folded and the select turned into a bitwise or. |
| 3231 | Value *Op1 = 0, *Op2 = 0; |
| 3232 | if (LHSI->hasOneUse()) { |
| 3233 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 3234 | // Fold the known value into the constant operand. |
| 3235 | Op1 = ConstantExpr::get(I.getOpcode(), C, RHSC); |
| 3236 | // Insert a new SetCC of the other select operand. |
| 3237 | Op2 = InsertNewInstBefore(new SetCondInst(I.getOpcode(), |
| 3238 | LHSI->getOperand(2), RHSC, |
| 3239 | I.getName()), I); |
| 3240 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 3241 | // Fold the known value into the constant operand. |
| 3242 | Op2 = ConstantExpr::get(I.getOpcode(), C, RHSC); |
| 3243 | // Insert a new SetCC of the other select operand. |
| 3244 | Op1 = InsertNewInstBefore(new SetCondInst(I.getOpcode(), |
| 3245 | LHSI->getOperand(1), RHSC, |
| 3246 | I.getName()), I); |
| 3247 | } |
| 3248 | } |
Jeff Cohen | 8263985 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 3249 | |
Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 3250 | if (Op1) |
| 3251 | return new SelectInst(LHSI->getOperand(0), Op1, Op2); |
| 3252 | break; |
| 3253 | } |
| 3254 | } |
| 3255 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3256 | // If we can optimize a 'setcc GEP, P' or 'setcc P, GEP', do so now. |
| 3257 | if (User *GEP = dyn_castGetElementPtr(Op0)) |
| 3258 | if (Instruction *NI = FoldGEPSetCC(GEP, Op1, I.getOpcode(), I)) |
| 3259 | return NI; |
| 3260 | if (User *GEP = dyn_castGetElementPtr(Op1)) |
| 3261 | if (Instruction *NI = FoldGEPSetCC(GEP, Op0, |
| 3262 | SetCondInst::getSwappedCondition(I.getOpcode()), I)) |
| 3263 | return NI; |
| 3264 | |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 3265 | // Test to see if the operands of the setcc are casted versions of other |
| 3266 | // 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] | 3267 | if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
| 3268 | Value *CastOp0 = CI->getOperand(0); |
| 3269 | if (CastOp0->getType()->isLosslesslyConvertibleTo(CI->getType()) && |
Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 3270 | (isa<Constant>(Op1) || isa<CastInst>(Op1)) && |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 3271 | (I.getOpcode() == Instruction::SetEQ || |
| 3272 | I.getOpcode() == Instruction::SetNE)) { |
| 3273 | // We keep moving the cast from the left operand over to the right |
| 3274 | // operand, where it can often be eliminated completely. |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 3275 | Op0 = CastOp0; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3276 | |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 3277 | // If operand #1 is a cast instruction, see if we can eliminate it as |
| 3278 | // well. |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 3279 | if (CastInst *CI2 = dyn_cast<CastInst>(Op1)) |
| 3280 | if (CI2->getOperand(0)->getType()->isLosslesslyConvertibleTo( |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 3281 | Op0->getType())) |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 3282 | Op1 = CI2->getOperand(0); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3283 | |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 3284 | // If Op1 is a constant, we can fold the cast into the constant. |
| 3285 | if (Op1->getType() != Op0->getType()) |
| 3286 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
| 3287 | Op1 = ConstantExpr::getCast(Op1C, Op0->getType()); |
| 3288 | } else { |
| 3289 | // Otherwise, cast the RHS right before the setcc |
| 3290 | Op1 = new CastInst(Op1, Op0->getType(), Op1->getName()); |
| 3291 | InsertNewInstBefore(cast<Instruction>(Op1), I); |
| 3292 | } |
| 3293 | return BinaryOperator::create(I.getOpcode(), Op0, Op1); |
| 3294 | } |
| 3295 | |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 3296 | // Handle the special case of: setcc (cast bool to X), <cst> |
| 3297 | // This comes up when you have code like |
| 3298 | // int X = A < B; |
| 3299 | // if (X) ... |
| 3300 | // For generality, we handle any zero-extension of any operand comparison |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3301 | // with a constant or another cast from the same type. |
| 3302 | if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1)) |
| 3303 | if (Instruction *R = visitSetCondInstWithCastAndCast(I)) |
| 3304 | return R; |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 3305 | } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3306 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3309 | // visitSetCondInstWithCastAndCast - Handle setcond (cast x to y), (cast/cst). |
| 3310 | // We only handle extending casts so far. |
| 3311 | // |
| 3312 | Instruction *InstCombiner::visitSetCondInstWithCastAndCast(SetCondInst &SCI) { |
| 3313 | Value *LHSCIOp = cast<CastInst>(SCI.getOperand(0))->getOperand(0); |
| 3314 | const Type *SrcTy = LHSCIOp->getType(); |
| 3315 | const Type *DestTy = SCI.getOperand(0)->getType(); |
| 3316 | Value *RHSCIOp; |
| 3317 | |
| 3318 | if (!DestTy->isIntegral() || !SrcTy->isIntegral()) |
Chris Lattner | 03f06f1 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 3319 | return 0; |
| 3320 | |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3321 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); |
| 3322 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); |
| 3323 | if (SrcBits >= DestBits) return 0; // Only handle extending cast. |
| 3324 | |
| 3325 | // Is this a sign or zero extension? |
| 3326 | bool isSignSrc = SrcTy->isSigned(); |
| 3327 | bool isSignDest = DestTy->isSigned(); |
| 3328 | |
| 3329 | if (CastInst *CI = dyn_cast<CastInst>(SCI.getOperand(1))) { |
| 3330 | // Not an extension from the same type? |
| 3331 | RHSCIOp = CI->getOperand(0); |
| 3332 | if (RHSCIOp->getType() != LHSCIOp->getType()) return 0; |
| 3333 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(SCI.getOperand(1))) { |
| 3334 | // Compute the constant that would happen if we truncated to SrcTy then |
| 3335 | // reextended to DestTy. |
| 3336 | Constant *Res = ConstantExpr::getCast(CI, SrcTy); |
| 3337 | |
| 3338 | if (ConstantExpr::getCast(Res, DestTy) == CI) { |
| 3339 | RHSCIOp = Res; |
| 3340 | } else { |
| 3341 | // If the value cannot be represented in the shorter type, we cannot emit |
| 3342 | // a simple comparison. |
| 3343 | if (SCI.getOpcode() == Instruction::SetEQ) |
| 3344 | return ReplaceInstUsesWith(SCI, ConstantBool::False); |
| 3345 | if (SCI.getOpcode() == Instruction::SetNE) |
| 3346 | return ReplaceInstUsesWith(SCI, ConstantBool::True); |
| 3347 | |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3348 | // Evaluate the comparison for LT. |
| 3349 | Value *Result; |
| 3350 | if (DestTy->isSigned()) { |
| 3351 | // We're performing a signed comparison. |
| 3352 | if (isSignSrc) { |
| 3353 | // Signed extend and signed comparison. |
| 3354 | if (cast<ConstantSInt>(CI)->getValue() < 0) // X < (small) --> false |
| 3355 | Result = ConstantBool::False; |
| 3356 | else |
| 3357 | Result = ConstantBool::True; // X < (large) --> true |
| 3358 | } else { |
| 3359 | // Unsigned extend and signed comparison. |
| 3360 | if (cast<ConstantSInt>(CI)->getValue() < 0) |
| 3361 | Result = ConstantBool::False; |
| 3362 | else |
| 3363 | Result = ConstantBool::True; |
| 3364 | } |
| 3365 | } else { |
| 3366 | // We're performing an unsigned comparison. |
| 3367 | if (!isSignSrc) { |
| 3368 | // Unsigned extend & compare -> always true. |
| 3369 | Result = ConstantBool::True; |
| 3370 | } else { |
| 3371 | // We're performing an unsigned comp with a sign extended value. |
| 3372 | // This is true if the input is >= 0. [aka >s -1] |
| 3373 | Constant *NegOne = ConstantIntegral::getAllOnesValue(SrcTy); |
| 3374 | Result = InsertNewInstBefore(BinaryOperator::createSetGT(LHSCIOp, |
| 3375 | NegOne, SCI.getName()), SCI); |
| 3376 | } |
Reid Spencer | 279fa25 | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 3377 | } |
Chris Lattner | 03f06f1 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 3378 | |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3379 | // Finally, return the value computed. |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3380 | if (SCI.getOpcode() == Instruction::SetLT) { |
| 3381 | return ReplaceInstUsesWith(SCI, Result); |
| 3382 | } else { |
| 3383 | assert(SCI.getOpcode()==Instruction::SetGT &&"SetCC should be folded!"); |
| 3384 | if (Constant *CI = dyn_cast<Constant>(Result)) |
| 3385 | return ReplaceInstUsesWith(SCI, ConstantExpr::getNot(CI)); |
| 3386 | else |
| 3387 | return BinaryOperator::createNot(Result); |
| 3388 | } |
Chris Lattner | 03f06f1 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 3389 | } |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3390 | } else { |
| 3391 | return 0; |
Reid Spencer | 279fa25 | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 3392 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3393 | |
Chris Lattner | 252a845 | 2005-06-16 03:00:08 +0000 | [diff] [blame] | 3394 | // Okay, just insert a compare of the reduced operands now! |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3395 | return BinaryOperator::create(SCI.getOpcode(), LHSCIOp, RHSCIOp); |
| 3396 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3397 | |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 3398 | Instruction *InstCombiner::visitShiftInst(ShiftInst &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3399 | assert(I.getOperand(1)->getType() == Type::UByteTy); |
| 3400 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3401 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3402 | |
| 3403 | // shl X, 0 == X and shr X, 0 == X |
| 3404 | // shl 0, X == 0 and shr 0, X == 0 |
| 3405 | if (Op1 == Constant::getNullValue(Type::UByteTy) || |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3406 | Op0 == Constant::getNullValue(Op0->getType())) |
| 3407 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3408 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3409 | if (isa<UndefValue>(Op0)) { // undef >>s X -> undef |
| 3410 | if (!isLeftShift && I.getType()->isSigned()) |
Chris Lattner | 67f0545 | 2004-10-16 23:28:04 +0000 | [diff] [blame] | 3411 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3412 | else // undef << X -> 0 AND undef >>u X -> 0 |
| 3413 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3414 | } |
| 3415 | if (isa<UndefValue>(Op1)) { |
Chris Lattner | 18aa4d8 | 2005-07-20 18:49:28 +0000 | [diff] [blame] | 3416 | if (isLeftShift || I.getType()->isUnsigned())// X << undef, X >>u undef -> 0 |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3417 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3418 | else |
| 3419 | return ReplaceInstUsesWith(I, Op0); // X >>s undef -> X |
| 3420 | } |
| 3421 | |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3422 | // shr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 3423 | if (!isLeftShift) |
| 3424 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0)) |
| 3425 | if (CSI->isAllOnesValue()) |
| 3426 | return ReplaceInstUsesWith(I, CSI); |
| 3427 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3428 | // Try to fold constant and into select arguments. |
| 3429 | if (isa<Constant>(Op0)) |
| 3430 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3431 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3432 | return R; |
| 3433 | |
Chris Lattner | b18dbbf | 2005-05-08 17:34:56 +0000 | [diff] [blame] | 3434 | // See if we can turn a signed shr into an unsigned shr. |
| 3435 | if (!isLeftShift && I.getType()->isSigned()) { |
| 3436 | if (MaskedValueIsZero(Op0, ConstantInt::getMinValue(I.getType()))) { |
| 3437 | Value *V = InsertCastBefore(Op0, I.getType()->getUnsignedVersion(), I); |
| 3438 | V = InsertNewInstBefore(new ShiftInst(Instruction::Shr, V, Op1, |
| 3439 | I.getName()), I); |
| 3440 | return new CastInst(V, I.getType()); |
| 3441 | } |
| 3442 | } |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3443 | |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3444 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) |
| 3445 | if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I)) |
| 3446 | return Res; |
| 3447 | return 0; |
| 3448 | } |
| 3449 | |
| 3450 | Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantUInt *Op1, |
| 3451 | ShiftInst &I) { |
| 3452 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | b330939 | 2006-01-06 07:22:22 +0000 | [diff] [blame] | 3453 | bool isSignedShift = Op0->getType()->isSigned(); |
| 3454 | bool isUnsignedShift = !isSignedShift; |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3455 | |
| 3456 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr |
| 3457 | // of a signed value. |
| 3458 | // |
| 3459 | unsigned TypeBits = Op0->getType()->getPrimitiveSizeInBits(); |
| 3460 | if (Op1->getValue() >= TypeBits) { |
Chris Lattner | b330939 | 2006-01-06 07:22:22 +0000 | [diff] [blame] | 3461 | if (isUnsignedShift || isLeftShift) |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3462 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
| 3463 | else { |
| 3464 | I.setOperand(1, ConstantUInt::get(Type::UByteTy, TypeBits-1)); |
| 3465 | return &I; |
Chris Lattner | f5ce254 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 3466 | } |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3467 | } |
| 3468 | |
| 3469 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 3470 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 3471 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 3472 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
| 3473 | return BinaryOperator::createMul(BO->getOperand(0), |
| 3474 | ConstantExpr::getShl(BOOp, Op1)); |
| 3475 | |
| 3476 | // Try to fold constant and into select arguments. |
| 3477 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 3478 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 3479 | return R; |
| 3480 | if (isa<PHINode>(Op0)) |
| 3481 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3482 | return NV; |
| 3483 | |
| 3484 | if (Op0->hasOneUse()) { |
| 3485 | // If this is a SHL of a sign-extending cast, see if we can turn the input |
| 3486 | // into a zero extending cast (a simple strength reduction). |
| 3487 | if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
| 3488 | const Type *SrcTy = CI->getOperand(0)->getType(); |
| 3489 | if (isLeftShift && SrcTy->isInteger() && SrcTy->isSigned() && |
| 3490 | SrcTy->getPrimitiveSizeInBits() < |
| 3491 | CI->getType()->getPrimitiveSizeInBits()) { |
| 3492 | // We can change it to a zero extension if we are shifting out all of |
| 3493 | // the sign extended bits. To check this, form a mask of all of the |
| 3494 | // sign extend bits, then shift them left and see if we have anything |
| 3495 | // left. |
| 3496 | Constant *Mask = ConstantIntegral::getAllOnesValue(SrcTy); // 1111 |
| 3497 | Mask = ConstantExpr::getZeroExtend(Mask, CI->getType()); // 00001111 |
| 3498 | Mask = ConstantExpr::getNot(Mask); // 1's in the sign bits: 11110000 |
| 3499 | if (ConstantExpr::getShl(Mask, Op1)->isNullValue()) { |
| 3500 | // If the shift is nuking all of the sign bits, change this to a |
| 3501 | // zero extension cast. To do this, cast the cast input to |
| 3502 | // unsigned, then to the requested size. |
| 3503 | Value *CastOp = CI->getOperand(0); |
| 3504 | Instruction *NC = |
| 3505 | new CastInst(CastOp, CastOp->getType()->getUnsignedVersion(), |
| 3506 | CI->getName()+".uns"); |
| 3507 | NC = InsertNewInstBefore(NC, I); |
| 3508 | // Finally, insert a replacement for CI. |
| 3509 | NC = new CastInst(NC, CI->getType(), CI->getName()); |
| 3510 | CI->setName(""); |
| 3511 | NC = InsertNewInstBefore(NC, I); |
| 3512 | WorkList.push_back(CI); // Delete CI later. |
| 3513 | I.setOperand(0, NC); |
| 3514 | return &I; // The SHL operand was modified. |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3515 | } |
| 3516 | } |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3517 | } |
| 3518 | |
| 3519 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) { |
| 3520 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
| 3521 | Value *V1, *V2; |
| 3522 | ConstantInt *CC; |
| 3523 | switch (Op0BO->getOpcode()) { |
Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 3524 | default: break; |
| 3525 | case Instruction::Add: |
| 3526 | case Instruction::And: |
| 3527 | case Instruction::Or: |
| 3528 | case Instruction::Xor: |
| 3529 | // These operators commute. |
| 3530 | // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 3531 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
| 3532 | match(Op0BO->getOperand(1), |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3533 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 3534 | Instruction *YS = new ShiftInst(Instruction::Shl, |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3535 | Op0BO->getOperand(0), Op1, |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 3536 | Op0BO->getName()); |
| 3537 | InsertNewInstBefore(YS, I); // (Y << C) |
| 3538 | Instruction *X = BinaryOperator::create(Op0BO->getOpcode(), YS, |
| 3539 | V1, |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3540 | Op0BO->getOperand(1)->getName()); |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 3541 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
| 3542 | Constant *C2 = ConstantInt::getAllOnesValue(X->getType()); |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3543 | C2 = ConstantExpr::getShl(C2, Op1); |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 3544 | return BinaryOperator::createAnd(X, C2); |
| 3545 | } |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3546 | |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 3547 | // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C)) |
| 3548 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
| 3549 | match(Op0BO->getOperand(1), |
| 3550 | m_And(m_Shr(m_Value(V1), m_Value(V2)), |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3551 | m_ConstantInt(CC))) && V2 == Op1 && |
| 3552 | cast<BinaryOperator>(Op0BO->getOperand(1))->getOperand(0)->hasOneUse()) { |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 3553 | Instruction *YS = new ShiftInst(Instruction::Shl, |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3554 | Op0BO->getOperand(0), Op1, |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 3555 | Op0BO->getName()); |
| 3556 | InsertNewInstBefore(YS, I); // (Y << C) |
| 3557 | Instruction *XM = |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3558 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 3559 | V1->getName()+".mask"); |
| 3560 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 3561 | |
| 3562 | return BinaryOperator::create(Op0BO->getOpcode(), YS, XM); |
| 3563 | } |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3564 | |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 3565 | // FALL THROUGH. |
Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 3566 | case Instruction::Sub: |
| 3567 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 3568 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 3569 | match(Op0BO->getOperand(0), |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3570 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 3571 | Instruction *YS = new ShiftInst(Instruction::Shl, |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3572 | Op0BO->getOperand(1), Op1, |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 3573 | Op0BO->getName()); |
| 3574 | InsertNewInstBefore(YS, I); // (Y << C) |
| 3575 | Instruction *X = BinaryOperator::create(Op0BO->getOpcode(), YS, |
| 3576 | V1, |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3577 | Op0BO->getOperand(0)->getName()); |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 3578 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
| 3579 | Constant *C2 = ConstantInt::getAllOnesValue(X->getType()); |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3580 | C2 = ConstantExpr::getShl(C2, Op1); |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 3581 | return BinaryOperator::createAnd(X, C2); |
| 3582 | } |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3583 | |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 3584 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 3585 | match(Op0BO->getOperand(0), |
| 3586 | m_And(m_Shr(m_Value(V1), m_Value(V2)), |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3587 | m_ConstantInt(CC))) && V2 == Op1 && |
| 3588 | cast<BinaryOperator>(Op0BO->getOperand(0))->getOperand(0)->hasOneUse()) { |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 3589 | Instruction *YS = new ShiftInst(Instruction::Shl, |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3590 | Op0BO->getOperand(1), Op1, |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 3591 | Op0BO->getName()); |
| 3592 | InsertNewInstBefore(YS, I); // (Y << C) |
| 3593 | Instruction *XM = |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3594 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 3595 | V1->getName()+".mask"); |
| 3596 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 3597 | |
| 3598 | return BinaryOperator::create(Op0BO->getOpcode(), YS, XM); |
| 3599 | } |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3600 | |
Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 3601 | break; |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3602 | } |
| 3603 | |
| 3604 | |
| 3605 | // If the operand is an bitwise operator with a constant RHS, and the |
| 3606 | // shift is the only use, we can pull it out of the shift. |
| 3607 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 3608 | bool isValid = true; // Valid only for And, Or, Xor |
| 3609 | bool highBitSet = false; // Transform if high bit of constant set? |
| 3610 | |
| 3611 | switch (Op0BO->getOpcode()) { |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3612 | default: isValid = false; break; // Do not perform transform! |
Chris Lattner | 44bd392 | 2004-10-08 03:46:20 +0000 | [diff] [blame] | 3613 | case Instruction::Add: |
| 3614 | isValid = isLeftShift; |
| 3615 | break; |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3616 | case Instruction::Or: |
| 3617 | case Instruction::Xor: |
| 3618 | highBitSet = false; |
| 3619 | break; |
| 3620 | case Instruction::And: |
| 3621 | highBitSet = true; |
| 3622 | break; |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3623 | } |
| 3624 | |
| 3625 | // If this is a signed shift right, and the high bit is modified |
| 3626 | // by the logical operation, do not perform the transformation. |
| 3627 | // The highBitSet boolean indicates the value of the high bit of |
| 3628 | // the constant which would cause it to be modified for this |
| 3629 | // operation. |
| 3630 | // |
Chris Lattner | b330939 | 2006-01-06 07:22:22 +0000 | [diff] [blame] | 3631 | if (isValid && !isLeftShift && isSignedShift) { |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3632 | uint64_t Val = Op0C->getRawValue(); |
| 3633 | isValid = ((Val & (1 << (TypeBits-1))) != 0) == highBitSet; |
| 3634 | } |
| 3635 | |
| 3636 | if (isValid) { |
| 3637 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1); |
| 3638 | |
| 3639 | Instruction *NewShift = |
| 3640 | new ShiftInst(I.getOpcode(), Op0BO->getOperand(0), Op1, |
| 3641 | Op0BO->getName()); |
| 3642 | Op0BO->setName(""); |
| 3643 | InsertNewInstBefore(NewShift, I); |
| 3644 | |
| 3645 | return BinaryOperator::create(Op0BO->getOpcode(), NewShift, |
| 3646 | NewRHS); |
| 3647 | } |
| 3648 | } |
| 3649 | } |
| 3650 | } |
| 3651 | |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 3652 | // Find out if this is a shift of a shift by a constant. |
| 3653 | ShiftInst *ShiftOp = 0; |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3654 | if (ShiftInst *Op0SI = dyn_cast<ShiftInst>(Op0)) |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 3655 | ShiftOp = Op0SI; |
| 3656 | else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
| 3657 | // If this is a noop-integer case of a shift instruction, use the shift. |
| 3658 | if (CI->getOperand(0)->getType()->isInteger() && |
| 3659 | CI->getOperand(0)->getType()->getPrimitiveSizeInBits() == |
| 3660 | CI->getType()->getPrimitiveSizeInBits() && |
| 3661 | isa<ShiftInst>(CI->getOperand(0))) { |
| 3662 | ShiftOp = cast<ShiftInst>(CI->getOperand(0)); |
| 3663 | } |
| 3664 | } |
| 3665 | |
| 3666 | if (ShiftOp && isa<ConstantUInt>(ShiftOp->getOperand(1))) { |
| 3667 | // Find the operands and properties of the input shift. Note that the |
| 3668 | // signedness of the input shift may differ from the current shift if there |
| 3669 | // is a noop cast between the two. |
| 3670 | bool isShiftOfLeftShift = ShiftOp->getOpcode() == Instruction::Shl; |
| 3671 | bool isShiftOfSignedShift = ShiftOp->getType()->isSigned(); |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame^] | 3672 | bool isShiftOfUnsignedShift = !isShiftOfSignedShift; |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 3673 | |
| 3674 | ConstantUInt *ShiftAmt1C = cast<ConstantUInt>(ShiftOp->getOperand(1)); |
| 3675 | |
| 3676 | unsigned ShiftAmt1 = (unsigned)ShiftAmt1C->getValue(); |
| 3677 | unsigned ShiftAmt2 = (unsigned)Op1->getValue(); |
| 3678 | |
| 3679 | // Check for (A << c1) << c2 and (A >> c1) >> c2. |
| 3680 | if (isLeftShift == isShiftOfLeftShift) { |
| 3681 | // Do not fold these shifts if the first one is signed and the second one |
| 3682 | // is unsigned and this is a right shift. Further, don't do any folding |
| 3683 | // on them. |
| 3684 | if (isShiftOfSignedShift && isUnsignedShift && !isLeftShift) |
| 3685 | return 0; |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3686 | |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 3687 | unsigned Amt = ShiftAmt1+ShiftAmt2; // Fold into one big shift. |
| 3688 | if (Amt > Op0->getType()->getPrimitiveSizeInBits()) |
| 3689 | Amt = Op0->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 3690 | |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 3691 | Value *Op = ShiftOp->getOperand(0); |
| 3692 | if (isShiftOfSignedShift != isSignedShift) |
| 3693 | Op = InsertNewInstBefore(new CastInst(Op, I.getType(), "tmp"), I); |
| 3694 | return new ShiftInst(I.getOpcode(), Op, |
| 3695 | ConstantUInt::get(Type::UByteTy, Amt)); |
| 3696 | } |
| 3697 | |
| 3698 | // Check for (A << c1) >> c2 or (A >> c1) << c2. If we are dealing with |
| 3699 | // signed types, we can only support the (A >> c1) << c2 configuration, |
| 3700 | // because it can not turn an arbitrary bit of A into a sign bit. |
| 3701 | if (isUnsignedShift || isLeftShift) { |
| 3702 | // Calculate bitmask for what gets shifted off the edge. |
| 3703 | Constant *C = ConstantIntegral::getAllOnesValue(I.getType()); |
| 3704 | if (isLeftShift) |
| 3705 | C = ConstantExpr::getShl(C, ShiftAmt1C); |
| 3706 | else |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame^] | 3707 | C = ConstantExpr::getUShr(C, ShiftAmt1C); |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 3708 | |
| 3709 | Value *Op = ShiftOp->getOperand(0); |
| 3710 | if (isShiftOfSignedShift != isSignedShift) |
| 3711 | Op = InsertNewInstBefore(new CastInst(Op, I.getType(),Op->getName()),I); |
| 3712 | |
| 3713 | Instruction *Mask = |
| 3714 | BinaryOperator::createAnd(Op, C, Op->getName()+".mask"); |
| 3715 | InsertNewInstBefore(Mask, I); |
| 3716 | |
| 3717 | // Figure out what flavor of shift we should use... |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame^] | 3718 | if (ShiftAmt1 == ShiftAmt2) { |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 3719 | return ReplaceInstUsesWith(I, Mask); // (A << c) >> c === A & c2 |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame^] | 3720 | } else if (ShiftAmt1 < ShiftAmt2) { |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 3721 | return new ShiftInst(I.getOpcode(), Mask, |
| 3722 | ConstantUInt::get(Type::UByteTy, ShiftAmt2-ShiftAmt1)); |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame^] | 3723 | } else if (isShiftOfUnsignedShift || isShiftOfLeftShift) { |
| 3724 | if (isShiftOfUnsignedShift && !isShiftOfLeftShift && isSignedShift) { |
| 3725 | // Make sure to emit an unsigned shift right, not a signed one. |
| 3726 | Mask = InsertNewInstBefore(new CastInst(Mask, |
| 3727 | Mask->getType()->getUnsignedVersion(), |
| 3728 | Op->getName()), I); |
| 3729 | Mask = new ShiftInst(Instruction::Shr, Mask, |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 3730 | ConstantUInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2)); |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame^] | 3731 | InsertNewInstBefore(Mask, I); |
| 3732 | return new CastInst(Mask, I.getType()); |
| 3733 | } else { |
| 3734 | return new ShiftInst(ShiftOp->getOpcode(), Mask, |
| 3735 | ConstantUInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2)); |
| 3736 | } |
| 3737 | } else { |
| 3738 | // (X >>s C1) << C2 where C1 > C2 === (X >>s (C1-C2)) & mask |
| 3739 | Op = InsertNewInstBefore(new CastInst(Mask, |
| 3740 | I.getType()->getSignedVersion(), |
| 3741 | Mask->getName()), I); |
| 3742 | Instruction *Shift = |
| 3743 | new ShiftInst(ShiftOp->getOpcode(), Op, |
| 3744 | ConstantUInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2)); |
| 3745 | InsertNewInstBefore(Shift, I); |
| 3746 | |
| 3747 | C = ConstantIntegral::getAllOnesValue(Shift->getType()); |
| 3748 | C = ConstantExpr::getShl(C, Op1); |
| 3749 | Mask = BinaryOperator::createAnd(Shift, C, Op->getName()+".mask"); |
| 3750 | InsertNewInstBefore(Mask, I); |
| 3751 | return new CastInst(Mask, I.getType()); |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 3752 | } |
| 3753 | } else { |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame^] | 3754 | // We can handle signed (X << C1) >>s C2 if it's a sign extend. In |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 3755 | // this case, C1 == C2 and C1 is 8, 16, or 32. |
| 3756 | if (ShiftAmt1 == ShiftAmt2) { |
| 3757 | const Type *SExtType = 0; |
| 3758 | switch (ShiftAmt1) { |
| 3759 | case 8 : SExtType = Type::SByteTy; break; |
| 3760 | case 16: SExtType = Type::ShortTy; break; |
| 3761 | case 32: SExtType = Type::IntTy; break; |
| 3762 | } |
| 3763 | |
| 3764 | if (SExtType) { |
| 3765 | Instruction *NewTrunc = new CastInst(ShiftOp->getOperand(0), |
| 3766 | SExtType, "sext"); |
| 3767 | InsertNewInstBefore(NewTrunc, I); |
| 3768 | return new CastInst(NewTrunc, I.getType()); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3769 | } |
Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 3770 | } |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3771 | } |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 3772 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3773 | return 0; |
| 3774 | } |
| 3775 | |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 3776 | enum CastType { |
| 3777 | Noop = 0, |
| 3778 | Truncate = 1, |
| 3779 | Signext = 2, |
| 3780 | Zeroext = 3 |
| 3781 | }; |
| 3782 | |
| 3783 | /// getCastType - In the future, we will split the cast instruction into these |
| 3784 | /// various types. Until then, we have to do the analysis here. |
| 3785 | static CastType getCastType(const Type *Src, const Type *Dest) { |
| 3786 | assert(Src->isIntegral() && Dest->isIntegral() && |
| 3787 | "Only works on integral types!"); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3788 | unsigned SrcSize = Src->getPrimitiveSizeInBits(); |
| 3789 | unsigned DestSize = Dest->getPrimitiveSizeInBits(); |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 3790 | |
| 3791 | if (SrcSize == DestSize) return Noop; |
| 3792 | if (SrcSize > DestSize) return Truncate; |
| 3793 | if (Src->isSigned()) return Signext; |
| 3794 | return Zeroext; |
| 3795 | } |
| 3796 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3797 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3798 | // isEliminableCastOfCast - Return true if it is valid to eliminate the CI |
| 3799 | // instruction. |
| 3800 | // |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3801 | static inline bool isEliminableCastOfCast(const Type *SrcTy, const Type *MidTy, |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 3802 | const Type *DstTy, TargetData *TD) { |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3803 | |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3804 | // It is legal to eliminate the instruction if casting A->B->A if the sizes |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3805 | // are identical and the bits don't get reinterpreted (for example |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 3806 | // int->float->int would not be allowed). |
Misha Brukman | e5838c4 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 3807 | if (SrcTy == DstTy && SrcTy->isLosslesslyConvertibleTo(MidTy)) |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3808 | return true; |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3809 | |
Chris Lattner | 4fbad96 | 2004-07-21 04:27:24 +0000 | [diff] [blame] | 3810 | // If we are casting between pointer and integer types, treat pointers as |
| 3811 | // integers of the appropriate size for the code below. |
| 3812 | if (isa<PointerType>(SrcTy)) SrcTy = TD->getIntPtrType(); |
| 3813 | if (isa<PointerType>(MidTy)) MidTy = TD->getIntPtrType(); |
| 3814 | if (isa<PointerType>(DstTy)) DstTy = TD->getIntPtrType(); |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 3815 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3816 | // Allow free casting and conversion of sizes as long as the sign doesn't |
| 3817 | // change... |
Chris Lattner | b0b412e | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 3818 | if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral()) { |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 3819 | CastType FirstCast = getCastType(SrcTy, MidTy); |
| 3820 | CastType SecondCast = getCastType(MidTy, DstTy); |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3821 | |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 3822 | // Capture the effect of these two casts. If the result is a legal cast, |
| 3823 | // the CastType is stored here, otherwise a special code is used. |
| 3824 | static const unsigned CastResult[] = { |
| 3825 | // First cast is noop |
| 3826 | 0, 1, 2, 3, |
| 3827 | // First cast is a truncate |
| 3828 | 1, 1, 4, 4, // trunc->extend is not safe to eliminate |
| 3829 | // First cast is a sign ext |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 3830 | 2, 5, 2, 4, // signext->zeroext never ok |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 3831 | // First cast is a zero ext |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 3832 | 3, 5, 3, 3, |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 3833 | }; |
| 3834 | |
| 3835 | unsigned Result = CastResult[FirstCast*4+SecondCast]; |
| 3836 | switch (Result) { |
| 3837 | default: assert(0 && "Illegal table value!"); |
| 3838 | case 0: |
| 3839 | case 1: |
| 3840 | case 2: |
| 3841 | case 3: |
| 3842 | // FIXME: in the future, when LLVM has explicit sign/zeroextends and |
| 3843 | // truncates, we could eliminate more casts. |
| 3844 | return (unsigned)getCastType(SrcTy, DstTy) == Result; |
| 3845 | case 4: |
| 3846 | return false; // Not possible to eliminate this here. |
| 3847 | case 5: |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 3848 | // Sign or zero extend followed by truncate is always ok if the result |
| 3849 | // is a truncate or noop. |
| 3850 | CastType ResultCast = getCastType(SrcTy, DstTy); |
| 3851 | if (ResultCast == Noop || ResultCast == Truncate) |
| 3852 | return true; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3853 | // Otherwise we are still growing the value, we are only safe if the |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 3854 | // result will match the sign/zeroextendness of the result. |
| 3855 | return ResultCast == FirstCast; |
Chris Lattner | 3732aca | 2002-08-15 16:15:25 +0000 | [diff] [blame] | 3856 | } |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3857 | } |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3858 | return false; |
| 3859 | } |
| 3860 | |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 3861 | static bool ValueRequiresCast(const Value *V, const Type *Ty, TargetData *TD) { |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3862 | if (V->getType() == Ty || isa<Constant>(V)) return false; |
| 3863 | if (const CastInst *CI = dyn_cast<CastInst>(V)) |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 3864 | if (isEliminableCastOfCast(CI->getOperand(0)->getType(), CI->getType(), Ty, |
| 3865 | TD)) |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3866 | return false; |
| 3867 | return true; |
| 3868 | } |
| 3869 | |
| 3870 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 3871 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 3872 | /// casts that are known to not do anything... |
| 3873 | /// |
| 3874 | Value *InstCombiner::InsertOperandCastBefore(Value *V, const Type *DestTy, |
| 3875 | Instruction *InsertBefore) { |
| 3876 | if (V->getType() == DestTy) return V; |
| 3877 | if (Constant *C = dyn_cast<Constant>(V)) |
| 3878 | return ConstantExpr::getCast(C, DestTy); |
| 3879 | |
| 3880 | CastInst *CI = new CastInst(V, DestTy, V->getName()); |
| 3881 | InsertNewInstBefore(CI, *InsertBefore); |
| 3882 | return CI; |
| 3883 | } |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3884 | |
Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 3885 | /// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear |
| 3886 | /// expression. If so, decompose it, returning some value X, such that Val is |
| 3887 | /// X*Scale+Offset. |
| 3888 | /// |
| 3889 | static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale, |
| 3890 | unsigned &Offset) { |
| 3891 | assert(Val->getType() == Type::UIntTy && "Unexpected allocation size type!"); |
| 3892 | if (ConstantUInt *CI = dyn_cast<ConstantUInt>(Val)) { |
| 3893 | Offset = CI->getValue(); |
| 3894 | Scale = 1; |
| 3895 | return ConstantUInt::get(Type::UIntTy, 0); |
| 3896 | } else if (Instruction *I = dyn_cast<Instruction>(Val)) { |
| 3897 | if (I->getNumOperands() == 2) { |
| 3898 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(I->getOperand(1))) { |
| 3899 | if (I->getOpcode() == Instruction::Shl) { |
| 3900 | // This is a value scaled by '1 << the shift amt'. |
| 3901 | Scale = 1U << CUI->getValue(); |
| 3902 | Offset = 0; |
| 3903 | return I->getOperand(0); |
| 3904 | } else if (I->getOpcode() == Instruction::Mul) { |
| 3905 | // This value is scaled by 'CUI'. |
| 3906 | Scale = CUI->getValue(); |
| 3907 | Offset = 0; |
| 3908 | return I->getOperand(0); |
| 3909 | } else if (I->getOpcode() == Instruction::Add) { |
| 3910 | // We have X+C. Check to see if we really have (X*C2)+C1, where C1 is |
| 3911 | // divisible by C2. |
| 3912 | unsigned SubScale; |
| 3913 | Value *SubVal = DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, |
| 3914 | Offset); |
| 3915 | Offset += CUI->getValue(); |
| 3916 | if (SubScale > 1 && (Offset % SubScale == 0)) { |
| 3917 | Scale = SubScale; |
| 3918 | return SubVal; |
| 3919 | } |
| 3920 | } |
| 3921 | } |
| 3922 | } |
| 3923 | } |
| 3924 | |
| 3925 | // Otherwise, we can't look past this. |
| 3926 | Scale = 1; |
| 3927 | Offset = 0; |
| 3928 | return Val; |
| 3929 | } |
| 3930 | |
| 3931 | |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 3932 | /// PromoteCastOfAllocation - If we find a cast of an allocation instruction, |
| 3933 | /// try to eliminate the cast by moving the type information into the alloc. |
| 3934 | Instruction *InstCombiner::PromoteCastOfAllocation(CastInst &CI, |
| 3935 | AllocationInst &AI) { |
| 3936 | const PointerType *PTy = dyn_cast<PointerType>(CI.getType()); |
Chris Lattner | bb17180 | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 3937 | if (!PTy) return 0; // Not casting the allocation to a pointer type. |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 3938 | |
Chris Lattner | ac87beb | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 3939 | // Remove any uses of AI that are dead. |
| 3940 | assert(!CI.use_empty() && "Dead instructions should be removed earlier!"); |
| 3941 | std::vector<Instruction*> DeadUsers; |
| 3942 | for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) { |
| 3943 | Instruction *User = cast<Instruction>(*UI++); |
| 3944 | if (isInstructionTriviallyDead(User)) { |
| 3945 | while (UI != E && *UI == User) |
| 3946 | ++UI; // If this instruction uses AI more than once, don't break UI. |
| 3947 | |
| 3948 | // Add operands to the worklist. |
| 3949 | AddUsesToWorkList(*User); |
| 3950 | ++NumDeadInst; |
| 3951 | DEBUG(std::cerr << "IC: DCE: " << *User); |
| 3952 | |
| 3953 | User->eraseFromParent(); |
| 3954 | removeFromWorkList(User); |
| 3955 | } |
| 3956 | } |
| 3957 | |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 3958 | // Get the type really allocated and the type casted to. |
| 3959 | const Type *AllocElTy = AI.getAllocatedType(); |
| 3960 | const Type *CastElTy = PTy->getElementType(); |
| 3961 | if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0; |
Chris Lattner | 355ecc0 | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 3962 | |
| 3963 | unsigned AllocElTyAlign = TD->getTypeSize(AllocElTy); |
| 3964 | unsigned CastElTyAlign = TD->getTypeSize(CastElTy); |
| 3965 | if (CastElTyAlign < AllocElTyAlign) return 0; |
| 3966 | |
Chris Lattner | 46705b2 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 3967 | // If the allocation has multiple uses, only promote it if we are strictly |
| 3968 | // increasing the alignment of the resultant allocation. If we keep it the |
| 3969 | // same, we open the door to infinite loops of various kinds. |
| 3970 | if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0; |
| 3971 | |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 3972 | uint64_t AllocElTySize = TD->getTypeSize(AllocElTy); |
| 3973 | uint64_t CastElTySize = TD->getTypeSize(CastElTy); |
Chris Lattner | bb17180 | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 3974 | if (CastElTySize == 0 || AllocElTySize == 0) return 0; |
Chris Lattner | 355ecc0 | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 3975 | |
Chris Lattner | 8270c33 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 3976 | // See if we can satisfy the modulus by pulling a scale out of the array |
| 3977 | // size argument. |
Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 3978 | unsigned ArraySizeScale, ArrayOffset; |
| 3979 | Value *NumElements = // See if the array size is a decomposable linear expr. |
| 3980 | DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset); |
| 3981 | |
Chris Lattner | 8270c33 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 3982 | // If we can now satisfy the modulus, by using a non-1 scale, we really can |
| 3983 | // do the xform. |
Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 3984 | if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 || |
| 3985 | (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0; |
Chris Lattner | b3ecf96 | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 3986 | |
Chris Lattner | 8270c33 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 3987 | unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize; |
| 3988 | Value *Amt = 0; |
| 3989 | if (Scale == 1) { |
| 3990 | Amt = NumElements; |
| 3991 | } else { |
| 3992 | Amt = ConstantUInt::get(Type::UIntTy, Scale); |
| 3993 | if (ConstantUInt *CI = dyn_cast<ConstantUInt>(NumElements)) |
| 3994 | Amt = ConstantExpr::getMul(CI, cast<ConstantUInt>(Amt)); |
| 3995 | else if (Scale != 1) { |
| 3996 | Instruction *Tmp = BinaryOperator::createMul(Amt, NumElements, "tmp"); |
| 3997 | Amt = InsertNewInstBefore(Tmp, AI); |
Chris Lattner | b3ecf96 | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 3998 | } |
Chris Lattner | bb17180 | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 3999 | } |
| 4000 | |
Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 4001 | if (unsigned Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { |
| 4002 | Value *Off = ConstantUInt::get(Type::UIntTy, Offset); |
| 4003 | Instruction *Tmp = BinaryOperator::createAdd(Amt, Off, "tmp"); |
| 4004 | Amt = InsertNewInstBefore(Tmp, AI); |
| 4005 | } |
| 4006 | |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 4007 | std::string Name = AI.getName(); AI.setName(""); |
| 4008 | AllocationInst *New; |
| 4009 | if (isa<MallocInst>(AI)) |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 4010 | New = new MallocInst(CastElTy, Amt, AI.getAlignment(), Name); |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 4011 | else |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 4012 | New = new AllocaInst(CastElTy, Amt, AI.getAlignment(), Name); |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 4013 | InsertNewInstBefore(New, AI); |
Chris Lattner | 46705b2 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 4014 | |
| 4015 | // If the allocation has multiple uses, insert a cast and change all things |
| 4016 | // that used it to use the new cast. This will also hack on CI, but it will |
| 4017 | // die soon. |
| 4018 | if (!AI.hasOneUse()) { |
| 4019 | AddUsesToWorkList(AI); |
| 4020 | CastInst *NewCast = new CastInst(New, AI.getType(), "tmpcast"); |
| 4021 | InsertNewInstBefore(NewCast, AI); |
| 4022 | AI.replaceAllUsesWith(NewCast); |
| 4023 | } |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 4024 | return ReplaceInstUsesWith(CI, New); |
| 4025 | } |
| 4026 | |
| 4027 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4028 | // CastInst simplification |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 4029 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4030 | Instruction *InstCombiner::visitCastInst(CastInst &CI) { |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 4031 | Value *Src = CI.getOperand(0); |
| 4032 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4033 | // If the user is casting a value to the same type, eliminate this cast |
| 4034 | // instruction... |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 4035 | if (CI.getType() == Src->getType()) |
| 4036 | return ReplaceInstUsesWith(CI, Src); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4037 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4038 | if (isa<UndefValue>(Src)) // cast undef -> undef |
| 4039 | return ReplaceInstUsesWith(CI, UndefValue::get(CI.getType())); |
| 4040 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4041 | // If casting the result of another cast instruction, try to eliminate this |
| 4042 | // one! |
| 4043 | // |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4044 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
| 4045 | Value *A = CSrc->getOperand(0); |
| 4046 | if (isEliminableCastOfCast(A->getType(), CSrc->getType(), |
| 4047 | CI.getType(), TD)) { |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4048 | // This instruction now refers directly to the cast's src operand. This |
| 4049 | // has a good chance of making CSrc dead. |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4050 | CI.setOperand(0, CSrc->getOperand(0)); |
| 4051 | return &CI; |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4052 | } |
| 4053 | |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 4054 | // If this is an A->B->A cast, and we are dealing with integral types, try |
| 4055 | // to convert this into a logical 'and' instruction. |
| 4056 | // |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4057 | if (A->getType()->isInteger() && |
Chris Lattner | b0b412e | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 4058 | CI.getType()->isInteger() && CSrc->getType()->isInteger() && |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4059 | CSrc->getType()->isUnsigned() && // B->A cast must zero extend |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4060 | CSrc->getType()->getPrimitiveSizeInBits() < |
| 4061 | CI.getType()->getPrimitiveSizeInBits()&& |
| 4062 | A->getType()->getPrimitiveSizeInBits() == |
| 4063 | CI.getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 4064 | assert(CSrc->getType() != Type::ULongTy && |
| 4065 | "Cannot have type bigger than ulong!"); |
Chris Lattner | 2f1457f | 2005-04-24 17:46:05 +0000 | [diff] [blame] | 4066 | uint64_t AndValue = ~0ULL>>(64-CSrc->getType()->getPrimitiveSizeInBits()); |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4067 | Constant *AndOp = ConstantUInt::get(A->getType()->getUnsignedVersion(), |
| 4068 | AndValue); |
| 4069 | AndOp = ConstantExpr::getCast(AndOp, A->getType()); |
| 4070 | Instruction *And = BinaryOperator::createAnd(CSrc->getOperand(0), AndOp); |
| 4071 | if (And->getType() != CI.getType()) { |
| 4072 | And->setName(CSrc->getName()+".mask"); |
| 4073 | InsertNewInstBefore(And, CI); |
| 4074 | And = new CastInst(And, CI.getType()); |
| 4075 | } |
| 4076 | return And; |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 4077 | } |
| 4078 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4079 | |
Chris Lattner | 0384165 | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 4080 | // If this is a cast to bool, turn it into the appropriate setne instruction. |
| 4081 | if (CI.getType() == Type::BoolTy) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4082 | return BinaryOperator::createSetNE(CI.getOperand(0), |
Chris Lattner | 0384165 | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 4083 | Constant::getNullValue(CI.getOperand(0)->getType())); |
| 4084 | |
Chris Lattner | d0d5160 | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 4085 | // If casting the result of a getelementptr instruction with no offset, turn |
| 4086 | // this into a cast of the original pointer! |
| 4087 | // |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 4088 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
Chris Lattner | d0d5160 | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 4089 | bool AllZeroOperands = true; |
| 4090 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) |
| 4091 | if (!isa<Constant>(GEP->getOperand(i)) || |
| 4092 | !cast<Constant>(GEP->getOperand(i))->isNullValue()) { |
| 4093 | AllZeroOperands = false; |
| 4094 | break; |
| 4095 | } |
| 4096 | if (AllZeroOperands) { |
| 4097 | CI.setOperand(0, GEP->getOperand(0)); |
| 4098 | return &CI; |
| 4099 | } |
| 4100 | } |
| 4101 | |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 4102 | // If we are casting a malloc or alloca to a pointer to a type of the same |
| 4103 | // size, rewrite the allocation instruction to allocate the "right" type. |
| 4104 | // |
| 4105 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 4106 | if (Instruction *V = PromoteCastOfAllocation(CI, *AI)) |
| 4107 | return V; |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 4108 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4109 | if (SelectInst *SI = dyn_cast<SelectInst>(Src)) |
| 4110 | if (Instruction *NV = FoldOpIntoSelect(CI, SI, this)) |
| 4111 | return NV; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 4112 | if (isa<PHINode>(Src)) |
| 4113 | if (Instruction *NV = FoldOpIntoPhi(CI)) |
| 4114 | return NV; |
| 4115 | |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 4116 | // If the source value is an instruction with only this use, we can attempt to |
| 4117 | // propagate the cast into the instruction. Also, only handle integral types |
| 4118 | // for now. |
| 4119 | if (Instruction *SrcI = dyn_cast<Instruction>(Src)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 4120 | if (SrcI->hasOneUse() && Src->getType()->isIntegral() && |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 4121 | CI.getType()->isInteger()) { // Don't mess with casts to bool here |
| 4122 | const Type *DestTy = CI.getType(); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4123 | unsigned SrcBitSize = Src->getType()->getPrimitiveSizeInBits(); |
| 4124 | unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 4125 | |
| 4126 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; |
| 4127 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; |
| 4128 | |
| 4129 | switch (SrcI->getOpcode()) { |
| 4130 | case Instruction::Add: |
| 4131 | case Instruction::Mul: |
| 4132 | case Instruction::And: |
| 4133 | case Instruction::Or: |
| 4134 | case Instruction::Xor: |
| 4135 | // If we are discarding information, or just changing the sign, rewrite. |
| 4136 | if (DestBitSize <= SrcBitSize && DestBitSize != 1) { |
| 4137 | // Don't insert two casts if they cannot be eliminated. We allow two |
| 4138 | // casts to be inserted if the sizes are the same. This could only be |
| 4139 | // converting signedness, which is a noop. |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 4140 | if (DestBitSize == SrcBitSize || !ValueRequiresCast(Op1, DestTy,TD) || |
| 4141 | !ValueRequiresCast(Op0, DestTy, TD)) { |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 4142 | Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI); |
| 4143 | Value *Op1c = InsertOperandCastBefore(Op1, DestTy, SrcI); |
| 4144 | return BinaryOperator::create(cast<BinaryOperator>(SrcI) |
| 4145 | ->getOpcode(), Op0c, Op1c); |
| 4146 | } |
| 4147 | } |
Chris Lattner | 7208616 | 2005-05-06 02:07:39 +0000 | [diff] [blame] | 4148 | |
| 4149 | // cast (xor bool X, true) to int --> xor (cast bool X to int), 1 |
| 4150 | if (SrcBitSize == 1 && SrcI->getOpcode() == Instruction::Xor && |
| 4151 | Op1 == ConstantBool::True && |
| 4152 | (!Op0->hasOneUse() || !isa<SetCondInst>(Op0))) { |
| 4153 | Value *New = InsertOperandCastBefore(Op0, DestTy, &CI); |
| 4154 | return BinaryOperator::createXor(New, |
| 4155 | ConstantInt::get(CI.getType(), 1)); |
| 4156 | } |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 4157 | break; |
| 4158 | case Instruction::Shl: |
| 4159 | // Allow changing the sign of the source operand. Do not allow changing |
| 4160 | // the size of the shift, UNLESS the shift amount is a constant. We |
| 4161 | // mush not change variable sized shifts to a smaller size, because it |
| 4162 | // is undefined to shift more bits out than exist in the value. |
| 4163 | if (DestBitSize == SrcBitSize || |
| 4164 | (DestBitSize < SrcBitSize && isa<Constant>(Op1))) { |
| 4165 | Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI); |
| 4166 | return new ShiftInst(Instruction::Shl, Op0c, Op1); |
| 4167 | } |
| 4168 | break; |
Chris Lattner | 8738041 | 2005-05-06 04:18:52 +0000 | [diff] [blame] | 4169 | case Instruction::Shr: |
| 4170 | // If this is a signed shr, and if all bits shifted in are about to be |
| 4171 | // truncated off, turn it into an unsigned shr to allow greater |
| 4172 | // simplifications. |
| 4173 | if (DestBitSize < SrcBitSize && Src->getType()->isSigned() && |
| 4174 | isa<ConstantInt>(Op1)) { |
| 4175 | unsigned ShiftAmt = cast<ConstantUInt>(Op1)->getValue(); |
| 4176 | if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) { |
| 4177 | // Convert to unsigned. |
| 4178 | Value *N1 = InsertOperandCastBefore(Op0, |
| 4179 | Op0->getType()->getUnsignedVersion(), &CI); |
| 4180 | // Insert the new shift, which is now unsigned. |
| 4181 | N1 = InsertNewInstBefore(new ShiftInst(Instruction::Shr, N1, |
| 4182 | Op1, Src->getName()), CI); |
| 4183 | return new CastInst(N1, CI.getType()); |
| 4184 | } |
| 4185 | } |
| 4186 | break; |
| 4187 | |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 4188 | case Instruction::SetNE: |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 4189 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 4190 | if (Op1C->getRawValue() == 0) { |
| 4191 | // If the input only has the low bit set, simplify directly. |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4192 | Constant *Not1 = |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 4193 | ConstantExpr::getNot(ConstantInt::get(Op0->getType(), 1)); |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 4194 | // cast (X != 0) to int --> X if X&~1 == 0 |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 4195 | if (MaskedValueIsZero(Op0, cast<ConstantIntegral>(Not1))) { |
| 4196 | if (CI.getType() == Op0->getType()) |
| 4197 | return ReplaceInstUsesWith(CI, Op0); |
| 4198 | else |
| 4199 | return new CastInst(Op0, CI.getType()); |
| 4200 | } |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 4201 | |
| 4202 | // If the input is an and with a single bit, shift then simplify. |
| 4203 | ConstantInt *AndRHS; |
| 4204 | if (match(Op0, m_And(m_Value(), m_ConstantInt(AndRHS)))) |
| 4205 | if (AndRHS->getRawValue() && |
| 4206 | (AndRHS->getRawValue() & (AndRHS->getRawValue()-1)) == 0) { |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 4207 | unsigned ShiftAmt = Log2_64(AndRHS->getRawValue()); |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 4208 | // Perform an unsigned shr by shiftamt. Convert input to |
| 4209 | // unsigned if it is signed. |
| 4210 | Value *In = Op0; |
| 4211 | if (In->getType()->isSigned()) |
| 4212 | In = InsertNewInstBefore(new CastInst(In, |
| 4213 | In->getType()->getUnsignedVersion(), In->getName()),CI); |
| 4214 | // Insert the shift to put the result in the low bit. |
| 4215 | In = InsertNewInstBefore(new ShiftInst(Instruction::Shr, In, |
| 4216 | ConstantInt::get(Type::UByteTy, ShiftAmt), |
| 4217 | In->getName()+".lobit"), CI); |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 4218 | if (CI.getType() == In->getType()) |
| 4219 | return ReplaceInstUsesWith(CI, In); |
| 4220 | else |
| 4221 | return new CastInst(In, CI.getType()); |
| 4222 | } |
| 4223 | } |
| 4224 | } |
| 4225 | break; |
| 4226 | case Instruction::SetEQ: |
| 4227 | // We if we are just checking for a seteq of a single bit and casting it |
| 4228 | // to an integer. If so, shift the bit to the appropriate place then |
| 4229 | // cast to integer to avoid the comparison. |
| 4230 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { |
| 4231 | // Is Op1C a power of two or zero? |
| 4232 | if ((Op1C->getRawValue() & Op1C->getRawValue()-1) == 0) { |
| 4233 | // cast (X == 1) to int -> X iff X has only the low bit set. |
| 4234 | if (Op1C->getRawValue() == 1) { |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4235 | Constant *Not1 = |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 4236 | ConstantExpr::getNot(ConstantInt::get(Op0->getType(), 1)); |
| 4237 | if (MaskedValueIsZero(Op0, cast<ConstantIntegral>(Not1))) { |
| 4238 | if (CI.getType() == Op0->getType()) |
| 4239 | return ReplaceInstUsesWith(CI, Op0); |
| 4240 | else |
| 4241 | return new CastInst(Op0, CI.getType()); |
| 4242 | } |
| 4243 | } |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 4244 | } |
| 4245 | } |
| 4246 | break; |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 4247 | } |
| 4248 | } |
Chris Lattner | bb17180 | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 4249 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 4250 | return 0; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 4251 | } |
| 4252 | |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 4253 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
| 4254 | /// %C = or %A, %B |
| 4255 | /// %D = select %cond, %C, %A |
| 4256 | /// into: |
| 4257 | /// %C = select %cond, %B, 0 |
| 4258 | /// %D = or %A, %C |
| 4259 | /// |
| 4260 | /// Assuming that the specified instruction is an operand to the select, return |
| 4261 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 4262 | /// equal the other incoming value of the select. |
| 4263 | /// |
| 4264 | static unsigned GetSelectFoldableOperands(Instruction *I) { |
| 4265 | switch (I->getOpcode()) { |
| 4266 | case Instruction::Add: |
| 4267 | case Instruction::Mul: |
| 4268 | case Instruction::And: |
| 4269 | case Instruction::Or: |
| 4270 | case Instruction::Xor: |
| 4271 | return 3; // Can fold through either operand. |
| 4272 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 4273 | case Instruction::Shl: // Can only fold on the shift amount. |
| 4274 | case Instruction::Shr: |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4275 | return 1; |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 4276 | default: |
| 4277 | return 0; // Cannot fold |
| 4278 | } |
| 4279 | } |
| 4280 | |
| 4281 | /// GetSelectFoldableConstant - For the same transformation as the previous |
| 4282 | /// function, return the identity constant that goes into the select. |
| 4283 | static Constant *GetSelectFoldableConstant(Instruction *I) { |
| 4284 | switch (I->getOpcode()) { |
| 4285 | default: assert(0 && "This cannot happen!"); abort(); |
| 4286 | case Instruction::Add: |
| 4287 | case Instruction::Sub: |
| 4288 | case Instruction::Or: |
| 4289 | case Instruction::Xor: |
| 4290 | return Constant::getNullValue(I->getType()); |
| 4291 | case Instruction::Shl: |
| 4292 | case Instruction::Shr: |
| 4293 | return Constant::getNullValue(Type::UByteTy); |
| 4294 | case Instruction::And: |
| 4295 | return ConstantInt::getAllOnesValue(I->getType()); |
| 4296 | case Instruction::Mul: |
| 4297 | return ConstantInt::get(I->getType(), 1); |
| 4298 | } |
| 4299 | } |
| 4300 | |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 4301 | /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI |
| 4302 | /// have the same opcode and only one use each. Try to simplify this. |
| 4303 | Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 4304 | Instruction *FI) { |
| 4305 | if (TI->getNumOperands() == 1) { |
| 4306 | // If this is a non-volatile load or a cast from the same type, |
| 4307 | // merge. |
| 4308 | if (TI->getOpcode() == Instruction::Cast) { |
| 4309 | if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType()) |
| 4310 | return 0; |
| 4311 | } else { |
| 4312 | return 0; // unknown unary op. |
| 4313 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4314 | |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 4315 | // Fold this by inserting a select from the input values. |
| 4316 | SelectInst *NewSI = new SelectInst(SI.getCondition(), TI->getOperand(0), |
| 4317 | FI->getOperand(0), SI.getName()+".v"); |
| 4318 | InsertNewInstBefore(NewSI, SI); |
| 4319 | return new CastInst(NewSI, TI->getType()); |
| 4320 | } |
| 4321 | |
| 4322 | // Only handle binary operators here. |
| 4323 | if (!isa<ShiftInst>(TI) && !isa<BinaryOperator>(TI)) |
| 4324 | return 0; |
| 4325 | |
| 4326 | // Figure out if the operations have any operands in common. |
| 4327 | Value *MatchOp, *OtherOpT, *OtherOpF; |
| 4328 | bool MatchIsOpZero; |
| 4329 | if (TI->getOperand(0) == FI->getOperand(0)) { |
| 4330 | MatchOp = TI->getOperand(0); |
| 4331 | OtherOpT = TI->getOperand(1); |
| 4332 | OtherOpF = FI->getOperand(1); |
| 4333 | MatchIsOpZero = true; |
| 4334 | } else if (TI->getOperand(1) == FI->getOperand(1)) { |
| 4335 | MatchOp = TI->getOperand(1); |
| 4336 | OtherOpT = TI->getOperand(0); |
| 4337 | OtherOpF = FI->getOperand(0); |
| 4338 | MatchIsOpZero = false; |
| 4339 | } else if (!TI->isCommutative()) { |
| 4340 | return 0; |
| 4341 | } else if (TI->getOperand(0) == FI->getOperand(1)) { |
| 4342 | MatchOp = TI->getOperand(0); |
| 4343 | OtherOpT = TI->getOperand(1); |
| 4344 | OtherOpF = FI->getOperand(0); |
| 4345 | MatchIsOpZero = true; |
| 4346 | } else if (TI->getOperand(1) == FI->getOperand(0)) { |
| 4347 | MatchOp = TI->getOperand(1); |
| 4348 | OtherOpT = TI->getOperand(0); |
| 4349 | OtherOpF = FI->getOperand(1); |
| 4350 | MatchIsOpZero = true; |
| 4351 | } else { |
| 4352 | return 0; |
| 4353 | } |
| 4354 | |
| 4355 | // If we reach here, they do have operations in common. |
| 4356 | SelectInst *NewSI = new SelectInst(SI.getCondition(), OtherOpT, |
| 4357 | OtherOpF, SI.getName()+".v"); |
| 4358 | InsertNewInstBefore(NewSI, SI); |
| 4359 | |
| 4360 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) { |
| 4361 | if (MatchIsOpZero) |
| 4362 | return BinaryOperator::create(BO->getOpcode(), MatchOp, NewSI); |
| 4363 | else |
| 4364 | return BinaryOperator::create(BO->getOpcode(), NewSI, MatchOp); |
| 4365 | } else { |
| 4366 | if (MatchIsOpZero) |
| 4367 | return new ShiftInst(cast<ShiftInst>(TI)->getOpcode(), MatchOp, NewSI); |
| 4368 | else |
| 4369 | return new ShiftInst(cast<ShiftInst>(TI)->getOpcode(), NewSI, MatchOp); |
| 4370 | } |
| 4371 | } |
| 4372 | |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 4373 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 4374 | Value *CondVal = SI.getCondition(); |
| 4375 | Value *TrueVal = SI.getTrueValue(); |
| 4376 | Value *FalseVal = SI.getFalseValue(); |
| 4377 | |
| 4378 | // select true, X, Y -> X |
| 4379 | // select false, X, Y -> Y |
| 4380 | if (ConstantBool *C = dyn_cast<ConstantBool>(CondVal)) |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 4381 | if (C == ConstantBool::True) |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 4382 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 4383 | else { |
| 4384 | assert(C == ConstantBool::False); |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 4385 | return ReplaceInstUsesWith(SI, FalseVal); |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 4386 | } |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 4387 | |
| 4388 | // select C, X, X -> X |
| 4389 | if (TrueVal == FalseVal) |
| 4390 | return ReplaceInstUsesWith(SI, TrueVal); |
| 4391 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4392 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 4393 | return ReplaceInstUsesWith(SI, FalseVal); |
| 4394 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 4395 | return ReplaceInstUsesWith(SI, TrueVal); |
| 4396 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 4397 | if (isa<Constant>(TrueVal)) |
| 4398 | return ReplaceInstUsesWith(SI, TrueVal); |
| 4399 | else |
| 4400 | return ReplaceInstUsesWith(SI, FalseVal); |
| 4401 | } |
| 4402 | |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 4403 | if (SI.getType() == Type::BoolTy) |
| 4404 | if (ConstantBool *C = dyn_cast<ConstantBool>(TrueVal)) { |
| 4405 | if (C == ConstantBool::True) { |
| 4406 | // Change: A = select B, true, C --> A = or B, C |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4407 | return BinaryOperator::createOr(CondVal, FalseVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 4408 | } else { |
| 4409 | // Change: A = select B, false, C --> A = and !B, C |
| 4410 | Value *NotCond = |
| 4411 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 4412 | "not."+CondVal->getName()), SI); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4413 | return BinaryOperator::createAnd(NotCond, FalseVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 4414 | } |
| 4415 | } else if (ConstantBool *C = dyn_cast<ConstantBool>(FalseVal)) { |
| 4416 | if (C == ConstantBool::False) { |
| 4417 | // Change: A = select B, C, false --> A = and B, C |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4418 | return BinaryOperator::createAnd(CondVal, TrueVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 4419 | } else { |
| 4420 | // Change: A = select B, C, true --> A = or !B, C |
| 4421 | Value *NotCond = |
| 4422 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 4423 | "not."+CondVal->getName()), SI); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4424 | return BinaryOperator::createOr(NotCond, TrueVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 4425 | } |
| 4426 | } |
| 4427 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4428 | // Selecting between two integer constants? |
| 4429 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) |
| 4430 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { |
| 4431 | // select C, 1, 0 -> cast C to int |
| 4432 | if (FalseValC->isNullValue() && TrueValC->getRawValue() == 1) { |
| 4433 | return new CastInst(CondVal, SI.getType()); |
| 4434 | } else if (TrueValC->isNullValue() && FalseValC->getRawValue() == 1) { |
| 4435 | // select C, 0, 1 -> cast !C to int |
| 4436 | Value *NotCond = |
| 4437 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
Chris Lattner | cf7baf3 | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 4438 | "not."+CondVal->getName()), SI); |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4439 | return new CastInst(NotCond, SI.getType()); |
Chris Lattner | cf7baf3 | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 4440 | } |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 4441 | |
| 4442 | // If one of the constants is zero (we know they can't both be) and we |
| 4443 | // have a setcc instruction with zero, and we have an 'and' with the |
| 4444 | // non-constant value, eliminate this whole mess. This corresponds to |
| 4445 | // cases like this: ((X & 27) ? 27 : 0) |
| 4446 | if (TrueValC->isNullValue() || FalseValC->isNullValue()) |
| 4447 | if (Instruction *IC = dyn_cast<Instruction>(SI.getCondition())) |
| 4448 | if ((IC->getOpcode() == Instruction::SetEQ || |
| 4449 | IC->getOpcode() == Instruction::SetNE) && |
| 4450 | isa<ConstantInt>(IC->getOperand(1)) && |
| 4451 | cast<Constant>(IC->getOperand(1))->isNullValue()) |
| 4452 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) |
| 4453 | if (ICA->getOpcode() == Instruction::And && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4454 | isa<ConstantInt>(ICA->getOperand(1)) && |
| 4455 | (ICA->getOperand(1) == TrueValC || |
| 4456 | ICA->getOperand(1) == FalseValC) && |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 4457 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { |
| 4458 | // Okay, now we know that everything is set up, we just don't |
| 4459 | // know whether we have a setne or seteq and whether the true or |
| 4460 | // false val is the zero. |
| 4461 | bool ShouldNotVal = !TrueValC->isNullValue(); |
| 4462 | ShouldNotVal ^= IC->getOpcode() == Instruction::SetNE; |
| 4463 | Value *V = ICA; |
| 4464 | if (ShouldNotVal) |
| 4465 | V = InsertNewInstBefore(BinaryOperator::create( |
| 4466 | Instruction::Xor, V, ICA->getOperand(1)), SI); |
| 4467 | return ReplaceInstUsesWith(SI, V); |
| 4468 | } |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 4469 | } |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 4470 | |
| 4471 | // See if we are selecting two values based on a comparison of the two values. |
| 4472 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(CondVal)) { |
| 4473 | if (SCI->getOperand(0) == TrueVal && SCI->getOperand(1) == FalseVal) { |
| 4474 | // Transform (X == Y) ? X : Y -> Y |
| 4475 | if (SCI->getOpcode() == Instruction::SetEQ) |
| 4476 | return ReplaceInstUsesWith(SI, FalseVal); |
| 4477 | // Transform (X != Y) ? X : Y -> X |
| 4478 | if (SCI->getOpcode() == Instruction::SetNE) |
| 4479 | return ReplaceInstUsesWith(SI, TrueVal); |
| 4480 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 4481 | |
| 4482 | } else if (SCI->getOperand(0) == FalseVal && SCI->getOperand(1) == TrueVal){ |
| 4483 | // Transform (X == Y) ? Y : X -> X |
| 4484 | if (SCI->getOpcode() == Instruction::SetEQ) |
Chris Lattner | 24cf020 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 4485 | return ReplaceInstUsesWith(SI, FalseVal); |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 4486 | // Transform (X != Y) ? Y : X -> Y |
| 4487 | if (SCI->getOpcode() == Instruction::SetNE) |
Chris Lattner | 24cf020 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 4488 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 4489 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 4490 | } |
| 4491 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4492 | |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 4493 | if (Instruction *TI = dyn_cast<Instruction>(TrueVal)) |
| 4494 | if (Instruction *FI = dyn_cast<Instruction>(FalseVal)) |
| 4495 | if (TI->hasOneUse() && FI->hasOneUse()) { |
| 4496 | bool isInverse = false; |
| 4497 | Instruction *AddOp = 0, *SubOp = 0; |
| 4498 | |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 4499 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) |
| 4500 | if (TI->getOpcode() == FI->getOpcode()) |
| 4501 | if (Instruction *IV = FoldSelectOpOp(SI, TI, FI)) |
| 4502 | return IV; |
| 4503 | |
| 4504 | // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is |
| 4505 | // even legal for FP. |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 4506 | if (TI->getOpcode() == Instruction::Sub && |
| 4507 | FI->getOpcode() == Instruction::Add) { |
| 4508 | AddOp = FI; SubOp = TI; |
| 4509 | } else if (FI->getOpcode() == Instruction::Sub && |
| 4510 | TI->getOpcode() == Instruction::Add) { |
| 4511 | AddOp = TI; SubOp = FI; |
| 4512 | } |
| 4513 | |
| 4514 | if (AddOp) { |
| 4515 | Value *OtherAddOp = 0; |
| 4516 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { |
| 4517 | OtherAddOp = AddOp->getOperand(1); |
| 4518 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { |
| 4519 | OtherAddOp = AddOp->getOperand(0); |
| 4520 | } |
| 4521 | |
| 4522 | if (OtherAddOp) { |
| 4523 | // So at this point we know we have: |
| 4524 | // select C, (add X, Y), (sub X, ?) |
| 4525 | // We can do the transform profitably if either 'Y' = '?' or '?' is |
| 4526 | // a constant. |
| 4527 | if (SubOp->getOperand(1) == AddOp || |
| 4528 | isa<Constant>(SubOp->getOperand(1))) { |
| 4529 | Value *NegVal; |
| 4530 | if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) { |
| 4531 | NegVal = ConstantExpr::getNeg(C); |
| 4532 | } else { |
| 4533 | NegVal = InsertNewInstBefore( |
| 4534 | BinaryOperator::createNeg(SubOp->getOperand(1)), SI); |
| 4535 | } |
| 4536 | |
Chris Lattner | 51726c4 | 2005-01-14 17:35:12 +0000 | [diff] [blame] | 4537 | Value *NewTrueOp = OtherAddOp; |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 4538 | Value *NewFalseOp = NegVal; |
| 4539 | if (AddOp != TI) |
| 4540 | std::swap(NewTrueOp, NewFalseOp); |
| 4541 | Instruction *NewSel = |
| 4542 | new SelectInst(CondVal, NewTrueOp,NewFalseOp,SI.getName()+".p"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4543 | |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 4544 | NewSel = InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 51726c4 | 2005-01-14 17:35:12 +0000 | [diff] [blame] | 4545 | return BinaryOperator::createAdd(SubOp->getOperand(0), NewSel); |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 4546 | } |
| 4547 | } |
| 4548 | } |
| 4549 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4550 | |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 4551 | // See if we can fold the select into one of our operands. |
| 4552 | if (SI.getType()->isInteger()) { |
| 4553 | // See the comment above GetSelectFoldableOperands for a description of the |
| 4554 | // transformation we are doing here. |
| 4555 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) |
| 4556 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && |
| 4557 | !isa<Constant>(FalseVal)) |
| 4558 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { |
| 4559 | unsigned OpToFold = 0; |
| 4560 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 4561 | OpToFold = 1; |
| 4562 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
| 4563 | OpToFold = 2; |
| 4564 | } |
| 4565 | |
| 4566 | if (OpToFold) { |
| 4567 | Constant *C = GetSelectFoldableConstant(TVI); |
| 4568 | std::string Name = TVI->getName(); TVI->setName(""); |
| 4569 | Instruction *NewSel = |
| 4570 | new SelectInst(SI.getCondition(), TVI->getOperand(2-OpToFold), C, |
| 4571 | Name); |
| 4572 | InsertNewInstBefore(NewSel, SI); |
| 4573 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) |
| 4574 | return BinaryOperator::create(BO->getOpcode(), FalseVal, NewSel); |
| 4575 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(TVI)) |
| 4576 | return new ShiftInst(SI->getOpcode(), FalseVal, NewSel); |
| 4577 | else { |
| 4578 | assert(0 && "Unknown instruction!!"); |
| 4579 | } |
| 4580 | } |
| 4581 | } |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4582 | |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 4583 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) |
| 4584 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && |
| 4585 | !isa<Constant>(TrueVal)) |
| 4586 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { |
| 4587 | unsigned OpToFold = 0; |
| 4588 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 4589 | OpToFold = 1; |
| 4590 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
| 4591 | OpToFold = 2; |
| 4592 | } |
| 4593 | |
| 4594 | if (OpToFold) { |
| 4595 | Constant *C = GetSelectFoldableConstant(FVI); |
| 4596 | std::string Name = FVI->getName(); FVI->setName(""); |
| 4597 | Instruction *NewSel = |
| 4598 | new SelectInst(SI.getCondition(), C, FVI->getOperand(2-OpToFold), |
| 4599 | Name); |
| 4600 | InsertNewInstBefore(NewSel, SI); |
| 4601 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) |
| 4602 | return BinaryOperator::create(BO->getOpcode(), TrueVal, NewSel); |
| 4603 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(FVI)) |
| 4604 | return new ShiftInst(SI->getOpcode(), TrueVal, NewSel); |
| 4605 | else { |
| 4606 | assert(0 && "Unknown instruction!!"); |
| 4607 | } |
| 4608 | } |
| 4609 | } |
| 4610 | } |
Chris Lattner | d6f636a | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 4611 | |
| 4612 | if (BinaryOperator::isNot(CondVal)) { |
| 4613 | SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); |
| 4614 | SI.setOperand(1, FalseVal); |
| 4615 | SI.setOperand(2, TrueVal); |
| 4616 | return &SI; |
| 4617 | } |
| 4618 | |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 4619 | return 0; |
| 4620 | } |
| 4621 | |
| 4622 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4623 | // CallInst simplification |
| 4624 | // |
| 4625 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 4626 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 4627 | // visitCallSite. |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 4628 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(&CI)) { |
| 4629 | bool Changed = false; |
| 4630 | |
| 4631 | // memmove/cpy/set of zero bytes is a noop. |
| 4632 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { |
| 4633 | if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); |
| 4634 | |
| 4635 | // FIXME: Increase alignment here. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4636 | |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 4637 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
| 4638 | if (CI->getRawValue() == 1) { |
| 4639 | // Replace the instruction with just byte operations. We would |
| 4640 | // transform other cases to loads/stores, but we don't know if |
| 4641 | // alignment is sufficient. |
| 4642 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 4643 | } |
| 4644 | |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 4645 | // If we have a memmove and the source operation is a constant global, |
| 4646 | // then the source and dest pointers can't alias, so we can change this |
| 4647 | // into a call to memcpy. |
| 4648 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) |
| 4649 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
| 4650 | if (GVSrc->isConstant()) { |
| 4651 | Module *M = CI.getParent()->getParent()->getParent(); |
| 4652 | Function *MemCpy = M->getOrInsertFunction("llvm.memcpy", |
| 4653 | CI.getCalledFunction()->getFunctionType()); |
| 4654 | CI.setOperand(0, MemCpy); |
| 4655 | Changed = true; |
| 4656 | } |
| 4657 | |
| 4658 | if (Changed) return &CI; |
Chris Lattner | 9530754 | 2004-11-18 21:41:39 +0000 | [diff] [blame] | 4659 | } else if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(&CI)) { |
| 4660 | // If this stoppoint is at the same source location as the previous |
| 4661 | // stoppoint in the chain, it is not needed. |
| 4662 | if (DbgStopPointInst *PrevSPI = |
| 4663 | dyn_cast<DbgStopPointInst>(SPI->getChain())) |
| 4664 | if (SPI->getLineNo() == PrevSPI->getLineNo() && |
| 4665 | SPI->getColNo() == PrevSPI->getColNo()) { |
| 4666 | SPI->replaceAllUsesWith(PrevSPI); |
| 4667 | return EraseInstFromFunction(CI); |
| 4668 | } |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 4669 | } |
| 4670 | |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 4671 | return visitCallSite(&CI); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4672 | } |
| 4673 | |
| 4674 | // InvokeInst simplification |
| 4675 | // |
| 4676 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 4677 | return visitCallSite(&II); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4678 | } |
| 4679 | |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 4680 | // visitCallSite - Improvements for call and invoke instructions. |
| 4681 | // |
| 4682 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 4683 | bool Changed = false; |
| 4684 | |
| 4685 | // If the callee is a constexpr cast of a function, attempt to move the cast |
| 4686 | // to the arguments of the call/invoke. |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 4687 | if (transformConstExprCastCall(CS)) return 0; |
| 4688 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 4689 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4690 | |
Chris Lattner | 61d9d81 | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 4691 | if (Function *CalleeF = dyn_cast<Function>(Callee)) |
| 4692 | if (CalleeF->getCallingConv() != CS.getCallingConv()) { |
| 4693 | Instruction *OldCall = CS.getInstruction(); |
| 4694 | // If the call and callee calling conventions don't match, this call must |
| 4695 | // be unreachable, as the call is undefined. |
| 4696 | new StoreInst(ConstantBool::True, |
| 4697 | UndefValue::get(PointerType::get(Type::BoolTy)), OldCall); |
| 4698 | if (!OldCall->use_empty()) |
| 4699 | OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); |
| 4700 | if (isa<CallInst>(OldCall)) // Not worth removing an invoke here. |
| 4701 | return EraseInstFromFunction(*OldCall); |
| 4702 | return 0; |
| 4703 | } |
| 4704 | |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 4705 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
| 4706 | // This instruction is not reachable, just remove it. We insert a store to |
| 4707 | // undef so that we know that this code is not reachable, despite the fact |
| 4708 | // that we can't modify the CFG here. |
| 4709 | new StoreInst(ConstantBool::True, |
| 4710 | UndefValue::get(PointerType::get(Type::BoolTy)), |
| 4711 | CS.getInstruction()); |
| 4712 | |
| 4713 | if (!CS.getInstruction()->use_empty()) |
| 4714 | CS.getInstruction()-> |
| 4715 | replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); |
| 4716 | |
| 4717 | if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { |
| 4718 | // Don't break the CFG, insert a dummy cond branch. |
| 4719 | new BranchInst(II->getNormalDest(), II->getUnwindDest(), |
| 4720 | ConstantBool::True, II); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4721 | } |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 4722 | return EraseInstFromFunction(*CS.getInstruction()); |
| 4723 | } |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4724 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 4725 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 4726 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 4727 | if (FTy->isVarArg()) { |
| 4728 | // See if we can optimize any arguments passed through the varargs area of |
| 4729 | // the call. |
| 4730 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
| 4731 | E = CS.arg_end(); I != E; ++I) |
| 4732 | if (CastInst *CI = dyn_cast<CastInst>(*I)) { |
| 4733 | // If this cast does not effect the value passed through the varargs |
| 4734 | // area, we can eliminate the use of the cast. |
| 4735 | Value *Op = CI->getOperand(0); |
| 4736 | if (CI->getType()->isLosslesslyConvertibleTo(Op->getType())) { |
| 4737 | *I = Op; |
| 4738 | Changed = true; |
| 4739 | } |
| 4740 | } |
| 4741 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4742 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 4743 | return Changed ? CS.getInstruction() : 0; |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 4744 | } |
| 4745 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4746 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 4747 | // attempt to move the cast to the arguments of the call/invoke. |
| 4748 | // |
| 4749 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 4750 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 4751 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
Chris Lattner | f3edc49 | 2004-07-18 18:59:44 +0000 | [diff] [blame] | 4752 | if (CE->getOpcode() != Instruction::Cast || !isa<Function>(CE->getOperand(0))) |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4753 | return false; |
Reid Spencer | 8743687 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 4754 | Function *Callee = cast<Function>(CE->getOperand(0)); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4755 | Instruction *Caller = CS.getInstruction(); |
| 4756 | |
| 4757 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 4758 | // would cause a type conversion of one of our arguments, change this call to |
| 4759 | // be a direct call with arguments casted to the appropriate types. |
| 4760 | // |
| 4761 | const FunctionType *FT = Callee->getFunctionType(); |
| 4762 | const Type *OldRetTy = Caller->getType(); |
| 4763 | |
Chris Lattner | 1f7942f | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 4764 | // Check to see if we are changing the return type... |
| 4765 | if (OldRetTy != FT->getReturnType()) { |
| 4766 | if (Callee->isExternal() && |
| 4767 | !OldRetTy->isLosslesslyConvertibleTo(FT->getReturnType()) && |
| 4768 | !Caller->use_empty()) |
| 4769 | return false; // Cannot transform this return value... |
| 4770 | |
| 4771 | // If the callsite is an invoke instruction, and the return value is used by |
| 4772 | // a PHI node in a successor, we cannot change the return type of the call |
| 4773 | // because there is no place to put the cast instruction (without breaking |
| 4774 | // the critical edge). Bail out in this case. |
| 4775 | if (!Caller->use_empty()) |
| 4776 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 4777 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 4778 | UI != E; ++UI) |
| 4779 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 4780 | if (PN->getParent() == II->getNormalDest() || |
Chris Lattner | fae8ab3 | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 4781 | PN->getParent() == II->getUnwindDest()) |
Chris Lattner | 1f7942f | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 4782 | return false; |
| 4783 | } |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4784 | |
| 4785 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 4786 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4787 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4788 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 4789 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 4790 | const Type *ParamTy = FT->getParamType(i); |
| 4791 | bool isConvertible = (*AI)->getType()->isLosslesslyConvertibleTo(ParamTy); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4792 | if (Callee->isExternal() && !isConvertible) return false; |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4793 | } |
| 4794 | |
| 4795 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
| 4796 | Callee->isExternal()) |
| 4797 | return false; // Do not delete arguments unless we have a function body... |
| 4798 | |
| 4799 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 4800 | // inserting cast instructions as necessary... |
| 4801 | std::vector<Value*> Args; |
| 4802 | Args.reserve(NumActualArgs); |
| 4803 | |
| 4804 | AI = CS.arg_begin(); |
| 4805 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 4806 | const Type *ParamTy = FT->getParamType(i); |
| 4807 | if ((*AI)->getType() == ParamTy) { |
| 4808 | Args.push_back(*AI); |
| 4809 | } else { |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 4810 | Args.push_back(InsertNewInstBefore(new CastInst(*AI, ParamTy, "tmp"), |
| 4811 | *Caller)); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4812 | } |
| 4813 | } |
| 4814 | |
| 4815 | // If the function takes more arguments than the call was taking, add them |
| 4816 | // now... |
| 4817 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 4818 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 4819 | |
| 4820 | // If we are removing arguments to the function, emit an obnoxious warning... |
| 4821 | if (FT->getNumParams() < NumActualArgs) |
| 4822 | if (!FT->isVarArg()) { |
| 4823 | std::cerr << "WARNING: While resolving call to function '" |
| 4824 | << Callee->getName() << "' arguments were dropped!\n"; |
| 4825 | } else { |
| 4826 | // Add all of the arguments in their promoted form to the arg list... |
| 4827 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 4828 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 4829 | if (PTy != (*AI)->getType()) { |
| 4830 | // Must promote to pass through va_arg area! |
| 4831 | Instruction *Cast = new CastInst(*AI, PTy, "tmp"); |
| 4832 | InsertNewInstBefore(Cast, *Caller); |
| 4833 | Args.push_back(Cast); |
| 4834 | } else { |
| 4835 | Args.push_back(*AI); |
| 4836 | } |
| 4837 | } |
| 4838 | } |
| 4839 | |
| 4840 | if (FT->getReturnType() == Type::VoidTy) |
| 4841 | Caller->setName(""); // Void type should not have a name... |
| 4842 | |
| 4843 | Instruction *NC; |
| 4844 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Chris Lattner | fae8ab3 | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 4845 | NC = new InvokeInst(Callee, II->getNormalDest(), II->getUnwindDest(), |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4846 | Args, Caller->getName(), Caller); |
Chris Lattner | 05c703e | 2005-05-14 12:25:32 +0000 | [diff] [blame] | 4847 | cast<InvokeInst>(II)->setCallingConv(II->getCallingConv()); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4848 | } else { |
| 4849 | NC = new CallInst(Callee, Args, Caller->getName(), Caller); |
Chris Lattner | 6aacb0f | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 4850 | if (cast<CallInst>(Caller)->isTailCall()) |
| 4851 | cast<CallInst>(NC)->setTailCall(); |
Chris Lattner | 05c703e | 2005-05-14 12:25:32 +0000 | [diff] [blame] | 4852 | cast<CallInst>(NC)->setCallingConv(cast<CallInst>(Caller)->getCallingConv()); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4853 | } |
| 4854 | |
| 4855 | // Insert a cast of the return type as necessary... |
| 4856 | Value *NV = NC; |
| 4857 | if (Caller->getType() != NV->getType() && !Caller->use_empty()) { |
| 4858 | if (NV->getType() != Type::VoidTy) { |
| 4859 | NV = NC = new CastInst(NC, Caller->getType(), "tmp"); |
Chris Lattner | 686767f | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 4860 | |
| 4861 | // If this is an invoke instruction, we should insert it after the first |
| 4862 | // non-phi, instruction in the normal successor block. |
| 4863 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 4864 | BasicBlock::iterator I = II->getNormalDest()->begin(); |
| 4865 | while (isa<PHINode>(I)) ++I; |
| 4866 | InsertNewInstBefore(NC, *I); |
| 4867 | } else { |
| 4868 | // Otherwise, it's a call, just insert cast right after the call instr |
| 4869 | InsertNewInstBefore(NC, *Caller); |
| 4870 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 4871 | AddUsersToWorkList(*Caller); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4872 | } else { |
Chris Lattner | e29d634 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 4873 | NV = UndefValue::get(Caller->getType()); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4874 | } |
| 4875 | } |
| 4876 | |
| 4877 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 4878 | Caller->replaceAllUsesWith(NV); |
| 4879 | Caller->getParent()->getInstList().erase(Caller); |
| 4880 | removeFromWorkList(Caller); |
| 4881 | return true; |
| 4882 | } |
| 4883 | |
| 4884 | |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4885 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 4886 | // operator and they all are only used by the PHI, PHI together their |
| 4887 | // inputs, and do the operation once, to the result of the PHI. |
| 4888 | Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { |
| 4889 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
| 4890 | |
| 4891 | // Scan the instruction, looking for input operations that can be folded away. |
| 4892 | // If all input operands to the phi are the same instruction (e.g. a cast from |
| 4893 | // the same type or "+42") we can pull the operation through the PHI, reducing |
| 4894 | // code size and simplifying code. |
| 4895 | Constant *ConstantOp = 0; |
| 4896 | const Type *CastSrcTy = 0; |
| 4897 | if (isa<CastInst>(FirstInst)) { |
| 4898 | CastSrcTy = FirstInst->getOperand(0)->getType(); |
| 4899 | } else if (isa<BinaryOperator>(FirstInst) || isa<ShiftInst>(FirstInst)) { |
| 4900 | // Can fold binop or shift if the RHS is a constant. |
| 4901 | ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1)); |
| 4902 | if (ConstantOp == 0) return 0; |
| 4903 | } else { |
| 4904 | return 0; // Cannot fold this operation. |
| 4905 | } |
| 4906 | |
| 4907 | // Check to see if all arguments are the same operation. |
| 4908 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 4909 | if (!isa<Instruction>(PN.getIncomingValue(i))) return 0; |
| 4910 | Instruction *I = cast<Instruction>(PN.getIncomingValue(i)); |
| 4911 | if (!I->hasOneUse() || I->getOpcode() != FirstInst->getOpcode()) |
| 4912 | return 0; |
| 4913 | if (CastSrcTy) { |
| 4914 | if (I->getOperand(0)->getType() != CastSrcTy) |
| 4915 | return 0; // Cast operation must match. |
| 4916 | } else if (I->getOperand(1) != ConstantOp) { |
| 4917 | return 0; |
| 4918 | } |
| 4919 | } |
| 4920 | |
| 4921 | // Okay, they are all the same operation. Create a new PHI node of the |
| 4922 | // correct type, and PHI together all of the LHS's of the instructions. |
| 4923 | PHINode *NewPN = new PHINode(FirstInst->getOperand(0)->getType(), |
| 4924 | PN.getName()+".in"); |
Chris Lattner | d8e2018 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 4925 | NewPN->reserveOperandSpace(PN.getNumOperands()/2); |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 4926 | |
| 4927 | Value *InVal = FirstInst->getOperand(0); |
| 4928 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4929 | |
| 4930 | // Add all operands to the new PHI. |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 4931 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 4932 | Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 4933 | if (NewInVal != InVal) |
| 4934 | InVal = 0; |
| 4935 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); |
| 4936 | } |
| 4937 | |
| 4938 | Value *PhiVal; |
| 4939 | if (InVal) { |
| 4940 | // The new PHI unions all of the same values together. This is really |
| 4941 | // common, so we handle it intelligently here for compile-time speed. |
| 4942 | PhiVal = InVal; |
| 4943 | delete NewPN; |
| 4944 | } else { |
| 4945 | InsertNewInstBefore(NewPN, PN); |
| 4946 | PhiVal = NewPN; |
| 4947 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4948 | |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4949 | // Insert and return the new operation. |
| 4950 | if (isa<CastInst>(FirstInst)) |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 4951 | return new CastInst(PhiVal, PN.getType()); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4952 | else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 4953 | return BinaryOperator::create(BinOp->getOpcode(), PhiVal, ConstantOp); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4954 | else |
| 4955 | return new ShiftInst(cast<ShiftInst>(FirstInst)->getOpcode(), |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 4956 | PhiVal, ConstantOp); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4957 | } |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4958 | |
Chris Lattner | 7153643 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 4959 | /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle |
| 4960 | /// that is dead. |
| 4961 | static bool DeadPHICycle(PHINode *PN, std::set<PHINode*> &PotentiallyDeadPHIs) { |
| 4962 | if (PN->use_empty()) return true; |
| 4963 | if (!PN->hasOneUse()) return false; |
| 4964 | |
| 4965 | // Remember this node, and if we find the cycle, return. |
| 4966 | if (!PotentiallyDeadPHIs.insert(PN).second) |
| 4967 | return true; |
| 4968 | |
| 4969 | if (PHINode *PU = dyn_cast<PHINode>(PN->use_back())) |
| 4970 | return DeadPHICycle(PU, PotentiallyDeadPHIs); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4971 | |
Chris Lattner | 7153643 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 4972 | return false; |
| 4973 | } |
| 4974 | |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 4975 | // PHINode simplification |
| 4976 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4977 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Chris Lattner | 9f9c260 | 2005-08-05 01:04:30 +0000 | [diff] [blame] | 4978 | if (Value *V = PN.hasConstantValue()) |
| 4979 | return ReplaceInstUsesWith(PN, V); |
Chris Lattner | 4db2d22 | 2004-02-16 05:07:08 +0000 | [diff] [blame] | 4980 | |
| 4981 | // If the only user of this instruction is a cast instruction, and all of the |
| 4982 | // incoming values are constants, change this PHI to merge together the casted |
| 4983 | // constants. |
| 4984 | if (PN.hasOneUse()) |
| 4985 | if (CastInst *CI = dyn_cast<CastInst>(PN.use_back())) |
| 4986 | if (CI->getType() != PN.getType()) { // noop casts will be folded |
| 4987 | bool AllConstant = true; |
| 4988 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 4989 | if (!isa<Constant>(PN.getIncomingValue(i))) { |
| 4990 | AllConstant = false; |
| 4991 | break; |
| 4992 | } |
| 4993 | if (AllConstant) { |
| 4994 | // Make a new PHI with all casted values. |
| 4995 | PHINode *New = new PHINode(CI->getType(), PN.getName(), &PN); |
| 4996 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 4997 | Constant *OldArg = cast<Constant>(PN.getIncomingValue(i)); |
| 4998 | New->addIncoming(ConstantExpr::getCast(OldArg, New->getType()), |
| 4999 | PN.getIncomingBlock(i)); |
| 5000 | } |
| 5001 | |
| 5002 | // Update the cast instruction. |
| 5003 | CI->setOperand(0, New); |
| 5004 | WorkList.push_back(CI); // revisit the cast instruction to fold. |
| 5005 | WorkList.push_back(New); // Make sure to revisit the new Phi |
| 5006 | return &PN; // PN is now dead! |
| 5007 | } |
| 5008 | } |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 5009 | |
| 5010 | // If all PHI operands are the same operation, pull them through the PHI, |
| 5011 | // reducing code size. |
| 5012 | if (isa<Instruction>(PN.getIncomingValue(0)) && |
| 5013 | PN.getIncomingValue(0)->hasOneUse()) |
| 5014 | if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) |
| 5015 | return Result; |
| 5016 | |
Chris Lattner | 7153643 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 5017 | // If this is a trivial cycle in the PHI node graph, remove it. Basically, if |
| 5018 | // this PHI only has a single use (a PHI), and if that PHI only has one use (a |
| 5019 | // PHI)... break the cycle. |
| 5020 | if (PN.hasOneUse()) |
| 5021 | if (PHINode *PU = dyn_cast<PHINode>(PN.use_back())) { |
| 5022 | std::set<PHINode*> PotentiallyDeadPHIs; |
| 5023 | PotentiallyDeadPHIs.insert(&PN); |
| 5024 | if (DeadPHICycle(PU, PotentiallyDeadPHIs)) |
| 5025 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 5026 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5027 | |
Chris Lattner | 91daeb5 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 5028 | return 0; |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 5029 | } |
| 5030 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 5031 | static Value *InsertSignExtendToPtrTy(Value *V, const Type *DTy, |
| 5032 | Instruction *InsertPoint, |
| 5033 | InstCombiner *IC) { |
| 5034 | unsigned PS = IC->getTargetData().getPointerSize(); |
| 5035 | const Type *VTy = V->getType(); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 5036 | if (!VTy->isSigned() && VTy->getPrimitiveSize() < PS) |
| 5037 | // We must insert a cast to ensure we sign-extend. |
| 5038 | V = IC->InsertNewInstBefore(new CastInst(V, VTy->getSignedVersion(), |
| 5039 | V->getName()), *InsertPoint); |
| 5040 | return IC->InsertNewInstBefore(new CastInst(V, DTy, V->getName()), |
| 5041 | *InsertPoint); |
| 5042 | } |
| 5043 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 5044 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5045 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 5046 | Value *PtrOp = GEP.getOperand(0); |
Chris Lattner | 471bd76 | 2003-05-22 19:07:21 +0000 | [diff] [blame] | 5047 | // Is it 'getelementptr %P, long 0' or 'getelementptr %P' |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5048 | // If so, eliminate the noop. |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 5049 | if (GEP.getNumOperands() == 1) |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 5050 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 5051 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5052 | if (isa<UndefValue>(GEP.getOperand(0))) |
| 5053 | return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType())); |
| 5054 | |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 5055 | bool HasZeroPointerIndex = false; |
| 5056 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) |
| 5057 | HasZeroPointerIndex = C->isNullValue(); |
| 5058 | |
| 5059 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 5060 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 5061 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 5062 | // Eliminate unneeded casts for indices. |
| 5063 | bool MadeChange = false; |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 5064 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 5065 | for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI) |
| 5066 | if (isa<SequentialType>(*GTI)) { |
| 5067 | if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) { |
| 5068 | Value *Src = CI->getOperand(0); |
| 5069 | const Type *SrcTy = Src->getType(); |
| 5070 | const Type *DestTy = CI->getType(); |
| 5071 | if (Src->getType()->isInteger()) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5072 | if (SrcTy->getPrimitiveSizeInBits() == |
| 5073 | DestTy->getPrimitiveSizeInBits()) { |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 5074 | // We can always eliminate a cast from ulong or long to the other. |
| 5075 | // We can always eliminate a cast from uint to int or the other on |
| 5076 | // 32-bit pointer platforms. |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5077 | if (DestTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()){ |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 5078 | MadeChange = true; |
| 5079 | GEP.setOperand(i, Src); |
| 5080 | } |
| 5081 | } else if (SrcTy->getPrimitiveSize() < DestTy->getPrimitiveSize() && |
| 5082 | SrcTy->getPrimitiveSize() == 4) { |
| 5083 | // We can always eliminate a cast from int to [u]long. We can |
| 5084 | // eliminate a cast from uint to [u]long iff the target is a 32-bit |
| 5085 | // pointer target. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5086 | if (SrcTy->isSigned() || |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5087 | SrcTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()) { |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 5088 | MadeChange = true; |
| 5089 | GEP.setOperand(i, Src); |
| 5090 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 5091 | } |
| 5092 | } |
| 5093 | } |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 5094 | // If we are using a wider index than needed for this platform, shrink it |
| 5095 | // to what we need. If the incoming value needs a cast instruction, |
| 5096 | // insert it. This explicit cast can make subsequent optimizations more |
| 5097 | // obvious. |
| 5098 | Value *Op = GEP.getOperand(i); |
| 5099 | if (Op->getType()->getPrimitiveSize() > TD->getPointerSize()) |
Chris Lattner | 1e9ac1a | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 5100 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | 44d0b95 | 2004-07-20 01:48:15 +0000 | [diff] [blame] | 5101 | GEP.setOperand(i, ConstantExpr::getCast(C, |
| 5102 | TD->getIntPtrType()->getSignedVersion())); |
Chris Lattner | 1e9ac1a | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 5103 | MadeChange = true; |
| 5104 | } else { |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 5105 | Op = InsertNewInstBefore(new CastInst(Op, TD->getIntPtrType(), |
| 5106 | Op->getName()), GEP); |
| 5107 | GEP.setOperand(i, Op); |
| 5108 | MadeChange = true; |
| 5109 | } |
Chris Lattner | 44d0b95 | 2004-07-20 01:48:15 +0000 | [diff] [blame] | 5110 | |
| 5111 | // If this is a constant idx, make sure to canonicalize it to be a signed |
| 5112 | // operand, otherwise CSE and other optimizations are pessimized. |
| 5113 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op)) { |
| 5114 | GEP.setOperand(i, ConstantExpr::getCast(CUI, |
| 5115 | CUI->getType()->getSignedVersion())); |
| 5116 | MadeChange = true; |
| 5117 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 5118 | } |
| 5119 | if (MadeChange) return &GEP; |
| 5120 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 5121 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 5122 | // is a getelementptr instruction, combine the indices of the two |
| 5123 | // getelementptr instructions into a single instruction. |
| 5124 | // |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 5125 | std::vector<Value*> SrcGEPOperands; |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5126 | if (User *Src = dyn_castGetElementPtr(PtrOp)) |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 5127 | SrcGEPOperands.assign(Src->op_begin(), Src->op_end()); |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 5128 | |
| 5129 | if (!SrcGEPOperands.empty()) { |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 5130 | // Note that if our source is a gep chain itself that we wait for that |
| 5131 | // chain to be resolved before we perform this transformation. This |
| 5132 | // avoids us creating a TON of code in some cases. |
| 5133 | // |
| 5134 | if (isa<GetElementPtrInst>(SrcGEPOperands[0]) && |
| 5135 | cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2) |
| 5136 | return 0; // Wait until our source is folded to completion. |
| 5137 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 5138 | std::vector<Value *> Indices; |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 5139 | |
| 5140 | // Find out whether the last index in the source GEP is a sequential idx. |
| 5141 | bool EndsWithSequential = false; |
| 5142 | for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)), |
| 5143 | E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I) |
Chris Lattner | 8ec5f88 | 2004-05-08 22:41:42 +0000 | [diff] [blame] | 5144 | EndsWithSequential = !isa<StructType>(*I); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5145 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 5146 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 5147 | if (EndsWithSequential) { |
Chris Lattner | 235af56 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 5148 | // Replace: gep (gep %P, long B), long A, ... |
| 5149 | // With: T = long A+B; gep %P, T, ... |
| 5150 | // |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 5151 | Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 5152 | if (SO1 == Constant::getNullValue(SO1->getType())) { |
| 5153 | Sum = GO1; |
| 5154 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { |
| 5155 | Sum = SO1; |
| 5156 | } else { |
| 5157 | // If they aren't the same type, convert both to an integer of the |
| 5158 | // target's pointer size. |
| 5159 | if (SO1->getType() != GO1->getType()) { |
| 5160 | if (Constant *SO1C = dyn_cast<Constant>(SO1)) { |
| 5161 | SO1 = ConstantExpr::getCast(SO1C, GO1->getType()); |
| 5162 | } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) { |
| 5163 | GO1 = ConstantExpr::getCast(GO1C, SO1->getType()); |
| 5164 | } else { |
| 5165 | unsigned PS = TD->getPointerSize(); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 5166 | if (SO1->getType()->getPrimitiveSize() == PS) { |
| 5167 | // Convert GO1 to SO1's type. |
| 5168 | GO1 = InsertSignExtendToPtrTy(GO1, SO1->getType(), &GEP, this); |
| 5169 | |
| 5170 | } else if (GO1->getType()->getPrimitiveSize() == PS) { |
| 5171 | // Convert SO1 to GO1's type. |
| 5172 | SO1 = InsertSignExtendToPtrTy(SO1, GO1->getType(), &GEP, this); |
| 5173 | } else { |
| 5174 | const Type *PT = TD->getIntPtrType(); |
| 5175 | SO1 = InsertSignExtendToPtrTy(SO1, PT, &GEP, this); |
| 5176 | GO1 = InsertSignExtendToPtrTy(GO1, PT, &GEP, this); |
| 5177 | } |
| 5178 | } |
| 5179 | } |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 5180 | if (isa<Constant>(SO1) && isa<Constant>(GO1)) |
| 5181 | Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1)); |
| 5182 | else { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 5183 | Sum = BinaryOperator::createAdd(SO1, GO1, PtrOp->getName()+".sum"); |
| 5184 | InsertNewInstBefore(cast<Instruction>(Sum), GEP); |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 5185 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 5186 | } |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 5187 | |
| 5188 | // Recycle the GEP we already have if possible. |
| 5189 | if (SrcGEPOperands.size() == 2) { |
| 5190 | GEP.setOperand(0, SrcGEPOperands[0]); |
| 5191 | GEP.setOperand(1, Sum); |
| 5192 | return &GEP; |
| 5193 | } else { |
| 5194 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 5195 | SrcGEPOperands.end()-1); |
| 5196 | Indices.push_back(Sum); |
| 5197 | Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end()); |
| 5198 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5199 | } else if (isa<Constant>(*GEP.idx_begin()) && |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 5200 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5201 | SrcGEPOperands.size() != 1) { |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 5202 | // 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] | 5203 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 5204 | SrcGEPOperands.end()); |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 5205 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
| 5206 | } |
| 5207 | |
| 5208 | if (!Indices.empty()) |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 5209 | return new GetElementPtrInst(SrcGEPOperands[0], Indices, GEP.getName()); |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 5210 | |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 5211 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) { |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 5212 | // GEP of global variable. If all of the indices for this GEP are |
| 5213 | // constants, we can promote this to a constexpr instead of an instruction. |
| 5214 | |
| 5215 | // Scan for nonconstants... |
| 5216 | std::vector<Constant*> Indices; |
| 5217 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
| 5218 | for (; I != E && isa<Constant>(*I); ++I) |
| 5219 | Indices.push_back(cast<Constant>(*I)); |
| 5220 | |
| 5221 | if (I == E) { // If they are all constants... |
Chris Lattner | f3edc49 | 2004-07-18 18:59:44 +0000 | [diff] [blame] | 5222 | Constant *CE = ConstantExpr::getGetElementPtr(GV, Indices); |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 5223 | |
| 5224 | // Replace all uses of the GEP with the new constexpr... |
| 5225 | return ReplaceInstUsesWith(GEP, CE); |
| 5226 | } |
Chris Lattner | 567b81f | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 5227 | } else if (Value *X = isCast(PtrOp)) { // Is the operand a cast? |
| 5228 | if (!isa<PointerType>(X->getType())) { |
| 5229 | // Not interesting. Source pointer must be a cast from pointer. |
| 5230 | } else if (HasZeroPointerIndex) { |
| 5231 | // transform: GEP (cast [10 x ubyte]* X to [0 x ubyte]*), long 0, ... |
| 5232 | // into : GEP [10 x ubyte]* X, long 0, ... |
| 5233 | // |
| 5234 | // This occurs when the program declares an array extern like "int X[];" |
| 5235 | // |
| 5236 | const PointerType *CPTy = cast<PointerType>(PtrOp->getType()); |
| 5237 | const PointerType *XTy = cast<PointerType>(X->getType()); |
| 5238 | if (const ArrayType *XATy = |
| 5239 | dyn_cast<ArrayType>(XTy->getElementType())) |
| 5240 | if (const ArrayType *CATy = |
| 5241 | dyn_cast<ArrayType>(CPTy->getElementType())) |
| 5242 | if (CATy->getElementType() == XATy->getElementType()) { |
| 5243 | // At this point, we know that the cast source type is a pointer |
| 5244 | // to an array of the same type as the destination pointer |
| 5245 | // array. Because the array type is never stepped over (there |
| 5246 | // is a leading zero) we can fold the cast into this GEP. |
| 5247 | GEP.setOperand(0, X); |
| 5248 | return &GEP; |
| 5249 | } |
| 5250 | } else if (GEP.getNumOperands() == 2) { |
| 5251 | // Transform things like: |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 5252 | // %t = getelementptr ubyte* cast ([2 x int]* %str to uint*), uint %V |
| 5253 | // into: %t1 = getelementptr [2 x int*]* %str, int 0, uint %V; cast |
Chris Lattner | 567b81f | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 5254 | const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); |
| 5255 | const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType(); |
| 5256 | if (isa<ArrayType>(SrcElTy) && |
| 5257 | TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()) == |
| 5258 | TD->getTypeSize(ResElTy)) { |
| 5259 | Value *V = InsertNewInstBefore( |
| 5260 | new GetElementPtrInst(X, Constant::getNullValue(Type::IntTy), |
| 5261 | GEP.getOperand(1), GEP.getName()), GEP); |
| 5262 | return new CastInst(V, GEP.getType()); |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 5263 | } |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 5264 | |
| 5265 | // Transform things like: |
| 5266 | // getelementptr sbyte* cast ([100 x double]* X to sbyte*), int %tmp |
| 5267 | // (where tmp = 8*tmp2) into: |
| 5268 | // getelementptr [100 x double]* %arr, int 0, int %tmp.2 |
| 5269 | |
| 5270 | if (isa<ArrayType>(SrcElTy) && |
| 5271 | (ResElTy == Type::SByteTy || ResElTy == Type::UByteTy)) { |
| 5272 | uint64_t ArrayEltSize = |
| 5273 | TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()); |
| 5274 | |
| 5275 | // Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We |
| 5276 | // allow either a mul, shift, or constant here. |
| 5277 | Value *NewIdx = 0; |
| 5278 | ConstantInt *Scale = 0; |
| 5279 | if (ArrayEltSize == 1) { |
| 5280 | NewIdx = GEP.getOperand(1); |
| 5281 | Scale = ConstantInt::get(NewIdx->getType(), 1); |
| 5282 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) { |
Chris Lattner | a393e4d | 2005-09-14 17:32:56 +0000 | [diff] [blame] | 5283 | NewIdx = ConstantInt::get(CI->getType(), 1); |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 5284 | Scale = CI; |
| 5285 | } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){ |
| 5286 | if (Inst->getOpcode() == Instruction::Shl && |
| 5287 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 5288 | unsigned ShAmt =cast<ConstantUInt>(Inst->getOperand(1))->getValue(); |
| 5289 | if (Inst->getType()->isSigned()) |
| 5290 | Scale = ConstantSInt::get(Inst->getType(), 1ULL << ShAmt); |
| 5291 | else |
| 5292 | Scale = ConstantUInt::get(Inst->getType(), 1ULL << ShAmt); |
| 5293 | NewIdx = Inst->getOperand(0); |
| 5294 | } else if (Inst->getOpcode() == Instruction::Mul && |
| 5295 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 5296 | Scale = cast<ConstantInt>(Inst->getOperand(1)); |
| 5297 | NewIdx = Inst->getOperand(0); |
| 5298 | } |
| 5299 | } |
| 5300 | |
| 5301 | // If the index will be to exactly the right offset with the scale taken |
| 5302 | // out, perform the transformation. |
| 5303 | if (Scale && Scale->getRawValue() % ArrayEltSize == 0) { |
| 5304 | if (ConstantSInt *C = dyn_cast<ConstantSInt>(Scale)) |
| 5305 | Scale = ConstantSInt::get(C->getType(), |
Chris Lattner | a393e4d | 2005-09-14 17:32:56 +0000 | [diff] [blame] | 5306 | (int64_t)C->getRawValue() / |
| 5307 | (int64_t)ArrayEltSize); |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 5308 | else |
| 5309 | Scale = ConstantUInt::get(Scale->getType(), |
| 5310 | Scale->getRawValue() / ArrayEltSize); |
| 5311 | if (Scale->getRawValue() != 1) { |
| 5312 | Constant *C = ConstantExpr::getCast(Scale, NewIdx->getType()); |
| 5313 | Instruction *Sc = BinaryOperator::createMul(NewIdx, C, "idxscale"); |
| 5314 | NewIdx = InsertNewInstBefore(Sc, GEP); |
| 5315 | } |
| 5316 | |
| 5317 | // Insert the new GEP instruction. |
| 5318 | Instruction *Idx = |
| 5319 | new GetElementPtrInst(X, Constant::getNullValue(Type::IntTy), |
| 5320 | NewIdx, GEP.getName()); |
| 5321 | Idx = InsertNewInstBefore(Idx, GEP); |
| 5322 | return new CastInst(Idx, GEP.getType()); |
| 5323 | } |
| 5324 | } |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 5325 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 5326 | } |
| 5327 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 5328 | return 0; |
| 5329 | } |
| 5330 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 5331 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
| 5332 | // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1 |
| 5333 | if (AI.isArrayAllocation()) // Check C != 1 |
| 5334 | if (const ConstantUInt *C = dyn_cast<ConstantUInt>(AI.getArraySize())) { |
| 5335 | const Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getValue()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 5336 | AllocationInst *New = 0; |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 5337 | |
| 5338 | // Create and insert the replacement instruction... |
| 5339 | if (isa<MallocInst>(AI)) |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 5340 | New = new MallocInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 5341 | else { |
| 5342 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 5343 | New = new AllocaInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 5344 | } |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 5345 | |
| 5346 | InsertNewInstBefore(New, AI); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5347 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 5348 | // Scan to the end of the allocation instructions, to skip over a block of |
| 5349 | // allocas if possible... |
| 5350 | // |
| 5351 | BasicBlock::iterator It = New; |
| 5352 | while (isa<AllocationInst>(*It)) ++It; |
| 5353 | |
| 5354 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 5355 | // insert our getelementptr instruction... |
| 5356 | // |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 5357 | Value *NullIdx = Constant::getNullValue(Type::IntTy); |
| 5358 | Value *V = new GetElementPtrInst(New, NullIdx, NullIdx, |
| 5359 | New->getName()+".sub", It); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 5360 | |
| 5361 | // Now make everything use the getelementptr instead of the original |
| 5362 | // allocation. |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 5363 | return ReplaceInstUsesWith(AI, V); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5364 | } else if (isa<UndefValue>(AI.getArraySize())) { |
| 5365 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 5366 | } |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 5367 | |
| 5368 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. |
| 5369 | // Note that we only do this for alloca's, because malloc should allocate and |
| 5370 | // return a unique pointer, even for a zero byte allocation. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5371 | if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() && |
Chris Lattner | 49df6ce | 2004-07-02 22:55:47 +0000 | [diff] [blame] | 5372 | TD->getTypeSize(AI.getAllocatedType()) == 0) |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 5373 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
| 5374 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 5375 | return 0; |
| 5376 | } |
| 5377 | |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 5378 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { |
| 5379 | Value *Op = FI.getOperand(0); |
| 5380 | |
| 5381 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X |
| 5382 | if (CastInst *CI = dyn_cast<CastInst>(Op)) |
| 5383 | if (isa<PointerType>(CI->getOperand(0)->getType())) { |
| 5384 | FI.setOperand(0, CI->getOperand(0)); |
| 5385 | return &FI; |
| 5386 | } |
| 5387 | |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 5388 | // free undef -> unreachable. |
| 5389 | if (isa<UndefValue>(Op)) { |
| 5390 | // Insert a new store to null because we cannot modify the CFG here. |
| 5391 | new StoreInst(ConstantBool::True, |
| 5392 | UndefValue::get(PointerType::get(Type::BoolTy)), &FI); |
| 5393 | return EraseInstFromFunction(FI); |
| 5394 | } |
| 5395 | |
Chris Lattner | f3a3660 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 5396 | // If we have 'free null' delete the instruction. This can happen in stl code |
| 5397 | // when lots of inlining happens. |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 5398 | if (isa<ConstantPointerNull>(Op)) |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 5399 | return EraseInstFromFunction(FI); |
Chris Lattner | f3a3660 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 5400 | |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 5401 | return 0; |
| 5402 | } |
| 5403 | |
| 5404 | |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 5405 | /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible. |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 5406 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI) { |
| 5407 | User *CI = cast<User>(LI.getOperand(0)); |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 5408 | Value *CastOp = CI->getOperand(0); |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 5409 | |
| 5410 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 5411 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 5412 | const Type *SrcPTy = SrcTy->getElementType(); |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 5413 | |
| 5414 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) { |
| 5415 | // If the source is an array, the code below will not succeed. Check to |
| 5416 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 5417 | // constants. |
| 5418 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 5419 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 5420 | if (ASrcTy->getNumElements() != 0) { |
| 5421 | std::vector<Value*> Idxs(2, Constant::getNullValue(Type::IntTy)); |
| 5422 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs); |
| 5423 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 5424 | SrcPTy = SrcTy->getElementType(); |
| 5425 | } |
| 5426 | |
| 5427 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
Chris Lattner | ecfa9b5 | 2005-03-29 06:37:47 +0000 | [diff] [blame] | 5428 | // Do not allow turning this into a load of an integer, which is then |
| 5429 | // casted to a pointer, this pessimizes pointer analysis a lot. |
| 5430 | (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5431 | IC.getTargetData().getTypeSize(SrcPTy) == |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 5432 | IC.getTargetData().getTypeSize(DestPTy)) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5433 | |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 5434 | // Okay, we are casting from one integer or pointer type to another of |
| 5435 | // the same size. Instead of casting the pointer before the load, cast |
| 5436 | // the result of the loaded value. |
| 5437 | Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp, |
| 5438 | CI->getName(), |
| 5439 | LI.isVolatile()),LI); |
| 5440 | // Now cast the result of the load. |
| 5441 | return new CastInst(NewLoad, LI.getType()); |
| 5442 | } |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 5443 | } |
| 5444 | } |
| 5445 | return 0; |
| 5446 | } |
| 5447 | |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5448 | /// isSafeToLoadUnconditionally - Return true if we know that executing a load |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 5449 | /// from this value cannot trap. If it is not obviously safe to load from the |
| 5450 | /// specified pointer, we do a quick local scan of the basic block containing |
| 5451 | /// ScanFrom, to determine if the address is already accessed. |
| 5452 | static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) { |
| 5453 | // If it is an alloca or global variable, it is always safe to load from. |
| 5454 | if (isa<AllocaInst>(V) || isa<GlobalVariable>(V)) return true; |
| 5455 | |
| 5456 | // Otherwise, be a little bit agressive by scanning the local block where we |
| 5457 | // 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] | 5458 | // from/to. If so, the previous load or store would have already trapped, |
| 5459 | // so there is no harm doing an extra load (also, CSE will later eliminate |
| 5460 | // the load entirely). |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 5461 | BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); |
| 5462 | |
Alkis Evlogimenos | d59cebf | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 5463 | while (BBI != E) { |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 5464 | --BBI; |
| 5465 | |
| 5466 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 5467 | if (LI->getOperand(0) == V) return true; |
| 5468 | } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 5469 | if (SI->getOperand(1) == V) return true; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5470 | |
Alkis Evlogimenos | d59cebf | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 5471 | } |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 5472 | return false; |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5473 | } |
| 5474 | |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 5475 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 5476 | Value *Op = LI.getOperand(0); |
Chris Lattner | 7e8af38 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 5477 | |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 5478 | // load (cast X) --> cast (load X) iff safe |
| 5479 | if (CastInst *CI = dyn_cast<CastInst>(Op)) |
| 5480 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 5481 | return Res; |
| 5482 | |
| 5483 | // None of the following transforms are legal for volatile loads. |
| 5484 | if (LI.isVolatile()) return 0; |
Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 5485 | |
Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 5486 | if (&LI.getParent()->front() != &LI) { |
| 5487 | BasicBlock::iterator BBI = &LI; --BBI; |
Chris Lattner | e0bfdf1 | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 5488 | // If the instruction immediately before this is a store to the same |
| 5489 | // address, do a simple form of store->load forwarding. |
Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 5490 | if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 5491 | if (SI->getOperand(1) == LI.getOperand(0)) |
| 5492 | return ReplaceInstUsesWith(LI, SI->getOperand(0)); |
Chris Lattner | e0bfdf1 | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 5493 | if (LoadInst *LIB = dyn_cast<LoadInst>(BBI)) |
| 5494 | if (LIB->getOperand(0) == LI.getOperand(0)) |
| 5495 | return ReplaceInstUsesWith(LI, LIB); |
Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 5496 | } |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 5497 | |
| 5498 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) |
| 5499 | if (isa<ConstantPointerNull>(GEPI->getOperand(0)) || |
| 5500 | isa<UndefValue>(GEPI->getOperand(0))) { |
| 5501 | // Insert a new store to null instruction before the load to indicate |
| 5502 | // that this code is not reachable. We do this instead of inserting |
| 5503 | // an unreachable instruction directly because we cannot modify the |
| 5504 | // CFG. |
| 5505 | new StoreInst(UndefValue::get(LI.getType()), |
| 5506 | Constant::getNullValue(Op->getType()), &LI); |
| 5507 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 5508 | } |
| 5509 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5510 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 5511 | // load null/undef -> undef |
| 5512 | if ((C->isNullValue() || isa<UndefValue>(C))) { |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 5513 | // Insert a new store to null instruction before the load to indicate that |
| 5514 | // this code is not reachable. We do this instead of inserting an |
| 5515 | // unreachable instruction directly because we cannot modify the CFG. |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 5516 | new StoreInst(UndefValue::get(LI.getType()), |
| 5517 | Constant::getNullValue(Op->getType()), &LI); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5518 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 5519 | } |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 5520 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5521 | // Instcombine load (constant global) into the value loaded. |
| 5522 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) |
| 5523 | if (GV->isConstant() && !GV->isExternal()) |
| 5524 | return ReplaceInstUsesWith(LI, GV->getInitializer()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5525 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5526 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded. |
| 5527 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) |
| 5528 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 5529 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
| 5530 | if (GV->isConstant() && !GV->isExternal()) |
Chris Lattner | 0b011ec | 2005-09-26 05:28:06 +0000 | [diff] [blame] | 5531 | if (Constant *V = |
| 5532 | ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5533 | return ReplaceInstUsesWith(LI, V); |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 5534 | if (CE->getOperand(0)->isNullValue()) { |
| 5535 | // Insert a new store to null instruction before the load to indicate |
| 5536 | // that this code is not reachable. We do this instead of inserting |
| 5537 | // an unreachable instruction directly because we cannot modify the |
| 5538 | // CFG. |
| 5539 | new StoreInst(UndefValue::get(LI.getType()), |
| 5540 | Constant::getNullValue(Op->getType()), &LI); |
| 5541 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 5542 | } |
| 5543 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5544 | } else if (CE->getOpcode() == Instruction::Cast) { |
| 5545 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 5546 | return Res; |
| 5547 | } |
| 5548 | } |
Chris Lattner | e228ee5 | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 5549 | |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 5550 | if (Op->hasOneUse()) { |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5551 | // Change select and PHI nodes to select values instead of addresses: this |
| 5552 | // helps alias analysis out a lot, allows many others simplifications, and |
| 5553 | // exposes redundancy in the code. |
| 5554 | // |
| 5555 | // Note that we cannot do the transformation unless we know that the |
| 5556 | // introduced loads cannot trap! Something like this is valid as long as |
| 5557 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 5558 | // but it would not be valid if we transformed it to load from null |
| 5559 | // unconditionally. |
| 5560 | // |
| 5561 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 5562 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 5563 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && |
| 5564 | isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5565 | Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1), |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 5566 | SI->getOperand(1)->getName()+".val"), LI); |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5567 | Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2), |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 5568 | SI->getOperand(2)->getName()+".val"), LI); |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5569 | return new SelectInst(SI->getCondition(), V1, V2); |
| 5570 | } |
| 5571 | |
Chris Lattner | bdcf41a | 2004-09-23 15:46:00 +0000 | [diff] [blame] | 5572 | // load (select (cond, null, P)) -> load P |
| 5573 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) |
| 5574 | if (C->isNullValue()) { |
| 5575 | LI.setOperand(0, SI->getOperand(2)); |
| 5576 | return &LI; |
| 5577 | } |
| 5578 | |
| 5579 | // load (select (cond, P, null)) -> load P |
| 5580 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) |
| 5581 | if (C->isNullValue()) { |
| 5582 | LI.setOperand(0, SI->getOperand(1)); |
| 5583 | return &LI; |
| 5584 | } |
| 5585 | |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5586 | } else if (PHINode *PN = dyn_cast<PHINode>(Op)) { |
| 5587 | // load (phi (&V1, &V2, &V3)) --> phi(load &V1, load &V2, load &V3) |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 5588 | bool Safe = PN->getParent() == LI.getParent(); |
| 5589 | |
| 5590 | // Scan all of the instructions between the PHI and the load to make |
| 5591 | // sure there are no instructions that might possibly alter the value |
| 5592 | // loaded from the PHI. |
| 5593 | if (Safe) { |
| 5594 | BasicBlock::iterator I = &LI; |
| 5595 | for (--I; !isa<PHINode>(I); --I) |
| 5596 | if (isa<StoreInst>(I) || isa<CallInst>(I)) { |
| 5597 | Safe = false; |
| 5598 | break; |
| 5599 | } |
| 5600 | } |
| 5601 | |
| 5602 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e && Safe; ++i) |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 5603 | if (!isSafeToLoadUnconditionally(PN->getIncomingValue(i), |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 5604 | PN->getIncomingBlock(i)->getTerminator())) |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5605 | Safe = false; |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 5606 | |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5607 | if (Safe) { |
| 5608 | // Create the PHI. |
| 5609 | PHINode *NewPN = new PHINode(LI.getType(), PN->getName()); |
| 5610 | InsertNewInstBefore(NewPN, *PN); |
| 5611 | std::map<BasicBlock*,Value*> LoadMap; // Don't insert duplicate loads |
| 5612 | |
| 5613 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 5614 | BasicBlock *BB = PN->getIncomingBlock(i); |
| 5615 | Value *&TheLoad = LoadMap[BB]; |
| 5616 | if (TheLoad == 0) { |
| 5617 | Value *InVal = PN->getIncomingValue(i); |
| 5618 | TheLoad = InsertNewInstBefore(new LoadInst(InVal, |
| 5619 | InVal->getName()+".val"), |
| 5620 | *BB->getTerminator()); |
| 5621 | } |
| 5622 | NewPN->addIncoming(TheLoad, BB); |
| 5623 | } |
| 5624 | return ReplaceInstUsesWith(LI, NewPN); |
| 5625 | } |
| 5626 | } |
| 5627 | } |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 5628 | return 0; |
| 5629 | } |
| 5630 | |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 5631 | /// InstCombineStoreToCast - Fold 'store V, (cast P)' -> store (cast V), P' |
| 5632 | /// when possible. |
| 5633 | static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { |
| 5634 | User *CI = cast<User>(SI.getOperand(1)); |
| 5635 | Value *CastOp = CI->getOperand(0); |
| 5636 | |
| 5637 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
| 5638 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
| 5639 | const Type *SrcPTy = SrcTy->getElementType(); |
| 5640 | |
| 5641 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) { |
| 5642 | // If the source is an array, the code below will not succeed. Check to |
| 5643 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 5644 | // constants. |
| 5645 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 5646 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 5647 | if (ASrcTy->getNumElements() != 0) { |
| 5648 | std::vector<Value*> Idxs(2, Constant::getNullValue(Type::IntTy)); |
| 5649 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs); |
| 5650 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 5651 | SrcPTy = SrcTy->getElementType(); |
| 5652 | } |
| 5653 | |
| 5654 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5655 | IC.getTargetData().getTypeSize(SrcPTy) == |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 5656 | IC.getTargetData().getTypeSize(DestPTy)) { |
| 5657 | |
| 5658 | // Okay, we are casting from one integer or pointer type to another of |
| 5659 | // the same size. Instead of casting the pointer before the store, cast |
| 5660 | // the value to be stored. |
| 5661 | Value *NewCast; |
| 5662 | if (Constant *C = dyn_cast<Constant>(SI.getOperand(0))) |
| 5663 | NewCast = ConstantExpr::getCast(C, SrcPTy); |
| 5664 | else |
| 5665 | NewCast = IC.InsertNewInstBefore(new CastInst(SI.getOperand(0), |
| 5666 | SrcPTy, |
| 5667 | SI.getOperand(0)->getName()+".c"), SI); |
| 5668 | |
| 5669 | return new StoreInst(NewCast, CastOp); |
| 5670 | } |
| 5671 | } |
| 5672 | } |
| 5673 | return 0; |
| 5674 | } |
| 5675 | |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 5676 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
| 5677 | Value *Val = SI.getOperand(0); |
| 5678 | Value *Ptr = SI.getOperand(1); |
| 5679 | |
| 5680 | if (isa<UndefValue>(Ptr)) { // store X, undef -> noop (even if volatile) |
| 5681 | removeFromWorkList(&SI); |
| 5682 | SI.eraseFromParent(); |
| 5683 | ++NumCombined; |
| 5684 | return 0; |
| 5685 | } |
| 5686 | |
| 5687 | if (SI.isVolatile()) return 0; // Don't hack volatile loads. |
| 5688 | |
| 5689 | // store X, null -> turns into 'unreachable' in SimplifyCFG |
| 5690 | if (isa<ConstantPointerNull>(Ptr)) { |
| 5691 | if (!isa<UndefValue>(Val)) { |
| 5692 | SI.setOperand(0, UndefValue::get(Val->getType())); |
| 5693 | if (Instruction *U = dyn_cast<Instruction>(Val)) |
| 5694 | WorkList.push_back(U); // Dropped a use. |
| 5695 | ++NumCombined; |
| 5696 | } |
| 5697 | return 0; // Do not modify these! |
| 5698 | } |
| 5699 | |
| 5700 | // store undef, Ptr -> noop |
| 5701 | if (isa<UndefValue>(Val)) { |
| 5702 | removeFromWorkList(&SI); |
| 5703 | SI.eraseFromParent(); |
| 5704 | ++NumCombined; |
| 5705 | return 0; |
| 5706 | } |
| 5707 | |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 5708 | // If the pointer destination is a cast, see if we can fold the cast into the |
| 5709 | // source instead. |
| 5710 | if (CastInst *CI = dyn_cast<CastInst>(Ptr)) |
| 5711 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 5712 | return Res; |
| 5713 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
| 5714 | if (CE->getOpcode() == Instruction::Cast) |
| 5715 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 5716 | return Res; |
| 5717 | |
Chris Lattner | 219175c | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 5718 | |
| 5719 | // If this store is the last instruction in the basic block, and if the block |
| 5720 | // ends with an unconditional branch, try to move it to the successor block. |
| 5721 | BasicBlock::iterator BBI = &SI; ++BBI; |
| 5722 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) |
| 5723 | if (BI->isUnconditional()) { |
| 5724 | // Check to see if the successor block has exactly two incoming edges. If |
| 5725 | // so, see if the other predecessor contains a store to the same location. |
| 5726 | // if so, insert a PHI node (if needed) and move the stores down. |
| 5727 | BasicBlock *Dest = BI->getSuccessor(0); |
| 5728 | |
| 5729 | pred_iterator PI = pred_begin(Dest); |
| 5730 | BasicBlock *Other = 0; |
| 5731 | if (*PI != BI->getParent()) |
| 5732 | Other = *PI; |
| 5733 | ++PI; |
| 5734 | if (PI != pred_end(Dest)) { |
| 5735 | if (*PI != BI->getParent()) |
| 5736 | if (Other) |
| 5737 | Other = 0; |
| 5738 | else |
| 5739 | Other = *PI; |
| 5740 | if (++PI != pred_end(Dest)) |
| 5741 | Other = 0; |
| 5742 | } |
| 5743 | if (Other) { // If only one other pred... |
| 5744 | BBI = Other->getTerminator(); |
| 5745 | // Make sure this other block ends in an unconditional branch and that |
| 5746 | // there is an instruction before the branch. |
| 5747 | if (isa<BranchInst>(BBI) && cast<BranchInst>(BBI)->isUnconditional() && |
| 5748 | BBI != Other->begin()) { |
| 5749 | --BBI; |
| 5750 | StoreInst *OtherStore = dyn_cast<StoreInst>(BBI); |
| 5751 | |
| 5752 | // If this instruction is a store to the same location. |
| 5753 | if (OtherStore && OtherStore->getOperand(1) == SI.getOperand(1)) { |
| 5754 | // Okay, we know we can perform this transformation. Insert a PHI |
| 5755 | // node now if we need it. |
| 5756 | Value *MergedVal = OtherStore->getOperand(0); |
| 5757 | if (MergedVal != SI.getOperand(0)) { |
| 5758 | PHINode *PN = new PHINode(MergedVal->getType(), "storemerge"); |
| 5759 | PN->reserveOperandSpace(2); |
| 5760 | PN->addIncoming(SI.getOperand(0), SI.getParent()); |
| 5761 | PN->addIncoming(OtherStore->getOperand(0), Other); |
| 5762 | MergedVal = InsertNewInstBefore(PN, Dest->front()); |
| 5763 | } |
| 5764 | |
| 5765 | // Advance to a place where it is safe to insert the new store and |
| 5766 | // insert it. |
| 5767 | BBI = Dest->begin(); |
| 5768 | while (isa<PHINode>(BBI)) ++BBI; |
| 5769 | InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1), |
| 5770 | OtherStore->isVolatile()), *BBI); |
| 5771 | |
| 5772 | // Nuke the old stores. |
| 5773 | removeFromWorkList(&SI); |
| 5774 | removeFromWorkList(OtherStore); |
| 5775 | SI.eraseFromParent(); |
| 5776 | OtherStore->eraseFromParent(); |
| 5777 | ++NumCombined; |
| 5778 | return 0; |
| 5779 | } |
| 5780 | } |
| 5781 | } |
| 5782 | } |
| 5783 | |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 5784 | return 0; |
| 5785 | } |
| 5786 | |
| 5787 | |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 5788 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 5789 | // Change br (not X), label True, label False to: br X, label False, True |
Reid Spencer | 4fdd96c | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 5790 | Value *X = 0; |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 5791 | BasicBlock *TrueDest; |
| 5792 | BasicBlock *FalseDest; |
| 5793 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && |
| 5794 | !isa<Constant>(X)) { |
| 5795 | // Swap Destinations and condition... |
| 5796 | BI.setCondition(X); |
| 5797 | BI.setSuccessor(0, FalseDest); |
| 5798 | BI.setSuccessor(1, TrueDest); |
| 5799 | return &BI; |
| 5800 | } |
| 5801 | |
| 5802 | // Cannonicalize setne -> seteq |
| 5803 | Instruction::BinaryOps Op; Value *Y; |
| 5804 | if (match(&BI, m_Br(m_SetCond(Op, m_Value(X), m_Value(Y)), |
| 5805 | TrueDest, FalseDest))) |
| 5806 | if ((Op == Instruction::SetNE || Op == Instruction::SetLE || |
| 5807 | Op == Instruction::SetGE) && BI.getCondition()->hasOneUse()) { |
| 5808 | SetCondInst *I = cast<SetCondInst>(BI.getCondition()); |
| 5809 | std::string Name = I->getName(); I->setName(""); |
| 5810 | Instruction::BinaryOps NewOpcode = SetCondInst::getInverseCondition(Op); |
| 5811 | Value *NewSCC = BinaryOperator::create(NewOpcode, X, Y, Name, I); |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 5812 | // Swap Destinations and condition... |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 5813 | BI.setCondition(NewSCC); |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 5814 | BI.setSuccessor(0, FalseDest); |
| 5815 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 5816 | removeFromWorkList(I); |
| 5817 | I->getParent()->getInstList().erase(I); |
| 5818 | WorkList.push_back(cast<Instruction>(NewSCC)); |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 5819 | return &BI; |
| 5820 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5821 | |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 5822 | return 0; |
| 5823 | } |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 5824 | |
Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 5825 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
| 5826 | Value *Cond = SI.getCondition(); |
| 5827 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { |
| 5828 | if (I->getOpcode() == Instruction::Add) |
| 5829 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 5830 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' |
| 5831 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5832 | SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), |
Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 5833 | AddRHS)); |
| 5834 | SI.setOperand(0, I->getOperand(0)); |
| 5835 | WorkList.push_back(I); |
| 5836 | return &SI; |
| 5837 | } |
| 5838 | } |
| 5839 | return 0; |
| 5840 | } |
| 5841 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5842 | void InstCombiner::removeFromWorkList(Instruction *I) { |
| 5843 | WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I), |
| 5844 | WorkList.end()); |
| 5845 | } |
| 5846 | |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 5847 | |
| 5848 | /// TryToSinkInstruction - Try to move the specified instruction from its |
| 5849 | /// current block into the beginning of DestBlock, which can only happen if it's |
| 5850 | /// safe to move the instruction past all of the instructions between it and the |
| 5851 | /// end of its block. |
| 5852 | static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { |
| 5853 | assert(I->hasOneUse() && "Invariants didn't hold!"); |
| 5854 | |
Chris Lattner | c4f67e6 | 2005-10-27 17:13:11 +0000 | [diff] [blame] | 5855 | // Cannot move control-flow-involving, volatile loads, vaarg, etc. |
| 5856 | if (isa<PHINode>(I) || I->mayWriteToMemory()) return false; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5857 | |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 5858 | // Do not sink alloca instructions out of the entry block. |
| 5859 | if (isa<AllocaInst>(I) && I->getParent() == &DestBlock->getParent()->front()) |
| 5860 | return false; |
| 5861 | |
Chris Lattner | f17a2fb | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 5862 | // We can only sink load instructions if there is nothing between the load and |
| 5863 | // the end of block that could change the value. |
| 5864 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Chris Lattner | f17a2fb | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 5865 | for (BasicBlock::iterator Scan = LI, E = LI->getParent()->end(); |
| 5866 | Scan != E; ++Scan) |
| 5867 | if (Scan->mayWriteToMemory()) |
| 5868 | return false; |
Chris Lattner | f17a2fb | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 5869 | } |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 5870 | |
| 5871 | BasicBlock::iterator InsertPos = DestBlock->begin(); |
| 5872 | while (isa<PHINode>(InsertPos)) ++InsertPos; |
| 5873 | |
Chris Lattner | 9f269e4 | 2005-08-08 19:11:57 +0000 | [diff] [blame] | 5874 | I->moveBefore(InsertPos); |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 5875 | ++NumSunkInst; |
| 5876 | return true; |
| 5877 | } |
| 5878 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5879 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 5880 | bool Changed = false; |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 5881 | TD = &getAnalysis<TargetData>(); |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 5882 | |
Chris Lattner | 4ed40f7 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 5883 | { |
| 5884 | // Populate the worklist with the reachable instructions. |
| 5885 | std::set<BasicBlock*> Visited; |
| 5886 | for (df_ext_iterator<BasicBlock*> BB = df_ext_begin(&F.front(), Visited), |
| 5887 | E = df_ext_end(&F.front(), Visited); BB != E; ++BB) |
| 5888 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 5889 | WorkList.push_back(I); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 5890 | |
Chris Lattner | 4ed40f7 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 5891 | // Do a quick scan over the function. If we find any blocks that are |
| 5892 | // unreachable, remove any instructions inside of them. This prevents |
| 5893 | // the instcombine code from having to deal with some bad special cases. |
| 5894 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 5895 | if (!Visited.count(BB)) { |
| 5896 | Instruction *Term = BB->getTerminator(); |
| 5897 | while (Term != BB->begin()) { // Remove instrs bottom-up |
| 5898 | BasicBlock::iterator I = Term; --I; |
Chris Lattner | 2d3a7a6 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 5899 | |
Chris Lattner | 4ed40f7 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 5900 | DEBUG(std::cerr << "IC: DCE: " << *I); |
| 5901 | ++NumDeadInst; |
| 5902 | |
| 5903 | if (!I->use_empty()) |
| 5904 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 5905 | I->eraseFromParent(); |
| 5906 | } |
| 5907 | } |
| 5908 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 5909 | |
| 5910 | while (!WorkList.empty()) { |
| 5911 | Instruction *I = WorkList.back(); // Get an instruction from the worklist |
| 5912 | WorkList.pop_back(); |
| 5913 | |
Misha Brukman | 632df28 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 5914 | // Check to see if we can DCE or ConstantPropagate the instruction... |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5915 | // Check to see if we can DIE the instruction... |
| 5916 | if (isInstructionTriviallyDead(I)) { |
| 5917 | // Add operands to the worklist... |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5918 | if (I->getNumOperands() < 4) |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 5919 | AddUsesToWorkList(*I); |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5920 | ++NumDeadInst; |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5921 | |
Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 5922 | DEBUG(std::cerr << "IC: DCE: " << *I); |
| 5923 | |
| 5924 | I->eraseFromParent(); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5925 | removeFromWorkList(I); |
| 5926 | continue; |
| 5927 | } |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5928 | |
Misha Brukman | 632df28 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 5929 | // Instruction isn't dead, see if we can constant propagate it... |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5930 | if (Constant *C = ConstantFoldInstruction(I)) { |
Alkis Evlogimenos | a1291a0 | 2004-12-08 23:10:30 +0000 | [diff] [blame] | 5931 | Value* Ptr = I->getOperand(0); |
Chris Lattner | 6580e09 | 2004-10-16 19:44:59 +0000 | [diff] [blame] | 5932 | if (isa<GetElementPtrInst>(I) && |
Alkis Evlogimenos | a1291a0 | 2004-12-08 23:10:30 +0000 | [diff] [blame] | 5933 | cast<Constant>(Ptr)->isNullValue() && |
| 5934 | !isa<ConstantPointerNull>(C) && |
| 5935 | cast<PointerType>(Ptr->getType())->getElementType()->isSized()) { |
Chris Lattner | 6580e09 | 2004-10-16 19:44:59 +0000 | [diff] [blame] | 5936 | // If this is a constant expr gep that is effectively computing an |
| 5937 | // "offsetof", fold it into 'cast int X to T*' instead of 'gep 0, 0, 12' |
| 5938 | bool isFoldableGEP = true; |
| 5939 | for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i) |
| 5940 | if (!isa<ConstantInt>(I->getOperand(i))) |
| 5941 | isFoldableGEP = false; |
| 5942 | if (isFoldableGEP) { |
Alkis Evlogimenos | a1291a0 | 2004-12-08 23:10:30 +0000 | [diff] [blame] | 5943 | uint64_t Offset = TD->getIndexedOffset(Ptr->getType(), |
Chris Lattner | 6580e09 | 2004-10-16 19:44:59 +0000 | [diff] [blame] | 5944 | std::vector<Value*>(I->op_begin()+1, I->op_end())); |
| 5945 | C = ConstantUInt::get(Type::ULongTy, Offset); |
Chris Lattner | 684c5c6 | 2004-10-16 19:46:33 +0000 | [diff] [blame] | 5946 | C = ConstantExpr::getCast(C, TD->getIntPtrType()); |
Chris Lattner | 6580e09 | 2004-10-16 19:44:59 +0000 | [diff] [blame] | 5947 | C = ConstantExpr::getCast(C, I->getType()); |
| 5948 | } |
| 5949 | } |
| 5950 | |
Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 5951 | DEBUG(std::cerr << "IC: ConstFold to: " << *C << " from: " << *I); |
| 5952 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5953 | // Add operands to the worklist... |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 5954 | AddUsesToWorkList(*I); |
Chris Lattner | c6509f4 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 5955 | ReplaceInstUsesWith(*I, C); |
| 5956 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5957 | ++NumConstProp; |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5958 | I->getParent()->getInstList().erase(I); |
Chris Lattner | 800aaaf | 2003-10-07 15:17:02 +0000 | [diff] [blame] | 5959 | removeFromWorkList(I); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5960 | continue; |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5961 | } |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5962 | |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 5963 | // See if we can trivially sink this instruction to a successor basic block. |
| 5964 | if (I->hasOneUse()) { |
| 5965 | BasicBlock *BB = I->getParent(); |
| 5966 | BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent(); |
| 5967 | if (UserParent != BB) { |
| 5968 | bool UserIsSuccessor = false; |
| 5969 | // See if the user is one of our successors. |
| 5970 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) |
| 5971 | if (*SI == UserParent) { |
| 5972 | UserIsSuccessor = true; |
| 5973 | break; |
| 5974 | } |
| 5975 | |
| 5976 | // If the user is one of our immediate successors, and if that successor |
| 5977 | // only has us as a predecessors (we'd have to split the critical edge |
| 5978 | // otherwise), we can keep going. |
| 5979 | if (UserIsSuccessor && !isa<PHINode>(I->use_back()) && |
| 5980 | next(pred_begin(UserParent)) == pred_end(UserParent)) |
| 5981 | // Okay, the CFG is simple enough, try to sink this instruction. |
| 5982 | Changed |= TryToSinkInstruction(I, UserParent); |
| 5983 | } |
| 5984 | } |
| 5985 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 5986 | // Now that we have an instruction, try combining it to simplify it... |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 5987 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 5988 | ++NumCombined; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 5989 | // Should we replace the old instruction with a new one? |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 5990 | if (Result != I) { |
Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 5991 | DEBUG(std::cerr << "IC: Old = " << *I |
| 5992 | << " New = " << *Result); |
| 5993 | |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 5994 | // Everything uses the new instruction now. |
| 5995 | I->replaceAllUsesWith(Result); |
| 5996 | |
| 5997 | // Push the new instruction and any users onto the worklist. |
| 5998 | WorkList.push_back(Result); |
| 5999 | AddUsersToWorkList(*Result); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 6000 | |
| 6001 | // Move the name to the new instruction first... |
| 6002 | std::string OldName = I->getName(); I->setName(""); |
Chris Lattner | 950fc78 | 2003-10-07 22:58:41 +0000 | [diff] [blame] | 6003 | Result->setName(OldName); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 6004 | |
| 6005 | // Insert the new instruction into the basic block... |
| 6006 | BasicBlock *InstParent = I->getParent(); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 6007 | BasicBlock::iterator InsertPos = I; |
| 6008 | |
| 6009 | if (!isa<PHINode>(Result)) // If combining a PHI, don't insert |
| 6010 | while (isa<PHINode>(InsertPos)) // middle of a block of PHIs. |
| 6011 | ++InsertPos; |
| 6012 | |
| 6013 | InstParent->getInstList().insert(InsertPos, Result); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 6014 | |
Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 6015 | // Make sure that we reprocess all operands now that we reduced their |
| 6016 | // use counts. |
Chris Lattner | b643a9e | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 6017 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 6018 | if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i))) |
| 6019 | WorkList.push_back(OpI); |
| 6020 | |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 6021 | // Instructions can end up on the worklist more than once. Make sure |
| 6022 | // we do not process an instruction that has been deleted. |
| 6023 | removeFromWorkList(I); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 6024 | |
| 6025 | // Erase the old instruction. |
| 6026 | InstParent->getInstList().erase(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 6027 | } else { |
Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 6028 | DEBUG(std::cerr << "IC: MOD = " << *I); |
| 6029 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 6030 | // If the instruction was modified, it's possible that it is now dead. |
| 6031 | // if so, remove it. |
Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 6032 | if (isInstructionTriviallyDead(I)) { |
| 6033 | // Make sure we process all operands now that we are reducing their |
| 6034 | // use counts. |
| 6035 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 6036 | if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i))) |
| 6037 | WorkList.push_back(OpI); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6038 | |
Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 6039 | // Instructions may end up in the worklist more than once. Erase all |
| 6040 | // occurrances of this instruction. |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 6041 | removeFromWorkList(I); |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 6042 | I->eraseFromParent(); |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 6043 | } else { |
| 6044 | WorkList.push_back(Result); |
| 6045 | AddUsersToWorkList(*Result); |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 6046 | } |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 6047 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 6048 | Changed = true; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 6049 | } |
| 6050 | } |
| 6051 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 6052 | return Changed; |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 6053 | } |
| 6054 | |
Brian Gaeke | 38b79e8 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 6055 | FunctionPass *llvm::createInstructionCombiningPass() { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 6056 | return new InstCombiner(); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 6057 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 6058 | |