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 | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 225 | |
| 226 | Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
| 227 | bool Inside, Instruction &IB); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 228 | }; |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 229 | |
Chris Lattner | c8b7092 | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 230 | RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions"); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 233 | // getComplexity: Assign a complexity or rank value to LLVM Values... |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 234 | // 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 235 | static unsigned getComplexity(Value *V) { |
| 236 | if (isa<Instruction>(V)) { |
| 237 | if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V)) |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 238 | return 3; |
| 239 | return 4; |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 240 | } |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 241 | if (isa<Argument>(V)) return 3; |
| 242 | return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2; |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 243 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 244 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 245 | // isOnlyUse - Return true if this instruction will be deleted if we stop using |
| 246 | // it. |
| 247 | static bool isOnlyUse(Value *V) { |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 248 | return V->hasOneUse() || isa<Constant>(V); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 251 | // getPromotedType - Return the specified type promoted as it would be to pass |
| 252 | // though a va_arg area... |
| 253 | static const Type *getPromotedType(const Type *Ty) { |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 254 | switch (Ty->getTypeID()) { |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 255 | case Type::SByteTyID: |
| 256 | case Type::ShortTyID: return Type::IntTy; |
| 257 | case Type::UByteTyID: |
| 258 | case Type::UShortTyID: return Type::UIntTy; |
| 259 | case Type::FloatTyID: return Type::DoubleTy; |
| 260 | default: return Ty; |
| 261 | } |
| 262 | } |
| 263 | |
Chris Lattner | 567b81f | 2005-09-13 00:40:14 +0000 | [diff] [blame^] | 264 | /// isCast - If the specified operand is a CastInst or a constant expr cast, |
| 265 | /// return the operand value, otherwise return null. |
| 266 | static Value *isCast(Value *V) { |
| 267 | if (CastInst *I = dyn_cast<CastInst>(V)) |
| 268 | return I->getOperand(0); |
| 269 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 270 | if (CE->getOpcode() == Instruction::Cast) |
| 271 | return CE->getOperand(0); |
| 272 | return 0; |
| 273 | } |
| 274 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 275 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 276 | // operators: |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 277 | // |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 278 | // 1. Order operands such that they are listed from right (least complex) to |
| 279 | // left (most complex). This puts constants before unary operators before |
| 280 | // binary operators. |
| 281 | // |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 282 | // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) |
| 283 | // 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] | 284 | // |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 285 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 286 | bool Changed = false; |
| 287 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) |
| 288 | Changed = !I.swapOperands(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 289 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 290 | if (!I.isAssociative()) return Changed; |
| 291 | Instruction::BinaryOps Opcode = I.getOpcode(); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 292 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) |
| 293 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { |
| 294 | if (isa<Constant>(I.getOperand(1))) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 295 | Constant *Folded = ConstantExpr::get(I.getOpcode(), |
| 296 | cast<Constant>(I.getOperand(1)), |
| 297 | cast<Constant>(Op->getOperand(1))); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 298 | I.setOperand(0, Op->getOperand(0)); |
| 299 | I.setOperand(1, Folded); |
| 300 | return true; |
| 301 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) |
| 302 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && |
| 303 | isOnlyUse(Op) && isOnlyUse(Op1)) { |
| 304 | Constant *C1 = cast<Constant>(Op->getOperand(1)); |
| 305 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); |
| 306 | |
| 307 | // 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] | 308 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 309 | Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0), |
| 310 | Op1->getOperand(0), |
| 311 | Op1->getName(), &I); |
| 312 | WorkList.push_back(New); |
| 313 | I.setOperand(0, New); |
| 314 | I.setOperand(1, Folded); |
| 315 | return true; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 316 | } |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 317 | } |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 318 | return Changed; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 319 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 320 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 321 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
| 322 | // if the LHS is a constant zero (which is the 'negate' form). |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 323 | // |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 324 | static inline Value *dyn_castNegVal(Value *V) { |
| 325 | if (BinaryOperator::isNeg(V)) |
Chris Lattner | d6f636a | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 326 | return BinaryOperator::getNegArgument(V); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 327 | |
Chris Lattner | 9ad0d55 | 2004-12-14 20:08:06 +0000 | [diff] [blame] | 328 | // Constants can be considered to be negated values if they can be folded. |
| 329 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
| 330 | return ConstantExpr::getNeg(C); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 331 | return 0; |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 334 | static inline Value *dyn_castNotVal(Value *V) { |
| 335 | if (BinaryOperator::isNot(V)) |
Chris Lattner | d6f636a | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 336 | return BinaryOperator::getNotArgument(V); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 337 | |
| 338 | // Constants can be considered to be not'ed values... |
Chris Lattner | dd65d86 | 2003-04-30 22:34:06 +0000 | [diff] [blame] | 339 | if (ConstantIntegral *C = dyn_cast<ConstantIntegral>(V)) |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 340 | return ConstantExpr::getNot(C); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 341 | return 0; |
| 342 | } |
| 343 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 344 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
| 345 | // other computations (because it has a constant operand), return the |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 346 | // non-constant operand of the multiply, and set CST to point to the multiplier. |
| 347 | // Otherwise, return null. |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 348 | // |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 349 | static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) { |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 350 | if (V->hasOneUse() && V->getType()->isInteger()) |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 351 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 352 | if (I->getOpcode() == Instruction::Mul) |
Chris Lattner | 97013636 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 353 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 354 | return I->getOperand(0); |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 355 | if (I->getOpcode() == Instruction::Shl) |
Chris Lattner | 97013636 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 356 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) { |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 357 | // The multiplier is really 1 << CST. |
| 358 | Constant *One = ConstantInt::get(V->getType(), 1); |
| 359 | CST = cast<ConstantInt>(ConstantExpr::getShl(One, CST)); |
| 360 | return I->getOperand(0); |
| 361 | } |
| 362 | } |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 363 | return 0; |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 364 | } |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 365 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 366 | /// dyn_castGetElementPtr - If this is a getelementptr instruction or constant |
| 367 | /// expression, return it. |
| 368 | static User *dyn_castGetElementPtr(Value *V) { |
| 369 | if (isa<GetElementPtrInst>(V)) return cast<User>(V); |
| 370 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 371 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 372 | return cast<User>(V); |
| 373 | return false; |
| 374 | } |
| 375 | |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 376 | // AddOne, SubOne - Add or subtract a constant one from an integer constant... |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 377 | static ConstantInt *AddOne(ConstantInt *C) { |
| 378 | return cast<ConstantInt>(ConstantExpr::getAdd(C, |
| 379 | ConstantInt::get(C->getType(), 1))); |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 380 | } |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 381 | static ConstantInt *SubOne(ConstantInt *C) { |
| 382 | return cast<ConstantInt>(ConstantExpr::getSub(C, |
| 383 | ConstantInt::get(C->getType(), 1))); |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | // isTrueWhenEqual - Return true if the specified setcondinst instruction is |
| 387 | // true when both operands are equal... |
| 388 | // |
| 389 | static bool isTrueWhenEqual(Instruction &I) { |
| 390 | return I.getOpcode() == Instruction::SetEQ || |
| 391 | I.getOpcode() == Instruction::SetGE || |
| 392 | I.getOpcode() == Instruction::SetLE; |
| 393 | } |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 394 | |
| 395 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
| 396 | /// function is designed to check a chain of associative operators for a |
| 397 | /// potential to apply a certain optimization. Since the optimization may be |
| 398 | /// applicable if the expression was reassociated, this checks the chain, then |
| 399 | /// reassociates the expression as necessary to expose the optimization |
| 400 | /// opportunity. This makes use of a special Functor, which must define |
| 401 | /// 'shouldApply' and 'apply' methods. |
| 402 | /// |
| 403 | template<typename Functor> |
| 404 | Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { |
| 405 | unsigned Opcode = Root.getOpcode(); |
| 406 | Value *LHS = Root.getOperand(0); |
| 407 | |
| 408 | // Quick check, see if the immediate LHS matches... |
| 409 | if (F.shouldApply(LHS)) |
| 410 | return F.apply(Root); |
| 411 | |
| 412 | // Otherwise, if the LHS is not of the same opcode as the root, return. |
| 413 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 414 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 415 | // Should we apply this transform to the RHS? |
| 416 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); |
| 417 | |
| 418 | // If not to the RHS, check to see if we should apply to the LHS... |
| 419 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { |
| 420 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS |
| 421 | ShouldApply = true; |
| 422 | } |
| 423 | |
| 424 | // If the functor wants to apply the optimization to the RHS of LHSI, |
| 425 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) |
| 426 | if (ShouldApply) { |
| 427 | BasicBlock *BB = Root.getParent(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 428 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 429 | // Now all of the instructions are in the current basic block, go ahead |
| 430 | // and perform the reassociation. |
| 431 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); |
| 432 | |
| 433 | // First move the selected RHS to the LHS of the root... |
| 434 | Root.setOperand(0, LHSI->getOperand(1)); |
| 435 | |
| 436 | // Make what used to be the LHS of the root be the user of the root... |
| 437 | Value *ExtraOperand = TmpLHSI->getOperand(1); |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 438 | if (&Root == TmpLHSI) { |
Chris Lattner | 8953b90 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 439 | Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); |
| 440 | return 0; |
| 441 | } |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 442 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 443 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 444 | TmpLHSI->getParent()->getInstList().remove(TmpLHSI); |
| 445 | BasicBlock::iterator ARI = &Root; ++ARI; |
| 446 | BB->getInstList().insert(ARI, TmpLHSI); // Move TmpLHSI to after Root |
| 447 | ARI = Root; |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 448 | |
| 449 | // Now propagate the ExtraOperand down the chain of instructions until we |
| 450 | // get to LHSI. |
| 451 | while (TmpLHSI != LHSI) { |
| 452 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 453 | // Move the instruction to immediately before the chain we are |
| 454 | // constructing to avoid breaking dominance properties. |
| 455 | NextLHSI->getParent()->getInstList().remove(NextLHSI); |
| 456 | BB->getInstList().insert(ARI, NextLHSI); |
| 457 | ARI = NextLHSI; |
| 458 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 459 | Value *NextOp = NextLHSI->getOperand(1); |
| 460 | NextLHSI->setOperand(1, ExtraOperand); |
| 461 | TmpLHSI = NextLHSI; |
| 462 | ExtraOperand = NextOp; |
| 463 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 464 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 465 | // Now that the instructions are reassociated, have the functor perform |
| 466 | // the transformation... |
| 467 | return F.apply(Root); |
| 468 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 469 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 470 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
| 471 | } |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | |
| 476 | // AddRHS - Implements: X + X --> X << 1 |
| 477 | struct AddRHS { |
| 478 | Value *RHS; |
| 479 | AddRHS(Value *rhs) : RHS(rhs) {} |
| 480 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 481 | Instruction *apply(BinaryOperator &Add) const { |
| 482 | return new ShiftInst(Instruction::Shl, Add.getOperand(0), |
| 483 | ConstantInt::get(Type::UByteTy, 1)); |
| 484 | } |
| 485 | }; |
| 486 | |
| 487 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) |
| 488 | // iff C1&C2 == 0 |
| 489 | struct AddMaskingAnd { |
| 490 | Constant *C2; |
| 491 | AddMaskingAnd(Constant *c) : C2(c) {} |
| 492 | bool shouldApply(Value *LHS) const { |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 493 | ConstantInt *C1; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 494 | return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 495 | ConstantExpr::getAnd(C1, C2)->isNullValue(); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 496 | } |
| 497 | Instruction *apply(BinaryOperator &Add) const { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 498 | return BinaryOperator::createOr(Add.getOperand(0), Add.getOperand(1)); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 499 | } |
| 500 | }; |
| 501 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 502 | static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 503 | InstCombiner *IC) { |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 504 | if (isa<CastInst>(I)) { |
| 505 | if (Constant *SOC = dyn_cast<Constant>(SO)) |
| 506 | return ConstantExpr::getCast(SOC, I.getType()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 507 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 508 | return IC->InsertNewInstBefore(new CastInst(SO, I.getType(), |
| 509 | SO->getName() + ".cast"), I); |
| 510 | } |
| 511 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 512 | // Figure out if the constant is the left or the right argument. |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 513 | bool ConstIsRHS = isa<Constant>(I.getOperand(1)); |
| 514 | Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS)); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 515 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 516 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
| 517 | if (ConstIsRHS) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 518 | return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand); |
| 519 | return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC); |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | Value *Op0 = SO, *Op1 = ConstOperand; |
| 523 | if (!ConstIsRHS) |
| 524 | std::swap(Op0, Op1); |
| 525 | Instruction *New; |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 526 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| 527 | New = BinaryOperator::create(BO->getOpcode(), Op0, Op1,SO->getName()+".op"); |
| 528 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(&I)) |
| 529 | New = new ShiftInst(SI->getOpcode(), Op0, Op1, SO->getName()+".sh"); |
Chris Lattner | f9d9665 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 530 | else { |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 531 | assert(0 && "Unknown binary instruction type!"); |
Chris Lattner | f9d9665 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 532 | abort(); |
| 533 | } |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 534 | return IC->InsertNewInstBefore(New, I); |
| 535 | } |
| 536 | |
| 537 | // FoldOpIntoSelect - Given an instruction with a select as one operand and a |
| 538 | // constant as the other operand, try to fold the binary operator into the |
| 539 | // select arguments. This also works for Cast instructions, which obviously do |
| 540 | // not have a second operand. |
| 541 | static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI, |
| 542 | InstCombiner *IC) { |
| 543 | // Don't modify shared select instructions |
| 544 | if (!SI->hasOneUse()) return 0; |
| 545 | Value *TV = SI->getOperand(1); |
| 546 | Value *FV = SI->getOperand(2); |
| 547 | |
| 548 | if (isa<Constant>(TV) || isa<Constant>(FV)) { |
Chris Lattner | 374e659 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 549 | // Bool selects with constant operands can be folded to logical ops. |
| 550 | if (SI->getType() == Type::BoolTy) return 0; |
| 551 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 552 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC); |
| 553 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC); |
| 554 | |
| 555 | return new SelectInst(SI->getCondition(), SelectTrueVal, |
| 556 | SelectFalseVal); |
| 557 | } |
| 558 | return 0; |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 561 | |
| 562 | /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI |
| 563 | /// node as operand #0, see if we can fold the instruction into the PHI (which |
| 564 | /// is only possible if all operands to the PHI are constants). |
| 565 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { |
| 566 | PHINode *PN = cast<PHINode>(I.getOperand(0)); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 567 | unsigned NumPHIValues = PN->getNumIncomingValues(); |
| 568 | if (!PN->hasOneUse() || NumPHIValues == 0 || |
| 569 | !isa<Constant>(PN->getIncomingValue(0))) return 0; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 570 | |
| 571 | // Check to see if all of the operands of the PHI are constants. If not, we |
| 572 | // cannot do the transformation. |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 573 | for (unsigned i = 1; i != NumPHIValues; ++i) |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 574 | if (!isa<Constant>(PN->getIncomingValue(i))) |
| 575 | return 0; |
| 576 | |
| 577 | // Okay, we can do the transformation: create the new PHI node. |
| 578 | PHINode *NewPN = new PHINode(I.getType(), I.getName()); |
| 579 | I.setName(""); |
Chris Lattner | d8e2018 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 580 | NewPN->reserveOperandSpace(PN->getNumOperands()/2); |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 581 | InsertNewInstBefore(NewPN, *PN); |
| 582 | |
| 583 | // Next, add all of the operands to the PHI. |
| 584 | if (I.getNumOperands() == 2) { |
| 585 | Constant *C = cast<Constant>(I.getOperand(1)); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 586 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 587 | Constant *InV = cast<Constant>(PN->getIncomingValue(i)); |
| 588 | NewPN->addIncoming(ConstantExpr::get(I.getOpcode(), InV, C), |
| 589 | PN->getIncomingBlock(i)); |
| 590 | } |
| 591 | } else { |
| 592 | assert(isa<CastInst>(I) && "Unary op should be a cast!"); |
| 593 | const Type *RetTy = I.getType(); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 594 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 595 | Constant *InV = cast<Constant>(PN->getIncomingValue(i)); |
| 596 | NewPN->addIncoming(ConstantExpr::getCast(InV, RetTy), |
| 597 | PN->getIncomingBlock(i)); |
| 598 | } |
| 599 | } |
| 600 | return ReplaceInstUsesWith(I, NewPN); |
| 601 | } |
| 602 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 603 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 604 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 605 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 606 | |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 607 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 608 | // X + undef -> undef |
| 609 | if (isa<UndefValue>(RHS)) |
| 610 | return ReplaceInstUsesWith(I, RHS); |
| 611 | |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 612 | // X + 0 --> X |
| 613 | if (!I.getType()->isFloatingPoint() && // -0 + +0 = +0, so it's not a noop |
| 614 | RHSC->isNullValue()) |
| 615 | return ReplaceInstUsesWith(I, LHS); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 616 | |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 617 | // X + (signbit) --> X ^ signbit |
| 618 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 619 | unsigned NumBits = CI->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 620 | uint64_t Val = CI->getRawValue() & (1ULL << NumBits)-1; |
Chris Lattner | 33eb909 | 2004-11-05 04:45:43 +0000 | [diff] [blame] | 621 | if (Val == (1ULL << (NumBits-1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 622 | return BinaryOperator::createXor(LHS, RHS); |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 623 | } |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 624 | |
| 625 | if (isa<PHINode>(LHS)) |
| 626 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 627 | return NV; |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 628 | } |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 629 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 630 | // X + X --> X << 1 |
Robert Bocchino | 7b5b86c | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 631 | if (I.getType()->isInteger()) { |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 632 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; |
Chris Lattner | 4706046 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 633 | |
| 634 | if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) { |
| 635 | if (RHSI->getOpcode() == Instruction::Sub) |
| 636 | if (LHS == RHSI->getOperand(1)) // A + (B - A) --> B |
| 637 | return ReplaceInstUsesWith(I, RHSI->getOperand(0)); |
| 638 | } |
| 639 | if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) { |
| 640 | if (LHSI->getOpcode() == Instruction::Sub) |
| 641 | if (RHS == LHSI->getOperand(1)) // (B - A) + A --> B |
| 642 | return ReplaceInstUsesWith(I, LHSI->getOperand(0)); |
| 643 | } |
Robert Bocchino | 7b5b86c | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 644 | } |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 645 | |
Chris Lattner | 147e975 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 646 | // -A + B --> B - A |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 647 | if (Value *V = dyn_castNegVal(LHS)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 648 | return BinaryOperator::createSub(RHS, V); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 649 | |
| 650 | // A + -B --> A - B |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 651 | if (!isa<Constant>(RHS)) |
| 652 | if (Value *V = dyn_castNegVal(RHS)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 653 | return BinaryOperator::createSub(LHS, V); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 654 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 655 | |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 656 | ConstantInt *C2; |
| 657 | if (Value *X = dyn_castFoldableMul(LHS, C2)) { |
| 658 | if (X == RHS) // X*C + X --> X * (C+1) |
| 659 | return BinaryOperator::createMul(RHS, AddOne(C2)); |
| 660 | |
| 661 | // X*C1 + X*C2 --> X * (C1+C2) |
| 662 | ConstantInt *C1; |
| 663 | if (X == dyn_castFoldableMul(RHS, C1)) |
| 664 | return BinaryOperator::createMul(X, ConstantExpr::getAdd(C1, C2)); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | // X + X*C --> X * (C+1) |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 668 | if (dyn_castFoldableMul(RHS, C2) == LHS) |
| 669 | return BinaryOperator::createMul(LHS, AddOne(C2)); |
| 670 | |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 671 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 672 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 673 | if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 674 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) return R; |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 675 | |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 676 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 677 | Value *X; |
| 678 | if (match(LHS, m_Not(m_Value(X)))) { // ~X + C --> (C-1) - X |
| 679 | Constant *C= ConstantExpr::getSub(CRHS, ConstantInt::get(I.getType(), 1)); |
| 680 | return BinaryOperator::createSub(C, X); |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 681 | } |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 682 | |
Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 683 | // (X & FF00) + xx00 -> (X+xx00) & FF00 |
| 684 | if (LHS->hasOneUse() && match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) { |
| 685 | Constant *Anded = ConstantExpr::getAnd(CRHS, C2); |
| 686 | if (Anded == CRHS) { |
| 687 | // See if all bits from the first bit set in the Add RHS up are included |
| 688 | // in the mask. First, get the rightmost bit. |
| 689 | uint64_t AddRHSV = CRHS->getRawValue(); |
| 690 | |
| 691 | // Form a mask of all bits from the lowest bit added through the top. |
| 692 | uint64_t AddRHSHighBits = ~((AddRHSV & -AddRHSV)-1); |
Chris Lattner | 2f1457f | 2005-04-24 17:46:05 +0000 | [diff] [blame] | 693 | AddRHSHighBits &= ~0ULL >> (64-C2->getType()->getPrimitiveSizeInBits()); |
Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 694 | |
| 695 | // See if the and mask includes all of these bits. |
| 696 | uint64_t AddRHSHighBitsAnd = AddRHSHighBits & C2->getRawValue(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 697 | |
Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 698 | if (AddRHSHighBits == AddRHSHighBitsAnd) { |
| 699 | // Okay, the xform is safe. Insert the new add pronto. |
| 700 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, CRHS, |
| 701 | LHS->getName()), I); |
| 702 | return BinaryOperator::createAnd(NewAdd, C2); |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 707 | // Try to fold constant add into select arguments. |
| 708 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 709 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 710 | return R; |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 713 | return Changed ? &I : 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Chris Lattner | bdb0ce0 | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 716 | // isSignBit - Return true if the value represented by the constant only has the |
| 717 | // highest order bit set. |
| 718 | static bool isSignBit(ConstantInt *CI) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 719 | unsigned NumBits = CI->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 2f1457f | 2005-04-24 17:46:05 +0000 | [diff] [blame] | 720 | return (CI->getRawValue() & (~0ULL >> (64-NumBits))) == (1ULL << (NumBits-1)); |
Chris Lattner | bdb0ce0 | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 723 | /// RemoveNoopCast - Strip off nonconverting casts from the value. |
| 724 | /// |
| 725 | static Value *RemoveNoopCast(Value *V) { |
| 726 | if (CastInst *CI = dyn_cast<CastInst>(V)) { |
| 727 | const Type *CTy = CI->getType(); |
| 728 | const Type *OpTy = CI->getOperand(0)->getType(); |
| 729 | if (CTy->isInteger() && OpTy->isInteger()) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 730 | if (CTy->getPrimitiveSizeInBits() == OpTy->getPrimitiveSizeInBits()) |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 731 | return RemoveNoopCast(CI->getOperand(0)); |
| 732 | } else if (isa<PointerType>(CTy) && isa<PointerType>(OpTy)) |
| 733 | return RemoveNoopCast(CI->getOperand(0)); |
| 734 | } |
| 735 | return V; |
| 736 | } |
| 737 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 738 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 739 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 740 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 741 | if (Op0 == Op1) // sub X, X -> 0 |
| 742 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 743 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 744 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 745 | if (Value *V = dyn_castNegVal(Op1)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 746 | return BinaryOperator::createAdd(Op0, V); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 747 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 748 | if (isa<UndefValue>(Op0)) |
| 749 | return ReplaceInstUsesWith(I, Op0); // undef - X -> undef |
| 750 | if (isa<UndefValue>(Op1)) |
| 751 | return ReplaceInstUsesWith(I, Op1); // X - undef -> undef |
| 752 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 753 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
| 754 | // Replace (-1 - A) with (~A)... |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 755 | if (C->isAllOnesValue()) |
| 756 | return BinaryOperator::createNot(Op1); |
Chris Lattner | ad3c495 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 757 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 758 | // C - ~X == X + (1+C) |
Reid Spencer | 4fdd96c | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 759 | Value *X = 0; |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 760 | if (match(Op1, m_Not(m_Value(X)))) |
| 761 | return BinaryOperator::createAdd(X, |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 762 | ConstantExpr::getAdd(C, ConstantInt::get(I.getType(), 1))); |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 763 | // -((uint)X >> 31) -> ((int)X >> 31) |
| 764 | // -((int)X >> 31) -> ((uint)X >> 31) |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 765 | if (C->isNullValue()) { |
| 766 | Value *NoopCastedRHS = RemoveNoopCast(Op1); |
| 767 | if (ShiftInst *SI = dyn_cast<ShiftInst>(NoopCastedRHS)) |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 768 | if (SI->getOpcode() == Instruction::Shr) |
| 769 | if (ConstantUInt *CU = dyn_cast<ConstantUInt>(SI->getOperand(1))) { |
| 770 | const Type *NewTy; |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 771 | if (SI->getType()->isSigned()) |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 772 | NewTy = SI->getType()->getUnsignedVersion(); |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 773 | else |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 774 | NewTy = SI->getType()->getSignedVersion(); |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 775 | // 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] | 776 | if (CU->getValue() == SI->getType()->getPrimitiveSizeInBits()-1) { |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 777 | // Ok, the transformation is safe. Insert a cast of the incoming |
| 778 | // value, then the new shift, then the new cast. |
| 779 | Instruction *FirstCast = new CastInst(SI->getOperand(0), NewTy, |
| 780 | SI->getOperand(0)->getName()); |
| 781 | Value *InV = InsertNewInstBefore(FirstCast, I); |
| 782 | Instruction *NewShift = new ShiftInst(Instruction::Shr, FirstCast, |
| 783 | CU, SI->getName()); |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 784 | if (NewShift->getType() == I.getType()) |
| 785 | return NewShift; |
| 786 | else { |
| 787 | InV = InsertNewInstBefore(NewShift, I); |
| 788 | return new CastInst(NewShift, I.getType()); |
| 789 | } |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 790 | } |
| 791 | } |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 792 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 793 | |
| 794 | // Try to fold constant sub into select arguments. |
| 795 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 796 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 797 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 798 | |
| 799 | if (isa<PHINode>(Op0)) |
| 800 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 801 | return NV; |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 804 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
| 805 | if (Op1I->getOpcode() == Instruction::Add && |
| 806 | !Op0->getType()->isFloatingPoint()) { |
Chris Lattner | c7f3c1a | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 807 | if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 808 | return BinaryOperator::createNeg(Op1I->getOperand(1), I.getName()); |
Chris Lattner | c7f3c1a | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 809 | else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 810 | return BinaryOperator::createNeg(Op1I->getOperand(0), I.getName()); |
Chris Lattner | c7f3c1a | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 811 | else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) { |
| 812 | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1))) |
| 813 | // C1-(X+C2) --> (C1-C2)-X |
| 814 | return BinaryOperator::createSub(ConstantExpr::getSub(CI1, CI2), |
| 815 | Op1I->getOperand(0)); |
| 816 | } |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 819 | if (Op1I->hasOneUse()) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 820 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 821 | // is not used by anyone else... |
| 822 | // |
Chris Lattner | c2f0aa5 | 2004-02-02 20:09:56 +0000 | [diff] [blame] | 823 | if (Op1I->getOpcode() == Instruction::Sub && |
| 824 | !Op1I->getType()->isFloatingPoint()) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 825 | // Swap the two operands of the subexpr... |
| 826 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 827 | Op1I->setOperand(0, IIOp1); |
| 828 | Op1I->setOperand(1, IIOp0); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 829 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 830 | // Create the new top level add instruction... |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 831 | return BinaryOperator::createAdd(Op0, Op1); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 835 | // |
| 836 | if (Op1I->getOpcode() == Instruction::And && |
| 837 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 838 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 839 | |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 840 | Value *NewNot = |
| 841 | InsertNewInstBefore(BinaryOperator::createNot(OtherOp, "B.not"), I); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 842 | return BinaryOperator::createAnd(Op0, NewNot); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 843 | } |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 844 | |
Chris Lattner | 0aee4b7 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 845 | // -(X sdiv C) -> (X sdiv -C) |
| 846 | if (Op1I->getOpcode() == Instruction::Div) |
| 847 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0)) |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 848 | if (CSI->isNullValue()) |
Chris Lattner | 0aee4b7 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 849 | if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 850 | return BinaryOperator::createDiv(Op1I->getOperand(0), |
Chris Lattner | 0aee4b7 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 851 | ConstantExpr::getNeg(DivRHS)); |
| 852 | |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 853 | // X - X*C --> X * (1-C) |
Reid Spencer | 4fdd96c | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 854 | ConstantInt *C2 = 0; |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 855 | if (dyn_castFoldableMul(Op1I, C2) == Op0) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 856 | Constant *CP1 = |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 857 | ConstantExpr::getSub(ConstantInt::get(I.getType(), 1), C2); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 858 | return BinaryOperator::createMul(Op0, CP1); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 859 | } |
Chris Lattner | ad3c495 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 860 | } |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 861 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 862 | |
Chris Lattner | 4706046 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 863 | if (!Op0->getType()->isFloatingPoint()) |
| 864 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 865 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 866 | if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X |
| 867 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 868 | else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X |
| 869 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 4706046 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 870 | } else if (Op0I->getOpcode() == Instruction::Sub) { |
| 871 | if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y |
| 872 | return BinaryOperator::createNeg(Op0I->getOperand(1), I.getName()); |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 873 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 874 | |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 875 | ConstantInt *C1; |
| 876 | if (Value *X = dyn_castFoldableMul(Op0, C1)) { |
| 877 | if (X == Op1) { // X*C - X --> X * (C-1) |
| 878 | Constant *CP1 = ConstantExpr::getSub(C1, ConstantInt::get(I.getType(),1)); |
| 879 | return BinaryOperator::createMul(Op1, CP1); |
| 880 | } |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 881 | |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 882 | ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2) |
| 883 | if (X == dyn_castFoldableMul(Op1, C2)) |
| 884 | return BinaryOperator::createMul(Op1, ConstantExpr::getSub(C1, C2)); |
| 885 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 886 | return 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 889 | /// isSignBitCheck - Given an exploded setcc instruction, return true if it is |
| 890 | /// really just returns true if the most significant (sign) bit is set. |
| 891 | static bool isSignBitCheck(unsigned Opcode, Value *LHS, ConstantInt *RHS) { |
| 892 | if (RHS->getType()->isSigned()) { |
| 893 | // True if source is LHS < 0 or LHS <= -1 |
| 894 | return Opcode == Instruction::SetLT && RHS->isNullValue() || |
| 895 | Opcode == Instruction::SetLE && RHS->isAllOnesValue(); |
| 896 | } else { |
| 897 | ConstantUInt *RHSC = cast<ConstantUInt>(RHS); |
| 898 | // True if source is LHS > 127 or LHS >= 128, where the constants depend on |
| 899 | // the size of the integer type. |
| 900 | if (Opcode == Instruction::SetGE) |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 901 | return RHSC->getValue() == |
| 902 | 1ULL << (RHS->getType()->getPrimitiveSizeInBits()-1); |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 903 | if (Opcode == Instruction::SetGT) |
| 904 | return RHSC->getValue() == |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 905 | (1ULL << (RHS->getType()->getPrimitiveSizeInBits()-1))-1; |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 906 | } |
| 907 | return false; |
| 908 | } |
| 909 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 910 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 911 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 912 | Value *Op0 = I.getOperand(0); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 913 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 914 | if (isa<UndefValue>(I.getOperand(1))) // undef * X -> 0 |
| 915 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 916 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 917 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 918 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 919 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 920 | |
| 921 | // ((X << C1)*C2) == (X * (C2 << C1)) |
| 922 | if (ShiftInst *SI = dyn_cast<ShiftInst>(Op0)) |
| 923 | if (SI->getOpcode() == Instruction::Shl) |
| 924 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 925 | return BinaryOperator::createMul(SI->getOperand(0), |
| 926 | ConstantExpr::getShl(CI, ShOp)); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 927 | |
Chris Lattner | cce81be | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 928 | if (CI->isNullValue()) |
| 929 | return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 |
| 930 | if (CI->equalsInt(1)) // X * 1 == X |
| 931 | return ReplaceInstUsesWith(I, Op0); |
| 932 | if (CI->isAllOnesValue()) // X * -1 == 0 - X |
Chris Lattner | 35236d8 | 2003-06-25 17:09:20 +0000 | [diff] [blame] | 933 | return BinaryOperator::createNeg(Op0, I.getName()); |
Chris Lattner | 31ba129 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 934 | |
Chris Lattner | cce81be | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 935 | int64_t Val = (int64_t)cast<ConstantInt>(CI)->getRawValue(); |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 936 | if (isPowerOf2_64(Val)) { // Replace X*(2^C) with X << C |
| 937 | uint64_t C = Log2_64(Val); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 938 | return new ShiftInst(Instruction::Shl, Op0, |
| 939 | ConstantUInt::get(Type::UByteTy, C)); |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 940 | } |
Robert Bocchino | 7b5b86c | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 941 | } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 942 | if (Op1F->isNullValue()) |
| 943 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 31ba129 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 944 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 945 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 946 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) |
| 947 | if (Op1F->getValue() == 1.0) |
| 948 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
| 949 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 950 | |
| 951 | // Try to fold constant mul into select arguments. |
| 952 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 953 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 954 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 955 | |
| 956 | if (isa<PHINode>(Op0)) |
| 957 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 958 | return NV; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 959 | } |
| 960 | |
Chris Lattner | 934a64cf | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 961 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
| 962 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 963 | return BinaryOperator::createMul(Op0v, Op1v); |
Chris Lattner | 934a64cf | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 964 | |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 965 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 966 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 967 | // See if we can simplify things based on how the boolean was originally |
| 968 | // formed. |
| 969 | CastInst *BoolCast = 0; |
| 970 | if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(0))) |
| 971 | if (CI->getOperand(0)->getType() == Type::BoolTy) |
| 972 | BoolCast = CI; |
| 973 | if (!BoolCast) |
| 974 | if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(1))) |
| 975 | if (CI->getOperand(0)->getType() == Type::BoolTy) |
| 976 | BoolCast = CI; |
| 977 | if (BoolCast) { |
| 978 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(BoolCast->getOperand(0))) { |
| 979 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); |
| 980 | const Type *SCOpTy = SCIOp0->getType(); |
| 981 | |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 982 | // If the setcc is true iff the sign bit of X is set, then convert this |
| 983 | // multiply into a shift/and combination. |
| 984 | if (isa<ConstantInt>(SCIOp1) && |
| 985 | isSignBitCheck(SCI->getOpcode(), SCIOp0, cast<ConstantInt>(SCIOp1))) { |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 986 | // Shift the X value right to turn it into "all signbits". |
| 987 | Constant *Amt = ConstantUInt::get(Type::UByteTy, |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 988 | SCOpTy->getPrimitiveSizeInBits()-1); |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 989 | if (SCIOp0->getType()->isUnsigned()) { |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 990 | const Type *NewTy = SCIOp0->getType()->getSignedVersion(); |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 991 | SCIOp0 = InsertNewInstBefore(new CastInst(SCIOp0, NewTy, |
| 992 | SCIOp0->getName()), I); |
| 993 | } |
| 994 | |
| 995 | Value *V = |
| 996 | InsertNewInstBefore(new ShiftInst(Instruction::Shr, SCIOp0, Amt, |
| 997 | BoolCast->getOperand(0)->getName()+ |
| 998 | ".mask"), I); |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 999 | |
| 1000 | // If the multiply type is not the same as the source type, sign extend |
| 1001 | // or truncate to the multiply type. |
| 1002 | if (I.getType() != V->getType()) |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 1003 | V = InsertNewInstBefore(new CastInst(V, I.getType(), V->getName()),I); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1004 | |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 1005 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1006 | return BinaryOperator::createAnd(V, OtherOp); |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | } |
| 1010 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1011 | return Changed ? &I : 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1014 | Instruction *InstCombiner::visitDiv(BinaryOperator &I) { |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1015 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1016 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1017 | if (isa<UndefValue>(Op0)) // undef / X -> 0 |
| 1018 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1019 | if (isa<UndefValue>(Op1)) |
| 1020 | return ReplaceInstUsesWith(I, Op1); // X / undef -> undef |
| 1021 | |
| 1022 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | e20c334 | 2004-04-26 14:01:59 +0000 | [diff] [blame] | 1023 | // div X, 1 == X |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1024 | if (RHS->equalsInt(1)) |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1025 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1026 | |
Chris Lattner | e20c334 | 2004-04-26 14:01:59 +0000 | [diff] [blame] | 1027 | // div X, -1 == -X |
| 1028 | if (RHS->isAllOnesValue()) |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1029 | return BinaryOperator::createNeg(Op0); |
Chris Lattner | e20c334 | 2004-04-26 14:01:59 +0000 | [diff] [blame] | 1030 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1031 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 1032 | if (LHS->getOpcode() == Instruction::Div) |
| 1033 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 1034 | // (X / C1) / C2 -> X / (C1*C2) |
| 1035 | return BinaryOperator::createDiv(LHS->getOperand(0), |
| 1036 | ConstantExpr::getMul(RHS, LHSRHS)); |
| 1037 | } |
| 1038 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1039 | // Check to see if this is an unsigned division with an exact power of 2, |
| 1040 | // if so, convert to a right shift. |
| 1041 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS)) |
| 1042 | if (uint64_t Val = C->getValue()) // Don't break X / 0 |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 1043 | if (isPowerOf2_64(Val)) { |
| 1044 | uint64_t C = Log2_64(Val); |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1045 | return new ShiftInst(Instruction::Shr, Op0, |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1046 | ConstantUInt::get(Type::UByteTy, C)); |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 1047 | } |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1048 | |
Chris Lattner | 4ad0835 | 2004-10-09 02:50:40 +0000 | [diff] [blame] | 1049 | // -X/C -> X/-C |
| 1050 | if (RHS->getType()->isSigned()) |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1051 | if (Value *LHSNeg = dyn_castNegVal(Op0)) |
Chris Lattner | 4ad0835 | 2004-10-09 02:50:40 +0000 | [diff] [blame] | 1052 | return BinaryOperator::createDiv(LHSNeg, ConstantExpr::getNeg(RHS)); |
| 1053 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1054 | if (!RHS->isNullValue()) { |
| 1055 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1056 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1057 | return R; |
| 1058 | if (isa<PHINode>(Op0)) |
| 1059 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1060 | return NV; |
| 1061 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1064 | // If this is 'udiv X, (Cond ? C1, C2)' where C1&C2 are powers of two, |
| 1065 | // transform this into: '(Cond ? (udiv X, C1) : (udiv X, C2))'. |
| 1066 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 1067 | if (ConstantUInt *STO = dyn_cast<ConstantUInt>(SI->getOperand(1))) |
| 1068 | if (ConstantUInt *SFO = dyn_cast<ConstantUInt>(SI->getOperand(2))) { |
| 1069 | if (STO->getValue() == 0) { // Couldn't be this argument. |
| 1070 | I.setOperand(1, SFO); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1071 | return &I; |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1072 | } else if (SFO->getValue() == 0) { |
Chris Lattner | 89dc4f1 | 2005-06-16 04:55:52 +0000 | [diff] [blame] | 1073 | I.setOperand(1, STO); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1074 | return &I; |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Chris Lattner | 4236261 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 1077 | uint64_t TVA = STO->getValue(), FVA = SFO->getValue(); |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 1078 | if (isPowerOf2_64(TVA) && isPowerOf2_64(FVA)) { |
| 1079 | unsigned TSA = Log2_64(TVA), FSA = Log2_64(FVA); |
Chris Lattner | 4236261 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 1080 | Constant *TC = ConstantUInt::get(Type::UByteTy, TSA); |
| 1081 | Instruction *TSI = new ShiftInst(Instruction::Shr, Op0, |
| 1082 | TC, SI->getName()+".t"); |
| 1083 | TSI = InsertNewInstBefore(TSI, I); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1084 | |
Chris Lattner | 4236261 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 1085 | Constant *FC = ConstantUInt::get(Type::UByteTy, FSA); |
| 1086 | Instruction *FSI = new ShiftInst(Instruction::Shr, Op0, |
| 1087 | FC, SI->getName()+".f"); |
| 1088 | FSI = InsertNewInstBefore(FSI, I); |
| 1089 | return new SelectInst(SI->getOperand(0), TSI, FSI); |
| 1090 | } |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1091 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1092 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1093 | // 0 / X == 0, we don't need to preserve faults! |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1094 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1095 | if (LHS->equalsInt(0)) |
| 1096 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1097 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1098 | return 0; |
| 1099 | } |
| 1100 | |
| 1101 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1102 | Instruction *InstCombiner::visitRem(BinaryOperator &I) { |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1103 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 7fd5f07 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 1104 | if (I.getType()->isSigned()) |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1105 | if (Value *RHSNeg = dyn_castNegVal(Op1)) |
Chris Lattner | 98c6bdf | 2004-07-06 07:11:42 +0000 | [diff] [blame] | 1106 | if (!isa<ConstantSInt>(RHSNeg) || |
Chris Lattner | 8e72606 | 2004-08-09 21:05:48 +0000 | [diff] [blame] | 1107 | cast<ConstantSInt>(RHSNeg)->getValue() > 0) { |
Chris Lattner | 7fd5f07 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 1108 | // X % -Y -> X % Y |
| 1109 | AddUsesToWorkList(I); |
| 1110 | I.setOperand(1, RHSNeg); |
| 1111 | return &I; |
| 1112 | } |
| 1113 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1114 | if (isa<UndefValue>(Op0)) // undef % X -> 0 |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1115 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1116 | if (isa<UndefValue>(Op1)) |
| 1117 | return ReplaceInstUsesWith(I, Op1); // X % undef -> undef |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1118 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1119 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1120 | if (RHS->equalsInt(1)) // X % 1 == 0 |
| 1121 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1122 | |
| 1123 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 1124 | // if so, convert to a bitwise and. |
| 1125 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS)) |
| 1126 | 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] | 1127 | if (!(Val & (Val-1))) // Power of 2 |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1128 | return BinaryOperator::createAnd(Op0, |
| 1129 | ConstantUInt::get(I.getType(), Val-1)); |
| 1130 | |
| 1131 | if (!RHS->isNullValue()) { |
| 1132 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1133 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1134 | return R; |
| 1135 | if (isa<PHINode>(Op0)) |
| 1136 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1137 | return NV; |
| 1138 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1141 | // If this is 'urem X, (Cond ? C1, C2)' where C1&C2 are powers of two, |
| 1142 | // transform this into: '(Cond ? (urem X, C1) : (urem X, C2))'. |
| 1143 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 1144 | if (ConstantUInt *STO = dyn_cast<ConstantUInt>(SI->getOperand(1))) |
| 1145 | if (ConstantUInt *SFO = dyn_cast<ConstantUInt>(SI->getOperand(2))) { |
| 1146 | if (STO->getValue() == 0) { // Couldn't be this argument. |
| 1147 | I.setOperand(1, SFO); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1148 | return &I; |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1149 | } else if (SFO->getValue() == 0) { |
| 1150 | I.setOperand(1, STO); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1151 | return &I; |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | if (!(STO->getValue() & (STO->getValue()-1)) && |
| 1155 | !(SFO->getValue() & (SFO->getValue()-1))) { |
| 1156 | Value *TrueAnd = InsertNewInstBefore(BinaryOperator::createAnd(Op0, |
| 1157 | SubOne(STO), SI->getName()+".t"), I); |
| 1158 | Value *FalseAnd = InsertNewInstBefore(BinaryOperator::createAnd(Op0, |
| 1159 | SubOne(SFO), SI->getName()+".f"), I); |
| 1160 | return new SelectInst(SI->getOperand(0), TrueAnd, FalseAnd); |
| 1161 | } |
| 1162 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1163 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1164 | // 0 % X == 0, we don't need to preserve faults! |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1165 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1166 | if (LHS->equalsInt(0)) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1167 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1168 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1169 | return 0; |
| 1170 | } |
| 1171 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1172 | // isMaxValueMinusOne - return true if this is Max-1 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1173 | static bool isMaxValueMinusOne(const ConstantInt *C) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1174 | if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) { |
| 1175 | // Calculate -1 casted to the right type... |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1176 | unsigned TypeBits = C->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1177 | uint64_t Val = ~0ULL; // All ones |
| 1178 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 1179 | return CU->getValue() == Val-1; |
| 1180 | } |
| 1181 | |
| 1182 | const ConstantSInt *CS = cast<ConstantSInt>(C); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1183 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1184 | // Calculate 0111111111..11111 |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1185 | unsigned TypeBits = C->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1186 | int64_t Val = INT64_MAX; // All ones |
| 1187 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 1188 | return CS->getValue() == Val-1; |
| 1189 | } |
| 1190 | |
| 1191 | // isMinValuePlusOne - return true if this is Min+1 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1192 | static bool isMinValuePlusOne(const ConstantInt *C) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1193 | if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) |
| 1194 | return CU->getValue() == 1; |
| 1195 | |
| 1196 | const ConstantSInt *CS = cast<ConstantSInt>(C); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1197 | |
| 1198 | // Calculate 1111111111000000000000 |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1199 | unsigned TypeBits = C->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1200 | int64_t Val = -1; // All ones |
| 1201 | Val <<= TypeBits-1; // Shift over to the right spot |
| 1202 | return CS->getValue() == Val+1; |
| 1203 | } |
| 1204 | |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1205 | // isOneBitSet - Return true if there is exactly one bit set in the specified |
| 1206 | // constant. |
| 1207 | static bool isOneBitSet(const ConstantInt *CI) { |
| 1208 | uint64_t V = CI->getRawValue(); |
| 1209 | return V && (V & (V-1)) == 0; |
| 1210 | } |
| 1211 | |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 1212 | #if 0 // Currently unused |
| 1213 | // isLowOnes - Return true if the constant is of the form 0+1+. |
| 1214 | static bool isLowOnes(const ConstantInt *CI) { |
| 1215 | uint64_t V = CI->getRawValue(); |
| 1216 | |
| 1217 | // There won't be bits set in parts that the type doesn't contain. |
| 1218 | V &= ConstantInt::getAllOnesValue(CI->getType())->getRawValue(); |
| 1219 | |
| 1220 | uint64_t U = V+1; // If it is low ones, this should be a power of two. |
| 1221 | return U && V && (U & V) == 0; |
| 1222 | } |
| 1223 | #endif |
| 1224 | |
| 1225 | // isHighOnes - Return true if the constant is of the form 1+0+. |
| 1226 | // This is the same as lowones(~X). |
| 1227 | static bool isHighOnes(const ConstantInt *CI) { |
| 1228 | uint64_t V = ~CI->getRawValue(); |
Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 1229 | if (~V == 0) return false; // 0's does not match "1+" |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 1230 | |
| 1231 | // There won't be bits set in parts that the type doesn't contain. |
| 1232 | V &= ConstantInt::getAllOnesValue(CI->getType())->getRawValue(); |
| 1233 | |
| 1234 | uint64_t U = V+1; // If it is low ones, this should be a power of two. |
| 1235 | return U && V && (U & V) == 0; |
| 1236 | } |
| 1237 | |
| 1238 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1239 | /// getSetCondCode - Encode a setcc opcode into a three bit mask. These bits |
| 1240 | /// are carefully arranged to allow folding of expressions such as: |
| 1241 | /// |
| 1242 | /// (A < B) | (A > B) --> (A != B) |
| 1243 | /// |
| 1244 | /// Bit value '4' represents that the comparison is true if A > B, bit value '2' |
| 1245 | /// represents that the comparison is true if A == B, and bit value '1' is true |
| 1246 | /// if A < B. |
| 1247 | /// |
| 1248 | static unsigned getSetCondCode(const SetCondInst *SCI) { |
| 1249 | switch (SCI->getOpcode()) { |
| 1250 | // False -> 0 |
| 1251 | case Instruction::SetGT: return 1; |
| 1252 | case Instruction::SetEQ: return 2; |
| 1253 | case Instruction::SetGE: return 3; |
| 1254 | case Instruction::SetLT: return 4; |
| 1255 | case Instruction::SetNE: return 5; |
| 1256 | case Instruction::SetLE: return 6; |
| 1257 | // True -> 7 |
| 1258 | default: |
| 1259 | assert(0 && "Invalid SetCC opcode!"); |
| 1260 | return 0; |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | /// getSetCCValue - This is the complement of getSetCondCode, which turns an |
| 1265 | /// opcode and two operands into either a constant true or false, or a brand new |
| 1266 | /// SetCC instruction. |
| 1267 | static Value *getSetCCValue(unsigned Opcode, Value *LHS, Value *RHS) { |
| 1268 | switch (Opcode) { |
| 1269 | case 0: return ConstantBool::False; |
| 1270 | case 1: return new SetCondInst(Instruction::SetGT, LHS, RHS); |
| 1271 | case 2: return new SetCondInst(Instruction::SetEQ, LHS, RHS); |
| 1272 | case 3: return new SetCondInst(Instruction::SetGE, LHS, RHS); |
| 1273 | case 4: return new SetCondInst(Instruction::SetLT, LHS, RHS); |
| 1274 | case 5: return new SetCondInst(Instruction::SetNE, LHS, RHS); |
| 1275 | case 6: return new SetCondInst(Instruction::SetLE, LHS, RHS); |
| 1276 | case 7: return ConstantBool::True; |
| 1277 | default: assert(0 && "Illegal SetCCCode!"); return 0; |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | // FoldSetCCLogical - Implements (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B) |
| 1282 | struct FoldSetCCLogical { |
| 1283 | InstCombiner &IC; |
| 1284 | Value *LHS, *RHS; |
| 1285 | FoldSetCCLogical(InstCombiner &ic, SetCondInst *SCI) |
| 1286 | : IC(ic), LHS(SCI->getOperand(0)), RHS(SCI->getOperand(1)) {} |
| 1287 | bool shouldApply(Value *V) const { |
| 1288 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(V)) |
| 1289 | return (SCI->getOperand(0) == LHS && SCI->getOperand(1) == RHS || |
| 1290 | SCI->getOperand(0) == RHS && SCI->getOperand(1) == LHS); |
| 1291 | return false; |
| 1292 | } |
| 1293 | Instruction *apply(BinaryOperator &Log) const { |
| 1294 | SetCondInst *SCI = cast<SetCondInst>(Log.getOperand(0)); |
| 1295 | if (SCI->getOperand(0) != LHS) { |
| 1296 | assert(SCI->getOperand(1) == LHS); |
| 1297 | SCI->swapOperands(); // Swap the LHS and RHS of the SetCC |
| 1298 | } |
| 1299 | |
| 1300 | unsigned LHSCode = getSetCondCode(SCI); |
| 1301 | unsigned RHSCode = getSetCondCode(cast<SetCondInst>(Log.getOperand(1))); |
| 1302 | unsigned Code; |
| 1303 | switch (Log.getOpcode()) { |
| 1304 | case Instruction::And: Code = LHSCode & RHSCode; break; |
| 1305 | case Instruction::Or: Code = LHSCode | RHSCode; break; |
| 1306 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; |
Chris Lattner | 2caaaba | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 1307 | default: assert(0 && "Illegal logical opcode!"); return 0; |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | Value *RV = getSetCCValue(Code, LHS, RHS); |
| 1311 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 1312 | return I; |
| 1313 | // Otherwise, it's a constant boolean value... |
| 1314 | return IC.ReplaceInstUsesWith(Log, RV); |
| 1315 | } |
| 1316 | }; |
| 1317 | |
| 1318 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1319 | /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use |
| 1320 | /// this predicate to simplify operations downstream. V and Mask are known to |
| 1321 | /// be the same type. |
| 1322 | static bool MaskedValueIsZero(Value *V, ConstantIntegral *Mask) { |
Chris Lattner | 18aa4d8 | 2005-07-20 18:49:28 +0000 | [diff] [blame] | 1323 | // Note, we cannot consider 'undef' to be "IsZero" here. The problem is that |
| 1324 | // we cannot optimize based on the assumption that it is zero without changing |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1325 | // to to an explicit zero. If we don't change it to zero, other code could |
Chris Lattner | 18aa4d8 | 2005-07-20 18:49:28 +0000 | [diff] [blame] | 1326 | // optimized based on the contradictory assumption that it is non-zero. |
| 1327 | // Because instcombine aggressively folds operations with undef args anyway, |
| 1328 | // this won't lose us code quality. |
| 1329 | if (Mask->isNullValue()) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1330 | return true; |
| 1331 | if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V)) |
| 1332 | return ConstantExpr::getAnd(CI, Mask)->isNullValue(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1333 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1334 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 1335 | switch (I->getOpcode()) { |
| 1336 | case Instruction::And: |
| 1337 | // (X & C1) & C2 == 0 iff C1 & C2 == 0. |
| 1338 | if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(I->getOperand(1))) |
| 1339 | if (ConstantExpr::getAnd(CI, Mask)->isNullValue()) |
| 1340 | return true; |
| 1341 | break; |
Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1342 | case Instruction::Or: |
| 1343 | // If the LHS and the RHS are MaskedValueIsZero, the result is also zero. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1344 | return MaskedValueIsZero(I->getOperand(1), Mask) && |
Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1345 | MaskedValueIsZero(I->getOperand(0), Mask); |
| 1346 | case Instruction::Select: |
| 1347 | // If the T and F values are MaskedValueIsZero, the result is also zero. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1348 | return MaskedValueIsZero(I->getOperand(2), Mask) && |
Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1349 | MaskedValueIsZero(I->getOperand(1), Mask); |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1350 | case Instruction::Cast: { |
| 1351 | const Type *SrcTy = I->getOperand(0)->getType(); |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 1352 | if (SrcTy == Type::BoolTy) |
| 1353 | return (Mask->getRawValue() & 1) == 0; |
| 1354 | |
| 1355 | if (SrcTy->isInteger()) { |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1356 | // (cast <ty> X to int) & C2 == 0 iff <ty> could not have contained C2. |
| 1357 | if (SrcTy->isUnsigned() && // Only handle zero ext. |
| 1358 | ConstantExpr::getCast(Mask, SrcTy)->isNullValue()) |
| 1359 | return true; |
| 1360 | |
| 1361 | // If this is a noop cast, recurse. |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 1362 | if ((SrcTy->isSigned() && SrcTy->getUnsignedVersion() == I->getType())|| |
| 1363 | SrcTy->getSignedVersion() == I->getType()) { |
| 1364 | Constant *NewMask = |
| 1365 | ConstantExpr::getCast(Mask, I->getOperand(0)->getType()); |
| 1366 | return MaskedValueIsZero(I->getOperand(0), |
| 1367 | cast<ConstantIntegral>(NewMask)); |
| 1368 | } |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1369 | } |
| 1370 | break; |
| 1371 | } |
| 1372 | case Instruction::Shl: |
Chris Lattner | ef298a3 | 2005-05-06 04:53:20 +0000 | [diff] [blame] | 1373 | // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 |
| 1374 | if (ConstantUInt *SA = dyn_cast<ConstantUInt>(I->getOperand(1))) |
| 1375 | return MaskedValueIsZero(I->getOperand(0), |
| 1376 | cast<ConstantIntegral>(ConstantExpr::getUShr(Mask, SA))); |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1377 | break; |
| 1378 | case Instruction::Shr: |
| 1379 | // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
| 1380 | if (ConstantUInt *SA = dyn_cast<ConstantUInt>(I->getOperand(1))) |
| 1381 | if (I->getType()->isUnsigned()) { |
| 1382 | Constant *C1 = ConstantIntegral::getAllOnesValue(I->getType()); |
| 1383 | C1 = ConstantExpr::getShr(C1, SA); |
| 1384 | C1 = ConstantExpr::getAnd(C1, Mask); |
| 1385 | if (C1->isNullValue()) |
| 1386 | return true; |
| 1387 | } |
| 1388 | break; |
| 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | return false; |
| 1393 | } |
| 1394 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1395 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
| 1396 | // the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'. Op is |
| 1397 | // guaranteed to be either a shift instruction or a binary operator. |
| 1398 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
| 1399 | ConstantIntegral *OpRHS, |
| 1400 | ConstantIntegral *AndRHS, |
| 1401 | BinaryOperator &TheAnd) { |
| 1402 | Value *X = Op->getOperand(0); |
Chris Lattner | fcf21a7 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 1403 | Constant *Together = 0; |
| 1404 | if (!isa<ShiftInst>(Op)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1405 | Together = ConstantExpr::getAnd(AndRHS, OpRHS); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1406 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1407 | switch (Op->getOpcode()) { |
| 1408 | case Instruction::Xor: |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1409 | if (Op->hasOneUse()) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1410 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
| 1411 | std::string OpName = Op->getName(); Op->setName(""); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1412 | Instruction *And = BinaryOperator::createAnd(X, AndRHS, OpName); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1413 | InsertNewInstBefore(And, TheAnd); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1414 | return BinaryOperator::createXor(And, Together); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1415 | } |
| 1416 | break; |
| 1417 | case Instruction::Or: |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1418 | if (Together == AndRHS) // (X | C) & C --> C |
| 1419 | return ReplaceInstUsesWith(TheAnd, AndRHS); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1420 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1421 | if (Op->hasOneUse() && Together != OpRHS) { |
| 1422 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 |
| 1423 | std::string Op0Name = Op->getName(); Op->setName(""); |
| 1424 | Instruction *Or = BinaryOperator::createOr(X, Together, Op0Name); |
| 1425 | InsertNewInstBefore(Or, TheAnd); |
| 1426 | return BinaryOperator::createAnd(Or, AndRHS); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1427 | } |
| 1428 | break; |
| 1429 | case Instruction::Add: |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1430 | if (Op->hasOneUse()) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1431 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 1432 | // of the bit. First thing to check is to see if this AND is with a |
| 1433 | // single bit constant. |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1434 | uint64_t AndRHSV = cast<ConstantInt>(AndRHS)->getRawValue(); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1435 | |
| 1436 | // Clear bits that are not part of the constant. |
Chris Lattner | 2f1457f | 2005-04-24 17:46:05 +0000 | [diff] [blame] | 1437 | AndRHSV &= ~0ULL >> (64-AndRHS->getType()->getPrimitiveSizeInBits()); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1438 | |
| 1439 | // If there is only one bit set... |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1440 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1441 | // Ok, at this point, we know that we are masking the result of the |
| 1442 | // ADD down to exactly one bit. If the constant we are adding has |
| 1443 | // no bits set below this bit, then we can eliminate the ADD. |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1444 | uint64_t AddRHS = cast<ConstantInt>(OpRHS)->getRawValue(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1445 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1446 | // Check to see if any bits below the one bit set in AndRHSV are set. |
| 1447 | if ((AddRHS & (AndRHSV-1)) == 0) { |
| 1448 | // If not, the only thing that can effect the output of the AND is |
| 1449 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 1450 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 1451 | // no effect. |
| 1452 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop |
| 1453 | TheAnd.setOperand(0, X); |
| 1454 | return &TheAnd; |
| 1455 | } else { |
| 1456 | std::string Name = Op->getName(); Op->setName(""); |
| 1457 | // Pull the XOR out of the AND. |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1458 | Instruction *NewAnd = BinaryOperator::createAnd(X, AndRHS, Name); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1459 | InsertNewInstBefore(NewAnd, TheAnd); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1460 | return BinaryOperator::createXor(NewAnd, AndRHS); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1461 | } |
| 1462 | } |
| 1463 | } |
| 1464 | } |
| 1465 | break; |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1466 | |
| 1467 | case Instruction::Shl: { |
| 1468 | // We know that the AND will not produce any of the bits shifted in, so if |
| 1469 | // the anded constant includes them, clear them now! |
| 1470 | // |
| 1471 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1472 | Constant *ShlMask = ConstantExpr::getShl(AllOne, OpRHS); |
| 1473 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShlMask); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1474 | |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1475 | if (CI == ShlMask) { // Masking out bits that the shift already masks |
| 1476 | return ReplaceInstUsesWith(TheAnd, Op); // No need for the and. |
| 1477 | } else if (CI != AndRHS) { // Reducing bits set in and. |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1478 | TheAnd.setOperand(1, CI); |
| 1479 | return &TheAnd; |
| 1480 | } |
| 1481 | break; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1482 | } |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1483 | case Instruction::Shr: |
| 1484 | // We know that the AND will not produce any of the bits shifted in, so if |
| 1485 | // the anded constant includes them, clear them now! This only applies to |
| 1486 | // unsigned shifts, because a signed shr may bring in set bits! |
| 1487 | // |
| 1488 | if (AndRHS->getType()->isUnsigned()) { |
| 1489 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1490 | Constant *ShrMask = ConstantExpr::getShr(AllOne, OpRHS); |
| 1491 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask); |
| 1492 | |
| 1493 | if (CI == ShrMask) { // Masking out bits that the shift already masks. |
| 1494 | return ReplaceInstUsesWith(TheAnd, Op); |
| 1495 | } else if (CI != AndRHS) { |
| 1496 | TheAnd.setOperand(1, CI); // Reduce bits set in and cst. |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1497 | return &TheAnd; |
| 1498 | } |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1499 | } else { // Signed shr. |
| 1500 | // See if this is shifting in some sign extension, then masking it out |
| 1501 | // with an and. |
| 1502 | if (Op->hasOneUse()) { |
| 1503 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
| 1504 | Constant *ShrMask = ConstantExpr::getUShr(AllOne, OpRHS); |
| 1505 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask); |
Chris Lattner | 5c3c21e | 2004-10-22 04:53:16 +0000 | [diff] [blame] | 1506 | if (CI == AndRHS) { // Masking out bits shifted in. |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1507 | // Make the argument unsigned. |
| 1508 | Value *ShVal = Op->getOperand(0); |
| 1509 | ShVal = InsertCastBefore(ShVal, |
| 1510 | ShVal->getType()->getUnsignedVersion(), |
| 1511 | TheAnd); |
| 1512 | ShVal = InsertNewInstBefore(new ShiftInst(Instruction::Shr, ShVal, |
| 1513 | OpRHS, Op->getName()), |
| 1514 | TheAnd); |
Chris Lattner | 70c2039 | 2004-10-27 05:57:15 +0000 | [diff] [blame] | 1515 | Value *AndRHS2 = ConstantExpr::getCast(AndRHS, ShVal->getType()); |
| 1516 | ShVal = InsertNewInstBefore(BinaryOperator::createAnd(ShVal, AndRHS2, |
| 1517 | TheAnd.getName()), |
| 1518 | TheAnd); |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1519 | return new CastInst(ShVal, Op->getType()); |
| 1520 | } |
| 1521 | } |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1522 | } |
| 1523 | break; |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1524 | } |
| 1525 | return 0; |
| 1526 | } |
| 1527 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1528 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 1529 | /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is |
| 1530 | /// true, otherwise (V < Lo || V >= Hi). In pratice, we emit the more efficient |
| 1531 | /// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. IB is the location to |
| 1532 | /// insert new instructions. |
| 1533 | Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
| 1534 | bool Inside, Instruction &IB) { |
| 1535 | assert(cast<ConstantBool>(ConstantExpr::getSetLE(Lo, Hi))->getValue() && |
| 1536 | "Lo is not <= Hi in range emission code!"); |
| 1537 | if (Inside) { |
| 1538 | if (Lo == Hi) // Trivially false. |
| 1539 | return new SetCondInst(Instruction::SetNE, V, V); |
| 1540 | if (cast<ConstantIntegral>(Lo)->isMinValue()) |
| 1541 | return new SetCondInst(Instruction::SetLT, V, Hi); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1542 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 1543 | Constant *AddCST = ConstantExpr::getNeg(Lo); |
| 1544 | Instruction *Add = BinaryOperator::createAdd(V, AddCST,V->getName()+".off"); |
| 1545 | InsertNewInstBefore(Add, IB); |
| 1546 | // Convert to unsigned for the comparison. |
| 1547 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 1548 | Value *OffsetVal = InsertCastBefore(Add, UnsType, IB); |
| 1549 | AddCST = ConstantExpr::getAdd(AddCST, Hi); |
| 1550 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 1551 | return new SetCondInst(Instruction::SetLT, OffsetVal, AddCST); |
| 1552 | } |
| 1553 | |
| 1554 | if (Lo == Hi) // Trivially true. |
| 1555 | return new SetCondInst(Instruction::SetEQ, V, V); |
| 1556 | |
| 1557 | Hi = SubOne(cast<ConstantInt>(Hi)); |
| 1558 | if (cast<ConstantIntegral>(Lo)->isMinValue()) // V < 0 || V >= Hi ->'V > Hi-1' |
| 1559 | return new SetCondInst(Instruction::SetGT, V, Hi); |
| 1560 | |
| 1561 | // Emit X-Lo > Hi-Lo-1 |
| 1562 | Constant *AddCST = ConstantExpr::getNeg(Lo); |
| 1563 | Instruction *Add = BinaryOperator::createAdd(V, AddCST, V->getName()+".off"); |
| 1564 | InsertNewInstBefore(Add, IB); |
| 1565 | // Convert to unsigned for the comparison. |
| 1566 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 1567 | Value *OffsetVal = InsertCastBefore(Add, UnsType, IB); |
| 1568 | AddCST = ConstantExpr::getAdd(AddCST, Hi); |
| 1569 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 1570 | return new SetCondInst(Instruction::SetGT, OffsetVal, AddCST); |
| 1571 | } |
| 1572 | |
| 1573 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1574 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1575 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1576 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1577 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1578 | if (isa<UndefValue>(Op1)) // X & undef -> 0 |
| 1579 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1580 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1581 | // and X, X = X |
| 1582 | if (Op0 == Op1) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1583 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1584 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1585 | if (ConstantIntegral *AndRHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1586 | // and X, -1 == X |
| 1587 | if (AndRHS->isAllOnesValue()) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1588 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1589 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1590 | if (MaskedValueIsZero(Op0, AndRHS)) // LHS & RHS == 0 |
| 1591 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1592 | |
| 1593 | // If the mask is not masking out any bits, there is no reason to do the |
| 1594 | // and in the first place. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1595 | ConstantIntegral *NotAndRHS = |
Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1596 | cast<ConstantIntegral>(ConstantExpr::getNot(AndRHS)); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1597 | if (MaskedValueIsZero(Op0, NotAndRHS)) |
Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1598 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1599 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1600 | // Optimize a variety of ((val OP C1) & C2) combinations... |
| 1601 | if (isa<BinaryOperator>(Op0) || isa<ShiftInst>(Op0)) { |
| 1602 | Instruction *Op0I = cast<Instruction>(Op0); |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1603 | Value *Op0LHS = Op0I->getOperand(0); |
| 1604 | Value *Op0RHS = Op0I->getOperand(1); |
| 1605 | switch (Op0I->getOpcode()) { |
| 1606 | case Instruction::Xor: |
| 1607 | case Instruction::Or: |
| 1608 | // (X ^ V) & C2 --> (X & C2) iff (V & C2) == 0 |
| 1609 | // (X | V) & C2 --> (X & C2) iff (V & C2) == 0 |
| 1610 | if (MaskedValueIsZero(Op0LHS, AndRHS)) |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1611 | return BinaryOperator::createAnd(Op0RHS, AndRHS); |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1612 | if (MaskedValueIsZero(Op0RHS, AndRHS)) |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1613 | return BinaryOperator::createAnd(Op0LHS, AndRHS); |
Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1614 | |
| 1615 | // If the mask is only needed on one incoming arm, push it up. |
| 1616 | if (Op0I->hasOneUse()) { |
| 1617 | if (MaskedValueIsZero(Op0LHS, NotAndRHS)) { |
| 1618 | // Not masking anything out for the LHS, move to RHS. |
| 1619 | Instruction *NewRHS = BinaryOperator::createAnd(Op0RHS, AndRHS, |
| 1620 | Op0RHS->getName()+".masked"); |
| 1621 | InsertNewInstBefore(NewRHS, I); |
| 1622 | return BinaryOperator::create( |
| 1623 | cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1624 | } |
Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1625 | if (!isa<Constant>(NotAndRHS) && |
| 1626 | MaskedValueIsZero(Op0RHS, NotAndRHS)) { |
| 1627 | // Not masking anything out for the RHS, move to LHS. |
| 1628 | Instruction *NewLHS = BinaryOperator::createAnd(Op0LHS, AndRHS, |
| 1629 | Op0LHS->getName()+".masked"); |
| 1630 | InsertNewInstBefore(NewLHS, I); |
| 1631 | return BinaryOperator::create( |
| 1632 | cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS); |
| 1633 | } |
| 1634 | } |
| 1635 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1636 | break; |
| 1637 | case Instruction::And: |
| 1638 | // (X & V) & C2 --> 0 iff (V & C2) == 0 |
| 1639 | if (MaskedValueIsZero(Op0LHS, AndRHS) || |
| 1640 | MaskedValueIsZero(Op0RHS, AndRHS)) |
| 1641 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1642 | break; |
| 1643 | } |
| 1644 | |
Chris Lattner | 16464b3 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 1645 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1646 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1647 | return Res; |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1648 | } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
| 1649 | const Type *SrcTy = CI->getOperand(0)->getType(); |
| 1650 | |
Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 1651 | // If this is an integer truncation or change from signed-to-unsigned, and |
| 1652 | // if the source is an and/or with immediate, transform it. This |
| 1653 | // frequently occurs for bitfield accesses. |
| 1654 | if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) { |
| 1655 | if (SrcTy->getPrimitiveSizeInBits() >= |
| 1656 | I.getType()->getPrimitiveSizeInBits() && |
| 1657 | CastOp->getNumOperands() == 2) |
| 1658 | if (ConstantInt *AndCI =dyn_cast<ConstantInt>(CastOp->getOperand(1))) |
| 1659 | if (CastOp->getOpcode() == Instruction::And) { |
| 1660 | // Change: and (cast (and X, C1) to T), C2 |
| 1661 | // into : and (cast X to T), trunc(C1)&C2 |
| 1662 | // This will folds the two ands together, which may allow other |
| 1663 | // simplifications. |
| 1664 | Instruction *NewCast = |
| 1665 | new CastInst(CastOp->getOperand(0), I.getType(), |
| 1666 | CastOp->getName()+".shrunk"); |
| 1667 | NewCast = InsertNewInstBefore(NewCast, I); |
| 1668 | |
| 1669 | Constant *C3=ConstantExpr::getCast(AndCI, I.getType());//trunc(C1) |
| 1670 | C3 = ConstantExpr::getAnd(C3, AndRHS); // trunc(C1)&C2 |
| 1671 | return BinaryOperator::createAnd(NewCast, C3); |
| 1672 | } else if (CastOp->getOpcode() == Instruction::Or) { |
| 1673 | // Change: and (cast (or X, C1) to T), C2 |
| 1674 | // into : trunc(C1)&C2 iff trunc(C1)&C2 == C2 |
| 1675 | Constant *C3=ConstantExpr::getCast(AndCI, I.getType());//trunc(C1) |
| 1676 | if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS) // trunc(C1)&C2 |
| 1677 | return ReplaceInstUsesWith(I, AndRHS); |
| 1678 | } |
| 1679 | } |
| 1680 | |
| 1681 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1682 | // If this is an integer sign or zero extension instruction. |
| 1683 | if (SrcTy->isIntegral() && |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1684 | SrcTy->getPrimitiveSizeInBits() < |
| 1685 | CI->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1686 | |
| 1687 | if (SrcTy->isUnsigned()) { |
| 1688 | // See if this and is clearing out bits that are known to be zero |
| 1689 | // anyway (due to the zero extension). |
| 1690 | Constant *Mask = ConstantIntegral::getAllOnesValue(SrcTy); |
| 1691 | Mask = ConstantExpr::getZeroExtend(Mask, CI->getType()); |
| 1692 | Constant *Result = ConstantExpr::getAnd(Mask, AndRHS); |
| 1693 | if (Result == Mask) // The "and" isn't doing anything, remove it. |
| 1694 | return ReplaceInstUsesWith(I, CI); |
| 1695 | if (Result != AndRHS) { // Reduce the and RHS constant. |
| 1696 | I.setOperand(1, Result); |
| 1697 | return &I; |
| 1698 | } |
| 1699 | |
| 1700 | } else { |
| 1701 | if (CI->hasOneUse() && SrcTy->isInteger()) { |
| 1702 | // We can only do this if all of the sign bits brought in are masked |
| 1703 | // out. Compute this by first getting 0000011111, then inverting |
| 1704 | // it. |
| 1705 | Constant *Mask = ConstantIntegral::getAllOnesValue(SrcTy); |
| 1706 | Mask = ConstantExpr::getZeroExtend(Mask, CI->getType()); |
| 1707 | Mask = ConstantExpr::getNot(Mask); // 1's in the new bits. |
| 1708 | if (ConstantExpr::getAnd(Mask, AndRHS)->isNullValue()) { |
| 1709 | // If the and is clearing all of the sign bits, change this to a |
| 1710 | // zero extension cast. To do this, cast the cast input to |
| 1711 | // unsigned, then to the requested size. |
| 1712 | Value *CastOp = CI->getOperand(0); |
| 1713 | Instruction *NC = |
| 1714 | new CastInst(CastOp, CastOp->getType()->getUnsignedVersion(), |
| 1715 | CI->getName()+".uns"); |
| 1716 | NC = InsertNewInstBefore(NC, I); |
| 1717 | // Finally, insert a replacement for CI. |
| 1718 | NC = new CastInst(NC, CI->getType(), CI->getName()); |
| 1719 | CI->setName(""); |
| 1720 | NC = InsertNewInstBefore(NC, I); |
| 1721 | WorkList.push_back(CI); // Delete CI later. |
| 1722 | I.setOperand(0, NC); |
| 1723 | return &I; // The AND operand was modified. |
| 1724 | } |
| 1725 | } |
| 1726 | } |
| 1727 | } |
Chris Lattner | 33217db | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 1728 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1729 | |
| 1730 | // Try to fold constant and into select arguments. |
| 1731 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1732 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1733 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1734 | if (isa<PHINode>(Op0)) |
| 1735 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1736 | return NV; |
Chris Lattner | 49b47ae | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1739 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 1740 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1741 | |
Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 1742 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
| 1743 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1744 | |
Misha Brukman | 9c003d8 | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 1745 | // (~A & ~B) == (~(A | B)) - De Morgan's Law |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1746 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1747 | Instruction *Or = BinaryOperator::createOr(Op0NotVal, Op1NotVal, |
| 1748 | I.getName()+".demorgan"); |
Chris Lattner | 49b47ae | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 1749 | InsertNewInstBefore(Or, I); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1750 | return BinaryOperator::createNot(Or); |
| 1751 | } |
| 1752 | |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 1753 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(Op1)) { |
| 1754 | // (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B) |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1755 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 1756 | return R; |
| 1757 | |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 1758 | Value *LHSVal, *RHSVal; |
| 1759 | ConstantInt *LHSCst, *RHSCst; |
| 1760 | Instruction::BinaryOps LHSCC, RHSCC; |
| 1761 | if (match(Op0, m_SetCond(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 1762 | if (match(RHS, m_SetCond(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 1763 | if (LHSVal == RHSVal && // Found (X setcc C1) & (X setcc C2) |
| 1764 | // Set[GL]E X, CST is folded to Set[GL]T elsewhere. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1765 | LHSCC != Instruction::SetGE && LHSCC != Instruction::SetLE && |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 1766 | RHSCC != Instruction::SetGE && RHSCC != Instruction::SetLE) { |
| 1767 | // Ensure that the larger constant is on the RHS. |
| 1768 | Constant *Cmp = ConstantExpr::getSetGT(LHSCst, RHSCst); |
| 1769 | SetCondInst *LHS = cast<SetCondInst>(Op0); |
| 1770 | if (cast<ConstantBool>(Cmp)->getValue()) { |
| 1771 | std::swap(LHS, RHS); |
| 1772 | std::swap(LHSCst, RHSCst); |
| 1773 | std::swap(LHSCC, RHSCC); |
| 1774 | } |
| 1775 | |
| 1776 | // At this point, we know we have have two setcc instructions |
| 1777 | // comparing a value against two constants and and'ing the result |
| 1778 | // together. Because of the above check, we know that we only have |
| 1779 | // SetEQ, SetNE, SetLT, and SetGT here. We also know (from the |
| 1780 | // FoldSetCCLogical check above), that the two constants are not |
| 1781 | // equal. |
| 1782 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 1783 | |
| 1784 | switch (LHSCC) { |
| 1785 | default: assert(0 && "Unknown integer condition code!"); |
| 1786 | case Instruction::SetEQ: |
| 1787 | switch (RHSCC) { |
| 1788 | default: assert(0 && "Unknown integer condition code!"); |
| 1789 | case Instruction::SetEQ: // (X == 13 & X == 15) -> false |
| 1790 | case Instruction::SetGT: // (X == 13 & X > 15) -> false |
| 1791 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1792 | case Instruction::SetNE: // (X == 13 & X != 15) -> X == 13 |
| 1793 | case Instruction::SetLT: // (X == 13 & X < 15) -> X == 13 |
| 1794 | return ReplaceInstUsesWith(I, LHS); |
| 1795 | } |
| 1796 | case Instruction::SetNE: |
| 1797 | switch (RHSCC) { |
| 1798 | default: assert(0 && "Unknown integer condition code!"); |
| 1799 | case Instruction::SetLT: |
| 1800 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X < 14) -> X < 13 |
| 1801 | return new SetCondInst(Instruction::SetLT, LHSVal, LHSCst); |
| 1802 | break; // (X != 13 & X < 15) -> no change |
| 1803 | case Instruction::SetEQ: // (X != 13 & X == 15) -> X == 15 |
| 1804 | case Instruction::SetGT: // (X != 13 & X > 15) -> X > 15 |
| 1805 | return ReplaceInstUsesWith(I, RHS); |
| 1806 | case Instruction::SetNE: |
| 1807 | if (LHSCst == SubOne(RHSCst)) {// (X != 13 & X != 14) -> X-13 >u 1 |
| 1808 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 1809 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 1810 | LHSVal->getName()+".off"); |
| 1811 | InsertNewInstBefore(Add, I); |
| 1812 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 1813 | Value *OffsetVal = InsertCastBefore(Add, UnsType, I); |
| 1814 | AddCST = ConstantExpr::getSub(RHSCst, LHSCst); |
| 1815 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 1816 | return new SetCondInst(Instruction::SetGT, OffsetVal, AddCST); |
| 1817 | } |
| 1818 | break; // (X != 13 & X != 15) -> no change |
| 1819 | } |
| 1820 | break; |
| 1821 | case Instruction::SetLT: |
| 1822 | switch (RHSCC) { |
| 1823 | default: assert(0 && "Unknown integer condition code!"); |
| 1824 | case Instruction::SetEQ: // (X < 13 & X == 15) -> false |
| 1825 | case Instruction::SetGT: // (X < 13 & X > 15) -> false |
| 1826 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1827 | case Instruction::SetNE: // (X < 13 & X != 15) -> X < 13 |
| 1828 | case Instruction::SetLT: // (X < 13 & X < 15) -> X < 13 |
| 1829 | return ReplaceInstUsesWith(I, LHS); |
| 1830 | } |
| 1831 | case Instruction::SetGT: |
| 1832 | switch (RHSCC) { |
| 1833 | default: assert(0 && "Unknown integer condition code!"); |
| 1834 | case Instruction::SetEQ: // (X > 13 & X == 15) -> X > 13 |
| 1835 | return ReplaceInstUsesWith(I, LHS); |
| 1836 | case Instruction::SetGT: // (X > 13 & X > 15) -> X > 15 |
| 1837 | return ReplaceInstUsesWith(I, RHS); |
| 1838 | case Instruction::SetNE: |
| 1839 | if (RHSCst == AddOne(LHSCst)) // (X > 13 & X != 14) -> X > 14 |
| 1840 | return new SetCondInst(Instruction::SetGT, LHSVal, RHSCst); |
| 1841 | break; // (X > 13 & X != 15) -> no change |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 1842 | case Instruction::SetLT: // (X > 13 & X < 15) -> (X-14) <u 1 |
| 1843 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, true, I); |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 1844 | } |
| 1845 | } |
| 1846 | } |
| 1847 | } |
| 1848 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1849 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1852 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1853 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1854 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1855 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1856 | if (isa<UndefValue>(Op1)) |
| 1857 | return ReplaceInstUsesWith(I, // X | undef -> -1 |
| 1858 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 1859 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1860 | // or X, X = X or X, 0 == X |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1861 | if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) |
| 1862 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1863 | |
| 1864 | // or X, -1 == -1 |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1865 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1866 | // If X is known to only contain bits that already exist in RHS, just |
| 1867 | // replace this instruction with RHS directly. |
| 1868 | if (MaskedValueIsZero(Op0, |
| 1869 | cast<ConstantIntegral>(ConstantExpr::getNot(RHS)))) |
| 1870 | return ReplaceInstUsesWith(I, RHS); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1871 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1872 | ConstantInt *C1; Value *X; |
| 1873 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
| 1874 | 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] | 1875 | Instruction *Or = BinaryOperator::createOr(X, RHS, Op0->getName()); |
| 1876 | Op0->setName(""); |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1877 | InsertNewInstBefore(Or, I); |
| 1878 | return BinaryOperator::createAnd(Or, ConstantExpr::getOr(RHS, C1)); |
| 1879 | } |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1880 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1881 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
| 1882 | if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { |
| 1883 | std::string Op0Name = Op0->getName(); Op0->setName(""); |
| 1884 | Instruction *Or = BinaryOperator::createOr(X, RHS, Op0Name); |
| 1885 | InsertNewInstBefore(Or, I); |
| 1886 | return BinaryOperator::createXor(Or, |
| 1887 | ConstantExpr::getAnd(C1, ConstantExpr::getNot(RHS))); |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1888 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1889 | |
| 1890 | // Try to fold constant and into select arguments. |
| 1891 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1892 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1893 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1894 | if (isa<PHINode>(Op0)) |
| 1895 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1896 | return NV; |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1897 | } |
| 1898 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1899 | Value *A, *B; ConstantInt *C1, *C2; |
Chris Lattner | 4294cec | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 1900 | |
| 1901 | if (match(Op0, m_And(m_Value(A), m_Value(B)))) |
| 1902 | if (A == Op1 || B == Op1) // (A & ?) | A --> A |
| 1903 | return ReplaceInstUsesWith(I, Op1); |
| 1904 | if (match(Op1, m_And(m_Value(A), m_Value(B)))) |
| 1905 | if (A == Op0 || B == Op0) // A | (A & ?) --> A |
| 1906 | return ReplaceInstUsesWith(I, Op0); |
| 1907 | |
Chris Lattner | b62f508 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 1908 | // (X^C)|Y -> (X|Y)^C iff Y&C == 0 |
| 1909 | if (Op0->hasOneUse() && match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) && |
| 1910 | MaskedValueIsZero(Op1, C1)) { |
| 1911 | Instruction *NOr = BinaryOperator::createOr(A, Op1, Op0->getName()); |
| 1912 | Op0->setName(""); |
| 1913 | return BinaryOperator::createXor(InsertNewInstBefore(NOr, I), C1); |
| 1914 | } |
| 1915 | |
| 1916 | // Y|(X^C) -> (X|Y)^C iff Y&C == 0 |
| 1917 | if (Op1->hasOneUse() && match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) && |
| 1918 | MaskedValueIsZero(Op0, C1)) { |
| 1919 | Instruction *NOr = BinaryOperator::createOr(A, Op0, Op1->getName()); |
| 1920 | Op0->setName(""); |
| 1921 | return BinaryOperator::createXor(InsertNewInstBefore(NOr, I), C1); |
| 1922 | } |
| 1923 | |
Chris Lattner | 4294cec | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 1924 | // (A & C1)|(A & C2) == A & (C1|C2) |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1925 | if (match(Op0, m_And(m_Value(A), m_ConstantInt(C1))) && |
| 1926 | match(Op1, m_And(m_Value(B), m_ConstantInt(C2))) && A == B) |
| 1927 | return BinaryOperator::createAnd(A, ConstantExpr::getOr(C1, C2)); |
Chris Lattner | 812aab7 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 1928 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1929 | if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1 |
| 1930 | if (A == Op1) // ~A | A == -1 |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1931 | return ReplaceInstUsesWith(I, |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1932 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 1933 | } else { |
| 1934 | A = 0; |
| 1935 | } |
Chris Lattner | 4294cec | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 1936 | // Note, A is still live here! |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1937 | if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B |
| 1938 | if (Op0 == B) |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1939 | return ReplaceInstUsesWith(I, |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1940 | ConstantIntegral::getAllOnesValue(I.getType())); |
Chris Lattner | 3e327a4 | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 1941 | |
Misha Brukman | 9c003d8 | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 1942 | // (~A | ~B) == (~(A & B)) - De Morgan's Law |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1943 | if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
| 1944 | Value *And = InsertNewInstBefore(BinaryOperator::createAnd(A, B, |
| 1945 | I.getName()+".demorgan"), I); |
| 1946 | return BinaryOperator::createNot(And); |
| 1947 | } |
Chris Lattner | 3e327a4 | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 1948 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1949 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1950 | // (setcc1 A, B) | (setcc2 A, B) --> (setcc3 A, B) |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 1951 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1))) { |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1952 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 1953 | return R; |
| 1954 | |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 1955 | Value *LHSVal, *RHSVal; |
| 1956 | ConstantInt *LHSCst, *RHSCst; |
| 1957 | Instruction::BinaryOps LHSCC, RHSCC; |
| 1958 | if (match(Op0, m_SetCond(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 1959 | if (match(RHS, m_SetCond(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 1960 | if (LHSVal == RHSVal && // Found (X setcc C1) | (X setcc C2) |
| 1961 | // Set[GL]E X, CST is folded to Set[GL]T elsewhere. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1962 | LHSCC != Instruction::SetGE && LHSCC != Instruction::SetLE && |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 1963 | RHSCC != Instruction::SetGE && RHSCC != Instruction::SetLE) { |
| 1964 | // Ensure that the larger constant is on the RHS. |
| 1965 | Constant *Cmp = ConstantExpr::getSetGT(LHSCst, RHSCst); |
| 1966 | SetCondInst *LHS = cast<SetCondInst>(Op0); |
| 1967 | if (cast<ConstantBool>(Cmp)->getValue()) { |
| 1968 | std::swap(LHS, RHS); |
| 1969 | std::swap(LHSCst, RHSCst); |
| 1970 | std::swap(LHSCC, RHSCC); |
| 1971 | } |
| 1972 | |
| 1973 | // At this point, we know we have have two setcc instructions |
| 1974 | // comparing a value against two constants and or'ing the result |
| 1975 | // together. Because of the above check, we know that we only have |
| 1976 | // SetEQ, SetNE, SetLT, and SetGT here. We also know (from the |
| 1977 | // FoldSetCCLogical check above), that the two constants are not |
| 1978 | // equal. |
| 1979 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 1980 | |
| 1981 | switch (LHSCC) { |
| 1982 | default: assert(0 && "Unknown integer condition code!"); |
| 1983 | case Instruction::SetEQ: |
| 1984 | switch (RHSCC) { |
| 1985 | default: assert(0 && "Unknown integer condition code!"); |
| 1986 | case Instruction::SetEQ: |
| 1987 | if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13 <u 2 |
| 1988 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 1989 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 1990 | LHSVal->getName()+".off"); |
| 1991 | InsertNewInstBefore(Add, I); |
| 1992 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 1993 | Value *OffsetVal = InsertCastBefore(Add, UnsType, I); |
| 1994 | AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst); |
| 1995 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 1996 | return new SetCondInst(Instruction::SetLT, OffsetVal, AddCST); |
| 1997 | } |
| 1998 | break; // (X == 13 | X == 15) -> no change |
| 1999 | |
Chris Lattner | 5c21946 | 2005-04-19 06:04:18 +0000 | [diff] [blame] | 2000 | case Instruction::SetGT: // (X == 13 | X > 14) -> no change |
| 2001 | break; |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2002 | case Instruction::SetNE: // (X == 13 | X != 15) -> X != 15 |
| 2003 | case Instruction::SetLT: // (X == 13 | X < 15) -> X < 15 |
| 2004 | return ReplaceInstUsesWith(I, RHS); |
| 2005 | } |
| 2006 | break; |
| 2007 | case Instruction::SetNE: |
| 2008 | switch (RHSCC) { |
| 2009 | default: assert(0 && "Unknown integer condition code!"); |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2010 | case Instruction::SetEQ: // (X != 13 | X == 15) -> X != 13 |
| 2011 | case Instruction::SetGT: // (X != 13 | X > 15) -> X != 13 |
| 2012 | return ReplaceInstUsesWith(I, LHS); |
| 2013 | case Instruction::SetNE: // (X != 13 | X != 15) -> true |
Chris Lattner | 2ceb6ee | 2005-06-17 03:59:17 +0000 | [diff] [blame] | 2014 | case Instruction::SetLT: // (X != 13 | X < 15) -> true |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2015 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2016 | } |
| 2017 | break; |
| 2018 | case Instruction::SetLT: |
| 2019 | switch (RHSCC) { |
| 2020 | default: assert(0 && "Unknown integer condition code!"); |
| 2021 | case Instruction::SetEQ: // (X < 13 | X == 14) -> no change |
| 2022 | break; |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2023 | case Instruction::SetGT: // (X < 13 | X > 15) -> (X-13) > 2 |
| 2024 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false, I); |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2025 | case Instruction::SetNE: // (X < 13 | X != 15) -> X != 15 |
| 2026 | case Instruction::SetLT: // (X < 13 | X < 15) -> X < 15 |
| 2027 | return ReplaceInstUsesWith(I, RHS); |
| 2028 | } |
| 2029 | break; |
| 2030 | case Instruction::SetGT: |
| 2031 | switch (RHSCC) { |
| 2032 | default: assert(0 && "Unknown integer condition code!"); |
| 2033 | case Instruction::SetEQ: // (X > 13 | X == 15) -> X > 13 |
| 2034 | case Instruction::SetGT: // (X > 13 | X > 15) -> X > 13 |
| 2035 | return ReplaceInstUsesWith(I, LHS); |
| 2036 | case Instruction::SetNE: // (X > 13 | X != 15) -> true |
| 2037 | case Instruction::SetLT: // (X > 13 | X < 15) -> true |
| 2038 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2039 | } |
| 2040 | } |
| 2041 | } |
| 2042 | } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2043 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2044 | } |
| 2045 | |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 2046 | // XorSelf - Implements: X ^ X --> 0 |
| 2047 | struct XorSelf { |
| 2048 | Value *RHS; |
| 2049 | XorSelf(Value *rhs) : RHS(rhs) {} |
| 2050 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 2051 | Instruction *apply(BinaryOperator &Xor) const { |
| 2052 | return &Xor; |
| 2053 | } |
| 2054 | }; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2055 | |
| 2056 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2057 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2058 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2059 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2060 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2061 | if (isa<UndefValue>(Op1)) |
| 2062 | return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef |
| 2063 | |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 2064 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
| 2065 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { |
| 2066 | assert(Result == &I && "AssociativeOpt didn't work?"); |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2067 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 2068 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2069 | |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2070 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2071 | // xor X, 0 == X |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2072 | if (RHS->isNullValue()) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2073 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2074 | |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2075 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 2076 | // xor (setcc A, B), true = not (setcc A, B) = setncc A, B |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2077 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(Op0I)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2078 | if (RHS == ConstantBool::True && SCI->hasOneUse()) |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 2079 | return new SetCondInst(SCI->getInverseCondition(), |
| 2080 | SCI->getOperand(0), SCI->getOperand(1)); |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 2081 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2082 | // ~(c-X) == X-c-1 == X+(-c-1) |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2083 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
| 2084 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2085 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); |
| 2086 | Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2087 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2088 | return BinaryOperator::createAdd(Op0I->getOperand(1), ConstantRHS); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2089 | } |
Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 2090 | |
| 2091 | // ~(~X & Y) --> (X | ~Y) |
| 2092 | if (Op0I->getOpcode() == Instruction::And && RHS->isAllOnesValue()) { |
| 2093 | if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands(); |
| 2094 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { |
| 2095 | Instruction *NotY = |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2096 | BinaryOperator::createNot(Op0I->getOperand(1), |
Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 2097 | Op0I->getOperand(1)->getName()+".not"); |
| 2098 | InsertNewInstBefore(NotY, I); |
| 2099 | return BinaryOperator::createOr(Op0NotVal, NotY); |
| 2100 | } |
| 2101 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2102 | |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2103 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 2104 | switch (Op0I->getOpcode()) { |
| 2105 | case Instruction::Add: |
Chris Lattner | 0f68fa6 | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 2106 | // ~(X-c) --> (-c-1)-X |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2107 | if (RHS->isAllOnesValue()) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2108 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); |
| 2109 | return BinaryOperator::createSub( |
| 2110 | ConstantExpr::getSub(NegOp0CI, |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2111 | ConstantInt::get(I.getType(), 1)), |
Chris Lattner | 0f68fa6 | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 2112 | Op0I->getOperand(0)); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2113 | } |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 2114 | break; |
| 2115 | case Instruction::And: |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2116 | // (X & C1) ^ C2 --> (X & C1) | C2 iff (C1&C2) == 0 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2117 | if (ConstantExpr::getAnd(RHS, Op0CI)->isNullValue()) |
| 2118 | return BinaryOperator::createOr(Op0, RHS); |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 2119 | break; |
| 2120 | case Instruction::Or: |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2121 | // (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2122 | if (ConstantExpr::getAnd(RHS, Op0CI) == RHS) |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 2123 | return BinaryOperator::createAnd(Op0, ConstantExpr::getNot(RHS)); |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 2124 | break; |
| 2125 | default: break; |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2126 | } |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 2127 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2128 | |
| 2129 | // Try to fold constant and into select arguments. |
| 2130 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2131 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2132 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2133 | if (isa<PHINode>(Op0)) |
| 2134 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2135 | return NV; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2138 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2139 | if (X == Op1) |
| 2140 | return ReplaceInstUsesWith(I, |
| 2141 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 2142 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2143 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2144 | if (X == Op0) |
| 2145 | return ReplaceInstUsesWith(I, |
| 2146 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 2147 | |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2148 | if (Instruction *Op1I = dyn_cast<Instruction>(Op1)) |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 2149 | if (Op1I->getOpcode() == Instruction::Or) { |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2150 | if (Op1I->getOperand(0) == Op0) { // B^(B|A) == (A|B)^B |
| 2151 | cast<BinaryOperator>(Op1I)->swapOperands(); |
| 2152 | I.swapOperands(); |
| 2153 | std::swap(Op0, Op1); |
| 2154 | } else if (Op1I->getOperand(1) == Op0) { // B^(A|B) == (A|B)^B |
| 2155 | I.swapOperands(); |
| 2156 | std::swap(Op0, Op1); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2157 | } |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 2158 | } else if (Op1I->getOpcode() == Instruction::Xor) { |
| 2159 | if (Op0 == Op1I->getOperand(0)) // A^(A^B) == B |
| 2160 | return ReplaceInstUsesWith(I, Op1I->getOperand(1)); |
| 2161 | else if (Op0 == Op1I->getOperand(1)) // A^(B^A) == B |
| 2162 | return ReplaceInstUsesWith(I, Op1I->getOperand(0)); |
| 2163 | } |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2164 | |
| 2165 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2166 | if (Op0I->getOpcode() == Instruction::Or && Op0I->hasOneUse()) { |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2167 | if (Op0I->getOperand(0) == Op1) // (B|A)^B == (A|B)^B |
| 2168 | cast<BinaryOperator>(Op0I)->swapOperands(); |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2169 | if (Op0I->getOperand(1) == Op1) { // (A|B)^B == A & ~B |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 2170 | Value *NotB = InsertNewInstBefore(BinaryOperator::createNot(Op1, |
| 2171 | Op1->getName()+".not"), I); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2172 | return BinaryOperator::createAnd(Op0I->getOperand(0), NotB); |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2173 | } |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 2174 | } else if (Op0I->getOpcode() == Instruction::Xor) { |
| 2175 | if (Op1 == Op0I->getOperand(0)) // (A^B)^A == B |
| 2176 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 2177 | else if (Op1 == Op0I->getOperand(1)) // (B^A)^A == B |
| 2178 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
Chris Lattner | 7aa2d47 | 2004-08-01 19:42:59 +0000 | [diff] [blame] | 2181 | // (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2182 | Value *A, *B; ConstantInt *C1, *C2; |
| 2183 | if (match(Op0, m_And(m_Value(A), m_ConstantInt(C1))) && |
| 2184 | match(Op1, m_And(m_Value(B), m_ConstantInt(C2))) && |
Chris Lattner | 7aa2d47 | 2004-08-01 19:42:59 +0000 | [diff] [blame] | 2185 | ConstantExpr::getAnd(C1, C2)->isNullValue()) |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2186 | return BinaryOperator::createOr(Op0, Op1); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 2187 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2188 | // (setcc1 A, B) ^ (setcc2 A, B) --> (setcc3 A, B) |
| 2189 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1))) |
| 2190 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 2191 | return R; |
| 2192 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2193 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2194 | } |
| 2195 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2196 | /// MulWithOverflow - Compute Result = In1*In2, returning true if the result |
| 2197 | /// overflowed for this type. |
| 2198 | static bool MulWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
| 2199 | ConstantInt *In2) { |
| 2200 | Result = cast<ConstantInt>(ConstantExpr::getMul(In1, In2)); |
| 2201 | return !In2->isNullValue() && ConstantExpr::getDiv(Result, In2) != In1; |
| 2202 | } |
| 2203 | |
| 2204 | static bool isPositive(ConstantInt *C) { |
| 2205 | return cast<ConstantSInt>(C)->getValue() >= 0; |
| 2206 | } |
| 2207 | |
| 2208 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result |
| 2209 | /// overflowed for this type. |
| 2210 | static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
| 2211 | ConstantInt *In2) { |
| 2212 | Result = cast<ConstantInt>(ConstantExpr::getAdd(In1, In2)); |
| 2213 | |
| 2214 | if (In1->getType()->isUnsigned()) |
| 2215 | return cast<ConstantUInt>(Result)->getValue() < |
| 2216 | cast<ConstantUInt>(In1)->getValue(); |
| 2217 | if (isPositive(In1) != isPositive(In2)) |
| 2218 | return false; |
| 2219 | if (isPositive(In1)) |
| 2220 | return cast<ConstantSInt>(Result)->getValue() < |
| 2221 | cast<ConstantSInt>(In1)->getValue(); |
| 2222 | return cast<ConstantSInt>(Result)->getValue() > |
| 2223 | cast<ConstantSInt>(In1)->getValue(); |
| 2224 | } |
| 2225 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2226 | /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the |
| 2227 | /// code necessary to compute the offset from the base pointer (without adding |
| 2228 | /// in the base pointer). Return the result as a signed integer of intptr size. |
| 2229 | static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { |
| 2230 | TargetData &TD = IC.getTargetData(); |
| 2231 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 2232 | const Type *UIntPtrTy = TD.getIntPtrType(); |
| 2233 | const Type *SIntPtrTy = UIntPtrTy->getSignedVersion(); |
| 2234 | Value *Result = Constant::getNullValue(SIntPtrTy); |
| 2235 | |
| 2236 | // Build a mask for high order bits. |
| 2237 | uint64_t PtrSizeMask = ~0ULL; |
| 2238 | PtrSizeMask >>= 64-(TD.getPointerSize()*8); |
| 2239 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2240 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { |
| 2241 | Value *Op = GEP->getOperand(i); |
Chris Lattner | d35d210 | 2005-01-13 23:26:48 +0000 | [diff] [blame] | 2242 | uint64_t Size = TD.getTypeSize(GTI.getIndexedType()) & PtrSizeMask; |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2243 | Constant *Scale = ConstantExpr::getCast(ConstantUInt::get(UIntPtrTy, Size), |
| 2244 | SIntPtrTy); |
| 2245 | if (Constant *OpC = dyn_cast<Constant>(Op)) { |
| 2246 | if (!OpC->isNullValue()) { |
Chris Lattner | 4cb9fa3 | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 2247 | OpC = ConstantExpr::getCast(OpC, SIntPtrTy); |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2248 | Scale = ConstantExpr::getMul(OpC, Scale); |
| 2249 | if (Constant *RC = dyn_cast<Constant>(Result)) |
| 2250 | Result = ConstantExpr::getAdd(RC, Scale); |
| 2251 | else { |
| 2252 | // Emit an add instruction. |
| 2253 | Result = IC.InsertNewInstBefore( |
| 2254 | BinaryOperator::createAdd(Result, Scale, |
| 2255 | GEP->getName()+".offs"), I); |
| 2256 | } |
| 2257 | } |
| 2258 | } else { |
Chris Lattner | 7aa41cf | 2005-01-14 17:17:59 +0000 | [diff] [blame] | 2259 | // Convert to correct type. |
| 2260 | Op = IC.InsertNewInstBefore(new CastInst(Op, SIntPtrTy, |
| 2261 | Op->getName()+".c"), I); |
| 2262 | if (Size != 1) |
Chris Lattner | 4cb9fa3 | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 2263 | // We'll let instcombine(mul) convert this to a shl if possible. |
| 2264 | Op = IC.InsertNewInstBefore(BinaryOperator::createMul(Op, Scale, |
| 2265 | GEP->getName()+".idx"), I); |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2266 | |
| 2267 | // Emit an add instruction. |
Chris Lattner | 4cb9fa3 | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 2268 | Result = IC.InsertNewInstBefore(BinaryOperator::createAdd(Op, Result, |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2269 | GEP->getName()+".offs"), I); |
| 2270 | } |
| 2271 | } |
| 2272 | return Result; |
| 2273 | } |
| 2274 | |
| 2275 | /// FoldGEPSetCC - Fold comparisons between a GEP instruction and something |
| 2276 | /// else. At this point we know that the GEP is on the LHS of the comparison. |
| 2277 | Instruction *InstCombiner::FoldGEPSetCC(User *GEPLHS, Value *RHS, |
| 2278 | Instruction::BinaryOps Cond, |
| 2279 | Instruction &I) { |
| 2280 | assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!"); |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2281 | |
| 2282 | if (CastInst *CI = dyn_cast<CastInst>(RHS)) |
| 2283 | if (isa<PointerType>(CI->getOperand(0)->getType())) |
| 2284 | RHS = CI->getOperand(0); |
| 2285 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2286 | Value *PtrBase = GEPLHS->getOperand(0); |
| 2287 | if (PtrBase == RHS) { |
| 2288 | // As an optimization, we don't actually have to compute the actual value of |
| 2289 | // OFFSET if this is a seteq or setne comparison, just return whether each |
| 2290 | // index is zero or not. |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2291 | if (Cond == Instruction::SetEQ || Cond == Instruction::SetNE) { |
| 2292 | Instruction *InVal = 0; |
Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 2293 | gep_type_iterator GTI = gep_type_begin(GEPLHS); |
| 2294 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i, ++GTI) { |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2295 | bool EmitIt = true; |
| 2296 | if (Constant *C = dyn_cast<Constant>(GEPLHS->getOperand(i))) { |
| 2297 | if (isa<UndefValue>(C)) // undef index -> undef. |
| 2298 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); |
| 2299 | if (C->isNullValue()) |
| 2300 | EmitIt = false; |
Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 2301 | else if (TD->getTypeSize(GTI.getIndexedType()) == 0) { |
| 2302 | EmitIt = false; // This is indexing into a zero sized array? |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2303 | } else if (isa<ConstantInt>(C)) |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2304 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
| 2305 | ConstantBool::get(Cond == Instruction::SetNE)); |
| 2306 | } |
| 2307 | |
| 2308 | if (EmitIt) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2309 | Instruction *Comp = |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2310 | new SetCondInst(Cond, GEPLHS->getOperand(i), |
| 2311 | Constant::getNullValue(GEPLHS->getOperand(i)->getType())); |
| 2312 | if (InVal == 0) |
| 2313 | InVal = Comp; |
| 2314 | else { |
| 2315 | InVal = InsertNewInstBefore(InVal, I); |
| 2316 | InsertNewInstBefore(Comp, I); |
| 2317 | if (Cond == Instruction::SetNE) // True if any are unequal |
| 2318 | InVal = BinaryOperator::createOr(InVal, Comp); |
| 2319 | else // True if all are equal |
| 2320 | InVal = BinaryOperator::createAnd(InVal, Comp); |
| 2321 | } |
| 2322 | } |
| 2323 | } |
| 2324 | |
| 2325 | if (InVal) |
| 2326 | return InVal; |
| 2327 | else |
| 2328 | ReplaceInstUsesWith(I, // No comparison is needed here, all indexes = 0 |
| 2329 | ConstantBool::get(Cond == Instruction::SetEQ)); |
| 2330 | } |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2331 | |
| 2332 | // Only lower this if the setcc is the only user of the GEP or if we expect |
| 2333 | // the result to fold to a constant! |
| 2334 | if (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) { |
| 2335 | // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). |
| 2336 | Value *Offset = EmitGEPOffset(GEPLHS, I, *this); |
| 2337 | return new SetCondInst(Cond, Offset, |
| 2338 | Constant::getNullValue(Offset->getType())); |
| 2339 | } |
| 2340 | } else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) { |
Chris Lattner | a21bf8d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 2341 | // If the base pointers are different, but the indices are the same, just |
| 2342 | // compare the base pointer. |
| 2343 | if (PtrBase != GEPRHS->getOperand(0)) { |
| 2344 | bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands(); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2345 | IndicesTheSame &= GEPLHS->getOperand(0)->getType() == |
Chris Lattner | bd43b9d | 2005-04-26 14:40:41 +0000 | [diff] [blame] | 2346 | GEPRHS->getOperand(0)->getType(); |
Chris Lattner | a21bf8d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 2347 | if (IndicesTheSame) |
| 2348 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 2349 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 2350 | IndicesTheSame = false; |
| 2351 | break; |
| 2352 | } |
| 2353 | |
| 2354 | // If all indices are the same, just compare the base pointers. |
| 2355 | if (IndicesTheSame) |
| 2356 | return new SetCondInst(Cond, GEPLHS->getOperand(0), |
| 2357 | GEPRHS->getOperand(0)); |
| 2358 | |
| 2359 | // Otherwise, the base pointers are different and the indices are |
| 2360 | // different, bail out. |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2361 | return 0; |
Chris Lattner | a21bf8d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 2362 | } |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2363 | |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2364 | // If one of the GEPs has all zero indices, recurse. |
| 2365 | bool AllZeros = true; |
| 2366 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 2367 | if (!isa<Constant>(GEPLHS->getOperand(i)) || |
| 2368 | !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) { |
| 2369 | AllZeros = false; |
| 2370 | break; |
| 2371 | } |
| 2372 | if (AllZeros) |
| 2373 | return FoldGEPSetCC(GEPRHS, GEPLHS->getOperand(0), |
| 2374 | SetCondInst::getSwappedCondition(Cond), I); |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 2375 | |
| 2376 | // If the other GEP has all zero indices, recurse. |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2377 | AllZeros = true; |
| 2378 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 2379 | if (!isa<Constant>(GEPRHS->getOperand(i)) || |
| 2380 | !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) { |
| 2381 | AllZeros = false; |
| 2382 | break; |
| 2383 | } |
| 2384 | if (AllZeros) |
| 2385 | return FoldGEPSetCC(GEPLHS, GEPRHS->getOperand(0), Cond, I); |
| 2386 | |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 2387 | if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) { |
| 2388 | // If the GEPs only differ by one index, compare it. |
| 2389 | unsigned NumDifferences = 0; // Keep track of # differences. |
| 2390 | unsigned DiffOperand = 0; // The operand that differs. |
| 2391 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 2392 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2393 | if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() != |
| 2394 | GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | fc4429e | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 2395 | // Irreconcilable differences. |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 2396 | NumDifferences = 2; |
| 2397 | break; |
| 2398 | } else { |
| 2399 | if (NumDifferences++) break; |
| 2400 | DiffOperand = i; |
| 2401 | } |
| 2402 | } |
| 2403 | |
| 2404 | if (NumDifferences == 0) // SAME GEP? |
| 2405 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
| 2406 | ConstantBool::get(Cond == Instruction::SetEQ)); |
| 2407 | else if (NumDifferences == 1) { |
Chris Lattner | fc4429e | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 2408 | Value *LHSV = GEPLHS->getOperand(DiffOperand); |
| 2409 | Value *RHSV = GEPRHS->getOperand(DiffOperand); |
Chris Lattner | 247aef8 | 2005-07-18 23:07:33 +0000 | [diff] [blame] | 2410 | |
| 2411 | // Convert the operands to signed values to make sure to perform a |
| 2412 | // signed comparison. |
| 2413 | const Type *NewTy = LHSV->getType()->getSignedVersion(); |
| 2414 | if (LHSV->getType() != NewTy) |
| 2415 | LHSV = InsertNewInstBefore(new CastInst(LHSV, NewTy, |
| 2416 | LHSV->getName()), I); |
| 2417 | if (RHSV->getType() != NewTy) |
| 2418 | RHSV = InsertNewInstBefore(new CastInst(RHSV, NewTy, |
| 2419 | RHSV->getName()), I); |
| 2420 | return new SetCondInst(Cond, LHSV, RHSV); |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 2421 | } |
| 2422 | } |
| 2423 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2424 | // Only lower this if the setcc is the only user of the GEP or if we expect |
| 2425 | // the result to fold to a constant! |
| 2426 | if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) && |
| 2427 | (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) { |
| 2428 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) |
| 2429 | Value *L = EmitGEPOffset(GEPLHS, I, *this); |
| 2430 | Value *R = EmitGEPOffset(GEPRHS, I, *this); |
| 2431 | return new SetCondInst(Cond, L, R); |
| 2432 | } |
| 2433 | } |
| 2434 | return 0; |
| 2435 | } |
| 2436 | |
| 2437 | |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2438 | Instruction *InstCombiner::visitSetCondInst(SetCondInst &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2439 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2440 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2441 | const Type *Ty = Op0->getType(); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2442 | |
| 2443 | // setcc X, X |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2444 | if (Op0 == Op1) |
| 2445 | return ReplaceInstUsesWith(I, ConstantBool::get(isTrueWhenEqual(I))); |
Chris Lattner | 1fc23f3 | 2002-05-09 20:11:54 +0000 | [diff] [blame] | 2446 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2447 | if (isa<UndefValue>(Op1)) // X setcc undef -> undef |
| 2448 | return ReplaceInstUsesWith(I, UndefValue::get(Type::BoolTy)); |
| 2449 | |
Chris Lattner | 15ff1e1 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 2450 | // setcc <global/alloca*/null>, <global/alloca*/null> - Global/Stack value |
| 2451 | // addresses never equal each other! We already know that Op0 != Op1. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2452 | if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) || |
| 2453 | isa<ConstantPointerNull>(Op0)) && |
| 2454 | (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) || |
Chris Lattner | 15ff1e1 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 2455 | isa<ConstantPointerNull>(Op1))) |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2456 | return ReplaceInstUsesWith(I, ConstantBool::get(!isTrueWhenEqual(I))); |
| 2457 | |
| 2458 | // setcc's with boolean values can always be turned into bitwise operations |
| 2459 | if (Ty == Type::BoolTy) { |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 2460 | switch (I.getOpcode()) { |
| 2461 | default: assert(0 && "Invalid setcc instruction!"); |
| 2462 | case Instruction::SetEQ: { // seteq bool %A, %B -> ~(A^B) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2463 | Instruction *Xor = BinaryOperator::createXor(Op0, Op1, I.getName()+"tmp"); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2464 | InsertNewInstBefore(Xor, I); |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 2465 | return BinaryOperator::createNot(Xor); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2466 | } |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 2467 | case Instruction::SetNE: |
| 2468 | return BinaryOperator::createXor(Op0, Op1); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2469 | |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 2470 | case Instruction::SetGT: |
| 2471 | std::swap(Op0, Op1); // Change setgt -> setlt |
| 2472 | // FALL THROUGH |
| 2473 | case Instruction::SetLT: { // setlt bool A, B -> ~X & Y |
| 2474 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 2475 | InsertNewInstBefore(Not, I); |
| 2476 | return BinaryOperator::createAnd(Not, Op1); |
| 2477 | } |
| 2478 | case Instruction::SetGE: |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2479 | std::swap(Op0, Op1); // Change setge -> setle |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 2480 | // FALL THROUGH |
| 2481 | case Instruction::SetLE: { // setle bool %A, %B -> ~A | B |
| 2482 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 2483 | InsertNewInstBefore(Not, I); |
| 2484 | return BinaryOperator::createOr(Not, Op1); |
| 2485 | } |
| 2486 | } |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
Chris Lattner | 2dd0174 | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 2489 | // See if we are doing a comparison between a constant and an instruction that |
| 2490 | // can be folded into the comparison. |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2491 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2492 | // Check to see if we are comparing against the minimum or maximum value... |
| 2493 | if (CI->isMinValue()) { |
| 2494 | if (I.getOpcode() == Instruction::SetLT) // A < MIN -> FALSE |
| 2495 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2496 | if (I.getOpcode() == Instruction::SetGE) // A >= MIN -> TRUE |
| 2497 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2498 | if (I.getOpcode() == Instruction::SetLE) // A <= MIN -> A == MIN |
| 2499 | return BinaryOperator::createSetEQ(Op0, Op1); |
| 2500 | if (I.getOpcode() == Instruction::SetGT) // A > MIN -> A != MIN |
| 2501 | return BinaryOperator::createSetNE(Op0, Op1); |
| 2502 | |
| 2503 | } else if (CI->isMaxValue()) { |
| 2504 | if (I.getOpcode() == Instruction::SetGT) // A > MAX -> FALSE |
| 2505 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2506 | if (I.getOpcode() == Instruction::SetLE) // A <= MAX -> TRUE |
| 2507 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2508 | if (I.getOpcode() == Instruction::SetGE) // A >= MAX -> A == MAX |
| 2509 | return BinaryOperator::createSetEQ(Op0, Op1); |
| 2510 | if (I.getOpcode() == Instruction::SetLT) // A < MAX -> A != MAX |
| 2511 | return BinaryOperator::createSetNE(Op0, Op1); |
| 2512 | |
| 2513 | // Comparing against a value really close to min or max? |
| 2514 | } else if (isMinValuePlusOne(CI)) { |
| 2515 | if (I.getOpcode() == Instruction::SetLT) // A < MIN+1 -> A == MIN |
| 2516 | return BinaryOperator::createSetEQ(Op0, SubOne(CI)); |
| 2517 | if (I.getOpcode() == Instruction::SetGE) // A >= MIN-1 -> A != MIN |
| 2518 | return BinaryOperator::createSetNE(Op0, SubOne(CI)); |
| 2519 | |
| 2520 | } else if (isMaxValueMinusOne(CI)) { |
| 2521 | if (I.getOpcode() == Instruction::SetGT) // A > MAX-1 -> A == MAX |
| 2522 | return BinaryOperator::createSetEQ(Op0, AddOne(CI)); |
| 2523 | if (I.getOpcode() == Instruction::SetLE) // A <= MAX-1 -> A != MAX |
| 2524 | return BinaryOperator::createSetNE(Op0, AddOne(CI)); |
| 2525 | } |
| 2526 | |
| 2527 | // If we still have a setle or setge instruction, turn it into the |
| 2528 | // appropriate setlt or setgt instruction. Since the border cases have |
| 2529 | // already been handled above, this requires little checking. |
| 2530 | // |
| 2531 | if (I.getOpcode() == Instruction::SetLE) |
| 2532 | return BinaryOperator::createSetLT(Op0, AddOne(CI)); |
| 2533 | if (I.getOpcode() == Instruction::SetGE) |
| 2534 | return BinaryOperator::createSetGT(Op0, SubOne(CI)); |
| 2535 | |
Chris Lattner | e1e10e1 | 2004-05-25 06:32:08 +0000 | [diff] [blame] | 2536 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2537 | switch (LHSI->getOpcode()) { |
| 2538 | case Instruction::And: |
| 2539 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && |
| 2540 | LHSI->getOperand(0)->hasOneUse()) { |
| 2541 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare |
| 2542 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This |
| 2543 | // happens a LOT in code produced by the C front-end, for bitfield |
| 2544 | // access. |
| 2545 | ShiftInst *Shift = dyn_cast<ShiftInst>(LHSI->getOperand(0)); |
| 2546 | ConstantUInt *ShAmt; |
| 2547 | ShAmt = Shift ? dyn_cast<ConstantUInt>(Shift->getOperand(1)) : 0; |
| 2548 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); |
| 2549 | const Type *Ty = LHSI->getType(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2550 | |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2551 | // We can fold this as long as we can't shift unknown bits |
| 2552 | // into the mask. This can only happen with signed shift |
| 2553 | // rights, as they sign-extend. |
| 2554 | if (ShAmt) { |
| 2555 | bool CanFold = Shift->getOpcode() != Instruction::Shr || |
Chris Lattner | 6afc02f | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 2556 | Shift->getType()->isUnsigned(); |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2557 | if (!CanFold) { |
| 2558 | // To test for the bad case of the signed shr, see if any |
| 2559 | // of the bits shifted in could be tested after the mask. |
Chris Lattner | c53cb9d | 2005-06-17 01:29:28 +0000 | [diff] [blame] | 2560 | int ShAmtVal = Ty->getPrimitiveSizeInBits()-ShAmt->getValue(); |
| 2561 | if (ShAmtVal < 0) ShAmtVal = 0; // Out of range shift. |
| 2562 | |
| 2563 | Constant *OShAmt = ConstantUInt::get(Type::UByteTy, ShAmtVal); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2564 | Constant *ShVal = |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2565 | ConstantExpr::getShl(ConstantInt::getAllOnesValue(Ty), OShAmt); |
| 2566 | if (ConstantExpr::getAnd(ShVal, AndCST)->isNullValue()) |
| 2567 | CanFold = true; |
| 2568 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2569 | |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2570 | if (CanFold) { |
Chris Lattner | 6afc02f | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 2571 | Constant *NewCst; |
| 2572 | if (Shift->getOpcode() == Instruction::Shl) |
| 2573 | NewCst = ConstantExpr::getUShr(CI, ShAmt); |
| 2574 | else |
| 2575 | NewCst = ConstantExpr::getShl(CI, ShAmt); |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2576 | |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2577 | // Check to see if we are shifting out any of the bits being |
| 2578 | // compared. |
| 2579 | if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != CI){ |
| 2580 | // If we shifted bits out, the fold is not going to work out. |
| 2581 | // As a special case, check to see if this means that the |
| 2582 | // result is always true or false now. |
| 2583 | if (I.getOpcode() == Instruction::SetEQ) |
| 2584 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2585 | if (I.getOpcode() == Instruction::SetNE) |
| 2586 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2587 | } else { |
| 2588 | I.setOperand(1, NewCst); |
Chris Lattner | 6afc02f | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 2589 | Constant *NewAndCST; |
| 2590 | if (Shift->getOpcode() == Instruction::Shl) |
| 2591 | NewAndCST = ConstantExpr::getUShr(AndCST, ShAmt); |
| 2592 | else |
| 2593 | NewAndCST = ConstantExpr::getShl(AndCST, ShAmt); |
| 2594 | LHSI->setOperand(1, NewAndCST); |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2595 | LHSI->setOperand(0, Shift->getOperand(0)); |
| 2596 | WorkList.push_back(Shift); // Shift is dead. |
| 2597 | AddUsesToWorkList(I); |
| 2598 | return &I; |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 2599 | } |
| 2600 | } |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2601 | } |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2602 | } |
| 2603 | break; |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2604 | |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2605 | case Instruction::Shl: // (setcc (shl X, ShAmt), CI) |
| 2606 | if (ConstantUInt *ShAmt = dyn_cast<ConstantUInt>(LHSI->getOperand(1))) { |
| 2607 | switch (I.getOpcode()) { |
| 2608 | default: break; |
| 2609 | case Instruction::SetEQ: |
| 2610 | case Instruction::SetNE: { |
Chris Lattner | 19b57f5 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 2611 | unsigned TypeBits = CI->getType()->getPrimitiveSizeInBits(); |
| 2612 | |
| 2613 | // Check that the shift amount is in range. If not, don't perform |
| 2614 | // undefined shifts. When the shift is visited it will be |
| 2615 | // simplified. |
| 2616 | if (ShAmt->getValue() >= TypeBits) |
| 2617 | break; |
| 2618 | |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2619 | // If we are comparing against bits always shifted out, the |
| 2620 | // comparison cannot succeed. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2621 | Constant *Comp = |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2622 | ConstantExpr::getShl(ConstantExpr::getShr(CI, ShAmt), ShAmt); |
| 2623 | if (Comp != CI) {// Comparing against a bit that we know is zero. |
| 2624 | bool IsSetNE = I.getOpcode() == Instruction::SetNE; |
| 2625 | Constant *Cst = ConstantBool::get(IsSetNE); |
| 2626 | return ReplaceInstUsesWith(I, Cst); |
| 2627 | } |
| 2628 | |
| 2629 | if (LHSI->hasOneUse()) { |
| 2630 | // Otherwise strength reduce the shift into an and. |
Chris Lattner | fdfe3e49 | 2005-01-08 19:42:22 +0000 | [diff] [blame] | 2631 | unsigned ShAmtVal = (unsigned)ShAmt->getValue(); |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2632 | uint64_t Val = (1ULL << (TypeBits-ShAmtVal))-1; |
| 2633 | |
| 2634 | Constant *Mask; |
| 2635 | if (CI->getType()->isUnsigned()) { |
| 2636 | Mask = ConstantUInt::get(CI->getType(), Val); |
| 2637 | } else if (ShAmtVal != 0) { |
| 2638 | Mask = ConstantSInt::get(CI->getType(), Val); |
| 2639 | } else { |
| 2640 | Mask = ConstantInt::getAllOnesValue(CI->getType()); |
| 2641 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2642 | |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2643 | Instruction *AndI = |
| 2644 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 2645 | Mask, LHSI->getName()+".mask"); |
| 2646 | Value *And = InsertNewInstBefore(AndI, I); |
| 2647 | return new SetCondInst(I.getOpcode(), And, |
| 2648 | ConstantExpr::getUShr(CI, ShAmt)); |
| 2649 | } |
| 2650 | } |
| 2651 | } |
| 2652 | } |
| 2653 | break; |
| 2654 | |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2655 | case Instruction::Shr: // (setcc (shr X, ShAmt), CI) |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2656 | if (ConstantUInt *ShAmt = dyn_cast<ConstantUInt>(LHSI->getOperand(1))) { |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2657 | switch (I.getOpcode()) { |
| 2658 | default: break; |
| 2659 | case Instruction::SetEQ: |
| 2660 | case Instruction::SetNE: { |
Chris Lattner | 19b57f5 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 2661 | |
| 2662 | // Check that the shift amount is in range. If not, don't perform |
| 2663 | // undefined shifts. When the shift is visited it will be |
| 2664 | // simplified. |
Chris Lattner | 104002b | 2005-06-16 01:52:07 +0000 | [diff] [blame] | 2665 | unsigned TypeBits = CI->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 19b57f5 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 2666 | if (ShAmt->getValue() >= TypeBits) |
| 2667 | break; |
| 2668 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2669 | // If we are comparing against bits always shifted out, the |
| 2670 | // comparison cannot succeed. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2671 | Constant *Comp = |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2672 | ConstantExpr::getShr(ConstantExpr::getShl(CI, ShAmt), ShAmt); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2673 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2674 | if (Comp != CI) {// Comparing against a bit that we know is zero. |
| 2675 | bool IsSetNE = I.getOpcode() == Instruction::SetNE; |
| 2676 | Constant *Cst = ConstantBool::get(IsSetNE); |
| 2677 | return ReplaceInstUsesWith(I, Cst); |
| 2678 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2679 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2680 | if (LHSI->hasOneUse() || CI->isNullValue()) { |
Chris Lattner | fdfe3e49 | 2005-01-08 19:42:22 +0000 | [diff] [blame] | 2681 | unsigned ShAmtVal = (unsigned)ShAmt->getValue(); |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2682 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2683 | // Otherwise strength reduce the shift into an and. |
| 2684 | uint64_t Val = ~0ULL; // All ones. |
| 2685 | Val <<= ShAmtVal; // Shift over to the right spot. |
| 2686 | |
| 2687 | Constant *Mask; |
| 2688 | if (CI->getType()->isUnsigned()) { |
Chris Lattner | 2f1457f | 2005-04-24 17:46:05 +0000 | [diff] [blame] | 2689 | Val &= ~0ULL >> (64-TypeBits); |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2690 | Mask = ConstantUInt::get(CI->getType(), Val); |
| 2691 | } else { |
| 2692 | Mask = ConstantSInt::get(CI->getType(), Val); |
| 2693 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2694 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2695 | Instruction *AndI = |
| 2696 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 2697 | Mask, LHSI->getName()+".mask"); |
| 2698 | Value *And = InsertNewInstBefore(AndI, I); |
| 2699 | return new SetCondInst(I.getOpcode(), And, |
| 2700 | ConstantExpr::getShl(CI, ShAmt)); |
| 2701 | } |
| 2702 | break; |
| 2703 | } |
| 2704 | } |
| 2705 | } |
| 2706 | break; |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2707 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2708 | case Instruction::Div: |
| 2709 | // Fold: (div X, C1) op C2 -> range check |
| 2710 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
| 2711 | // Fold this div into the comparison, producing a range check. |
| 2712 | // Determine, based on the divide type, what the range is being |
| 2713 | // checked. If there is an overflow on the low or high side, remember |
| 2714 | // it, otherwise compute the range [low, hi) bounding the new value. |
| 2715 | bool LoOverflow = false, HiOverflow = 0; |
| 2716 | ConstantInt *LoBound = 0, *HiBound = 0; |
| 2717 | |
| 2718 | ConstantInt *Prod; |
| 2719 | bool ProdOV = MulWithOverflow(Prod, CI, DivRHS); |
| 2720 | |
Chris Lattner | a92af96 | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 2721 | Instruction::BinaryOps Opcode = I.getOpcode(); |
| 2722 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2723 | if (DivRHS->isNullValue()) { // Don't hack on divide by zeros. |
| 2724 | } else if (LHSI->getType()->isUnsigned()) { // udiv |
| 2725 | LoBound = Prod; |
| 2726 | LoOverflow = ProdOV; |
| 2727 | HiOverflow = ProdOV || AddWithOverflow(HiBound, LoBound, DivRHS); |
| 2728 | } else if (isPositive(DivRHS)) { // Divisor is > 0. |
| 2729 | if (CI->isNullValue()) { // (X / pos) op 0 |
| 2730 | // Can't overflow. |
| 2731 | LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS))); |
| 2732 | HiBound = DivRHS; |
| 2733 | } else if (isPositive(CI)) { // (X / pos) op pos |
| 2734 | LoBound = Prod; |
| 2735 | LoOverflow = ProdOV; |
| 2736 | HiOverflow = ProdOV || AddWithOverflow(HiBound, Prod, DivRHS); |
| 2737 | } else { // (X / pos) op neg |
| 2738 | Constant *DivRHSH = ConstantExpr::getNeg(SubOne(DivRHS)); |
| 2739 | LoOverflow = AddWithOverflow(LoBound, Prod, |
| 2740 | cast<ConstantInt>(DivRHSH)); |
| 2741 | HiBound = Prod; |
| 2742 | HiOverflow = ProdOV; |
| 2743 | } |
| 2744 | } else { // Divisor is < 0. |
| 2745 | if (CI->isNullValue()) { // (X / neg) op 0 |
| 2746 | LoBound = AddOne(DivRHS); |
| 2747 | HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS)); |
Chris Lattner | 73bcba5 | 2005-06-17 02:05:55 +0000 | [diff] [blame] | 2748 | if (HiBound == DivRHS) |
| 2749 | LoBound = 0; // - INTMIN = INTMIN |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2750 | } else if (isPositive(CI)) { // (X / neg) op pos |
| 2751 | HiOverflow = LoOverflow = ProdOV; |
| 2752 | if (!LoOverflow) |
| 2753 | LoOverflow = AddWithOverflow(LoBound, Prod, AddOne(DivRHS)); |
| 2754 | HiBound = AddOne(Prod); |
| 2755 | } else { // (X / neg) op neg |
| 2756 | LoBound = Prod; |
| 2757 | LoOverflow = HiOverflow = ProdOV; |
| 2758 | HiBound = cast<ConstantInt>(ConstantExpr::getSub(Prod, DivRHS)); |
| 2759 | } |
Chris Lattner | 0b41e86 | 2004-10-08 19:15:44 +0000 | [diff] [blame] | 2760 | |
Chris Lattner | a92af96 | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 2761 | // Dividing by a negate swaps the condition. |
| 2762 | Opcode = SetCondInst::getSwappedCondition(Opcode); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2763 | } |
| 2764 | |
| 2765 | if (LoBound) { |
| 2766 | Value *X = LHSI->getOperand(0); |
Chris Lattner | a92af96 | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 2767 | switch (Opcode) { |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2768 | default: assert(0 && "Unhandled setcc opcode!"); |
| 2769 | case Instruction::SetEQ: |
| 2770 | if (LoOverflow && HiOverflow) |
| 2771 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2772 | else if (HiOverflow) |
| 2773 | return new SetCondInst(Instruction::SetGE, X, LoBound); |
| 2774 | else if (LoOverflow) |
| 2775 | return new SetCondInst(Instruction::SetLT, X, HiBound); |
| 2776 | else |
| 2777 | return InsertRangeTest(X, LoBound, HiBound, true, I); |
| 2778 | case Instruction::SetNE: |
| 2779 | if (LoOverflow && HiOverflow) |
| 2780 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2781 | else if (HiOverflow) |
| 2782 | return new SetCondInst(Instruction::SetLT, X, LoBound); |
| 2783 | else if (LoOverflow) |
| 2784 | return new SetCondInst(Instruction::SetGE, X, HiBound); |
| 2785 | else |
| 2786 | return InsertRangeTest(X, LoBound, HiBound, false, I); |
| 2787 | case Instruction::SetLT: |
| 2788 | if (LoOverflow) |
| 2789 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2790 | return new SetCondInst(Instruction::SetLT, X, LoBound); |
| 2791 | case Instruction::SetGT: |
| 2792 | if (HiOverflow) |
| 2793 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2794 | return new SetCondInst(Instruction::SetGE, X, HiBound); |
| 2795 | } |
| 2796 | } |
| 2797 | } |
| 2798 | break; |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2799 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2800 | |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 2801 | // Simplify seteq and setne instructions... |
| 2802 | if (I.getOpcode() == Instruction::SetEQ || |
| 2803 | I.getOpcode() == Instruction::SetNE) { |
| 2804 | bool isSetNE = I.getOpcode() == Instruction::SetNE; |
| 2805 | |
Chris Lattner | cfbce7c | 2003-07-23 17:26:36 +0000 | [diff] [blame] | 2806 | // 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] | 2807 | // operand is a constant, simplify a bit. |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2808 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) { |
| 2809 | switch (BO->getOpcode()) { |
Chris Lattner | 23b47b6 | 2004-07-06 07:38:18 +0000 | [diff] [blame] | 2810 | case Instruction::Rem: |
| 2811 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. |
| 2812 | if (CI->isNullValue() && isa<ConstantSInt>(BO->getOperand(1)) && |
| 2813 | BO->hasOneUse() && |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 2814 | cast<ConstantSInt>(BO->getOperand(1))->getValue() > 1) { |
| 2815 | int64_t V = cast<ConstantSInt>(BO->getOperand(1))->getValue(); |
| 2816 | if (isPowerOf2_64(V)) { |
| 2817 | unsigned L2 = Log2_64(V); |
Chris Lattner | 23b47b6 | 2004-07-06 07:38:18 +0000 | [diff] [blame] | 2818 | const Type *UTy = BO->getType()->getUnsignedVersion(); |
| 2819 | Value *NewX = InsertNewInstBefore(new CastInst(BO->getOperand(0), |
| 2820 | UTy, "tmp"), I); |
| 2821 | Constant *RHSCst = ConstantUInt::get(UTy, 1ULL << L2); |
| 2822 | Value *NewRem =InsertNewInstBefore(BinaryOperator::createRem(NewX, |
| 2823 | RHSCst, BO->getName()), I); |
| 2824 | return BinaryOperator::create(I.getOpcode(), NewRem, |
| 2825 | Constant::getNullValue(UTy)); |
| 2826 | } |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 2827 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2828 | break; |
Chris Lattner | 23b47b6 | 2004-07-06 07:38:18 +0000 | [diff] [blame] | 2829 | |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2830 | case Instruction::Add: |
Chris Lattner | 6e07936 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 2831 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. |
| 2832 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | b121ae1 | 2004-09-21 21:35:23 +0000 | [diff] [blame] | 2833 | if (BO->hasOneUse()) |
| 2834 | return new SetCondInst(I.getOpcode(), BO->getOperand(0), |
| 2835 | ConstantExpr::getSub(CI, BOp1C)); |
Chris Lattner | 6e07936 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 2836 | } else if (CI->isNullValue()) { |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2837 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 2838 | // efficiently invertible, or if the add has just this one use. |
| 2839 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2840 | |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2841 | if (Value *NegVal = dyn_castNegVal(BOp1)) |
| 2842 | return new SetCondInst(I.getOpcode(), BOp0, NegVal); |
| 2843 | else if (Value *NegVal = dyn_castNegVal(BOp0)) |
| 2844 | return new SetCondInst(I.getOpcode(), NegVal, BOp1); |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2845 | else if (BO->hasOneUse()) { |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2846 | Instruction *Neg = BinaryOperator::createNeg(BOp1, BO->getName()); |
| 2847 | BO->setName(""); |
| 2848 | InsertNewInstBefore(Neg, I); |
| 2849 | return new SetCondInst(I.getOpcode(), BOp0, Neg); |
| 2850 | } |
| 2851 | } |
| 2852 | break; |
| 2853 | case Instruction::Xor: |
| 2854 | // For the xor case, we can xor two constants together, eliminating |
| 2855 | // the explicit xor. |
| 2856 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) |
| 2857 | return BinaryOperator::create(I.getOpcode(), BO->getOperand(0), |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2858 | ConstantExpr::getXor(CI, BOC)); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2859 | |
| 2860 | // FALLTHROUGH |
| 2861 | case Instruction::Sub: |
| 2862 | // Replace (([sub|xor] A, B) != 0) with (A != B) |
| 2863 | if (CI->isNullValue()) |
| 2864 | return new SetCondInst(I.getOpcode(), BO->getOperand(0), |
| 2865 | BO->getOperand(1)); |
| 2866 | break; |
| 2867 | |
| 2868 | case Instruction::Or: |
| 2869 | // If bits are being or'd in that are not present in the constant we |
| 2870 | // are comparing against, then the comparison could never succeed! |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2871 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 2872 | Constant *NotCI = ConstantExpr::getNot(CI); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2873 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 2874 | return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE)); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2875 | } |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2876 | break; |
| 2877 | |
| 2878 | case Instruction::And: |
| 2879 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 2880 | // If bits are being compared against that are and'd out, then the |
| 2881 | // comparison can never succeed! |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 2882 | if (!ConstantExpr::getAnd(CI, |
| 2883 | ConstantExpr::getNot(BOC))->isNullValue()) |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 2884 | return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE)); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2885 | |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2886 | // If we have ((X & C) == C), turn it into ((X & C) != 0). |
Chris Lattner | ee59d4b | 2004-06-10 02:33:20 +0000 | [diff] [blame] | 2887 | if (CI == BOC && isOneBitSet(CI)) |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2888 | return new SetCondInst(isSetNE ? Instruction::SetEQ : |
| 2889 | Instruction::SetNE, Op0, |
| 2890 | Constant::getNullValue(CI->getType())); |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2891 | |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2892 | // Replace (and X, (1 << size(X)-1) != 0) with x < 0, converting X |
| 2893 | // to be a signed value as appropriate. |
| 2894 | if (isSignBit(BOC)) { |
| 2895 | Value *X = BO->getOperand(0); |
| 2896 | // If 'X' is not signed, insert a cast now... |
| 2897 | if (!BOC->getType()->isSigned()) { |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 2898 | const Type *DestTy = BOC->getType()->getSignedVersion(); |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2899 | X = InsertCastBefore(X, DestTy, I); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2900 | } |
| 2901 | return new SetCondInst(isSetNE ? Instruction::SetLT : |
| 2902 | Instruction::SetGE, X, |
| 2903 | Constant::getNullValue(X->getType())); |
| 2904 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2905 | |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2906 | // ((X & ~7) == 0) --> X < 8 |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2907 | if (CI->isNullValue() && isHighOnes(BOC)) { |
| 2908 | Value *X = BO->getOperand(0); |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2909 | Constant *NegX = ConstantExpr::getNeg(BOC); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2910 | |
| 2911 | // If 'X' is signed, insert a cast now. |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2912 | if (NegX->getType()->isSigned()) { |
| 2913 | const Type *DestTy = NegX->getType()->getUnsignedVersion(); |
| 2914 | X = InsertCastBefore(X, DestTy, I); |
| 2915 | NegX = ConstantExpr::getCast(NegX, DestTy); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
| 2918 | return new SetCondInst(isSetNE ? Instruction::SetGE : |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2919 | Instruction::SetLT, X, NegX); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2920 | } |
| 2921 | |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 2922 | } |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2923 | default: break; |
| 2924 | } |
| 2925 | } |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2926 | } else { // Not a SetEQ/SetNE |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2927 | // 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] | 2928 | if (CastInst *Cast = dyn_cast<CastInst>(Op0)) { |
| 2929 | Value *CastOp = Cast->getOperand(0); |
| 2930 | const Type *SrcTy = CastOp->getType(); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2931 | unsigned SrcTySize = SrcTy->getPrimitiveSizeInBits(); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2932 | if (SrcTy != Cast->getType() && SrcTy->isInteger() && |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2933 | SrcTySize == Cast->getType()->getPrimitiveSizeInBits()) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2934 | assert((SrcTy->isSigned() ^ Cast->getType()->isSigned()) && |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2935 | "Source and destination signednesses should differ!"); |
| 2936 | if (Cast->getType()->isSigned()) { |
| 2937 | // If this is a signed comparison, check for comparisons in the |
| 2938 | // vicinity of zero. |
| 2939 | if (I.getOpcode() == Instruction::SetLT && CI->isNullValue()) |
| 2940 | // X < 0 => x > 127 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2941 | return BinaryOperator::createSetGT(CastOp, |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2942 | ConstantUInt::get(SrcTy, (1ULL << (SrcTySize-1))-1)); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2943 | else if (I.getOpcode() == Instruction::SetGT && |
| 2944 | cast<ConstantSInt>(CI)->getValue() == -1) |
| 2945 | // X > -1 => x < 128 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2946 | return BinaryOperator::createSetLT(CastOp, |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2947 | ConstantUInt::get(SrcTy, 1ULL << (SrcTySize-1))); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2948 | } else { |
| 2949 | ConstantUInt *CUI = cast<ConstantUInt>(CI); |
| 2950 | if (I.getOpcode() == Instruction::SetLT && |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2951 | CUI->getValue() == 1ULL << (SrcTySize-1)) |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2952 | // X < 128 => X > -1 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2953 | return BinaryOperator::createSetGT(CastOp, |
| 2954 | ConstantSInt::get(SrcTy, -1)); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2955 | else if (I.getOpcode() == Instruction::SetGT && |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2956 | CUI->getValue() == (1ULL << (SrcTySize-1))-1) |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2957 | // X > 127 => X < 0 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2958 | return BinaryOperator::createSetLT(CastOp, |
| 2959 | Constant::getNullValue(SrcTy)); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2960 | } |
| 2961 | } |
| 2962 | } |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 2963 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 2966 | // Handle setcc with constant RHS's that can be integer, FP or pointer. |
| 2967 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 2968 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 2969 | switch (LHSI->getOpcode()) { |
Chris Lattner | a816eee | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 2970 | case Instruction::GetElementPtr: |
| 2971 | if (RHSC->isNullValue()) { |
| 2972 | // Transform setcc GEP P, int 0, int 0, int 0, null -> setcc P, null |
| 2973 | bool isAllZeros = true; |
| 2974 | for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i) |
| 2975 | if (!isa<Constant>(LHSI->getOperand(i)) || |
| 2976 | !cast<Constant>(LHSI->getOperand(i))->isNullValue()) { |
| 2977 | isAllZeros = false; |
| 2978 | break; |
| 2979 | } |
| 2980 | if (isAllZeros) |
| 2981 | return new SetCondInst(I.getOpcode(), LHSI->getOperand(0), |
| 2982 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
| 2983 | } |
| 2984 | break; |
| 2985 | |
Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 2986 | case Instruction::PHI: |
| 2987 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2988 | return NV; |
| 2989 | break; |
| 2990 | case Instruction::Select: |
| 2991 | // If either operand of the select is a constant, we can fold the |
| 2992 | // comparison into the select arms, which will cause one to be |
| 2993 | // constant folded and the select turned into a bitwise or. |
| 2994 | Value *Op1 = 0, *Op2 = 0; |
| 2995 | if (LHSI->hasOneUse()) { |
| 2996 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 2997 | // Fold the known value into the constant operand. |
| 2998 | Op1 = ConstantExpr::get(I.getOpcode(), C, RHSC); |
| 2999 | // Insert a new SetCC of the other select operand. |
| 3000 | Op2 = InsertNewInstBefore(new SetCondInst(I.getOpcode(), |
| 3001 | LHSI->getOperand(2), RHSC, |
| 3002 | I.getName()), I); |
| 3003 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 3004 | // Fold the known value into the constant operand. |
| 3005 | Op2 = ConstantExpr::get(I.getOpcode(), C, RHSC); |
| 3006 | // Insert a new SetCC of the other select operand. |
| 3007 | Op1 = InsertNewInstBefore(new SetCondInst(I.getOpcode(), |
| 3008 | LHSI->getOperand(1), RHSC, |
| 3009 | I.getName()), I); |
| 3010 | } |
| 3011 | } |
Jeff Cohen | 8263985 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 3012 | |
Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 3013 | if (Op1) |
| 3014 | return new SelectInst(LHSI->getOperand(0), Op1, Op2); |
| 3015 | break; |
| 3016 | } |
| 3017 | } |
| 3018 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3019 | // If we can optimize a 'setcc GEP, P' or 'setcc P, GEP', do so now. |
| 3020 | if (User *GEP = dyn_castGetElementPtr(Op0)) |
| 3021 | if (Instruction *NI = FoldGEPSetCC(GEP, Op1, I.getOpcode(), I)) |
| 3022 | return NI; |
| 3023 | if (User *GEP = dyn_castGetElementPtr(Op1)) |
| 3024 | if (Instruction *NI = FoldGEPSetCC(GEP, Op0, |
| 3025 | SetCondInst::getSwappedCondition(I.getOpcode()), I)) |
| 3026 | return NI; |
| 3027 | |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 3028 | // Test to see if the operands of the setcc are casted versions of other |
| 3029 | // 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] | 3030 | if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
| 3031 | Value *CastOp0 = CI->getOperand(0); |
| 3032 | if (CastOp0->getType()->isLosslesslyConvertibleTo(CI->getType()) && |
Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 3033 | (isa<Constant>(Op1) || isa<CastInst>(Op1)) && |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 3034 | (I.getOpcode() == Instruction::SetEQ || |
| 3035 | I.getOpcode() == Instruction::SetNE)) { |
| 3036 | // We keep moving the cast from the left operand over to the right |
| 3037 | // operand, where it can often be eliminated completely. |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 3038 | Op0 = CastOp0; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3039 | |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 3040 | // If operand #1 is a cast instruction, see if we can eliminate it as |
| 3041 | // well. |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 3042 | if (CastInst *CI2 = dyn_cast<CastInst>(Op1)) |
| 3043 | if (CI2->getOperand(0)->getType()->isLosslesslyConvertibleTo( |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 3044 | Op0->getType())) |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 3045 | Op1 = CI2->getOperand(0); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3046 | |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 3047 | // If Op1 is a constant, we can fold the cast into the constant. |
| 3048 | if (Op1->getType() != Op0->getType()) |
| 3049 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
| 3050 | Op1 = ConstantExpr::getCast(Op1C, Op0->getType()); |
| 3051 | } else { |
| 3052 | // Otherwise, cast the RHS right before the setcc |
| 3053 | Op1 = new CastInst(Op1, Op0->getType(), Op1->getName()); |
| 3054 | InsertNewInstBefore(cast<Instruction>(Op1), I); |
| 3055 | } |
| 3056 | return BinaryOperator::create(I.getOpcode(), Op0, Op1); |
| 3057 | } |
| 3058 | |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 3059 | // Handle the special case of: setcc (cast bool to X), <cst> |
| 3060 | // This comes up when you have code like |
| 3061 | // int X = A < B; |
| 3062 | // if (X) ... |
| 3063 | // For generality, we handle any zero-extension of any operand comparison |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3064 | // with a constant or another cast from the same type. |
| 3065 | if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1)) |
| 3066 | if (Instruction *R = visitSetCondInstWithCastAndCast(I)) |
| 3067 | return R; |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 3068 | } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3069 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3070 | } |
| 3071 | |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3072 | // visitSetCondInstWithCastAndCast - Handle setcond (cast x to y), (cast/cst). |
| 3073 | // We only handle extending casts so far. |
| 3074 | // |
| 3075 | Instruction *InstCombiner::visitSetCondInstWithCastAndCast(SetCondInst &SCI) { |
| 3076 | Value *LHSCIOp = cast<CastInst>(SCI.getOperand(0))->getOperand(0); |
| 3077 | const Type *SrcTy = LHSCIOp->getType(); |
| 3078 | const Type *DestTy = SCI.getOperand(0)->getType(); |
| 3079 | Value *RHSCIOp; |
| 3080 | |
| 3081 | if (!DestTy->isIntegral() || !SrcTy->isIntegral()) |
Chris Lattner | 03f06f1 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 3082 | return 0; |
| 3083 | |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3084 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); |
| 3085 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); |
| 3086 | if (SrcBits >= DestBits) return 0; // Only handle extending cast. |
| 3087 | |
| 3088 | // Is this a sign or zero extension? |
| 3089 | bool isSignSrc = SrcTy->isSigned(); |
| 3090 | bool isSignDest = DestTy->isSigned(); |
| 3091 | |
| 3092 | if (CastInst *CI = dyn_cast<CastInst>(SCI.getOperand(1))) { |
| 3093 | // Not an extension from the same type? |
| 3094 | RHSCIOp = CI->getOperand(0); |
| 3095 | if (RHSCIOp->getType() != LHSCIOp->getType()) return 0; |
| 3096 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(SCI.getOperand(1))) { |
| 3097 | // Compute the constant that would happen if we truncated to SrcTy then |
| 3098 | // reextended to DestTy. |
| 3099 | Constant *Res = ConstantExpr::getCast(CI, SrcTy); |
| 3100 | |
| 3101 | if (ConstantExpr::getCast(Res, DestTy) == CI) { |
| 3102 | RHSCIOp = Res; |
| 3103 | } else { |
| 3104 | // If the value cannot be represented in the shorter type, we cannot emit |
| 3105 | // a simple comparison. |
| 3106 | if (SCI.getOpcode() == Instruction::SetEQ) |
| 3107 | return ReplaceInstUsesWith(SCI, ConstantBool::False); |
| 3108 | if (SCI.getOpcode() == Instruction::SetNE) |
| 3109 | return ReplaceInstUsesWith(SCI, ConstantBool::True); |
| 3110 | |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3111 | // Evaluate the comparison for LT. |
| 3112 | Value *Result; |
| 3113 | if (DestTy->isSigned()) { |
| 3114 | // We're performing a signed comparison. |
| 3115 | if (isSignSrc) { |
| 3116 | // Signed extend and signed comparison. |
| 3117 | if (cast<ConstantSInt>(CI)->getValue() < 0) // X < (small) --> false |
| 3118 | Result = ConstantBool::False; |
| 3119 | else |
| 3120 | Result = ConstantBool::True; // X < (large) --> true |
| 3121 | } else { |
| 3122 | // Unsigned extend and signed comparison. |
| 3123 | if (cast<ConstantSInt>(CI)->getValue() < 0) |
| 3124 | Result = ConstantBool::False; |
| 3125 | else |
| 3126 | Result = ConstantBool::True; |
| 3127 | } |
| 3128 | } else { |
| 3129 | // We're performing an unsigned comparison. |
| 3130 | if (!isSignSrc) { |
| 3131 | // Unsigned extend & compare -> always true. |
| 3132 | Result = ConstantBool::True; |
| 3133 | } else { |
| 3134 | // We're performing an unsigned comp with a sign extended value. |
| 3135 | // This is true if the input is >= 0. [aka >s -1] |
| 3136 | Constant *NegOne = ConstantIntegral::getAllOnesValue(SrcTy); |
| 3137 | Result = InsertNewInstBefore(BinaryOperator::createSetGT(LHSCIOp, |
| 3138 | NegOne, SCI.getName()), SCI); |
| 3139 | } |
Reid Spencer | 279fa25 | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 3140 | } |
Chris Lattner | 03f06f1 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 3141 | |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3142 | // Finally, return the value computed. |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3143 | if (SCI.getOpcode() == Instruction::SetLT) { |
| 3144 | return ReplaceInstUsesWith(SCI, Result); |
| 3145 | } else { |
| 3146 | assert(SCI.getOpcode()==Instruction::SetGT &&"SetCC should be folded!"); |
| 3147 | if (Constant *CI = dyn_cast<Constant>(Result)) |
| 3148 | return ReplaceInstUsesWith(SCI, ConstantExpr::getNot(CI)); |
| 3149 | else |
| 3150 | return BinaryOperator::createNot(Result); |
| 3151 | } |
Chris Lattner | 03f06f1 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 3152 | } |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3153 | } else { |
| 3154 | return 0; |
Reid Spencer | 279fa25 | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 3155 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3156 | |
Chris Lattner | 252a845 | 2005-06-16 03:00:08 +0000 | [diff] [blame] | 3157 | // Okay, just insert a compare of the reduced operands now! |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3158 | return BinaryOperator::create(SCI.getOpcode(), LHSCIOp, RHSCIOp); |
| 3159 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3160 | |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 3161 | Instruction *InstCombiner::visitShiftInst(ShiftInst &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3162 | assert(I.getOperand(1)->getType() == Type::UByteTy); |
| 3163 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3164 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3165 | |
| 3166 | // shl X, 0 == X and shr X, 0 == X |
| 3167 | // shl 0, X == 0 and shr 0, X == 0 |
| 3168 | if (Op1 == Constant::getNullValue(Type::UByteTy) || |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3169 | Op0 == Constant::getNullValue(Op0->getType())) |
| 3170 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3171 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3172 | if (isa<UndefValue>(Op0)) { // undef >>s X -> undef |
| 3173 | if (!isLeftShift && I.getType()->isSigned()) |
Chris Lattner | 67f0545 | 2004-10-16 23:28:04 +0000 | [diff] [blame] | 3174 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3175 | else // undef << X -> 0 AND undef >>u X -> 0 |
| 3176 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3177 | } |
| 3178 | if (isa<UndefValue>(Op1)) { |
Chris Lattner | 18aa4d8 | 2005-07-20 18:49:28 +0000 | [diff] [blame] | 3179 | if (isLeftShift || I.getType()->isUnsigned())// X << undef, X >>u undef -> 0 |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3180 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3181 | else |
| 3182 | return ReplaceInstUsesWith(I, Op0); // X >>s undef -> X |
| 3183 | } |
| 3184 | |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3185 | // shr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 3186 | if (!isLeftShift) |
| 3187 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0)) |
| 3188 | if (CSI->isAllOnesValue()) |
| 3189 | return ReplaceInstUsesWith(I, CSI); |
| 3190 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3191 | // Try to fold constant and into select arguments. |
| 3192 | if (isa<Constant>(Op0)) |
| 3193 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3194 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3195 | return R; |
| 3196 | |
Chris Lattner | b18dbbf | 2005-05-08 17:34:56 +0000 | [diff] [blame] | 3197 | // See if we can turn a signed shr into an unsigned shr. |
| 3198 | if (!isLeftShift && I.getType()->isSigned()) { |
| 3199 | if (MaskedValueIsZero(Op0, ConstantInt::getMinValue(I.getType()))) { |
| 3200 | Value *V = InsertCastBefore(Op0, I.getType()->getUnsignedVersion(), I); |
| 3201 | V = InsertNewInstBefore(new ShiftInst(Instruction::Shr, V, Op1, |
| 3202 | I.getName()), I); |
| 3203 | return new CastInst(V, I.getType()); |
| 3204 | } |
| 3205 | } |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3206 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3207 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) { |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3208 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr |
| 3209 | // of a signed value. |
| 3210 | // |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3211 | unsigned TypeBits = Op0->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | f5ce254 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 3212 | if (CUI->getValue() >= TypeBits) { |
| 3213 | if (!Op0->getType()->isSigned() || isLeftShift) |
| 3214 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
| 3215 | else { |
| 3216 | I.setOperand(1, ConstantUInt::get(Type::UByteTy, TypeBits-1)); |
| 3217 | return &I; |
| 3218 | } |
| 3219 | } |
Chris Lattner | 55f3d94 | 2002-09-10 23:04:09 +0000 | [diff] [blame] | 3220 | |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 3221 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 3222 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 3223 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 3224 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3225 | return BinaryOperator::createMul(BO->getOperand(0), |
| 3226 | ConstantExpr::getShl(BOOp, CUI)); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3227 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3228 | // Try to fold constant and into select arguments. |
| 3229 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3230 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3231 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3232 | if (isa<PHINode>(Op0)) |
| 3233 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3234 | return NV; |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 3235 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3236 | if (Op0->hasOneUse()) { |
| 3237 | // If this is a SHL of a sign-extending cast, see if we can turn the input |
| 3238 | // into a zero extending cast (a simple strength reduction). |
| 3239 | if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
| 3240 | const Type *SrcTy = CI->getOperand(0)->getType(); |
| 3241 | if (isLeftShift && SrcTy->isInteger() && SrcTy->isSigned() && |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3242 | SrcTy->getPrimitiveSizeInBits() < |
| 3243 | CI->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3244 | // We can change it to a zero extension if we are shifting out all of |
| 3245 | // the sign extended bits. To check this, form a mask of all of the |
| 3246 | // sign extend bits, then shift them left and see if we have anything |
| 3247 | // left. |
| 3248 | Constant *Mask = ConstantIntegral::getAllOnesValue(SrcTy); // 1111 |
| 3249 | Mask = ConstantExpr::getZeroExtend(Mask, CI->getType()); // 00001111 |
| 3250 | Mask = ConstantExpr::getNot(Mask); // 1's in the sign bits: 11110000 |
| 3251 | if (ConstantExpr::getShl(Mask, CUI)->isNullValue()) { |
| 3252 | // If the shift is nuking all of the sign bits, change this to a |
| 3253 | // zero extension cast. To do this, cast the cast input to |
| 3254 | // unsigned, then to the requested size. |
| 3255 | Value *CastOp = CI->getOperand(0); |
| 3256 | Instruction *NC = |
| 3257 | new CastInst(CastOp, CastOp->getType()->getUnsignedVersion(), |
| 3258 | CI->getName()+".uns"); |
| 3259 | NC = InsertNewInstBefore(NC, I); |
| 3260 | // Finally, insert a replacement for CI. |
| 3261 | NC = new CastInst(NC, CI->getType(), CI->getName()); |
| 3262 | CI->setName(""); |
| 3263 | NC = InsertNewInstBefore(NC, I); |
| 3264 | WorkList.push_back(CI); // Delete CI later. |
| 3265 | I.setOperand(0, NC); |
| 3266 | return &I; // The SHL operand was modified. |
| 3267 | } |
| 3268 | } |
| 3269 | } |
| 3270 | |
| 3271 | // If the operand is an bitwise operator with a constant RHS, and the |
| 3272 | // 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] | 3273 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) |
| 3274 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 3275 | bool isValid = true; // Valid only for And, Or, Xor |
| 3276 | bool highBitSet = false; // Transform if high bit of constant set? |
| 3277 | |
| 3278 | switch (Op0BO->getOpcode()) { |
| 3279 | default: isValid = false; break; // Do not perform transform! |
Chris Lattner | 44bd392 | 2004-10-08 03:46:20 +0000 | [diff] [blame] | 3280 | case Instruction::Add: |
| 3281 | isValid = isLeftShift; |
| 3282 | break; |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3283 | case Instruction::Or: |
| 3284 | case Instruction::Xor: |
| 3285 | highBitSet = false; |
| 3286 | break; |
| 3287 | case Instruction::And: |
| 3288 | highBitSet = true; |
| 3289 | break; |
| 3290 | } |
| 3291 | |
| 3292 | // If this is a signed shift right, and the high bit is modified |
| 3293 | // by the logical operation, do not perform the transformation. |
| 3294 | // The highBitSet boolean indicates the value of the high bit of |
| 3295 | // the constant which would cause it to be modified for this |
| 3296 | // operation. |
| 3297 | // |
| 3298 | if (isValid && !isLeftShift && !I.getType()->isUnsigned()) { |
| 3299 | uint64_t Val = Op0C->getRawValue(); |
| 3300 | isValid = ((Val & (1 << (TypeBits-1))) != 0) == highBitSet; |
| 3301 | } |
| 3302 | |
| 3303 | if (isValid) { |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3304 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, CUI); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3305 | |
| 3306 | Instruction *NewShift = |
| 3307 | new ShiftInst(I.getOpcode(), Op0BO->getOperand(0), CUI, |
| 3308 | Op0BO->getName()); |
| 3309 | Op0BO->setName(""); |
| 3310 | InsertNewInstBefore(NewShift, I); |
| 3311 | |
| 3312 | return BinaryOperator::create(Op0BO->getOpcode(), NewShift, |
| 3313 | NewRHS); |
| 3314 | } |
| 3315 | } |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3316 | } |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3317 | |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3318 | // 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] | 3319 | if (ShiftInst *Op0SI = dyn_cast<ShiftInst>(Op0)) |
Chris Lattner | ab780df | 2003-07-24 18:38:56 +0000 | [diff] [blame] | 3320 | if (ConstantUInt *ShiftAmt1C = |
| 3321 | dyn_cast<ConstantUInt>(Op0SI->getOperand(1))) { |
Chris Lattner | fdfe3e49 | 2005-01-08 19:42:22 +0000 | [diff] [blame] | 3322 | unsigned ShiftAmt1 = (unsigned)ShiftAmt1C->getValue(); |
| 3323 | unsigned ShiftAmt2 = (unsigned)CUI->getValue(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3324 | |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3325 | // Check for (A << c1) << c2 and (A >> c1) >> c2 |
| 3326 | if (I.getOpcode() == Op0SI->getOpcode()) { |
| 3327 | unsigned Amt = ShiftAmt1+ShiftAmt2; // Fold into one big shift... |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3328 | if (Op0->getType()->getPrimitiveSizeInBits() < Amt) |
| 3329 | Amt = Op0->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3330 | return new ShiftInst(I.getOpcode(), Op0SI->getOperand(0), |
| 3331 | ConstantUInt::get(Type::UByteTy, Amt)); |
| 3332 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3333 | |
Chris Lattner | ab780df | 2003-07-24 18:38:56 +0000 | [diff] [blame] | 3334 | // Check for (A << c1) >> c2 or visaversa. If we are dealing with |
| 3335 | // signed types, we can only support the (A >> c1) << c2 configuration, |
| 3336 | // 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] | 3337 | if (I.getType()->isUnsigned() || isLeftShift) { |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3338 | // Calculate bitmask for what gets shifted off the edge... |
| 3339 | Constant *C = ConstantIntegral::getAllOnesValue(I.getType()); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3340 | if (isLeftShift) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3341 | C = ConstantExpr::getShl(C, ShiftAmt1C); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3342 | else |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3343 | C = ConstantExpr::getShr(C, ShiftAmt1C); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3344 | |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3345 | Instruction *Mask = |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3346 | BinaryOperator::createAnd(Op0SI->getOperand(0), C, |
| 3347 | Op0SI->getOperand(0)->getName()+".mask"); |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3348 | InsertNewInstBefore(Mask, I); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3349 | |
Chris Lattner | 3204d4e | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3350 | // Figure out what flavor of shift we should use... |
| 3351 | if (ShiftAmt1 == ShiftAmt2) |
| 3352 | return ReplaceInstUsesWith(I, Mask); // (A << c) >> c === A & c2 |
| 3353 | else if (ShiftAmt1 < ShiftAmt2) { |
| 3354 | return new ShiftInst(I.getOpcode(), Mask, |
| 3355 | ConstantUInt::get(Type::UByteTy, ShiftAmt2-ShiftAmt1)); |
| 3356 | } else { |
| 3357 | return new ShiftInst(Op0SI->getOpcode(), Mask, |
| 3358 | ConstantUInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2)); |
| 3359 | } |
| 3360 | } |
| 3361 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3362 | } |
Chris Lattner | 2e0fb39 | 2002-10-08 16:16:40 +0000 | [diff] [blame] | 3363 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3364 | return 0; |
| 3365 | } |
| 3366 | |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 3367 | enum CastType { |
| 3368 | Noop = 0, |
| 3369 | Truncate = 1, |
| 3370 | Signext = 2, |
| 3371 | Zeroext = 3 |
| 3372 | }; |
| 3373 | |
| 3374 | /// getCastType - In the future, we will split the cast instruction into these |
| 3375 | /// various types. Until then, we have to do the analysis here. |
| 3376 | static CastType getCastType(const Type *Src, const Type *Dest) { |
| 3377 | assert(Src->isIntegral() && Dest->isIntegral() && |
| 3378 | "Only works on integral types!"); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3379 | unsigned SrcSize = Src->getPrimitiveSizeInBits(); |
| 3380 | unsigned DestSize = Dest->getPrimitiveSizeInBits(); |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 3381 | |
| 3382 | if (SrcSize == DestSize) return Noop; |
| 3383 | if (SrcSize > DestSize) return Truncate; |
| 3384 | if (Src->isSigned()) return Signext; |
| 3385 | return Zeroext; |
| 3386 | } |
| 3387 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3388 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3389 | // isEliminableCastOfCast - Return true if it is valid to eliminate the CI |
| 3390 | // instruction. |
| 3391 | // |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3392 | static inline bool isEliminableCastOfCast(const Type *SrcTy, const Type *MidTy, |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 3393 | const Type *DstTy, TargetData *TD) { |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3394 | |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3395 | // 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] | 3396 | // are identical and the bits don't get reinterpreted (for example |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 3397 | // int->float->int would not be allowed). |
Misha Brukman | e5838c4 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 3398 | if (SrcTy == DstTy && SrcTy->isLosslesslyConvertibleTo(MidTy)) |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3399 | return true; |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3400 | |
Chris Lattner | 4fbad96 | 2004-07-21 04:27:24 +0000 | [diff] [blame] | 3401 | // If we are casting between pointer and integer types, treat pointers as |
| 3402 | // integers of the appropriate size for the code below. |
| 3403 | if (isa<PointerType>(SrcTy)) SrcTy = TD->getIntPtrType(); |
| 3404 | if (isa<PointerType>(MidTy)) MidTy = TD->getIntPtrType(); |
| 3405 | if (isa<PointerType>(DstTy)) DstTy = TD->getIntPtrType(); |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 3406 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3407 | // Allow free casting and conversion of sizes as long as the sign doesn't |
| 3408 | // change... |
Chris Lattner | b0b412e | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 3409 | if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral()) { |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 3410 | CastType FirstCast = getCastType(SrcTy, MidTy); |
| 3411 | CastType SecondCast = getCastType(MidTy, DstTy); |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3412 | |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 3413 | // Capture the effect of these two casts. If the result is a legal cast, |
| 3414 | // the CastType is stored here, otherwise a special code is used. |
| 3415 | static const unsigned CastResult[] = { |
| 3416 | // First cast is noop |
| 3417 | 0, 1, 2, 3, |
| 3418 | // First cast is a truncate |
| 3419 | 1, 1, 4, 4, // trunc->extend is not safe to eliminate |
| 3420 | // First cast is a sign ext |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 3421 | 2, 5, 2, 4, // signext->zeroext never ok |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 3422 | // First cast is a zero ext |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 3423 | 3, 5, 3, 3, |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 3424 | }; |
| 3425 | |
| 3426 | unsigned Result = CastResult[FirstCast*4+SecondCast]; |
| 3427 | switch (Result) { |
| 3428 | default: assert(0 && "Illegal table value!"); |
| 3429 | case 0: |
| 3430 | case 1: |
| 3431 | case 2: |
| 3432 | case 3: |
| 3433 | // FIXME: in the future, when LLVM has explicit sign/zeroextends and |
| 3434 | // truncates, we could eliminate more casts. |
| 3435 | return (unsigned)getCastType(SrcTy, DstTy) == Result; |
| 3436 | case 4: |
| 3437 | return false; // Not possible to eliminate this here. |
| 3438 | case 5: |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 3439 | // Sign or zero extend followed by truncate is always ok if the result |
| 3440 | // is a truncate or noop. |
| 3441 | CastType ResultCast = getCastType(SrcTy, DstTy); |
| 3442 | if (ResultCast == Noop || ResultCast == Truncate) |
| 3443 | return true; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3444 | // 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] | 3445 | // result will match the sign/zeroextendness of the result. |
| 3446 | return ResultCast == FirstCast; |
Chris Lattner | 3732aca | 2002-08-15 16:15:25 +0000 | [diff] [blame] | 3447 | } |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3448 | } |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3449 | return false; |
| 3450 | } |
| 3451 | |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 3452 | static bool ValueRequiresCast(const Value *V, const Type *Ty, TargetData *TD) { |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3453 | if (V->getType() == Ty || isa<Constant>(V)) return false; |
| 3454 | if (const CastInst *CI = dyn_cast<CastInst>(V)) |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 3455 | if (isEliminableCastOfCast(CI->getOperand(0)->getType(), CI->getType(), Ty, |
| 3456 | TD)) |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3457 | return false; |
| 3458 | return true; |
| 3459 | } |
| 3460 | |
| 3461 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 3462 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 3463 | /// casts that are known to not do anything... |
| 3464 | /// |
| 3465 | Value *InstCombiner::InsertOperandCastBefore(Value *V, const Type *DestTy, |
| 3466 | Instruction *InsertBefore) { |
| 3467 | if (V->getType() == DestTy) return V; |
| 3468 | if (Constant *C = dyn_cast<Constant>(V)) |
| 3469 | return ConstantExpr::getCast(C, DestTy); |
| 3470 | |
| 3471 | CastInst *CI = new CastInst(V, DestTy, V->getName()); |
| 3472 | InsertNewInstBefore(CI, *InsertBefore); |
| 3473 | return CI; |
| 3474 | } |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3475 | |
| 3476 | // CastInst simplification |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 3477 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3478 | Instruction *InstCombiner::visitCastInst(CastInst &CI) { |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 3479 | Value *Src = CI.getOperand(0); |
| 3480 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3481 | // If the user is casting a value to the same type, eliminate this cast |
| 3482 | // instruction... |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 3483 | if (CI.getType() == Src->getType()) |
| 3484 | return ReplaceInstUsesWith(CI, Src); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3485 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3486 | if (isa<UndefValue>(Src)) // cast undef -> undef |
| 3487 | return ReplaceInstUsesWith(CI, UndefValue::get(CI.getType())); |
| 3488 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3489 | // If casting the result of another cast instruction, try to eliminate this |
| 3490 | // one! |
| 3491 | // |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3492 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
| 3493 | Value *A = CSrc->getOperand(0); |
| 3494 | if (isEliminableCastOfCast(A->getType(), CSrc->getType(), |
| 3495 | CI.getType(), TD)) { |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3496 | // This instruction now refers directly to the cast's src operand. This |
| 3497 | // has a good chance of making CSrc dead. |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3498 | CI.setOperand(0, CSrc->getOperand(0)); |
| 3499 | return &CI; |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3500 | } |
| 3501 | |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3502 | // If this is an A->B->A cast, and we are dealing with integral types, try |
| 3503 | // to convert this into a logical 'and' instruction. |
| 3504 | // |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3505 | if (A->getType()->isInteger() && |
Chris Lattner | b0b412e | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 3506 | CI.getType()->isInteger() && CSrc->getType()->isInteger() && |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3507 | CSrc->getType()->isUnsigned() && // B->A cast must zero extend |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3508 | CSrc->getType()->getPrimitiveSizeInBits() < |
| 3509 | CI.getType()->getPrimitiveSizeInBits()&& |
| 3510 | A->getType()->getPrimitiveSizeInBits() == |
| 3511 | CI.getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3512 | assert(CSrc->getType() != Type::ULongTy && |
| 3513 | "Cannot have type bigger than ulong!"); |
Chris Lattner | 2f1457f | 2005-04-24 17:46:05 +0000 | [diff] [blame] | 3514 | uint64_t AndValue = ~0ULL>>(64-CSrc->getType()->getPrimitiveSizeInBits()); |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3515 | Constant *AndOp = ConstantUInt::get(A->getType()->getUnsignedVersion(), |
| 3516 | AndValue); |
| 3517 | AndOp = ConstantExpr::getCast(AndOp, A->getType()); |
| 3518 | Instruction *And = BinaryOperator::createAnd(CSrc->getOperand(0), AndOp); |
| 3519 | if (And->getType() != CI.getType()) { |
| 3520 | And->setName(CSrc->getName()+".mask"); |
| 3521 | InsertNewInstBefore(And, CI); |
| 3522 | And = new CastInst(And, CI.getType()); |
| 3523 | } |
| 3524 | return And; |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3525 | } |
| 3526 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3527 | |
Chris Lattner | 0384165 | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 3528 | // If this is a cast to bool, turn it into the appropriate setne instruction. |
| 3529 | if (CI.getType() == Type::BoolTy) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3530 | return BinaryOperator::createSetNE(CI.getOperand(0), |
Chris Lattner | 0384165 | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 3531 | Constant::getNullValue(CI.getOperand(0)->getType())); |
| 3532 | |
Chris Lattner | d0d5160 | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 3533 | // If casting the result of a getelementptr instruction with no offset, turn |
| 3534 | // this into a cast of the original pointer! |
| 3535 | // |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 3536 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
Chris Lattner | d0d5160 | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 3537 | bool AllZeroOperands = true; |
| 3538 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) |
| 3539 | if (!isa<Constant>(GEP->getOperand(i)) || |
| 3540 | !cast<Constant>(GEP->getOperand(i))->isNullValue()) { |
| 3541 | AllZeroOperands = false; |
| 3542 | break; |
| 3543 | } |
| 3544 | if (AllZeroOperands) { |
| 3545 | CI.setOperand(0, GEP->getOperand(0)); |
| 3546 | return &CI; |
| 3547 | } |
| 3548 | } |
| 3549 | |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 3550 | // If we are casting a malloc or alloca to a pointer to a type of the same |
| 3551 | // size, rewrite the allocation instruction to allocate the "right" type. |
| 3552 | // |
| 3553 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) |
Chris Lattner | d4d987d | 2003-11-02 06:54:48 +0000 | [diff] [blame] | 3554 | if (AI->hasOneUse() && !AI->isArrayAllocation()) |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 3555 | if (const PointerType *PTy = dyn_cast<PointerType>(CI.getType())) { |
| 3556 | // Get the type really allocated and the type casted to... |
| 3557 | const Type *AllocElTy = AI->getAllocatedType(); |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 3558 | const Type *CastElTy = PTy->getElementType(); |
Chris Lattner | 9eb9ccd | 2004-07-06 19:28:42 +0000 | [diff] [blame] | 3559 | if (AllocElTy->isSized() && CastElTy->isSized()) { |
Chris Lattner | fdfe3e49 | 2005-01-08 19:42:22 +0000 | [diff] [blame] | 3560 | uint64_t AllocElTySize = TD->getTypeSize(AllocElTy); |
| 3561 | uint64_t CastElTySize = TD->getTypeSize(CastElTy); |
Chris Lattner | 7c94d11 | 2003-11-05 17:31:36 +0000 | [diff] [blame] | 3562 | |
Chris Lattner | 9eb9ccd | 2004-07-06 19:28:42 +0000 | [diff] [blame] | 3563 | // If the allocation is for an even multiple of the cast type size |
| 3564 | if (CastElTySize && (AllocElTySize % CastElTySize == 0)) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3565 | Value *Amt = ConstantUInt::get(Type::UIntTy, |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 3566 | AllocElTySize/CastElTySize); |
Chris Lattner | 9eb9ccd | 2004-07-06 19:28:42 +0000 | [diff] [blame] | 3567 | std::string Name = AI->getName(); AI->setName(""); |
| 3568 | AllocationInst *New; |
| 3569 | if (isa<MallocInst>(AI)) |
| 3570 | New = new MallocInst(CastElTy, Amt, Name); |
| 3571 | else |
| 3572 | New = new AllocaInst(CastElTy, Amt, Name); |
| 3573 | InsertNewInstBefore(New, *AI); |
| 3574 | return ReplaceInstUsesWith(CI, New); |
| 3575 | } |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 3576 | } |
| 3577 | } |
| 3578 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3579 | if (SelectInst *SI = dyn_cast<SelectInst>(Src)) |
| 3580 | if (Instruction *NV = FoldOpIntoSelect(CI, SI, this)) |
| 3581 | return NV; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3582 | if (isa<PHINode>(Src)) |
| 3583 | if (Instruction *NV = FoldOpIntoPhi(CI)) |
| 3584 | return NV; |
| 3585 | |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3586 | // If the source value is an instruction with only this use, we can attempt to |
| 3587 | // propagate the cast into the instruction. Also, only handle integral types |
| 3588 | // for now. |
| 3589 | if (Instruction *SrcI = dyn_cast<Instruction>(Src)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 3590 | if (SrcI->hasOneUse() && Src->getType()->isIntegral() && |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3591 | CI.getType()->isInteger()) { // Don't mess with casts to bool here |
| 3592 | const Type *DestTy = CI.getType(); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3593 | unsigned SrcBitSize = Src->getType()->getPrimitiveSizeInBits(); |
| 3594 | unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3595 | |
| 3596 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; |
| 3597 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; |
| 3598 | |
| 3599 | switch (SrcI->getOpcode()) { |
| 3600 | case Instruction::Add: |
| 3601 | case Instruction::Mul: |
| 3602 | case Instruction::And: |
| 3603 | case Instruction::Or: |
| 3604 | case Instruction::Xor: |
| 3605 | // If we are discarding information, or just changing the sign, rewrite. |
| 3606 | if (DestBitSize <= SrcBitSize && DestBitSize != 1) { |
| 3607 | // Don't insert two casts if they cannot be eliminated. We allow two |
| 3608 | // casts to be inserted if the sizes are the same. This could only be |
| 3609 | // converting signedness, which is a noop. |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 3610 | if (DestBitSize == SrcBitSize || !ValueRequiresCast(Op1, DestTy,TD) || |
| 3611 | !ValueRequiresCast(Op0, DestTy, TD)) { |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3612 | Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI); |
| 3613 | Value *Op1c = InsertOperandCastBefore(Op1, DestTy, SrcI); |
| 3614 | return BinaryOperator::create(cast<BinaryOperator>(SrcI) |
| 3615 | ->getOpcode(), Op0c, Op1c); |
| 3616 | } |
| 3617 | } |
Chris Lattner | 7208616 | 2005-05-06 02:07:39 +0000 | [diff] [blame] | 3618 | |
| 3619 | // cast (xor bool X, true) to int --> xor (cast bool X to int), 1 |
| 3620 | if (SrcBitSize == 1 && SrcI->getOpcode() == Instruction::Xor && |
| 3621 | Op1 == ConstantBool::True && |
| 3622 | (!Op0->hasOneUse() || !isa<SetCondInst>(Op0))) { |
| 3623 | Value *New = InsertOperandCastBefore(Op0, DestTy, &CI); |
| 3624 | return BinaryOperator::createXor(New, |
| 3625 | ConstantInt::get(CI.getType(), 1)); |
| 3626 | } |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3627 | break; |
| 3628 | case Instruction::Shl: |
| 3629 | // Allow changing the sign of the source operand. Do not allow changing |
| 3630 | // the size of the shift, UNLESS the shift amount is a constant. We |
| 3631 | // mush not change variable sized shifts to a smaller size, because it |
| 3632 | // is undefined to shift more bits out than exist in the value. |
| 3633 | if (DestBitSize == SrcBitSize || |
| 3634 | (DestBitSize < SrcBitSize && isa<Constant>(Op1))) { |
| 3635 | Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI); |
| 3636 | return new ShiftInst(Instruction::Shl, Op0c, Op1); |
| 3637 | } |
| 3638 | break; |
Chris Lattner | 8738041 | 2005-05-06 04:18:52 +0000 | [diff] [blame] | 3639 | case Instruction::Shr: |
| 3640 | // If this is a signed shr, and if all bits shifted in are about to be |
| 3641 | // truncated off, turn it into an unsigned shr to allow greater |
| 3642 | // simplifications. |
| 3643 | if (DestBitSize < SrcBitSize && Src->getType()->isSigned() && |
| 3644 | isa<ConstantInt>(Op1)) { |
| 3645 | unsigned ShiftAmt = cast<ConstantUInt>(Op1)->getValue(); |
| 3646 | if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) { |
| 3647 | // Convert to unsigned. |
| 3648 | Value *N1 = InsertOperandCastBefore(Op0, |
| 3649 | Op0->getType()->getUnsignedVersion(), &CI); |
| 3650 | // Insert the new shift, which is now unsigned. |
| 3651 | N1 = InsertNewInstBefore(new ShiftInst(Instruction::Shr, N1, |
| 3652 | Op1, Src->getName()), CI); |
| 3653 | return new CastInst(N1, CI.getType()); |
| 3654 | } |
| 3655 | } |
| 3656 | break; |
| 3657 | |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 3658 | case Instruction::SetNE: |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 3659 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 3660 | if (Op1C->getRawValue() == 0) { |
| 3661 | // If the input only has the low bit set, simplify directly. |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3662 | Constant *Not1 = |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 3663 | ConstantExpr::getNot(ConstantInt::get(Op0->getType(), 1)); |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 3664 | // cast (X != 0) to int --> X if X&~1 == 0 |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 3665 | if (MaskedValueIsZero(Op0, cast<ConstantIntegral>(Not1))) { |
| 3666 | if (CI.getType() == Op0->getType()) |
| 3667 | return ReplaceInstUsesWith(CI, Op0); |
| 3668 | else |
| 3669 | return new CastInst(Op0, CI.getType()); |
| 3670 | } |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 3671 | |
| 3672 | // If the input is an and with a single bit, shift then simplify. |
| 3673 | ConstantInt *AndRHS; |
| 3674 | if (match(Op0, m_And(m_Value(), m_ConstantInt(AndRHS)))) |
| 3675 | if (AndRHS->getRawValue() && |
| 3676 | (AndRHS->getRawValue() & (AndRHS->getRawValue()-1)) == 0) { |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 3677 | unsigned ShiftAmt = Log2_64(AndRHS->getRawValue()); |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 3678 | // Perform an unsigned shr by shiftamt. Convert input to |
| 3679 | // unsigned if it is signed. |
| 3680 | Value *In = Op0; |
| 3681 | if (In->getType()->isSigned()) |
| 3682 | In = InsertNewInstBefore(new CastInst(In, |
| 3683 | In->getType()->getUnsignedVersion(), In->getName()),CI); |
| 3684 | // Insert the shift to put the result in the low bit. |
| 3685 | In = InsertNewInstBefore(new ShiftInst(Instruction::Shr, In, |
| 3686 | ConstantInt::get(Type::UByteTy, ShiftAmt), |
| 3687 | In->getName()+".lobit"), CI); |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 3688 | if (CI.getType() == In->getType()) |
| 3689 | return ReplaceInstUsesWith(CI, In); |
| 3690 | else |
| 3691 | return new CastInst(In, CI.getType()); |
| 3692 | } |
| 3693 | } |
| 3694 | } |
| 3695 | break; |
| 3696 | case Instruction::SetEQ: |
| 3697 | // We if we are just checking for a seteq of a single bit and casting it |
| 3698 | // to an integer. If so, shift the bit to the appropriate place then |
| 3699 | // cast to integer to avoid the comparison. |
| 3700 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { |
| 3701 | // Is Op1C a power of two or zero? |
| 3702 | if ((Op1C->getRawValue() & Op1C->getRawValue()-1) == 0) { |
| 3703 | // cast (X == 1) to int -> X iff X has only the low bit set. |
| 3704 | if (Op1C->getRawValue() == 1) { |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3705 | Constant *Not1 = |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 3706 | ConstantExpr::getNot(ConstantInt::get(Op0->getType(), 1)); |
| 3707 | if (MaskedValueIsZero(Op0, cast<ConstantIntegral>(Not1))) { |
| 3708 | if (CI.getType() == Op0->getType()) |
| 3709 | return ReplaceInstUsesWith(CI, Op0); |
| 3710 | else |
| 3711 | return new CastInst(Op0, CI.getType()); |
| 3712 | } |
| 3713 | } |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 3714 | } |
| 3715 | } |
| 3716 | break; |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3717 | } |
| 3718 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 3719 | return 0; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 3720 | } |
| 3721 | |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 3722 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
| 3723 | /// %C = or %A, %B |
| 3724 | /// %D = select %cond, %C, %A |
| 3725 | /// into: |
| 3726 | /// %C = select %cond, %B, 0 |
| 3727 | /// %D = or %A, %C |
| 3728 | /// |
| 3729 | /// Assuming that the specified instruction is an operand to the select, return |
| 3730 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 3731 | /// equal the other incoming value of the select. |
| 3732 | /// |
| 3733 | static unsigned GetSelectFoldableOperands(Instruction *I) { |
| 3734 | switch (I->getOpcode()) { |
| 3735 | case Instruction::Add: |
| 3736 | case Instruction::Mul: |
| 3737 | case Instruction::And: |
| 3738 | case Instruction::Or: |
| 3739 | case Instruction::Xor: |
| 3740 | return 3; // Can fold through either operand. |
| 3741 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 3742 | case Instruction::Shl: // Can only fold on the shift amount. |
| 3743 | case Instruction::Shr: |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3744 | return 1; |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 3745 | default: |
| 3746 | return 0; // Cannot fold |
| 3747 | } |
| 3748 | } |
| 3749 | |
| 3750 | /// GetSelectFoldableConstant - For the same transformation as the previous |
| 3751 | /// function, return the identity constant that goes into the select. |
| 3752 | static Constant *GetSelectFoldableConstant(Instruction *I) { |
| 3753 | switch (I->getOpcode()) { |
| 3754 | default: assert(0 && "This cannot happen!"); abort(); |
| 3755 | case Instruction::Add: |
| 3756 | case Instruction::Sub: |
| 3757 | case Instruction::Or: |
| 3758 | case Instruction::Xor: |
| 3759 | return Constant::getNullValue(I->getType()); |
| 3760 | case Instruction::Shl: |
| 3761 | case Instruction::Shr: |
| 3762 | return Constant::getNullValue(Type::UByteTy); |
| 3763 | case Instruction::And: |
| 3764 | return ConstantInt::getAllOnesValue(I->getType()); |
| 3765 | case Instruction::Mul: |
| 3766 | return ConstantInt::get(I->getType(), 1); |
| 3767 | } |
| 3768 | } |
| 3769 | |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 3770 | /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI |
| 3771 | /// have the same opcode and only one use each. Try to simplify this. |
| 3772 | Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 3773 | Instruction *FI) { |
| 3774 | if (TI->getNumOperands() == 1) { |
| 3775 | // If this is a non-volatile load or a cast from the same type, |
| 3776 | // merge. |
| 3777 | if (TI->getOpcode() == Instruction::Cast) { |
| 3778 | if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType()) |
| 3779 | return 0; |
| 3780 | } else { |
| 3781 | return 0; // unknown unary op. |
| 3782 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3783 | |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 3784 | // Fold this by inserting a select from the input values. |
| 3785 | SelectInst *NewSI = new SelectInst(SI.getCondition(), TI->getOperand(0), |
| 3786 | FI->getOperand(0), SI.getName()+".v"); |
| 3787 | InsertNewInstBefore(NewSI, SI); |
| 3788 | return new CastInst(NewSI, TI->getType()); |
| 3789 | } |
| 3790 | |
| 3791 | // Only handle binary operators here. |
| 3792 | if (!isa<ShiftInst>(TI) && !isa<BinaryOperator>(TI)) |
| 3793 | return 0; |
| 3794 | |
| 3795 | // Figure out if the operations have any operands in common. |
| 3796 | Value *MatchOp, *OtherOpT, *OtherOpF; |
| 3797 | bool MatchIsOpZero; |
| 3798 | if (TI->getOperand(0) == FI->getOperand(0)) { |
| 3799 | MatchOp = TI->getOperand(0); |
| 3800 | OtherOpT = TI->getOperand(1); |
| 3801 | OtherOpF = FI->getOperand(1); |
| 3802 | MatchIsOpZero = true; |
| 3803 | } else if (TI->getOperand(1) == FI->getOperand(1)) { |
| 3804 | MatchOp = TI->getOperand(1); |
| 3805 | OtherOpT = TI->getOperand(0); |
| 3806 | OtherOpF = FI->getOperand(0); |
| 3807 | MatchIsOpZero = false; |
| 3808 | } else if (!TI->isCommutative()) { |
| 3809 | return 0; |
| 3810 | } else if (TI->getOperand(0) == FI->getOperand(1)) { |
| 3811 | MatchOp = TI->getOperand(0); |
| 3812 | OtherOpT = TI->getOperand(1); |
| 3813 | OtherOpF = FI->getOperand(0); |
| 3814 | MatchIsOpZero = true; |
| 3815 | } else if (TI->getOperand(1) == FI->getOperand(0)) { |
| 3816 | MatchOp = TI->getOperand(1); |
| 3817 | OtherOpT = TI->getOperand(0); |
| 3818 | OtherOpF = FI->getOperand(1); |
| 3819 | MatchIsOpZero = true; |
| 3820 | } else { |
| 3821 | return 0; |
| 3822 | } |
| 3823 | |
| 3824 | // If we reach here, they do have operations in common. |
| 3825 | SelectInst *NewSI = new SelectInst(SI.getCondition(), OtherOpT, |
| 3826 | OtherOpF, SI.getName()+".v"); |
| 3827 | InsertNewInstBefore(NewSI, SI); |
| 3828 | |
| 3829 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) { |
| 3830 | if (MatchIsOpZero) |
| 3831 | return BinaryOperator::create(BO->getOpcode(), MatchOp, NewSI); |
| 3832 | else |
| 3833 | return BinaryOperator::create(BO->getOpcode(), NewSI, MatchOp); |
| 3834 | } else { |
| 3835 | if (MatchIsOpZero) |
| 3836 | return new ShiftInst(cast<ShiftInst>(TI)->getOpcode(), MatchOp, NewSI); |
| 3837 | else |
| 3838 | return new ShiftInst(cast<ShiftInst>(TI)->getOpcode(), NewSI, MatchOp); |
| 3839 | } |
| 3840 | } |
| 3841 | |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 3842 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 3843 | Value *CondVal = SI.getCondition(); |
| 3844 | Value *TrueVal = SI.getTrueValue(); |
| 3845 | Value *FalseVal = SI.getFalseValue(); |
| 3846 | |
| 3847 | // select true, X, Y -> X |
| 3848 | // select false, X, Y -> Y |
| 3849 | if (ConstantBool *C = dyn_cast<ConstantBool>(CondVal)) |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 3850 | if (C == ConstantBool::True) |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 3851 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 3852 | else { |
| 3853 | assert(C == ConstantBool::False); |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 3854 | return ReplaceInstUsesWith(SI, FalseVal); |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 3855 | } |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 3856 | |
| 3857 | // select C, X, X -> X |
| 3858 | if (TrueVal == FalseVal) |
| 3859 | return ReplaceInstUsesWith(SI, TrueVal); |
| 3860 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3861 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 3862 | return ReplaceInstUsesWith(SI, FalseVal); |
| 3863 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 3864 | return ReplaceInstUsesWith(SI, TrueVal); |
| 3865 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 3866 | if (isa<Constant>(TrueVal)) |
| 3867 | return ReplaceInstUsesWith(SI, TrueVal); |
| 3868 | else |
| 3869 | return ReplaceInstUsesWith(SI, FalseVal); |
| 3870 | } |
| 3871 | |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 3872 | if (SI.getType() == Type::BoolTy) |
| 3873 | if (ConstantBool *C = dyn_cast<ConstantBool>(TrueVal)) { |
| 3874 | if (C == ConstantBool::True) { |
| 3875 | // Change: A = select B, true, C --> A = or B, C |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3876 | return BinaryOperator::createOr(CondVal, FalseVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 3877 | } else { |
| 3878 | // Change: A = select B, false, C --> A = and !B, C |
| 3879 | Value *NotCond = |
| 3880 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 3881 | "not."+CondVal->getName()), SI); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3882 | return BinaryOperator::createAnd(NotCond, FalseVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 3883 | } |
| 3884 | } else if (ConstantBool *C = dyn_cast<ConstantBool>(FalseVal)) { |
| 3885 | if (C == ConstantBool::False) { |
| 3886 | // Change: A = select B, C, false --> A = and B, C |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3887 | return BinaryOperator::createAnd(CondVal, TrueVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 3888 | } else { |
| 3889 | // Change: A = select B, C, true --> A = or !B, C |
| 3890 | Value *NotCond = |
| 3891 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 3892 | "not."+CondVal->getName()), SI); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3893 | return BinaryOperator::createOr(NotCond, TrueVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 3894 | } |
| 3895 | } |
| 3896 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3897 | // Selecting between two integer constants? |
| 3898 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) |
| 3899 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { |
| 3900 | // select C, 1, 0 -> cast C to int |
| 3901 | if (FalseValC->isNullValue() && TrueValC->getRawValue() == 1) { |
| 3902 | return new CastInst(CondVal, SI.getType()); |
| 3903 | } else if (TrueValC->isNullValue() && FalseValC->getRawValue() == 1) { |
| 3904 | // select C, 0, 1 -> cast !C to int |
| 3905 | Value *NotCond = |
| 3906 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
Chris Lattner | cf7baf3 | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 3907 | "not."+CondVal->getName()), SI); |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3908 | return new CastInst(NotCond, SI.getType()); |
Chris Lattner | cf7baf3 | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 3909 | } |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3910 | |
| 3911 | // If one of the constants is zero (we know they can't both be) and we |
| 3912 | // have a setcc instruction with zero, and we have an 'and' with the |
| 3913 | // non-constant value, eliminate this whole mess. This corresponds to |
| 3914 | // cases like this: ((X & 27) ? 27 : 0) |
| 3915 | if (TrueValC->isNullValue() || FalseValC->isNullValue()) |
| 3916 | if (Instruction *IC = dyn_cast<Instruction>(SI.getCondition())) |
| 3917 | if ((IC->getOpcode() == Instruction::SetEQ || |
| 3918 | IC->getOpcode() == Instruction::SetNE) && |
| 3919 | isa<ConstantInt>(IC->getOperand(1)) && |
| 3920 | cast<Constant>(IC->getOperand(1))->isNullValue()) |
| 3921 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) |
| 3922 | if (ICA->getOpcode() == Instruction::And && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3923 | isa<ConstantInt>(ICA->getOperand(1)) && |
| 3924 | (ICA->getOperand(1) == TrueValC || |
| 3925 | ICA->getOperand(1) == FalseValC) && |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3926 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { |
| 3927 | // Okay, now we know that everything is set up, we just don't |
| 3928 | // know whether we have a setne or seteq and whether the true or |
| 3929 | // false val is the zero. |
| 3930 | bool ShouldNotVal = !TrueValC->isNullValue(); |
| 3931 | ShouldNotVal ^= IC->getOpcode() == Instruction::SetNE; |
| 3932 | Value *V = ICA; |
| 3933 | if (ShouldNotVal) |
| 3934 | V = InsertNewInstBefore(BinaryOperator::create( |
| 3935 | Instruction::Xor, V, ICA->getOperand(1)), SI); |
| 3936 | return ReplaceInstUsesWith(SI, V); |
| 3937 | } |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 3938 | } |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 3939 | |
| 3940 | // See if we are selecting two values based on a comparison of the two values. |
| 3941 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(CondVal)) { |
| 3942 | if (SCI->getOperand(0) == TrueVal && SCI->getOperand(1) == FalseVal) { |
| 3943 | // Transform (X == Y) ? X : Y -> Y |
| 3944 | if (SCI->getOpcode() == Instruction::SetEQ) |
| 3945 | return ReplaceInstUsesWith(SI, FalseVal); |
| 3946 | // Transform (X != Y) ? X : Y -> X |
| 3947 | if (SCI->getOpcode() == Instruction::SetNE) |
| 3948 | return ReplaceInstUsesWith(SI, TrueVal); |
| 3949 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 3950 | |
| 3951 | } else if (SCI->getOperand(0) == FalseVal && SCI->getOperand(1) == TrueVal){ |
| 3952 | // Transform (X == Y) ? Y : X -> X |
| 3953 | if (SCI->getOpcode() == Instruction::SetEQ) |
Chris Lattner | 24cf020 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 3954 | return ReplaceInstUsesWith(SI, FalseVal); |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 3955 | // Transform (X != Y) ? Y : X -> Y |
| 3956 | if (SCI->getOpcode() == Instruction::SetNE) |
Chris Lattner | 24cf020 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 3957 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 3958 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 3959 | } |
| 3960 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3961 | |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 3962 | if (Instruction *TI = dyn_cast<Instruction>(TrueVal)) |
| 3963 | if (Instruction *FI = dyn_cast<Instruction>(FalseVal)) |
| 3964 | if (TI->hasOneUse() && FI->hasOneUse()) { |
| 3965 | bool isInverse = false; |
| 3966 | Instruction *AddOp = 0, *SubOp = 0; |
| 3967 | |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 3968 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) |
| 3969 | if (TI->getOpcode() == FI->getOpcode()) |
| 3970 | if (Instruction *IV = FoldSelectOpOp(SI, TI, FI)) |
| 3971 | return IV; |
| 3972 | |
| 3973 | // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is |
| 3974 | // even legal for FP. |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 3975 | if (TI->getOpcode() == Instruction::Sub && |
| 3976 | FI->getOpcode() == Instruction::Add) { |
| 3977 | AddOp = FI; SubOp = TI; |
| 3978 | } else if (FI->getOpcode() == Instruction::Sub && |
| 3979 | TI->getOpcode() == Instruction::Add) { |
| 3980 | AddOp = TI; SubOp = FI; |
| 3981 | } |
| 3982 | |
| 3983 | if (AddOp) { |
| 3984 | Value *OtherAddOp = 0; |
| 3985 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { |
| 3986 | OtherAddOp = AddOp->getOperand(1); |
| 3987 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { |
| 3988 | OtherAddOp = AddOp->getOperand(0); |
| 3989 | } |
| 3990 | |
| 3991 | if (OtherAddOp) { |
| 3992 | // So at this point we know we have: |
| 3993 | // select C, (add X, Y), (sub X, ?) |
| 3994 | // We can do the transform profitably if either 'Y' = '?' or '?' is |
| 3995 | // a constant. |
| 3996 | if (SubOp->getOperand(1) == AddOp || |
| 3997 | isa<Constant>(SubOp->getOperand(1))) { |
| 3998 | Value *NegVal; |
| 3999 | if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) { |
| 4000 | NegVal = ConstantExpr::getNeg(C); |
| 4001 | } else { |
| 4002 | NegVal = InsertNewInstBefore( |
| 4003 | BinaryOperator::createNeg(SubOp->getOperand(1)), SI); |
| 4004 | } |
| 4005 | |
Chris Lattner | 51726c4 | 2005-01-14 17:35:12 +0000 | [diff] [blame] | 4006 | Value *NewTrueOp = OtherAddOp; |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 4007 | Value *NewFalseOp = NegVal; |
| 4008 | if (AddOp != TI) |
| 4009 | std::swap(NewTrueOp, NewFalseOp); |
| 4010 | Instruction *NewSel = |
| 4011 | new SelectInst(CondVal, NewTrueOp,NewFalseOp,SI.getName()+".p"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4012 | |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 4013 | NewSel = InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 51726c4 | 2005-01-14 17:35:12 +0000 | [diff] [blame] | 4014 | return BinaryOperator::createAdd(SubOp->getOperand(0), NewSel); |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 4015 | } |
| 4016 | } |
| 4017 | } |
| 4018 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4019 | |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 4020 | // See if we can fold the select into one of our operands. |
| 4021 | if (SI.getType()->isInteger()) { |
| 4022 | // See the comment above GetSelectFoldableOperands for a description of the |
| 4023 | // transformation we are doing here. |
| 4024 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) |
| 4025 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && |
| 4026 | !isa<Constant>(FalseVal)) |
| 4027 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { |
| 4028 | unsigned OpToFold = 0; |
| 4029 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 4030 | OpToFold = 1; |
| 4031 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
| 4032 | OpToFold = 2; |
| 4033 | } |
| 4034 | |
| 4035 | if (OpToFold) { |
| 4036 | Constant *C = GetSelectFoldableConstant(TVI); |
| 4037 | std::string Name = TVI->getName(); TVI->setName(""); |
| 4038 | Instruction *NewSel = |
| 4039 | new SelectInst(SI.getCondition(), TVI->getOperand(2-OpToFold), C, |
| 4040 | Name); |
| 4041 | InsertNewInstBefore(NewSel, SI); |
| 4042 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) |
| 4043 | return BinaryOperator::create(BO->getOpcode(), FalseVal, NewSel); |
| 4044 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(TVI)) |
| 4045 | return new ShiftInst(SI->getOpcode(), FalseVal, NewSel); |
| 4046 | else { |
| 4047 | assert(0 && "Unknown instruction!!"); |
| 4048 | } |
| 4049 | } |
| 4050 | } |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4051 | |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 4052 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) |
| 4053 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && |
| 4054 | !isa<Constant>(TrueVal)) |
| 4055 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { |
| 4056 | unsigned OpToFold = 0; |
| 4057 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 4058 | OpToFold = 1; |
| 4059 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
| 4060 | OpToFold = 2; |
| 4061 | } |
| 4062 | |
| 4063 | if (OpToFold) { |
| 4064 | Constant *C = GetSelectFoldableConstant(FVI); |
| 4065 | std::string Name = FVI->getName(); FVI->setName(""); |
| 4066 | Instruction *NewSel = |
| 4067 | new SelectInst(SI.getCondition(), C, FVI->getOperand(2-OpToFold), |
| 4068 | Name); |
| 4069 | InsertNewInstBefore(NewSel, SI); |
| 4070 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) |
| 4071 | return BinaryOperator::create(BO->getOpcode(), TrueVal, NewSel); |
| 4072 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(FVI)) |
| 4073 | return new ShiftInst(SI->getOpcode(), TrueVal, NewSel); |
| 4074 | else { |
| 4075 | assert(0 && "Unknown instruction!!"); |
| 4076 | } |
| 4077 | } |
| 4078 | } |
| 4079 | } |
Chris Lattner | d6f636a | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 4080 | |
| 4081 | if (BinaryOperator::isNot(CondVal)) { |
| 4082 | SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); |
| 4083 | SI.setOperand(1, FalseVal); |
| 4084 | SI.setOperand(2, TrueVal); |
| 4085 | return &SI; |
| 4086 | } |
| 4087 | |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 4088 | return 0; |
| 4089 | } |
| 4090 | |
| 4091 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4092 | // CallInst simplification |
| 4093 | // |
| 4094 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 4095 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 4096 | // visitCallSite. |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 4097 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(&CI)) { |
| 4098 | bool Changed = false; |
| 4099 | |
| 4100 | // memmove/cpy/set of zero bytes is a noop. |
| 4101 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { |
| 4102 | if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); |
| 4103 | |
| 4104 | // FIXME: Increase alignment here. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4105 | |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 4106 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
| 4107 | if (CI->getRawValue() == 1) { |
| 4108 | // Replace the instruction with just byte operations. We would |
| 4109 | // transform other cases to loads/stores, but we don't know if |
| 4110 | // alignment is sufficient. |
| 4111 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 4112 | } |
| 4113 | |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 4114 | // If we have a memmove and the source operation is a constant global, |
| 4115 | // then the source and dest pointers can't alias, so we can change this |
| 4116 | // into a call to memcpy. |
| 4117 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) |
| 4118 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
| 4119 | if (GVSrc->isConstant()) { |
| 4120 | Module *M = CI.getParent()->getParent()->getParent(); |
| 4121 | Function *MemCpy = M->getOrInsertFunction("llvm.memcpy", |
| 4122 | CI.getCalledFunction()->getFunctionType()); |
| 4123 | CI.setOperand(0, MemCpy); |
| 4124 | Changed = true; |
| 4125 | } |
| 4126 | |
| 4127 | if (Changed) return &CI; |
Chris Lattner | 9530754 | 2004-11-18 21:41:39 +0000 | [diff] [blame] | 4128 | } else if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(&CI)) { |
| 4129 | // If this stoppoint is at the same source location as the previous |
| 4130 | // stoppoint in the chain, it is not needed. |
| 4131 | if (DbgStopPointInst *PrevSPI = |
| 4132 | dyn_cast<DbgStopPointInst>(SPI->getChain())) |
| 4133 | if (SPI->getLineNo() == PrevSPI->getLineNo() && |
| 4134 | SPI->getColNo() == PrevSPI->getColNo()) { |
| 4135 | SPI->replaceAllUsesWith(PrevSPI); |
| 4136 | return EraseInstFromFunction(CI); |
| 4137 | } |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 4138 | } |
| 4139 | |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 4140 | return visitCallSite(&CI); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4141 | } |
| 4142 | |
| 4143 | // InvokeInst simplification |
| 4144 | // |
| 4145 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 4146 | return visitCallSite(&II); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 4149 | // visitCallSite - Improvements for call and invoke instructions. |
| 4150 | // |
| 4151 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 4152 | bool Changed = false; |
| 4153 | |
| 4154 | // If the callee is a constexpr cast of a function, attempt to move the cast |
| 4155 | // to the arguments of the call/invoke. |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 4156 | if (transformConstExprCastCall(CS)) return 0; |
| 4157 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 4158 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4159 | |
Chris Lattner | 61d9d81 | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 4160 | if (Function *CalleeF = dyn_cast<Function>(Callee)) |
| 4161 | if (CalleeF->getCallingConv() != CS.getCallingConv()) { |
| 4162 | Instruction *OldCall = CS.getInstruction(); |
| 4163 | // If the call and callee calling conventions don't match, this call must |
| 4164 | // be unreachable, as the call is undefined. |
| 4165 | new StoreInst(ConstantBool::True, |
| 4166 | UndefValue::get(PointerType::get(Type::BoolTy)), OldCall); |
| 4167 | if (!OldCall->use_empty()) |
| 4168 | OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); |
| 4169 | if (isa<CallInst>(OldCall)) // Not worth removing an invoke here. |
| 4170 | return EraseInstFromFunction(*OldCall); |
| 4171 | return 0; |
| 4172 | } |
| 4173 | |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 4174 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
| 4175 | // This instruction is not reachable, just remove it. We insert a store to |
| 4176 | // undef so that we know that this code is not reachable, despite the fact |
| 4177 | // that we can't modify the CFG here. |
| 4178 | new StoreInst(ConstantBool::True, |
| 4179 | UndefValue::get(PointerType::get(Type::BoolTy)), |
| 4180 | CS.getInstruction()); |
| 4181 | |
| 4182 | if (!CS.getInstruction()->use_empty()) |
| 4183 | CS.getInstruction()-> |
| 4184 | replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); |
| 4185 | |
| 4186 | if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { |
| 4187 | // Don't break the CFG, insert a dummy cond branch. |
| 4188 | new BranchInst(II->getNormalDest(), II->getUnwindDest(), |
| 4189 | ConstantBool::True, II); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4190 | } |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 4191 | return EraseInstFromFunction(*CS.getInstruction()); |
| 4192 | } |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4193 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 4194 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 4195 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 4196 | if (FTy->isVarArg()) { |
| 4197 | // See if we can optimize any arguments passed through the varargs area of |
| 4198 | // the call. |
| 4199 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
| 4200 | E = CS.arg_end(); I != E; ++I) |
| 4201 | if (CastInst *CI = dyn_cast<CastInst>(*I)) { |
| 4202 | // If this cast does not effect the value passed through the varargs |
| 4203 | // area, we can eliminate the use of the cast. |
| 4204 | Value *Op = CI->getOperand(0); |
| 4205 | if (CI->getType()->isLosslesslyConvertibleTo(Op->getType())) { |
| 4206 | *I = Op; |
| 4207 | Changed = true; |
| 4208 | } |
| 4209 | } |
| 4210 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4211 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 4212 | return Changed ? CS.getInstruction() : 0; |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 4213 | } |
| 4214 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4215 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 4216 | // attempt to move the cast to the arguments of the call/invoke. |
| 4217 | // |
| 4218 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 4219 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 4220 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
Chris Lattner | f3edc49 | 2004-07-18 18:59:44 +0000 | [diff] [blame] | 4221 | if (CE->getOpcode() != Instruction::Cast || !isa<Function>(CE->getOperand(0))) |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4222 | return false; |
Reid Spencer | 8743687 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 4223 | Function *Callee = cast<Function>(CE->getOperand(0)); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4224 | Instruction *Caller = CS.getInstruction(); |
| 4225 | |
| 4226 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 4227 | // would cause a type conversion of one of our arguments, change this call to |
| 4228 | // be a direct call with arguments casted to the appropriate types. |
| 4229 | // |
| 4230 | const FunctionType *FT = Callee->getFunctionType(); |
| 4231 | const Type *OldRetTy = Caller->getType(); |
| 4232 | |
Chris Lattner | 1f7942f | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 4233 | // Check to see if we are changing the return type... |
| 4234 | if (OldRetTy != FT->getReturnType()) { |
| 4235 | if (Callee->isExternal() && |
| 4236 | !OldRetTy->isLosslesslyConvertibleTo(FT->getReturnType()) && |
| 4237 | !Caller->use_empty()) |
| 4238 | return false; // Cannot transform this return value... |
| 4239 | |
| 4240 | // If the callsite is an invoke instruction, and the return value is used by |
| 4241 | // a PHI node in a successor, we cannot change the return type of the call |
| 4242 | // because there is no place to put the cast instruction (without breaking |
| 4243 | // the critical edge). Bail out in this case. |
| 4244 | if (!Caller->use_empty()) |
| 4245 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 4246 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 4247 | UI != E; ++UI) |
| 4248 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 4249 | if (PN->getParent() == II->getNormalDest() || |
Chris Lattner | fae8ab3 | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 4250 | PN->getParent() == II->getUnwindDest()) |
Chris Lattner | 1f7942f | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 4251 | return false; |
| 4252 | } |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4253 | |
| 4254 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 4255 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4256 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4257 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 4258 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 4259 | const Type *ParamTy = FT->getParamType(i); |
| 4260 | bool isConvertible = (*AI)->getType()->isLosslesslyConvertibleTo(ParamTy); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4261 | if (Callee->isExternal() && !isConvertible) return false; |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4262 | } |
| 4263 | |
| 4264 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
| 4265 | Callee->isExternal()) |
| 4266 | return false; // Do not delete arguments unless we have a function body... |
| 4267 | |
| 4268 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 4269 | // inserting cast instructions as necessary... |
| 4270 | std::vector<Value*> Args; |
| 4271 | Args.reserve(NumActualArgs); |
| 4272 | |
| 4273 | AI = CS.arg_begin(); |
| 4274 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 4275 | const Type *ParamTy = FT->getParamType(i); |
| 4276 | if ((*AI)->getType() == ParamTy) { |
| 4277 | Args.push_back(*AI); |
| 4278 | } else { |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 4279 | Args.push_back(InsertNewInstBefore(new CastInst(*AI, ParamTy, "tmp"), |
| 4280 | *Caller)); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4281 | } |
| 4282 | } |
| 4283 | |
| 4284 | // If the function takes more arguments than the call was taking, add them |
| 4285 | // now... |
| 4286 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 4287 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 4288 | |
| 4289 | // If we are removing arguments to the function, emit an obnoxious warning... |
| 4290 | if (FT->getNumParams() < NumActualArgs) |
| 4291 | if (!FT->isVarArg()) { |
| 4292 | std::cerr << "WARNING: While resolving call to function '" |
| 4293 | << Callee->getName() << "' arguments were dropped!\n"; |
| 4294 | } else { |
| 4295 | // Add all of the arguments in their promoted form to the arg list... |
| 4296 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 4297 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 4298 | if (PTy != (*AI)->getType()) { |
| 4299 | // Must promote to pass through va_arg area! |
| 4300 | Instruction *Cast = new CastInst(*AI, PTy, "tmp"); |
| 4301 | InsertNewInstBefore(Cast, *Caller); |
| 4302 | Args.push_back(Cast); |
| 4303 | } else { |
| 4304 | Args.push_back(*AI); |
| 4305 | } |
| 4306 | } |
| 4307 | } |
| 4308 | |
| 4309 | if (FT->getReturnType() == Type::VoidTy) |
| 4310 | Caller->setName(""); // Void type should not have a name... |
| 4311 | |
| 4312 | Instruction *NC; |
| 4313 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Chris Lattner | fae8ab3 | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 4314 | NC = new InvokeInst(Callee, II->getNormalDest(), II->getUnwindDest(), |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4315 | Args, Caller->getName(), Caller); |
Chris Lattner | 05c703e | 2005-05-14 12:25:32 +0000 | [diff] [blame] | 4316 | cast<InvokeInst>(II)->setCallingConv(II->getCallingConv()); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4317 | } else { |
| 4318 | NC = new CallInst(Callee, Args, Caller->getName(), Caller); |
Chris Lattner | 6aacb0f | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 4319 | if (cast<CallInst>(Caller)->isTailCall()) |
| 4320 | cast<CallInst>(NC)->setTailCall(); |
Chris Lattner | 05c703e | 2005-05-14 12:25:32 +0000 | [diff] [blame] | 4321 | cast<CallInst>(NC)->setCallingConv(cast<CallInst>(Caller)->getCallingConv()); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4322 | } |
| 4323 | |
| 4324 | // Insert a cast of the return type as necessary... |
| 4325 | Value *NV = NC; |
| 4326 | if (Caller->getType() != NV->getType() && !Caller->use_empty()) { |
| 4327 | if (NV->getType() != Type::VoidTy) { |
| 4328 | NV = NC = new CastInst(NC, Caller->getType(), "tmp"); |
Chris Lattner | 686767f | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 4329 | |
| 4330 | // If this is an invoke instruction, we should insert it after the first |
| 4331 | // non-phi, instruction in the normal successor block. |
| 4332 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 4333 | BasicBlock::iterator I = II->getNormalDest()->begin(); |
| 4334 | while (isa<PHINode>(I)) ++I; |
| 4335 | InsertNewInstBefore(NC, *I); |
| 4336 | } else { |
| 4337 | // Otherwise, it's a call, just insert cast right after the call instr |
| 4338 | InsertNewInstBefore(NC, *Caller); |
| 4339 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 4340 | AddUsersToWorkList(*Caller); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4341 | } else { |
Chris Lattner | e29d634 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 4342 | NV = UndefValue::get(Caller->getType()); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4343 | } |
| 4344 | } |
| 4345 | |
| 4346 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 4347 | Caller->replaceAllUsesWith(NV); |
| 4348 | Caller->getParent()->getInstList().erase(Caller); |
| 4349 | removeFromWorkList(Caller); |
| 4350 | return true; |
| 4351 | } |
| 4352 | |
| 4353 | |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4354 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 4355 | // operator and they all are only used by the PHI, PHI together their |
| 4356 | // inputs, and do the operation once, to the result of the PHI. |
| 4357 | Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { |
| 4358 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
| 4359 | |
| 4360 | // Scan the instruction, looking for input operations that can be folded away. |
| 4361 | // If all input operands to the phi are the same instruction (e.g. a cast from |
| 4362 | // the same type or "+42") we can pull the operation through the PHI, reducing |
| 4363 | // code size and simplifying code. |
| 4364 | Constant *ConstantOp = 0; |
| 4365 | const Type *CastSrcTy = 0; |
| 4366 | if (isa<CastInst>(FirstInst)) { |
| 4367 | CastSrcTy = FirstInst->getOperand(0)->getType(); |
| 4368 | } else if (isa<BinaryOperator>(FirstInst) || isa<ShiftInst>(FirstInst)) { |
| 4369 | // Can fold binop or shift if the RHS is a constant. |
| 4370 | ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1)); |
| 4371 | if (ConstantOp == 0) return 0; |
| 4372 | } else { |
| 4373 | return 0; // Cannot fold this operation. |
| 4374 | } |
| 4375 | |
| 4376 | // Check to see if all arguments are the same operation. |
| 4377 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 4378 | if (!isa<Instruction>(PN.getIncomingValue(i))) return 0; |
| 4379 | Instruction *I = cast<Instruction>(PN.getIncomingValue(i)); |
| 4380 | if (!I->hasOneUse() || I->getOpcode() != FirstInst->getOpcode()) |
| 4381 | return 0; |
| 4382 | if (CastSrcTy) { |
| 4383 | if (I->getOperand(0)->getType() != CastSrcTy) |
| 4384 | return 0; // Cast operation must match. |
| 4385 | } else if (I->getOperand(1) != ConstantOp) { |
| 4386 | return 0; |
| 4387 | } |
| 4388 | } |
| 4389 | |
| 4390 | // Okay, they are all the same operation. Create a new PHI node of the |
| 4391 | // correct type, and PHI together all of the LHS's of the instructions. |
| 4392 | PHINode *NewPN = new PHINode(FirstInst->getOperand(0)->getType(), |
| 4393 | PN.getName()+".in"); |
Chris Lattner | d8e2018 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 4394 | NewPN->reserveOperandSpace(PN.getNumOperands()/2); |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 4395 | |
| 4396 | Value *InVal = FirstInst->getOperand(0); |
| 4397 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4398 | |
| 4399 | // Add all operands to the new PHI. |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 4400 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 4401 | Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 4402 | if (NewInVal != InVal) |
| 4403 | InVal = 0; |
| 4404 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); |
| 4405 | } |
| 4406 | |
| 4407 | Value *PhiVal; |
| 4408 | if (InVal) { |
| 4409 | // The new PHI unions all of the same values together. This is really |
| 4410 | // common, so we handle it intelligently here for compile-time speed. |
| 4411 | PhiVal = InVal; |
| 4412 | delete NewPN; |
| 4413 | } else { |
| 4414 | InsertNewInstBefore(NewPN, PN); |
| 4415 | PhiVal = NewPN; |
| 4416 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4417 | |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4418 | // Insert and return the new operation. |
| 4419 | if (isa<CastInst>(FirstInst)) |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 4420 | return new CastInst(PhiVal, PN.getType()); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4421 | else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 4422 | return BinaryOperator::create(BinOp->getOpcode(), PhiVal, ConstantOp); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4423 | else |
| 4424 | return new ShiftInst(cast<ShiftInst>(FirstInst)->getOpcode(), |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 4425 | PhiVal, ConstantOp); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4426 | } |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4427 | |
Chris Lattner | 7153643 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 4428 | /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle |
| 4429 | /// that is dead. |
| 4430 | static bool DeadPHICycle(PHINode *PN, std::set<PHINode*> &PotentiallyDeadPHIs) { |
| 4431 | if (PN->use_empty()) return true; |
| 4432 | if (!PN->hasOneUse()) return false; |
| 4433 | |
| 4434 | // Remember this node, and if we find the cycle, return. |
| 4435 | if (!PotentiallyDeadPHIs.insert(PN).second) |
| 4436 | return true; |
| 4437 | |
| 4438 | if (PHINode *PU = dyn_cast<PHINode>(PN->use_back())) |
| 4439 | return DeadPHICycle(PU, PotentiallyDeadPHIs); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4440 | |
Chris Lattner | 7153643 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 4441 | return false; |
| 4442 | } |
| 4443 | |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 4444 | // PHINode simplification |
| 4445 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4446 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Chris Lattner | 9f9c260 | 2005-08-05 01:04:30 +0000 | [diff] [blame] | 4447 | if (Value *V = PN.hasConstantValue()) |
| 4448 | return ReplaceInstUsesWith(PN, V); |
Chris Lattner | 4db2d22 | 2004-02-16 05:07:08 +0000 | [diff] [blame] | 4449 | |
| 4450 | // If the only user of this instruction is a cast instruction, and all of the |
| 4451 | // incoming values are constants, change this PHI to merge together the casted |
| 4452 | // constants. |
| 4453 | if (PN.hasOneUse()) |
| 4454 | if (CastInst *CI = dyn_cast<CastInst>(PN.use_back())) |
| 4455 | if (CI->getType() != PN.getType()) { // noop casts will be folded |
| 4456 | bool AllConstant = true; |
| 4457 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 4458 | if (!isa<Constant>(PN.getIncomingValue(i))) { |
| 4459 | AllConstant = false; |
| 4460 | break; |
| 4461 | } |
| 4462 | if (AllConstant) { |
| 4463 | // Make a new PHI with all casted values. |
| 4464 | PHINode *New = new PHINode(CI->getType(), PN.getName(), &PN); |
| 4465 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 4466 | Constant *OldArg = cast<Constant>(PN.getIncomingValue(i)); |
| 4467 | New->addIncoming(ConstantExpr::getCast(OldArg, New->getType()), |
| 4468 | PN.getIncomingBlock(i)); |
| 4469 | } |
| 4470 | |
| 4471 | // Update the cast instruction. |
| 4472 | CI->setOperand(0, New); |
| 4473 | WorkList.push_back(CI); // revisit the cast instruction to fold. |
| 4474 | WorkList.push_back(New); // Make sure to revisit the new Phi |
| 4475 | return &PN; // PN is now dead! |
| 4476 | } |
| 4477 | } |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4478 | |
| 4479 | // If all PHI operands are the same operation, pull them through the PHI, |
| 4480 | // reducing code size. |
| 4481 | if (isa<Instruction>(PN.getIncomingValue(0)) && |
| 4482 | PN.getIncomingValue(0)->hasOneUse()) |
| 4483 | if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) |
| 4484 | return Result; |
| 4485 | |
Chris Lattner | 7153643 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 4486 | // If this is a trivial cycle in the PHI node graph, remove it. Basically, if |
| 4487 | // this PHI only has a single use (a PHI), and if that PHI only has one use (a |
| 4488 | // PHI)... break the cycle. |
| 4489 | if (PN.hasOneUse()) |
| 4490 | if (PHINode *PU = dyn_cast<PHINode>(PN.use_back())) { |
| 4491 | std::set<PHINode*> PotentiallyDeadPHIs; |
| 4492 | PotentiallyDeadPHIs.insert(&PN); |
| 4493 | if (DeadPHICycle(PU, PotentiallyDeadPHIs)) |
| 4494 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 4495 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4496 | |
Chris Lattner | 91daeb5 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 4497 | return 0; |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 4498 | } |
| 4499 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4500 | static Value *InsertSignExtendToPtrTy(Value *V, const Type *DTy, |
| 4501 | Instruction *InsertPoint, |
| 4502 | InstCombiner *IC) { |
| 4503 | unsigned PS = IC->getTargetData().getPointerSize(); |
| 4504 | const Type *VTy = V->getType(); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4505 | if (!VTy->isSigned() && VTy->getPrimitiveSize() < PS) |
| 4506 | // We must insert a cast to ensure we sign-extend. |
| 4507 | V = IC->InsertNewInstBefore(new CastInst(V, VTy->getSignedVersion(), |
| 4508 | V->getName()), *InsertPoint); |
| 4509 | return IC->InsertNewInstBefore(new CastInst(V, DTy, V->getName()), |
| 4510 | *InsertPoint); |
| 4511 | } |
| 4512 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4513 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4514 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4515 | Value *PtrOp = GEP.getOperand(0); |
Chris Lattner | 471bd76 | 2003-05-22 19:07:21 +0000 | [diff] [blame] | 4516 | // Is it 'getelementptr %P, long 0' or 'getelementptr %P' |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4517 | // If so, eliminate the noop. |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 4518 | if (GEP.getNumOperands() == 1) |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4519 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 4520 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4521 | if (isa<UndefValue>(GEP.getOperand(0))) |
| 4522 | return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType())); |
| 4523 | |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 4524 | bool HasZeroPointerIndex = false; |
| 4525 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) |
| 4526 | HasZeroPointerIndex = C->isNullValue(); |
| 4527 | |
| 4528 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4529 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4530 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4531 | // Eliminate unneeded casts for indices. |
| 4532 | bool MadeChange = false; |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 4533 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 4534 | for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI) |
| 4535 | if (isa<SequentialType>(*GTI)) { |
| 4536 | if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) { |
| 4537 | Value *Src = CI->getOperand(0); |
| 4538 | const Type *SrcTy = Src->getType(); |
| 4539 | const Type *DestTy = CI->getType(); |
| 4540 | if (Src->getType()->isInteger()) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4541 | if (SrcTy->getPrimitiveSizeInBits() == |
| 4542 | DestTy->getPrimitiveSizeInBits()) { |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 4543 | // We can always eliminate a cast from ulong or long to the other. |
| 4544 | // We can always eliminate a cast from uint to int or the other on |
| 4545 | // 32-bit pointer platforms. |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4546 | if (DestTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()){ |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 4547 | MadeChange = true; |
| 4548 | GEP.setOperand(i, Src); |
| 4549 | } |
| 4550 | } else if (SrcTy->getPrimitiveSize() < DestTy->getPrimitiveSize() && |
| 4551 | SrcTy->getPrimitiveSize() == 4) { |
| 4552 | // We can always eliminate a cast from int to [u]long. We can |
| 4553 | // eliminate a cast from uint to [u]long iff the target is a 32-bit |
| 4554 | // pointer target. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4555 | if (SrcTy->isSigned() || |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4556 | SrcTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()) { |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 4557 | MadeChange = true; |
| 4558 | GEP.setOperand(i, Src); |
| 4559 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4560 | } |
| 4561 | } |
| 4562 | } |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 4563 | // If we are using a wider index than needed for this platform, shrink it |
| 4564 | // to what we need. If the incoming value needs a cast instruction, |
| 4565 | // insert it. This explicit cast can make subsequent optimizations more |
| 4566 | // obvious. |
| 4567 | Value *Op = GEP.getOperand(i); |
| 4568 | if (Op->getType()->getPrimitiveSize() > TD->getPointerSize()) |
Chris Lattner | 1e9ac1a | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 4569 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | 44d0b95 | 2004-07-20 01:48:15 +0000 | [diff] [blame] | 4570 | GEP.setOperand(i, ConstantExpr::getCast(C, |
| 4571 | TD->getIntPtrType()->getSignedVersion())); |
Chris Lattner | 1e9ac1a | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 4572 | MadeChange = true; |
| 4573 | } else { |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 4574 | Op = InsertNewInstBefore(new CastInst(Op, TD->getIntPtrType(), |
| 4575 | Op->getName()), GEP); |
| 4576 | GEP.setOperand(i, Op); |
| 4577 | MadeChange = true; |
| 4578 | } |
Chris Lattner | 44d0b95 | 2004-07-20 01:48:15 +0000 | [diff] [blame] | 4579 | |
| 4580 | // If this is a constant idx, make sure to canonicalize it to be a signed |
| 4581 | // operand, otherwise CSE and other optimizations are pessimized. |
| 4582 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op)) { |
| 4583 | GEP.setOperand(i, ConstantExpr::getCast(CUI, |
| 4584 | CUI->getType()->getSignedVersion())); |
| 4585 | MadeChange = true; |
| 4586 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4587 | } |
| 4588 | if (MadeChange) return &GEP; |
| 4589 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 4590 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 4591 | // is a getelementptr instruction, combine the indices of the two |
| 4592 | // getelementptr instructions into a single instruction. |
| 4593 | // |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 4594 | std::vector<Value*> SrcGEPOperands; |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4595 | if (User *Src = dyn_castGetElementPtr(PtrOp)) |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 4596 | SrcGEPOperands.assign(Src->op_begin(), Src->op_end()); |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 4597 | |
| 4598 | if (!SrcGEPOperands.empty()) { |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4599 | // Note that if our source is a gep chain itself that we wait for that |
| 4600 | // chain to be resolved before we perform this transformation. This |
| 4601 | // avoids us creating a TON of code in some cases. |
| 4602 | // |
| 4603 | if (isa<GetElementPtrInst>(SrcGEPOperands[0]) && |
| 4604 | cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2) |
| 4605 | return 0; // Wait until our source is folded to completion. |
| 4606 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 4607 | std::vector<Value *> Indices; |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4608 | |
| 4609 | // Find out whether the last index in the source GEP is a sequential idx. |
| 4610 | bool EndsWithSequential = false; |
| 4611 | for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)), |
| 4612 | E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I) |
Chris Lattner | 8ec5f88 | 2004-05-08 22:41:42 +0000 | [diff] [blame] | 4613 | EndsWithSequential = !isa<StructType>(*I); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4614 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 4615 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4616 | if (EndsWithSequential) { |
Chris Lattner | 235af56 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 4617 | // Replace: gep (gep %P, long B), long A, ... |
| 4618 | // With: T = long A+B; gep %P, T, ... |
| 4619 | // |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4620 | Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4621 | if (SO1 == Constant::getNullValue(SO1->getType())) { |
| 4622 | Sum = GO1; |
| 4623 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { |
| 4624 | Sum = SO1; |
| 4625 | } else { |
| 4626 | // If they aren't the same type, convert both to an integer of the |
| 4627 | // target's pointer size. |
| 4628 | if (SO1->getType() != GO1->getType()) { |
| 4629 | if (Constant *SO1C = dyn_cast<Constant>(SO1)) { |
| 4630 | SO1 = ConstantExpr::getCast(SO1C, GO1->getType()); |
| 4631 | } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) { |
| 4632 | GO1 = ConstantExpr::getCast(GO1C, SO1->getType()); |
| 4633 | } else { |
| 4634 | unsigned PS = TD->getPointerSize(); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4635 | if (SO1->getType()->getPrimitiveSize() == PS) { |
| 4636 | // Convert GO1 to SO1's type. |
| 4637 | GO1 = InsertSignExtendToPtrTy(GO1, SO1->getType(), &GEP, this); |
| 4638 | |
| 4639 | } else if (GO1->getType()->getPrimitiveSize() == PS) { |
| 4640 | // Convert SO1 to GO1's type. |
| 4641 | SO1 = InsertSignExtendToPtrTy(SO1, GO1->getType(), &GEP, this); |
| 4642 | } else { |
| 4643 | const Type *PT = TD->getIntPtrType(); |
| 4644 | SO1 = InsertSignExtendToPtrTy(SO1, PT, &GEP, this); |
| 4645 | GO1 = InsertSignExtendToPtrTy(GO1, PT, &GEP, this); |
| 4646 | } |
| 4647 | } |
| 4648 | } |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4649 | if (isa<Constant>(SO1) && isa<Constant>(GO1)) |
| 4650 | Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1)); |
| 4651 | else { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4652 | Sum = BinaryOperator::createAdd(SO1, GO1, PtrOp->getName()+".sum"); |
| 4653 | InsertNewInstBefore(cast<Instruction>(Sum), GEP); |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4654 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4655 | } |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4656 | |
| 4657 | // Recycle the GEP we already have if possible. |
| 4658 | if (SrcGEPOperands.size() == 2) { |
| 4659 | GEP.setOperand(0, SrcGEPOperands[0]); |
| 4660 | GEP.setOperand(1, Sum); |
| 4661 | return &GEP; |
| 4662 | } else { |
| 4663 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 4664 | SrcGEPOperands.end()-1); |
| 4665 | Indices.push_back(Sum); |
| 4666 | Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end()); |
| 4667 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4668 | } else if (isa<Constant>(*GEP.idx_begin()) && |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4669 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4670 | SrcGEPOperands.size() != 1) { |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 4671 | // 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] | 4672 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 4673 | SrcGEPOperands.end()); |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 4674 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
| 4675 | } |
| 4676 | |
| 4677 | if (!Indices.empty()) |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 4678 | return new GetElementPtrInst(SrcGEPOperands[0], Indices, GEP.getName()); |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 4679 | |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4680 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) { |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 4681 | // GEP of global variable. If all of the indices for this GEP are |
| 4682 | // constants, we can promote this to a constexpr instead of an instruction. |
| 4683 | |
| 4684 | // Scan for nonconstants... |
| 4685 | std::vector<Constant*> Indices; |
| 4686 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
| 4687 | for (; I != E && isa<Constant>(*I); ++I) |
| 4688 | Indices.push_back(cast<Constant>(*I)); |
| 4689 | |
| 4690 | if (I == E) { // If they are all constants... |
Chris Lattner | f3edc49 | 2004-07-18 18:59:44 +0000 | [diff] [blame] | 4691 | Constant *CE = ConstantExpr::getGetElementPtr(GV, Indices); |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 4692 | |
| 4693 | // Replace all uses of the GEP with the new constexpr... |
| 4694 | return ReplaceInstUsesWith(GEP, CE); |
| 4695 | } |
Chris Lattner | 567b81f | 2005-09-13 00:40:14 +0000 | [diff] [blame^] | 4696 | } else if (Value *X = isCast(PtrOp)) { // Is the operand a cast? |
| 4697 | if (!isa<PointerType>(X->getType())) { |
| 4698 | // Not interesting. Source pointer must be a cast from pointer. |
| 4699 | } else if (HasZeroPointerIndex) { |
| 4700 | // transform: GEP (cast [10 x ubyte]* X to [0 x ubyte]*), long 0, ... |
| 4701 | // into : GEP [10 x ubyte]* X, long 0, ... |
| 4702 | // |
| 4703 | // This occurs when the program declares an array extern like "int X[];" |
| 4704 | // |
| 4705 | const PointerType *CPTy = cast<PointerType>(PtrOp->getType()); |
| 4706 | const PointerType *XTy = cast<PointerType>(X->getType()); |
| 4707 | if (const ArrayType *XATy = |
| 4708 | dyn_cast<ArrayType>(XTy->getElementType())) |
| 4709 | if (const ArrayType *CATy = |
| 4710 | dyn_cast<ArrayType>(CPTy->getElementType())) |
| 4711 | if (CATy->getElementType() == XATy->getElementType()) { |
| 4712 | // At this point, we know that the cast source type is a pointer |
| 4713 | // to an array of the same type as the destination pointer |
| 4714 | // array. Because the array type is never stepped over (there |
| 4715 | // is a leading zero) we can fold the cast into this GEP. |
| 4716 | GEP.setOperand(0, X); |
| 4717 | return &GEP; |
| 4718 | } |
| 4719 | } else if (GEP.getNumOperands() == 2) { |
| 4720 | // Transform things like: |
| 4721 | // %t = getelementptr ubyte* cast ([2 x sbyte]* %str to ubyte*), uint %V |
| 4722 | // into: %t1 = getelementptr [2 x sbyte*]* %str, int 0, uint %V; cast |
| 4723 | const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); |
| 4724 | const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType(); |
| 4725 | if (isa<ArrayType>(SrcElTy) && |
| 4726 | TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()) == |
| 4727 | TD->getTypeSize(ResElTy)) { |
| 4728 | Value *V = InsertNewInstBefore( |
| 4729 | new GetElementPtrInst(X, Constant::getNullValue(Type::IntTy), |
| 4730 | GEP.getOperand(1), GEP.getName()), GEP); |
| 4731 | return new CastInst(V, GEP.getType()); |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 4732 | } |
| 4733 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 4734 | } |
| 4735 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 4736 | return 0; |
| 4737 | } |
| 4738 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 4739 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
| 4740 | // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1 |
| 4741 | if (AI.isArrayAllocation()) // Check C != 1 |
| 4742 | if (const ConstantUInt *C = dyn_cast<ConstantUInt>(AI.getArraySize())) { |
| 4743 | const Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getValue()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 4744 | AllocationInst *New = 0; |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 4745 | |
| 4746 | // Create and insert the replacement instruction... |
| 4747 | if (isa<MallocInst>(AI)) |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 4748 | New = new MallocInst(NewTy, 0, AI.getName()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 4749 | else { |
| 4750 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 4751 | New = new AllocaInst(NewTy, 0, AI.getName()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 4752 | } |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 4753 | |
| 4754 | InsertNewInstBefore(New, AI); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4755 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 4756 | // Scan to the end of the allocation instructions, to skip over a block of |
| 4757 | // allocas if possible... |
| 4758 | // |
| 4759 | BasicBlock::iterator It = New; |
| 4760 | while (isa<AllocationInst>(*It)) ++It; |
| 4761 | |
| 4762 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 4763 | // insert our getelementptr instruction... |
| 4764 | // |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 4765 | Value *NullIdx = Constant::getNullValue(Type::IntTy); |
| 4766 | Value *V = new GetElementPtrInst(New, NullIdx, NullIdx, |
| 4767 | New->getName()+".sub", It); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 4768 | |
| 4769 | // Now make everything use the getelementptr instead of the original |
| 4770 | // allocation. |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 4771 | return ReplaceInstUsesWith(AI, V); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4772 | } else if (isa<UndefValue>(AI.getArraySize())) { |
| 4773 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 4774 | } |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 4775 | |
| 4776 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. |
| 4777 | // Note that we only do this for alloca's, because malloc should allocate and |
| 4778 | // return a unique pointer, even for a zero byte allocation. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4779 | if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() && |
Chris Lattner | 49df6ce | 2004-07-02 22:55:47 +0000 | [diff] [blame] | 4780 | TD->getTypeSize(AI.getAllocatedType()) == 0) |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 4781 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
| 4782 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 4783 | return 0; |
| 4784 | } |
| 4785 | |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 4786 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { |
| 4787 | Value *Op = FI.getOperand(0); |
| 4788 | |
| 4789 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X |
| 4790 | if (CastInst *CI = dyn_cast<CastInst>(Op)) |
| 4791 | if (isa<PointerType>(CI->getOperand(0)->getType())) { |
| 4792 | FI.setOperand(0, CI->getOperand(0)); |
| 4793 | return &FI; |
| 4794 | } |
| 4795 | |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 4796 | // free undef -> unreachable. |
| 4797 | if (isa<UndefValue>(Op)) { |
| 4798 | // Insert a new store to null because we cannot modify the CFG here. |
| 4799 | new StoreInst(ConstantBool::True, |
| 4800 | UndefValue::get(PointerType::get(Type::BoolTy)), &FI); |
| 4801 | return EraseInstFromFunction(FI); |
| 4802 | } |
| 4803 | |
Chris Lattner | f3a3660 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 4804 | // If we have 'free null' delete the instruction. This can happen in stl code |
| 4805 | // when lots of inlining happens. |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 4806 | if (isa<ConstantPointerNull>(Op)) |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 4807 | return EraseInstFromFunction(FI); |
Chris Lattner | f3a3660 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 4808 | |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 4809 | return 0; |
| 4810 | } |
| 4811 | |
| 4812 | |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 4813 | /// GetGEPGlobalInitializer - Given a constant, and a getelementptr |
| 4814 | /// constantexpr, return the constant value being addressed by the constant |
| 4815 | /// expression, or null if something is funny. |
| 4816 | /// |
| 4817 | static Constant *GetGEPGlobalInitializer(Constant *C, ConstantExpr *CE) { |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4818 | if (CE->getOperand(1) != Constant::getNullValue(CE->getOperand(1)->getType())) |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 4819 | return 0; // Do not allow stepping over the value! |
| 4820 | |
| 4821 | // Loop over all of the operands, tracking down which value we are |
| 4822 | // addressing... |
Chris Lattner | ed79d8a | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 4823 | gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE); |
| 4824 | for (++I; I != E; ++I) |
| 4825 | if (const StructType *STy = dyn_cast<StructType>(*I)) { |
| 4826 | ConstantUInt *CU = cast<ConstantUInt>(I.getOperand()); |
| 4827 | assert(CU->getValue() < STy->getNumElements() && |
| 4828 | "Struct index out of range!"); |
Chris Lattner | fdfe3e49 | 2005-01-08 19:42:22 +0000 | [diff] [blame] | 4829 | unsigned El = (unsigned)CU->getValue(); |
Chris Lattner | ed79d8a | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 4830 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { |
Chris Lattner | fdfe3e49 | 2005-01-08 19:42:22 +0000 | [diff] [blame] | 4831 | C = CS->getOperand(El); |
Chris Lattner | ed79d8a | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 4832 | } else if (isa<ConstantAggregateZero>(C)) { |
Jeff Cohen | 8263985 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 4833 | C = Constant::getNullValue(STy->getElementType(El)); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4834 | } else if (isa<UndefValue>(C)) { |
Jeff Cohen | 8263985 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 4835 | C = UndefValue::get(STy->getElementType(El)); |
Chris Lattner | ed79d8a | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 4836 | } else { |
| 4837 | return 0; |
| 4838 | } |
| 4839 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand())) { |
| 4840 | const ArrayType *ATy = cast<ArrayType>(*I); |
| 4841 | if ((uint64_t)CI->getRawValue() >= ATy->getNumElements()) return 0; |
| 4842 | if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) |
Chris Lattner | fdfe3e49 | 2005-01-08 19:42:22 +0000 | [diff] [blame] | 4843 | C = CA->getOperand((unsigned)CI->getRawValue()); |
Chris Lattner | ed79d8a | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 4844 | else if (isa<ConstantAggregateZero>(C)) |
| 4845 | C = Constant::getNullValue(ATy->getElementType()); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4846 | else if (isa<UndefValue>(C)) |
| 4847 | C = UndefValue::get(ATy->getElementType()); |
Chris Lattner | ed79d8a | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 4848 | else |
| 4849 | return 0; |
| 4850 | } else { |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 4851 | return 0; |
Chris Lattner | ed79d8a | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 4852 | } |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 4853 | return C; |
| 4854 | } |
| 4855 | |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 4856 | /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible. |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 4857 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI) { |
| 4858 | User *CI = cast<User>(LI.getOperand(0)); |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 4859 | Value *CastOp = CI->getOperand(0); |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 4860 | |
| 4861 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 4862 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 4863 | const Type *SrcPTy = SrcTy->getElementType(); |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 4864 | |
| 4865 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) { |
| 4866 | // If the source is an array, the code below will not succeed. Check to |
| 4867 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 4868 | // constants. |
| 4869 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 4870 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 4871 | if (ASrcTy->getNumElements() != 0) { |
| 4872 | std::vector<Value*> Idxs(2, Constant::getNullValue(Type::IntTy)); |
| 4873 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs); |
| 4874 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 4875 | SrcPTy = SrcTy->getElementType(); |
| 4876 | } |
| 4877 | |
| 4878 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
Chris Lattner | ecfa9b5 | 2005-03-29 06:37:47 +0000 | [diff] [blame] | 4879 | // Do not allow turning this into a load of an integer, which is then |
| 4880 | // casted to a pointer, this pessimizes pointer analysis a lot. |
| 4881 | (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4882 | IC.getTargetData().getTypeSize(SrcPTy) == |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 4883 | IC.getTargetData().getTypeSize(DestPTy)) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4884 | |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 4885 | // Okay, we are casting from one integer or pointer type to another of |
| 4886 | // the same size. Instead of casting the pointer before the load, cast |
| 4887 | // the result of the loaded value. |
| 4888 | Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp, |
| 4889 | CI->getName(), |
| 4890 | LI.isVolatile()),LI); |
| 4891 | // Now cast the result of the load. |
| 4892 | return new CastInst(NewLoad, LI.getType()); |
| 4893 | } |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 4894 | } |
| 4895 | } |
| 4896 | return 0; |
| 4897 | } |
| 4898 | |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 4899 | /// isSafeToLoadUnconditionally - Return true if we know that executing a load |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 4900 | /// from this value cannot trap. If it is not obviously safe to load from the |
| 4901 | /// specified pointer, we do a quick local scan of the basic block containing |
| 4902 | /// ScanFrom, to determine if the address is already accessed. |
| 4903 | static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) { |
| 4904 | // If it is an alloca or global variable, it is always safe to load from. |
| 4905 | if (isa<AllocaInst>(V) || isa<GlobalVariable>(V)) return true; |
| 4906 | |
| 4907 | // Otherwise, be a little bit agressive by scanning the local block where we |
| 4908 | // 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] | 4909 | // from/to. If so, the previous load or store would have already trapped, |
| 4910 | // so there is no harm doing an extra load (also, CSE will later eliminate |
| 4911 | // the load entirely). |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 4912 | BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); |
| 4913 | |
Alkis Evlogimenos | d59cebf | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 4914 | while (BBI != E) { |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 4915 | --BBI; |
| 4916 | |
| 4917 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 4918 | if (LI->getOperand(0) == V) return true; |
| 4919 | } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 4920 | if (SI->getOperand(1) == V) return true; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4921 | |
Alkis Evlogimenos | d59cebf | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 4922 | } |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 4923 | return false; |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 4924 | } |
| 4925 | |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 4926 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 4927 | Value *Op = LI.getOperand(0); |
Chris Lattner | 7e8af38 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 4928 | |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 4929 | // load (cast X) --> cast (load X) iff safe |
| 4930 | if (CastInst *CI = dyn_cast<CastInst>(Op)) |
| 4931 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 4932 | return Res; |
| 4933 | |
| 4934 | // None of the following transforms are legal for volatile loads. |
| 4935 | if (LI.isVolatile()) return 0; |
Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 4936 | |
Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 4937 | if (&LI.getParent()->front() != &LI) { |
| 4938 | BasicBlock::iterator BBI = &LI; --BBI; |
Chris Lattner | e0bfdf1 | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 4939 | // If the instruction immediately before this is a store to the same |
| 4940 | // address, do a simple form of store->load forwarding. |
Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 4941 | if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 4942 | if (SI->getOperand(1) == LI.getOperand(0)) |
| 4943 | return ReplaceInstUsesWith(LI, SI->getOperand(0)); |
Chris Lattner | e0bfdf1 | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 4944 | if (LoadInst *LIB = dyn_cast<LoadInst>(BBI)) |
| 4945 | if (LIB->getOperand(0) == LI.getOperand(0)) |
| 4946 | return ReplaceInstUsesWith(LI, LIB); |
Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 4947 | } |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 4948 | |
| 4949 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) |
| 4950 | if (isa<ConstantPointerNull>(GEPI->getOperand(0)) || |
| 4951 | isa<UndefValue>(GEPI->getOperand(0))) { |
| 4952 | // Insert a new store to null instruction before the load to indicate |
| 4953 | // that this code is not reachable. We do this instead of inserting |
| 4954 | // an unreachable instruction directly because we cannot modify the |
| 4955 | // CFG. |
| 4956 | new StoreInst(UndefValue::get(LI.getType()), |
| 4957 | Constant::getNullValue(Op->getType()), &LI); |
| 4958 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 4959 | } |
| 4960 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4961 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 4962 | // load null/undef -> undef |
| 4963 | if ((C->isNullValue() || isa<UndefValue>(C))) { |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 4964 | // Insert a new store to null instruction before the load to indicate that |
| 4965 | // this code is not reachable. We do this instead of inserting an |
| 4966 | // unreachable instruction directly because we cannot modify the CFG. |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 4967 | new StoreInst(UndefValue::get(LI.getType()), |
| 4968 | Constant::getNullValue(Op->getType()), &LI); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4969 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 4970 | } |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 4971 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4972 | // Instcombine load (constant global) into the value loaded. |
| 4973 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) |
| 4974 | if (GV->isConstant() && !GV->isExternal()) |
| 4975 | return ReplaceInstUsesWith(LI, GV->getInitializer()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4976 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4977 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded. |
| 4978 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) |
| 4979 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 4980 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
| 4981 | if (GV->isConstant() && !GV->isExternal()) |
| 4982 | if (Constant *V = GetGEPGlobalInitializer(GV->getInitializer(), CE)) |
| 4983 | return ReplaceInstUsesWith(LI, V); |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 4984 | if (CE->getOperand(0)->isNullValue()) { |
| 4985 | // Insert a new store to null instruction before the load to indicate |
| 4986 | // that this code is not reachable. We do this instead of inserting |
| 4987 | // an unreachable instruction directly because we cannot modify the |
| 4988 | // CFG. |
| 4989 | new StoreInst(UndefValue::get(LI.getType()), |
| 4990 | Constant::getNullValue(Op->getType()), &LI); |
| 4991 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 4992 | } |
| 4993 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4994 | } else if (CE->getOpcode() == Instruction::Cast) { |
| 4995 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 4996 | return Res; |
| 4997 | } |
| 4998 | } |
Chris Lattner | e228ee5 | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 4999 | |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 5000 | if (Op->hasOneUse()) { |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5001 | // Change select and PHI nodes to select values instead of addresses: this |
| 5002 | // helps alias analysis out a lot, allows many others simplifications, and |
| 5003 | // exposes redundancy in the code. |
| 5004 | // |
| 5005 | // Note that we cannot do the transformation unless we know that the |
| 5006 | // introduced loads cannot trap! Something like this is valid as long as |
| 5007 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 5008 | // but it would not be valid if we transformed it to load from null |
| 5009 | // unconditionally. |
| 5010 | // |
| 5011 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 5012 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 5013 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && |
| 5014 | isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5015 | Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1), |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 5016 | SI->getOperand(1)->getName()+".val"), LI); |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5017 | Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2), |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 5018 | SI->getOperand(2)->getName()+".val"), LI); |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5019 | return new SelectInst(SI->getCondition(), V1, V2); |
| 5020 | } |
| 5021 | |
Chris Lattner | bdcf41a | 2004-09-23 15:46:00 +0000 | [diff] [blame] | 5022 | // load (select (cond, null, P)) -> load P |
| 5023 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) |
| 5024 | if (C->isNullValue()) { |
| 5025 | LI.setOperand(0, SI->getOperand(2)); |
| 5026 | return &LI; |
| 5027 | } |
| 5028 | |
| 5029 | // load (select (cond, P, null)) -> load P |
| 5030 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) |
| 5031 | if (C->isNullValue()) { |
| 5032 | LI.setOperand(0, SI->getOperand(1)); |
| 5033 | return &LI; |
| 5034 | } |
| 5035 | |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5036 | } else if (PHINode *PN = dyn_cast<PHINode>(Op)) { |
| 5037 | // load (phi (&V1, &V2, &V3)) --> phi(load &V1, load &V2, load &V3) |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 5038 | bool Safe = PN->getParent() == LI.getParent(); |
| 5039 | |
| 5040 | // Scan all of the instructions between the PHI and the load to make |
| 5041 | // sure there are no instructions that might possibly alter the value |
| 5042 | // loaded from the PHI. |
| 5043 | if (Safe) { |
| 5044 | BasicBlock::iterator I = &LI; |
| 5045 | for (--I; !isa<PHINode>(I); --I) |
| 5046 | if (isa<StoreInst>(I) || isa<CallInst>(I)) { |
| 5047 | Safe = false; |
| 5048 | break; |
| 5049 | } |
| 5050 | } |
| 5051 | |
| 5052 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e && Safe; ++i) |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 5053 | if (!isSafeToLoadUnconditionally(PN->getIncomingValue(i), |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 5054 | PN->getIncomingBlock(i)->getTerminator())) |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5055 | Safe = false; |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 5056 | |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5057 | if (Safe) { |
| 5058 | // Create the PHI. |
| 5059 | PHINode *NewPN = new PHINode(LI.getType(), PN->getName()); |
| 5060 | InsertNewInstBefore(NewPN, *PN); |
| 5061 | std::map<BasicBlock*,Value*> LoadMap; // Don't insert duplicate loads |
| 5062 | |
| 5063 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 5064 | BasicBlock *BB = PN->getIncomingBlock(i); |
| 5065 | Value *&TheLoad = LoadMap[BB]; |
| 5066 | if (TheLoad == 0) { |
| 5067 | Value *InVal = PN->getIncomingValue(i); |
| 5068 | TheLoad = InsertNewInstBefore(new LoadInst(InVal, |
| 5069 | InVal->getName()+".val"), |
| 5070 | *BB->getTerminator()); |
| 5071 | } |
| 5072 | NewPN->addIncoming(TheLoad, BB); |
| 5073 | } |
| 5074 | return ReplaceInstUsesWith(LI, NewPN); |
| 5075 | } |
| 5076 | } |
| 5077 | } |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 5078 | return 0; |
| 5079 | } |
| 5080 | |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 5081 | /// InstCombineStoreToCast - Fold 'store V, (cast P)' -> store (cast V), P' |
| 5082 | /// when possible. |
| 5083 | static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { |
| 5084 | User *CI = cast<User>(SI.getOperand(1)); |
| 5085 | Value *CastOp = CI->getOperand(0); |
| 5086 | |
| 5087 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
| 5088 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
| 5089 | const Type *SrcPTy = SrcTy->getElementType(); |
| 5090 | |
| 5091 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) { |
| 5092 | // If the source is an array, the code below will not succeed. Check to |
| 5093 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 5094 | // constants. |
| 5095 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 5096 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 5097 | if (ASrcTy->getNumElements() != 0) { |
| 5098 | std::vector<Value*> Idxs(2, Constant::getNullValue(Type::IntTy)); |
| 5099 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs); |
| 5100 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 5101 | SrcPTy = SrcTy->getElementType(); |
| 5102 | } |
| 5103 | |
| 5104 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5105 | IC.getTargetData().getTypeSize(SrcPTy) == |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 5106 | IC.getTargetData().getTypeSize(DestPTy)) { |
| 5107 | |
| 5108 | // Okay, we are casting from one integer or pointer type to another of |
| 5109 | // the same size. Instead of casting the pointer before the store, cast |
| 5110 | // the value to be stored. |
| 5111 | Value *NewCast; |
| 5112 | if (Constant *C = dyn_cast<Constant>(SI.getOperand(0))) |
| 5113 | NewCast = ConstantExpr::getCast(C, SrcPTy); |
| 5114 | else |
| 5115 | NewCast = IC.InsertNewInstBefore(new CastInst(SI.getOperand(0), |
| 5116 | SrcPTy, |
| 5117 | SI.getOperand(0)->getName()+".c"), SI); |
| 5118 | |
| 5119 | return new StoreInst(NewCast, CastOp); |
| 5120 | } |
| 5121 | } |
| 5122 | } |
| 5123 | return 0; |
| 5124 | } |
| 5125 | |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 5126 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
| 5127 | Value *Val = SI.getOperand(0); |
| 5128 | Value *Ptr = SI.getOperand(1); |
| 5129 | |
| 5130 | if (isa<UndefValue>(Ptr)) { // store X, undef -> noop (even if volatile) |
| 5131 | removeFromWorkList(&SI); |
| 5132 | SI.eraseFromParent(); |
| 5133 | ++NumCombined; |
| 5134 | return 0; |
| 5135 | } |
| 5136 | |
| 5137 | if (SI.isVolatile()) return 0; // Don't hack volatile loads. |
| 5138 | |
| 5139 | // store X, null -> turns into 'unreachable' in SimplifyCFG |
| 5140 | if (isa<ConstantPointerNull>(Ptr)) { |
| 5141 | if (!isa<UndefValue>(Val)) { |
| 5142 | SI.setOperand(0, UndefValue::get(Val->getType())); |
| 5143 | if (Instruction *U = dyn_cast<Instruction>(Val)) |
| 5144 | WorkList.push_back(U); // Dropped a use. |
| 5145 | ++NumCombined; |
| 5146 | } |
| 5147 | return 0; // Do not modify these! |
| 5148 | } |
| 5149 | |
| 5150 | // store undef, Ptr -> noop |
| 5151 | if (isa<UndefValue>(Val)) { |
| 5152 | removeFromWorkList(&SI); |
| 5153 | SI.eraseFromParent(); |
| 5154 | ++NumCombined; |
| 5155 | return 0; |
| 5156 | } |
| 5157 | |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 5158 | // If the pointer destination is a cast, see if we can fold the cast into the |
| 5159 | // source instead. |
| 5160 | if (CastInst *CI = dyn_cast<CastInst>(Ptr)) |
| 5161 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 5162 | return Res; |
| 5163 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
| 5164 | if (CE->getOpcode() == Instruction::Cast) |
| 5165 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 5166 | return Res; |
| 5167 | |
Chris Lattner | 219175c | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 5168 | |
| 5169 | // If this store is the last instruction in the basic block, and if the block |
| 5170 | // ends with an unconditional branch, try to move it to the successor block. |
| 5171 | BasicBlock::iterator BBI = &SI; ++BBI; |
| 5172 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) |
| 5173 | if (BI->isUnconditional()) { |
| 5174 | // Check to see if the successor block has exactly two incoming edges. If |
| 5175 | // so, see if the other predecessor contains a store to the same location. |
| 5176 | // if so, insert a PHI node (if needed) and move the stores down. |
| 5177 | BasicBlock *Dest = BI->getSuccessor(0); |
| 5178 | |
| 5179 | pred_iterator PI = pred_begin(Dest); |
| 5180 | BasicBlock *Other = 0; |
| 5181 | if (*PI != BI->getParent()) |
| 5182 | Other = *PI; |
| 5183 | ++PI; |
| 5184 | if (PI != pred_end(Dest)) { |
| 5185 | if (*PI != BI->getParent()) |
| 5186 | if (Other) |
| 5187 | Other = 0; |
| 5188 | else |
| 5189 | Other = *PI; |
| 5190 | if (++PI != pred_end(Dest)) |
| 5191 | Other = 0; |
| 5192 | } |
| 5193 | if (Other) { // If only one other pred... |
| 5194 | BBI = Other->getTerminator(); |
| 5195 | // Make sure this other block ends in an unconditional branch and that |
| 5196 | // there is an instruction before the branch. |
| 5197 | if (isa<BranchInst>(BBI) && cast<BranchInst>(BBI)->isUnconditional() && |
| 5198 | BBI != Other->begin()) { |
| 5199 | --BBI; |
| 5200 | StoreInst *OtherStore = dyn_cast<StoreInst>(BBI); |
| 5201 | |
| 5202 | // If this instruction is a store to the same location. |
| 5203 | if (OtherStore && OtherStore->getOperand(1) == SI.getOperand(1)) { |
| 5204 | // Okay, we know we can perform this transformation. Insert a PHI |
| 5205 | // node now if we need it. |
| 5206 | Value *MergedVal = OtherStore->getOperand(0); |
| 5207 | if (MergedVal != SI.getOperand(0)) { |
| 5208 | PHINode *PN = new PHINode(MergedVal->getType(), "storemerge"); |
| 5209 | PN->reserveOperandSpace(2); |
| 5210 | PN->addIncoming(SI.getOperand(0), SI.getParent()); |
| 5211 | PN->addIncoming(OtherStore->getOperand(0), Other); |
| 5212 | MergedVal = InsertNewInstBefore(PN, Dest->front()); |
| 5213 | } |
| 5214 | |
| 5215 | // Advance to a place where it is safe to insert the new store and |
| 5216 | // insert it. |
| 5217 | BBI = Dest->begin(); |
| 5218 | while (isa<PHINode>(BBI)) ++BBI; |
| 5219 | InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1), |
| 5220 | OtherStore->isVolatile()), *BBI); |
| 5221 | |
| 5222 | // Nuke the old stores. |
| 5223 | removeFromWorkList(&SI); |
| 5224 | removeFromWorkList(OtherStore); |
| 5225 | SI.eraseFromParent(); |
| 5226 | OtherStore->eraseFromParent(); |
| 5227 | ++NumCombined; |
| 5228 | return 0; |
| 5229 | } |
| 5230 | } |
| 5231 | } |
| 5232 | } |
| 5233 | |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 5234 | return 0; |
| 5235 | } |
| 5236 | |
| 5237 | |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 5238 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 5239 | // 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] | 5240 | Value *X = 0; |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 5241 | BasicBlock *TrueDest; |
| 5242 | BasicBlock *FalseDest; |
| 5243 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && |
| 5244 | !isa<Constant>(X)) { |
| 5245 | // Swap Destinations and condition... |
| 5246 | BI.setCondition(X); |
| 5247 | BI.setSuccessor(0, FalseDest); |
| 5248 | BI.setSuccessor(1, TrueDest); |
| 5249 | return &BI; |
| 5250 | } |
| 5251 | |
| 5252 | // Cannonicalize setne -> seteq |
| 5253 | Instruction::BinaryOps Op; Value *Y; |
| 5254 | if (match(&BI, m_Br(m_SetCond(Op, m_Value(X), m_Value(Y)), |
| 5255 | TrueDest, FalseDest))) |
| 5256 | if ((Op == Instruction::SetNE || Op == Instruction::SetLE || |
| 5257 | Op == Instruction::SetGE) && BI.getCondition()->hasOneUse()) { |
| 5258 | SetCondInst *I = cast<SetCondInst>(BI.getCondition()); |
| 5259 | std::string Name = I->getName(); I->setName(""); |
| 5260 | Instruction::BinaryOps NewOpcode = SetCondInst::getInverseCondition(Op); |
| 5261 | Value *NewSCC = BinaryOperator::create(NewOpcode, X, Y, Name, I); |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 5262 | // Swap Destinations and condition... |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 5263 | BI.setCondition(NewSCC); |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 5264 | BI.setSuccessor(0, FalseDest); |
| 5265 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 5266 | removeFromWorkList(I); |
| 5267 | I->getParent()->getInstList().erase(I); |
| 5268 | WorkList.push_back(cast<Instruction>(NewSCC)); |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 5269 | return &BI; |
| 5270 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5271 | |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 5272 | return 0; |
| 5273 | } |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 5274 | |
Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 5275 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
| 5276 | Value *Cond = SI.getCondition(); |
| 5277 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { |
| 5278 | if (I->getOpcode() == Instruction::Add) |
| 5279 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 5280 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' |
| 5281 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5282 | SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), |
Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 5283 | AddRHS)); |
| 5284 | SI.setOperand(0, I->getOperand(0)); |
| 5285 | WorkList.push_back(I); |
| 5286 | return &SI; |
| 5287 | } |
| 5288 | } |
| 5289 | return 0; |
| 5290 | } |
| 5291 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 5292 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5293 | void InstCombiner::removeFromWorkList(Instruction *I) { |
| 5294 | WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I), |
| 5295 | WorkList.end()); |
| 5296 | } |
| 5297 | |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 5298 | |
| 5299 | /// TryToSinkInstruction - Try to move the specified instruction from its |
| 5300 | /// current block into the beginning of DestBlock, which can only happen if it's |
| 5301 | /// safe to move the instruction past all of the instructions between it and the |
| 5302 | /// end of its block. |
| 5303 | static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { |
| 5304 | assert(I->hasOneUse() && "Invariants didn't hold!"); |
| 5305 | |
| 5306 | // Cannot move control-flow-involving instructions. |
| 5307 | if (isa<PHINode>(I) || isa<InvokeInst>(I) || isa<CallInst>(I)) return false; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5308 | |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 5309 | // Do not sink alloca instructions out of the entry block. |
| 5310 | if (isa<AllocaInst>(I) && I->getParent() == &DestBlock->getParent()->front()) |
| 5311 | return false; |
| 5312 | |
Chris Lattner | f17a2fb | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 5313 | // We can only sink load instructions if there is nothing between the load and |
| 5314 | // the end of block that could change the value. |
| 5315 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 5316 | if (LI->isVolatile()) return false; // Don't sink volatile loads. |
| 5317 | |
| 5318 | for (BasicBlock::iterator Scan = LI, E = LI->getParent()->end(); |
| 5319 | Scan != E; ++Scan) |
| 5320 | if (Scan->mayWriteToMemory()) |
| 5321 | return false; |
Chris Lattner | f17a2fb | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 5322 | } |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 5323 | |
| 5324 | BasicBlock::iterator InsertPos = DestBlock->begin(); |
| 5325 | while (isa<PHINode>(InsertPos)) ++InsertPos; |
| 5326 | |
Chris Lattner | 9f269e4 | 2005-08-08 19:11:57 +0000 | [diff] [blame] | 5327 | I->moveBefore(InsertPos); |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 5328 | ++NumSunkInst; |
| 5329 | return true; |
| 5330 | } |
| 5331 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5332 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 5333 | bool Changed = false; |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 5334 | TD = &getAnalysis<TargetData>(); |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 5335 | |
Chris Lattner | 4ed40f7 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 5336 | { |
| 5337 | // Populate the worklist with the reachable instructions. |
| 5338 | std::set<BasicBlock*> Visited; |
| 5339 | for (df_ext_iterator<BasicBlock*> BB = df_ext_begin(&F.front(), Visited), |
| 5340 | E = df_ext_end(&F.front(), Visited); BB != E; ++BB) |
| 5341 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 5342 | WorkList.push_back(I); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 5343 | |
Chris Lattner | 4ed40f7 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 5344 | // Do a quick scan over the function. If we find any blocks that are |
| 5345 | // unreachable, remove any instructions inside of them. This prevents |
| 5346 | // the instcombine code from having to deal with some bad special cases. |
| 5347 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 5348 | if (!Visited.count(BB)) { |
| 5349 | Instruction *Term = BB->getTerminator(); |
| 5350 | while (Term != BB->begin()) { // Remove instrs bottom-up |
| 5351 | BasicBlock::iterator I = Term; --I; |
Chris Lattner | 2d3a7a6 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 5352 | |
Chris Lattner | 4ed40f7 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 5353 | DEBUG(std::cerr << "IC: DCE: " << *I); |
| 5354 | ++NumDeadInst; |
| 5355 | |
| 5356 | if (!I->use_empty()) |
| 5357 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 5358 | I->eraseFromParent(); |
| 5359 | } |
| 5360 | } |
| 5361 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 5362 | |
| 5363 | while (!WorkList.empty()) { |
| 5364 | Instruction *I = WorkList.back(); // Get an instruction from the worklist |
| 5365 | WorkList.pop_back(); |
| 5366 | |
Misha Brukman | 632df28 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 5367 | // Check to see if we can DCE or ConstantPropagate the instruction... |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5368 | // Check to see if we can DIE the instruction... |
| 5369 | if (isInstructionTriviallyDead(I)) { |
| 5370 | // Add operands to the worklist... |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5371 | if (I->getNumOperands() < 4) |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 5372 | AddUsesToWorkList(*I); |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5373 | ++NumDeadInst; |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5374 | |
Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 5375 | DEBUG(std::cerr << "IC: DCE: " << *I); |
| 5376 | |
| 5377 | I->eraseFromParent(); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5378 | removeFromWorkList(I); |
| 5379 | continue; |
| 5380 | } |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5381 | |
Misha Brukman | 632df28 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 5382 | // Instruction isn't dead, see if we can constant propagate it... |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5383 | if (Constant *C = ConstantFoldInstruction(I)) { |
Alkis Evlogimenos | a1291a0 | 2004-12-08 23:10:30 +0000 | [diff] [blame] | 5384 | Value* Ptr = I->getOperand(0); |
Chris Lattner | 6580e09 | 2004-10-16 19:44:59 +0000 | [diff] [blame] | 5385 | if (isa<GetElementPtrInst>(I) && |
Alkis Evlogimenos | a1291a0 | 2004-12-08 23:10:30 +0000 | [diff] [blame] | 5386 | cast<Constant>(Ptr)->isNullValue() && |
| 5387 | !isa<ConstantPointerNull>(C) && |
| 5388 | cast<PointerType>(Ptr->getType())->getElementType()->isSized()) { |
Chris Lattner | 6580e09 | 2004-10-16 19:44:59 +0000 | [diff] [blame] | 5389 | // If this is a constant expr gep that is effectively computing an |
| 5390 | // "offsetof", fold it into 'cast int X to T*' instead of 'gep 0, 0, 12' |
| 5391 | bool isFoldableGEP = true; |
| 5392 | for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i) |
| 5393 | if (!isa<ConstantInt>(I->getOperand(i))) |
| 5394 | isFoldableGEP = false; |
| 5395 | if (isFoldableGEP) { |
Alkis Evlogimenos | a1291a0 | 2004-12-08 23:10:30 +0000 | [diff] [blame] | 5396 | uint64_t Offset = TD->getIndexedOffset(Ptr->getType(), |
Chris Lattner | 6580e09 | 2004-10-16 19:44:59 +0000 | [diff] [blame] | 5397 | std::vector<Value*>(I->op_begin()+1, I->op_end())); |
| 5398 | C = ConstantUInt::get(Type::ULongTy, Offset); |
Chris Lattner | 684c5c6 | 2004-10-16 19:46:33 +0000 | [diff] [blame] | 5399 | C = ConstantExpr::getCast(C, TD->getIntPtrType()); |
Chris Lattner | 6580e09 | 2004-10-16 19:44:59 +0000 | [diff] [blame] | 5400 | C = ConstantExpr::getCast(C, I->getType()); |
| 5401 | } |
| 5402 | } |
| 5403 | |
Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 5404 | DEBUG(std::cerr << "IC: ConstFold to: " << *C << " from: " << *I); |
| 5405 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5406 | // Add operands to the worklist... |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 5407 | AddUsesToWorkList(*I); |
Chris Lattner | c6509f4 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 5408 | ReplaceInstUsesWith(*I, C); |
| 5409 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5410 | ++NumConstProp; |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5411 | I->getParent()->getInstList().erase(I); |
Chris Lattner | 800aaaf | 2003-10-07 15:17:02 +0000 | [diff] [blame] | 5412 | removeFromWorkList(I); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5413 | continue; |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5414 | } |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5415 | |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 5416 | // See if we can trivially sink this instruction to a successor basic block. |
| 5417 | if (I->hasOneUse()) { |
| 5418 | BasicBlock *BB = I->getParent(); |
| 5419 | BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent(); |
| 5420 | if (UserParent != BB) { |
| 5421 | bool UserIsSuccessor = false; |
| 5422 | // See if the user is one of our successors. |
| 5423 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) |
| 5424 | if (*SI == UserParent) { |
| 5425 | UserIsSuccessor = true; |
| 5426 | break; |
| 5427 | } |
| 5428 | |
| 5429 | // If the user is one of our immediate successors, and if that successor |
| 5430 | // only has us as a predecessors (we'd have to split the critical edge |
| 5431 | // otherwise), we can keep going. |
| 5432 | if (UserIsSuccessor && !isa<PHINode>(I->use_back()) && |
| 5433 | next(pred_begin(UserParent)) == pred_end(UserParent)) |
| 5434 | // Okay, the CFG is simple enough, try to sink this instruction. |
| 5435 | Changed |= TryToSinkInstruction(I, UserParent); |
| 5436 | } |
| 5437 | } |
| 5438 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 5439 | // Now that we have an instruction, try combining it to simplify it... |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 5440 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 5441 | ++NumCombined; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 5442 | // Should we replace the old instruction with a new one? |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 5443 | if (Result != I) { |
Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 5444 | DEBUG(std::cerr << "IC: Old = " << *I |
| 5445 | << " New = " << *Result); |
| 5446 | |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 5447 | // Everything uses the new instruction now. |
| 5448 | I->replaceAllUsesWith(Result); |
| 5449 | |
| 5450 | // Push the new instruction and any users onto the worklist. |
| 5451 | WorkList.push_back(Result); |
| 5452 | AddUsersToWorkList(*Result); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5453 | |
| 5454 | // Move the name to the new instruction first... |
| 5455 | std::string OldName = I->getName(); I->setName(""); |
Chris Lattner | 950fc78 | 2003-10-07 22:58:41 +0000 | [diff] [blame] | 5456 | Result->setName(OldName); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5457 | |
| 5458 | // Insert the new instruction into the basic block... |
| 5459 | BasicBlock *InstParent = I->getParent(); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 5460 | BasicBlock::iterator InsertPos = I; |
| 5461 | |
| 5462 | if (!isa<PHINode>(Result)) // If combining a PHI, don't insert |
| 5463 | while (isa<PHINode>(InsertPos)) // middle of a block of PHIs. |
| 5464 | ++InsertPos; |
| 5465 | |
| 5466 | InstParent->getInstList().insert(InsertPos, Result); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5467 | |
Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 5468 | // Make sure that we reprocess all operands now that we reduced their |
| 5469 | // use counts. |
Chris Lattner | b643a9e | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 5470 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 5471 | if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i))) |
| 5472 | WorkList.push_back(OpI); |
| 5473 | |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 5474 | // Instructions can end up on the worklist more than once. Make sure |
| 5475 | // we do not process an instruction that has been deleted. |
| 5476 | removeFromWorkList(I); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5477 | |
| 5478 | // Erase the old instruction. |
| 5479 | InstParent->getInstList().erase(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5480 | } else { |
Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 5481 | DEBUG(std::cerr << "IC: MOD = " << *I); |
| 5482 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 5483 | // If the instruction was modified, it's possible that it is now dead. |
| 5484 | // if so, remove it. |
Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 5485 | if (isInstructionTriviallyDead(I)) { |
| 5486 | // Make sure we process all operands now that we are reducing their |
| 5487 | // use counts. |
| 5488 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 5489 | if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i))) |
| 5490 | WorkList.push_back(OpI); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5491 | |
Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 5492 | // Instructions may end up in the worklist more than once. Erase all |
| 5493 | // occurrances of this instruction. |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5494 | removeFromWorkList(I); |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 5495 | I->eraseFromParent(); |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 5496 | } else { |
| 5497 | WorkList.push_back(Result); |
| 5498 | AddUsersToWorkList(*Result); |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 5499 | } |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 5500 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 5501 | Changed = true; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 5502 | } |
| 5503 | } |
| 5504 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 5505 | return Changed; |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 5506 | } |
| 5507 | |
Brian Gaeke | 38b79e8 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 5508 | FunctionPass *llvm::createInstructionCombiningPass() { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 5509 | return new InstCombiner(); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 5510 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 5511 | |