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 | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 122 | Instruction *visitCastInst(CastInst &CI); |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 123 | Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 124 | Instruction *FI); |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 125 | Instruction *visitSelectInst(SelectInst &CI); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 126 | Instruction *visitCallInst(CallInst &CI); |
| 127 | Instruction *visitInvokeInst(InvokeInst &II); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 128 | Instruction *visitPHINode(PHINode &PN); |
| 129 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 130 | Instruction *visitAllocationInst(AllocationInst &AI); |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 131 | Instruction *visitFreeInst(FreeInst &FI); |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 132 | Instruction *visitLoadInst(LoadInst &LI); |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 133 | Instruction *visitStoreInst(StoreInst &SI); |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 134 | Instruction *visitBranchInst(BranchInst &BI); |
Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 135 | Instruction *visitSwitchInst(SwitchInst &SI); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 136 | |
| 137 | // visitInstruction - Specify what to return for unhandled instructions... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 138 | Instruction *visitInstruction(Instruction &I) { return 0; } |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 140 | private: |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 141 | Instruction *visitCallSite(CallSite CS); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 142 | bool transformConstExprCastCall(CallSite CS); |
| 143 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 144 | public: |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 145 | // InsertNewInstBefore - insert an instruction New before instruction Old |
| 146 | // in the program. Add the new instruction to the worklist. |
| 147 | // |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 148 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Chris Lattner | 65217ff | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 149 | assert(New && New->getParent() == 0 && |
| 150 | "New instruction already inserted into a basic block!"); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 151 | BasicBlock *BB = Old.getParent(); |
| 152 | BB->getInstList().insert(&Old, New); // Insert inst |
| 153 | WorkList.push_back(New); // Add to worklist |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 154 | return New; |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 157 | /// InsertCastBefore - Insert a cast of V to TY before the instruction POS. |
| 158 | /// This also adds the cast to the worklist. Finally, this returns the |
| 159 | /// cast. |
| 160 | Value *InsertCastBefore(Value *V, const Type *Ty, Instruction &Pos) { |
| 161 | if (V->getType() == Ty) return V; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 162 | |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 163 | Instruction *C = new CastInst(V, Ty, V->getName(), &Pos); |
| 164 | WorkList.push_back(C); |
| 165 | return C; |
| 166 | } |
| 167 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 168 | // ReplaceInstUsesWith - This method is to be used when an instruction is |
| 169 | // found to be dead, replacable with another preexisting expression. Here |
| 170 | // we add all uses of I to the worklist, replace all uses of I with the new |
| 171 | // value, then return I, so that the inst combiner will know that I was |
| 172 | // modified. |
| 173 | // |
| 174 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 175 | AddUsersToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 8953b90 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 176 | if (&I != V) { |
| 177 | I.replaceAllUsesWith(V); |
| 178 | return &I; |
| 179 | } else { |
| 180 | // If we are replacing the instruction with itself, this must be in a |
| 181 | // segment of unreachable code, so just clobber the instruction. |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 182 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
Chris Lattner | 8953b90 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 183 | return &I; |
| 184 | } |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 185 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 186 | |
| 187 | // EraseInstFromFunction - When dealing with an instruction that has side |
| 188 | // effects or produces a void value, we can't rely on DCE to delete the |
| 189 | // instruction. Instead, visit methods should return the value returned by |
| 190 | // this function. |
| 191 | Instruction *EraseInstFromFunction(Instruction &I) { |
| 192 | assert(I.use_empty() && "Cannot erase instruction that is used!"); |
| 193 | AddUsesToWorkList(I); |
| 194 | removeFromWorkList(&I); |
Chris Lattner | 9530754 | 2004-11-18 21:41:39 +0000 | [diff] [blame] | 195 | I.eraseFromParent(); |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 196 | return 0; // Don't do anything with FI |
| 197 | } |
| 198 | |
| 199 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 200 | private: |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 201 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 202 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 203 | /// casts that are known to not do anything... |
| 204 | /// |
| 205 | Value *InsertOperandCastBefore(Value *V, const Type *DestTy, |
| 206 | Instruction *InsertBefore); |
| 207 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 208 | // SimplifyCommutative - This performs a few simplifications for commutative |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 209 | // operators. |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 210 | bool SimplifyCommutative(BinaryOperator &I); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 211 | |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 212 | |
| 213 | // FoldOpIntoPhi - Given a binary operator or cast instruction which has a |
| 214 | // PHI node as operand #0, see if we can fold the instruction into the PHI |
| 215 | // (which is only possible if all operands to the PHI are constants). |
| 216 | Instruction *FoldOpIntoPhi(Instruction &I); |
| 217 | |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 218 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 219 | // operator and they all are only used by the PHI, PHI together their |
| 220 | // inputs, and do the operation once, to the result of the PHI. |
| 221 | Instruction *FoldPHIArgOpIntoPHI(PHINode &PN); |
| 222 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 223 | Instruction *OptAndOp(Instruction *Op, ConstantIntegral *OpRHS, |
| 224 | ConstantIntegral *AndRHS, BinaryOperator &TheAnd); |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 225 | |
| 226 | Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantIntegral *Mask, |
| 227 | bool isSub, Instruction &I); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 228 | Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
| 229 | bool Inside, Instruction &IB); |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 230 | Instruction *PromoteCastOfAllocation(CastInst &CI, AllocationInst &AI); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 231 | }; |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 232 | |
Chris Lattner | c8b7092 | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 233 | RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions"); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 236 | // getComplexity: Assign a complexity or rank value to LLVM Values... |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 237 | // 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 238 | static unsigned getComplexity(Value *V) { |
| 239 | if (isa<Instruction>(V)) { |
| 240 | if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V)) |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 241 | return 3; |
| 242 | return 4; |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 243 | } |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 244 | if (isa<Argument>(V)) return 3; |
| 245 | return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2; |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 246 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 247 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 248 | // isOnlyUse - Return true if this instruction will be deleted if we stop using |
| 249 | // it. |
| 250 | static bool isOnlyUse(Value *V) { |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 251 | return V->hasOneUse() || isa<Constant>(V); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 254 | // getPromotedType - Return the specified type promoted as it would be to pass |
| 255 | // though a va_arg area... |
| 256 | static const Type *getPromotedType(const Type *Ty) { |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 257 | switch (Ty->getTypeID()) { |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 258 | case Type::SByteTyID: |
| 259 | case Type::ShortTyID: return Type::IntTy; |
| 260 | case Type::UByteTyID: |
| 261 | case Type::UShortTyID: return Type::UIntTy; |
| 262 | case Type::FloatTyID: return Type::DoubleTy; |
| 263 | default: return Ty; |
| 264 | } |
| 265 | } |
| 266 | |
Chris Lattner | 567b81f | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 267 | /// isCast - If the specified operand is a CastInst or a constant expr cast, |
| 268 | /// return the operand value, otherwise return null. |
| 269 | static Value *isCast(Value *V) { |
| 270 | if (CastInst *I = dyn_cast<CastInst>(V)) |
| 271 | return I->getOperand(0); |
| 272 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 273 | if (CE->getOpcode() == Instruction::Cast) |
| 274 | return CE->getOperand(0); |
| 275 | return 0; |
| 276 | } |
| 277 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 278 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 279 | // operators: |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 280 | // |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 281 | // 1. Order operands such that they are listed from right (least complex) to |
| 282 | // left (most complex). This puts constants before unary operators before |
| 283 | // binary operators. |
| 284 | // |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 285 | // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) |
| 286 | // 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] | 287 | // |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 288 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 289 | bool Changed = false; |
| 290 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) |
| 291 | Changed = !I.swapOperands(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 292 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 293 | if (!I.isAssociative()) return Changed; |
| 294 | Instruction::BinaryOps Opcode = I.getOpcode(); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 295 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) |
| 296 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { |
| 297 | if (isa<Constant>(I.getOperand(1))) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 298 | Constant *Folded = ConstantExpr::get(I.getOpcode(), |
| 299 | cast<Constant>(I.getOperand(1)), |
| 300 | cast<Constant>(Op->getOperand(1))); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 301 | I.setOperand(0, Op->getOperand(0)); |
| 302 | I.setOperand(1, Folded); |
| 303 | return true; |
| 304 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) |
| 305 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && |
| 306 | isOnlyUse(Op) && isOnlyUse(Op1)) { |
| 307 | Constant *C1 = cast<Constant>(Op->getOperand(1)); |
| 308 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); |
| 309 | |
| 310 | // 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] | 311 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 312 | Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0), |
| 313 | Op1->getOperand(0), |
| 314 | Op1->getName(), &I); |
| 315 | WorkList.push_back(New); |
| 316 | I.setOperand(0, New); |
| 317 | I.setOperand(1, Folded); |
| 318 | return true; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 319 | } |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 320 | } |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 321 | return Changed; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 322 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 323 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 324 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
| 325 | // if the LHS is a constant zero (which is the 'negate' form). |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 326 | // |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 327 | static inline Value *dyn_castNegVal(Value *V) { |
| 328 | if (BinaryOperator::isNeg(V)) |
Chris Lattner | d6f636a | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 329 | return BinaryOperator::getNegArgument(V); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 330 | |
Chris Lattner | 9ad0d55 | 2004-12-14 20:08:06 +0000 | [diff] [blame] | 331 | // Constants can be considered to be negated values if they can be folded. |
| 332 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
| 333 | return ConstantExpr::getNeg(C); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 334 | return 0; |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 337 | static inline Value *dyn_castNotVal(Value *V) { |
| 338 | if (BinaryOperator::isNot(V)) |
Chris Lattner | d6f636a | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 339 | return BinaryOperator::getNotArgument(V); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 340 | |
| 341 | // Constants can be considered to be not'ed values... |
Chris Lattner | dd65d86 | 2003-04-30 22:34:06 +0000 | [diff] [blame] | 342 | if (ConstantIntegral *C = dyn_cast<ConstantIntegral>(V)) |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 343 | return ConstantExpr::getNot(C); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 344 | return 0; |
| 345 | } |
| 346 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 347 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
| 348 | // other computations (because it has a constant operand), return the |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 349 | // non-constant operand of the multiply, and set CST to point to the multiplier. |
| 350 | // Otherwise, return null. |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 351 | // |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 352 | static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) { |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 353 | if (V->hasOneUse() && V->getType()->isInteger()) |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 354 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 355 | if (I->getOpcode() == Instruction::Mul) |
Chris Lattner | 97013636 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 356 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 357 | return I->getOperand(0); |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 358 | if (I->getOpcode() == Instruction::Shl) |
Chris Lattner | 97013636 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 359 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) { |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 360 | // The multiplier is really 1 << CST. |
| 361 | Constant *One = ConstantInt::get(V->getType(), 1); |
| 362 | CST = cast<ConstantInt>(ConstantExpr::getShl(One, CST)); |
| 363 | return I->getOperand(0); |
| 364 | } |
| 365 | } |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 366 | return 0; |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 367 | } |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 368 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 369 | /// dyn_castGetElementPtr - If this is a getelementptr instruction or constant |
| 370 | /// expression, return it. |
| 371 | static User *dyn_castGetElementPtr(Value *V) { |
| 372 | if (isa<GetElementPtrInst>(V)) return cast<User>(V); |
| 373 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 374 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 375 | return cast<User>(V); |
| 376 | return false; |
| 377 | } |
| 378 | |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 379 | // AddOne, SubOne - Add or subtract a constant one from an integer constant... |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 380 | static ConstantInt *AddOne(ConstantInt *C) { |
| 381 | return cast<ConstantInt>(ConstantExpr::getAdd(C, |
| 382 | ConstantInt::get(C->getType(), 1))); |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 383 | } |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 384 | static ConstantInt *SubOne(ConstantInt *C) { |
| 385 | return cast<ConstantInt>(ConstantExpr::getSub(C, |
| 386 | ConstantInt::get(C->getType(), 1))); |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 389 | /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use |
| 390 | /// this predicate to simplify operations downstream. V and Mask are known to |
| 391 | /// be the same type. |
| 392 | static bool MaskedValueIsZero(Value *V, ConstantIntegral *Mask) { |
| 393 | // Note, we cannot consider 'undef' to be "IsZero" here. The problem is that |
| 394 | // we cannot optimize based on the assumption that it is zero without changing |
| 395 | // to to an explicit zero. If we don't change it to zero, other code could |
| 396 | // optimized based on the contradictory assumption that it is non-zero. |
| 397 | // Because instcombine aggressively folds operations with undef args anyway, |
| 398 | // this won't lose us code quality. |
| 399 | if (Mask->isNullValue()) |
| 400 | return true; |
| 401 | if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V)) |
| 402 | return ConstantExpr::getAnd(CI, Mask)->isNullValue(); |
| 403 | |
| 404 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 405 | switch (I->getOpcode()) { |
Chris Lattner | 62010c4 | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 406 | case Instruction::And: |
| 407 | // (X & C1) & C2 == 0 iff C1 & C2 == 0. |
Chris Lattner | 03b9eb5 | 2005-10-09 22:08:50 +0000 | [diff] [blame] | 408 | if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(I->getOperand(1))) { |
| 409 | ConstantIntegral *C1C2 = |
| 410 | cast<ConstantIntegral>(ConstantExpr::getAnd(CI, Mask)); |
| 411 | if (MaskedValueIsZero(I->getOperand(0), C1C2)) |
Chris Lattner | 62010c4 | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 412 | return true; |
Chris Lattner | 03b9eb5 | 2005-10-09 22:08:50 +0000 | [diff] [blame] | 413 | } |
| 414 | // If either the LHS or the RHS are MaskedValueIsZero, the result is zero. |
| 415 | return MaskedValueIsZero(I->getOperand(1), Mask) || |
| 416 | MaskedValueIsZero(I->getOperand(0), Mask); |
Chris Lattner | 62010c4 | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 417 | case Instruction::Or: |
Chris Lattner | 03b9eb5 | 2005-10-09 22:08:50 +0000 | [diff] [blame] | 418 | case Instruction::Xor: |
Chris Lattner | 62010c4 | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 419 | // If the LHS and the RHS are MaskedValueIsZero, the result is also zero. |
| 420 | return MaskedValueIsZero(I->getOperand(1), Mask) && |
| 421 | MaskedValueIsZero(I->getOperand(0), Mask); |
| 422 | case Instruction::Select: |
| 423 | // If the T and F values are MaskedValueIsZero, the result is also zero. |
| 424 | return MaskedValueIsZero(I->getOperand(2), Mask) && |
| 425 | MaskedValueIsZero(I->getOperand(1), Mask); |
| 426 | case Instruction::Cast: { |
| 427 | const Type *SrcTy = I->getOperand(0)->getType(); |
| 428 | if (SrcTy == Type::BoolTy) |
| 429 | return (Mask->getRawValue() & 1) == 0; |
| 430 | |
| 431 | if (SrcTy->isInteger()) { |
| 432 | // (cast <ty> X to int) & C2 == 0 iff <ty> could not have contained C2. |
| 433 | if (SrcTy->isUnsigned() && // Only handle zero ext. |
| 434 | ConstantExpr::getCast(Mask, SrcTy)->isNullValue()) |
| 435 | return true; |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 436 | |
Chris Lattner | 62010c4 | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 437 | // If this is a noop cast, recurse. |
| 438 | if ((SrcTy->isSigned() && SrcTy->getUnsignedVersion() == I->getType())|| |
| 439 | SrcTy->getSignedVersion() == I->getType()) { |
| 440 | Constant *NewMask = |
| 441 | ConstantExpr::getCast(Mask, I->getOperand(0)->getType()); |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 442 | return MaskedValueIsZero(I->getOperand(0), |
Chris Lattner | 62010c4 | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 443 | cast<ConstantIntegral>(NewMask)); |
| 444 | } |
| 445 | } |
| 446 | break; |
| 447 | } |
| 448 | case Instruction::Shl: |
| 449 | // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 |
| 450 | if (ConstantUInt *SA = dyn_cast<ConstantUInt>(I->getOperand(1))) |
| 451 | return MaskedValueIsZero(I->getOperand(0), |
| 452 | cast<ConstantIntegral>(ConstantExpr::getUShr(Mask, SA))); |
| 453 | break; |
| 454 | case Instruction::Shr: |
| 455 | // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
| 456 | if (ConstantUInt *SA = dyn_cast<ConstantUInt>(I->getOperand(1))) |
| 457 | if (I->getType()->isUnsigned()) { |
| 458 | Constant *C1 = ConstantIntegral::getAllOnesValue(I->getType()); |
| 459 | C1 = ConstantExpr::getShr(C1, SA); |
| 460 | C1 = ConstantExpr::getAnd(C1, Mask); |
| 461 | if (C1->isNullValue()) |
| 462 | return true; |
| 463 | } |
| 464 | break; |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
| 468 | return false; |
| 469 | } |
| 470 | |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 471 | // isTrueWhenEqual - Return true if the specified setcondinst instruction is |
| 472 | // true when both operands are equal... |
| 473 | // |
| 474 | static bool isTrueWhenEqual(Instruction &I) { |
| 475 | return I.getOpcode() == Instruction::SetEQ || |
| 476 | I.getOpcode() == Instruction::SetGE || |
| 477 | I.getOpcode() == Instruction::SetLE; |
| 478 | } |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 479 | |
| 480 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
| 481 | /// function is designed to check a chain of associative operators for a |
| 482 | /// potential to apply a certain optimization. Since the optimization may be |
| 483 | /// applicable if the expression was reassociated, this checks the chain, then |
| 484 | /// reassociates the expression as necessary to expose the optimization |
| 485 | /// opportunity. This makes use of a special Functor, which must define |
| 486 | /// 'shouldApply' and 'apply' methods. |
| 487 | /// |
| 488 | template<typename Functor> |
| 489 | Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { |
| 490 | unsigned Opcode = Root.getOpcode(); |
| 491 | Value *LHS = Root.getOperand(0); |
| 492 | |
| 493 | // Quick check, see if the immediate LHS matches... |
| 494 | if (F.shouldApply(LHS)) |
| 495 | return F.apply(Root); |
| 496 | |
| 497 | // Otherwise, if the LHS is not of the same opcode as the root, return. |
| 498 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 499 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 500 | // Should we apply this transform to the RHS? |
| 501 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); |
| 502 | |
| 503 | // If not to the RHS, check to see if we should apply to the LHS... |
| 504 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { |
| 505 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS |
| 506 | ShouldApply = true; |
| 507 | } |
| 508 | |
| 509 | // If the functor wants to apply the optimization to the RHS of LHSI, |
| 510 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) |
| 511 | if (ShouldApply) { |
| 512 | BasicBlock *BB = Root.getParent(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 513 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 514 | // Now all of the instructions are in the current basic block, go ahead |
| 515 | // and perform the reassociation. |
| 516 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); |
| 517 | |
| 518 | // First move the selected RHS to the LHS of the root... |
| 519 | Root.setOperand(0, LHSI->getOperand(1)); |
| 520 | |
| 521 | // Make what used to be the LHS of the root be the user of the root... |
| 522 | Value *ExtraOperand = TmpLHSI->getOperand(1); |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 523 | if (&Root == TmpLHSI) { |
Chris Lattner | 8953b90 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 524 | Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); |
| 525 | return 0; |
| 526 | } |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 527 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 528 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 529 | TmpLHSI->getParent()->getInstList().remove(TmpLHSI); |
| 530 | BasicBlock::iterator ARI = &Root; ++ARI; |
| 531 | BB->getInstList().insert(ARI, TmpLHSI); // Move TmpLHSI to after Root |
| 532 | ARI = Root; |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 533 | |
| 534 | // Now propagate the ExtraOperand down the chain of instructions until we |
| 535 | // get to LHSI. |
| 536 | while (TmpLHSI != LHSI) { |
| 537 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 538 | // Move the instruction to immediately before the chain we are |
| 539 | // constructing to avoid breaking dominance properties. |
| 540 | NextLHSI->getParent()->getInstList().remove(NextLHSI); |
| 541 | BB->getInstList().insert(ARI, NextLHSI); |
| 542 | ARI = NextLHSI; |
| 543 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 544 | Value *NextOp = NextLHSI->getOperand(1); |
| 545 | NextLHSI->setOperand(1, ExtraOperand); |
| 546 | TmpLHSI = NextLHSI; |
| 547 | ExtraOperand = NextOp; |
| 548 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 549 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 550 | // Now that the instructions are reassociated, have the functor perform |
| 551 | // the transformation... |
| 552 | return F.apply(Root); |
| 553 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 554 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 555 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
| 556 | } |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | |
| 561 | // AddRHS - Implements: X + X --> X << 1 |
| 562 | struct AddRHS { |
| 563 | Value *RHS; |
| 564 | AddRHS(Value *rhs) : RHS(rhs) {} |
| 565 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 566 | Instruction *apply(BinaryOperator &Add) const { |
| 567 | return new ShiftInst(Instruction::Shl, Add.getOperand(0), |
| 568 | ConstantInt::get(Type::UByteTy, 1)); |
| 569 | } |
| 570 | }; |
| 571 | |
| 572 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) |
| 573 | // iff C1&C2 == 0 |
| 574 | struct AddMaskingAnd { |
| 575 | Constant *C2; |
| 576 | AddMaskingAnd(Constant *c) : C2(c) {} |
| 577 | bool shouldApply(Value *LHS) const { |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 578 | ConstantInt *C1; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 579 | return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 580 | ConstantExpr::getAnd(C1, C2)->isNullValue(); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 581 | } |
| 582 | Instruction *apply(BinaryOperator &Add) const { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 583 | return BinaryOperator::createOr(Add.getOperand(0), Add.getOperand(1)); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 584 | } |
| 585 | }; |
| 586 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 587 | static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 588 | InstCombiner *IC) { |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 589 | if (isa<CastInst>(I)) { |
| 590 | if (Constant *SOC = dyn_cast<Constant>(SO)) |
| 591 | return ConstantExpr::getCast(SOC, I.getType()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 592 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 593 | return IC->InsertNewInstBefore(new CastInst(SO, I.getType(), |
| 594 | SO->getName() + ".cast"), I); |
| 595 | } |
| 596 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 597 | // Figure out if the constant is the left or the right argument. |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 598 | bool ConstIsRHS = isa<Constant>(I.getOperand(1)); |
| 599 | Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS)); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 600 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 601 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
| 602 | if (ConstIsRHS) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 603 | return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand); |
| 604 | return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC); |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | Value *Op0 = SO, *Op1 = ConstOperand; |
| 608 | if (!ConstIsRHS) |
| 609 | std::swap(Op0, Op1); |
| 610 | Instruction *New; |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 611 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| 612 | New = BinaryOperator::create(BO->getOpcode(), Op0, Op1,SO->getName()+".op"); |
| 613 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(&I)) |
| 614 | New = new ShiftInst(SI->getOpcode(), Op0, Op1, SO->getName()+".sh"); |
Chris Lattner | f9d9665 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 615 | else { |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 616 | assert(0 && "Unknown binary instruction type!"); |
Chris Lattner | f9d9665 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 617 | abort(); |
| 618 | } |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 619 | return IC->InsertNewInstBefore(New, I); |
| 620 | } |
| 621 | |
| 622 | // FoldOpIntoSelect - Given an instruction with a select as one operand and a |
| 623 | // constant as the other operand, try to fold the binary operator into the |
| 624 | // select arguments. This also works for Cast instructions, which obviously do |
| 625 | // not have a second operand. |
| 626 | static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI, |
| 627 | InstCombiner *IC) { |
| 628 | // Don't modify shared select instructions |
| 629 | if (!SI->hasOneUse()) return 0; |
| 630 | Value *TV = SI->getOperand(1); |
| 631 | Value *FV = SI->getOperand(2); |
| 632 | |
| 633 | if (isa<Constant>(TV) || isa<Constant>(FV)) { |
Chris Lattner | 374e659 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 634 | // Bool selects with constant operands can be folded to logical ops. |
| 635 | if (SI->getType() == Type::BoolTy) return 0; |
| 636 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 637 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC); |
| 638 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC); |
| 639 | |
| 640 | return new SelectInst(SI->getCondition(), SelectTrueVal, |
| 641 | SelectFalseVal); |
| 642 | } |
| 643 | return 0; |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 646 | |
| 647 | /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI |
| 648 | /// node as operand #0, see if we can fold the instruction into the PHI (which |
| 649 | /// is only possible if all operands to the PHI are constants). |
| 650 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { |
| 651 | PHINode *PN = cast<PHINode>(I.getOperand(0)); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 652 | unsigned NumPHIValues = PN->getNumIncomingValues(); |
| 653 | if (!PN->hasOneUse() || NumPHIValues == 0 || |
| 654 | !isa<Constant>(PN->getIncomingValue(0))) return 0; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 655 | |
| 656 | // Check to see if all of the operands of the PHI are constants. If not, we |
| 657 | // cannot do the transformation. |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 658 | for (unsigned i = 1; i != NumPHIValues; ++i) |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 659 | if (!isa<Constant>(PN->getIncomingValue(i))) |
| 660 | return 0; |
| 661 | |
| 662 | // Okay, we can do the transformation: create the new PHI node. |
| 663 | PHINode *NewPN = new PHINode(I.getType(), I.getName()); |
| 664 | I.setName(""); |
Chris Lattner | d8e2018 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 665 | NewPN->reserveOperandSpace(PN->getNumOperands()/2); |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 666 | InsertNewInstBefore(NewPN, *PN); |
| 667 | |
| 668 | // Next, add all of the operands to the PHI. |
| 669 | if (I.getNumOperands() == 2) { |
| 670 | Constant *C = cast<Constant>(I.getOperand(1)); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 671 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 672 | Constant *InV = cast<Constant>(PN->getIncomingValue(i)); |
| 673 | NewPN->addIncoming(ConstantExpr::get(I.getOpcode(), InV, C), |
| 674 | PN->getIncomingBlock(i)); |
| 675 | } |
| 676 | } else { |
| 677 | assert(isa<CastInst>(I) && "Unary op should be a cast!"); |
| 678 | const Type *RetTy = I.getType(); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 679 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 680 | Constant *InV = cast<Constant>(PN->getIncomingValue(i)); |
| 681 | NewPN->addIncoming(ConstantExpr::getCast(InV, RetTy), |
| 682 | PN->getIncomingBlock(i)); |
| 683 | } |
| 684 | } |
| 685 | return ReplaceInstUsesWith(I, NewPN); |
| 686 | } |
| 687 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 688 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 689 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 690 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 691 | |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 692 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 693 | // X + undef -> undef |
| 694 | if (isa<UndefValue>(RHS)) |
| 695 | return ReplaceInstUsesWith(I, RHS); |
| 696 | |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 697 | // X + 0 --> X |
Chris Lattner | 7fde91e | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 698 | if (!I.getType()->isFloatingPoint()) { // NOTE: -0 + +0 = +0. |
| 699 | if (RHSC->isNullValue()) |
| 700 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | da1b152 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 701 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
| 702 | if (CFP->isExactlyValue(-0.0)) |
| 703 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 7fde91e | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 704 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 705 | |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 706 | // X + (signbit) --> X ^ signbit |
| 707 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 708 | unsigned NumBits = CI->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 709 | uint64_t Val = CI->getRawValue() & (1ULL << NumBits)-1; |
Chris Lattner | 33eb909 | 2004-11-05 04:45:43 +0000 | [diff] [blame] | 710 | if (Val == (1ULL << (NumBits-1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 711 | return BinaryOperator::createXor(LHS, RHS); |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 712 | } |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 713 | |
| 714 | if (isa<PHINode>(LHS)) |
| 715 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 716 | return NV; |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 717 | |
| 718 | ConstantInt *XorRHS; |
| 719 | Value *XorLHS; |
| 720 | if (match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) { |
| 721 | unsigned TySizeBits = I.getType()->getPrimitiveSizeInBits(); |
| 722 | int64_t RHSSExt = cast<ConstantInt>(RHSC)->getSExtValue(); |
| 723 | uint64_t RHSZExt = cast<ConstantInt>(RHSC)->getZExtValue(); |
| 724 | |
| 725 | uint64_t C0080Val = 1ULL << 31; |
| 726 | int64_t CFF80Val = -C0080Val; |
| 727 | unsigned Size = 32; |
| 728 | do { |
| 729 | if (TySizeBits > Size) { |
| 730 | bool Found = false; |
| 731 | // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext. |
| 732 | // If we have ADD(XOR(AND(X, 0xFF), 0xF..F80), 0x80), it's a sext. |
| 733 | if (RHSSExt == CFF80Val) { |
| 734 | if (XorRHS->getZExtValue() == C0080Val) |
| 735 | Found = true; |
| 736 | } else if (RHSZExt == C0080Val) { |
| 737 | if (XorRHS->getSExtValue() == CFF80Val) |
| 738 | Found = true; |
| 739 | } |
| 740 | if (Found) { |
| 741 | // This is a sign extend if the top bits are known zero. |
| 742 | Constant *Mask = ConstantInt::getAllOnesValue(XorLHS->getType()); |
| 743 | Mask = ConstantExpr::getShl(Mask, |
| 744 | ConstantInt::get(Type::UByteTy, 64-TySizeBits-Size)); |
| 745 | if (!MaskedValueIsZero(XorLHS, cast<ConstantInt>(Mask))) |
| 746 | Size = 0; // Not a sign ext, but can't be any others either. |
| 747 | goto FoundSExt; |
| 748 | } |
| 749 | } |
| 750 | Size >>= 1; |
| 751 | C0080Val >>= Size; |
| 752 | CFF80Val >>= Size; |
| 753 | } while (Size >= 8); |
| 754 | |
| 755 | FoundSExt: |
| 756 | const Type *MiddleType = 0; |
| 757 | switch (Size) { |
| 758 | default: break; |
| 759 | case 32: MiddleType = Type::IntTy; break; |
| 760 | case 16: MiddleType = Type::ShortTy; break; |
| 761 | case 8: MiddleType = Type::SByteTy; break; |
| 762 | } |
| 763 | if (MiddleType) { |
| 764 | Instruction *NewTrunc = new CastInst(XorLHS, MiddleType, "sext"); |
| 765 | InsertNewInstBefore(NewTrunc, I); |
| 766 | return new CastInst(NewTrunc, I.getType()); |
| 767 | } |
| 768 | } |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 769 | } |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 770 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 771 | // X + X --> X << 1 |
Robert Bocchino | 7b5b86c | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 772 | if (I.getType()->isInteger()) { |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 773 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; |
Chris Lattner | 4706046 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 774 | |
| 775 | if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) { |
| 776 | if (RHSI->getOpcode() == Instruction::Sub) |
| 777 | if (LHS == RHSI->getOperand(1)) // A + (B - A) --> B |
| 778 | return ReplaceInstUsesWith(I, RHSI->getOperand(0)); |
| 779 | } |
| 780 | if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) { |
| 781 | if (LHSI->getOpcode() == Instruction::Sub) |
| 782 | if (RHS == LHSI->getOperand(1)) // (B - A) + A --> B |
| 783 | return ReplaceInstUsesWith(I, LHSI->getOperand(0)); |
| 784 | } |
Robert Bocchino | 7b5b86c | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 785 | } |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 786 | |
Chris Lattner | 147e975 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 787 | // -A + B --> B - A |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 788 | if (Value *V = dyn_castNegVal(LHS)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 789 | return BinaryOperator::createSub(RHS, V); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 790 | |
| 791 | // A + -B --> A - B |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 792 | if (!isa<Constant>(RHS)) |
| 793 | if (Value *V = dyn_castNegVal(RHS)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 794 | return BinaryOperator::createSub(LHS, V); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 795 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 796 | |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 797 | ConstantInt *C2; |
| 798 | if (Value *X = dyn_castFoldableMul(LHS, C2)) { |
| 799 | if (X == RHS) // X*C + X --> X * (C+1) |
| 800 | return BinaryOperator::createMul(RHS, AddOne(C2)); |
| 801 | |
| 802 | // X*C1 + X*C2 --> X * (C1+C2) |
| 803 | ConstantInt *C1; |
| 804 | if (X == dyn_castFoldableMul(RHS, C1)) |
| 805 | return BinaryOperator::createMul(X, ConstantExpr::getAdd(C1, C2)); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | // X + X*C --> X * (C+1) |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 809 | if (dyn_castFoldableMul(RHS, C2) == LHS) |
| 810 | return BinaryOperator::createMul(LHS, AddOne(C2)); |
| 811 | |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 812 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 813 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 814 | if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 815 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) return R; |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 816 | |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 817 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 818 | Value *X; |
| 819 | if (match(LHS, m_Not(m_Value(X)))) { // ~X + C --> (C-1) - X |
| 820 | Constant *C= ConstantExpr::getSub(CRHS, ConstantInt::get(I.getType(), 1)); |
| 821 | return BinaryOperator::createSub(C, X); |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 822 | } |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 823 | |
Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 824 | // (X & FF00) + xx00 -> (X+xx00) & FF00 |
| 825 | if (LHS->hasOneUse() && match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) { |
| 826 | Constant *Anded = ConstantExpr::getAnd(CRHS, C2); |
| 827 | if (Anded == CRHS) { |
| 828 | // See if all bits from the first bit set in the Add RHS up are included |
| 829 | // in the mask. First, get the rightmost bit. |
| 830 | uint64_t AddRHSV = CRHS->getRawValue(); |
| 831 | |
| 832 | // Form a mask of all bits from the lowest bit added through the top. |
| 833 | uint64_t AddRHSHighBits = ~((AddRHSV & -AddRHSV)-1); |
Chris Lattner | 2f1457f | 2005-04-24 17:46:05 +0000 | [diff] [blame] | 834 | AddRHSHighBits &= ~0ULL >> (64-C2->getType()->getPrimitiveSizeInBits()); |
Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 835 | |
| 836 | // See if the and mask includes all of these bits. |
| 837 | uint64_t AddRHSHighBitsAnd = AddRHSHighBits & C2->getRawValue(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 838 | |
Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 839 | if (AddRHSHighBits == AddRHSHighBitsAnd) { |
| 840 | // Okay, the xform is safe. Insert the new add pronto. |
| 841 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, CRHS, |
| 842 | LHS->getName()), I); |
| 843 | return BinaryOperator::createAnd(NewAdd, C2); |
| 844 | } |
| 845 | } |
| 846 | } |
| 847 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 848 | // Try to fold constant add into select arguments. |
| 849 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 850 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 851 | return R; |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 854 | return Changed ? &I : 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Chris Lattner | bdb0ce0 | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 857 | // isSignBit - Return true if the value represented by the constant only has the |
| 858 | // highest order bit set. |
| 859 | static bool isSignBit(ConstantInt *CI) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 860 | unsigned NumBits = CI->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 2f1457f | 2005-04-24 17:46:05 +0000 | [diff] [blame] | 861 | return (CI->getRawValue() & (~0ULL >> (64-NumBits))) == (1ULL << (NumBits-1)); |
Chris Lattner | bdb0ce0 | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 864 | /// RemoveNoopCast - Strip off nonconverting casts from the value. |
| 865 | /// |
| 866 | static Value *RemoveNoopCast(Value *V) { |
| 867 | if (CastInst *CI = dyn_cast<CastInst>(V)) { |
| 868 | const Type *CTy = CI->getType(); |
| 869 | const Type *OpTy = CI->getOperand(0)->getType(); |
| 870 | if (CTy->isInteger() && OpTy->isInteger()) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 871 | if (CTy->getPrimitiveSizeInBits() == OpTy->getPrimitiveSizeInBits()) |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 872 | return RemoveNoopCast(CI->getOperand(0)); |
| 873 | } else if (isa<PointerType>(CTy) && isa<PointerType>(OpTy)) |
| 874 | return RemoveNoopCast(CI->getOperand(0)); |
| 875 | } |
| 876 | return V; |
| 877 | } |
| 878 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 879 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 880 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 881 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 882 | if (Op0 == Op1) // sub X, X -> 0 |
| 883 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 884 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 885 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 886 | if (Value *V = dyn_castNegVal(Op1)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 887 | return BinaryOperator::createAdd(Op0, V); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 888 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 889 | if (isa<UndefValue>(Op0)) |
| 890 | return ReplaceInstUsesWith(I, Op0); // undef - X -> undef |
| 891 | if (isa<UndefValue>(Op1)) |
| 892 | return ReplaceInstUsesWith(I, Op1); // X - undef -> undef |
| 893 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 894 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
| 895 | // Replace (-1 - A) with (~A)... |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 896 | if (C->isAllOnesValue()) |
| 897 | return BinaryOperator::createNot(Op1); |
Chris Lattner | ad3c495 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 898 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 899 | // C - ~X == X + (1+C) |
Reid Spencer | 4fdd96c | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 900 | Value *X = 0; |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 901 | if (match(Op1, m_Not(m_Value(X)))) |
| 902 | return BinaryOperator::createAdd(X, |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 903 | ConstantExpr::getAdd(C, ConstantInt::get(I.getType(), 1))); |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 904 | // -((uint)X >> 31) -> ((int)X >> 31) |
| 905 | // -((int)X >> 31) -> ((uint)X >> 31) |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 906 | if (C->isNullValue()) { |
| 907 | Value *NoopCastedRHS = RemoveNoopCast(Op1); |
| 908 | if (ShiftInst *SI = dyn_cast<ShiftInst>(NoopCastedRHS)) |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 909 | if (SI->getOpcode() == Instruction::Shr) |
| 910 | if (ConstantUInt *CU = dyn_cast<ConstantUInt>(SI->getOperand(1))) { |
| 911 | const Type *NewTy; |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 912 | if (SI->getType()->isSigned()) |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 913 | NewTy = SI->getType()->getUnsignedVersion(); |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 914 | else |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 915 | NewTy = SI->getType()->getSignedVersion(); |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 916 | // 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] | 917 | if (CU->getValue() == SI->getType()->getPrimitiveSizeInBits()-1) { |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 918 | // Ok, the transformation is safe. Insert a cast of the incoming |
| 919 | // value, then the new shift, then the new cast. |
| 920 | Instruction *FirstCast = new CastInst(SI->getOperand(0), NewTy, |
| 921 | SI->getOperand(0)->getName()); |
| 922 | Value *InV = InsertNewInstBefore(FirstCast, I); |
| 923 | Instruction *NewShift = new ShiftInst(Instruction::Shr, FirstCast, |
| 924 | CU, SI->getName()); |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 925 | if (NewShift->getType() == I.getType()) |
| 926 | return NewShift; |
| 927 | else { |
| 928 | InV = InsertNewInstBefore(NewShift, I); |
| 929 | return new CastInst(NewShift, I.getType()); |
| 930 | } |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 931 | } |
| 932 | } |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 933 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 934 | |
| 935 | // Try to fold constant sub into select arguments. |
| 936 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 937 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 938 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 939 | |
| 940 | if (isa<PHINode>(Op0)) |
| 941 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 942 | return NV; |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 945 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
| 946 | if (Op1I->getOpcode() == Instruction::Add && |
| 947 | !Op0->getType()->isFloatingPoint()) { |
Chris Lattner | c7f3c1a | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 948 | if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 949 | return BinaryOperator::createNeg(Op1I->getOperand(1), I.getName()); |
Chris Lattner | c7f3c1a | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 950 | else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 951 | return BinaryOperator::createNeg(Op1I->getOperand(0), I.getName()); |
Chris Lattner | c7f3c1a | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 952 | else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) { |
| 953 | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1))) |
| 954 | // C1-(X+C2) --> (C1-C2)-X |
| 955 | return BinaryOperator::createSub(ConstantExpr::getSub(CI1, CI2), |
| 956 | Op1I->getOperand(0)); |
| 957 | } |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 960 | if (Op1I->hasOneUse()) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 961 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 962 | // is not used by anyone else... |
| 963 | // |
Chris Lattner | c2f0aa5 | 2004-02-02 20:09:56 +0000 | [diff] [blame] | 964 | if (Op1I->getOpcode() == Instruction::Sub && |
| 965 | !Op1I->getType()->isFloatingPoint()) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 966 | // Swap the two operands of the subexpr... |
| 967 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 968 | Op1I->setOperand(0, IIOp1); |
| 969 | Op1I->setOperand(1, IIOp0); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 970 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 971 | // Create the new top level add instruction... |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 972 | return BinaryOperator::createAdd(Op0, Op1); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 976 | // |
| 977 | if (Op1I->getOpcode() == Instruction::And && |
| 978 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 979 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 980 | |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 981 | Value *NewNot = |
| 982 | InsertNewInstBefore(BinaryOperator::createNot(OtherOp, "B.not"), I); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 983 | return BinaryOperator::createAnd(Op0, NewNot); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 984 | } |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 985 | |
Chris Lattner | 0aee4b7 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 986 | // -(X sdiv C) -> (X sdiv -C) |
| 987 | if (Op1I->getOpcode() == Instruction::Div) |
| 988 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0)) |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 989 | if (CSI->isNullValue()) |
Chris Lattner | 0aee4b7 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 990 | if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 991 | return BinaryOperator::createDiv(Op1I->getOperand(0), |
Chris Lattner | 0aee4b7 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 992 | ConstantExpr::getNeg(DivRHS)); |
| 993 | |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 994 | // X - X*C --> X * (1-C) |
Reid Spencer | 4fdd96c | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 995 | ConstantInt *C2 = 0; |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 996 | if (dyn_castFoldableMul(Op1I, C2) == Op0) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 997 | Constant *CP1 = |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 998 | ConstantExpr::getSub(ConstantInt::get(I.getType(), 1), C2); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 999 | return BinaryOperator::createMul(Op0, CP1); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1000 | } |
Chris Lattner | ad3c495 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 1001 | } |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 1002 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1003 | |
Chris Lattner | 4706046 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 1004 | if (!Op0->getType()->isFloatingPoint()) |
| 1005 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 1006 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 1007 | if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X |
| 1008 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 1009 | else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X |
| 1010 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 4706046 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 1011 | } else if (Op0I->getOpcode() == Instruction::Sub) { |
| 1012 | if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y |
| 1013 | return BinaryOperator::createNeg(Op0I->getOperand(1), I.getName()); |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 1014 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1015 | |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1016 | ConstantInt *C1; |
| 1017 | if (Value *X = dyn_castFoldableMul(Op0, C1)) { |
| 1018 | if (X == Op1) { // X*C - X --> X * (C-1) |
| 1019 | Constant *CP1 = ConstantExpr::getSub(C1, ConstantInt::get(I.getType(),1)); |
| 1020 | return BinaryOperator::createMul(Op1, CP1); |
| 1021 | } |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1022 | |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1023 | ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2) |
| 1024 | if (X == dyn_castFoldableMul(Op1, C2)) |
| 1025 | return BinaryOperator::createMul(Op1, ConstantExpr::getSub(C1, C2)); |
| 1026 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1027 | return 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 1030 | /// isSignBitCheck - Given an exploded setcc instruction, return true if it is |
| 1031 | /// really just returns true if the most significant (sign) bit is set. |
| 1032 | static bool isSignBitCheck(unsigned Opcode, Value *LHS, ConstantInt *RHS) { |
| 1033 | if (RHS->getType()->isSigned()) { |
| 1034 | // True if source is LHS < 0 or LHS <= -1 |
| 1035 | return Opcode == Instruction::SetLT && RHS->isNullValue() || |
| 1036 | Opcode == Instruction::SetLE && RHS->isAllOnesValue(); |
| 1037 | } else { |
| 1038 | ConstantUInt *RHSC = cast<ConstantUInt>(RHS); |
| 1039 | // True if source is LHS > 127 or LHS >= 128, where the constants depend on |
| 1040 | // the size of the integer type. |
| 1041 | if (Opcode == Instruction::SetGE) |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1042 | return RHSC->getValue() == |
| 1043 | 1ULL << (RHS->getType()->getPrimitiveSizeInBits()-1); |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 1044 | if (Opcode == Instruction::SetGT) |
| 1045 | return RHSC->getValue() == |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1046 | (1ULL << (RHS->getType()->getPrimitiveSizeInBits()-1))-1; |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 1047 | } |
| 1048 | return false; |
| 1049 | } |
| 1050 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1051 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1052 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1053 | Value *Op0 = I.getOperand(0); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1054 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1055 | if (isa<UndefValue>(I.getOperand(1))) // undef * X -> 0 |
| 1056 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1057 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1058 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1059 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 1060 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 1061 | |
| 1062 | // ((X << C1)*C2) == (X * (C2 << C1)) |
| 1063 | if (ShiftInst *SI = dyn_cast<ShiftInst>(Op0)) |
| 1064 | if (SI->getOpcode() == Instruction::Shl) |
| 1065 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1066 | return BinaryOperator::createMul(SI->getOperand(0), |
| 1067 | ConstantExpr::getShl(CI, ShOp)); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1068 | |
Chris Lattner | cce81be | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 1069 | if (CI->isNullValue()) |
| 1070 | return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 |
| 1071 | if (CI->equalsInt(1)) // X * 1 == X |
| 1072 | return ReplaceInstUsesWith(I, Op0); |
| 1073 | if (CI->isAllOnesValue()) // X * -1 == 0 - X |
Chris Lattner | 35236d8 | 2003-06-25 17:09:20 +0000 | [diff] [blame] | 1074 | return BinaryOperator::createNeg(Op0, I.getName()); |
Chris Lattner | 31ba129 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 1075 | |
Chris Lattner | cce81be | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 1076 | int64_t Val = (int64_t)cast<ConstantInt>(CI)->getRawValue(); |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 1077 | if (isPowerOf2_64(Val)) { // Replace X*(2^C) with X << C |
| 1078 | uint64_t C = Log2_64(Val); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1079 | return new ShiftInst(Instruction::Shl, Op0, |
| 1080 | ConstantUInt::get(Type::UByteTy, C)); |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 1081 | } |
Robert Bocchino | 7b5b86c | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 1082 | } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1083 | if (Op1F->isNullValue()) |
| 1084 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 31ba129 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 1085 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1086 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 1087 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) |
| 1088 | if (Op1F->getValue() == 1.0) |
| 1089 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
| 1090 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1091 | |
| 1092 | // Try to fold constant mul into select arguments. |
| 1093 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1094 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1095 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1096 | |
| 1097 | if (isa<PHINode>(Op0)) |
| 1098 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1099 | return NV; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
Chris Lattner | 934a64cf | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 1102 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
| 1103 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1104 | return BinaryOperator::createMul(Op0v, Op1v); |
Chris Lattner | 934a64cf | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 1105 | |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 1106 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 1107 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 1108 | // See if we can simplify things based on how the boolean was originally |
| 1109 | // formed. |
| 1110 | CastInst *BoolCast = 0; |
| 1111 | if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(0))) |
| 1112 | if (CI->getOperand(0)->getType() == Type::BoolTy) |
| 1113 | BoolCast = CI; |
| 1114 | if (!BoolCast) |
| 1115 | if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(1))) |
| 1116 | if (CI->getOperand(0)->getType() == Type::BoolTy) |
| 1117 | BoolCast = CI; |
| 1118 | if (BoolCast) { |
| 1119 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(BoolCast->getOperand(0))) { |
| 1120 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); |
| 1121 | const Type *SCOpTy = SCIOp0->getType(); |
| 1122 | |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 1123 | // If the setcc is true iff the sign bit of X is set, then convert this |
| 1124 | // multiply into a shift/and combination. |
| 1125 | if (isa<ConstantInt>(SCIOp1) && |
| 1126 | isSignBitCheck(SCI->getOpcode(), SCIOp0, cast<ConstantInt>(SCIOp1))) { |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 1127 | // Shift the X value right to turn it into "all signbits". |
| 1128 | Constant *Amt = ConstantUInt::get(Type::UByteTy, |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1129 | SCOpTy->getPrimitiveSizeInBits()-1); |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 1130 | if (SCIOp0->getType()->isUnsigned()) { |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 1131 | const Type *NewTy = SCIOp0->getType()->getSignedVersion(); |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 1132 | SCIOp0 = InsertNewInstBefore(new CastInst(SCIOp0, NewTy, |
| 1133 | SCIOp0->getName()), I); |
| 1134 | } |
| 1135 | |
| 1136 | Value *V = |
| 1137 | InsertNewInstBefore(new ShiftInst(Instruction::Shr, SCIOp0, Amt, |
| 1138 | BoolCast->getOperand(0)->getName()+ |
| 1139 | ".mask"), I); |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 1140 | |
| 1141 | // If the multiply type is not the same as the source type, sign extend |
| 1142 | // or truncate to the multiply type. |
| 1143 | if (I.getType() != V->getType()) |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 1144 | V = InsertNewInstBefore(new CastInst(V, I.getType(), V->getName()),I); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1145 | |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 1146 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1147 | return BinaryOperator::createAnd(V, OtherOp); |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 1148 | } |
| 1149 | } |
| 1150 | } |
| 1151 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1152 | return Changed ? &I : 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1155 | Instruction *InstCombiner::visitDiv(BinaryOperator &I) { |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1156 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1157 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1158 | if (isa<UndefValue>(Op0)) // undef / X -> 0 |
| 1159 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1160 | if (isa<UndefValue>(Op1)) |
| 1161 | return ReplaceInstUsesWith(I, Op1); // X / undef -> undef |
| 1162 | |
| 1163 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | e20c334 | 2004-04-26 14:01:59 +0000 | [diff] [blame] | 1164 | // div X, 1 == X |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1165 | if (RHS->equalsInt(1)) |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1166 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1167 | |
Chris Lattner | e20c334 | 2004-04-26 14:01:59 +0000 | [diff] [blame] | 1168 | // div X, -1 == -X |
| 1169 | if (RHS->isAllOnesValue()) |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1170 | return BinaryOperator::createNeg(Op0); |
Chris Lattner | e20c334 | 2004-04-26 14:01:59 +0000 | [diff] [blame] | 1171 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1172 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 1173 | if (LHS->getOpcode() == Instruction::Div) |
| 1174 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 1175 | // (X / C1) / C2 -> X / (C1*C2) |
| 1176 | return BinaryOperator::createDiv(LHS->getOperand(0), |
| 1177 | ConstantExpr::getMul(RHS, LHSRHS)); |
| 1178 | } |
| 1179 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1180 | // Check to see if this is an unsigned division with an exact power of 2, |
| 1181 | // if so, convert to a right shift. |
| 1182 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS)) |
| 1183 | if (uint64_t Val = C->getValue()) // Don't break X / 0 |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 1184 | if (isPowerOf2_64(Val)) { |
| 1185 | uint64_t C = Log2_64(Val); |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1186 | return new ShiftInst(Instruction::Shr, Op0, |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1187 | ConstantUInt::get(Type::UByteTy, C)); |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 1188 | } |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1189 | |
Chris Lattner | 4ad0835 | 2004-10-09 02:50:40 +0000 | [diff] [blame] | 1190 | // -X/C -> X/-C |
| 1191 | if (RHS->getType()->isSigned()) |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1192 | if (Value *LHSNeg = dyn_castNegVal(Op0)) |
Chris Lattner | 4ad0835 | 2004-10-09 02:50:40 +0000 | [diff] [blame] | 1193 | return BinaryOperator::createDiv(LHSNeg, ConstantExpr::getNeg(RHS)); |
| 1194 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1195 | if (!RHS->isNullValue()) { |
| 1196 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1197 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1198 | return R; |
| 1199 | if (isa<PHINode>(Op0)) |
| 1200 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1201 | return NV; |
| 1202 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1205 | // If this is 'udiv X, (Cond ? C1, C2)' where C1&C2 are powers of two, |
| 1206 | // transform this into: '(Cond ? (udiv X, C1) : (udiv X, C2))'. |
| 1207 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 1208 | if (ConstantUInt *STO = dyn_cast<ConstantUInt>(SI->getOperand(1))) |
| 1209 | if (ConstantUInt *SFO = dyn_cast<ConstantUInt>(SI->getOperand(2))) { |
| 1210 | if (STO->getValue() == 0) { // Couldn't be this argument. |
| 1211 | I.setOperand(1, SFO); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1212 | return &I; |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1213 | } else if (SFO->getValue() == 0) { |
Chris Lattner | 89dc4f1 | 2005-06-16 04:55:52 +0000 | [diff] [blame] | 1214 | I.setOperand(1, STO); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1215 | return &I; |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Chris Lattner | 4236261 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 1218 | uint64_t TVA = STO->getValue(), FVA = SFO->getValue(); |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 1219 | if (isPowerOf2_64(TVA) && isPowerOf2_64(FVA)) { |
| 1220 | unsigned TSA = Log2_64(TVA), FSA = Log2_64(FVA); |
Chris Lattner | 4236261 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 1221 | Constant *TC = ConstantUInt::get(Type::UByteTy, TSA); |
| 1222 | Instruction *TSI = new ShiftInst(Instruction::Shr, Op0, |
| 1223 | TC, SI->getName()+".t"); |
| 1224 | TSI = InsertNewInstBefore(TSI, I); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1225 | |
Chris Lattner | 4236261 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 1226 | Constant *FC = ConstantUInt::get(Type::UByteTy, FSA); |
| 1227 | Instruction *FSI = new ShiftInst(Instruction::Shr, Op0, |
| 1228 | FC, SI->getName()+".f"); |
| 1229 | FSI = InsertNewInstBefore(FSI, I); |
| 1230 | return new SelectInst(SI->getOperand(0), TSI, FSI); |
| 1231 | } |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1232 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1233 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1234 | // 0 / X == 0, we don't need to preserve faults! |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1235 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1236 | if (LHS->equalsInt(0)) |
| 1237 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1238 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1239 | return 0; |
| 1240 | } |
| 1241 | |
| 1242 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1243 | Instruction *InstCombiner::visitRem(BinaryOperator &I) { |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1244 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 7fd5f07 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 1245 | if (I.getType()->isSigned()) |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1246 | if (Value *RHSNeg = dyn_castNegVal(Op1)) |
Chris Lattner | 98c6bdf | 2004-07-06 07:11:42 +0000 | [diff] [blame] | 1247 | if (!isa<ConstantSInt>(RHSNeg) || |
Chris Lattner | 8e72606 | 2004-08-09 21:05:48 +0000 | [diff] [blame] | 1248 | cast<ConstantSInt>(RHSNeg)->getValue() > 0) { |
Chris Lattner | 7fd5f07 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 1249 | // X % -Y -> X % Y |
| 1250 | AddUsesToWorkList(I); |
| 1251 | I.setOperand(1, RHSNeg); |
| 1252 | return &I; |
| 1253 | } |
| 1254 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1255 | if (isa<UndefValue>(Op0)) // undef % X -> 0 |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1256 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1257 | if (isa<UndefValue>(Op1)) |
| 1258 | return ReplaceInstUsesWith(I, Op1); // X % undef -> undef |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1259 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1260 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1261 | if (RHS->equalsInt(1)) // X % 1 == 0 |
| 1262 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1263 | |
| 1264 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 1265 | // if so, convert to a bitwise and. |
| 1266 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS)) |
| 1267 | 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] | 1268 | if (!(Val & (Val-1))) // Power of 2 |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1269 | return BinaryOperator::createAnd(Op0, |
| 1270 | ConstantUInt::get(I.getType(), Val-1)); |
| 1271 | |
| 1272 | if (!RHS->isNullValue()) { |
| 1273 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1274 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1275 | return R; |
| 1276 | if (isa<PHINode>(Op0)) |
| 1277 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1278 | return NV; |
| 1279 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1282 | // If this is 'urem X, (Cond ? C1, C2)' where C1&C2 are powers of two, |
| 1283 | // transform this into: '(Cond ? (urem X, C1) : (urem X, C2))'. |
| 1284 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 1285 | if (ConstantUInt *STO = dyn_cast<ConstantUInt>(SI->getOperand(1))) |
| 1286 | if (ConstantUInt *SFO = dyn_cast<ConstantUInt>(SI->getOperand(2))) { |
| 1287 | if (STO->getValue() == 0) { // Couldn't be this argument. |
| 1288 | I.setOperand(1, SFO); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1289 | return &I; |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1290 | } else if (SFO->getValue() == 0) { |
| 1291 | I.setOperand(1, STO); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1292 | return &I; |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | if (!(STO->getValue() & (STO->getValue()-1)) && |
| 1296 | !(SFO->getValue() & (SFO->getValue()-1))) { |
| 1297 | Value *TrueAnd = InsertNewInstBefore(BinaryOperator::createAnd(Op0, |
| 1298 | SubOne(STO), SI->getName()+".t"), I); |
| 1299 | Value *FalseAnd = InsertNewInstBefore(BinaryOperator::createAnd(Op0, |
| 1300 | SubOne(SFO), SI->getName()+".f"), I); |
| 1301 | return new SelectInst(SI->getOperand(0), TrueAnd, FalseAnd); |
| 1302 | } |
| 1303 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1304 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1305 | // 0 % X == 0, we don't need to preserve faults! |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1306 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1307 | if (LHS->equalsInt(0)) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1308 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1309 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1310 | return 0; |
| 1311 | } |
| 1312 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1313 | // isMaxValueMinusOne - return true if this is Max-1 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1314 | static bool isMaxValueMinusOne(const ConstantInt *C) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1315 | if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) { |
| 1316 | // Calculate -1 casted to the right type... |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1317 | unsigned TypeBits = C->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1318 | uint64_t Val = ~0ULL; // All ones |
| 1319 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 1320 | return CU->getValue() == Val-1; |
| 1321 | } |
| 1322 | |
| 1323 | const ConstantSInt *CS = cast<ConstantSInt>(C); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1324 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1325 | // Calculate 0111111111..11111 |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1326 | unsigned TypeBits = C->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1327 | int64_t Val = INT64_MAX; // All ones |
| 1328 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 1329 | return CS->getValue() == Val-1; |
| 1330 | } |
| 1331 | |
| 1332 | // isMinValuePlusOne - return true if this is Min+1 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1333 | static bool isMinValuePlusOne(const ConstantInt *C) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1334 | if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) |
| 1335 | return CU->getValue() == 1; |
| 1336 | |
| 1337 | const ConstantSInt *CS = cast<ConstantSInt>(C); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1338 | |
| 1339 | // Calculate 1111111111000000000000 |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1340 | unsigned TypeBits = C->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1341 | int64_t Val = -1; // All ones |
| 1342 | Val <<= TypeBits-1; // Shift over to the right spot |
| 1343 | return CS->getValue() == Val+1; |
| 1344 | } |
| 1345 | |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1346 | // isOneBitSet - Return true if there is exactly one bit set in the specified |
| 1347 | // constant. |
| 1348 | static bool isOneBitSet(const ConstantInt *CI) { |
| 1349 | uint64_t V = CI->getRawValue(); |
| 1350 | return V && (V & (V-1)) == 0; |
| 1351 | } |
| 1352 | |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 1353 | #if 0 // Currently unused |
| 1354 | // isLowOnes - Return true if the constant is of the form 0+1+. |
| 1355 | static bool isLowOnes(const ConstantInt *CI) { |
| 1356 | uint64_t V = CI->getRawValue(); |
| 1357 | |
| 1358 | // There won't be bits set in parts that the type doesn't contain. |
| 1359 | V &= ConstantInt::getAllOnesValue(CI->getType())->getRawValue(); |
| 1360 | |
| 1361 | uint64_t U = V+1; // If it is low ones, this should be a power of two. |
| 1362 | return U && V && (U & V) == 0; |
| 1363 | } |
| 1364 | #endif |
| 1365 | |
| 1366 | // isHighOnes - Return true if the constant is of the form 1+0+. |
| 1367 | // This is the same as lowones(~X). |
| 1368 | static bool isHighOnes(const ConstantInt *CI) { |
| 1369 | uint64_t V = ~CI->getRawValue(); |
Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 1370 | if (~V == 0) return false; // 0's does not match "1+" |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 1371 | |
| 1372 | // There won't be bits set in parts that the type doesn't contain. |
| 1373 | V &= ConstantInt::getAllOnesValue(CI->getType())->getRawValue(); |
| 1374 | |
| 1375 | uint64_t U = V+1; // If it is low ones, this should be a power of two. |
| 1376 | return U && V && (U & V) == 0; |
| 1377 | } |
| 1378 | |
| 1379 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1380 | /// getSetCondCode - Encode a setcc opcode into a three bit mask. These bits |
| 1381 | /// are carefully arranged to allow folding of expressions such as: |
| 1382 | /// |
| 1383 | /// (A < B) | (A > B) --> (A != B) |
| 1384 | /// |
| 1385 | /// Bit value '4' represents that the comparison is true if A > B, bit value '2' |
| 1386 | /// represents that the comparison is true if A == B, and bit value '1' is true |
| 1387 | /// if A < B. |
| 1388 | /// |
| 1389 | static unsigned getSetCondCode(const SetCondInst *SCI) { |
| 1390 | switch (SCI->getOpcode()) { |
| 1391 | // False -> 0 |
| 1392 | case Instruction::SetGT: return 1; |
| 1393 | case Instruction::SetEQ: return 2; |
| 1394 | case Instruction::SetGE: return 3; |
| 1395 | case Instruction::SetLT: return 4; |
| 1396 | case Instruction::SetNE: return 5; |
| 1397 | case Instruction::SetLE: return 6; |
| 1398 | // True -> 7 |
| 1399 | default: |
| 1400 | assert(0 && "Invalid SetCC opcode!"); |
| 1401 | return 0; |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | /// getSetCCValue - This is the complement of getSetCondCode, which turns an |
| 1406 | /// opcode and two operands into either a constant true or false, or a brand new |
| 1407 | /// SetCC instruction. |
| 1408 | static Value *getSetCCValue(unsigned Opcode, Value *LHS, Value *RHS) { |
| 1409 | switch (Opcode) { |
| 1410 | case 0: return ConstantBool::False; |
| 1411 | case 1: return new SetCondInst(Instruction::SetGT, LHS, RHS); |
| 1412 | case 2: return new SetCondInst(Instruction::SetEQ, LHS, RHS); |
| 1413 | case 3: return new SetCondInst(Instruction::SetGE, LHS, RHS); |
| 1414 | case 4: return new SetCondInst(Instruction::SetLT, LHS, RHS); |
| 1415 | case 5: return new SetCondInst(Instruction::SetNE, LHS, RHS); |
| 1416 | case 6: return new SetCondInst(Instruction::SetLE, LHS, RHS); |
| 1417 | case 7: return ConstantBool::True; |
| 1418 | default: assert(0 && "Illegal SetCCCode!"); return 0; |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | // FoldSetCCLogical - Implements (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B) |
| 1423 | struct FoldSetCCLogical { |
| 1424 | InstCombiner &IC; |
| 1425 | Value *LHS, *RHS; |
| 1426 | FoldSetCCLogical(InstCombiner &ic, SetCondInst *SCI) |
| 1427 | : IC(ic), LHS(SCI->getOperand(0)), RHS(SCI->getOperand(1)) {} |
| 1428 | bool shouldApply(Value *V) const { |
| 1429 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(V)) |
| 1430 | return (SCI->getOperand(0) == LHS && SCI->getOperand(1) == RHS || |
| 1431 | SCI->getOperand(0) == RHS && SCI->getOperand(1) == LHS); |
| 1432 | return false; |
| 1433 | } |
| 1434 | Instruction *apply(BinaryOperator &Log) const { |
| 1435 | SetCondInst *SCI = cast<SetCondInst>(Log.getOperand(0)); |
| 1436 | if (SCI->getOperand(0) != LHS) { |
| 1437 | assert(SCI->getOperand(1) == LHS); |
| 1438 | SCI->swapOperands(); // Swap the LHS and RHS of the SetCC |
| 1439 | } |
| 1440 | |
| 1441 | unsigned LHSCode = getSetCondCode(SCI); |
| 1442 | unsigned RHSCode = getSetCondCode(cast<SetCondInst>(Log.getOperand(1))); |
| 1443 | unsigned Code; |
| 1444 | switch (Log.getOpcode()) { |
| 1445 | case Instruction::And: Code = LHSCode & RHSCode; break; |
| 1446 | case Instruction::Or: Code = LHSCode | RHSCode; break; |
| 1447 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; |
Chris Lattner | 2caaaba | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 1448 | default: assert(0 && "Illegal logical opcode!"); return 0; |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | Value *RV = getSetCCValue(Code, LHS, RHS); |
| 1452 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 1453 | return I; |
| 1454 | // Otherwise, it's a constant boolean value... |
| 1455 | return IC.ReplaceInstUsesWith(Log, RV); |
| 1456 | } |
| 1457 | }; |
| 1458 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1459 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
| 1460 | // the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'. Op is |
| 1461 | // guaranteed to be either a shift instruction or a binary operator. |
| 1462 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
| 1463 | ConstantIntegral *OpRHS, |
| 1464 | ConstantIntegral *AndRHS, |
| 1465 | BinaryOperator &TheAnd) { |
| 1466 | Value *X = Op->getOperand(0); |
Chris Lattner | fcf21a7 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 1467 | Constant *Together = 0; |
| 1468 | if (!isa<ShiftInst>(Op)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1469 | Together = ConstantExpr::getAnd(AndRHS, OpRHS); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1470 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1471 | switch (Op->getOpcode()) { |
| 1472 | case Instruction::Xor: |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1473 | if (Op->hasOneUse()) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1474 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
| 1475 | std::string OpName = Op->getName(); Op->setName(""); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1476 | Instruction *And = BinaryOperator::createAnd(X, AndRHS, OpName); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1477 | InsertNewInstBefore(And, TheAnd); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1478 | return BinaryOperator::createXor(And, Together); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1479 | } |
| 1480 | break; |
| 1481 | case Instruction::Or: |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1482 | if (Together == AndRHS) // (X | C) & C --> C |
| 1483 | return ReplaceInstUsesWith(TheAnd, AndRHS); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1484 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1485 | if (Op->hasOneUse() && Together != OpRHS) { |
| 1486 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 |
| 1487 | std::string Op0Name = Op->getName(); Op->setName(""); |
| 1488 | Instruction *Or = BinaryOperator::createOr(X, Together, Op0Name); |
| 1489 | InsertNewInstBefore(Or, TheAnd); |
| 1490 | return BinaryOperator::createAnd(Or, AndRHS); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1491 | } |
| 1492 | break; |
| 1493 | case Instruction::Add: |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1494 | if (Op->hasOneUse()) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1495 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 1496 | // of the bit. First thing to check is to see if this AND is with a |
| 1497 | // single bit constant. |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1498 | uint64_t AndRHSV = cast<ConstantInt>(AndRHS)->getRawValue(); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1499 | |
| 1500 | // Clear bits that are not part of the constant. |
Chris Lattner | 2f1457f | 2005-04-24 17:46:05 +0000 | [diff] [blame] | 1501 | AndRHSV &= ~0ULL >> (64-AndRHS->getType()->getPrimitiveSizeInBits()); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1502 | |
| 1503 | // If there is only one bit set... |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1504 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1505 | // Ok, at this point, we know that we are masking the result of the |
| 1506 | // ADD down to exactly one bit. If the constant we are adding has |
| 1507 | // no bits set below this bit, then we can eliminate the ADD. |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1508 | uint64_t AddRHS = cast<ConstantInt>(OpRHS)->getRawValue(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1509 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1510 | // Check to see if any bits below the one bit set in AndRHSV are set. |
| 1511 | if ((AddRHS & (AndRHSV-1)) == 0) { |
| 1512 | // If not, the only thing that can effect the output of the AND is |
| 1513 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 1514 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 1515 | // no effect. |
| 1516 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop |
| 1517 | TheAnd.setOperand(0, X); |
| 1518 | return &TheAnd; |
| 1519 | } else { |
| 1520 | std::string Name = Op->getName(); Op->setName(""); |
| 1521 | // Pull the XOR out of the AND. |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1522 | Instruction *NewAnd = BinaryOperator::createAnd(X, AndRHS, Name); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1523 | InsertNewInstBefore(NewAnd, TheAnd); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1524 | return BinaryOperator::createXor(NewAnd, AndRHS); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1525 | } |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | break; |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1530 | |
| 1531 | case Instruction::Shl: { |
| 1532 | // We know that the AND will not produce any of the bits shifted in, so if |
| 1533 | // the anded constant includes them, clear them now! |
| 1534 | // |
| 1535 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1536 | Constant *ShlMask = ConstantExpr::getShl(AllOne, OpRHS); |
| 1537 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShlMask); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1538 | |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1539 | if (CI == ShlMask) { // Masking out bits that the shift already masks |
| 1540 | return ReplaceInstUsesWith(TheAnd, Op); // No need for the and. |
| 1541 | } else if (CI != AndRHS) { // Reducing bits set in and. |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1542 | TheAnd.setOperand(1, CI); |
| 1543 | return &TheAnd; |
| 1544 | } |
| 1545 | break; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1546 | } |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1547 | case Instruction::Shr: |
| 1548 | // We know that the AND will not produce any of the bits shifted in, so if |
| 1549 | // the anded constant includes them, clear them now! This only applies to |
| 1550 | // unsigned shifts, because a signed shr may bring in set bits! |
| 1551 | // |
| 1552 | if (AndRHS->getType()->isUnsigned()) { |
| 1553 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1554 | Constant *ShrMask = ConstantExpr::getShr(AllOne, OpRHS); |
| 1555 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask); |
| 1556 | |
| 1557 | if (CI == ShrMask) { // Masking out bits that the shift already masks. |
| 1558 | return ReplaceInstUsesWith(TheAnd, Op); |
| 1559 | } else if (CI != AndRHS) { |
| 1560 | TheAnd.setOperand(1, CI); // Reduce bits set in and cst. |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1561 | return &TheAnd; |
| 1562 | } |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1563 | } else { // Signed shr. |
| 1564 | // See if this is shifting in some sign extension, then masking it out |
| 1565 | // with an and. |
| 1566 | if (Op->hasOneUse()) { |
| 1567 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
| 1568 | Constant *ShrMask = ConstantExpr::getUShr(AllOne, OpRHS); |
| 1569 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask); |
Chris Lattner | 5c3c21e | 2004-10-22 04:53:16 +0000 | [diff] [blame] | 1570 | if (CI == AndRHS) { // Masking out bits shifted in. |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1571 | // Make the argument unsigned. |
| 1572 | Value *ShVal = Op->getOperand(0); |
| 1573 | ShVal = InsertCastBefore(ShVal, |
| 1574 | ShVal->getType()->getUnsignedVersion(), |
| 1575 | TheAnd); |
| 1576 | ShVal = InsertNewInstBefore(new ShiftInst(Instruction::Shr, ShVal, |
| 1577 | OpRHS, Op->getName()), |
| 1578 | TheAnd); |
Chris Lattner | 70c2039 | 2004-10-27 05:57:15 +0000 | [diff] [blame] | 1579 | Value *AndRHS2 = ConstantExpr::getCast(AndRHS, ShVal->getType()); |
| 1580 | ShVal = InsertNewInstBefore(BinaryOperator::createAnd(ShVal, AndRHS2, |
| 1581 | TheAnd.getName()), |
| 1582 | TheAnd); |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1583 | return new CastInst(ShVal, Op->getType()); |
| 1584 | } |
| 1585 | } |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1586 | } |
| 1587 | break; |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1588 | } |
| 1589 | return 0; |
| 1590 | } |
| 1591 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1592 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 1593 | /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is |
| 1594 | /// true, otherwise (V < Lo || V >= Hi). In pratice, we emit the more efficient |
| 1595 | /// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. IB is the location to |
| 1596 | /// insert new instructions. |
| 1597 | Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
| 1598 | bool Inside, Instruction &IB) { |
| 1599 | assert(cast<ConstantBool>(ConstantExpr::getSetLE(Lo, Hi))->getValue() && |
| 1600 | "Lo is not <= Hi in range emission code!"); |
| 1601 | if (Inside) { |
| 1602 | if (Lo == Hi) // Trivially false. |
| 1603 | return new SetCondInst(Instruction::SetNE, V, V); |
| 1604 | if (cast<ConstantIntegral>(Lo)->isMinValue()) |
| 1605 | return new SetCondInst(Instruction::SetLT, V, Hi); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1606 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 1607 | Constant *AddCST = ConstantExpr::getNeg(Lo); |
| 1608 | Instruction *Add = BinaryOperator::createAdd(V, AddCST,V->getName()+".off"); |
| 1609 | InsertNewInstBefore(Add, IB); |
| 1610 | // Convert to unsigned for the comparison. |
| 1611 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 1612 | Value *OffsetVal = InsertCastBefore(Add, UnsType, IB); |
| 1613 | AddCST = ConstantExpr::getAdd(AddCST, Hi); |
| 1614 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 1615 | return new SetCondInst(Instruction::SetLT, OffsetVal, AddCST); |
| 1616 | } |
| 1617 | |
| 1618 | if (Lo == Hi) // Trivially true. |
| 1619 | return new SetCondInst(Instruction::SetEQ, V, V); |
| 1620 | |
| 1621 | Hi = SubOne(cast<ConstantInt>(Hi)); |
| 1622 | if (cast<ConstantIntegral>(Lo)->isMinValue()) // V < 0 || V >= Hi ->'V > Hi-1' |
| 1623 | return new SetCondInst(Instruction::SetGT, V, Hi); |
| 1624 | |
| 1625 | // Emit X-Lo > Hi-Lo-1 |
| 1626 | Constant *AddCST = ConstantExpr::getNeg(Lo); |
| 1627 | Instruction *Add = BinaryOperator::createAdd(V, AddCST, V->getName()+".off"); |
| 1628 | InsertNewInstBefore(Add, IB); |
| 1629 | // Convert to unsigned for the comparison. |
| 1630 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 1631 | Value *OffsetVal = InsertCastBefore(Add, UnsType, IB); |
| 1632 | AddCST = ConstantExpr::getAdd(AddCST, Hi); |
| 1633 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 1634 | return new SetCondInst(Instruction::SetGT, OffsetVal, AddCST); |
| 1635 | } |
| 1636 | |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 1637 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 1638 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 1639 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 1640 | // not, since all 1s are not contiguous. |
| 1641 | static bool isRunOfOnes(ConstantIntegral *Val, unsigned &MB, unsigned &ME) { |
| 1642 | uint64_t V = Val->getRawValue(); |
| 1643 | if (!isShiftedMask_64(V)) return false; |
| 1644 | |
| 1645 | // look for the first zero bit after the run of ones |
| 1646 | MB = 64-CountLeadingZeros_64((V - 1) ^ V); |
| 1647 | // look for the first non-zero bit |
| 1648 | ME = 64-CountLeadingZeros_64(V); |
| 1649 | return true; |
| 1650 | } |
| 1651 | |
| 1652 | |
| 1653 | |
| 1654 | /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask, |
| 1655 | /// where isSub determines whether the operator is a sub. If we can fold one of |
| 1656 | /// the following xforms: |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 1657 | /// |
| 1658 | /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask |
| 1659 | /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 1660 | /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 1661 | /// |
| 1662 | /// return (A +/- B). |
| 1663 | /// |
| 1664 | Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS, |
| 1665 | ConstantIntegral *Mask, bool isSub, |
| 1666 | Instruction &I) { |
| 1667 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
| 1668 | if (!LHSI || LHSI->getNumOperands() != 2 || |
| 1669 | !isa<ConstantInt>(LHSI->getOperand(1))) return 0; |
| 1670 | |
| 1671 | ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1)); |
| 1672 | |
| 1673 | switch (LHSI->getOpcode()) { |
| 1674 | default: return 0; |
| 1675 | case Instruction::And: |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 1676 | if (ConstantExpr::getAnd(N, Mask) == Mask) { |
| 1677 | // If the AndRHS is a power of two minus one (0+1+), this is simple. |
| 1678 | if ((Mask->getRawValue() & Mask->getRawValue()+1) == 0) |
| 1679 | break; |
| 1680 | |
| 1681 | // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+ |
| 1682 | // part, we don't need any explicit masks to take them out of A. If that |
| 1683 | // is all N is, ignore it. |
| 1684 | unsigned MB, ME; |
| 1685 | if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive |
| 1686 | Constant *Mask = ConstantInt::getAllOnesValue(RHS->getType()); |
| 1687 | Mask = ConstantExpr::getUShr(Mask, |
| 1688 | ConstantInt::get(Type::UByteTy, |
| 1689 | (64-MB+1))); |
| 1690 | if (MaskedValueIsZero(RHS, cast<ConstantIntegral>(Mask))) |
| 1691 | break; |
| 1692 | } |
| 1693 | } |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 1694 | return 0; |
| 1695 | case Instruction::Or: |
| 1696 | case Instruction::Xor: |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 1697 | // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0 |
| 1698 | if ((Mask->getRawValue() & Mask->getRawValue()+1) == 0 && |
| 1699 | ConstantExpr::getAnd(N, Mask)->isNullValue()) |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 1700 | break; |
| 1701 | return 0; |
| 1702 | } |
| 1703 | |
| 1704 | Instruction *New; |
| 1705 | if (isSub) |
| 1706 | New = BinaryOperator::createSub(LHSI->getOperand(0), RHS, "fold"); |
| 1707 | else |
| 1708 | New = BinaryOperator::createAdd(LHSI->getOperand(0), RHS, "fold"); |
| 1709 | return InsertNewInstBefore(New, I); |
| 1710 | } |
| 1711 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 1712 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1713 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1714 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1715 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1716 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1717 | if (isa<UndefValue>(Op1)) // X & undef -> 0 |
| 1718 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1719 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1720 | // and X, X = X |
| 1721 | if (Op0 == Op1) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1722 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1723 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1724 | if (ConstantIntegral *AndRHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1725 | // and X, -1 == X |
| 1726 | if (AndRHS->isAllOnesValue()) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1727 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 38a1b00 | 2005-10-26 17:18:16 +0000 | [diff] [blame^] | 1728 | |
| 1729 | // and (and X, c1), c2 -> and (x, c1&c2). Handle this case here, before |
| 1730 | // calling MaskedValueIsZero, to avoid inefficient cases where we traipse |
| 1731 | // through many levels of ands. |
| 1732 | { |
| 1733 | Value *X; ConstantInt *C1; |
| 1734 | if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1)))) |
| 1735 | return BinaryOperator::createAnd(X, ConstantExpr::getAnd(C1, AndRHS)); |
| 1736 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1737 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1738 | if (MaskedValueIsZero(Op0, AndRHS)) // LHS & RHS == 0 |
| 1739 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1740 | |
| 1741 | // If the mask is not masking out any bits, there is no reason to do the |
| 1742 | // and in the first place. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1743 | ConstantIntegral *NotAndRHS = |
Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1744 | cast<ConstantIntegral>(ConstantExpr::getNot(AndRHS)); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1745 | if (MaskedValueIsZero(Op0, NotAndRHS)) |
Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1746 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1747 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1748 | // Optimize a variety of ((val OP C1) & C2) combinations... |
| 1749 | if (isa<BinaryOperator>(Op0) || isa<ShiftInst>(Op0)) { |
| 1750 | Instruction *Op0I = cast<Instruction>(Op0); |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1751 | Value *Op0LHS = Op0I->getOperand(0); |
| 1752 | Value *Op0RHS = Op0I->getOperand(1); |
| 1753 | switch (Op0I->getOpcode()) { |
| 1754 | case Instruction::Xor: |
| 1755 | case Instruction::Or: |
| 1756 | // (X ^ V) & C2 --> (X & C2) iff (V & C2) == 0 |
| 1757 | // (X | V) & C2 --> (X & C2) iff (V & C2) == 0 |
| 1758 | if (MaskedValueIsZero(Op0LHS, AndRHS)) |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1759 | return BinaryOperator::createAnd(Op0RHS, AndRHS); |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1760 | if (MaskedValueIsZero(Op0RHS, AndRHS)) |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1761 | return BinaryOperator::createAnd(Op0LHS, AndRHS); |
Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1762 | |
| 1763 | // If the mask is only needed on one incoming arm, push it up. |
| 1764 | if (Op0I->hasOneUse()) { |
| 1765 | if (MaskedValueIsZero(Op0LHS, NotAndRHS)) { |
| 1766 | // Not masking anything out for the LHS, move to RHS. |
| 1767 | Instruction *NewRHS = BinaryOperator::createAnd(Op0RHS, AndRHS, |
| 1768 | Op0RHS->getName()+".masked"); |
| 1769 | InsertNewInstBefore(NewRHS, I); |
| 1770 | return BinaryOperator::create( |
| 1771 | cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1772 | } |
Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1773 | if (!isa<Constant>(NotAndRHS) && |
| 1774 | MaskedValueIsZero(Op0RHS, NotAndRHS)) { |
| 1775 | // Not masking anything out for the RHS, move to LHS. |
| 1776 | Instruction *NewLHS = BinaryOperator::createAnd(Op0LHS, AndRHS, |
| 1777 | Op0LHS->getName()+".masked"); |
| 1778 | InsertNewInstBefore(NewLHS, I); |
| 1779 | return BinaryOperator::create( |
| 1780 | cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS); |
| 1781 | } |
| 1782 | } |
| 1783 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1784 | break; |
| 1785 | case Instruction::And: |
| 1786 | // (X & V) & C2 --> 0 iff (V & C2) == 0 |
| 1787 | if (MaskedValueIsZero(Op0LHS, AndRHS) || |
| 1788 | MaskedValueIsZero(Op0RHS, AndRHS)) |
| 1789 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1790 | break; |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 1791 | case Instruction::Add: |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 1792 | // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS. |
| 1793 | // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 1794 | // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 1795 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I)) |
| 1796 | return BinaryOperator::createAnd(V, AndRHS); |
| 1797 | if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I)) |
| 1798 | return BinaryOperator::createAnd(V, AndRHS); // Add commutes |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 1799 | break; |
| 1800 | |
| 1801 | case Instruction::Sub: |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 1802 | // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS. |
| 1803 | // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 1804 | // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 1805 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I)) |
| 1806 | return BinaryOperator::createAnd(V, AndRHS); |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 1807 | break; |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
Chris Lattner | 16464b3 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 1810 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1811 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1812 | return Res; |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1813 | } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
| 1814 | const Type *SrcTy = CI->getOperand(0)->getType(); |
| 1815 | |
Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 1816 | // If this is an integer truncation or change from signed-to-unsigned, and |
| 1817 | // if the source is an and/or with immediate, transform it. This |
| 1818 | // frequently occurs for bitfield accesses. |
| 1819 | if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) { |
| 1820 | if (SrcTy->getPrimitiveSizeInBits() >= |
| 1821 | I.getType()->getPrimitiveSizeInBits() && |
| 1822 | CastOp->getNumOperands() == 2) |
| 1823 | if (ConstantInt *AndCI =dyn_cast<ConstantInt>(CastOp->getOperand(1))) |
| 1824 | if (CastOp->getOpcode() == Instruction::And) { |
| 1825 | // Change: and (cast (and X, C1) to T), C2 |
| 1826 | // into : and (cast X to T), trunc(C1)&C2 |
| 1827 | // This will folds the two ands together, which may allow other |
| 1828 | // simplifications. |
| 1829 | Instruction *NewCast = |
| 1830 | new CastInst(CastOp->getOperand(0), I.getType(), |
| 1831 | CastOp->getName()+".shrunk"); |
| 1832 | NewCast = InsertNewInstBefore(NewCast, I); |
| 1833 | |
| 1834 | Constant *C3=ConstantExpr::getCast(AndCI, I.getType());//trunc(C1) |
| 1835 | C3 = ConstantExpr::getAnd(C3, AndRHS); // trunc(C1)&C2 |
| 1836 | return BinaryOperator::createAnd(NewCast, C3); |
| 1837 | } else if (CastOp->getOpcode() == Instruction::Or) { |
| 1838 | // Change: and (cast (or X, C1) to T), C2 |
| 1839 | // into : trunc(C1)&C2 iff trunc(C1)&C2 == C2 |
| 1840 | Constant *C3=ConstantExpr::getCast(AndCI, I.getType());//trunc(C1) |
| 1841 | if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS) // trunc(C1)&C2 |
| 1842 | return ReplaceInstUsesWith(I, AndRHS); |
| 1843 | } |
| 1844 | } |
| 1845 | |
| 1846 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1847 | // If this is an integer sign or zero extension instruction. |
| 1848 | if (SrcTy->isIntegral() && |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1849 | SrcTy->getPrimitiveSizeInBits() < |
| 1850 | CI->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1851 | |
| 1852 | if (SrcTy->isUnsigned()) { |
| 1853 | // See if this and is clearing out bits that are known to be zero |
| 1854 | // anyway (due to the zero extension). |
| 1855 | Constant *Mask = ConstantIntegral::getAllOnesValue(SrcTy); |
| 1856 | Mask = ConstantExpr::getZeroExtend(Mask, CI->getType()); |
| 1857 | Constant *Result = ConstantExpr::getAnd(Mask, AndRHS); |
| 1858 | if (Result == Mask) // The "and" isn't doing anything, remove it. |
| 1859 | return ReplaceInstUsesWith(I, CI); |
| 1860 | if (Result != AndRHS) { // Reduce the and RHS constant. |
| 1861 | I.setOperand(1, Result); |
| 1862 | return &I; |
| 1863 | } |
| 1864 | |
| 1865 | } else { |
| 1866 | if (CI->hasOneUse() && SrcTy->isInteger()) { |
| 1867 | // We can only do this if all of the sign bits brought in are masked |
| 1868 | // out. Compute this by first getting 0000011111, then inverting |
| 1869 | // it. |
| 1870 | Constant *Mask = ConstantIntegral::getAllOnesValue(SrcTy); |
| 1871 | Mask = ConstantExpr::getZeroExtend(Mask, CI->getType()); |
| 1872 | Mask = ConstantExpr::getNot(Mask); // 1's in the new bits. |
| 1873 | if (ConstantExpr::getAnd(Mask, AndRHS)->isNullValue()) { |
| 1874 | // If the and is clearing all of the sign bits, change this to a |
| 1875 | // zero extension cast. To do this, cast the cast input to |
| 1876 | // unsigned, then to the requested size. |
| 1877 | Value *CastOp = CI->getOperand(0); |
| 1878 | Instruction *NC = |
| 1879 | new CastInst(CastOp, CastOp->getType()->getUnsignedVersion(), |
| 1880 | CI->getName()+".uns"); |
| 1881 | NC = InsertNewInstBefore(NC, I); |
| 1882 | // Finally, insert a replacement for CI. |
| 1883 | NC = new CastInst(NC, CI->getType(), CI->getName()); |
| 1884 | CI->setName(""); |
| 1885 | NC = InsertNewInstBefore(NC, I); |
| 1886 | WorkList.push_back(CI); // Delete CI later. |
| 1887 | I.setOperand(0, NC); |
| 1888 | return &I; // The AND operand was modified. |
| 1889 | } |
| 1890 | } |
| 1891 | } |
| 1892 | } |
Chris Lattner | 33217db | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 1893 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1894 | |
| 1895 | // Try to fold constant and into select arguments. |
| 1896 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1897 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1898 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1899 | if (isa<PHINode>(Op0)) |
| 1900 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1901 | return NV; |
Chris Lattner | 49b47ae | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1904 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 1905 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1906 | |
Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 1907 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
| 1908 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1909 | |
Misha Brukman | 9c003d8 | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 1910 | // (~A & ~B) == (~(A | B)) - De Morgan's Law |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1911 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1912 | Instruction *Or = BinaryOperator::createOr(Op0NotVal, Op1NotVal, |
| 1913 | I.getName()+".demorgan"); |
Chris Lattner | 49b47ae | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 1914 | InsertNewInstBefore(Or, I); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1915 | return BinaryOperator::createNot(Or); |
| 1916 | } |
| 1917 | |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 1918 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(Op1)) { |
| 1919 | // (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B) |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1920 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 1921 | return R; |
| 1922 | |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 1923 | Value *LHSVal, *RHSVal; |
| 1924 | ConstantInt *LHSCst, *RHSCst; |
| 1925 | Instruction::BinaryOps LHSCC, RHSCC; |
| 1926 | if (match(Op0, m_SetCond(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 1927 | if (match(RHS, m_SetCond(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 1928 | if (LHSVal == RHSVal && // Found (X setcc C1) & (X setcc C2) |
| 1929 | // Set[GL]E X, CST is folded to Set[GL]T elsewhere. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1930 | LHSCC != Instruction::SetGE && LHSCC != Instruction::SetLE && |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 1931 | RHSCC != Instruction::SetGE && RHSCC != Instruction::SetLE) { |
| 1932 | // Ensure that the larger constant is on the RHS. |
| 1933 | Constant *Cmp = ConstantExpr::getSetGT(LHSCst, RHSCst); |
| 1934 | SetCondInst *LHS = cast<SetCondInst>(Op0); |
| 1935 | if (cast<ConstantBool>(Cmp)->getValue()) { |
| 1936 | std::swap(LHS, RHS); |
| 1937 | std::swap(LHSCst, RHSCst); |
| 1938 | std::swap(LHSCC, RHSCC); |
| 1939 | } |
| 1940 | |
| 1941 | // At this point, we know we have have two setcc instructions |
| 1942 | // comparing a value against two constants and and'ing the result |
| 1943 | // together. Because of the above check, we know that we only have |
| 1944 | // SetEQ, SetNE, SetLT, and SetGT here. We also know (from the |
| 1945 | // FoldSetCCLogical check above), that the two constants are not |
| 1946 | // equal. |
| 1947 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 1948 | |
| 1949 | switch (LHSCC) { |
| 1950 | default: assert(0 && "Unknown integer condition code!"); |
| 1951 | case Instruction::SetEQ: |
| 1952 | switch (RHSCC) { |
| 1953 | default: assert(0 && "Unknown integer condition code!"); |
| 1954 | case Instruction::SetEQ: // (X == 13 & X == 15) -> false |
| 1955 | case Instruction::SetGT: // (X == 13 & X > 15) -> false |
| 1956 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1957 | case Instruction::SetNE: // (X == 13 & X != 15) -> X == 13 |
| 1958 | case Instruction::SetLT: // (X == 13 & X < 15) -> X == 13 |
| 1959 | return ReplaceInstUsesWith(I, LHS); |
| 1960 | } |
| 1961 | case Instruction::SetNE: |
| 1962 | switch (RHSCC) { |
| 1963 | default: assert(0 && "Unknown integer condition code!"); |
| 1964 | case Instruction::SetLT: |
| 1965 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X < 14) -> X < 13 |
| 1966 | return new SetCondInst(Instruction::SetLT, LHSVal, LHSCst); |
| 1967 | break; // (X != 13 & X < 15) -> no change |
| 1968 | case Instruction::SetEQ: // (X != 13 & X == 15) -> X == 15 |
| 1969 | case Instruction::SetGT: // (X != 13 & X > 15) -> X > 15 |
| 1970 | return ReplaceInstUsesWith(I, RHS); |
| 1971 | case Instruction::SetNE: |
| 1972 | if (LHSCst == SubOne(RHSCst)) {// (X != 13 & X != 14) -> X-13 >u 1 |
| 1973 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 1974 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 1975 | LHSVal->getName()+".off"); |
| 1976 | InsertNewInstBefore(Add, I); |
| 1977 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 1978 | Value *OffsetVal = InsertCastBefore(Add, UnsType, I); |
| 1979 | AddCST = ConstantExpr::getSub(RHSCst, LHSCst); |
| 1980 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 1981 | return new SetCondInst(Instruction::SetGT, OffsetVal, AddCST); |
| 1982 | } |
| 1983 | break; // (X != 13 & X != 15) -> no change |
| 1984 | } |
| 1985 | break; |
| 1986 | case Instruction::SetLT: |
| 1987 | switch (RHSCC) { |
| 1988 | default: assert(0 && "Unknown integer condition code!"); |
| 1989 | case Instruction::SetEQ: // (X < 13 & X == 15) -> false |
| 1990 | case Instruction::SetGT: // (X < 13 & X > 15) -> false |
| 1991 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1992 | case Instruction::SetNE: // (X < 13 & X != 15) -> X < 13 |
| 1993 | case Instruction::SetLT: // (X < 13 & X < 15) -> X < 13 |
| 1994 | return ReplaceInstUsesWith(I, LHS); |
| 1995 | } |
| 1996 | case Instruction::SetGT: |
| 1997 | switch (RHSCC) { |
| 1998 | default: assert(0 && "Unknown integer condition code!"); |
| 1999 | case Instruction::SetEQ: // (X > 13 & X == 15) -> X > 13 |
| 2000 | return ReplaceInstUsesWith(I, LHS); |
| 2001 | case Instruction::SetGT: // (X > 13 & X > 15) -> X > 15 |
| 2002 | return ReplaceInstUsesWith(I, RHS); |
| 2003 | case Instruction::SetNE: |
| 2004 | if (RHSCst == AddOne(LHSCst)) // (X > 13 & X != 14) -> X > 14 |
| 2005 | return new SetCondInst(Instruction::SetGT, LHSVal, RHSCst); |
| 2006 | break; // (X > 13 & X != 15) -> no change |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2007 | case Instruction::SetLT: // (X > 13 & X < 15) -> (X-14) <u 1 |
| 2008 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, true, I); |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 2009 | } |
| 2010 | } |
| 2011 | } |
| 2012 | } |
| 2013 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2014 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2017 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2018 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2019 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2020 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2021 | if (isa<UndefValue>(Op1)) |
| 2022 | return ReplaceInstUsesWith(I, // X | undef -> -1 |
| 2023 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 2024 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2025 | // or X, X = X or X, 0 == X |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2026 | if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) |
| 2027 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2028 | |
| 2029 | // or X, -1 == -1 |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 2030 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2031 | // If X is known to only contain bits that already exist in RHS, just |
| 2032 | // replace this instruction with RHS directly. |
| 2033 | if (MaskedValueIsZero(Op0, |
| 2034 | cast<ConstantIntegral>(ConstantExpr::getNot(RHS)))) |
| 2035 | return ReplaceInstUsesWith(I, RHS); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2036 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2037 | ConstantInt *C1; Value *X; |
| 2038 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
| 2039 | 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] | 2040 | Instruction *Or = BinaryOperator::createOr(X, RHS, Op0->getName()); |
| 2041 | Op0->setName(""); |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2042 | InsertNewInstBefore(Or, I); |
| 2043 | return BinaryOperator::createAnd(Or, ConstantExpr::getOr(RHS, C1)); |
| 2044 | } |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 2045 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2046 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
| 2047 | if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { |
| 2048 | std::string Op0Name = Op0->getName(); Op0->setName(""); |
| 2049 | Instruction *Or = BinaryOperator::createOr(X, RHS, Op0Name); |
| 2050 | InsertNewInstBefore(Or, I); |
| 2051 | return BinaryOperator::createXor(Or, |
| 2052 | ConstantExpr::getAnd(C1, ConstantExpr::getNot(RHS))); |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 2053 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2054 | |
| 2055 | // Try to fold constant and into select arguments. |
| 2056 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2057 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2058 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2059 | if (isa<PHINode>(Op0)) |
| 2060 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2061 | return NV; |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2064 | Value *A, *B; ConstantInt *C1, *C2; |
Chris Lattner | 4294cec | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 2065 | |
| 2066 | if (match(Op0, m_And(m_Value(A), m_Value(B)))) |
| 2067 | if (A == Op1 || B == Op1) // (A & ?) | A --> A |
| 2068 | return ReplaceInstUsesWith(I, Op1); |
| 2069 | if (match(Op1, m_And(m_Value(A), m_Value(B)))) |
| 2070 | if (A == Op0 || B == Op0) // A | (A & ?) --> A |
| 2071 | return ReplaceInstUsesWith(I, Op0); |
| 2072 | |
Chris Lattner | b62f508 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 2073 | // (X^C)|Y -> (X|Y)^C iff Y&C == 0 |
| 2074 | if (Op0->hasOneUse() && match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) && |
| 2075 | MaskedValueIsZero(Op1, C1)) { |
| 2076 | Instruction *NOr = BinaryOperator::createOr(A, Op1, Op0->getName()); |
| 2077 | Op0->setName(""); |
| 2078 | return BinaryOperator::createXor(InsertNewInstBefore(NOr, I), C1); |
| 2079 | } |
| 2080 | |
| 2081 | // Y|(X^C) -> (X|Y)^C iff Y&C == 0 |
| 2082 | if (Op1->hasOneUse() && match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) && |
| 2083 | MaskedValueIsZero(Op0, C1)) { |
| 2084 | Instruction *NOr = BinaryOperator::createOr(A, Op0, Op1->getName()); |
| 2085 | Op0->setName(""); |
| 2086 | return BinaryOperator::createXor(InsertNewInstBefore(NOr, I), C1); |
| 2087 | } |
| 2088 | |
Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 2089 | // (A & C1)|(B & C2) |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2090 | if (match(Op0, m_And(m_Value(A), m_ConstantInt(C1))) && |
Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 2091 | match(Op1, m_And(m_Value(B), m_ConstantInt(C2)))) { |
| 2092 | |
| 2093 | if (A == B) // (A & C1)|(A & C2) == A & (C1|C2) |
| 2094 | return BinaryOperator::createAnd(A, ConstantExpr::getOr(C1, C2)); |
| 2095 | |
| 2096 | |
Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 2097 | // If we have: ((V + N) & C1) | (V & C2) |
| 2098 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 2099 | // replace with V+N. |
| 2100 | if (C1 == ConstantExpr::getNot(C2)) { |
| 2101 | Value *V1, *V2; |
| 2102 | if ((C2->getRawValue() & (C2->getRawValue()+1)) == 0 && // C2 == 0+1+ |
| 2103 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { |
| 2104 | // Add commutes, try both ways. |
| 2105 | if (V1 == B && MaskedValueIsZero(V2, C2)) |
| 2106 | return ReplaceInstUsesWith(I, A); |
| 2107 | if (V2 == B && MaskedValueIsZero(V1, C2)) |
| 2108 | return ReplaceInstUsesWith(I, A); |
| 2109 | } |
| 2110 | // Or commutes, try both ways. |
| 2111 | if ((C1->getRawValue() & (C1->getRawValue()+1)) == 0 && |
| 2112 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { |
| 2113 | // Add commutes, try both ways. |
| 2114 | if (V1 == A && MaskedValueIsZero(V2, C1)) |
| 2115 | return ReplaceInstUsesWith(I, B); |
| 2116 | if (V2 == A && MaskedValueIsZero(V1, C1)) |
| 2117 | return ReplaceInstUsesWith(I, B); |
Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 2118 | } |
| 2119 | } |
| 2120 | } |
Chris Lattner | 812aab7 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 2121 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2122 | if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1 |
| 2123 | if (A == Op1) // ~A | A == -1 |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2124 | return ReplaceInstUsesWith(I, |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2125 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 2126 | } else { |
| 2127 | A = 0; |
| 2128 | } |
Chris Lattner | 4294cec | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 2129 | // Note, A is still live here! |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2130 | if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B |
| 2131 | if (Op0 == B) |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2132 | return ReplaceInstUsesWith(I, |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2133 | ConstantIntegral::getAllOnesValue(I.getType())); |
Chris Lattner | 3e327a4 | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 2134 | |
Misha Brukman | 9c003d8 | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 2135 | // (~A | ~B) == (~(A & B)) - De Morgan's Law |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2136 | if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
| 2137 | Value *And = InsertNewInstBefore(BinaryOperator::createAnd(A, B, |
| 2138 | I.getName()+".demorgan"), I); |
| 2139 | return BinaryOperator::createNot(And); |
| 2140 | } |
Chris Lattner | 3e327a4 | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 2141 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2142 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2143 | // (setcc1 A, B) | (setcc2 A, B) --> (setcc3 A, B) |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2144 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1))) { |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2145 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 2146 | return R; |
| 2147 | |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2148 | Value *LHSVal, *RHSVal; |
| 2149 | ConstantInt *LHSCst, *RHSCst; |
| 2150 | Instruction::BinaryOps LHSCC, RHSCC; |
| 2151 | if (match(Op0, m_SetCond(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 2152 | if (match(RHS, m_SetCond(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 2153 | if (LHSVal == RHSVal && // Found (X setcc C1) | (X setcc C2) |
| 2154 | // Set[GL]E X, CST is folded to Set[GL]T elsewhere. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2155 | LHSCC != Instruction::SetGE && LHSCC != Instruction::SetLE && |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2156 | RHSCC != Instruction::SetGE && RHSCC != Instruction::SetLE) { |
| 2157 | // Ensure that the larger constant is on the RHS. |
| 2158 | Constant *Cmp = ConstantExpr::getSetGT(LHSCst, RHSCst); |
| 2159 | SetCondInst *LHS = cast<SetCondInst>(Op0); |
| 2160 | if (cast<ConstantBool>(Cmp)->getValue()) { |
| 2161 | std::swap(LHS, RHS); |
| 2162 | std::swap(LHSCst, RHSCst); |
| 2163 | std::swap(LHSCC, RHSCC); |
| 2164 | } |
| 2165 | |
| 2166 | // At this point, we know we have have two setcc instructions |
| 2167 | // comparing a value against two constants and or'ing the result |
| 2168 | // together. Because of the above check, we know that we only have |
| 2169 | // SetEQ, SetNE, SetLT, and SetGT here. We also know (from the |
| 2170 | // FoldSetCCLogical check above), that the two constants are not |
| 2171 | // equal. |
| 2172 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 2173 | |
| 2174 | switch (LHSCC) { |
| 2175 | default: assert(0 && "Unknown integer condition code!"); |
| 2176 | case Instruction::SetEQ: |
| 2177 | switch (RHSCC) { |
| 2178 | default: assert(0 && "Unknown integer condition code!"); |
| 2179 | case Instruction::SetEQ: |
| 2180 | if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13 <u 2 |
| 2181 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 2182 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 2183 | LHSVal->getName()+".off"); |
| 2184 | InsertNewInstBefore(Add, I); |
| 2185 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 2186 | Value *OffsetVal = InsertCastBefore(Add, UnsType, I); |
| 2187 | AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst); |
| 2188 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 2189 | return new SetCondInst(Instruction::SetLT, OffsetVal, AddCST); |
| 2190 | } |
| 2191 | break; // (X == 13 | X == 15) -> no change |
| 2192 | |
Chris Lattner | 5c21946 | 2005-04-19 06:04:18 +0000 | [diff] [blame] | 2193 | case Instruction::SetGT: // (X == 13 | X > 14) -> no change |
| 2194 | break; |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2195 | case Instruction::SetNE: // (X == 13 | X != 15) -> X != 15 |
| 2196 | case Instruction::SetLT: // (X == 13 | X < 15) -> X < 15 |
| 2197 | return ReplaceInstUsesWith(I, RHS); |
| 2198 | } |
| 2199 | break; |
| 2200 | case Instruction::SetNE: |
| 2201 | switch (RHSCC) { |
| 2202 | default: assert(0 && "Unknown integer condition code!"); |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2203 | case Instruction::SetEQ: // (X != 13 | X == 15) -> X != 13 |
| 2204 | case Instruction::SetGT: // (X != 13 | X > 15) -> X != 13 |
| 2205 | return ReplaceInstUsesWith(I, LHS); |
| 2206 | case Instruction::SetNE: // (X != 13 | X != 15) -> true |
Chris Lattner | 2ceb6ee | 2005-06-17 03:59:17 +0000 | [diff] [blame] | 2207 | case Instruction::SetLT: // (X != 13 | X < 15) -> true |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2208 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2209 | } |
| 2210 | break; |
| 2211 | case Instruction::SetLT: |
| 2212 | switch (RHSCC) { |
| 2213 | default: assert(0 && "Unknown integer condition code!"); |
| 2214 | case Instruction::SetEQ: // (X < 13 | X == 14) -> no change |
| 2215 | break; |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2216 | case Instruction::SetGT: // (X < 13 | X > 15) -> (X-13) > 2 |
| 2217 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false, I); |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2218 | case Instruction::SetNE: // (X < 13 | X != 15) -> X != 15 |
| 2219 | case Instruction::SetLT: // (X < 13 | X < 15) -> X < 15 |
| 2220 | return ReplaceInstUsesWith(I, RHS); |
| 2221 | } |
| 2222 | break; |
| 2223 | case Instruction::SetGT: |
| 2224 | switch (RHSCC) { |
| 2225 | default: assert(0 && "Unknown integer condition code!"); |
| 2226 | case Instruction::SetEQ: // (X > 13 | X == 15) -> X > 13 |
| 2227 | case Instruction::SetGT: // (X > 13 | X > 15) -> X > 13 |
| 2228 | return ReplaceInstUsesWith(I, LHS); |
| 2229 | case Instruction::SetNE: // (X > 13 | X != 15) -> true |
| 2230 | case Instruction::SetLT: // (X > 13 | X < 15) -> true |
| 2231 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2232 | } |
| 2233 | } |
| 2234 | } |
| 2235 | } |
Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 2236 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2237 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2238 | } |
| 2239 | |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 2240 | // XorSelf - Implements: X ^ X --> 0 |
| 2241 | struct XorSelf { |
| 2242 | Value *RHS; |
| 2243 | XorSelf(Value *rhs) : RHS(rhs) {} |
| 2244 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 2245 | Instruction *apply(BinaryOperator &Xor) const { |
| 2246 | return &Xor; |
| 2247 | } |
| 2248 | }; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2249 | |
| 2250 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2251 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2252 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2253 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2254 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2255 | if (isa<UndefValue>(Op1)) |
| 2256 | return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef |
| 2257 | |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 2258 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
| 2259 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { |
| 2260 | assert(Result == &I && "AssociativeOpt didn't work?"); |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2261 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 2262 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2263 | |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2264 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2265 | // xor X, 0 == X |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2266 | if (RHS->isNullValue()) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2267 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2268 | |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2269 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 2270 | // xor (setcc A, B), true = not (setcc A, B) = setncc A, B |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2271 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(Op0I)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2272 | if (RHS == ConstantBool::True && SCI->hasOneUse()) |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 2273 | return new SetCondInst(SCI->getInverseCondition(), |
| 2274 | SCI->getOperand(0), SCI->getOperand(1)); |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 2275 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2276 | // ~(c-X) == X-c-1 == X+(-c-1) |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2277 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
| 2278 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2279 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); |
| 2280 | Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2281 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2282 | return BinaryOperator::createAdd(Op0I->getOperand(1), ConstantRHS); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2283 | } |
Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 2284 | |
| 2285 | // ~(~X & Y) --> (X | ~Y) |
| 2286 | if (Op0I->getOpcode() == Instruction::And && RHS->isAllOnesValue()) { |
| 2287 | if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands(); |
| 2288 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { |
| 2289 | Instruction *NotY = |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2290 | BinaryOperator::createNot(Op0I->getOperand(1), |
Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 2291 | Op0I->getOperand(1)->getName()+".not"); |
| 2292 | InsertNewInstBefore(NotY, I); |
| 2293 | return BinaryOperator::createOr(Op0NotVal, NotY); |
| 2294 | } |
| 2295 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2296 | |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2297 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 2298 | switch (Op0I->getOpcode()) { |
| 2299 | case Instruction::Add: |
Chris Lattner | 0f68fa6 | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 2300 | // ~(X-c) --> (-c-1)-X |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2301 | if (RHS->isAllOnesValue()) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2302 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); |
| 2303 | return BinaryOperator::createSub( |
| 2304 | ConstantExpr::getSub(NegOp0CI, |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2305 | ConstantInt::get(I.getType(), 1)), |
Chris Lattner | 0f68fa6 | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 2306 | Op0I->getOperand(0)); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2307 | } |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 2308 | break; |
| 2309 | case Instruction::And: |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2310 | // (X & C1) ^ C2 --> (X & C1) | C2 iff (C1&C2) == 0 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2311 | if (ConstantExpr::getAnd(RHS, Op0CI)->isNullValue()) |
| 2312 | return BinaryOperator::createOr(Op0, RHS); |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 2313 | break; |
| 2314 | case Instruction::Or: |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2315 | // (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2316 | if (ConstantExpr::getAnd(RHS, Op0CI) == RHS) |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 2317 | return BinaryOperator::createAnd(Op0, ConstantExpr::getNot(RHS)); |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 2318 | break; |
| 2319 | default: break; |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2320 | } |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 2321 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2322 | |
| 2323 | // Try to fold constant and into select arguments. |
| 2324 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2325 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2326 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2327 | if (isa<PHINode>(Op0)) |
| 2328 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2329 | return NV; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2330 | } |
| 2331 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2332 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2333 | if (X == Op1) |
| 2334 | return ReplaceInstUsesWith(I, |
| 2335 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 2336 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2337 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2338 | if (X == Op0) |
| 2339 | return ReplaceInstUsesWith(I, |
| 2340 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 2341 | |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2342 | if (Instruction *Op1I = dyn_cast<Instruction>(Op1)) |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 2343 | if (Op1I->getOpcode() == Instruction::Or) { |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2344 | if (Op1I->getOperand(0) == Op0) { // B^(B|A) == (A|B)^B |
| 2345 | cast<BinaryOperator>(Op1I)->swapOperands(); |
| 2346 | I.swapOperands(); |
| 2347 | std::swap(Op0, Op1); |
| 2348 | } else if (Op1I->getOperand(1) == Op0) { // B^(A|B) == (A|B)^B |
| 2349 | I.swapOperands(); |
| 2350 | std::swap(Op0, Op1); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2351 | } |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 2352 | } else if (Op1I->getOpcode() == Instruction::Xor) { |
| 2353 | if (Op0 == Op1I->getOperand(0)) // A^(A^B) == B |
| 2354 | return ReplaceInstUsesWith(I, Op1I->getOperand(1)); |
| 2355 | else if (Op0 == Op1I->getOperand(1)) // A^(B^A) == B |
| 2356 | return ReplaceInstUsesWith(I, Op1I->getOperand(0)); |
| 2357 | } |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2358 | |
| 2359 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2360 | if (Op0I->getOpcode() == Instruction::Or && Op0I->hasOneUse()) { |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2361 | if (Op0I->getOperand(0) == Op1) // (B|A)^B == (A|B)^B |
| 2362 | cast<BinaryOperator>(Op0I)->swapOperands(); |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2363 | if (Op0I->getOperand(1) == Op1) { // (A|B)^B == A & ~B |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 2364 | Value *NotB = InsertNewInstBefore(BinaryOperator::createNot(Op1, |
| 2365 | Op1->getName()+".not"), I); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2366 | return BinaryOperator::createAnd(Op0I->getOperand(0), NotB); |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2367 | } |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 2368 | } else if (Op0I->getOpcode() == Instruction::Xor) { |
| 2369 | if (Op1 == Op0I->getOperand(0)) // (A^B)^A == B |
| 2370 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 2371 | else if (Op1 == Op0I->getOperand(1)) // (B^A)^A == B |
| 2372 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
Chris Lattner | 7aa2d47 | 2004-08-01 19:42:59 +0000 | [diff] [blame] | 2375 | // (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2376 | Value *A, *B; ConstantInt *C1, *C2; |
| 2377 | if (match(Op0, m_And(m_Value(A), m_ConstantInt(C1))) && |
| 2378 | match(Op1, m_And(m_Value(B), m_ConstantInt(C2))) && |
Chris Lattner | 7aa2d47 | 2004-08-01 19:42:59 +0000 | [diff] [blame] | 2379 | ConstantExpr::getAnd(C1, C2)->isNullValue()) |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2380 | return BinaryOperator::createOr(Op0, Op1); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 2381 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2382 | // (setcc1 A, B) ^ (setcc2 A, B) --> (setcc3 A, B) |
| 2383 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1))) |
| 2384 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 2385 | return R; |
| 2386 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2387 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2390 | /// MulWithOverflow - Compute Result = In1*In2, returning true if the result |
| 2391 | /// overflowed for this type. |
| 2392 | static bool MulWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
| 2393 | ConstantInt *In2) { |
| 2394 | Result = cast<ConstantInt>(ConstantExpr::getMul(In1, In2)); |
| 2395 | return !In2->isNullValue() && ConstantExpr::getDiv(Result, In2) != In1; |
| 2396 | } |
| 2397 | |
| 2398 | static bool isPositive(ConstantInt *C) { |
| 2399 | return cast<ConstantSInt>(C)->getValue() >= 0; |
| 2400 | } |
| 2401 | |
| 2402 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result |
| 2403 | /// overflowed for this type. |
| 2404 | static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
| 2405 | ConstantInt *In2) { |
| 2406 | Result = cast<ConstantInt>(ConstantExpr::getAdd(In1, In2)); |
| 2407 | |
| 2408 | if (In1->getType()->isUnsigned()) |
| 2409 | return cast<ConstantUInt>(Result)->getValue() < |
| 2410 | cast<ConstantUInt>(In1)->getValue(); |
| 2411 | if (isPositive(In1) != isPositive(In2)) |
| 2412 | return false; |
| 2413 | if (isPositive(In1)) |
| 2414 | return cast<ConstantSInt>(Result)->getValue() < |
| 2415 | cast<ConstantSInt>(In1)->getValue(); |
| 2416 | return cast<ConstantSInt>(Result)->getValue() > |
| 2417 | cast<ConstantSInt>(In1)->getValue(); |
| 2418 | } |
| 2419 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2420 | /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the |
| 2421 | /// code necessary to compute the offset from the base pointer (without adding |
| 2422 | /// in the base pointer). Return the result as a signed integer of intptr size. |
| 2423 | static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { |
| 2424 | TargetData &TD = IC.getTargetData(); |
| 2425 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 2426 | const Type *UIntPtrTy = TD.getIntPtrType(); |
| 2427 | const Type *SIntPtrTy = UIntPtrTy->getSignedVersion(); |
| 2428 | Value *Result = Constant::getNullValue(SIntPtrTy); |
| 2429 | |
| 2430 | // Build a mask for high order bits. |
| 2431 | uint64_t PtrSizeMask = ~0ULL; |
| 2432 | PtrSizeMask >>= 64-(TD.getPointerSize()*8); |
| 2433 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2434 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { |
| 2435 | Value *Op = GEP->getOperand(i); |
Chris Lattner | d35d210 | 2005-01-13 23:26:48 +0000 | [diff] [blame] | 2436 | uint64_t Size = TD.getTypeSize(GTI.getIndexedType()) & PtrSizeMask; |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2437 | Constant *Scale = ConstantExpr::getCast(ConstantUInt::get(UIntPtrTy, Size), |
| 2438 | SIntPtrTy); |
| 2439 | if (Constant *OpC = dyn_cast<Constant>(Op)) { |
| 2440 | if (!OpC->isNullValue()) { |
Chris Lattner | 4cb9fa3 | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 2441 | OpC = ConstantExpr::getCast(OpC, SIntPtrTy); |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2442 | Scale = ConstantExpr::getMul(OpC, Scale); |
| 2443 | if (Constant *RC = dyn_cast<Constant>(Result)) |
| 2444 | Result = ConstantExpr::getAdd(RC, Scale); |
| 2445 | else { |
| 2446 | // Emit an add instruction. |
| 2447 | Result = IC.InsertNewInstBefore( |
| 2448 | BinaryOperator::createAdd(Result, Scale, |
| 2449 | GEP->getName()+".offs"), I); |
| 2450 | } |
| 2451 | } |
| 2452 | } else { |
Chris Lattner | 7aa41cf | 2005-01-14 17:17:59 +0000 | [diff] [blame] | 2453 | // Convert to correct type. |
| 2454 | Op = IC.InsertNewInstBefore(new CastInst(Op, SIntPtrTy, |
| 2455 | Op->getName()+".c"), I); |
| 2456 | if (Size != 1) |
Chris Lattner | 4cb9fa3 | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 2457 | // We'll let instcombine(mul) convert this to a shl if possible. |
| 2458 | Op = IC.InsertNewInstBefore(BinaryOperator::createMul(Op, Scale, |
| 2459 | GEP->getName()+".idx"), I); |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2460 | |
| 2461 | // Emit an add instruction. |
Chris Lattner | 4cb9fa3 | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 2462 | Result = IC.InsertNewInstBefore(BinaryOperator::createAdd(Op, Result, |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2463 | GEP->getName()+".offs"), I); |
| 2464 | } |
| 2465 | } |
| 2466 | return Result; |
| 2467 | } |
| 2468 | |
| 2469 | /// FoldGEPSetCC - Fold comparisons between a GEP instruction and something |
| 2470 | /// else. At this point we know that the GEP is on the LHS of the comparison. |
| 2471 | Instruction *InstCombiner::FoldGEPSetCC(User *GEPLHS, Value *RHS, |
| 2472 | Instruction::BinaryOps Cond, |
| 2473 | Instruction &I) { |
| 2474 | assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!"); |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2475 | |
| 2476 | if (CastInst *CI = dyn_cast<CastInst>(RHS)) |
| 2477 | if (isa<PointerType>(CI->getOperand(0)->getType())) |
| 2478 | RHS = CI->getOperand(0); |
| 2479 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2480 | Value *PtrBase = GEPLHS->getOperand(0); |
| 2481 | if (PtrBase == RHS) { |
| 2482 | // As an optimization, we don't actually have to compute the actual value of |
| 2483 | // OFFSET if this is a seteq or setne comparison, just return whether each |
| 2484 | // index is zero or not. |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2485 | if (Cond == Instruction::SetEQ || Cond == Instruction::SetNE) { |
| 2486 | Instruction *InVal = 0; |
Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 2487 | gep_type_iterator GTI = gep_type_begin(GEPLHS); |
| 2488 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i, ++GTI) { |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2489 | bool EmitIt = true; |
| 2490 | if (Constant *C = dyn_cast<Constant>(GEPLHS->getOperand(i))) { |
| 2491 | if (isa<UndefValue>(C)) // undef index -> undef. |
| 2492 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); |
| 2493 | if (C->isNullValue()) |
| 2494 | EmitIt = false; |
Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 2495 | else if (TD->getTypeSize(GTI.getIndexedType()) == 0) { |
| 2496 | EmitIt = false; // This is indexing into a zero sized array? |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2497 | } else if (isa<ConstantInt>(C)) |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2498 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
| 2499 | ConstantBool::get(Cond == Instruction::SetNE)); |
| 2500 | } |
| 2501 | |
| 2502 | if (EmitIt) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2503 | Instruction *Comp = |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2504 | new SetCondInst(Cond, GEPLHS->getOperand(i), |
| 2505 | Constant::getNullValue(GEPLHS->getOperand(i)->getType())); |
| 2506 | if (InVal == 0) |
| 2507 | InVal = Comp; |
| 2508 | else { |
| 2509 | InVal = InsertNewInstBefore(InVal, I); |
| 2510 | InsertNewInstBefore(Comp, I); |
| 2511 | if (Cond == Instruction::SetNE) // True if any are unequal |
| 2512 | InVal = BinaryOperator::createOr(InVal, Comp); |
| 2513 | else // True if all are equal |
| 2514 | InVal = BinaryOperator::createAnd(InVal, Comp); |
| 2515 | } |
| 2516 | } |
| 2517 | } |
| 2518 | |
| 2519 | if (InVal) |
| 2520 | return InVal; |
| 2521 | else |
| 2522 | ReplaceInstUsesWith(I, // No comparison is needed here, all indexes = 0 |
| 2523 | ConstantBool::get(Cond == Instruction::SetEQ)); |
| 2524 | } |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2525 | |
| 2526 | // Only lower this if the setcc is the only user of the GEP or if we expect |
| 2527 | // the result to fold to a constant! |
| 2528 | if (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) { |
| 2529 | // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). |
| 2530 | Value *Offset = EmitGEPOffset(GEPLHS, I, *this); |
| 2531 | return new SetCondInst(Cond, Offset, |
| 2532 | Constant::getNullValue(Offset->getType())); |
| 2533 | } |
| 2534 | } else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) { |
Chris Lattner | a21bf8d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 2535 | // If the base pointers are different, but the indices are the same, just |
| 2536 | // compare the base pointer. |
| 2537 | if (PtrBase != GEPRHS->getOperand(0)) { |
| 2538 | bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands(); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2539 | IndicesTheSame &= GEPLHS->getOperand(0)->getType() == |
Chris Lattner | bd43b9d | 2005-04-26 14:40:41 +0000 | [diff] [blame] | 2540 | GEPRHS->getOperand(0)->getType(); |
Chris Lattner | a21bf8d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 2541 | if (IndicesTheSame) |
| 2542 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 2543 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 2544 | IndicesTheSame = false; |
| 2545 | break; |
| 2546 | } |
| 2547 | |
| 2548 | // If all indices are the same, just compare the base pointers. |
| 2549 | if (IndicesTheSame) |
| 2550 | return new SetCondInst(Cond, GEPLHS->getOperand(0), |
| 2551 | GEPRHS->getOperand(0)); |
| 2552 | |
| 2553 | // Otherwise, the base pointers are different and the indices are |
| 2554 | // different, bail out. |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2555 | return 0; |
Chris Lattner | a21bf8d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 2556 | } |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2557 | |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2558 | // If one of the GEPs has all zero indices, recurse. |
| 2559 | bool AllZeros = true; |
| 2560 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 2561 | if (!isa<Constant>(GEPLHS->getOperand(i)) || |
| 2562 | !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) { |
| 2563 | AllZeros = false; |
| 2564 | break; |
| 2565 | } |
| 2566 | if (AllZeros) |
| 2567 | return FoldGEPSetCC(GEPRHS, GEPLHS->getOperand(0), |
| 2568 | SetCondInst::getSwappedCondition(Cond), I); |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 2569 | |
| 2570 | // If the other GEP has all zero indices, recurse. |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2571 | AllZeros = true; |
| 2572 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 2573 | if (!isa<Constant>(GEPRHS->getOperand(i)) || |
| 2574 | !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) { |
| 2575 | AllZeros = false; |
| 2576 | break; |
| 2577 | } |
| 2578 | if (AllZeros) |
| 2579 | return FoldGEPSetCC(GEPLHS, GEPRHS->getOperand(0), Cond, I); |
| 2580 | |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 2581 | if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) { |
| 2582 | // If the GEPs only differ by one index, compare it. |
| 2583 | unsigned NumDifferences = 0; // Keep track of # differences. |
| 2584 | unsigned DiffOperand = 0; // The operand that differs. |
| 2585 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 2586 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2587 | if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() != |
| 2588 | GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | fc4429e | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 2589 | // Irreconcilable differences. |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 2590 | NumDifferences = 2; |
| 2591 | break; |
| 2592 | } else { |
| 2593 | if (NumDifferences++) break; |
| 2594 | DiffOperand = i; |
| 2595 | } |
| 2596 | } |
| 2597 | |
| 2598 | if (NumDifferences == 0) // SAME GEP? |
| 2599 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
| 2600 | ConstantBool::get(Cond == Instruction::SetEQ)); |
| 2601 | else if (NumDifferences == 1) { |
Chris Lattner | fc4429e | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 2602 | Value *LHSV = GEPLHS->getOperand(DiffOperand); |
| 2603 | Value *RHSV = GEPRHS->getOperand(DiffOperand); |
Chris Lattner | 247aef8 | 2005-07-18 23:07:33 +0000 | [diff] [blame] | 2604 | |
| 2605 | // Convert the operands to signed values to make sure to perform a |
| 2606 | // signed comparison. |
| 2607 | const Type *NewTy = LHSV->getType()->getSignedVersion(); |
| 2608 | if (LHSV->getType() != NewTy) |
| 2609 | LHSV = InsertNewInstBefore(new CastInst(LHSV, NewTy, |
| 2610 | LHSV->getName()), I); |
| 2611 | if (RHSV->getType() != NewTy) |
| 2612 | RHSV = InsertNewInstBefore(new CastInst(RHSV, NewTy, |
| 2613 | RHSV->getName()), I); |
| 2614 | return new SetCondInst(Cond, LHSV, RHSV); |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 2615 | } |
| 2616 | } |
| 2617 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2618 | // Only lower this if the setcc is the only user of the GEP or if we expect |
| 2619 | // the result to fold to a constant! |
| 2620 | if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) && |
| 2621 | (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) { |
| 2622 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) |
| 2623 | Value *L = EmitGEPOffset(GEPLHS, I, *this); |
| 2624 | Value *R = EmitGEPOffset(GEPRHS, I, *this); |
| 2625 | return new SetCondInst(Cond, L, R); |
| 2626 | } |
| 2627 | } |
| 2628 | return 0; |
| 2629 | } |
| 2630 | |
| 2631 | |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2632 | Instruction *InstCombiner::visitSetCondInst(SetCondInst &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2633 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2634 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2635 | const Type *Ty = Op0->getType(); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2636 | |
| 2637 | // setcc X, X |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2638 | if (Op0 == Op1) |
| 2639 | return ReplaceInstUsesWith(I, ConstantBool::get(isTrueWhenEqual(I))); |
Chris Lattner | 1fc23f3 | 2002-05-09 20:11:54 +0000 | [diff] [blame] | 2640 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2641 | if (isa<UndefValue>(Op1)) // X setcc undef -> undef |
| 2642 | return ReplaceInstUsesWith(I, UndefValue::get(Type::BoolTy)); |
| 2643 | |
Chris Lattner | 15ff1e1 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 2644 | // setcc <global/alloca*/null>, <global/alloca*/null> - Global/Stack value |
| 2645 | // addresses never equal each other! We already know that Op0 != Op1. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2646 | if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) || |
| 2647 | isa<ConstantPointerNull>(Op0)) && |
| 2648 | (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) || |
Chris Lattner | 15ff1e1 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 2649 | isa<ConstantPointerNull>(Op1))) |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2650 | return ReplaceInstUsesWith(I, ConstantBool::get(!isTrueWhenEqual(I))); |
| 2651 | |
| 2652 | // setcc's with boolean values can always be turned into bitwise operations |
| 2653 | if (Ty == Type::BoolTy) { |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 2654 | switch (I.getOpcode()) { |
| 2655 | default: assert(0 && "Invalid setcc instruction!"); |
| 2656 | case Instruction::SetEQ: { // seteq bool %A, %B -> ~(A^B) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2657 | Instruction *Xor = BinaryOperator::createXor(Op0, Op1, I.getName()+"tmp"); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2658 | InsertNewInstBefore(Xor, I); |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 2659 | return BinaryOperator::createNot(Xor); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2660 | } |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 2661 | case Instruction::SetNE: |
| 2662 | return BinaryOperator::createXor(Op0, Op1); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2663 | |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 2664 | case Instruction::SetGT: |
| 2665 | std::swap(Op0, Op1); // Change setgt -> setlt |
| 2666 | // FALL THROUGH |
| 2667 | case Instruction::SetLT: { // setlt bool A, B -> ~X & Y |
| 2668 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 2669 | InsertNewInstBefore(Not, I); |
| 2670 | return BinaryOperator::createAnd(Not, Op1); |
| 2671 | } |
| 2672 | case Instruction::SetGE: |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2673 | std::swap(Op0, Op1); // Change setge -> setle |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 2674 | // FALL THROUGH |
| 2675 | case Instruction::SetLE: { // setle bool %A, %B -> ~A | B |
| 2676 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 2677 | InsertNewInstBefore(Not, I); |
| 2678 | return BinaryOperator::createOr(Not, Op1); |
| 2679 | } |
| 2680 | } |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2681 | } |
| 2682 | |
Chris Lattner | 2dd0174 | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 2683 | // See if we are doing a comparison between a constant and an instruction that |
| 2684 | // can be folded into the comparison. |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2685 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2686 | // Check to see if we are comparing against the minimum or maximum value... |
| 2687 | if (CI->isMinValue()) { |
| 2688 | if (I.getOpcode() == Instruction::SetLT) // A < MIN -> FALSE |
| 2689 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2690 | if (I.getOpcode() == Instruction::SetGE) // A >= MIN -> TRUE |
| 2691 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2692 | if (I.getOpcode() == Instruction::SetLE) // A <= MIN -> A == MIN |
| 2693 | return BinaryOperator::createSetEQ(Op0, Op1); |
| 2694 | if (I.getOpcode() == Instruction::SetGT) // A > MIN -> A != MIN |
| 2695 | return BinaryOperator::createSetNE(Op0, Op1); |
| 2696 | |
| 2697 | } else if (CI->isMaxValue()) { |
| 2698 | if (I.getOpcode() == Instruction::SetGT) // A > MAX -> FALSE |
| 2699 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2700 | if (I.getOpcode() == Instruction::SetLE) // A <= MAX -> TRUE |
| 2701 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2702 | if (I.getOpcode() == Instruction::SetGE) // A >= MAX -> A == MAX |
| 2703 | return BinaryOperator::createSetEQ(Op0, Op1); |
| 2704 | if (I.getOpcode() == Instruction::SetLT) // A < MAX -> A != MAX |
| 2705 | return BinaryOperator::createSetNE(Op0, Op1); |
| 2706 | |
| 2707 | // Comparing against a value really close to min or max? |
| 2708 | } else if (isMinValuePlusOne(CI)) { |
| 2709 | if (I.getOpcode() == Instruction::SetLT) // A < MIN+1 -> A == MIN |
| 2710 | return BinaryOperator::createSetEQ(Op0, SubOne(CI)); |
| 2711 | if (I.getOpcode() == Instruction::SetGE) // A >= MIN-1 -> A != MIN |
| 2712 | return BinaryOperator::createSetNE(Op0, SubOne(CI)); |
| 2713 | |
| 2714 | } else if (isMaxValueMinusOne(CI)) { |
| 2715 | if (I.getOpcode() == Instruction::SetGT) // A > MAX-1 -> A == MAX |
| 2716 | return BinaryOperator::createSetEQ(Op0, AddOne(CI)); |
| 2717 | if (I.getOpcode() == Instruction::SetLE) // A <= MAX-1 -> A != MAX |
| 2718 | return BinaryOperator::createSetNE(Op0, AddOne(CI)); |
| 2719 | } |
| 2720 | |
| 2721 | // If we still have a setle or setge instruction, turn it into the |
| 2722 | // appropriate setlt or setgt instruction. Since the border cases have |
| 2723 | // already been handled above, this requires little checking. |
| 2724 | // |
| 2725 | if (I.getOpcode() == Instruction::SetLE) |
| 2726 | return BinaryOperator::createSetLT(Op0, AddOne(CI)); |
| 2727 | if (I.getOpcode() == Instruction::SetGE) |
| 2728 | return BinaryOperator::createSetGT(Op0, SubOne(CI)); |
| 2729 | |
Chris Lattner | e1e10e1 | 2004-05-25 06:32:08 +0000 | [diff] [blame] | 2730 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2731 | switch (LHSI->getOpcode()) { |
| 2732 | case Instruction::And: |
| 2733 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && |
| 2734 | LHSI->getOperand(0)->hasOneUse()) { |
| 2735 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare |
| 2736 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This |
| 2737 | // happens a LOT in code produced by the C front-end, for bitfield |
| 2738 | // access. |
| 2739 | ShiftInst *Shift = dyn_cast<ShiftInst>(LHSI->getOperand(0)); |
| 2740 | ConstantUInt *ShAmt; |
| 2741 | ShAmt = Shift ? dyn_cast<ConstantUInt>(Shift->getOperand(1)) : 0; |
| 2742 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); |
| 2743 | const Type *Ty = LHSI->getType(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2744 | |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2745 | // We can fold this as long as we can't shift unknown bits |
| 2746 | // into the mask. This can only happen with signed shift |
| 2747 | // rights, as they sign-extend. |
| 2748 | if (ShAmt) { |
| 2749 | bool CanFold = Shift->getOpcode() != Instruction::Shr || |
Chris Lattner | 6afc02f | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 2750 | Shift->getType()->isUnsigned(); |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2751 | if (!CanFold) { |
| 2752 | // To test for the bad case of the signed shr, see if any |
| 2753 | // of the bits shifted in could be tested after the mask. |
Chris Lattner | c53cb9d | 2005-06-17 01:29:28 +0000 | [diff] [blame] | 2754 | int ShAmtVal = Ty->getPrimitiveSizeInBits()-ShAmt->getValue(); |
| 2755 | if (ShAmtVal < 0) ShAmtVal = 0; // Out of range shift. |
| 2756 | |
| 2757 | Constant *OShAmt = ConstantUInt::get(Type::UByteTy, ShAmtVal); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2758 | Constant *ShVal = |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2759 | ConstantExpr::getShl(ConstantInt::getAllOnesValue(Ty), OShAmt); |
| 2760 | if (ConstantExpr::getAnd(ShVal, AndCST)->isNullValue()) |
| 2761 | CanFold = true; |
| 2762 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2763 | |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2764 | if (CanFold) { |
Chris Lattner | 6afc02f | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 2765 | Constant *NewCst; |
| 2766 | if (Shift->getOpcode() == Instruction::Shl) |
| 2767 | NewCst = ConstantExpr::getUShr(CI, ShAmt); |
| 2768 | else |
| 2769 | NewCst = ConstantExpr::getShl(CI, ShAmt); |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2770 | |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2771 | // Check to see if we are shifting out any of the bits being |
| 2772 | // compared. |
| 2773 | if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != CI){ |
| 2774 | // If we shifted bits out, the fold is not going to work out. |
| 2775 | // As a special case, check to see if this means that the |
| 2776 | // result is always true or false now. |
| 2777 | if (I.getOpcode() == Instruction::SetEQ) |
| 2778 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2779 | if (I.getOpcode() == Instruction::SetNE) |
| 2780 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2781 | } else { |
| 2782 | I.setOperand(1, NewCst); |
Chris Lattner | 6afc02f | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 2783 | Constant *NewAndCST; |
| 2784 | if (Shift->getOpcode() == Instruction::Shl) |
| 2785 | NewAndCST = ConstantExpr::getUShr(AndCST, ShAmt); |
| 2786 | else |
| 2787 | NewAndCST = ConstantExpr::getShl(AndCST, ShAmt); |
| 2788 | LHSI->setOperand(1, NewAndCST); |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2789 | LHSI->setOperand(0, Shift->getOperand(0)); |
| 2790 | WorkList.push_back(Shift); // Shift is dead. |
| 2791 | AddUsesToWorkList(I); |
| 2792 | return &I; |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 2793 | } |
| 2794 | } |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2795 | } |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2796 | } |
| 2797 | break; |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2798 | |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2799 | case Instruction::Shl: // (setcc (shl X, ShAmt), CI) |
| 2800 | if (ConstantUInt *ShAmt = dyn_cast<ConstantUInt>(LHSI->getOperand(1))) { |
| 2801 | switch (I.getOpcode()) { |
| 2802 | default: break; |
| 2803 | case Instruction::SetEQ: |
| 2804 | case Instruction::SetNE: { |
Chris Lattner | 19b57f5 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 2805 | unsigned TypeBits = CI->getType()->getPrimitiveSizeInBits(); |
| 2806 | |
| 2807 | // Check that the shift amount is in range. If not, don't perform |
| 2808 | // undefined shifts. When the shift is visited it will be |
| 2809 | // simplified. |
| 2810 | if (ShAmt->getValue() >= TypeBits) |
| 2811 | break; |
| 2812 | |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2813 | // If we are comparing against bits always shifted out, the |
| 2814 | // comparison cannot succeed. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2815 | Constant *Comp = |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2816 | ConstantExpr::getShl(ConstantExpr::getShr(CI, ShAmt), ShAmt); |
| 2817 | if (Comp != CI) {// Comparing against a bit that we know is zero. |
| 2818 | bool IsSetNE = I.getOpcode() == Instruction::SetNE; |
| 2819 | Constant *Cst = ConstantBool::get(IsSetNE); |
| 2820 | return ReplaceInstUsesWith(I, Cst); |
| 2821 | } |
| 2822 | |
| 2823 | if (LHSI->hasOneUse()) { |
| 2824 | // Otherwise strength reduce the shift into an and. |
Chris Lattner | fdfe3e49 | 2005-01-08 19:42:22 +0000 | [diff] [blame] | 2825 | unsigned ShAmtVal = (unsigned)ShAmt->getValue(); |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2826 | uint64_t Val = (1ULL << (TypeBits-ShAmtVal))-1; |
| 2827 | |
| 2828 | Constant *Mask; |
| 2829 | if (CI->getType()->isUnsigned()) { |
| 2830 | Mask = ConstantUInt::get(CI->getType(), Val); |
| 2831 | } else if (ShAmtVal != 0) { |
| 2832 | Mask = ConstantSInt::get(CI->getType(), Val); |
| 2833 | } else { |
| 2834 | Mask = ConstantInt::getAllOnesValue(CI->getType()); |
| 2835 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2836 | |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2837 | Instruction *AndI = |
| 2838 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 2839 | Mask, LHSI->getName()+".mask"); |
| 2840 | Value *And = InsertNewInstBefore(AndI, I); |
| 2841 | return new SetCondInst(I.getOpcode(), And, |
| 2842 | ConstantExpr::getUShr(CI, ShAmt)); |
| 2843 | } |
| 2844 | } |
| 2845 | } |
| 2846 | } |
| 2847 | break; |
| 2848 | |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2849 | case Instruction::Shr: // (setcc (shr X, ShAmt), CI) |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2850 | if (ConstantUInt *ShAmt = dyn_cast<ConstantUInt>(LHSI->getOperand(1))) { |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2851 | switch (I.getOpcode()) { |
| 2852 | default: break; |
| 2853 | case Instruction::SetEQ: |
| 2854 | case Instruction::SetNE: { |
Chris Lattner | 19b57f5 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 2855 | |
| 2856 | // Check that the shift amount is in range. If not, don't perform |
| 2857 | // undefined shifts. When the shift is visited it will be |
| 2858 | // simplified. |
Chris Lattner | 104002b | 2005-06-16 01:52:07 +0000 | [diff] [blame] | 2859 | unsigned TypeBits = CI->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 19b57f5 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 2860 | if (ShAmt->getValue() >= TypeBits) |
| 2861 | break; |
| 2862 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2863 | // If we are comparing against bits always shifted out, the |
| 2864 | // comparison cannot succeed. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2865 | Constant *Comp = |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2866 | ConstantExpr::getShr(ConstantExpr::getShl(CI, ShAmt), ShAmt); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2867 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2868 | if (Comp != CI) {// Comparing against a bit that we know is zero. |
| 2869 | bool IsSetNE = I.getOpcode() == Instruction::SetNE; |
| 2870 | Constant *Cst = ConstantBool::get(IsSetNE); |
| 2871 | return ReplaceInstUsesWith(I, Cst); |
| 2872 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2873 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2874 | if (LHSI->hasOneUse() || CI->isNullValue()) { |
Chris Lattner | fdfe3e49 | 2005-01-08 19:42:22 +0000 | [diff] [blame] | 2875 | unsigned ShAmtVal = (unsigned)ShAmt->getValue(); |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2876 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2877 | // Otherwise strength reduce the shift into an and. |
| 2878 | uint64_t Val = ~0ULL; // All ones. |
| 2879 | Val <<= ShAmtVal; // Shift over to the right spot. |
| 2880 | |
| 2881 | Constant *Mask; |
| 2882 | if (CI->getType()->isUnsigned()) { |
Chris Lattner | 2f1457f | 2005-04-24 17:46:05 +0000 | [diff] [blame] | 2883 | Val &= ~0ULL >> (64-TypeBits); |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2884 | Mask = ConstantUInt::get(CI->getType(), Val); |
| 2885 | } else { |
| 2886 | Mask = ConstantSInt::get(CI->getType(), Val); |
| 2887 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2888 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2889 | Instruction *AndI = |
| 2890 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 2891 | Mask, LHSI->getName()+".mask"); |
| 2892 | Value *And = InsertNewInstBefore(AndI, I); |
| 2893 | return new SetCondInst(I.getOpcode(), And, |
| 2894 | ConstantExpr::getShl(CI, ShAmt)); |
| 2895 | } |
| 2896 | break; |
| 2897 | } |
| 2898 | } |
| 2899 | } |
| 2900 | break; |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2901 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2902 | case Instruction::Div: |
| 2903 | // Fold: (div X, C1) op C2 -> range check |
| 2904 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
| 2905 | // Fold this div into the comparison, producing a range check. |
| 2906 | // Determine, based on the divide type, what the range is being |
| 2907 | // checked. If there is an overflow on the low or high side, remember |
| 2908 | // it, otherwise compute the range [low, hi) bounding the new value. |
| 2909 | bool LoOverflow = false, HiOverflow = 0; |
| 2910 | ConstantInt *LoBound = 0, *HiBound = 0; |
| 2911 | |
| 2912 | ConstantInt *Prod; |
| 2913 | bool ProdOV = MulWithOverflow(Prod, CI, DivRHS); |
| 2914 | |
Chris Lattner | a92af96 | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 2915 | Instruction::BinaryOps Opcode = I.getOpcode(); |
| 2916 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2917 | if (DivRHS->isNullValue()) { // Don't hack on divide by zeros. |
| 2918 | } else if (LHSI->getType()->isUnsigned()) { // udiv |
| 2919 | LoBound = Prod; |
| 2920 | LoOverflow = ProdOV; |
| 2921 | HiOverflow = ProdOV || AddWithOverflow(HiBound, LoBound, DivRHS); |
| 2922 | } else if (isPositive(DivRHS)) { // Divisor is > 0. |
| 2923 | if (CI->isNullValue()) { // (X / pos) op 0 |
| 2924 | // Can't overflow. |
| 2925 | LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS))); |
| 2926 | HiBound = DivRHS; |
| 2927 | } else if (isPositive(CI)) { // (X / pos) op pos |
| 2928 | LoBound = Prod; |
| 2929 | LoOverflow = ProdOV; |
| 2930 | HiOverflow = ProdOV || AddWithOverflow(HiBound, Prod, DivRHS); |
| 2931 | } else { // (X / pos) op neg |
| 2932 | Constant *DivRHSH = ConstantExpr::getNeg(SubOne(DivRHS)); |
| 2933 | LoOverflow = AddWithOverflow(LoBound, Prod, |
| 2934 | cast<ConstantInt>(DivRHSH)); |
| 2935 | HiBound = Prod; |
| 2936 | HiOverflow = ProdOV; |
| 2937 | } |
| 2938 | } else { // Divisor is < 0. |
| 2939 | if (CI->isNullValue()) { // (X / neg) op 0 |
| 2940 | LoBound = AddOne(DivRHS); |
| 2941 | HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS)); |
Chris Lattner | 73bcba5 | 2005-06-17 02:05:55 +0000 | [diff] [blame] | 2942 | if (HiBound == DivRHS) |
| 2943 | LoBound = 0; // - INTMIN = INTMIN |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2944 | } else if (isPositive(CI)) { // (X / neg) op pos |
| 2945 | HiOverflow = LoOverflow = ProdOV; |
| 2946 | if (!LoOverflow) |
| 2947 | LoOverflow = AddWithOverflow(LoBound, Prod, AddOne(DivRHS)); |
| 2948 | HiBound = AddOne(Prod); |
| 2949 | } else { // (X / neg) op neg |
| 2950 | LoBound = Prod; |
| 2951 | LoOverflow = HiOverflow = ProdOV; |
| 2952 | HiBound = cast<ConstantInt>(ConstantExpr::getSub(Prod, DivRHS)); |
| 2953 | } |
Chris Lattner | 0b41e86 | 2004-10-08 19:15:44 +0000 | [diff] [blame] | 2954 | |
Chris Lattner | a92af96 | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 2955 | // Dividing by a negate swaps the condition. |
| 2956 | Opcode = SetCondInst::getSwappedCondition(Opcode); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
| 2959 | if (LoBound) { |
| 2960 | Value *X = LHSI->getOperand(0); |
Chris Lattner | a92af96 | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 2961 | switch (Opcode) { |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2962 | default: assert(0 && "Unhandled setcc opcode!"); |
| 2963 | case Instruction::SetEQ: |
| 2964 | if (LoOverflow && HiOverflow) |
| 2965 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2966 | else if (HiOverflow) |
| 2967 | return new SetCondInst(Instruction::SetGE, X, LoBound); |
| 2968 | else if (LoOverflow) |
| 2969 | return new SetCondInst(Instruction::SetLT, X, HiBound); |
| 2970 | else |
| 2971 | return InsertRangeTest(X, LoBound, HiBound, true, I); |
| 2972 | case Instruction::SetNE: |
| 2973 | if (LoOverflow && HiOverflow) |
| 2974 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2975 | else if (HiOverflow) |
| 2976 | return new SetCondInst(Instruction::SetLT, X, LoBound); |
| 2977 | else if (LoOverflow) |
| 2978 | return new SetCondInst(Instruction::SetGE, X, HiBound); |
| 2979 | else |
| 2980 | return InsertRangeTest(X, LoBound, HiBound, false, I); |
| 2981 | case Instruction::SetLT: |
| 2982 | if (LoOverflow) |
| 2983 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2984 | return new SetCondInst(Instruction::SetLT, X, LoBound); |
| 2985 | case Instruction::SetGT: |
| 2986 | if (HiOverflow) |
| 2987 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2988 | return new SetCondInst(Instruction::SetGE, X, HiBound); |
| 2989 | } |
| 2990 | } |
| 2991 | } |
| 2992 | break; |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2993 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2994 | |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 2995 | // Simplify seteq and setne instructions... |
| 2996 | if (I.getOpcode() == Instruction::SetEQ || |
| 2997 | I.getOpcode() == Instruction::SetNE) { |
| 2998 | bool isSetNE = I.getOpcode() == Instruction::SetNE; |
| 2999 | |
Chris Lattner | cfbce7c | 2003-07-23 17:26:36 +0000 | [diff] [blame] | 3000 | // 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] | 3001 | // operand is a constant, simplify a bit. |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3002 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) { |
| 3003 | switch (BO->getOpcode()) { |
Chris Lattner | 23b47b6 | 2004-07-06 07:38:18 +0000 | [diff] [blame] | 3004 | case Instruction::Rem: |
| 3005 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. |
| 3006 | if (CI->isNullValue() && isa<ConstantSInt>(BO->getOperand(1)) && |
| 3007 | BO->hasOneUse() && |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 3008 | cast<ConstantSInt>(BO->getOperand(1))->getValue() > 1) { |
| 3009 | int64_t V = cast<ConstantSInt>(BO->getOperand(1))->getValue(); |
| 3010 | if (isPowerOf2_64(V)) { |
| 3011 | unsigned L2 = Log2_64(V); |
Chris Lattner | 23b47b6 | 2004-07-06 07:38:18 +0000 | [diff] [blame] | 3012 | const Type *UTy = BO->getType()->getUnsignedVersion(); |
| 3013 | Value *NewX = InsertNewInstBefore(new CastInst(BO->getOperand(0), |
| 3014 | UTy, "tmp"), I); |
| 3015 | Constant *RHSCst = ConstantUInt::get(UTy, 1ULL << L2); |
| 3016 | Value *NewRem =InsertNewInstBefore(BinaryOperator::createRem(NewX, |
| 3017 | RHSCst, BO->getName()), I); |
| 3018 | return BinaryOperator::create(I.getOpcode(), NewRem, |
| 3019 | Constant::getNullValue(UTy)); |
| 3020 | } |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 3021 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3022 | break; |
Chris Lattner | 23b47b6 | 2004-07-06 07:38:18 +0000 | [diff] [blame] | 3023 | |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3024 | case Instruction::Add: |
Chris Lattner | 6e07936 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 3025 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. |
| 3026 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | b121ae1 | 2004-09-21 21:35:23 +0000 | [diff] [blame] | 3027 | if (BO->hasOneUse()) |
| 3028 | return new SetCondInst(I.getOpcode(), BO->getOperand(0), |
| 3029 | ConstantExpr::getSub(CI, BOp1C)); |
Chris Lattner | 6e07936 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 3030 | } else if (CI->isNullValue()) { |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3031 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 3032 | // efficiently invertible, or if the add has just this one use. |
| 3033 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3034 | |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3035 | if (Value *NegVal = dyn_castNegVal(BOp1)) |
| 3036 | return new SetCondInst(I.getOpcode(), BOp0, NegVal); |
| 3037 | else if (Value *NegVal = dyn_castNegVal(BOp0)) |
| 3038 | return new SetCondInst(I.getOpcode(), NegVal, BOp1); |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 3039 | else if (BO->hasOneUse()) { |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3040 | Instruction *Neg = BinaryOperator::createNeg(BOp1, BO->getName()); |
| 3041 | BO->setName(""); |
| 3042 | InsertNewInstBefore(Neg, I); |
| 3043 | return new SetCondInst(I.getOpcode(), BOp0, Neg); |
| 3044 | } |
| 3045 | } |
| 3046 | break; |
| 3047 | case Instruction::Xor: |
| 3048 | // For the xor case, we can xor two constants together, eliminating |
| 3049 | // the explicit xor. |
| 3050 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) |
| 3051 | return BinaryOperator::create(I.getOpcode(), BO->getOperand(0), |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3052 | ConstantExpr::getXor(CI, BOC)); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3053 | |
| 3054 | // FALLTHROUGH |
| 3055 | case Instruction::Sub: |
| 3056 | // Replace (([sub|xor] A, B) != 0) with (A != B) |
| 3057 | if (CI->isNullValue()) |
| 3058 | return new SetCondInst(I.getOpcode(), BO->getOperand(0), |
| 3059 | BO->getOperand(1)); |
| 3060 | break; |
| 3061 | |
| 3062 | case Instruction::Or: |
| 3063 | // If bits are being or'd in that are not present in the constant we |
| 3064 | // are comparing against, then the comparison could never succeed! |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3065 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 3066 | Constant *NotCI = ConstantExpr::getNot(CI); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3067 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 3068 | return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE)); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3069 | } |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3070 | break; |
| 3071 | |
| 3072 | case Instruction::And: |
| 3073 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 3074 | // If bits are being compared against that are and'd out, then the |
| 3075 | // comparison can never succeed! |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 3076 | if (!ConstantExpr::getAnd(CI, |
| 3077 | ConstantExpr::getNot(BOC))->isNullValue()) |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 3078 | return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE)); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3079 | |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3080 | // If we have ((X & C) == C), turn it into ((X & C) != 0). |
Chris Lattner | ee59d4b | 2004-06-10 02:33:20 +0000 | [diff] [blame] | 3081 | if (CI == BOC && isOneBitSet(CI)) |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3082 | return new SetCondInst(isSetNE ? Instruction::SetEQ : |
| 3083 | Instruction::SetNE, Op0, |
| 3084 | Constant::getNullValue(CI->getType())); |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3085 | |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3086 | // Replace (and X, (1 << size(X)-1) != 0) with x < 0, converting X |
| 3087 | // to be a signed value as appropriate. |
| 3088 | if (isSignBit(BOC)) { |
| 3089 | Value *X = BO->getOperand(0); |
| 3090 | // If 'X' is not signed, insert a cast now... |
| 3091 | if (!BOC->getType()->isSigned()) { |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 3092 | const Type *DestTy = BOC->getType()->getSignedVersion(); |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 3093 | X = InsertCastBefore(X, DestTy, I); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3094 | } |
| 3095 | return new SetCondInst(isSetNE ? Instruction::SetLT : |
| 3096 | Instruction::SetGE, X, |
| 3097 | Constant::getNullValue(X->getType())); |
| 3098 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3099 | |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 3100 | // ((X & ~7) == 0) --> X < 8 |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3101 | if (CI->isNullValue() && isHighOnes(BOC)) { |
| 3102 | Value *X = BO->getOperand(0); |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 3103 | Constant *NegX = ConstantExpr::getNeg(BOC); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3104 | |
| 3105 | // If 'X' is signed, insert a cast now. |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 3106 | if (NegX->getType()->isSigned()) { |
| 3107 | const Type *DestTy = NegX->getType()->getUnsignedVersion(); |
| 3108 | X = InsertCastBefore(X, DestTy, I); |
| 3109 | NegX = ConstantExpr::getCast(NegX, DestTy); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3110 | } |
| 3111 | |
| 3112 | return new SetCondInst(isSetNE ? Instruction::SetGE : |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 3113 | Instruction::SetLT, X, NegX); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 3116 | } |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3117 | default: break; |
| 3118 | } |
| 3119 | } |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3120 | } else { // Not a SetEQ/SetNE |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3121 | // 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] | 3122 | if (CastInst *Cast = dyn_cast<CastInst>(Op0)) { |
| 3123 | Value *CastOp = Cast->getOperand(0); |
| 3124 | const Type *SrcTy = CastOp->getType(); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3125 | unsigned SrcTySize = SrcTy->getPrimitiveSizeInBits(); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3126 | if (SrcTy != Cast->getType() && SrcTy->isInteger() && |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3127 | SrcTySize == Cast->getType()->getPrimitiveSizeInBits()) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3128 | assert((SrcTy->isSigned() ^ Cast->getType()->isSigned()) && |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3129 | "Source and destination signednesses should differ!"); |
| 3130 | if (Cast->getType()->isSigned()) { |
| 3131 | // If this is a signed comparison, check for comparisons in the |
| 3132 | // vicinity of zero. |
| 3133 | if (I.getOpcode() == Instruction::SetLT && CI->isNullValue()) |
| 3134 | // X < 0 => x > 127 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3135 | return BinaryOperator::createSetGT(CastOp, |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3136 | ConstantUInt::get(SrcTy, (1ULL << (SrcTySize-1))-1)); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3137 | else if (I.getOpcode() == Instruction::SetGT && |
| 3138 | cast<ConstantSInt>(CI)->getValue() == -1) |
| 3139 | // X > -1 => x < 128 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3140 | return BinaryOperator::createSetLT(CastOp, |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3141 | ConstantUInt::get(SrcTy, 1ULL << (SrcTySize-1))); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3142 | } else { |
| 3143 | ConstantUInt *CUI = cast<ConstantUInt>(CI); |
| 3144 | if (I.getOpcode() == Instruction::SetLT && |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3145 | CUI->getValue() == 1ULL << (SrcTySize-1)) |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3146 | // X < 128 => X > -1 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3147 | return BinaryOperator::createSetGT(CastOp, |
| 3148 | ConstantSInt::get(SrcTy, -1)); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3149 | else if (I.getOpcode() == Instruction::SetGT && |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3150 | CUI->getValue() == (1ULL << (SrcTySize-1))-1) |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3151 | // X > 127 => X < 0 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3152 | return BinaryOperator::createSetLT(CastOp, |
| 3153 | Constant::getNullValue(SrcTy)); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3154 | } |
| 3155 | } |
| 3156 | } |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 3157 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3158 | } |
| 3159 | |
Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 3160 | // Handle setcc with constant RHS's that can be integer, FP or pointer. |
| 3161 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 3162 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 3163 | switch (LHSI->getOpcode()) { |
Chris Lattner | a816eee | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 3164 | case Instruction::GetElementPtr: |
| 3165 | if (RHSC->isNullValue()) { |
| 3166 | // Transform setcc GEP P, int 0, int 0, int 0, null -> setcc P, null |
| 3167 | bool isAllZeros = true; |
| 3168 | for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i) |
| 3169 | if (!isa<Constant>(LHSI->getOperand(i)) || |
| 3170 | !cast<Constant>(LHSI->getOperand(i))->isNullValue()) { |
| 3171 | isAllZeros = false; |
| 3172 | break; |
| 3173 | } |
| 3174 | if (isAllZeros) |
| 3175 | return new SetCondInst(I.getOpcode(), LHSI->getOperand(0), |
| 3176 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
| 3177 | } |
| 3178 | break; |
| 3179 | |
Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 3180 | case Instruction::PHI: |
| 3181 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3182 | return NV; |
| 3183 | break; |
| 3184 | case Instruction::Select: |
| 3185 | // If either operand of the select is a constant, we can fold the |
| 3186 | // comparison into the select arms, which will cause one to be |
| 3187 | // constant folded and the select turned into a bitwise or. |
| 3188 | Value *Op1 = 0, *Op2 = 0; |
| 3189 | if (LHSI->hasOneUse()) { |
| 3190 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 3191 | // Fold the known value into the constant operand. |
| 3192 | Op1 = ConstantExpr::get(I.getOpcode(), C, RHSC); |
| 3193 | // Insert a new SetCC of the other select operand. |
| 3194 | Op2 = InsertNewInstBefore(new SetCondInst(I.getOpcode(), |
| 3195 | LHSI->getOperand(2), RHSC, |
| 3196 | I.getName()), I); |
| 3197 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 3198 | // Fold the known value into the constant operand. |
| 3199 | Op2 = ConstantExpr::get(I.getOpcode(), C, RHSC); |
| 3200 | // Insert a new SetCC of the other select operand. |
| 3201 | Op1 = InsertNewInstBefore(new SetCondInst(I.getOpcode(), |
| 3202 | LHSI->getOperand(1), RHSC, |
| 3203 | I.getName()), I); |
| 3204 | } |
| 3205 | } |
Jeff Cohen | 8263985 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 3206 | |
Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 3207 | if (Op1) |
| 3208 | return new SelectInst(LHSI->getOperand(0), Op1, Op2); |
| 3209 | break; |
| 3210 | } |
| 3211 | } |
| 3212 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3213 | // If we can optimize a 'setcc GEP, P' or 'setcc P, GEP', do so now. |
| 3214 | if (User *GEP = dyn_castGetElementPtr(Op0)) |
| 3215 | if (Instruction *NI = FoldGEPSetCC(GEP, Op1, I.getOpcode(), I)) |
| 3216 | return NI; |
| 3217 | if (User *GEP = dyn_castGetElementPtr(Op1)) |
| 3218 | if (Instruction *NI = FoldGEPSetCC(GEP, Op0, |
| 3219 | SetCondInst::getSwappedCondition(I.getOpcode()), I)) |
| 3220 | return NI; |
| 3221 | |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 3222 | // Test to see if the operands of the setcc are casted versions of other |
| 3223 | // 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] | 3224 | if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
| 3225 | Value *CastOp0 = CI->getOperand(0); |
| 3226 | if (CastOp0->getType()->isLosslesslyConvertibleTo(CI->getType()) && |
Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 3227 | (isa<Constant>(Op1) || isa<CastInst>(Op1)) && |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 3228 | (I.getOpcode() == Instruction::SetEQ || |
| 3229 | I.getOpcode() == Instruction::SetNE)) { |
| 3230 | // We keep moving the cast from the left operand over to the right |
| 3231 | // operand, where it can often be eliminated completely. |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 3232 | Op0 = CastOp0; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3233 | |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 3234 | // If operand #1 is a cast instruction, see if we can eliminate it as |
| 3235 | // well. |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 3236 | if (CastInst *CI2 = dyn_cast<CastInst>(Op1)) |
| 3237 | if (CI2->getOperand(0)->getType()->isLosslesslyConvertibleTo( |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 3238 | Op0->getType())) |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 3239 | Op1 = CI2->getOperand(0); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3240 | |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 3241 | // If Op1 is a constant, we can fold the cast into the constant. |
| 3242 | if (Op1->getType() != Op0->getType()) |
| 3243 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
| 3244 | Op1 = ConstantExpr::getCast(Op1C, Op0->getType()); |
| 3245 | } else { |
| 3246 | // Otherwise, cast the RHS right before the setcc |
| 3247 | Op1 = new CastInst(Op1, Op0->getType(), Op1->getName()); |
| 3248 | InsertNewInstBefore(cast<Instruction>(Op1), I); |
| 3249 | } |
| 3250 | return BinaryOperator::create(I.getOpcode(), Op0, Op1); |
| 3251 | } |
| 3252 | |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 3253 | // Handle the special case of: setcc (cast bool to X), <cst> |
| 3254 | // This comes up when you have code like |
| 3255 | // int X = A < B; |
| 3256 | // if (X) ... |
| 3257 | // For generality, we handle any zero-extension of any operand comparison |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3258 | // with a constant or another cast from the same type. |
| 3259 | if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1)) |
| 3260 | if (Instruction *R = visitSetCondInstWithCastAndCast(I)) |
| 3261 | return R; |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 3262 | } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3263 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3264 | } |
| 3265 | |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3266 | // visitSetCondInstWithCastAndCast - Handle setcond (cast x to y), (cast/cst). |
| 3267 | // We only handle extending casts so far. |
| 3268 | // |
| 3269 | Instruction *InstCombiner::visitSetCondInstWithCastAndCast(SetCondInst &SCI) { |
| 3270 | Value *LHSCIOp = cast<CastInst>(SCI.getOperand(0))->getOperand(0); |
| 3271 | const Type *SrcTy = LHSCIOp->getType(); |
| 3272 | const Type *DestTy = SCI.getOperand(0)->getType(); |
| 3273 | Value *RHSCIOp; |
| 3274 | |
| 3275 | if (!DestTy->isIntegral() || !SrcTy->isIntegral()) |
Chris Lattner | 03f06f1 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 3276 | return 0; |
| 3277 | |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3278 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); |
| 3279 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); |
| 3280 | if (SrcBits >= DestBits) return 0; // Only handle extending cast. |
| 3281 | |
| 3282 | // Is this a sign or zero extension? |
| 3283 | bool isSignSrc = SrcTy->isSigned(); |
| 3284 | bool isSignDest = DestTy->isSigned(); |
| 3285 | |
| 3286 | if (CastInst *CI = dyn_cast<CastInst>(SCI.getOperand(1))) { |
| 3287 | // Not an extension from the same type? |
| 3288 | RHSCIOp = CI->getOperand(0); |
| 3289 | if (RHSCIOp->getType() != LHSCIOp->getType()) return 0; |
| 3290 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(SCI.getOperand(1))) { |
| 3291 | // Compute the constant that would happen if we truncated to SrcTy then |
| 3292 | // reextended to DestTy. |
| 3293 | Constant *Res = ConstantExpr::getCast(CI, SrcTy); |
| 3294 | |
| 3295 | if (ConstantExpr::getCast(Res, DestTy) == CI) { |
| 3296 | RHSCIOp = Res; |
| 3297 | } else { |
| 3298 | // If the value cannot be represented in the shorter type, we cannot emit |
| 3299 | // a simple comparison. |
| 3300 | if (SCI.getOpcode() == Instruction::SetEQ) |
| 3301 | return ReplaceInstUsesWith(SCI, ConstantBool::False); |
| 3302 | if (SCI.getOpcode() == Instruction::SetNE) |
| 3303 | return ReplaceInstUsesWith(SCI, ConstantBool::True); |
| 3304 | |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3305 | // Evaluate the comparison for LT. |
| 3306 | Value *Result; |
| 3307 | if (DestTy->isSigned()) { |
| 3308 | // We're performing a signed comparison. |
| 3309 | if (isSignSrc) { |
| 3310 | // Signed extend and signed comparison. |
| 3311 | if (cast<ConstantSInt>(CI)->getValue() < 0) // X < (small) --> false |
| 3312 | Result = ConstantBool::False; |
| 3313 | else |
| 3314 | Result = ConstantBool::True; // X < (large) --> true |
| 3315 | } else { |
| 3316 | // Unsigned extend and signed comparison. |
| 3317 | if (cast<ConstantSInt>(CI)->getValue() < 0) |
| 3318 | Result = ConstantBool::False; |
| 3319 | else |
| 3320 | Result = ConstantBool::True; |
| 3321 | } |
| 3322 | } else { |
| 3323 | // We're performing an unsigned comparison. |
| 3324 | if (!isSignSrc) { |
| 3325 | // Unsigned extend & compare -> always true. |
| 3326 | Result = ConstantBool::True; |
| 3327 | } else { |
| 3328 | // We're performing an unsigned comp with a sign extended value. |
| 3329 | // This is true if the input is >= 0. [aka >s -1] |
| 3330 | Constant *NegOne = ConstantIntegral::getAllOnesValue(SrcTy); |
| 3331 | Result = InsertNewInstBefore(BinaryOperator::createSetGT(LHSCIOp, |
| 3332 | NegOne, SCI.getName()), SCI); |
| 3333 | } |
Reid Spencer | 279fa25 | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 3334 | } |
Chris Lattner | 03f06f1 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 3335 | |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3336 | // Finally, return the value computed. |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3337 | if (SCI.getOpcode() == Instruction::SetLT) { |
| 3338 | return ReplaceInstUsesWith(SCI, Result); |
| 3339 | } else { |
| 3340 | assert(SCI.getOpcode()==Instruction::SetGT &&"SetCC should be folded!"); |
| 3341 | if (Constant *CI = dyn_cast<Constant>(Result)) |
| 3342 | return ReplaceInstUsesWith(SCI, ConstantExpr::getNot(CI)); |
| 3343 | else |
| 3344 | return BinaryOperator::createNot(Result); |
| 3345 | } |
Chris Lattner | 03f06f1 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 3346 | } |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3347 | } else { |
| 3348 | return 0; |
Reid Spencer | 279fa25 | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 3349 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3350 | |
Chris Lattner | 252a845 | 2005-06-16 03:00:08 +0000 | [diff] [blame] | 3351 | // Okay, just insert a compare of the reduced operands now! |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3352 | return BinaryOperator::create(SCI.getOpcode(), LHSCIOp, RHSCIOp); |
| 3353 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3354 | |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 3355 | Instruction *InstCombiner::visitShiftInst(ShiftInst &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3356 | assert(I.getOperand(1)->getType() == Type::UByteTy); |
| 3357 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3358 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3359 | |
| 3360 | // shl X, 0 == X and shr X, 0 == X |
| 3361 | // shl 0, X == 0 and shr 0, X == 0 |
| 3362 | if (Op1 == Constant::getNullValue(Type::UByteTy) || |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3363 | Op0 == Constant::getNullValue(Op0->getType())) |
| 3364 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3365 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3366 | if (isa<UndefValue>(Op0)) { // undef >>s X -> undef |
| 3367 | if (!isLeftShift && I.getType()->isSigned()) |
Chris Lattner | 67f0545 | 2004-10-16 23:28:04 +0000 | [diff] [blame] | 3368 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3369 | else // undef << X -> 0 AND undef >>u X -> 0 |
| 3370 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3371 | } |
| 3372 | if (isa<UndefValue>(Op1)) { |
Chris Lattner | 18aa4d8 | 2005-07-20 18:49:28 +0000 | [diff] [blame] | 3373 | if (isLeftShift || I.getType()->isUnsigned())// X << undef, X >>u undef -> 0 |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3374 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3375 | else |
| 3376 | return ReplaceInstUsesWith(I, Op0); // X >>s undef -> X |
| 3377 | } |
| 3378 | |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3379 | // shr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 3380 | if (!isLeftShift) |
| 3381 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0)) |
| 3382 | if (CSI->isAllOnesValue()) |
| 3383 | return ReplaceInstUsesWith(I, CSI); |
| 3384 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3385 | // Try to fold constant and into select arguments. |
| 3386 | if (isa<Constant>(Op0)) |
| 3387 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3388 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3389 | return R; |
| 3390 | |
Chris Lattner | b18dbbf | 2005-05-08 17:34:56 +0000 | [diff] [blame] | 3391 | // See if we can turn a signed shr into an unsigned shr. |
| 3392 | if (!isLeftShift && I.getType()->isSigned()) { |
| 3393 | if (MaskedValueIsZero(Op0, ConstantInt::getMinValue(I.getType()))) { |
| 3394 | Value *V = InsertCastBefore(Op0, I.getType()->getUnsignedVersion(), I); |
| 3395 | V = InsertNewInstBefore(new ShiftInst(Instruction::Shr, V, Op1, |
| 3396 | I.getName()), I); |
| 3397 | return new CastInst(V, I.getType()); |
| 3398 | } |
| 3399 | } |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3400 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3401 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) { |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3402 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr |
| 3403 | // of a signed value. |
| 3404 | // |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3405 | unsigned TypeBits = Op0->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | f5ce254 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 3406 | if (CUI->getValue() >= TypeBits) { |
| 3407 | if (!Op0->getType()->isSigned() || isLeftShift) |
| 3408 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
| 3409 | else { |
| 3410 | I.setOperand(1, ConstantUInt::get(Type::UByteTy, TypeBits-1)); |
| 3411 | return &I; |
| 3412 | } |
| 3413 | } |
Chris Lattner | 55f3d94 | 2002-09-10 23:04:09 +0000 | [diff] [blame] | 3414 | |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 3415 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 3416 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 3417 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 3418 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3419 | return BinaryOperator::createMul(BO->getOperand(0), |
| 3420 | ConstantExpr::getShl(BOOp, CUI)); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3421 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3422 | // Try to fold constant and into select arguments. |
| 3423 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3424 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3425 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3426 | if (isa<PHINode>(Op0)) |
| 3427 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3428 | return NV; |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 3429 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3430 | if (Op0->hasOneUse()) { |
| 3431 | // If this is a SHL of a sign-extending cast, see if we can turn the input |
| 3432 | // into a zero extending cast (a simple strength reduction). |
| 3433 | if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
| 3434 | const Type *SrcTy = CI->getOperand(0)->getType(); |
| 3435 | if (isLeftShift && SrcTy->isInteger() && SrcTy->isSigned() && |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3436 | SrcTy->getPrimitiveSizeInBits() < |
| 3437 | CI->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3438 | // We can change it to a zero extension if we are shifting out all of |
| 3439 | // the sign extended bits. To check this, form a mask of all of the |
| 3440 | // sign extend bits, then shift them left and see if we have anything |
| 3441 | // left. |
| 3442 | Constant *Mask = ConstantIntegral::getAllOnesValue(SrcTy); // 1111 |
| 3443 | Mask = ConstantExpr::getZeroExtend(Mask, CI->getType()); // 00001111 |
| 3444 | Mask = ConstantExpr::getNot(Mask); // 1's in the sign bits: 11110000 |
| 3445 | if (ConstantExpr::getShl(Mask, CUI)->isNullValue()) { |
| 3446 | // If the shift is nuking all of the sign bits, change this to a |
| 3447 | // zero extension cast. To do this, cast the cast input to |
| 3448 | // unsigned, then to the requested size. |
| 3449 | Value *CastOp = CI->getOperand(0); |
| 3450 | Instruction *NC = |
| 3451 | new CastInst(CastOp, CastOp->getType()->getUnsignedVersion(), |
| 3452 | CI->getName()+".uns"); |
| 3453 | NC = InsertNewInstBefore(NC, I); |
| 3454 | // Finally, insert a replacement for CI. |
| 3455 | NC = new CastInst(NC, CI->getType(), CI->getName()); |
| 3456 | CI->setName(""); |
| 3457 | NC = InsertNewInstBefore(NC, I); |
| 3458 | WorkList.push_back(CI); // Delete CI later. |
| 3459 | I.setOperand(0, NC); |
| 3460 | return &I; // The SHL operand was modified. |
| 3461 | } |
| 3462 | } |
| 3463 | } |
| 3464 | |
Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 3465 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) { |
| 3466 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
Jeff Cohen | 572910c | 2005-10-07 05:28:29 +0000 | [diff] [blame] | 3467 | Value *V1, *V2; |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 3468 | ConstantInt *CC; |
Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 3469 | switch (Op0BO->getOpcode()) { |
| 3470 | default: break; |
| 3471 | case Instruction::Add: |
| 3472 | case Instruction::And: |
| 3473 | case Instruction::Or: |
| 3474 | case Instruction::Xor: |
| 3475 | // These operators commute. |
| 3476 | // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 3477 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
| 3478 | match(Op0BO->getOperand(1), |
| 3479 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == CUI) { |
| 3480 | Instruction *YS = new ShiftInst(Instruction::Shl, |
| 3481 | Op0BO->getOperand(0), CUI, |
| 3482 | Op0BO->getName()); |
| 3483 | InsertNewInstBefore(YS, I); // (Y << C) |
| 3484 | Instruction *X = BinaryOperator::create(Op0BO->getOpcode(), YS, |
| 3485 | V1, |
| 3486 | Op0BO->getOperand(1)->getName()); |
| 3487 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
| 3488 | Constant *C2 = ConstantInt::getAllOnesValue(X->getType()); |
| 3489 | C2 = ConstantExpr::getShl(C2, CUI); |
| 3490 | return BinaryOperator::createAnd(X, C2); |
| 3491 | } |
| 3492 | |
| 3493 | // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C)) |
| 3494 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
| 3495 | match(Op0BO->getOperand(1), |
| 3496 | m_And(m_Shr(m_Value(V1), m_Value(V2)), |
| 3497 | m_ConstantInt(CC))) && V2 == CUI && |
| 3498 | cast<BinaryOperator>(Op0BO->getOperand(1))->getOperand(0)->hasOneUse()) { |
| 3499 | Instruction *YS = new ShiftInst(Instruction::Shl, |
| 3500 | Op0BO->getOperand(0), CUI, |
| 3501 | Op0BO->getName()); |
| 3502 | InsertNewInstBefore(YS, I); // (Y << C) |
| 3503 | Instruction *XM = |
| 3504 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, CUI), |
| 3505 | V1->getName()+".mask"); |
| 3506 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 3507 | |
| 3508 | return BinaryOperator::create(Op0BO->getOpcode(), YS, XM); |
| 3509 | } |
| 3510 | |
| 3511 | // FALL THROUGH. |
Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 3512 | case Instruction::Sub: |
| 3513 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 3514 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 3515 | match(Op0BO->getOperand(0), |
| 3516 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == CUI) { |
| 3517 | Instruction *YS = new ShiftInst(Instruction::Shl, |
| 3518 | Op0BO->getOperand(1), CUI, |
| 3519 | Op0BO->getName()); |
| 3520 | InsertNewInstBefore(YS, I); // (Y << C) |
| 3521 | Instruction *X = BinaryOperator::create(Op0BO->getOpcode(), YS, |
| 3522 | V1, |
| 3523 | Op0BO->getOperand(0)->getName()); |
| 3524 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
| 3525 | Constant *C2 = ConstantInt::getAllOnesValue(X->getType()); |
| 3526 | C2 = ConstantExpr::getShl(C2, CUI); |
| 3527 | return BinaryOperator::createAnd(X, C2); |
| 3528 | } |
| 3529 | |
| 3530 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 3531 | match(Op0BO->getOperand(0), |
| 3532 | m_And(m_Shr(m_Value(V1), m_Value(V2)), |
| 3533 | m_ConstantInt(CC))) && V2 == CUI && |
| 3534 | cast<BinaryOperator>(Op0BO->getOperand(0))->getOperand(0)->hasOneUse()) { |
| 3535 | Instruction *YS = new ShiftInst(Instruction::Shl, |
| 3536 | Op0BO->getOperand(1), CUI, |
| 3537 | Op0BO->getName()); |
| 3538 | InsertNewInstBefore(YS, I); // (Y << C) |
| 3539 | Instruction *XM = |
| 3540 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, CUI), |
| 3541 | V1->getName()+".mask"); |
| 3542 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 3543 | |
| 3544 | return BinaryOperator::create(Op0BO->getOpcode(), YS, XM); |
| 3545 | } |
| 3546 | |
Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 3547 | break; |
| 3548 | } |
| 3549 | |
| 3550 | |
| 3551 | // If the operand is an bitwise operator with a constant RHS, and the |
| 3552 | // shift is the only use, we can pull it out of the shift. |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3553 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 3554 | bool isValid = true; // Valid only for And, Or, Xor |
| 3555 | bool highBitSet = false; // Transform if high bit of constant set? |
| 3556 | |
| 3557 | switch (Op0BO->getOpcode()) { |
| 3558 | default: isValid = false; break; // Do not perform transform! |
Chris Lattner | 44bd392 | 2004-10-08 03:46:20 +0000 | [diff] [blame] | 3559 | case Instruction::Add: |
| 3560 | isValid = isLeftShift; |
| 3561 | break; |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3562 | case Instruction::Or: |
| 3563 | case Instruction::Xor: |
| 3564 | highBitSet = false; |
| 3565 | break; |
| 3566 | case Instruction::And: |
| 3567 | highBitSet = true; |
| 3568 | break; |
| 3569 | } |
| 3570 | |
| 3571 | // If this is a signed shift right, and the high bit is modified |
| 3572 | // by the logical operation, do not perform the transformation. |
| 3573 | // The highBitSet boolean indicates the value of the high bit of |
| 3574 | // the constant which would cause it to be modified for this |
| 3575 | // operation. |
| 3576 | // |
| 3577 | if (isValid && !isLeftShift && !I.getType()->isUnsigned()) { |
| 3578 | uint64_t Val = Op0C->getRawValue(); |
| 3579 | isValid = ((Val & (1 << (TypeBits-1))) != 0) == highBitSet; |
| 3580 | } |
| 3581 | |
| 3582 | if (isValid) { |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3583 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, CUI); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3584 | |
| 3585 | Instruction *NewShift = |
| 3586 | new ShiftInst(I.getOpcode(), Op0BO->getOperand(0), CUI, |
| 3587 | Op0BO->getName()); |
| 3588 | Op0BO->setName(""); |
| 3589 | InsertNewInstBefore(NewShift, I); |
| 3590 | |
| 3591 | return BinaryOperator::create(Op0BO->getOpcode(), NewShift, |
| 3592 | NewRHS); |
| 3593 | } |
| 3594 | } |
Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 3595 | } |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3596 | } |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3597 | |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3598 | // If this is a shift of a shift, see if we can fold the two together... |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3599 | if (ShiftInst *Op0SI = dyn_cast<ShiftInst>(Op0)) |
Chris Lattner | ab780df | 2003-07-24 18:38:56 +0000 | [diff] [blame] | 3600 | if (ConstantUInt *ShiftAmt1C = |
| 3601 | dyn_cast<ConstantUInt>(Op0SI->getOperand(1))) { |
Chris Lattner | fdfe3e49 | 2005-01-08 19:42:22 +0000 | [diff] [blame] | 3602 | unsigned ShiftAmt1 = (unsigned)ShiftAmt1C->getValue(); |
| 3603 | unsigned ShiftAmt2 = (unsigned)CUI->getValue(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3604 | |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3605 | // Check for (A << c1) << c2 and (A >> c1) >> c2 |
| 3606 | if (I.getOpcode() == Op0SI->getOpcode()) { |
| 3607 | unsigned Amt = ShiftAmt1+ShiftAmt2; // Fold into one big shift... |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3608 | if (Op0->getType()->getPrimitiveSizeInBits() < Amt) |
| 3609 | Amt = Op0->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3610 | return new ShiftInst(I.getOpcode(), Op0SI->getOperand(0), |
| 3611 | ConstantUInt::get(Type::UByteTy, Amt)); |
| 3612 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3613 | |
Chris Lattner | ab780df | 2003-07-24 18:38:56 +0000 | [diff] [blame] | 3614 | // Check for (A << c1) >> c2 or visaversa. If we are dealing with |
| 3615 | // signed types, we can only support the (A >> c1) << c2 configuration, |
| 3616 | // because it can not turn an arbitrary bit of A into a sign bit. |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3617 | if (I.getType()->isUnsigned() || isLeftShift) { |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3618 | // Calculate bitmask for what gets shifted off the edge... |
| 3619 | Constant *C = ConstantIntegral::getAllOnesValue(I.getType()); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3620 | if (isLeftShift) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3621 | C = ConstantExpr::getShl(C, ShiftAmt1C); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3622 | else |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3623 | C = ConstantExpr::getShr(C, ShiftAmt1C); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3624 | |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3625 | Instruction *Mask = |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3626 | BinaryOperator::createAnd(Op0SI->getOperand(0), C, |
| 3627 | Op0SI->getOperand(0)->getName()+".mask"); |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3628 | InsertNewInstBefore(Mask, I); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3629 | |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3630 | // Figure out what flavor of shift we should use... |
| 3631 | if (ShiftAmt1 == ShiftAmt2) |
| 3632 | return ReplaceInstUsesWith(I, Mask); // (A << c) >> c === A & c2 |
| 3633 | else if (ShiftAmt1 < ShiftAmt2) { |
| 3634 | return new ShiftInst(I.getOpcode(), Mask, |
| 3635 | ConstantUInt::get(Type::UByteTy, ShiftAmt2-ShiftAmt1)); |
| 3636 | } else { |
| 3637 | return new ShiftInst(Op0SI->getOpcode(), Mask, |
| 3638 | ConstantUInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2)); |
| 3639 | } |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 3640 | } else { |
| 3641 | // We can handle signed (X << C1) >> C2 if it's a sign extend. In |
| 3642 | // this case, C1 == C2 and C1 is 8, 16, or 32. |
| 3643 | if (ShiftAmt1 == ShiftAmt2) { |
| 3644 | const Type *SExtType = 0; |
| 3645 | switch (ShiftAmt1) { |
| 3646 | case 8 : SExtType = Type::SByteTy; break; |
| 3647 | case 16: SExtType = Type::ShortTy; break; |
| 3648 | case 32: SExtType = Type::IntTy; break; |
| 3649 | } |
| 3650 | |
| 3651 | if (SExtType) { |
| 3652 | Instruction *NewTrunc = new CastInst(Op0SI->getOperand(0), |
| 3653 | SExtType, "sext"); |
| 3654 | InsertNewInstBefore(NewTrunc, I); |
| 3655 | return new CastInst(NewTrunc, I.getType()); |
| 3656 | } |
| 3657 | } |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3658 | } |
| 3659 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3660 | } |
Chris Lattner | 2e0fb39 | 2002-10-08 16:16:40 +0000 | [diff] [blame] | 3661 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3662 | return 0; |
| 3663 | } |
| 3664 | |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 3665 | enum CastType { |
| 3666 | Noop = 0, |
| 3667 | Truncate = 1, |
| 3668 | Signext = 2, |
| 3669 | Zeroext = 3 |
| 3670 | }; |
| 3671 | |
| 3672 | /// getCastType - In the future, we will split the cast instruction into these |
| 3673 | /// various types. Until then, we have to do the analysis here. |
| 3674 | static CastType getCastType(const Type *Src, const Type *Dest) { |
| 3675 | assert(Src->isIntegral() && Dest->isIntegral() && |
| 3676 | "Only works on integral types!"); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3677 | unsigned SrcSize = Src->getPrimitiveSizeInBits(); |
| 3678 | unsigned DestSize = Dest->getPrimitiveSizeInBits(); |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 3679 | |
| 3680 | if (SrcSize == DestSize) return Noop; |
| 3681 | if (SrcSize > DestSize) return Truncate; |
| 3682 | if (Src->isSigned()) return Signext; |
| 3683 | return Zeroext; |
| 3684 | } |
| 3685 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3686 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3687 | // isEliminableCastOfCast - Return true if it is valid to eliminate the CI |
| 3688 | // instruction. |
| 3689 | // |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3690 | static inline bool isEliminableCastOfCast(const Type *SrcTy, const Type *MidTy, |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 3691 | const Type *DstTy, TargetData *TD) { |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3692 | |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3693 | // 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] | 3694 | // are identical and the bits don't get reinterpreted (for example |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 3695 | // int->float->int would not be allowed). |
Misha Brukman | e5838c4 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 3696 | if (SrcTy == DstTy && SrcTy->isLosslesslyConvertibleTo(MidTy)) |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3697 | return true; |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3698 | |
Chris Lattner | 4fbad96 | 2004-07-21 04:27:24 +0000 | [diff] [blame] | 3699 | // If we are casting between pointer and integer types, treat pointers as |
| 3700 | // integers of the appropriate size for the code below. |
| 3701 | if (isa<PointerType>(SrcTy)) SrcTy = TD->getIntPtrType(); |
| 3702 | if (isa<PointerType>(MidTy)) MidTy = TD->getIntPtrType(); |
| 3703 | if (isa<PointerType>(DstTy)) DstTy = TD->getIntPtrType(); |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 3704 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3705 | // Allow free casting and conversion of sizes as long as the sign doesn't |
| 3706 | // change... |
Chris Lattner | b0b412e | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 3707 | if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral()) { |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 3708 | CastType FirstCast = getCastType(SrcTy, MidTy); |
| 3709 | CastType SecondCast = getCastType(MidTy, DstTy); |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3710 | |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 3711 | // Capture the effect of these two casts. If the result is a legal cast, |
| 3712 | // the CastType is stored here, otherwise a special code is used. |
| 3713 | static const unsigned CastResult[] = { |
| 3714 | // First cast is noop |
| 3715 | 0, 1, 2, 3, |
| 3716 | // First cast is a truncate |
| 3717 | 1, 1, 4, 4, // trunc->extend is not safe to eliminate |
| 3718 | // First cast is a sign ext |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 3719 | 2, 5, 2, 4, // signext->zeroext never ok |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 3720 | // First cast is a zero ext |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 3721 | 3, 5, 3, 3, |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 3722 | }; |
| 3723 | |
| 3724 | unsigned Result = CastResult[FirstCast*4+SecondCast]; |
| 3725 | switch (Result) { |
| 3726 | default: assert(0 && "Illegal table value!"); |
| 3727 | case 0: |
| 3728 | case 1: |
| 3729 | case 2: |
| 3730 | case 3: |
| 3731 | // FIXME: in the future, when LLVM has explicit sign/zeroextends and |
| 3732 | // truncates, we could eliminate more casts. |
| 3733 | return (unsigned)getCastType(SrcTy, DstTy) == Result; |
| 3734 | case 4: |
| 3735 | return false; // Not possible to eliminate this here. |
| 3736 | case 5: |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 3737 | // Sign or zero extend followed by truncate is always ok if the result |
| 3738 | // is a truncate or noop. |
| 3739 | CastType ResultCast = getCastType(SrcTy, DstTy); |
| 3740 | if (ResultCast == Noop || ResultCast == Truncate) |
| 3741 | return true; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3742 | // 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] | 3743 | // result will match the sign/zeroextendness of the result. |
| 3744 | return ResultCast == FirstCast; |
Chris Lattner | 3732aca | 2002-08-15 16:15:25 +0000 | [diff] [blame] | 3745 | } |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3746 | } |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3747 | return false; |
| 3748 | } |
| 3749 | |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 3750 | static bool ValueRequiresCast(const Value *V, const Type *Ty, TargetData *TD) { |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3751 | if (V->getType() == Ty || isa<Constant>(V)) return false; |
| 3752 | if (const CastInst *CI = dyn_cast<CastInst>(V)) |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 3753 | if (isEliminableCastOfCast(CI->getOperand(0)->getType(), CI->getType(), Ty, |
| 3754 | TD)) |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3755 | return false; |
| 3756 | return true; |
| 3757 | } |
| 3758 | |
| 3759 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 3760 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 3761 | /// casts that are known to not do anything... |
| 3762 | /// |
| 3763 | Value *InstCombiner::InsertOperandCastBefore(Value *V, const Type *DestTy, |
| 3764 | Instruction *InsertBefore) { |
| 3765 | if (V->getType() == DestTy) return V; |
| 3766 | if (Constant *C = dyn_cast<Constant>(V)) |
| 3767 | return ConstantExpr::getCast(C, DestTy); |
| 3768 | |
| 3769 | CastInst *CI = new CastInst(V, DestTy, V->getName()); |
| 3770 | InsertNewInstBefore(CI, *InsertBefore); |
| 3771 | return CI; |
| 3772 | } |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3773 | |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 3774 | /// PromoteCastOfAllocation - If we find a cast of an allocation instruction, |
| 3775 | /// try to eliminate the cast by moving the type information into the alloc. |
| 3776 | Instruction *InstCombiner::PromoteCastOfAllocation(CastInst &CI, |
| 3777 | AllocationInst &AI) { |
| 3778 | const PointerType *PTy = dyn_cast<PointerType>(CI.getType()); |
| 3779 | if (AI.isArrayAllocation() || !PTy) return 0; |
| 3780 | |
Chris Lattner | ac87beb | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 3781 | // Remove any uses of AI that are dead. |
| 3782 | assert(!CI.use_empty() && "Dead instructions should be removed earlier!"); |
| 3783 | std::vector<Instruction*> DeadUsers; |
| 3784 | for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) { |
| 3785 | Instruction *User = cast<Instruction>(*UI++); |
| 3786 | if (isInstructionTriviallyDead(User)) { |
| 3787 | while (UI != E && *UI == User) |
| 3788 | ++UI; // If this instruction uses AI more than once, don't break UI. |
| 3789 | |
| 3790 | // Add operands to the worklist. |
| 3791 | AddUsesToWorkList(*User); |
| 3792 | ++NumDeadInst; |
| 3793 | DEBUG(std::cerr << "IC: DCE: " << *User); |
| 3794 | |
| 3795 | User->eraseFromParent(); |
| 3796 | removeFromWorkList(User); |
| 3797 | } |
| 3798 | } |
| 3799 | |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 3800 | // Get the type really allocated and the type casted to. |
| 3801 | const Type *AllocElTy = AI.getAllocatedType(); |
| 3802 | const Type *CastElTy = PTy->getElementType(); |
| 3803 | if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0; |
Chris Lattner | 355ecc0 | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 3804 | |
| 3805 | unsigned AllocElTyAlign = TD->getTypeSize(AllocElTy); |
| 3806 | unsigned CastElTyAlign = TD->getTypeSize(CastElTy); |
| 3807 | if (CastElTyAlign < AllocElTyAlign) return 0; |
| 3808 | |
Chris Lattner | 46705b2 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 3809 | // If the allocation has multiple uses, only promote it if we are strictly |
| 3810 | // increasing the alignment of the resultant allocation. If we keep it the |
| 3811 | // same, we open the door to infinite loops of various kinds. |
| 3812 | if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0; |
| 3813 | |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 3814 | uint64_t AllocElTySize = TD->getTypeSize(AllocElTy); |
| 3815 | uint64_t CastElTySize = TD->getTypeSize(CastElTy); |
Chris Lattner | 355ecc0 | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 3816 | |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 3817 | // If the allocation is for an even multiple of the cast type size |
| 3818 | if (CastElTySize == 0 || AllocElTySize % CastElTySize != 0) |
| 3819 | return 0; |
| 3820 | Value *Amt = ConstantUInt::get(Type::UIntTy, |
| 3821 | AllocElTySize/CastElTySize); |
| 3822 | std::string Name = AI.getName(); AI.setName(""); |
| 3823 | AllocationInst *New; |
| 3824 | if (isa<MallocInst>(AI)) |
| 3825 | New = new MallocInst(CastElTy, Amt, Name); |
| 3826 | else |
| 3827 | New = new AllocaInst(CastElTy, Amt, Name); |
| 3828 | InsertNewInstBefore(New, AI); |
Chris Lattner | 46705b2 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 3829 | |
| 3830 | // If the allocation has multiple uses, insert a cast and change all things |
| 3831 | // that used it to use the new cast. This will also hack on CI, but it will |
| 3832 | // die soon. |
| 3833 | if (!AI.hasOneUse()) { |
| 3834 | AddUsesToWorkList(AI); |
| 3835 | CastInst *NewCast = new CastInst(New, AI.getType(), "tmpcast"); |
| 3836 | InsertNewInstBefore(NewCast, AI); |
| 3837 | AI.replaceAllUsesWith(NewCast); |
| 3838 | } |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 3839 | return ReplaceInstUsesWith(CI, New); |
| 3840 | } |
| 3841 | |
| 3842 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3843 | // CastInst simplification |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 3844 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3845 | Instruction *InstCombiner::visitCastInst(CastInst &CI) { |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 3846 | Value *Src = CI.getOperand(0); |
| 3847 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3848 | // If the user is casting a value to the same type, eliminate this cast |
| 3849 | // instruction... |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 3850 | if (CI.getType() == Src->getType()) |
| 3851 | return ReplaceInstUsesWith(CI, Src); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3852 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3853 | if (isa<UndefValue>(Src)) // cast undef -> undef |
| 3854 | return ReplaceInstUsesWith(CI, UndefValue::get(CI.getType())); |
| 3855 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3856 | // If casting the result of another cast instruction, try to eliminate this |
| 3857 | // one! |
| 3858 | // |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3859 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
| 3860 | Value *A = CSrc->getOperand(0); |
| 3861 | if (isEliminableCastOfCast(A->getType(), CSrc->getType(), |
| 3862 | CI.getType(), TD)) { |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3863 | // This instruction now refers directly to the cast's src operand. This |
| 3864 | // has a good chance of making CSrc dead. |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3865 | CI.setOperand(0, CSrc->getOperand(0)); |
| 3866 | return &CI; |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3867 | } |
| 3868 | |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3869 | // If this is an A->B->A cast, and we are dealing with integral types, try |
| 3870 | // to convert this into a logical 'and' instruction. |
| 3871 | // |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3872 | if (A->getType()->isInteger() && |
Chris Lattner | b0b412e | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 3873 | CI.getType()->isInteger() && CSrc->getType()->isInteger() && |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3874 | CSrc->getType()->isUnsigned() && // B->A cast must zero extend |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3875 | CSrc->getType()->getPrimitiveSizeInBits() < |
| 3876 | CI.getType()->getPrimitiveSizeInBits()&& |
| 3877 | A->getType()->getPrimitiveSizeInBits() == |
| 3878 | CI.getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3879 | assert(CSrc->getType() != Type::ULongTy && |
| 3880 | "Cannot have type bigger than ulong!"); |
Chris Lattner | 2f1457f | 2005-04-24 17:46:05 +0000 | [diff] [blame] | 3881 | uint64_t AndValue = ~0ULL>>(64-CSrc->getType()->getPrimitiveSizeInBits()); |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3882 | Constant *AndOp = ConstantUInt::get(A->getType()->getUnsignedVersion(), |
| 3883 | AndValue); |
| 3884 | AndOp = ConstantExpr::getCast(AndOp, A->getType()); |
| 3885 | Instruction *And = BinaryOperator::createAnd(CSrc->getOperand(0), AndOp); |
| 3886 | if (And->getType() != CI.getType()) { |
| 3887 | And->setName(CSrc->getName()+".mask"); |
| 3888 | InsertNewInstBefore(And, CI); |
| 3889 | And = new CastInst(And, CI.getType()); |
| 3890 | } |
| 3891 | return And; |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3892 | } |
| 3893 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3894 | |
Chris Lattner | 0384165 | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 3895 | // If this is a cast to bool, turn it into the appropriate setne instruction. |
| 3896 | if (CI.getType() == Type::BoolTy) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3897 | return BinaryOperator::createSetNE(CI.getOperand(0), |
Chris Lattner | 0384165 | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 3898 | Constant::getNullValue(CI.getOperand(0)->getType())); |
| 3899 | |
Chris Lattner | d0d5160 | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 3900 | // If casting the result of a getelementptr instruction with no offset, turn |
| 3901 | // this into a cast of the original pointer! |
| 3902 | // |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 3903 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
Chris Lattner | d0d5160 | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 3904 | bool AllZeroOperands = true; |
| 3905 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) |
| 3906 | if (!isa<Constant>(GEP->getOperand(i)) || |
| 3907 | !cast<Constant>(GEP->getOperand(i))->isNullValue()) { |
| 3908 | AllZeroOperands = false; |
| 3909 | break; |
| 3910 | } |
| 3911 | if (AllZeroOperands) { |
| 3912 | CI.setOperand(0, GEP->getOperand(0)); |
| 3913 | return &CI; |
| 3914 | } |
| 3915 | } |
| 3916 | |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 3917 | // If we are casting a malloc or alloca to a pointer to a type of the same |
| 3918 | // size, rewrite the allocation instruction to allocate the "right" type. |
| 3919 | // |
| 3920 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 3921 | if (Instruction *V = PromoteCastOfAllocation(CI, *AI)) |
| 3922 | return V; |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 3923 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3924 | if (SelectInst *SI = dyn_cast<SelectInst>(Src)) |
| 3925 | if (Instruction *NV = FoldOpIntoSelect(CI, SI, this)) |
| 3926 | return NV; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3927 | if (isa<PHINode>(Src)) |
| 3928 | if (Instruction *NV = FoldOpIntoPhi(CI)) |
| 3929 | return NV; |
| 3930 | |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3931 | // If the source value is an instruction with only this use, we can attempt to |
| 3932 | // propagate the cast into the instruction. Also, only handle integral types |
| 3933 | // for now. |
| 3934 | if (Instruction *SrcI = dyn_cast<Instruction>(Src)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 3935 | if (SrcI->hasOneUse() && Src->getType()->isIntegral() && |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3936 | CI.getType()->isInteger()) { // Don't mess with casts to bool here |
| 3937 | const Type *DestTy = CI.getType(); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3938 | unsigned SrcBitSize = Src->getType()->getPrimitiveSizeInBits(); |
| 3939 | unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3940 | |
| 3941 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; |
| 3942 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; |
| 3943 | |
| 3944 | switch (SrcI->getOpcode()) { |
| 3945 | case Instruction::Add: |
| 3946 | case Instruction::Mul: |
| 3947 | case Instruction::And: |
| 3948 | case Instruction::Or: |
| 3949 | case Instruction::Xor: |
| 3950 | // If we are discarding information, or just changing the sign, rewrite. |
| 3951 | if (DestBitSize <= SrcBitSize && DestBitSize != 1) { |
| 3952 | // Don't insert two casts if they cannot be eliminated. We allow two |
| 3953 | // casts to be inserted if the sizes are the same. This could only be |
| 3954 | // converting signedness, which is a noop. |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 3955 | if (DestBitSize == SrcBitSize || !ValueRequiresCast(Op1, DestTy,TD) || |
| 3956 | !ValueRequiresCast(Op0, DestTy, TD)) { |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3957 | Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI); |
| 3958 | Value *Op1c = InsertOperandCastBefore(Op1, DestTy, SrcI); |
| 3959 | return BinaryOperator::create(cast<BinaryOperator>(SrcI) |
| 3960 | ->getOpcode(), Op0c, Op1c); |
| 3961 | } |
| 3962 | } |
Chris Lattner | 7208616 | 2005-05-06 02:07:39 +0000 | [diff] [blame] | 3963 | |
| 3964 | // cast (xor bool X, true) to int --> xor (cast bool X to int), 1 |
| 3965 | if (SrcBitSize == 1 && SrcI->getOpcode() == Instruction::Xor && |
| 3966 | Op1 == ConstantBool::True && |
| 3967 | (!Op0->hasOneUse() || !isa<SetCondInst>(Op0))) { |
| 3968 | Value *New = InsertOperandCastBefore(Op0, DestTy, &CI); |
| 3969 | return BinaryOperator::createXor(New, |
| 3970 | ConstantInt::get(CI.getType(), 1)); |
| 3971 | } |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3972 | break; |
| 3973 | case Instruction::Shl: |
| 3974 | // Allow changing the sign of the source operand. Do not allow changing |
| 3975 | // the size of the shift, UNLESS the shift amount is a constant. We |
| 3976 | // mush not change variable sized shifts to a smaller size, because it |
| 3977 | // is undefined to shift more bits out than exist in the value. |
| 3978 | if (DestBitSize == SrcBitSize || |
| 3979 | (DestBitSize < SrcBitSize && isa<Constant>(Op1))) { |
| 3980 | Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI); |
| 3981 | return new ShiftInst(Instruction::Shl, Op0c, Op1); |
| 3982 | } |
| 3983 | break; |
Chris Lattner | 8738041 | 2005-05-06 04:18:52 +0000 | [diff] [blame] | 3984 | case Instruction::Shr: |
| 3985 | // If this is a signed shr, and if all bits shifted in are about to be |
| 3986 | // truncated off, turn it into an unsigned shr to allow greater |
| 3987 | // simplifications. |
| 3988 | if (DestBitSize < SrcBitSize && Src->getType()->isSigned() && |
| 3989 | isa<ConstantInt>(Op1)) { |
| 3990 | unsigned ShiftAmt = cast<ConstantUInt>(Op1)->getValue(); |
| 3991 | if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) { |
| 3992 | // Convert to unsigned. |
| 3993 | Value *N1 = InsertOperandCastBefore(Op0, |
| 3994 | Op0->getType()->getUnsignedVersion(), &CI); |
| 3995 | // Insert the new shift, which is now unsigned. |
| 3996 | N1 = InsertNewInstBefore(new ShiftInst(Instruction::Shr, N1, |
| 3997 | Op1, Src->getName()), CI); |
| 3998 | return new CastInst(N1, CI.getType()); |
| 3999 | } |
| 4000 | } |
| 4001 | break; |
| 4002 | |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 4003 | case Instruction::SetNE: |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 4004 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 4005 | if (Op1C->getRawValue() == 0) { |
| 4006 | // If the input only has the low bit set, simplify directly. |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4007 | Constant *Not1 = |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 4008 | ConstantExpr::getNot(ConstantInt::get(Op0->getType(), 1)); |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 4009 | // cast (X != 0) to int --> X if X&~1 == 0 |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 4010 | if (MaskedValueIsZero(Op0, cast<ConstantIntegral>(Not1))) { |
| 4011 | if (CI.getType() == Op0->getType()) |
| 4012 | return ReplaceInstUsesWith(CI, Op0); |
| 4013 | else |
| 4014 | return new CastInst(Op0, CI.getType()); |
| 4015 | } |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 4016 | |
| 4017 | // If the input is an and with a single bit, shift then simplify. |
| 4018 | ConstantInt *AndRHS; |
| 4019 | if (match(Op0, m_And(m_Value(), m_ConstantInt(AndRHS)))) |
| 4020 | if (AndRHS->getRawValue() && |
| 4021 | (AndRHS->getRawValue() & (AndRHS->getRawValue()-1)) == 0) { |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 4022 | unsigned ShiftAmt = Log2_64(AndRHS->getRawValue()); |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 4023 | // Perform an unsigned shr by shiftamt. Convert input to |
| 4024 | // unsigned if it is signed. |
| 4025 | Value *In = Op0; |
| 4026 | if (In->getType()->isSigned()) |
| 4027 | In = InsertNewInstBefore(new CastInst(In, |
| 4028 | In->getType()->getUnsignedVersion(), In->getName()),CI); |
| 4029 | // Insert the shift to put the result in the low bit. |
| 4030 | In = InsertNewInstBefore(new ShiftInst(Instruction::Shr, In, |
| 4031 | ConstantInt::get(Type::UByteTy, ShiftAmt), |
| 4032 | In->getName()+".lobit"), CI); |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 4033 | if (CI.getType() == In->getType()) |
| 4034 | return ReplaceInstUsesWith(CI, In); |
| 4035 | else |
| 4036 | return new CastInst(In, CI.getType()); |
| 4037 | } |
| 4038 | } |
| 4039 | } |
| 4040 | break; |
| 4041 | case Instruction::SetEQ: |
| 4042 | // We if we are just checking for a seteq of a single bit and casting it |
| 4043 | // to an integer. If so, shift the bit to the appropriate place then |
| 4044 | // cast to integer to avoid the comparison. |
| 4045 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { |
| 4046 | // Is Op1C a power of two or zero? |
| 4047 | if ((Op1C->getRawValue() & Op1C->getRawValue()-1) == 0) { |
| 4048 | // cast (X == 1) to int -> X iff X has only the low bit set. |
| 4049 | if (Op1C->getRawValue() == 1) { |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4050 | Constant *Not1 = |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 4051 | ConstantExpr::getNot(ConstantInt::get(Op0->getType(), 1)); |
| 4052 | if (MaskedValueIsZero(Op0, cast<ConstantIntegral>(Not1))) { |
| 4053 | if (CI.getType() == Op0->getType()) |
| 4054 | return ReplaceInstUsesWith(CI, Op0); |
| 4055 | else |
| 4056 | return new CastInst(Op0, CI.getType()); |
| 4057 | } |
| 4058 | } |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 4059 | } |
| 4060 | } |
| 4061 | break; |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 4062 | } |
| 4063 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 4064 | return 0; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 4065 | } |
| 4066 | |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 4067 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
| 4068 | /// %C = or %A, %B |
| 4069 | /// %D = select %cond, %C, %A |
| 4070 | /// into: |
| 4071 | /// %C = select %cond, %B, 0 |
| 4072 | /// %D = or %A, %C |
| 4073 | /// |
| 4074 | /// Assuming that the specified instruction is an operand to the select, return |
| 4075 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 4076 | /// equal the other incoming value of the select. |
| 4077 | /// |
| 4078 | static unsigned GetSelectFoldableOperands(Instruction *I) { |
| 4079 | switch (I->getOpcode()) { |
| 4080 | case Instruction::Add: |
| 4081 | case Instruction::Mul: |
| 4082 | case Instruction::And: |
| 4083 | case Instruction::Or: |
| 4084 | case Instruction::Xor: |
| 4085 | return 3; // Can fold through either operand. |
| 4086 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 4087 | case Instruction::Shl: // Can only fold on the shift amount. |
| 4088 | case Instruction::Shr: |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4089 | return 1; |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 4090 | default: |
| 4091 | return 0; // Cannot fold |
| 4092 | } |
| 4093 | } |
| 4094 | |
| 4095 | /// GetSelectFoldableConstant - For the same transformation as the previous |
| 4096 | /// function, return the identity constant that goes into the select. |
| 4097 | static Constant *GetSelectFoldableConstant(Instruction *I) { |
| 4098 | switch (I->getOpcode()) { |
| 4099 | default: assert(0 && "This cannot happen!"); abort(); |
| 4100 | case Instruction::Add: |
| 4101 | case Instruction::Sub: |
| 4102 | case Instruction::Or: |
| 4103 | case Instruction::Xor: |
| 4104 | return Constant::getNullValue(I->getType()); |
| 4105 | case Instruction::Shl: |
| 4106 | case Instruction::Shr: |
| 4107 | return Constant::getNullValue(Type::UByteTy); |
| 4108 | case Instruction::And: |
| 4109 | return ConstantInt::getAllOnesValue(I->getType()); |
| 4110 | case Instruction::Mul: |
| 4111 | return ConstantInt::get(I->getType(), 1); |
| 4112 | } |
| 4113 | } |
| 4114 | |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 4115 | /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI |
| 4116 | /// have the same opcode and only one use each. Try to simplify this. |
| 4117 | Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 4118 | Instruction *FI) { |
| 4119 | if (TI->getNumOperands() == 1) { |
| 4120 | // If this is a non-volatile load or a cast from the same type, |
| 4121 | // merge. |
| 4122 | if (TI->getOpcode() == Instruction::Cast) { |
| 4123 | if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType()) |
| 4124 | return 0; |
| 4125 | } else { |
| 4126 | return 0; // unknown unary op. |
| 4127 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4128 | |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 4129 | // Fold this by inserting a select from the input values. |
| 4130 | SelectInst *NewSI = new SelectInst(SI.getCondition(), TI->getOperand(0), |
| 4131 | FI->getOperand(0), SI.getName()+".v"); |
| 4132 | InsertNewInstBefore(NewSI, SI); |
| 4133 | return new CastInst(NewSI, TI->getType()); |
| 4134 | } |
| 4135 | |
| 4136 | // Only handle binary operators here. |
| 4137 | if (!isa<ShiftInst>(TI) && !isa<BinaryOperator>(TI)) |
| 4138 | return 0; |
| 4139 | |
| 4140 | // Figure out if the operations have any operands in common. |
| 4141 | Value *MatchOp, *OtherOpT, *OtherOpF; |
| 4142 | bool MatchIsOpZero; |
| 4143 | if (TI->getOperand(0) == FI->getOperand(0)) { |
| 4144 | MatchOp = TI->getOperand(0); |
| 4145 | OtherOpT = TI->getOperand(1); |
| 4146 | OtherOpF = FI->getOperand(1); |
| 4147 | MatchIsOpZero = true; |
| 4148 | } else if (TI->getOperand(1) == FI->getOperand(1)) { |
| 4149 | MatchOp = TI->getOperand(1); |
| 4150 | OtherOpT = TI->getOperand(0); |
| 4151 | OtherOpF = FI->getOperand(0); |
| 4152 | MatchIsOpZero = false; |
| 4153 | } else if (!TI->isCommutative()) { |
| 4154 | return 0; |
| 4155 | } else if (TI->getOperand(0) == FI->getOperand(1)) { |
| 4156 | MatchOp = TI->getOperand(0); |
| 4157 | OtherOpT = TI->getOperand(1); |
| 4158 | OtherOpF = FI->getOperand(0); |
| 4159 | MatchIsOpZero = true; |
| 4160 | } else if (TI->getOperand(1) == FI->getOperand(0)) { |
| 4161 | MatchOp = TI->getOperand(1); |
| 4162 | OtherOpT = TI->getOperand(0); |
| 4163 | OtherOpF = FI->getOperand(1); |
| 4164 | MatchIsOpZero = true; |
| 4165 | } else { |
| 4166 | return 0; |
| 4167 | } |
| 4168 | |
| 4169 | // If we reach here, they do have operations in common. |
| 4170 | SelectInst *NewSI = new SelectInst(SI.getCondition(), OtherOpT, |
| 4171 | OtherOpF, SI.getName()+".v"); |
| 4172 | InsertNewInstBefore(NewSI, SI); |
| 4173 | |
| 4174 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) { |
| 4175 | if (MatchIsOpZero) |
| 4176 | return BinaryOperator::create(BO->getOpcode(), MatchOp, NewSI); |
| 4177 | else |
| 4178 | return BinaryOperator::create(BO->getOpcode(), NewSI, MatchOp); |
| 4179 | } else { |
| 4180 | if (MatchIsOpZero) |
| 4181 | return new ShiftInst(cast<ShiftInst>(TI)->getOpcode(), MatchOp, NewSI); |
| 4182 | else |
| 4183 | return new ShiftInst(cast<ShiftInst>(TI)->getOpcode(), NewSI, MatchOp); |
| 4184 | } |
| 4185 | } |
| 4186 | |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 4187 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 4188 | Value *CondVal = SI.getCondition(); |
| 4189 | Value *TrueVal = SI.getTrueValue(); |
| 4190 | Value *FalseVal = SI.getFalseValue(); |
| 4191 | |
| 4192 | // select true, X, Y -> X |
| 4193 | // select false, X, Y -> Y |
| 4194 | if (ConstantBool *C = dyn_cast<ConstantBool>(CondVal)) |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 4195 | if (C == ConstantBool::True) |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 4196 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 4197 | else { |
| 4198 | assert(C == ConstantBool::False); |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 4199 | return ReplaceInstUsesWith(SI, FalseVal); |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 4200 | } |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 4201 | |
| 4202 | // select C, X, X -> X |
| 4203 | if (TrueVal == FalseVal) |
| 4204 | return ReplaceInstUsesWith(SI, TrueVal); |
| 4205 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4206 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 4207 | return ReplaceInstUsesWith(SI, FalseVal); |
| 4208 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 4209 | return ReplaceInstUsesWith(SI, TrueVal); |
| 4210 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 4211 | if (isa<Constant>(TrueVal)) |
| 4212 | return ReplaceInstUsesWith(SI, TrueVal); |
| 4213 | else |
| 4214 | return ReplaceInstUsesWith(SI, FalseVal); |
| 4215 | } |
| 4216 | |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 4217 | if (SI.getType() == Type::BoolTy) |
| 4218 | if (ConstantBool *C = dyn_cast<ConstantBool>(TrueVal)) { |
| 4219 | if (C == ConstantBool::True) { |
| 4220 | // Change: A = select B, true, C --> A = or B, C |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4221 | return BinaryOperator::createOr(CondVal, FalseVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 4222 | } else { |
| 4223 | // Change: A = select B, false, C --> A = and !B, C |
| 4224 | Value *NotCond = |
| 4225 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 4226 | "not."+CondVal->getName()), SI); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4227 | return BinaryOperator::createAnd(NotCond, FalseVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 4228 | } |
| 4229 | } else if (ConstantBool *C = dyn_cast<ConstantBool>(FalseVal)) { |
| 4230 | if (C == ConstantBool::False) { |
| 4231 | // Change: A = select B, C, false --> A = and B, C |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4232 | return BinaryOperator::createAnd(CondVal, TrueVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 4233 | } else { |
| 4234 | // Change: A = select B, C, true --> A = or !B, C |
| 4235 | Value *NotCond = |
| 4236 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 4237 | "not."+CondVal->getName()), SI); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4238 | return BinaryOperator::createOr(NotCond, TrueVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 4239 | } |
| 4240 | } |
| 4241 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4242 | // Selecting between two integer constants? |
| 4243 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) |
| 4244 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { |
| 4245 | // select C, 1, 0 -> cast C to int |
| 4246 | if (FalseValC->isNullValue() && TrueValC->getRawValue() == 1) { |
| 4247 | return new CastInst(CondVal, SI.getType()); |
| 4248 | } else if (TrueValC->isNullValue() && FalseValC->getRawValue() == 1) { |
| 4249 | // select C, 0, 1 -> cast !C to int |
| 4250 | Value *NotCond = |
| 4251 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
Chris Lattner | cf7baf3 | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 4252 | "not."+CondVal->getName()), SI); |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4253 | return new CastInst(NotCond, SI.getType()); |
Chris Lattner | cf7baf3 | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 4254 | } |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 4255 | |
| 4256 | // If one of the constants is zero (we know they can't both be) and we |
| 4257 | // have a setcc instruction with zero, and we have an 'and' with the |
| 4258 | // non-constant value, eliminate this whole mess. This corresponds to |
| 4259 | // cases like this: ((X & 27) ? 27 : 0) |
| 4260 | if (TrueValC->isNullValue() || FalseValC->isNullValue()) |
| 4261 | if (Instruction *IC = dyn_cast<Instruction>(SI.getCondition())) |
| 4262 | if ((IC->getOpcode() == Instruction::SetEQ || |
| 4263 | IC->getOpcode() == Instruction::SetNE) && |
| 4264 | isa<ConstantInt>(IC->getOperand(1)) && |
| 4265 | cast<Constant>(IC->getOperand(1))->isNullValue()) |
| 4266 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) |
| 4267 | if (ICA->getOpcode() == Instruction::And && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4268 | isa<ConstantInt>(ICA->getOperand(1)) && |
| 4269 | (ICA->getOperand(1) == TrueValC || |
| 4270 | ICA->getOperand(1) == FalseValC) && |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 4271 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { |
| 4272 | // Okay, now we know that everything is set up, we just don't |
| 4273 | // know whether we have a setne or seteq and whether the true or |
| 4274 | // false val is the zero. |
| 4275 | bool ShouldNotVal = !TrueValC->isNullValue(); |
| 4276 | ShouldNotVal ^= IC->getOpcode() == Instruction::SetNE; |
| 4277 | Value *V = ICA; |
| 4278 | if (ShouldNotVal) |
| 4279 | V = InsertNewInstBefore(BinaryOperator::create( |
| 4280 | Instruction::Xor, V, ICA->getOperand(1)), SI); |
| 4281 | return ReplaceInstUsesWith(SI, V); |
| 4282 | } |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 4283 | } |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 4284 | |
| 4285 | // See if we are selecting two values based on a comparison of the two values. |
| 4286 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(CondVal)) { |
| 4287 | if (SCI->getOperand(0) == TrueVal && SCI->getOperand(1) == FalseVal) { |
| 4288 | // Transform (X == Y) ? X : Y -> Y |
| 4289 | if (SCI->getOpcode() == Instruction::SetEQ) |
| 4290 | return ReplaceInstUsesWith(SI, FalseVal); |
| 4291 | // Transform (X != Y) ? X : Y -> X |
| 4292 | if (SCI->getOpcode() == Instruction::SetNE) |
| 4293 | return ReplaceInstUsesWith(SI, TrueVal); |
| 4294 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 4295 | |
| 4296 | } else if (SCI->getOperand(0) == FalseVal && SCI->getOperand(1) == TrueVal){ |
| 4297 | // Transform (X == Y) ? Y : X -> X |
| 4298 | if (SCI->getOpcode() == Instruction::SetEQ) |
Chris Lattner | 24cf020 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 4299 | return ReplaceInstUsesWith(SI, FalseVal); |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 4300 | // Transform (X != Y) ? Y : X -> Y |
| 4301 | if (SCI->getOpcode() == Instruction::SetNE) |
Chris Lattner | 24cf020 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 4302 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 4303 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 4304 | } |
| 4305 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4306 | |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 4307 | if (Instruction *TI = dyn_cast<Instruction>(TrueVal)) |
| 4308 | if (Instruction *FI = dyn_cast<Instruction>(FalseVal)) |
| 4309 | if (TI->hasOneUse() && FI->hasOneUse()) { |
| 4310 | bool isInverse = false; |
| 4311 | Instruction *AddOp = 0, *SubOp = 0; |
| 4312 | |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 4313 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) |
| 4314 | if (TI->getOpcode() == FI->getOpcode()) |
| 4315 | if (Instruction *IV = FoldSelectOpOp(SI, TI, FI)) |
| 4316 | return IV; |
| 4317 | |
| 4318 | // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is |
| 4319 | // even legal for FP. |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 4320 | if (TI->getOpcode() == Instruction::Sub && |
| 4321 | FI->getOpcode() == Instruction::Add) { |
| 4322 | AddOp = FI; SubOp = TI; |
| 4323 | } else if (FI->getOpcode() == Instruction::Sub && |
| 4324 | TI->getOpcode() == Instruction::Add) { |
| 4325 | AddOp = TI; SubOp = FI; |
| 4326 | } |
| 4327 | |
| 4328 | if (AddOp) { |
| 4329 | Value *OtherAddOp = 0; |
| 4330 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { |
| 4331 | OtherAddOp = AddOp->getOperand(1); |
| 4332 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { |
| 4333 | OtherAddOp = AddOp->getOperand(0); |
| 4334 | } |
| 4335 | |
| 4336 | if (OtherAddOp) { |
| 4337 | // So at this point we know we have: |
| 4338 | // select C, (add X, Y), (sub X, ?) |
| 4339 | // We can do the transform profitably if either 'Y' = '?' or '?' is |
| 4340 | // a constant. |
| 4341 | if (SubOp->getOperand(1) == AddOp || |
| 4342 | isa<Constant>(SubOp->getOperand(1))) { |
| 4343 | Value *NegVal; |
| 4344 | if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) { |
| 4345 | NegVal = ConstantExpr::getNeg(C); |
| 4346 | } else { |
| 4347 | NegVal = InsertNewInstBefore( |
| 4348 | BinaryOperator::createNeg(SubOp->getOperand(1)), SI); |
| 4349 | } |
| 4350 | |
Chris Lattner | 51726c4 | 2005-01-14 17:35:12 +0000 | [diff] [blame] | 4351 | Value *NewTrueOp = OtherAddOp; |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 4352 | Value *NewFalseOp = NegVal; |
| 4353 | if (AddOp != TI) |
| 4354 | std::swap(NewTrueOp, NewFalseOp); |
| 4355 | Instruction *NewSel = |
| 4356 | new SelectInst(CondVal, NewTrueOp,NewFalseOp,SI.getName()+".p"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4357 | |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 4358 | NewSel = InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 51726c4 | 2005-01-14 17:35:12 +0000 | [diff] [blame] | 4359 | return BinaryOperator::createAdd(SubOp->getOperand(0), NewSel); |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 4360 | } |
| 4361 | } |
| 4362 | } |
| 4363 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4364 | |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 4365 | // See if we can fold the select into one of our operands. |
| 4366 | if (SI.getType()->isInteger()) { |
| 4367 | // See the comment above GetSelectFoldableOperands for a description of the |
| 4368 | // transformation we are doing here. |
| 4369 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) |
| 4370 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && |
| 4371 | !isa<Constant>(FalseVal)) |
| 4372 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { |
| 4373 | unsigned OpToFold = 0; |
| 4374 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 4375 | OpToFold = 1; |
| 4376 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
| 4377 | OpToFold = 2; |
| 4378 | } |
| 4379 | |
| 4380 | if (OpToFold) { |
| 4381 | Constant *C = GetSelectFoldableConstant(TVI); |
| 4382 | std::string Name = TVI->getName(); TVI->setName(""); |
| 4383 | Instruction *NewSel = |
| 4384 | new SelectInst(SI.getCondition(), TVI->getOperand(2-OpToFold), C, |
| 4385 | Name); |
| 4386 | InsertNewInstBefore(NewSel, SI); |
| 4387 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) |
| 4388 | return BinaryOperator::create(BO->getOpcode(), FalseVal, NewSel); |
| 4389 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(TVI)) |
| 4390 | return new ShiftInst(SI->getOpcode(), FalseVal, NewSel); |
| 4391 | else { |
| 4392 | assert(0 && "Unknown instruction!!"); |
| 4393 | } |
| 4394 | } |
| 4395 | } |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4396 | |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 4397 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) |
| 4398 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && |
| 4399 | !isa<Constant>(TrueVal)) |
| 4400 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { |
| 4401 | unsigned OpToFold = 0; |
| 4402 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 4403 | OpToFold = 1; |
| 4404 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
| 4405 | OpToFold = 2; |
| 4406 | } |
| 4407 | |
| 4408 | if (OpToFold) { |
| 4409 | Constant *C = GetSelectFoldableConstant(FVI); |
| 4410 | std::string Name = FVI->getName(); FVI->setName(""); |
| 4411 | Instruction *NewSel = |
| 4412 | new SelectInst(SI.getCondition(), C, FVI->getOperand(2-OpToFold), |
| 4413 | Name); |
| 4414 | InsertNewInstBefore(NewSel, SI); |
| 4415 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) |
| 4416 | return BinaryOperator::create(BO->getOpcode(), TrueVal, NewSel); |
| 4417 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(FVI)) |
| 4418 | return new ShiftInst(SI->getOpcode(), TrueVal, NewSel); |
| 4419 | else { |
| 4420 | assert(0 && "Unknown instruction!!"); |
| 4421 | } |
| 4422 | } |
| 4423 | } |
| 4424 | } |
Chris Lattner | d6f636a | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 4425 | |
| 4426 | if (BinaryOperator::isNot(CondVal)) { |
| 4427 | SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); |
| 4428 | SI.setOperand(1, FalseVal); |
| 4429 | SI.setOperand(2, TrueVal); |
| 4430 | return &SI; |
| 4431 | } |
| 4432 | |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 4433 | return 0; |
| 4434 | } |
| 4435 | |
| 4436 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4437 | // CallInst simplification |
| 4438 | // |
| 4439 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 4440 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 4441 | // visitCallSite. |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 4442 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(&CI)) { |
| 4443 | bool Changed = false; |
| 4444 | |
| 4445 | // memmove/cpy/set of zero bytes is a noop. |
| 4446 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { |
| 4447 | if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); |
| 4448 | |
| 4449 | // FIXME: Increase alignment here. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4450 | |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 4451 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
| 4452 | if (CI->getRawValue() == 1) { |
| 4453 | // Replace the instruction with just byte operations. We would |
| 4454 | // transform other cases to loads/stores, but we don't know if |
| 4455 | // alignment is sufficient. |
| 4456 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 4457 | } |
| 4458 | |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 4459 | // If we have a memmove and the source operation is a constant global, |
| 4460 | // then the source and dest pointers can't alias, so we can change this |
| 4461 | // into a call to memcpy. |
| 4462 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) |
| 4463 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
| 4464 | if (GVSrc->isConstant()) { |
| 4465 | Module *M = CI.getParent()->getParent()->getParent(); |
| 4466 | Function *MemCpy = M->getOrInsertFunction("llvm.memcpy", |
| 4467 | CI.getCalledFunction()->getFunctionType()); |
| 4468 | CI.setOperand(0, MemCpy); |
| 4469 | Changed = true; |
| 4470 | } |
| 4471 | |
| 4472 | if (Changed) return &CI; |
Chris Lattner | 9530754 | 2004-11-18 21:41:39 +0000 | [diff] [blame] | 4473 | } else if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(&CI)) { |
| 4474 | // If this stoppoint is at the same source location as the previous |
| 4475 | // stoppoint in the chain, it is not needed. |
| 4476 | if (DbgStopPointInst *PrevSPI = |
| 4477 | dyn_cast<DbgStopPointInst>(SPI->getChain())) |
| 4478 | if (SPI->getLineNo() == PrevSPI->getLineNo() && |
| 4479 | SPI->getColNo() == PrevSPI->getColNo()) { |
| 4480 | SPI->replaceAllUsesWith(PrevSPI); |
| 4481 | return EraseInstFromFunction(CI); |
| 4482 | } |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 4483 | } |
| 4484 | |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 4485 | return visitCallSite(&CI); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4486 | } |
| 4487 | |
| 4488 | // InvokeInst simplification |
| 4489 | // |
| 4490 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 4491 | return visitCallSite(&II); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4492 | } |
| 4493 | |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 4494 | // visitCallSite - Improvements for call and invoke instructions. |
| 4495 | // |
| 4496 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 4497 | bool Changed = false; |
| 4498 | |
| 4499 | // If the callee is a constexpr cast of a function, attempt to move the cast |
| 4500 | // to the arguments of the call/invoke. |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 4501 | if (transformConstExprCastCall(CS)) return 0; |
| 4502 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 4503 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4504 | |
Chris Lattner | 61d9d81 | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 4505 | if (Function *CalleeF = dyn_cast<Function>(Callee)) |
| 4506 | if (CalleeF->getCallingConv() != CS.getCallingConv()) { |
| 4507 | Instruction *OldCall = CS.getInstruction(); |
| 4508 | // If the call and callee calling conventions don't match, this call must |
| 4509 | // be unreachable, as the call is undefined. |
| 4510 | new StoreInst(ConstantBool::True, |
| 4511 | UndefValue::get(PointerType::get(Type::BoolTy)), OldCall); |
| 4512 | if (!OldCall->use_empty()) |
| 4513 | OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); |
| 4514 | if (isa<CallInst>(OldCall)) // Not worth removing an invoke here. |
| 4515 | return EraseInstFromFunction(*OldCall); |
| 4516 | return 0; |
| 4517 | } |
| 4518 | |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 4519 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
| 4520 | // This instruction is not reachable, just remove it. We insert a store to |
| 4521 | // undef so that we know that this code is not reachable, despite the fact |
| 4522 | // that we can't modify the CFG here. |
| 4523 | new StoreInst(ConstantBool::True, |
| 4524 | UndefValue::get(PointerType::get(Type::BoolTy)), |
| 4525 | CS.getInstruction()); |
| 4526 | |
| 4527 | if (!CS.getInstruction()->use_empty()) |
| 4528 | CS.getInstruction()-> |
| 4529 | replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); |
| 4530 | |
| 4531 | if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { |
| 4532 | // Don't break the CFG, insert a dummy cond branch. |
| 4533 | new BranchInst(II->getNormalDest(), II->getUnwindDest(), |
| 4534 | ConstantBool::True, II); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4535 | } |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 4536 | return EraseInstFromFunction(*CS.getInstruction()); |
| 4537 | } |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4538 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 4539 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 4540 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 4541 | if (FTy->isVarArg()) { |
| 4542 | // See if we can optimize any arguments passed through the varargs area of |
| 4543 | // the call. |
| 4544 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
| 4545 | E = CS.arg_end(); I != E; ++I) |
| 4546 | if (CastInst *CI = dyn_cast<CastInst>(*I)) { |
| 4547 | // If this cast does not effect the value passed through the varargs |
| 4548 | // area, we can eliminate the use of the cast. |
| 4549 | Value *Op = CI->getOperand(0); |
| 4550 | if (CI->getType()->isLosslesslyConvertibleTo(Op->getType())) { |
| 4551 | *I = Op; |
| 4552 | Changed = true; |
| 4553 | } |
| 4554 | } |
| 4555 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4556 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 4557 | return Changed ? CS.getInstruction() : 0; |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 4558 | } |
| 4559 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4560 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 4561 | // attempt to move the cast to the arguments of the call/invoke. |
| 4562 | // |
| 4563 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 4564 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 4565 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
Chris Lattner | f3edc49 | 2004-07-18 18:59:44 +0000 | [diff] [blame] | 4566 | if (CE->getOpcode() != Instruction::Cast || !isa<Function>(CE->getOperand(0))) |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4567 | return false; |
Reid Spencer | 8743687 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 4568 | Function *Callee = cast<Function>(CE->getOperand(0)); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4569 | Instruction *Caller = CS.getInstruction(); |
| 4570 | |
| 4571 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 4572 | // would cause a type conversion of one of our arguments, change this call to |
| 4573 | // be a direct call with arguments casted to the appropriate types. |
| 4574 | // |
| 4575 | const FunctionType *FT = Callee->getFunctionType(); |
| 4576 | const Type *OldRetTy = Caller->getType(); |
| 4577 | |
Chris Lattner | 1f7942f | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 4578 | // Check to see if we are changing the return type... |
| 4579 | if (OldRetTy != FT->getReturnType()) { |
| 4580 | if (Callee->isExternal() && |
| 4581 | !OldRetTy->isLosslesslyConvertibleTo(FT->getReturnType()) && |
| 4582 | !Caller->use_empty()) |
| 4583 | return false; // Cannot transform this return value... |
| 4584 | |
| 4585 | // If the callsite is an invoke instruction, and the return value is used by |
| 4586 | // a PHI node in a successor, we cannot change the return type of the call |
| 4587 | // because there is no place to put the cast instruction (without breaking |
| 4588 | // the critical edge). Bail out in this case. |
| 4589 | if (!Caller->use_empty()) |
| 4590 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 4591 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 4592 | UI != E; ++UI) |
| 4593 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 4594 | if (PN->getParent() == II->getNormalDest() || |
Chris Lattner | fae8ab3 | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 4595 | PN->getParent() == II->getUnwindDest()) |
Chris Lattner | 1f7942f | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 4596 | return false; |
| 4597 | } |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4598 | |
| 4599 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 4600 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4601 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4602 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 4603 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 4604 | const Type *ParamTy = FT->getParamType(i); |
| 4605 | bool isConvertible = (*AI)->getType()->isLosslesslyConvertibleTo(ParamTy); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4606 | if (Callee->isExternal() && !isConvertible) return false; |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4607 | } |
| 4608 | |
| 4609 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
| 4610 | Callee->isExternal()) |
| 4611 | return false; // Do not delete arguments unless we have a function body... |
| 4612 | |
| 4613 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 4614 | // inserting cast instructions as necessary... |
| 4615 | std::vector<Value*> Args; |
| 4616 | Args.reserve(NumActualArgs); |
| 4617 | |
| 4618 | AI = CS.arg_begin(); |
| 4619 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 4620 | const Type *ParamTy = FT->getParamType(i); |
| 4621 | if ((*AI)->getType() == ParamTy) { |
| 4622 | Args.push_back(*AI); |
| 4623 | } else { |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 4624 | Args.push_back(InsertNewInstBefore(new CastInst(*AI, ParamTy, "tmp"), |
| 4625 | *Caller)); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4626 | } |
| 4627 | } |
| 4628 | |
| 4629 | // If the function takes more arguments than the call was taking, add them |
| 4630 | // now... |
| 4631 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 4632 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 4633 | |
| 4634 | // If we are removing arguments to the function, emit an obnoxious warning... |
| 4635 | if (FT->getNumParams() < NumActualArgs) |
| 4636 | if (!FT->isVarArg()) { |
| 4637 | std::cerr << "WARNING: While resolving call to function '" |
| 4638 | << Callee->getName() << "' arguments were dropped!\n"; |
| 4639 | } else { |
| 4640 | // Add all of the arguments in their promoted form to the arg list... |
| 4641 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 4642 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 4643 | if (PTy != (*AI)->getType()) { |
| 4644 | // Must promote to pass through va_arg area! |
| 4645 | Instruction *Cast = new CastInst(*AI, PTy, "tmp"); |
| 4646 | InsertNewInstBefore(Cast, *Caller); |
| 4647 | Args.push_back(Cast); |
| 4648 | } else { |
| 4649 | Args.push_back(*AI); |
| 4650 | } |
| 4651 | } |
| 4652 | } |
| 4653 | |
| 4654 | if (FT->getReturnType() == Type::VoidTy) |
| 4655 | Caller->setName(""); // Void type should not have a name... |
| 4656 | |
| 4657 | Instruction *NC; |
| 4658 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Chris Lattner | fae8ab3 | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 4659 | NC = new InvokeInst(Callee, II->getNormalDest(), II->getUnwindDest(), |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4660 | Args, Caller->getName(), Caller); |
Chris Lattner | 05c703e | 2005-05-14 12:25:32 +0000 | [diff] [blame] | 4661 | cast<InvokeInst>(II)->setCallingConv(II->getCallingConv()); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4662 | } else { |
| 4663 | NC = new CallInst(Callee, Args, Caller->getName(), Caller); |
Chris Lattner | 6aacb0f | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 4664 | if (cast<CallInst>(Caller)->isTailCall()) |
| 4665 | cast<CallInst>(NC)->setTailCall(); |
Chris Lattner | 05c703e | 2005-05-14 12:25:32 +0000 | [diff] [blame] | 4666 | cast<CallInst>(NC)->setCallingConv(cast<CallInst>(Caller)->getCallingConv()); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4667 | } |
| 4668 | |
| 4669 | // Insert a cast of the return type as necessary... |
| 4670 | Value *NV = NC; |
| 4671 | if (Caller->getType() != NV->getType() && !Caller->use_empty()) { |
| 4672 | if (NV->getType() != Type::VoidTy) { |
| 4673 | NV = NC = new CastInst(NC, Caller->getType(), "tmp"); |
Chris Lattner | 686767f | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 4674 | |
| 4675 | // If this is an invoke instruction, we should insert it after the first |
| 4676 | // non-phi, instruction in the normal successor block. |
| 4677 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 4678 | BasicBlock::iterator I = II->getNormalDest()->begin(); |
| 4679 | while (isa<PHINode>(I)) ++I; |
| 4680 | InsertNewInstBefore(NC, *I); |
| 4681 | } else { |
| 4682 | // Otherwise, it's a call, just insert cast right after the call instr |
| 4683 | InsertNewInstBefore(NC, *Caller); |
| 4684 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 4685 | AddUsersToWorkList(*Caller); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4686 | } else { |
Chris Lattner | e29d634 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 4687 | NV = UndefValue::get(Caller->getType()); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4688 | } |
| 4689 | } |
| 4690 | |
| 4691 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 4692 | Caller->replaceAllUsesWith(NV); |
| 4693 | Caller->getParent()->getInstList().erase(Caller); |
| 4694 | removeFromWorkList(Caller); |
| 4695 | return true; |
| 4696 | } |
| 4697 | |
| 4698 | |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4699 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 4700 | // operator and they all are only used by the PHI, PHI together their |
| 4701 | // inputs, and do the operation once, to the result of the PHI. |
| 4702 | Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { |
| 4703 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
| 4704 | |
| 4705 | // Scan the instruction, looking for input operations that can be folded away. |
| 4706 | // If all input operands to the phi are the same instruction (e.g. a cast from |
| 4707 | // the same type or "+42") we can pull the operation through the PHI, reducing |
| 4708 | // code size and simplifying code. |
| 4709 | Constant *ConstantOp = 0; |
| 4710 | const Type *CastSrcTy = 0; |
| 4711 | if (isa<CastInst>(FirstInst)) { |
| 4712 | CastSrcTy = FirstInst->getOperand(0)->getType(); |
| 4713 | } else if (isa<BinaryOperator>(FirstInst) || isa<ShiftInst>(FirstInst)) { |
| 4714 | // Can fold binop or shift if the RHS is a constant. |
| 4715 | ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1)); |
| 4716 | if (ConstantOp == 0) return 0; |
| 4717 | } else { |
| 4718 | return 0; // Cannot fold this operation. |
| 4719 | } |
| 4720 | |
| 4721 | // Check to see if all arguments are the same operation. |
| 4722 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 4723 | if (!isa<Instruction>(PN.getIncomingValue(i))) return 0; |
| 4724 | Instruction *I = cast<Instruction>(PN.getIncomingValue(i)); |
| 4725 | if (!I->hasOneUse() || I->getOpcode() != FirstInst->getOpcode()) |
| 4726 | return 0; |
| 4727 | if (CastSrcTy) { |
| 4728 | if (I->getOperand(0)->getType() != CastSrcTy) |
| 4729 | return 0; // Cast operation must match. |
| 4730 | } else if (I->getOperand(1) != ConstantOp) { |
| 4731 | return 0; |
| 4732 | } |
| 4733 | } |
| 4734 | |
| 4735 | // Okay, they are all the same operation. Create a new PHI node of the |
| 4736 | // correct type, and PHI together all of the LHS's of the instructions. |
| 4737 | PHINode *NewPN = new PHINode(FirstInst->getOperand(0)->getType(), |
| 4738 | PN.getName()+".in"); |
Chris Lattner | d8e2018 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 4739 | NewPN->reserveOperandSpace(PN.getNumOperands()/2); |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 4740 | |
| 4741 | Value *InVal = FirstInst->getOperand(0); |
| 4742 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4743 | |
| 4744 | // Add all operands to the new PHI. |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 4745 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 4746 | Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 4747 | if (NewInVal != InVal) |
| 4748 | InVal = 0; |
| 4749 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); |
| 4750 | } |
| 4751 | |
| 4752 | Value *PhiVal; |
| 4753 | if (InVal) { |
| 4754 | // The new PHI unions all of the same values together. This is really |
| 4755 | // common, so we handle it intelligently here for compile-time speed. |
| 4756 | PhiVal = InVal; |
| 4757 | delete NewPN; |
| 4758 | } else { |
| 4759 | InsertNewInstBefore(NewPN, PN); |
| 4760 | PhiVal = NewPN; |
| 4761 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4762 | |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4763 | // Insert and return the new operation. |
| 4764 | if (isa<CastInst>(FirstInst)) |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 4765 | return new CastInst(PhiVal, PN.getType()); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4766 | else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 4767 | return BinaryOperator::create(BinOp->getOpcode(), PhiVal, ConstantOp); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4768 | else |
| 4769 | return new ShiftInst(cast<ShiftInst>(FirstInst)->getOpcode(), |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 4770 | PhiVal, ConstantOp); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4771 | } |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4772 | |
Chris Lattner | 7153643 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 4773 | /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle |
| 4774 | /// that is dead. |
| 4775 | static bool DeadPHICycle(PHINode *PN, std::set<PHINode*> &PotentiallyDeadPHIs) { |
| 4776 | if (PN->use_empty()) return true; |
| 4777 | if (!PN->hasOneUse()) return false; |
| 4778 | |
| 4779 | // Remember this node, and if we find the cycle, return. |
| 4780 | if (!PotentiallyDeadPHIs.insert(PN).second) |
| 4781 | return true; |
| 4782 | |
| 4783 | if (PHINode *PU = dyn_cast<PHINode>(PN->use_back())) |
| 4784 | return DeadPHICycle(PU, PotentiallyDeadPHIs); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4785 | |
Chris Lattner | 7153643 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 4786 | return false; |
| 4787 | } |
| 4788 | |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 4789 | // PHINode simplification |
| 4790 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4791 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Chris Lattner | 9f9c260 | 2005-08-05 01:04:30 +0000 | [diff] [blame] | 4792 | if (Value *V = PN.hasConstantValue()) |
| 4793 | return ReplaceInstUsesWith(PN, V); |
Chris Lattner | 4db2d22 | 2004-02-16 05:07:08 +0000 | [diff] [blame] | 4794 | |
| 4795 | // If the only user of this instruction is a cast instruction, and all of the |
| 4796 | // incoming values are constants, change this PHI to merge together the casted |
| 4797 | // constants. |
| 4798 | if (PN.hasOneUse()) |
| 4799 | if (CastInst *CI = dyn_cast<CastInst>(PN.use_back())) |
| 4800 | if (CI->getType() != PN.getType()) { // noop casts will be folded |
| 4801 | bool AllConstant = true; |
| 4802 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 4803 | if (!isa<Constant>(PN.getIncomingValue(i))) { |
| 4804 | AllConstant = false; |
| 4805 | break; |
| 4806 | } |
| 4807 | if (AllConstant) { |
| 4808 | // Make a new PHI with all casted values. |
| 4809 | PHINode *New = new PHINode(CI->getType(), PN.getName(), &PN); |
| 4810 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 4811 | Constant *OldArg = cast<Constant>(PN.getIncomingValue(i)); |
| 4812 | New->addIncoming(ConstantExpr::getCast(OldArg, New->getType()), |
| 4813 | PN.getIncomingBlock(i)); |
| 4814 | } |
| 4815 | |
| 4816 | // Update the cast instruction. |
| 4817 | CI->setOperand(0, New); |
| 4818 | WorkList.push_back(CI); // revisit the cast instruction to fold. |
| 4819 | WorkList.push_back(New); // Make sure to revisit the new Phi |
| 4820 | return &PN; // PN is now dead! |
| 4821 | } |
| 4822 | } |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4823 | |
| 4824 | // If all PHI operands are the same operation, pull them through the PHI, |
| 4825 | // reducing code size. |
| 4826 | if (isa<Instruction>(PN.getIncomingValue(0)) && |
| 4827 | PN.getIncomingValue(0)->hasOneUse()) |
| 4828 | if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) |
| 4829 | return Result; |
| 4830 | |
Chris Lattner | 7153643 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 4831 | // If this is a trivial cycle in the PHI node graph, remove it. Basically, if |
| 4832 | // this PHI only has a single use (a PHI), and if that PHI only has one use (a |
| 4833 | // PHI)... break the cycle. |
| 4834 | if (PN.hasOneUse()) |
| 4835 | if (PHINode *PU = dyn_cast<PHINode>(PN.use_back())) { |
| 4836 | std::set<PHINode*> PotentiallyDeadPHIs; |
| 4837 | PotentiallyDeadPHIs.insert(&PN); |
| 4838 | if (DeadPHICycle(PU, PotentiallyDeadPHIs)) |
| 4839 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 4840 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4841 | |
Chris Lattner | 91daeb5 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 4842 | return 0; |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 4843 | } |
| 4844 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4845 | static Value *InsertSignExtendToPtrTy(Value *V, const Type *DTy, |
| 4846 | Instruction *InsertPoint, |
| 4847 | InstCombiner *IC) { |
| 4848 | unsigned PS = IC->getTargetData().getPointerSize(); |
| 4849 | const Type *VTy = V->getType(); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4850 | if (!VTy->isSigned() && VTy->getPrimitiveSize() < PS) |
| 4851 | // We must insert a cast to ensure we sign-extend. |
| 4852 | V = IC->InsertNewInstBefore(new CastInst(V, VTy->getSignedVersion(), |
| 4853 | V->getName()), *InsertPoint); |
| 4854 | return IC->InsertNewInstBefore(new CastInst(V, DTy, V->getName()), |
| 4855 | *InsertPoint); |
| 4856 | } |
| 4857 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4858 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4859 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4860 | Value *PtrOp = GEP.getOperand(0); |
Chris Lattner | 471bd76 | 2003-05-22 19:07:21 +0000 | [diff] [blame] | 4861 | // Is it 'getelementptr %P, long 0' or 'getelementptr %P' |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4862 | // If so, eliminate the noop. |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 4863 | if (GEP.getNumOperands() == 1) |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4864 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 4865 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4866 | if (isa<UndefValue>(GEP.getOperand(0))) |
| 4867 | return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType())); |
| 4868 | |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 4869 | bool HasZeroPointerIndex = false; |
| 4870 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) |
| 4871 | HasZeroPointerIndex = C->isNullValue(); |
| 4872 | |
| 4873 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4874 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4875 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4876 | // Eliminate unneeded casts for indices. |
| 4877 | bool MadeChange = false; |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 4878 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 4879 | for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI) |
| 4880 | if (isa<SequentialType>(*GTI)) { |
| 4881 | if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) { |
| 4882 | Value *Src = CI->getOperand(0); |
| 4883 | const Type *SrcTy = Src->getType(); |
| 4884 | const Type *DestTy = CI->getType(); |
| 4885 | if (Src->getType()->isInteger()) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4886 | if (SrcTy->getPrimitiveSizeInBits() == |
| 4887 | DestTy->getPrimitiveSizeInBits()) { |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 4888 | // We can always eliminate a cast from ulong or long to the other. |
| 4889 | // We can always eliminate a cast from uint to int or the other on |
| 4890 | // 32-bit pointer platforms. |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4891 | if (DestTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()){ |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 4892 | MadeChange = true; |
| 4893 | GEP.setOperand(i, Src); |
| 4894 | } |
| 4895 | } else if (SrcTy->getPrimitiveSize() < DestTy->getPrimitiveSize() && |
| 4896 | SrcTy->getPrimitiveSize() == 4) { |
| 4897 | // We can always eliminate a cast from int to [u]long. We can |
| 4898 | // eliminate a cast from uint to [u]long iff the target is a 32-bit |
| 4899 | // pointer target. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4900 | if (SrcTy->isSigned() || |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4901 | SrcTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()) { |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 4902 | MadeChange = true; |
| 4903 | GEP.setOperand(i, Src); |
| 4904 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4905 | } |
| 4906 | } |
| 4907 | } |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 4908 | // If we are using a wider index than needed for this platform, shrink it |
| 4909 | // to what we need. If the incoming value needs a cast instruction, |
| 4910 | // insert it. This explicit cast can make subsequent optimizations more |
| 4911 | // obvious. |
| 4912 | Value *Op = GEP.getOperand(i); |
| 4913 | if (Op->getType()->getPrimitiveSize() > TD->getPointerSize()) |
Chris Lattner | 1e9ac1a | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 4914 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | 44d0b95 | 2004-07-20 01:48:15 +0000 | [diff] [blame] | 4915 | GEP.setOperand(i, ConstantExpr::getCast(C, |
| 4916 | TD->getIntPtrType()->getSignedVersion())); |
Chris Lattner | 1e9ac1a | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 4917 | MadeChange = true; |
| 4918 | } else { |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 4919 | Op = InsertNewInstBefore(new CastInst(Op, TD->getIntPtrType(), |
| 4920 | Op->getName()), GEP); |
| 4921 | GEP.setOperand(i, Op); |
| 4922 | MadeChange = true; |
| 4923 | } |
Chris Lattner | 44d0b95 | 2004-07-20 01:48:15 +0000 | [diff] [blame] | 4924 | |
| 4925 | // If this is a constant idx, make sure to canonicalize it to be a signed |
| 4926 | // operand, otherwise CSE and other optimizations are pessimized. |
| 4927 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op)) { |
| 4928 | GEP.setOperand(i, ConstantExpr::getCast(CUI, |
| 4929 | CUI->getType()->getSignedVersion())); |
| 4930 | MadeChange = true; |
| 4931 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4932 | } |
| 4933 | if (MadeChange) return &GEP; |
| 4934 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 4935 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 4936 | // is a getelementptr instruction, combine the indices of the two |
| 4937 | // getelementptr instructions into a single instruction. |
| 4938 | // |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 4939 | std::vector<Value*> SrcGEPOperands; |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4940 | if (User *Src = dyn_castGetElementPtr(PtrOp)) |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 4941 | SrcGEPOperands.assign(Src->op_begin(), Src->op_end()); |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 4942 | |
| 4943 | if (!SrcGEPOperands.empty()) { |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4944 | // Note that if our source is a gep chain itself that we wait for that |
| 4945 | // chain to be resolved before we perform this transformation. This |
| 4946 | // avoids us creating a TON of code in some cases. |
| 4947 | // |
| 4948 | if (isa<GetElementPtrInst>(SrcGEPOperands[0]) && |
| 4949 | cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2) |
| 4950 | return 0; // Wait until our source is folded to completion. |
| 4951 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 4952 | std::vector<Value *> Indices; |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4953 | |
| 4954 | // Find out whether the last index in the source GEP is a sequential idx. |
| 4955 | bool EndsWithSequential = false; |
| 4956 | for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)), |
| 4957 | E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I) |
Chris Lattner | 8ec5f88 | 2004-05-08 22:41:42 +0000 | [diff] [blame] | 4958 | EndsWithSequential = !isa<StructType>(*I); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4959 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 4960 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4961 | if (EndsWithSequential) { |
Chris Lattner | 235af56 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 4962 | // Replace: gep (gep %P, long B), long A, ... |
| 4963 | // With: T = long A+B; gep %P, T, ... |
| 4964 | // |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4965 | Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4966 | if (SO1 == Constant::getNullValue(SO1->getType())) { |
| 4967 | Sum = GO1; |
| 4968 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { |
| 4969 | Sum = SO1; |
| 4970 | } else { |
| 4971 | // If they aren't the same type, convert both to an integer of the |
| 4972 | // target's pointer size. |
| 4973 | if (SO1->getType() != GO1->getType()) { |
| 4974 | if (Constant *SO1C = dyn_cast<Constant>(SO1)) { |
| 4975 | SO1 = ConstantExpr::getCast(SO1C, GO1->getType()); |
| 4976 | } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) { |
| 4977 | GO1 = ConstantExpr::getCast(GO1C, SO1->getType()); |
| 4978 | } else { |
| 4979 | unsigned PS = TD->getPointerSize(); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4980 | if (SO1->getType()->getPrimitiveSize() == PS) { |
| 4981 | // Convert GO1 to SO1's type. |
| 4982 | GO1 = InsertSignExtendToPtrTy(GO1, SO1->getType(), &GEP, this); |
| 4983 | |
| 4984 | } else if (GO1->getType()->getPrimitiveSize() == PS) { |
| 4985 | // Convert SO1 to GO1's type. |
| 4986 | SO1 = InsertSignExtendToPtrTy(SO1, GO1->getType(), &GEP, this); |
| 4987 | } else { |
| 4988 | const Type *PT = TD->getIntPtrType(); |
| 4989 | SO1 = InsertSignExtendToPtrTy(SO1, PT, &GEP, this); |
| 4990 | GO1 = InsertSignExtendToPtrTy(GO1, PT, &GEP, this); |
| 4991 | } |
| 4992 | } |
| 4993 | } |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4994 | if (isa<Constant>(SO1) && isa<Constant>(GO1)) |
| 4995 | Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1)); |
| 4996 | else { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4997 | Sum = BinaryOperator::createAdd(SO1, GO1, PtrOp->getName()+".sum"); |
| 4998 | InsertNewInstBefore(cast<Instruction>(Sum), GEP); |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4999 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 5000 | } |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 5001 | |
| 5002 | // Recycle the GEP we already have if possible. |
| 5003 | if (SrcGEPOperands.size() == 2) { |
| 5004 | GEP.setOperand(0, SrcGEPOperands[0]); |
| 5005 | GEP.setOperand(1, Sum); |
| 5006 | return &GEP; |
| 5007 | } else { |
| 5008 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 5009 | SrcGEPOperands.end()-1); |
| 5010 | Indices.push_back(Sum); |
| 5011 | Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end()); |
| 5012 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5013 | } else if (isa<Constant>(*GEP.idx_begin()) && |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 5014 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5015 | SrcGEPOperands.size() != 1) { |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 5016 | // 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] | 5017 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 5018 | SrcGEPOperands.end()); |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 5019 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
| 5020 | } |
| 5021 | |
| 5022 | if (!Indices.empty()) |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 5023 | return new GetElementPtrInst(SrcGEPOperands[0], Indices, GEP.getName()); |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 5024 | |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 5025 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) { |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 5026 | // GEP of global variable. If all of the indices for this GEP are |
| 5027 | // constants, we can promote this to a constexpr instead of an instruction. |
| 5028 | |
| 5029 | // Scan for nonconstants... |
| 5030 | std::vector<Constant*> Indices; |
| 5031 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
| 5032 | for (; I != E && isa<Constant>(*I); ++I) |
| 5033 | Indices.push_back(cast<Constant>(*I)); |
| 5034 | |
| 5035 | if (I == E) { // If they are all constants... |
Chris Lattner | f3edc49 | 2004-07-18 18:59:44 +0000 | [diff] [blame] | 5036 | Constant *CE = ConstantExpr::getGetElementPtr(GV, Indices); |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 5037 | |
| 5038 | // Replace all uses of the GEP with the new constexpr... |
| 5039 | return ReplaceInstUsesWith(GEP, CE); |
| 5040 | } |
Chris Lattner | 567b81f | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 5041 | } else if (Value *X = isCast(PtrOp)) { // Is the operand a cast? |
| 5042 | if (!isa<PointerType>(X->getType())) { |
| 5043 | // Not interesting. Source pointer must be a cast from pointer. |
| 5044 | } else if (HasZeroPointerIndex) { |
| 5045 | // transform: GEP (cast [10 x ubyte]* X to [0 x ubyte]*), long 0, ... |
| 5046 | // into : GEP [10 x ubyte]* X, long 0, ... |
| 5047 | // |
| 5048 | // This occurs when the program declares an array extern like "int X[];" |
| 5049 | // |
| 5050 | const PointerType *CPTy = cast<PointerType>(PtrOp->getType()); |
| 5051 | const PointerType *XTy = cast<PointerType>(X->getType()); |
| 5052 | if (const ArrayType *XATy = |
| 5053 | dyn_cast<ArrayType>(XTy->getElementType())) |
| 5054 | if (const ArrayType *CATy = |
| 5055 | dyn_cast<ArrayType>(CPTy->getElementType())) |
| 5056 | if (CATy->getElementType() == XATy->getElementType()) { |
| 5057 | // At this point, we know that the cast source type is a pointer |
| 5058 | // to an array of the same type as the destination pointer |
| 5059 | // array. Because the array type is never stepped over (there |
| 5060 | // is a leading zero) we can fold the cast into this GEP. |
| 5061 | GEP.setOperand(0, X); |
| 5062 | return &GEP; |
| 5063 | } |
| 5064 | } else if (GEP.getNumOperands() == 2) { |
| 5065 | // Transform things like: |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 5066 | // %t = getelementptr ubyte* cast ([2 x int]* %str to uint*), uint %V |
| 5067 | // into: %t1 = getelementptr [2 x int*]* %str, int 0, uint %V; cast |
Chris Lattner | 567b81f | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 5068 | const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); |
| 5069 | const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType(); |
| 5070 | if (isa<ArrayType>(SrcElTy) && |
| 5071 | TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()) == |
| 5072 | TD->getTypeSize(ResElTy)) { |
| 5073 | Value *V = InsertNewInstBefore( |
| 5074 | new GetElementPtrInst(X, Constant::getNullValue(Type::IntTy), |
| 5075 | GEP.getOperand(1), GEP.getName()), GEP); |
| 5076 | return new CastInst(V, GEP.getType()); |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 5077 | } |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 5078 | |
| 5079 | // Transform things like: |
| 5080 | // getelementptr sbyte* cast ([100 x double]* X to sbyte*), int %tmp |
| 5081 | // (where tmp = 8*tmp2) into: |
| 5082 | // getelementptr [100 x double]* %arr, int 0, int %tmp.2 |
| 5083 | |
| 5084 | if (isa<ArrayType>(SrcElTy) && |
| 5085 | (ResElTy == Type::SByteTy || ResElTy == Type::UByteTy)) { |
| 5086 | uint64_t ArrayEltSize = |
| 5087 | TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()); |
| 5088 | |
| 5089 | // Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We |
| 5090 | // allow either a mul, shift, or constant here. |
| 5091 | Value *NewIdx = 0; |
| 5092 | ConstantInt *Scale = 0; |
| 5093 | if (ArrayEltSize == 1) { |
| 5094 | NewIdx = GEP.getOperand(1); |
| 5095 | Scale = ConstantInt::get(NewIdx->getType(), 1); |
| 5096 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) { |
Chris Lattner | a393e4d | 2005-09-14 17:32:56 +0000 | [diff] [blame] | 5097 | NewIdx = ConstantInt::get(CI->getType(), 1); |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 5098 | Scale = CI; |
| 5099 | } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){ |
| 5100 | if (Inst->getOpcode() == Instruction::Shl && |
| 5101 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 5102 | unsigned ShAmt =cast<ConstantUInt>(Inst->getOperand(1))->getValue(); |
| 5103 | if (Inst->getType()->isSigned()) |
| 5104 | Scale = ConstantSInt::get(Inst->getType(), 1ULL << ShAmt); |
| 5105 | else |
| 5106 | Scale = ConstantUInt::get(Inst->getType(), 1ULL << ShAmt); |
| 5107 | NewIdx = Inst->getOperand(0); |
| 5108 | } else if (Inst->getOpcode() == Instruction::Mul && |
| 5109 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 5110 | Scale = cast<ConstantInt>(Inst->getOperand(1)); |
| 5111 | NewIdx = Inst->getOperand(0); |
| 5112 | } |
| 5113 | } |
| 5114 | |
| 5115 | // If the index will be to exactly the right offset with the scale taken |
| 5116 | // out, perform the transformation. |
| 5117 | if (Scale && Scale->getRawValue() % ArrayEltSize == 0) { |
| 5118 | if (ConstantSInt *C = dyn_cast<ConstantSInt>(Scale)) |
| 5119 | Scale = ConstantSInt::get(C->getType(), |
Chris Lattner | a393e4d | 2005-09-14 17:32:56 +0000 | [diff] [blame] | 5120 | (int64_t)C->getRawValue() / |
| 5121 | (int64_t)ArrayEltSize); |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 5122 | else |
| 5123 | Scale = ConstantUInt::get(Scale->getType(), |
| 5124 | Scale->getRawValue() / ArrayEltSize); |
| 5125 | if (Scale->getRawValue() != 1) { |
| 5126 | Constant *C = ConstantExpr::getCast(Scale, NewIdx->getType()); |
| 5127 | Instruction *Sc = BinaryOperator::createMul(NewIdx, C, "idxscale"); |
| 5128 | NewIdx = InsertNewInstBefore(Sc, GEP); |
| 5129 | } |
| 5130 | |
| 5131 | // Insert the new GEP instruction. |
| 5132 | Instruction *Idx = |
| 5133 | new GetElementPtrInst(X, Constant::getNullValue(Type::IntTy), |
| 5134 | NewIdx, GEP.getName()); |
| 5135 | Idx = InsertNewInstBefore(Idx, GEP); |
| 5136 | return new CastInst(Idx, GEP.getType()); |
| 5137 | } |
| 5138 | } |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 5139 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 5140 | } |
| 5141 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 5142 | return 0; |
| 5143 | } |
| 5144 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 5145 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
| 5146 | // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1 |
| 5147 | if (AI.isArrayAllocation()) // Check C != 1 |
| 5148 | if (const ConstantUInt *C = dyn_cast<ConstantUInt>(AI.getArraySize())) { |
| 5149 | const Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getValue()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 5150 | AllocationInst *New = 0; |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 5151 | |
| 5152 | // Create and insert the replacement instruction... |
| 5153 | if (isa<MallocInst>(AI)) |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 5154 | New = new MallocInst(NewTy, 0, AI.getName()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 5155 | else { |
| 5156 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 5157 | New = new AllocaInst(NewTy, 0, AI.getName()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 5158 | } |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 5159 | |
| 5160 | InsertNewInstBefore(New, AI); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5161 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 5162 | // Scan to the end of the allocation instructions, to skip over a block of |
| 5163 | // allocas if possible... |
| 5164 | // |
| 5165 | BasicBlock::iterator It = New; |
| 5166 | while (isa<AllocationInst>(*It)) ++It; |
| 5167 | |
| 5168 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 5169 | // insert our getelementptr instruction... |
| 5170 | // |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 5171 | Value *NullIdx = Constant::getNullValue(Type::IntTy); |
| 5172 | Value *V = new GetElementPtrInst(New, NullIdx, NullIdx, |
| 5173 | New->getName()+".sub", It); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 5174 | |
| 5175 | // Now make everything use the getelementptr instead of the original |
| 5176 | // allocation. |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 5177 | return ReplaceInstUsesWith(AI, V); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5178 | } else if (isa<UndefValue>(AI.getArraySize())) { |
| 5179 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 5180 | } |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 5181 | |
| 5182 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. |
| 5183 | // Note that we only do this for alloca's, because malloc should allocate and |
| 5184 | // return a unique pointer, even for a zero byte allocation. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5185 | if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() && |
Chris Lattner | 49df6ce | 2004-07-02 22:55:47 +0000 | [diff] [blame] | 5186 | TD->getTypeSize(AI.getAllocatedType()) == 0) |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 5187 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
| 5188 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 5189 | return 0; |
| 5190 | } |
| 5191 | |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 5192 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { |
| 5193 | Value *Op = FI.getOperand(0); |
| 5194 | |
| 5195 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X |
| 5196 | if (CastInst *CI = dyn_cast<CastInst>(Op)) |
| 5197 | if (isa<PointerType>(CI->getOperand(0)->getType())) { |
| 5198 | FI.setOperand(0, CI->getOperand(0)); |
| 5199 | return &FI; |
| 5200 | } |
| 5201 | |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 5202 | // free undef -> unreachable. |
| 5203 | if (isa<UndefValue>(Op)) { |
| 5204 | // Insert a new store to null because we cannot modify the CFG here. |
| 5205 | new StoreInst(ConstantBool::True, |
| 5206 | UndefValue::get(PointerType::get(Type::BoolTy)), &FI); |
| 5207 | return EraseInstFromFunction(FI); |
| 5208 | } |
| 5209 | |
Chris Lattner | f3a3660 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 5210 | // If we have 'free null' delete the instruction. This can happen in stl code |
| 5211 | // when lots of inlining happens. |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 5212 | if (isa<ConstantPointerNull>(Op)) |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 5213 | return EraseInstFromFunction(FI); |
Chris Lattner | f3a3660 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 5214 | |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 5215 | return 0; |
| 5216 | } |
| 5217 | |
| 5218 | |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 5219 | /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible. |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 5220 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI) { |
| 5221 | User *CI = cast<User>(LI.getOperand(0)); |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 5222 | Value *CastOp = CI->getOperand(0); |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 5223 | |
| 5224 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 5225 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 5226 | const Type *SrcPTy = SrcTy->getElementType(); |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 5227 | |
| 5228 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) { |
| 5229 | // If the source is an array, the code below will not succeed. Check to |
| 5230 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 5231 | // constants. |
| 5232 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 5233 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 5234 | if (ASrcTy->getNumElements() != 0) { |
| 5235 | std::vector<Value*> Idxs(2, Constant::getNullValue(Type::IntTy)); |
| 5236 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs); |
| 5237 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 5238 | SrcPTy = SrcTy->getElementType(); |
| 5239 | } |
| 5240 | |
| 5241 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
Chris Lattner | ecfa9b5 | 2005-03-29 06:37:47 +0000 | [diff] [blame] | 5242 | // Do not allow turning this into a load of an integer, which is then |
| 5243 | // casted to a pointer, this pessimizes pointer analysis a lot. |
| 5244 | (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5245 | IC.getTargetData().getTypeSize(SrcPTy) == |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 5246 | IC.getTargetData().getTypeSize(DestPTy)) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5247 | |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 5248 | // Okay, we are casting from one integer or pointer type to another of |
| 5249 | // the same size. Instead of casting the pointer before the load, cast |
| 5250 | // the result of the loaded value. |
| 5251 | Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp, |
| 5252 | CI->getName(), |
| 5253 | LI.isVolatile()),LI); |
| 5254 | // Now cast the result of the load. |
| 5255 | return new CastInst(NewLoad, LI.getType()); |
| 5256 | } |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 5257 | } |
| 5258 | } |
| 5259 | return 0; |
| 5260 | } |
| 5261 | |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5262 | /// isSafeToLoadUnconditionally - Return true if we know that executing a load |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 5263 | /// from this value cannot trap. If it is not obviously safe to load from the |
| 5264 | /// specified pointer, we do a quick local scan of the basic block containing |
| 5265 | /// ScanFrom, to determine if the address is already accessed. |
| 5266 | static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) { |
| 5267 | // If it is an alloca or global variable, it is always safe to load from. |
| 5268 | if (isa<AllocaInst>(V) || isa<GlobalVariable>(V)) return true; |
| 5269 | |
| 5270 | // Otherwise, be a little bit agressive by scanning the local block where we |
| 5271 | // 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] | 5272 | // from/to. If so, the previous load or store would have already trapped, |
| 5273 | // so there is no harm doing an extra load (also, CSE will later eliminate |
| 5274 | // the load entirely). |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 5275 | BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); |
| 5276 | |
Alkis Evlogimenos | d59cebf | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 5277 | while (BBI != E) { |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 5278 | --BBI; |
| 5279 | |
| 5280 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 5281 | if (LI->getOperand(0) == V) return true; |
| 5282 | } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 5283 | if (SI->getOperand(1) == V) return true; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5284 | |
Alkis Evlogimenos | d59cebf | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 5285 | } |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 5286 | return false; |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5287 | } |
| 5288 | |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 5289 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 5290 | Value *Op = LI.getOperand(0); |
Chris Lattner | 7e8af38 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 5291 | |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 5292 | // load (cast X) --> cast (load X) iff safe |
| 5293 | if (CastInst *CI = dyn_cast<CastInst>(Op)) |
| 5294 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 5295 | return Res; |
| 5296 | |
| 5297 | // None of the following transforms are legal for volatile loads. |
| 5298 | if (LI.isVolatile()) return 0; |
Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 5299 | |
Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 5300 | if (&LI.getParent()->front() != &LI) { |
| 5301 | BasicBlock::iterator BBI = &LI; --BBI; |
Chris Lattner | e0bfdf1 | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 5302 | // If the instruction immediately before this is a store to the same |
| 5303 | // address, do a simple form of store->load forwarding. |
Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 5304 | if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 5305 | if (SI->getOperand(1) == LI.getOperand(0)) |
| 5306 | return ReplaceInstUsesWith(LI, SI->getOperand(0)); |
Chris Lattner | e0bfdf1 | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 5307 | if (LoadInst *LIB = dyn_cast<LoadInst>(BBI)) |
| 5308 | if (LIB->getOperand(0) == LI.getOperand(0)) |
| 5309 | return ReplaceInstUsesWith(LI, LIB); |
Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 5310 | } |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 5311 | |
| 5312 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) |
| 5313 | if (isa<ConstantPointerNull>(GEPI->getOperand(0)) || |
| 5314 | isa<UndefValue>(GEPI->getOperand(0))) { |
| 5315 | // Insert a new store to null instruction before the load to indicate |
| 5316 | // that this code is not reachable. We do this instead of inserting |
| 5317 | // an unreachable instruction directly because we cannot modify the |
| 5318 | // CFG. |
| 5319 | new StoreInst(UndefValue::get(LI.getType()), |
| 5320 | Constant::getNullValue(Op->getType()), &LI); |
| 5321 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 5322 | } |
| 5323 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5324 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 5325 | // load null/undef -> undef |
| 5326 | if ((C->isNullValue() || isa<UndefValue>(C))) { |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 5327 | // Insert a new store to null instruction before the load to indicate that |
| 5328 | // this code is not reachable. We do this instead of inserting an |
| 5329 | // unreachable instruction directly because we cannot modify the CFG. |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 5330 | new StoreInst(UndefValue::get(LI.getType()), |
| 5331 | Constant::getNullValue(Op->getType()), &LI); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5332 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 5333 | } |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 5334 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5335 | // Instcombine load (constant global) into the value loaded. |
| 5336 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) |
| 5337 | if (GV->isConstant() && !GV->isExternal()) |
| 5338 | return ReplaceInstUsesWith(LI, GV->getInitializer()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5339 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5340 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded. |
| 5341 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) |
| 5342 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 5343 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
| 5344 | if (GV->isConstant() && !GV->isExternal()) |
Chris Lattner | 0b011ec | 2005-09-26 05:28:06 +0000 | [diff] [blame] | 5345 | if (Constant *V = |
| 5346 | ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5347 | return ReplaceInstUsesWith(LI, V); |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 5348 | if (CE->getOperand(0)->isNullValue()) { |
| 5349 | // Insert a new store to null instruction before the load to indicate |
| 5350 | // that this code is not reachable. We do this instead of inserting |
| 5351 | // an unreachable instruction directly because we cannot modify the |
| 5352 | // CFG. |
| 5353 | new StoreInst(UndefValue::get(LI.getType()), |
| 5354 | Constant::getNullValue(Op->getType()), &LI); |
| 5355 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 5356 | } |
| 5357 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5358 | } else if (CE->getOpcode() == Instruction::Cast) { |
| 5359 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 5360 | return Res; |
| 5361 | } |
| 5362 | } |
Chris Lattner | e228ee5 | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 5363 | |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 5364 | if (Op->hasOneUse()) { |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5365 | // Change select and PHI nodes to select values instead of addresses: this |
| 5366 | // helps alias analysis out a lot, allows many others simplifications, and |
| 5367 | // exposes redundancy in the code. |
| 5368 | // |
| 5369 | // Note that we cannot do the transformation unless we know that the |
| 5370 | // introduced loads cannot trap! Something like this is valid as long as |
| 5371 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 5372 | // but it would not be valid if we transformed it to load from null |
| 5373 | // unconditionally. |
| 5374 | // |
| 5375 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 5376 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 5377 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && |
| 5378 | isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5379 | Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1), |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 5380 | SI->getOperand(1)->getName()+".val"), LI); |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5381 | Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2), |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 5382 | SI->getOperand(2)->getName()+".val"), LI); |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5383 | return new SelectInst(SI->getCondition(), V1, V2); |
| 5384 | } |
| 5385 | |
Chris Lattner | bdcf41a | 2004-09-23 15:46:00 +0000 | [diff] [blame] | 5386 | // load (select (cond, null, P)) -> load P |
| 5387 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) |
| 5388 | if (C->isNullValue()) { |
| 5389 | LI.setOperand(0, SI->getOperand(2)); |
| 5390 | return &LI; |
| 5391 | } |
| 5392 | |
| 5393 | // load (select (cond, P, null)) -> load P |
| 5394 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) |
| 5395 | if (C->isNullValue()) { |
| 5396 | LI.setOperand(0, SI->getOperand(1)); |
| 5397 | return &LI; |
| 5398 | } |
| 5399 | |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5400 | } else if (PHINode *PN = dyn_cast<PHINode>(Op)) { |
| 5401 | // load (phi (&V1, &V2, &V3)) --> phi(load &V1, load &V2, load &V3) |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 5402 | bool Safe = PN->getParent() == LI.getParent(); |
| 5403 | |
| 5404 | // Scan all of the instructions between the PHI and the load to make |
| 5405 | // sure there are no instructions that might possibly alter the value |
| 5406 | // loaded from the PHI. |
| 5407 | if (Safe) { |
| 5408 | BasicBlock::iterator I = &LI; |
| 5409 | for (--I; !isa<PHINode>(I); --I) |
| 5410 | if (isa<StoreInst>(I) || isa<CallInst>(I)) { |
| 5411 | Safe = false; |
| 5412 | break; |
| 5413 | } |
| 5414 | } |
| 5415 | |
| 5416 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e && Safe; ++i) |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 5417 | if (!isSafeToLoadUnconditionally(PN->getIncomingValue(i), |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 5418 | PN->getIncomingBlock(i)->getTerminator())) |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5419 | Safe = false; |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 5420 | |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5421 | if (Safe) { |
| 5422 | // Create the PHI. |
| 5423 | PHINode *NewPN = new PHINode(LI.getType(), PN->getName()); |
| 5424 | InsertNewInstBefore(NewPN, *PN); |
| 5425 | std::map<BasicBlock*,Value*> LoadMap; // Don't insert duplicate loads |
| 5426 | |
| 5427 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 5428 | BasicBlock *BB = PN->getIncomingBlock(i); |
| 5429 | Value *&TheLoad = LoadMap[BB]; |
| 5430 | if (TheLoad == 0) { |
| 5431 | Value *InVal = PN->getIncomingValue(i); |
| 5432 | TheLoad = InsertNewInstBefore(new LoadInst(InVal, |
| 5433 | InVal->getName()+".val"), |
| 5434 | *BB->getTerminator()); |
| 5435 | } |
| 5436 | NewPN->addIncoming(TheLoad, BB); |
| 5437 | } |
| 5438 | return ReplaceInstUsesWith(LI, NewPN); |
| 5439 | } |
| 5440 | } |
| 5441 | } |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 5442 | return 0; |
| 5443 | } |
| 5444 | |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 5445 | /// InstCombineStoreToCast - Fold 'store V, (cast P)' -> store (cast V), P' |
| 5446 | /// when possible. |
| 5447 | static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { |
| 5448 | User *CI = cast<User>(SI.getOperand(1)); |
| 5449 | Value *CastOp = CI->getOperand(0); |
| 5450 | |
| 5451 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
| 5452 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
| 5453 | const Type *SrcPTy = SrcTy->getElementType(); |
| 5454 | |
| 5455 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) { |
| 5456 | // If the source is an array, the code below will not succeed. Check to |
| 5457 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 5458 | // constants. |
| 5459 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 5460 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 5461 | if (ASrcTy->getNumElements() != 0) { |
| 5462 | std::vector<Value*> Idxs(2, Constant::getNullValue(Type::IntTy)); |
| 5463 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs); |
| 5464 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 5465 | SrcPTy = SrcTy->getElementType(); |
| 5466 | } |
| 5467 | |
| 5468 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5469 | IC.getTargetData().getTypeSize(SrcPTy) == |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 5470 | IC.getTargetData().getTypeSize(DestPTy)) { |
| 5471 | |
| 5472 | // Okay, we are casting from one integer or pointer type to another of |
| 5473 | // the same size. Instead of casting the pointer before the store, cast |
| 5474 | // the value to be stored. |
| 5475 | Value *NewCast; |
| 5476 | if (Constant *C = dyn_cast<Constant>(SI.getOperand(0))) |
| 5477 | NewCast = ConstantExpr::getCast(C, SrcPTy); |
| 5478 | else |
| 5479 | NewCast = IC.InsertNewInstBefore(new CastInst(SI.getOperand(0), |
| 5480 | SrcPTy, |
| 5481 | SI.getOperand(0)->getName()+".c"), SI); |
| 5482 | |
| 5483 | return new StoreInst(NewCast, CastOp); |
| 5484 | } |
| 5485 | } |
| 5486 | } |
| 5487 | return 0; |
| 5488 | } |
| 5489 | |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 5490 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
| 5491 | Value *Val = SI.getOperand(0); |
| 5492 | Value *Ptr = SI.getOperand(1); |
| 5493 | |
| 5494 | if (isa<UndefValue>(Ptr)) { // store X, undef -> noop (even if volatile) |
| 5495 | removeFromWorkList(&SI); |
| 5496 | SI.eraseFromParent(); |
| 5497 | ++NumCombined; |
| 5498 | return 0; |
| 5499 | } |
| 5500 | |
| 5501 | if (SI.isVolatile()) return 0; // Don't hack volatile loads. |
| 5502 | |
| 5503 | // store X, null -> turns into 'unreachable' in SimplifyCFG |
| 5504 | if (isa<ConstantPointerNull>(Ptr)) { |
| 5505 | if (!isa<UndefValue>(Val)) { |
| 5506 | SI.setOperand(0, UndefValue::get(Val->getType())); |
| 5507 | if (Instruction *U = dyn_cast<Instruction>(Val)) |
| 5508 | WorkList.push_back(U); // Dropped a use. |
| 5509 | ++NumCombined; |
| 5510 | } |
| 5511 | return 0; // Do not modify these! |
| 5512 | } |
| 5513 | |
| 5514 | // store undef, Ptr -> noop |
| 5515 | if (isa<UndefValue>(Val)) { |
| 5516 | removeFromWorkList(&SI); |
| 5517 | SI.eraseFromParent(); |
| 5518 | ++NumCombined; |
| 5519 | return 0; |
| 5520 | } |
| 5521 | |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 5522 | // If the pointer destination is a cast, see if we can fold the cast into the |
| 5523 | // source instead. |
| 5524 | if (CastInst *CI = dyn_cast<CastInst>(Ptr)) |
| 5525 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 5526 | return Res; |
| 5527 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
| 5528 | if (CE->getOpcode() == Instruction::Cast) |
| 5529 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 5530 | return Res; |
| 5531 | |
Chris Lattner | 219175c | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 5532 | |
| 5533 | // If this store is the last instruction in the basic block, and if the block |
| 5534 | // ends with an unconditional branch, try to move it to the successor block. |
| 5535 | BasicBlock::iterator BBI = &SI; ++BBI; |
| 5536 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) |
| 5537 | if (BI->isUnconditional()) { |
| 5538 | // Check to see if the successor block has exactly two incoming edges. If |
| 5539 | // so, see if the other predecessor contains a store to the same location. |
| 5540 | // if so, insert a PHI node (if needed) and move the stores down. |
| 5541 | BasicBlock *Dest = BI->getSuccessor(0); |
| 5542 | |
| 5543 | pred_iterator PI = pred_begin(Dest); |
| 5544 | BasicBlock *Other = 0; |
| 5545 | if (*PI != BI->getParent()) |
| 5546 | Other = *PI; |
| 5547 | ++PI; |
| 5548 | if (PI != pred_end(Dest)) { |
| 5549 | if (*PI != BI->getParent()) |
| 5550 | if (Other) |
| 5551 | Other = 0; |
| 5552 | else |
| 5553 | Other = *PI; |
| 5554 | if (++PI != pred_end(Dest)) |
| 5555 | Other = 0; |
| 5556 | } |
| 5557 | if (Other) { // If only one other pred... |
| 5558 | BBI = Other->getTerminator(); |
| 5559 | // Make sure this other block ends in an unconditional branch and that |
| 5560 | // there is an instruction before the branch. |
| 5561 | if (isa<BranchInst>(BBI) && cast<BranchInst>(BBI)->isUnconditional() && |
| 5562 | BBI != Other->begin()) { |
| 5563 | --BBI; |
| 5564 | StoreInst *OtherStore = dyn_cast<StoreInst>(BBI); |
| 5565 | |
| 5566 | // If this instruction is a store to the same location. |
| 5567 | if (OtherStore && OtherStore->getOperand(1) == SI.getOperand(1)) { |
| 5568 | // Okay, we know we can perform this transformation. Insert a PHI |
| 5569 | // node now if we need it. |
| 5570 | Value *MergedVal = OtherStore->getOperand(0); |
| 5571 | if (MergedVal != SI.getOperand(0)) { |
| 5572 | PHINode *PN = new PHINode(MergedVal->getType(), "storemerge"); |
| 5573 | PN->reserveOperandSpace(2); |
| 5574 | PN->addIncoming(SI.getOperand(0), SI.getParent()); |
| 5575 | PN->addIncoming(OtherStore->getOperand(0), Other); |
| 5576 | MergedVal = InsertNewInstBefore(PN, Dest->front()); |
| 5577 | } |
| 5578 | |
| 5579 | // Advance to a place where it is safe to insert the new store and |
| 5580 | // insert it. |
| 5581 | BBI = Dest->begin(); |
| 5582 | while (isa<PHINode>(BBI)) ++BBI; |
| 5583 | InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1), |
| 5584 | OtherStore->isVolatile()), *BBI); |
| 5585 | |
| 5586 | // Nuke the old stores. |
| 5587 | removeFromWorkList(&SI); |
| 5588 | removeFromWorkList(OtherStore); |
| 5589 | SI.eraseFromParent(); |
| 5590 | OtherStore->eraseFromParent(); |
| 5591 | ++NumCombined; |
| 5592 | return 0; |
| 5593 | } |
| 5594 | } |
| 5595 | } |
| 5596 | } |
| 5597 | |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 5598 | return 0; |
| 5599 | } |
| 5600 | |
| 5601 | |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 5602 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 5603 | // 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] | 5604 | Value *X = 0; |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 5605 | BasicBlock *TrueDest; |
| 5606 | BasicBlock *FalseDest; |
| 5607 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && |
| 5608 | !isa<Constant>(X)) { |
| 5609 | // Swap Destinations and condition... |
| 5610 | BI.setCondition(X); |
| 5611 | BI.setSuccessor(0, FalseDest); |
| 5612 | BI.setSuccessor(1, TrueDest); |
| 5613 | return &BI; |
| 5614 | } |
| 5615 | |
| 5616 | // Cannonicalize setne -> seteq |
| 5617 | Instruction::BinaryOps Op; Value *Y; |
| 5618 | if (match(&BI, m_Br(m_SetCond(Op, m_Value(X), m_Value(Y)), |
| 5619 | TrueDest, FalseDest))) |
| 5620 | if ((Op == Instruction::SetNE || Op == Instruction::SetLE || |
| 5621 | Op == Instruction::SetGE) && BI.getCondition()->hasOneUse()) { |
| 5622 | SetCondInst *I = cast<SetCondInst>(BI.getCondition()); |
| 5623 | std::string Name = I->getName(); I->setName(""); |
| 5624 | Instruction::BinaryOps NewOpcode = SetCondInst::getInverseCondition(Op); |
| 5625 | Value *NewSCC = BinaryOperator::create(NewOpcode, X, Y, Name, I); |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 5626 | // Swap Destinations and condition... |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 5627 | BI.setCondition(NewSCC); |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 5628 | BI.setSuccessor(0, FalseDest); |
| 5629 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 5630 | removeFromWorkList(I); |
| 5631 | I->getParent()->getInstList().erase(I); |
| 5632 | WorkList.push_back(cast<Instruction>(NewSCC)); |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 5633 | return &BI; |
| 5634 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5635 | |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 5636 | return 0; |
| 5637 | } |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 5638 | |
Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 5639 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
| 5640 | Value *Cond = SI.getCondition(); |
| 5641 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { |
| 5642 | if (I->getOpcode() == Instruction::Add) |
| 5643 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 5644 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' |
| 5645 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5646 | SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), |
Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 5647 | AddRHS)); |
| 5648 | SI.setOperand(0, I->getOperand(0)); |
| 5649 | WorkList.push_back(I); |
| 5650 | return &SI; |
| 5651 | } |
| 5652 | } |
| 5653 | return 0; |
| 5654 | } |
| 5655 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5656 | void InstCombiner::removeFromWorkList(Instruction *I) { |
| 5657 | WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I), |
| 5658 | WorkList.end()); |
| 5659 | } |
| 5660 | |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 5661 | |
| 5662 | /// TryToSinkInstruction - Try to move the specified instruction from its |
| 5663 | /// current block into the beginning of DestBlock, which can only happen if it's |
| 5664 | /// safe to move the instruction past all of the instructions between it and the |
| 5665 | /// end of its block. |
| 5666 | static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { |
| 5667 | assert(I->hasOneUse() && "Invariants didn't hold!"); |
| 5668 | |
| 5669 | // Cannot move control-flow-involving instructions. |
| 5670 | if (isa<PHINode>(I) || isa<InvokeInst>(I) || isa<CallInst>(I)) return false; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5671 | |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 5672 | // Do not sink alloca instructions out of the entry block. |
| 5673 | if (isa<AllocaInst>(I) && I->getParent() == &DestBlock->getParent()->front()) |
| 5674 | return false; |
| 5675 | |
Chris Lattner | f17a2fb | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 5676 | // We can only sink load instructions if there is nothing between the load and |
| 5677 | // the end of block that could change the value. |
| 5678 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 5679 | if (LI->isVolatile()) return false; // Don't sink volatile loads. |
| 5680 | |
| 5681 | for (BasicBlock::iterator Scan = LI, E = LI->getParent()->end(); |
| 5682 | Scan != E; ++Scan) |
| 5683 | if (Scan->mayWriteToMemory()) |
| 5684 | return false; |
Chris Lattner | f17a2fb | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 5685 | } |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 5686 | |
| 5687 | BasicBlock::iterator InsertPos = DestBlock->begin(); |
| 5688 | while (isa<PHINode>(InsertPos)) ++InsertPos; |
| 5689 | |
Chris Lattner | 9f269e4 | 2005-08-08 19:11:57 +0000 | [diff] [blame] | 5690 | I->moveBefore(InsertPos); |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 5691 | ++NumSunkInst; |
| 5692 | return true; |
| 5693 | } |
| 5694 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5695 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 5696 | bool Changed = false; |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 5697 | TD = &getAnalysis<TargetData>(); |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 5698 | |
Chris Lattner | 4ed40f7 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 5699 | { |
| 5700 | // Populate the worklist with the reachable instructions. |
| 5701 | std::set<BasicBlock*> Visited; |
| 5702 | for (df_ext_iterator<BasicBlock*> BB = df_ext_begin(&F.front(), Visited), |
| 5703 | E = df_ext_end(&F.front(), Visited); BB != E; ++BB) |
| 5704 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 5705 | WorkList.push_back(I); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 5706 | |
Chris Lattner | 4ed40f7 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 5707 | // Do a quick scan over the function. If we find any blocks that are |
| 5708 | // unreachable, remove any instructions inside of them. This prevents |
| 5709 | // the instcombine code from having to deal with some bad special cases. |
| 5710 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 5711 | if (!Visited.count(BB)) { |
| 5712 | Instruction *Term = BB->getTerminator(); |
| 5713 | while (Term != BB->begin()) { // Remove instrs bottom-up |
| 5714 | BasicBlock::iterator I = Term; --I; |
Chris Lattner | 2d3a7a6 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 5715 | |
Chris Lattner | 4ed40f7 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 5716 | DEBUG(std::cerr << "IC: DCE: " << *I); |
| 5717 | ++NumDeadInst; |
| 5718 | |
| 5719 | if (!I->use_empty()) |
| 5720 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 5721 | I->eraseFromParent(); |
| 5722 | } |
| 5723 | } |
| 5724 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 5725 | |
| 5726 | while (!WorkList.empty()) { |
| 5727 | Instruction *I = WorkList.back(); // Get an instruction from the worklist |
| 5728 | WorkList.pop_back(); |
| 5729 | |
Misha Brukman | 632df28 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 5730 | // Check to see if we can DCE or ConstantPropagate the instruction... |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5731 | // Check to see if we can DIE the instruction... |
| 5732 | if (isInstructionTriviallyDead(I)) { |
| 5733 | // Add operands to the worklist... |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5734 | if (I->getNumOperands() < 4) |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 5735 | AddUsesToWorkList(*I); |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5736 | ++NumDeadInst; |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5737 | |
Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 5738 | DEBUG(std::cerr << "IC: DCE: " << *I); |
| 5739 | |
| 5740 | I->eraseFromParent(); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5741 | removeFromWorkList(I); |
| 5742 | continue; |
| 5743 | } |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5744 | |
Misha Brukman | 632df28 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 5745 | // Instruction isn't dead, see if we can constant propagate it... |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5746 | if (Constant *C = ConstantFoldInstruction(I)) { |
Alkis Evlogimenos | a1291a0 | 2004-12-08 23:10:30 +0000 | [diff] [blame] | 5747 | Value* Ptr = I->getOperand(0); |
Chris Lattner | 6580e09 | 2004-10-16 19:44:59 +0000 | [diff] [blame] | 5748 | if (isa<GetElementPtrInst>(I) && |
Alkis Evlogimenos | a1291a0 | 2004-12-08 23:10:30 +0000 | [diff] [blame] | 5749 | cast<Constant>(Ptr)->isNullValue() && |
| 5750 | !isa<ConstantPointerNull>(C) && |
| 5751 | cast<PointerType>(Ptr->getType())->getElementType()->isSized()) { |
Chris Lattner | 6580e09 | 2004-10-16 19:44:59 +0000 | [diff] [blame] | 5752 | // If this is a constant expr gep that is effectively computing an |
| 5753 | // "offsetof", fold it into 'cast int X to T*' instead of 'gep 0, 0, 12' |
| 5754 | bool isFoldableGEP = true; |
| 5755 | for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i) |
| 5756 | if (!isa<ConstantInt>(I->getOperand(i))) |
| 5757 | isFoldableGEP = false; |
| 5758 | if (isFoldableGEP) { |
Alkis Evlogimenos | a1291a0 | 2004-12-08 23:10:30 +0000 | [diff] [blame] | 5759 | uint64_t Offset = TD->getIndexedOffset(Ptr->getType(), |
Chris Lattner | 6580e09 | 2004-10-16 19:44:59 +0000 | [diff] [blame] | 5760 | std::vector<Value*>(I->op_begin()+1, I->op_end())); |
| 5761 | C = ConstantUInt::get(Type::ULongTy, Offset); |
Chris Lattner | 684c5c6 | 2004-10-16 19:46:33 +0000 | [diff] [blame] | 5762 | C = ConstantExpr::getCast(C, TD->getIntPtrType()); |
Chris Lattner | 6580e09 | 2004-10-16 19:44:59 +0000 | [diff] [blame] | 5763 | C = ConstantExpr::getCast(C, I->getType()); |
| 5764 | } |
| 5765 | } |
| 5766 | |
Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 5767 | DEBUG(std::cerr << "IC: ConstFold to: " << *C << " from: " << *I); |
| 5768 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5769 | // Add operands to the worklist... |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 5770 | AddUsesToWorkList(*I); |
Chris Lattner | c6509f4 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 5771 | ReplaceInstUsesWith(*I, C); |
| 5772 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5773 | ++NumConstProp; |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5774 | I->getParent()->getInstList().erase(I); |
Chris Lattner | 800aaaf | 2003-10-07 15:17:02 +0000 | [diff] [blame] | 5775 | removeFromWorkList(I); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5776 | continue; |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5777 | } |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5778 | |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 5779 | // See if we can trivially sink this instruction to a successor basic block. |
| 5780 | if (I->hasOneUse()) { |
| 5781 | BasicBlock *BB = I->getParent(); |
| 5782 | BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent(); |
| 5783 | if (UserParent != BB) { |
| 5784 | bool UserIsSuccessor = false; |
| 5785 | // See if the user is one of our successors. |
| 5786 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) |
| 5787 | if (*SI == UserParent) { |
| 5788 | UserIsSuccessor = true; |
| 5789 | break; |
| 5790 | } |
| 5791 | |
| 5792 | // If the user is one of our immediate successors, and if that successor |
| 5793 | // only has us as a predecessors (we'd have to split the critical edge |
| 5794 | // otherwise), we can keep going. |
| 5795 | if (UserIsSuccessor && !isa<PHINode>(I->use_back()) && |
| 5796 | next(pred_begin(UserParent)) == pred_end(UserParent)) |
| 5797 | // Okay, the CFG is simple enough, try to sink this instruction. |
| 5798 | Changed |= TryToSinkInstruction(I, UserParent); |
| 5799 | } |
| 5800 | } |
| 5801 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 5802 | // Now that we have an instruction, try combining it to simplify it... |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 5803 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 5804 | ++NumCombined; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 5805 | // Should we replace the old instruction with a new one? |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 5806 | if (Result != I) { |
Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 5807 | DEBUG(std::cerr << "IC: Old = " << *I |
| 5808 | << " New = " << *Result); |
| 5809 | |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 5810 | // Everything uses the new instruction now. |
| 5811 | I->replaceAllUsesWith(Result); |
| 5812 | |
| 5813 | // Push the new instruction and any users onto the worklist. |
| 5814 | WorkList.push_back(Result); |
| 5815 | AddUsersToWorkList(*Result); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5816 | |
| 5817 | // Move the name to the new instruction first... |
| 5818 | std::string OldName = I->getName(); I->setName(""); |
Chris Lattner | 950fc78 | 2003-10-07 22:58:41 +0000 | [diff] [blame] | 5819 | Result->setName(OldName); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5820 | |
| 5821 | // Insert the new instruction into the basic block... |
| 5822 | BasicBlock *InstParent = I->getParent(); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 5823 | BasicBlock::iterator InsertPos = I; |
| 5824 | |
| 5825 | if (!isa<PHINode>(Result)) // If combining a PHI, don't insert |
| 5826 | while (isa<PHINode>(InsertPos)) // middle of a block of PHIs. |
| 5827 | ++InsertPos; |
| 5828 | |
| 5829 | InstParent->getInstList().insert(InsertPos, Result); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5830 | |
Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 5831 | // Make sure that we reprocess all operands now that we reduced their |
| 5832 | // use counts. |
Chris Lattner | b643a9e | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 5833 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 5834 | if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i))) |
| 5835 | WorkList.push_back(OpI); |
| 5836 | |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 5837 | // Instructions can end up on the worklist more than once. Make sure |
| 5838 | // we do not process an instruction that has been deleted. |
| 5839 | removeFromWorkList(I); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5840 | |
| 5841 | // Erase the old instruction. |
| 5842 | InstParent->getInstList().erase(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5843 | } else { |
Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 5844 | DEBUG(std::cerr << "IC: MOD = " << *I); |
| 5845 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 5846 | // If the instruction was modified, it's possible that it is now dead. |
| 5847 | // if so, remove it. |
Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 5848 | if (isInstructionTriviallyDead(I)) { |
| 5849 | // Make sure we process all operands now that we are reducing their |
| 5850 | // use counts. |
| 5851 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 5852 | if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i))) |
| 5853 | WorkList.push_back(OpI); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5854 | |
Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 5855 | // Instructions may end up in the worklist more than once. Erase all |
| 5856 | // occurrances of this instruction. |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5857 | removeFromWorkList(I); |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 5858 | I->eraseFromParent(); |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 5859 | } else { |
| 5860 | WorkList.push_back(Result); |
| 5861 | AddUsersToWorkList(*Result); |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 5862 | } |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 5863 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 5864 | Changed = true; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 5865 | } |
| 5866 | } |
| 5867 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 5868 | return Changed; |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 5869 | } |
| 5870 | |
Brian Gaeke | 38b79e8 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 5871 | FunctionPass *llvm::createInstructionCombiningPass() { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 5872 | return new InstCombiner(); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 5873 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 5874 | |