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 | c597b8a | 2006-01-22 23:32:06 +0000 | [diff] [blame] | 55 | #include <iostream> |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 56 | using namespace llvm; |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 57 | using namespace llvm::PatternMatch; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 59 | namespace { |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 60 | Statistic<> NumCombined ("instcombine", "Number of insts combined"); |
| 61 | Statistic<> NumConstProp("instcombine", "Number of constant folds"); |
| 62 | Statistic<> NumDeadInst ("instcombine", "Number of dead inst eliminated"); |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 63 | Statistic<> NumDeadStore("instcombine", "Number of dead stores eliminated"); |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 64 | Statistic<> NumSunkInst ("instcombine", "Number of instructions sunk"); |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 65 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 66 | class InstCombiner : public FunctionPass, |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 67 | public InstVisitor<InstCombiner, Instruction*> { |
| 68 | // Worklist of all of the instructions that need to be simplified. |
| 69 | std::vector<Instruction*> WorkList; |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 70 | TargetData *TD; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 72 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
| 73 | /// the instruction to the work lists because they might get more simplified |
| 74 | /// now. |
| 75 | /// |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 76 | void AddUsersToWorkList(Value &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 77 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 78 | UI != UE; ++UI) |
| 79 | WorkList.push_back(cast<Instruction>(*UI)); |
| 80 | } |
| 81 | |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 82 | /// AddUsesToWorkList - When an instruction is simplified, add operands to |
| 83 | /// the work lists because they might get more simplified now. |
| 84 | /// |
| 85 | void AddUsesToWorkList(Instruction &I) { |
| 86 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 87 | if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) |
| 88 | WorkList.push_back(Op); |
| 89 | } |
| 90 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 91 | // removeFromWorkList - remove all instances of I from the worklist. |
| 92 | void removeFromWorkList(Instruction *I); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 93 | public: |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 94 | virtual bool runOnFunction(Function &F); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 95 | |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 96 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 97 | AU.addRequired<TargetData>(); |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 98 | AU.setPreservesCFG(); |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 101 | TargetData &getTargetData() const { return *TD; } |
| 102 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 103 | // Visitation implementation - Implement instruction combining for different |
| 104 | // instruction types. The semantics are as follows: |
| 105 | // Return Value: |
| 106 | // null - No change was made |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 107 | // 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] | 108 | // otherwise - Change was made, replace I with returned instruction |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 109 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 110 | Instruction *visitAdd(BinaryOperator &I); |
| 111 | Instruction *visitSub(BinaryOperator &I); |
| 112 | Instruction *visitMul(BinaryOperator &I); |
| 113 | Instruction *visitDiv(BinaryOperator &I); |
| 114 | Instruction *visitRem(BinaryOperator &I); |
| 115 | Instruction *visitAnd(BinaryOperator &I); |
| 116 | Instruction *visitOr (BinaryOperator &I); |
| 117 | Instruction *visitXor(BinaryOperator &I); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 118 | Instruction *visitSetCondInst(SetCondInst &I); |
| 119 | Instruction *visitSetCondInstWithCastAndCast(SetCondInst &SCI); |
| 120 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 121 | Instruction *FoldGEPSetCC(User *GEPLHS, Value *RHS, |
| 122 | Instruction::BinaryOps Cond, Instruction &I); |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 123 | Instruction *visitShiftInst(ShiftInst &I); |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 124 | Instruction *FoldShiftByConstant(Value *Op0, ConstantUInt *Op1, |
| 125 | ShiftInst &I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 126 | Instruction *visitCastInst(CastInst &CI); |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 127 | Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 128 | Instruction *FI); |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 129 | Instruction *visitSelectInst(SelectInst &CI); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 130 | Instruction *visitCallInst(CallInst &CI); |
| 131 | Instruction *visitInvokeInst(InvokeInst &II); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 132 | Instruction *visitPHINode(PHINode &PN); |
| 133 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 134 | Instruction *visitAllocationInst(AllocationInst &AI); |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 135 | Instruction *visitFreeInst(FreeInst &FI); |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 136 | Instruction *visitLoadInst(LoadInst &LI); |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 137 | Instruction *visitStoreInst(StoreInst &SI); |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 138 | Instruction *visitBranchInst(BranchInst &BI); |
Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 139 | Instruction *visitSwitchInst(SwitchInst &SI); |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 140 | Instruction *visitExtractElementInst(ExtractElementInst &EI); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 141 | |
| 142 | // visitInstruction - Specify what to return for unhandled instructions... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 143 | Instruction *visitInstruction(Instruction &I) { return 0; } |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 145 | private: |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 146 | Instruction *visitCallSite(CallSite CS); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 147 | bool transformConstExprCastCall(CallSite CS); |
| 148 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 149 | public: |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 150 | // InsertNewInstBefore - insert an instruction New before instruction Old |
| 151 | // in the program. Add the new instruction to the worklist. |
| 152 | // |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 153 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Chris Lattner | 65217ff | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 154 | assert(New && New->getParent() == 0 && |
| 155 | "New instruction already inserted into a basic block!"); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 156 | BasicBlock *BB = Old.getParent(); |
| 157 | BB->getInstList().insert(&Old, New); // Insert inst |
| 158 | WorkList.push_back(New); // Add to worklist |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 159 | return New; |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 162 | /// InsertCastBefore - Insert a cast of V to TY before the instruction POS. |
| 163 | /// This also adds the cast to the worklist. Finally, this returns the |
| 164 | /// cast. |
| 165 | Value *InsertCastBefore(Value *V, const Type *Ty, Instruction &Pos) { |
| 166 | if (V->getType() == Ty) return V; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 168 | Instruction *C = new CastInst(V, Ty, V->getName(), &Pos); |
| 169 | WorkList.push_back(C); |
| 170 | return C; |
| 171 | } |
| 172 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 173 | // ReplaceInstUsesWith - This method is to be used when an instruction is |
| 174 | // found to be dead, replacable with another preexisting expression. Here |
| 175 | // we add all uses of I to the worklist, replace all uses of I with the new |
| 176 | // value, then return I, so that the inst combiner will know that I was |
| 177 | // modified. |
| 178 | // |
| 179 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 180 | AddUsersToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 8953b90 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 181 | if (&I != V) { |
| 182 | I.replaceAllUsesWith(V); |
| 183 | return &I; |
| 184 | } else { |
| 185 | // If we are replacing the instruction with itself, this must be in a |
| 186 | // segment of unreachable code, so just clobber the instruction. |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 187 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
Chris Lattner | 8953b90 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 188 | return &I; |
| 189 | } |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 190 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 191 | |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 192 | // UpdateValueUsesWith - This method is to be used when an value is |
| 193 | // found to be replacable with another preexisting expression or was |
| 194 | // updated. Here we add all uses of I to the worklist, replace all uses of |
| 195 | // I with the new value (unless the instruction was just updated), then |
| 196 | // return true, so that the inst combiner will know that I was modified. |
| 197 | // |
| 198 | bool UpdateValueUsesWith(Value *Old, Value *New) { |
| 199 | AddUsersToWorkList(*Old); // Add all modified instrs to worklist |
| 200 | if (Old != New) |
| 201 | Old->replaceAllUsesWith(New); |
| 202 | if (Instruction *I = dyn_cast<Instruction>(Old)) |
| 203 | WorkList.push_back(I); |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 204 | if (Instruction *I = dyn_cast<Instruction>(New)) |
| 205 | WorkList.push_back(I); |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 206 | return true; |
| 207 | } |
| 208 | |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 209 | // EraseInstFromFunction - When dealing with an instruction that has side |
| 210 | // effects or produces a void value, we can't rely on DCE to delete the |
| 211 | // instruction. Instead, visit methods should return the value returned by |
| 212 | // this function. |
| 213 | Instruction *EraseInstFromFunction(Instruction &I) { |
| 214 | assert(I.use_empty() && "Cannot erase instruction that is used!"); |
| 215 | AddUsesToWorkList(I); |
| 216 | removeFromWorkList(&I); |
Chris Lattner | 9530754 | 2004-11-18 21:41:39 +0000 | [diff] [blame] | 217 | I.eraseFromParent(); |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 218 | return 0; // Don't do anything with FI |
| 219 | } |
| 220 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 221 | private: |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 222 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 223 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 224 | /// casts that are known to not do anything... |
| 225 | /// |
| 226 | Value *InsertOperandCastBefore(Value *V, const Type *DestTy, |
| 227 | Instruction *InsertBefore); |
| 228 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 229 | // SimplifyCommutative - This performs a few simplifications for commutative |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 230 | // operators. |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 231 | bool SimplifyCommutative(BinaryOperator &I); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 232 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 233 | bool SimplifyDemandedBits(Value *V, uint64_t Mask, |
| 234 | uint64_t &KnownZero, uint64_t &KnownOne, |
| 235 | unsigned Depth = 0); |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 236 | |
| 237 | // FoldOpIntoPhi - Given a binary operator or cast instruction which has a |
| 238 | // PHI node as operand #0, see if we can fold the instruction into the PHI |
| 239 | // (which is only possible if all operands to the PHI are constants). |
| 240 | Instruction *FoldOpIntoPhi(Instruction &I); |
| 241 | |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 242 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 243 | // operator and they all are only used by the PHI, PHI together their |
| 244 | // inputs, and do the operation once, to the result of the PHI. |
| 245 | Instruction *FoldPHIArgOpIntoPHI(PHINode &PN); |
| 246 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 247 | Instruction *OptAndOp(Instruction *Op, ConstantIntegral *OpRHS, |
| 248 | ConstantIntegral *AndRHS, BinaryOperator &TheAnd); |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 249 | |
| 250 | Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantIntegral *Mask, |
| 251 | bool isSub, Instruction &I); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 252 | Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
| 253 | bool Inside, Instruction &IB); |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 254 | Instruction *PromoteCastOfAllocation(CastInst &CI, AllocationInst &AI); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 255 | }; |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 256 | |
Chris Lattner | c8b7092 | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 257 | RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions"); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 260 | // getComplexity: Assign a complexity or rank value to LLVM Values... |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 261 | // 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 262 | static unsigned getComplexity(Value *V) { |
| 263 | if (isa<Instruction>(V)) { |
| 264 | if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V)) |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 265 | return 3; |
| 266 | return 4; |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 267 | } |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 268 | if (isa<Argument>(V)) return 3; |
| 269 | return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2; |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 270 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 271 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 272 | // isOnlyUse - Return true if this instruction will be deleted if we stop using |
| 273 | // it. |
| 274 | static bool isOnlyUse(Value *V) { |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 275 | return V->hasOneUse() || isa<Constant>(V); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 278 | // getPromotedType - Return the specified type promoted as it would be to pass |
| 279 | // though a va_arg area... |
| 280 | static const Type *getPromotedType(const Type *Ty) { |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 281 | switch (Ty->getTypeID()) { |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 282 | case Type::SByteTyID: |
| 283 | case Type::ShortTyID: return Type::IntTy; |
| 284 | case Type::UByteTyID: |
| 285 | case Type::UShortTyID: return Type::UIntTy; |
| 286 | case Type::FloatTyID: return Type::DoubleTy; |
| 287 | default: return Ty; |
| 288 | } |
| 289 | } |
| 290 | |
Chris Lattner | 567b81f | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 291 | /// isCast - If the specified operand is a CastInst or a constant expr cast, |
| 292 | /// return the operand value, otherwise return null. |
| 293 | static Value *isCast(Value *V) { |
| 294 | if (CastInst *I = dyn_cast<CastInst>(V)) |
| 295 | return I->getOperand(0); |
| 296 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 297 | if (CE->getOpcode() == Instruction::Cast) |
| 298 | return CE->getOperand(0); |
| 299 | return 0; |
| 300 | } |
| 301 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 302 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 303 | // operators: |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 304 | // |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 305 | // 1. Order operands such that they are listed from right (least complex) to |
| 306 | // left (most complex). This puts constants before unary operators before |
| 307 | // binary operators. |
| 308 | // |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 309 | // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) |
| 310 | // 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] | 311 | // |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 312 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 313 | bool Changed = false; |
| 314 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) |
| 315 | Changed = !I.swapOperands(); |
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 | if (!I.isAssociative()) return Changed; |
| 318 | Instruction::BinaryOps Opcode = I.getOpcode(); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 319 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) |
| 320 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { |
| 321 | if (isa<Constant>(I.getOperand(1))) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 322 | Constant *Folded = ConstantExpr::get(I.getOpcode(), |
| 323 | cast<Constant>(I.getOperand(1)), |
| 324 | cast<Constant>(Op->getOperand(1))); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 325 | I.setOperand(0, Op->getOperand(0)); |
| 326 | I.setOperand(1, Folded); |
| 327 | return true; |
| 328 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) |
| 329 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && |
| 330 | isOnlyUse(Op) && isOnlyUse(Op1)) { |
| 331 | Constant *C1 = cast<Constant>(Op->getOperand(1)); |
| 332 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); |
| 333 | |
| 334 | // 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] | 335 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 336 | Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0), |
| 337 | Op1->getOperand(0), |
| 338 | Op1->getName(), &I); |
| 339 | WorkList.push_back(New); |
| 340 | I.setOperand(0, New); |
| 341 | I.setOperand(1, Folded); |
| 342 | return true; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 343 | } |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 344 | } |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 345 | return Changed; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 346 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 347 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 348 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
| 349 | // if the LHS is a constant zero (which is the 'negate' form). |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 350 | // |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 351 | static inline Value *dyn_castNegVal(Value *V) { |
| 352 | if (BinaryOperator::isNeg(V)) |
Chris Lattner | d6f636a | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 353 | return BinaryOperator::getNegArgument(V); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 354 | |
Chris Lattner | 9ad0d55 | 2004-12-14 20:08:06 +0000 | [diff] [blame] | 355 | // Constants can be considered to be negated values if they can be folded. |
| 356 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
| 357 | return ConstantExpr::getNeg(C); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 358 | return 0; |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 361 | static inline Value *dyn_castNotVal(Value *V) { |
| 362 | if (BinaryOperator::isNot(V)) |
Chris Lattner | d6f636a | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 363 | return BinaryOperator::getNotArgument(V); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 364 | |
| 365 | // Constants can be considered to be not'ed values... |
Chris Lattner | dd65d86 | 2003-04-30 22:34:06 +0000 | [diff] [blame] | 366 | if (ConstantIntegral *C = dyn_cast<ConstantIntegral>(V)) |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 367 | return ConstantExpr::getNot(C); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 368 | return 0; |
| 369 | } |
| 370 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 371 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
| 372 | // other computations (because it has a constant operand), return the |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 373 | // non-constant operand of the multiply, and set CST to point to the multiplier. |
| 374 | // Otherwise, return null. |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 375 | // |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 376 | static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) { |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 377 | if (V->hasOneUse() && V->getType()->isInteger()) |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 378 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 379 | if (I->getOpcode() == Instruction::Mul) |
Chris Lattner | 97013636 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 380 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 381 | return I->getOperand(0); |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 382 | if (I->getOpcode() == Instruction::Shl) |
Chris Lattner | 97013636 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 383 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) { |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 384 | // The multiplier is really 1 << CST. |
| 385 | Constant *One = ConstantInt::get(V->getType(), 1); |
| 386 | CST = cast<ConstantInt>(ConstantExpr::getShl(One, CST)); |
| 387 | return I->getOperand(0); |
| 388 | } |
| 389 | } |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 390 | return 0; |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 391 | } |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 392 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 393 | /// dyn_castGetElementPtr - If this is a getelementptr instruction or constant |
| 394 | /// expression, return it. |
| 395 | static User *dyn_castGetElementPtr(Value *V) { |
| 396 | if (isa<GetElementPtrInst>(V)) return cast<User>(V); |
| 397 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 398 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 399 | return cast<User>(V); |
| 400 | return false; |
| 401 | } |
| 402 | |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 403 | // AddOne, SubOne - Add or subtract a constant one from an integer constant... |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 404 | static ConstantInt *AddOne(ConstantInt *C) { |
| 405 | return cast<ConstantInt>(ConstantExpr::getAdd(C, |
| 406 | ConstantInt::get(C->getType(), 1))); |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 407 | } |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 408 | static ConstantInt *SubOne(ConstantInt *C) { |
| 409 | return cast<ConstantInt>(ConstantExpr::getSub(C, |
| 410 | ConstantInt::get(C->getType(), 1))); |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 413 | /// GetConstantInType - Return a ConstantInt with the specified type and value. |
| 414 | /// |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 415 | static ConstantIntegral *GetConstantInType(const Type *Ty, uint64_t Val) { |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 416 | if (Ty->isUnsigned()) |
| 417 | return ConstantUInt::get(Ty, Val); |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 418 | else if (Ty->getTypeID() == Type::BoolTyID) |
| 419 | return ConstantBool::get(Val); |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 420 | int64_t SVal = Val; |
| 421 | SVal <<= 64-Ty->getPrimitiveSizeInBits(); |
| 422 | SVal >>= 64-Ty->getPrimitiveSizeInBits(); |
| 423 | return ConstantSInt::get(Ty, SVal); |
| 424 | } |
| 425 | |
| 426 | |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 427 | /// ComputeMaskedBits - Determine which of the bits specified in Mask are |
| 428 | /// known to be either zero or one and return them in the KnownZero/KnownOne |
| 429 | /// bitsets. This code only analyzes bits in Mask, in order to short-circuit |
| 430 | /// processing. |
| 431 | static void ComputeMaskedBits(Value *V, uint64_t Mask, uint64_t &KnownZero, |
| 432 | uint64_t &KnownOne, unsigned Depth = 0) { |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 433 | // Note, we cannot consider 'undef' to be "IsZero" here. The problem is that |
| 434 | // we cannot optimize based on the assumption that it is zero without changing |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 435 | // it to be an explicit zero. If we don't change it to zero, other code could |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 436 | // optimized based on the contradictory assumption that it is non-zero. |
| 437 | // Because instcombine aggressively folds operations with undef args anyway, |
| 438 | // this won't lose us code quality. |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 439 | if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V)) { |
| 440 | // We know all of the bits for a constant! |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 441 | KnownOne = CI->getZExtValue() & Mask; |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 442 | KnownZero = ~KnownOne & Mask; |
| 443 | return; |
| 444 | } |
| 445 | |
| 446 | KnownZero = KnownOne = 0; // Don't know anything. |
Chris Lattner | 92a6865 | 2006-02-07 08:05:22 +0000 | [diff] [blame] | 447 | if (Depth == 6 || Mask == 0) |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 448 | return; // Limit search depth. |
| 449 | |
| 450 | uint64_t KnownZero2, KnownOne2; |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 451 | Instruction *I = dyn_cast<Instruction>(V); |
| 452 | if (!I) return; |
| 453 | |
| 454 | switch (I->getOpcode()) { |
| 455 | case Instruction::And: |
| 456 | // If either the LHS or the RHS are Zero, the result is zero. |
| 457 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
| 458 | Mask &= ~KnownZero; |
| 459 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); |
| 460 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 461 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 462 | |
| 463 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 464 | KnownOne &= KnownOne2; |
| 465 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 466 | KnownZero |= KnownZero2; |
| 467 | return; |
| 468 | case Instruction::Or: |
| 469 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
| 470 | Mask &= ~KnownOne; |
| 471 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); |
| 472 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 473 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 474 | |
| 475 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 476 | KnownZero &= KnownZero2; |
| 477 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 478 | KnownOne |= KnownOne2; |
| 479 | return; |
| 480 | case Instruction::Xor: { |
| 481 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
| 482 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); |
| 483 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 484 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 485 | |
| 486 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 487 | uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); |
| 488 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 489 | KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); |
| 490 | KnownZero = KnownZeroOut; |
| 491 | return; |
| 492 | } |
| 493 | case Instruction::Select: |
| 494 | ComputeMaskedBits(I->getOperand(2), Mask, KnownZero, KnownOne, Depth+1); |
| 495 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1); |
| 496 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 497 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 498 | |
| 499 | // Only known if known in both the LHS and RHS. |
| 500 | KnownOne &= KnownOne2; |
| 501 | KnownZero &= KnownZero2; |
| 502 | return; |
| 503 | case Instruction::Cast: { |
| 504 | const Type *SrcTy = I->getOperand(0)->getType(); |
| 505 | if (!SrcTy->isIntegral()) return; |
| 506 | |
| 507 | // If this is an integer truncate or noop, just look in the input. |
| 508 | if (SrcTy->getPrimitiveSizeInBits() >= |
| 509 | I->getType()->getPrimitiveSizeInBits()) { |
| 510 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 511 | return; |
| 512 | } |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 513 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 514 | // Sign or Zero extension. Compute the bits in the result that are not |
| 515 | // present in the input. |
| 516 | uint64_t NotIn = ~SrcTy->getIntegralTypeMask(); |
| 517 | uint64_t NewBits = I->getType()->getIntegralTypeMask() & NotIn; |
Chris Lattner | 62010c4 | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 518 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 519 | // Handle zero extension. |
| 520 | if (!SrcTy->isSigned()) { |
| 521 | Mask &= SrcTy->getIntegralTypeMask(); |
| 522 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
| 523 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 524 | // The top bits are known to be zero. |
| 525 | KnownZero |= NewBits; |
| 526 | } else { |
| 527 | // Sign extension. |
| 528 | Mask &= SrcTy->getIntegralTypeMask(); |
| 529 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
| 530 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Chris Lattner | 92a6865 | 2006-02-07 08:05:22 +0000 | [diff] [blame] | 531 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 532 | // If the sign bit of the input is known set or clear, then we know the |
| 533 | // top bits of the result. |
| 534 | uint64_t InSignBit = 1ULL << (SrcTy->getPrimitiveSizeInBits()-1); |
| 535 | if (KnownZero & InSignBit) { // Input sign bit known zero |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 536 | KnownZero |= NewBits; |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 537 | KnownOne &= ~NewBits; |
| 538 | } else if (KnownOne & InSignBit) { // Input sign bit known set |
| 539 | KnownOne |= NewBits; |
| 540 | KnownZero &= ~NewBits; |
| 541 | } else { // Input sign bit unknown |
| 542 | KnownZero &= ~NewBits; |
| 543 | KnownOne &= ~NewBits; |
| 544 | } |
| 545 | } |
| 546 | return; |
| 547 | } |
| 548 | case Instruction::Shl: |
| 549 | // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 |
| 550 | if (ConstantUInt *SA = dyn_cast<ConstantUInt>(I->getOperand(1))) { |
| 551 | Mask >>= SA->getValue(); |
| 552 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
| 553 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 554 | KnownZero <<= SA->getValue(); |
| 555 | KnownOne <<= SA->getValue(); |
| 556 | KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero. |
| 557 | return; |
| 558 | } |
| 559 | break; |
| 560 | case Instruction::Shr: |
| 561 | // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
| 562 | if (ConstantUInt *SA = dyn_cast<ConstantUInt>(I->getOperand(1))) { |
| 563 | // Compute the new bits that are at the top now. |
| 564 | uint64_t HighBits = (1ULL << SA->getValue())-1; |
| 565 | HighBits <<= I->getType()->getPrimitiveSizeInBits()-SA->getValue(); |
| 566 | |
| 567 | if (I->getType()->isUnsigned()) { // Unsigned shift right. |
| 568 | Mask <<= SA->getValue(); |
| 569 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero,KnownOne,Depth+1); |
| 570 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 571 | KnownZero >>= SA->getValue(); |
| 572 | KnownOne >>= SA->getValue(); |
| 573 | KnownZero |= HighBits; // high bits known zero. |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 574 | } else { |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 575 | Mask <<= SA->getValue(); |
| 576 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero,KnownOne,Depth+1); |
| 577 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 578 | KnownZero >>= SA->getValue(); |
| 579 | KnownOne >>= SA->getValue(); |
| 580 | |
| 581 | // Handle the sign bits. |
| 582 | uint64_t SignBit = 1ULL << (I->getType()->getPrimitiveSizeInBits()-1); |
| 583 | SignBit >>= SA->getValue(); // Adjust to where it is now in the mask. |
| 584 | |
| 585 | if (KnownZero & SignBit) { // New bits are known zero. |
| 586 | KnownZero |= HighBits; |
| 587 | } else if (KnownOne & SignBit) { // New bits are known one. |
| 588 | KnownOne |= HighBits; |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | return; |
Chris Lattner | 62010c4 | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 592 | } |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 593 | break; |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 594 | } |
Chris Lattner | 92a6865 | 2006-02-07 08:05:22 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use |
| 598 | /// this predicate to simplify operations downstream. Mask is known to be zero |
| 599 | /// for bits that V cannot have. |
| 600 | static bool MaskedValueIsZero(Value *V, uint64_t Mask, unsigned Depth = 0) { |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 601 | uint64_t KnownZero, KnownOne; |
| 602 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne, Depth); |
| 603 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 604 | return (KnownZero & Mask) == Mask; |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 607 | /// ShrinkDemandedConstant - Check to see if the specified operand of the |
| 608 | /// specified instruction is a constant integer. If so, check to see if there |
| 609 | /// are any bits set in the constant that are not demanded. If so, shrink the |
| 610 | /// constant and return true. |
| 611 | static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, |
| 612 | uint64_t Demanded) { |
| 613 | ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo)); |
| 614 | if (!OpC) return false; |
| 615 | |
| 616 | // If there are no bits set that aren't demanded, nothing to do. |
| 617 | if ((~Demanded & OpC->getZExtValue()) == 0) |
| 618 | return false; |
| 619 | |
| 620 | // This is producing any bits that are not needed, shrink the RHS. |
| 621 | uint64_t Val = Demanded & OpC->getZExtValue(); |
| 622 | I->setOperand(OpNo, GetConstantInType(OpC->getType(), Val)); |
| 623 | return true; |
| 624 | } |
| 625 | |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 626 | // ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a |
| 627 | // set of known zero and one bits, compute the maximum and minimum values that |
| 628 | // could have the specified known zero and known one bits, returning them in |
| 629 | // min/max. |
| 630 | static void ComputeSignedMinMaxValuesFromKnownBits(const Type *Ty, |
| 631 | uint64_t KnownZero, |
| 632 | uint64_t KnownOne, |
| 633 | int64_t &Min, int64_t &Max) { |
| 634 | uint64_t TypeBits = Ty->getIntegralTypeMask(); |
| 635 | uint64_t UnknownBits = ~(KnownZero|KnownOne) & TypeBits; |
| 636 | |
| 637 | uint64_t SignBit = 1ULL << (Ty->getPrimitiveSizeInBits()-1); |
| 638 | |
| 639 | // The minimum value is when all unknown bits are zeros, EXCEPT for the sign |
| 640 | // bit if it is unknown. |
| 641 | Min = KnownOne; |
| 642 | Max = KnownOne|UnknownBits; |
| 643 | |
| 644 | if (SignBit & UnknownBits) { // Sign bit is unknown |
| 645 | Min |= SignBit; |
| 646 | Max &= ~SignBit; |
| 647 | } |
| 648 | |
| 649 | // Sign extend the min/max values. |
| 650 | int ShAmt = 64-Ty->getPrimitiveSizeInBits(); |
| 651 | Min = (Min << ShAmt) >> ShAmt; |
| 652 | Max = (Max << ShAmt) >> ShAmt; |
| 653 | } |
| 654 | |
| 655 | // ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and |
| 656 | // a set of known zero and one bits, compute the maximum and minimum values that |
| 657 | // could have the specified known zero and known one bits, returning them in |
| 658 | // min/max. |
| 659 | static void ComputeUnsignedMinMaxValuesFromKnownBits(const Type *Ty, |
| 660 | uint64_t KnownZero, |
| 661 | uint64_t KnownOne, |
| 662 | uint64_t &Min, |
| 663 | uint64_t &Max) { |
| 664 | uint64_t TypeBits = Ty->getIntegralTypeMask(); |
| 665 | uint64_t UnknownBits = ~(KnownZero|KnownOne) & TypeBits; |
| 666 | |
| 667 | // The minimum value is when the unknown bits are all zeros. |
| 668 | Min = KnownOne; |
| 669 | // The maximum value is when the unknown bits are all ones. |
| 670 | Max = KnownOne|UnknownBits; |
| 671 | } |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 672 | |
| 673 | |
| 674 | /// SimplifyDemandedBits - Look at V. At this point, we know that only the |
| 675 | /// DemandedMask bits of the result of V are ever used downstream. If we can |
| 676 | /// use this information to simplify V, do so and return true. Otherwise, |
| 677 | /// analyze the expression and return a mask of KnownOne and KnownZero bits for |
| 678 | /// the expression (used to simplify the caller). The KnownZero/One bits may |
| 679 | /// only be accurate for those bits in the DemandedMask. |
| 680 | bool InstCombiner::SimplifyDemandedBits(Value *V, uint64_t DemandedMask, |
| 681 | uint64_t &KnownZero, uint64_t &KnownOne, |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 682 | unsigned Depth) { |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 683 | if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V)) { |
| 684 | // We know all of the bits for a constant! |
| 685 | KnownOne = CI->getZExtValue() & DemandedMask; |
| 686 | KnownZero = ~KnownOne & DemandedMask; |
| 687 | return false; |
| 688 | } |
| 689 | |
| 690 | KnownZero = KnownOne = 0; |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 691 | if (!V->hasOneUse()) { // Other users may use these bits. |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 692 | if (Depth != 0) { // Not at the root. |
| 693 | // Just compute the KnownZero/KnownOne bits to simplify things downstream. |
| 694 | ComputeMaskedBits(V, DemandedMask, KnownZero, KnownOne, Depth); |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 695 | return false; |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 696 | } |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 697 | // If this is the root being simplified, allow it to have multiple uses, |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 698 | // just set the DemandedMask to all bits. |
| 699 | DemandedMask = V->getType()->getIntegralTypeMask(); |
| 700 | } else if (DemandedMask == 0) { // Not demanding any bits from V. |
Chris Lattner | 92a6865 | 2006-02-07 08:05:22 +0000 | [diff] [blame] | 701 | if (V != UndefValue::get(V->getType())) |
| 702 | return UpdateValueUsesWith(V, UndefValue::get(V->getType())); |
| 703 | return false; |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 704 | } else if (Depth == 6) { // Limit search depth. |
| 705 | return false; |
| 706 | } |
| 707 | |
| 708 | Instruction *I = dyn_cast<Instruction>(V); |
| 709 | if (!I) return false; // Only analyze instructions. |
| 710 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 711 | uint64_t KnownZero2, KnownOne2; |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 712 | switch (I->getOpcode()) { |
| 713 | default: break; |
| 714 | case Instruction::And: |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 715 | // If either the LHS or the RHS are Zero, the result is zero. |
| 716 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 717 | KnownZero, KnownOne, Depth+1)) |
| 718 | return true; |
| 719 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 720 | |
| 721 | // If something is known zero on the RHS, the bits aren't demanded on the |
| 722 | // LHS. |
| 723 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~KnownZero, |
| 724 | KnownZero2, KnownOne2, Depth+1)) |
| 725 | return true; |
| 726 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 727 | |
| 728 | // If all of the demanded bits are known one on one side, return the other. |
| 729 | // These bits cannot contribute to the result of the 'and'. |
| 730 | if ((DemandedMask & ~KnownZero2 & KnownOne) == (DemandedMask & ~KnownZero2)) |
| 731 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 732 | if ((DemandedMask & ~KnownZero & KnownOne2) == (DemandedMask & ~KnownZero)) |
| 733 | return UpdateValueUsesWith(I, I->getOperand(1)); |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 734 | |
| 735 | // If all of the demanded bits in the inputs are known zeros, return zero. |
| 736 | if ((DemandedMask & (KnownZero|KnownZero2)) == DemandedMask) |
| 737 | return UpdateValueUsesWith(I, Constant::getNullValue(I->getType())); |
| 738 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 739 | // If the RHS is a constant, see if we can simplify it. |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 740 | if (ShrinkDemandedConstant(I, 1, DemandedMask & ~KnownZero2)) |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 741 | return UpdateValueUsesWith(I, I); |
| 742 | |
| 743 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 744 | KnownOne &= KnownOne2; |
| 745 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 746 | KnownZero |= KnownZero2; |
| 747 | break; |
| 748 | case Instruction::Or: |
| 749 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 750 | KnownZero, KnownOne, Depth+1)) |
| 751 | return true; |
| 752 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 753 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~KnownOne, |
| 754 | KnownZero2, KnownOne2, Depth+1)) |
| 755 | return true; |
| 756 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 757 | |
| 758 | // If all of the demanded bits are known zero on one side, return the other. |
| 759 | // These bits cannot contribute to the result of the 'or'. |
Jeff Cohen | 0add83e | 2006-02-18 03:20:33 +0000 | [diff] [blame] | 760 | if ((DemandedMask & ~KnownOne2 & KnownZero) == (DemandedMask & ~KnownOne2)) |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 761 | return UpdateValueUsesWith(I, I->getOperand(0)); |
Jeff Cohen | 0add83e | 2006-02-18 03:20:33 +0000 | [diff] [blame] | 762 | if ((DemandedMask & ~KnownOne & KnownZero2) == (DemandedMask & ~KnownOne)) |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 763 | return UpdateValueUsesWith(I, I->getOperand(1)); |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 764 | |
| 765 | // If all of the potentially set bits on one side are known to be set on |
| 766 | // the other side, just use the 'other' side. |
| 767 | if ((DemandedMask & (~KnownZero) & KnownOne2) == |
| 768 | (DemandedMask & (~KnownZero))) |
| 769 | return UpdateValueUsesWith(I, I->getOperand(0)); |
Nate Begeman | 8a77efe | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 770 | if ((DemandedMask & (~KnownZero2) & KnownOne) == |
| 771 | (DemandedMask & (~KnownZero2))) |
| 772 | return UpdateValueUsesWith(I, I->getOperand(1)); |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 773 | |
| 774 | // If the RHS is a constant, see if we can simplify it. |
| 775 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 776 | return UpdateValueUsesWith(I, I); |
| 777 | |
| 778 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 779 | KnownZero &= KnownZero2; |
| 780 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 781 | KnownOne |= KnownOne2; |
| 782 | break; |
| 783 | case Instruction::Xor: { |
| 784 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 785 | KnownZero, KnownOne, Depth+1)) |
| 786 | return true; |
| 787 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 788 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 789 | KnownZero2, KnownOne2, Depth+1)) |
| 790 | return true; |
| 791 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 792 | |
| 793 | // If all of the demanded bits are known zero on one side, return the other. |
| 794 | // These bits cannot contribute to the result of the 'xor'. |
| 795 | if ((DemandedMask & KnownZero) == DemandedMask) |
| 796 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 797 | if ((DemandedMask & KnownZero2) == DemandedMask) |
| 798 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 799 | |
| 800 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 801 | uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); |
| 802 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 803 | uint64_t KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); |
| 804 | |
| 805 | // If all of the unknown bits are known to be zero on one side or the other |
| 806 | // (but not both) turn this into an *inclusive* or. |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 807 | // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 808 | if (uint64_t UnknownBits = DemandedMask & ~(KnownZeroOut|KnownOneOut)) { |
| 809 | if ((UnknownBits & (KnownZero|KnownZero2)) == UnknownBits) { |
| 810 | Instruction *Or = |
| 811 | BinaryOperator::createOr(I->getOperand(0), I->getOperand(1), |
| 812 | I->getName()); |
| 813 | InsertNewInstBefore(Or, *I); |
| 814 | return UpdateValueUsesWith(I, Or); |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 815 | } |
| 816 | } |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 817 | |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 818 | // If all of the demanded bits on one side are known, and all of the set |
| 819 | // bits on that side are also known to be set on the other side, turn this |
| 820 | // into an AND, as we know the bits will be cleared. |
| 821 | // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 |
| 822 | if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask) { // all known |
| 823 | if ((KnownOne & KnownOne2) == KnownOne) { |
| 824 | Constant *AndC = GetConstantInType(I->getType(), |
| 825 | ~KnownOne & DemandedMask); |
| 826 | Instruction *And = |
| 827 | BinaryOperator::createAnd(I->getOperand(0), AndC, "tmp"); |
| 828 | InsertNewInstBefore(And, *I); |
| 829 | return UpdateValueUsesWith(I, And); |
| 830 | } |
| 831 | } |
| 832 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 833 | // If the RHS is a constant, see if we can simplify it. |
| 834 | // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1. |
| 835 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 836 | return UpdateValueUsesWith(I, I); |
| 837 | |
| 838 | KnownZero = KnownZeroOut; |
| 839 | KnownOne = KnownOneOut; |
| 840 | break; |
| 841 | } |
| 842 | case Instruction::Select: |
| 843 | if (SimplifyDemandedBits(I->getOperand(2), DemandedMask, |
| 844 | KnownZero, KnownOne, Depth+1)) |
| 845 | return true; |
| 846 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 847 | KnownZero2, KnownOne2, Depth+1)) |
| 848 | return true; |
| 849 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 850 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 851 | |
| 852 | // If the operands are constants, see if we can simplify them. |
| 853 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 854 | return UpdateValueUsesWith(I, I); |
| 855 | if (ShrinkDemandedConstant(I, 2, DemandedMask)) |
| 856 | return UpdateValueUsesWith(I, I); |
| 857 | |
| 858 | // Only known if known in both the LHS and RHS. |
| 859 | KnownOne &= KnownOne2; |
| 860 | KnownZero &= KnownZero2; |
| 861 | break; |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 862 | case Instruction::Cast: { |
| 863 | const Type *SrcTy = I->getOperand(0)->getType(); |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 864 | if (!SrcTy->isIntegral()) return false; |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 865 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 866 | // If this is an integer truncate or noop, just look in the input. |
| 867 | if (SrcTy->getPrimitiveSizeInBits() >= |
| 868 | I->getType()->getPrimitiveSizeInBits()) { |
| 869 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 870 | KnownZero, KnownOne, Depth+1)) |
| 871 | return true; |
| 872 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 873 | break; |
| 874 | } |
| 875 | |
| 876 | // Sign or Zero extension. Compute the bits in the result that are not |
| 877 | // present in the input. |
| 878 | uint64_t NotIn = ~SrcTy->getIntegralTypeMask(); |
| 879 | uint64_t NewBits = I->getType()->getIntegralTypeMask() & NotIn; |
| 880 | |
| 881 | // Handle zero extension. |
| 882 | if (!SrcTy->isSigned()) { |
| 883 | DemandedMask &= SrcTy->getIntegralTypeMask(); |
| 884 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 885 | KnownZero, KnownOne, Depth+1)) |
| 886 | return true; |
| 887 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 888 | // The top bits are known to be zero. |
| 889 | KnownZero |= NewBits; |
| 890 | } else { |
| 891 | // Sign extension. |
Chris Lattner | 7d85228 | 2006-02-13 22:41:07 +0000 | [diff] [blame] | 892 | uint64_t InSignBit = 1ULL << (SrcTy->getPrimitiveSizeInBits()-1); |
| 893 | int64_t InputDemandedBits = DemandedMask & SrcTy->getIntegralTypeMask(); |
| 894 | |
| 895 | // If any of the sign extended bits are demanded, we know that the sign |
| 896 | // bit is demanded. |
| 897 | if (NewBits & DemandedMask) |
| 898 | InputDemandedBits |= InSignBit; |
| 899 | |
| 900 | if (SimplifyDemandedBits(I->getOperand(0), InputDemandedBits, |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 901 | KnownZero, KnownOne, Depth+1)) |
| 902 | return true; |
| 903 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 904 | |
| 905 | // If the sign bit of the input is known set or clear, then we know the |
| 906 | // top bits of the result. |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 907 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 908 | // If the input sign bit is known zero, or if the NewBits are not demanded |
| 909 | // convert this into a zero extension. |
| 910 | if ((KnownZero & InSignBit) || (NewBits & ~DemandedMask) == NewBits) { |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 911 | // Convert to unsigned first. |
Chris Lattner | 4431482 | 2006-02-07 19:07:40 +0000 | [diff] [blame] | 912 | Instruction *NewVal; |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 913 | NewVal = new CastInst(I->getOperand(0), SrcTy->getUnsignedVersion(), |
Chris Lattner | 4431482 | 2006-02-07 19:07:40 +0000 | [diff] [blame] | 914 | I->getOperand(0)->getName()); |
| 915 | InsertNewInstBefore(NewVal, *I); |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 916 | // Then cast that to the destination type. |
Chris Lattner | 4431482 | 2006-02-07 19:07:40 +0000 | [diff] [blame] | 917 | NewVal = new CastInst(NewVal, I->getType(), I->getName()); |
| 918 | InsertNewInstBefore(NewVal, *I); |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 919 | return UpdateValueUsesWith(I, NewVal); |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 920 | } else if (KnownOne & InSignBit) { // Input sign bit known set |
| 921 | KnownOne |= NewBits; |
| 922 | KnownZero &= ~NewBits; |
| 923 | } else { // Input sign bit unknown |
| 924 | KnownZero &= ~NewBits; |
| 925 | KnownOne &= ~NewBits; |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 926 | } |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 927 | } |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 928 | break; |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 929 | } |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 930 | case Instruction::Shl: |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 931 | if (ConstantUInt *SA = dyn_cast<ConstantUInt>(I->getOperand(1))) { |
| 932 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask >> SA->getValue(), |
| 933 | KnownZero, KnownOne, Depth+1)) |
| 934 | return true; |
| 935 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 936 | KnownZero <<= SA->getValue(); |
| 937 | KnownOne <<= SA->getValue(); |
| 938 | KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero. |
| 939 | } |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 940 | break; |
| 941 | case Instruction::Shr: |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 942 | if (ConstantUInt *SA = dyn_cast<ConstantUInt>(I->getOperand(1))) { |
| 943 | unsigned ShAmt = SA->getValue(); |
| 944 | |
| 945 | // Compute the new bits that are at the top now. |
| 946 | uint64_t HighBits = (1ULL << ShAmt)-1; |
| 947 | HighBits <<= I->getType()->getPrimitiveSizeInBits() - ShAmt; |
Chris Lattner | 68e7475 | 2006-02-13 06:09:08 +0000 | [diff] [blame] | 948 | uint64_t TypeMask = I->getType()->getIntegralTypeMask(); |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 949 | if (I->getType()->isUnsigned()) { // Unsigned shift right. |
Chris Lattner | 68e7475 | 2006-02-13 06:09:08 +0000 | [diff] [blame] | 950 | if (SimplifyDemandedBits(I->getOperand(0), |
| 951 | (DemandedMask << ShAmt) & TypeMask, |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 952 | KnownZero, KnownOne, Depth+1)) |
| 953 | return true; |
| 954 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Chris Lattner | 68e7475 | 2006-02-13 06:09:08 +0000 | [diff] [blame] | 955 | KnownZero &= TypeMask; |
| 956 | KnownOne &= TypeMask; |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 957 | KnownZero >>= ShAmt; |
| 958 | KnownOne >>= ShAmt; |
| 959 | KnownZero |= HighBits; // high bits known zero. |
| 960 | } else { // Signed shift right. |
Chris Lattner | 68e7475 | 2006-02-13 06:09:08 +0000 | [diff] [blame] | 961 | if (SimplifyDemandedBits(I->getOperand(0), |
| 962 | (DemandedMask << ShAmt) & TypeMask, |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 963 | KnownZero, KnownOne, Depth+1)) |
| 964 | return true; |
| 965 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Chris Lattner | 68e7475 | 2006-02-13 06:09:08 +0000 | [diff] [blame] | 966 | KnownZero &= TypeMask; |
| 967 | KnownOne &= TypeMask; |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 968 | KnownZero >>= SA->getValue(); |
| 969 | KnownOne >>= SA->getValue(); |
| 970 | |
| 971 | // Handle the sign bits. |
| 972 | uint64_t SignBit = 1ULL << (I->getType()->getPrimitiveSizeInBits()-1); |
| 973 | SignBit >>= SA->getValue(); // Adjust to where it is now in the mask. |
| 974 | |
| 975 | // If the input sign bit is known to be zero, or if none of the top bits |
| 976 | // are demanded, turn this into an unsigned shift right. |
| 977 | if ((KnownZero & SignBit) || (HighBits & ~DemandedMask) == HighBits) { |
| 978 | // Convert the input to unsigned. |
| 979 | Instruction *NewVal; |
| 980 | NewVal = new CastInst(I->getOperand(0), |
| 981 | I->getType()->getUnsignedVersion(), |
| 982 | I->getOperand(0)->getName()); |
| 983 | InsertNewInstBefore(NewVal, *I); |
| 984 | // Perform the unsigned shift right. |
| 985 | NewVal = new ShiftInst(Instruction::Shr, NewVal, SA, I->getName()); |
| 986 | InsertNewInstBefore(NewVal, *I); |
| 987 | // Then cast that to the destination type. |
| 988 | NewVal = new CastInst(NewVal, I->getType(), I->getName()); |
| 989 | InsertNewInstBefore(NewVal, *I); |
| 990 | return UpdateValueUsesWith(I, NewVal); |
| 991 | } else if (KnownOne & SignBit) { // New bits are known one. |
| 992 | KnownOne |= HighBits; |
| 993 | } |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 994 | } |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 995 | } |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 996 | break; |
| 997 | } |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 998 | |
| 999 | // If the client is only demanding bits that we know, return the known |
| 1000 | // constant. |
| 1001 | if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask) |
| 1002 | return UpdateValueUsesWith(I, GetConstantInType(I->getType(), KnownOne)); |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1003 | return false; |
| 1004 | } |
| 1005 | |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 1006 | // isTrueWhenEqual - Return true if the specified setcondinst instruction is |
| 1007 | // true when both operands are equal... |
| 1008 | // |
| 1009 | static bool isTrueWhenEqual(Instruction &I) { |
| 1010 | return I.getOpcode() == Instruction::SetEQ || |
| 1011 | I.getOpcode() == Instruction::SetGE || |
| 1012 | I.getOpcode() == Instruction::SetLE; |
| 1013 | } |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1014 | |
| 1015 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
| 1016 | /// function is designed to check a chain of associative operators for a |
| 1017 | /// potential to apply a certain optimization. Since the optimization may be |
| 1018 | /// applicable if the expression was reassociated, this checks the chain, then |
| 1019 | /// reassociates the expression as necessary to expose the optimization |
| 1020 | /// opportunity. This makes use of a special Functor, which must define |
| 1021 | /// 'shouldApply' and 'apply' methods. |
| 1022 | /// |
| 1023 | template<typename Functor> |
| 1024 | Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { |
| 1025 | unsigned Opcode = Root.getOpcode(); |
| 1026 | Value *LHS = Root.getOperand(0); |
| 1027 | |
| 1028 | // Quick check, see if the immediate LHS matches... |
| 1029 | if (F.shouldApply(LHS)) |
| 1030 | return F.apply(Root); |
| 1031 | |
| 1032 | // Otherwise, if the LHS is not of the same opcode as the root, return. |
| 1033 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1034 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1035 | // Should we apply this transform to the RHS? |
| 1036 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); |
| 1037 | |
| 1038 | // If not to the RHS, check to see if we should apply to the LHS... |
| 1039 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { |
| 1040 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS |
| 1041 | ShouldApply = true; |
| 1042 | } |
| 1043 | |
| 1044 | // If the functor wants to apply the optimization to the RHS of LHSI, |
| 1045 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) |
| 1046 | if (ShouldApply) { |
| 1047 | BasicBlock *BB = Root.getParent(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1048 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1049 | // Now all of the instructions are in the current basic block, go ahead |
| 1050 | // and perform the reassociation. |
| 1051 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); |
| 1052 | |
| 1053 | // First move the selected RHS to the LHS of the root... |
| 1054 | Root.setOperand(0, LHSI->getOperand(1)); |
| 1055 | |
| 1056 | // Make what used to be the LHS of the root be the user of the root... |
| 1057 | Value *ExtraOperand = TmpLHSI->getOperand(1); |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1058 | if (&Root == TmpLHSI) { |
Chris Lattner | 8953b90 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 1059 | Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); |
| 1060 | return 0; |
| 1061 | } |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1062 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1063 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1064 | TmpLHSI->getParent()->getInstList().remove(TmpLHSI); |
| 1065 | BasicBlock::iterator ARI = &Root; ++ARI; |
| 1066 | BB->getInstList().insert(ARI, TmpLHSI); // Move TmpLHSI to after Root |
| 1067 | ARI = Root; |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1068 | |
| 1069 | // Now propagate the ExtraOperand down the chain of instructions until we |
| 1070 | // get to LHSI. |
| 1071 | while (TmpLHSI != LHSI) { |
| 1072 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1073 | // Move the instruction to immediately before the chain we are |
| 1074 | // constructing to avoid breaking dominance properties. |
| 1075 | NextLHSI->getParent()->getInstList().remove(NextLHSI); |
| 1076 | BB->getInstList().insert(ARI, NextLHSI); |
| 1077 | ARI = NextLHSI; |
| 1078 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1079 | Value *NextOp = NextLHSI->getOperand(1); |
| 1080 | NextLHSI->setOperand(1, ExtraOperand); |
| 1081 | TmpLHSI = NextLHSI; |
| 1082 | ExtraOperand = NextOp; |
| 1083 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1084 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1085 | // Now that the instructions are reassociated, have the functor perform |
| 1086 | // the transformation... |
| 1087 | return F.apply(Root); |
| 1088 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1089 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1090 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
| 1091 | } |
| 1092 | return 0; |
| 1093 | } |
| 1094 | |
| 1095 | |
| 1096 | // AddRHS - Implements: X + X --> X << 1 |
| 1097 | struct AddRHS { |
| 1098 | Value *RHS; |
| 1099 | AddRHS(Value *rhs) : RHS(rhs) {} |
| 1100 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 1101 | Instruction *apply(BinaryOperator &Add) const { |
| 1102 | return new ShiftInst(Instruction::Shl, Add.getOperand(0), |
| 1103 | ConstantInt::get(Type::UByteTy, 1)); |
| 1104 | } |
| 1105 | }; |
| 1106 | |
| 1107 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) |
| 1108 | // iff C1&C2 == 0 |
| 1109 | struct AddMaskingAnd { |
| 1110 | Constant *C2; |
| 1111 | AddMaskingAnd(Constant *c) : C2(c) {} |
| 1112 | bool shouldApply(Value *LHS) const { |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1113 | ConstantInt *C1; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1114 | return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1115 | ConstantExpr::getAnd(C1, C2)->isNullValue(); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1116 | } |
| 1117 | Instruction *apply(BinaryOperator &Add) const { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1118 | return BinaryOperator::createOr(Add.getOperand(0), Add.getOperand(1)); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1119 | } |
| 1120 | }; |
| 1121 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1122 | static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1123 | InstCombiner *IC) { |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1124 | if (isa<CastInst>(I)) { |
| 1125 | if (Constant *SOC = dyn_cast<Constant>(SO)) |
| 1126 | return ConstantExpr::getCast(SOC, I.getType()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1127 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1128 | return IC->InsertNewInstBefore(new CastInst(SO, I.getType(), |
| 1129 | SO->getName() + ".cast"), I); |
| 1130 | } |
| 1131 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1132 | // Figure out if the constant is the left or the right argument. |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1133 | bool ConstIsRHS = isa<Constant>(I.getOperand(1)); |
| 1134 | Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS)); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1135 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1136 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
| 1137 | if (ConstIsRHS) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1138 | return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand); |
| 1139 | return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC); |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | Value *Op0 = SO, *Op1 = ConstOperand; |
| 1143 | if (!ConstIsRHS) |
| 1144 | std::swap(Op0, Op1); |
| 1145 | Instruction *New; |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1146 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| 1147 | New = BinaryOperator::create(BO->getOpcode(), Op0, Op1,SO->getName()+".op"); |
| 1148 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(&I)) |
| 1149 | New = new ShiftInst(SI->getOpcode(), Op0, Op1, SO->getName()+".sh"); |
Chris Lattner | f9d9665 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 1150 | else { |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1151 | assert(0 && "Unknown binary instruction type!"); |
Chris Lattner | f9d9665 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 1152 | abort(); |
| 1153 | } |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1154 | return IC->InsertNewInstBefore(New, I); |
| 1155 | } |
| 1156 | |
| 1157 | // FoldOpIntoSelect - Given an instruction with a select as one operand and a |
| 1158 | // constant as the other operand, try to fold the binary operator into the |
| 1159 | // select arguments. This also works for Cast instructions, which obviously do |
| 1160 | // not have a second operand. |
| 1161 | static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI, |
| 1162 | InstCombiner *IC) { |
| 1163 | // Don't modify shared select instructions |
| 1164 | if (!SI->hasOneUse()) return 0; |
| 1165 | Value *TV = SI->getOperand(1); |
| 1166 | Value *FV = SI->getOperand(2); |
| 1167 | |
| 1168 | if (isa<Constant>(TV) || isa<Constant>(FV)) { |
Chris Lattner | 374e659 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 1169 | // Bool selects with constant operands can be folded to logical ops. |
| 1170 | if (SI->getType() == Type::BoolTy) return 0; |
| 1171 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1172 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC); |
| 1173 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC); |
| 1174 | |
| 1175 | return new SelectInst(SI->getCondition(), SelectTrueVal, |
| 1176 | SelectFalseVal); |
| 1177 | } |
| 1178 | return 0; |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1181 | |
| 1182 | /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI |
| 1183 | /// node as operand #0, see if we can fold the instruction into the PHI (which |
| 1184 | /// is only possible if all operands to the PHI are constants). |
| 1185 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { |
| 1186 | PHINode *PN = cast<PHINode>(I.getOperand(0)); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1187 | unsigned NumPHIValues = PN->getNumIncomingValues(); |
| 1188 | if (!PN->hasOneUse() || NumPHIValues == 0 || |
| 1189 | !isa<Constant>(PN->getIncomingValue(0))) return 0; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1190 | |
| 1191 | // Check to see if all of the operands of the PHI are constants. If not, we |
| 1192 | // cannot do the transformation. |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1193 | for (unsigned i = 1; i != NumPHIValues; ++i) |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1194 | if (!isa<Constant>(PN->getIncomingValue(i))) |
| 1195 | return 0; |
| 1196 | |
| 1197 | // Okay, we can do the transformation: create the new PHI node. |
| 1198 | PHINode *NewPN = new PHINode(I.getType(), I.getName()); |
| 1199 | I.setName(""); |
Chris Lattner | d8e2018 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 1200 | NewPN->reserveOperandSpace(PN->getNumOperands()/2); |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1201 | InsertNewInstBefore(NewPN, *PN); |
| 1202 | |
| 1203 | // Next, add all of the operands to the PHI. |
| 1204 | if (I.getNumOperands() == 2) { |
| 1205 | Constant *C = cast<Constant>(I.getOperand(1)); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1206 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1207 | Constant *InV = cast<Constant>(PN->getIncomingValue(i)); |
| 1208 | NewPN->addIncoming(ConstantExpr::get(I.getOpcode(), InV, C), |
| 1209 | PN->getIncomingBlock(i)); |
| 1210 | } |
| 1211 | } else { |
| 1212 | assert(isa<CastInst>(I) && "Unary op should be a cast!"); |
| 1213 | const Type *RetTy = I.getType(); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1214 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1215 | Constant *InV = cast<Constant>(PN->getIncomingValue(i)); |
| 1216 | NewPN->addIncoming(ConstantExpr::getCast(InV, RetTy), |
| 1217 | PN->getIncomingBlock(i)); |
| 1218 | } |
| 1219 | } |
| 1220 | return ReplaceInstUsesWith(I, NewPN); |
| 1221 | } |
| 1222 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1223 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1224 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1225 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1226 | |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1227 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1228 | // X + undef -> undef |
| 1229 | if (isa<UndefValue>(RHS)) |
| 1230 | return ReplaceInstUsesWith(I, RHS); |
| 1231 | |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1232 | // X + 0 --> X |
Chris Lattner | 7fde91e | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 1233 | if (!I.getType()->isFloatingPoint()) { // NOTE: -0 + +0 = +0. |
| 1234 | if (RHSC->isNullValue()) |
| 1235 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | da1b152 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 1236 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
| 1237 | if (CFP->isExactlyValue(-0.0)) |
| 1238 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 7fde91e | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 1239 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1240 | |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1241 | // X + (signbit) --> X ^ signbit |
| 1242 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { |
Chris Lattner | 92a6865 | 2006-02-07 08:05:22 +0000 | [diff] [blame] | 1243 | uint64_t Val = CI->getZExtValue(); |
Chris Lattner | 77defba | 2006-02-07 07:00:41 +0000 | [diff] [blame] | 1244 | if (Val == (1ULL << (CI->getType()->getPrimitiveSizeInBits()-1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1245 | return BinaryOperator::createXor(LHS, RHS); |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1246 | } |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1247 | |
| 1248 | if (isa<PHINode>(LHS)) |
| 1249 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1250 | return NV; |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1251 | |
Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 1252 | ConstantInt *XorRHS = 0; |
| 1253 | Value *XorLHS = 0; |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1254 | if (match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) { |
| 1255 | unsigned TySizeBits = I.getType()->getPrimitiveSizeInBits(); |
| 1256 | int64_t RHSSExt = cast<ConstantInt>(RHSC)->getSExtValue(); |
| 1257 | uint64_t RHSZExt = cast<ConstantInt>(RHSC)->getZExtValue(); |
| 1258 | |
| 1259 | uint64_t C0080Val = 1ULL << 31; |
| 1260 | int64_t CFF80Val = -C0080Val; |
| 1261 | unsigned Size = 32; |
| 1262 | do { |
| 1263 | if (TySizeBits > Size) { |
| 1264 | bool Found = false; |
| 1265 | // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext. |
| 1266 | // If we have ADD(XOR(AND(X, 0xFF), 0xF..F80), 0x80), it's a sext. |
| 1267 | if (RHSSExt == CFF80Val) { |
| 1268 | if (XorRHS->getZExtValue() == C0080Val) |
| 1269 | Found = true; |
| 1270 | } else if (RHSZExt == C0080Val) { |
| 1271 | if (XorRHS->getSExtValue() == CFF80Val) |
| 1272 | Found = true; |
| 1273 | } |
| 1274 | if (Found) { |
| 1275 | // This is a sign extend if the top bits are known zero. |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 1276 | uint64_t Mask = ~0ULL; |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 1277 | Mask <<= 64-(TySizeBits-Size); |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 1278 | Mask &= XorLHS->getType()->getIntegralTypeMask(); |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 1279 | if (!MaskedValueIsZero(XorLHS, Mask)) |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1280 | Size = 0; // Not a sign ext, but can't be any others either. |
| 1281 | goto FoundSExt; |
| 1282 | } |
| 1283 | } |
| 1284 | Size >>= 1; |
| 1285 | C0080Val >>= Size; |
| 1286 | CFF80Val >>= Size; |
| 1287 | } while (Size >= 8); |
| 1288 | |
| 1289 | FoundSExt: |
| 1290 | const Type *MiddleType = 0; |
| 1291 | switch (Size) { |
| 1292 | default: break; |
| 1293 | case 32: MiddleType = Type::IntTy; break; |
| 1294 | case 16: MiddleType = Type::ShortTy; break; |
| 1295 | case 8: MiddleType = Type::SByteTy; break; |
| 1296 | } |
| 1297 | if (MiddleType) { |
| 1298 | Instruction *NewTrunc = new CastInst(XorLHS, MiddleType, "sext"); |
| 1299 | InsertNewInstBefore(NewTrunc, I); |
| 1300 | return new CastInst(NewTrunc, I.getType()); |
| 1301 | } |
| 1302 | } |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1303 | } |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1304 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1305 | // X + X --> X << 1 |
Robert Bocchino | 7b5b86c | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 1306 | if (I.getType()->isInteger()) { |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1307 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; |
Chris Lattner | 4706046 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 1308 | |
| 1309 | if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) { |
| 1310 | if (RHSI->getOpcode() == Instruction::Sub) |
| 1311 | if (LHS == RHSI->getOperand(1)) // A + (B - A) --> B |
| 1312 | return ReplaceInstUsesWith(I, RHSI->getOperand(0)); |
| 1313 | } |
| 1314 | if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) { |
| 1315 | if (LHSI->getOpcode() == Instruction::Sub) |
| 1316 | if (RHS == LHSI->getOperand(1)) // (B - A) + A --> B |
| 1317 | return ReplaceInstUsesWith(I, LHSI->getOperand(0)); |
| 1318 | } |
Robert Bocchino | 7b5b86c | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 1319 | } |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 1320 | |
Chris Lattner | 147e975 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 1321 | // -A + B --> B - A |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1322 | if (Value *V = dyn_castNegVal(LHS)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1323 | return BinaryOperator::createSub(RHS, V); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1324 | |
| 1325 | // A + -B --> A - B |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1326 | if (!isa<Constant>(RHS)) |
| 1327 | if (Value *V = dyn_castNegVal(RHS)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1328 | return BinaryOperator::createSub(LHS, V); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1329 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1330 | |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1331 | ConstantInt *C2; |
| 1332 | if (Value *X = dyn_castFoldableMul(LHS, C2)) { |
| 1333 | if (X == RHS) // X*C + X --> X * (C+1) |
| 1334 | return BinaryOperator::createMul(RHS, AddOne(C2)); |
| 1335 | |
| 1336 | // X*C1 + X*C2 --> X * (C1+C2) |
| 1337 | ConstantInt *C1; |
| 1338 | if (X == dyn_castFoldableMul(RHS, C1)) |
| 1339 | return BinaryOperator::createMul(X, ConstantExpr::getAdd(C1, C2)); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | // X + X*C --> X * (C+1) |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1343 | if (dyn_castFoldableMul(RHS, C2) == LHS) |
| 1344 | return BinaryOperator::createMul(LHS, AddOne(C2)); |
| 1345 | |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1346 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1347 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1348 | if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1349 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) return R; |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 1350 | |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 1351 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 1352 | Value *X = 0; |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1353 | if (match(LHS, m_Not(m_Value(X)))) { // ~X + C --> (C-1) - X |
| 1354 | Constant *C= ConstantExpr::getSub(CRHS, ConstantInt::get(I.getType(), 1)); |
| 1355 | return BinaryOperator::createSub(C, X); |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 1356 | } |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1357 | |
Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 1358 | // (X & FF00) + xx00 -> (X+xx00) & FF00 |
| 1359 | if (LHS->hasOneUse() && match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) { |
| 1360 | Constant *Anded = ConstantExpr::getAnd(CRHS, C2); |
| 1361 | if (Anded == CRHS) { |
| 1362 | // See if all bits from the first bit set in the Add RHS up are included |
| 1363 | // in the mask. First, get the rightmost bit. |
| 1364 | uint64_t AddRHSV = CRHS->getRawValue(); |
| 1365 | |
| 1366 | // Form a mask of all bits from the lowest bit added through the top. |
| 1367 | uint64_t AddRHSHighBits = ~((AddRHSV & -AddRHSV)-1); |
Chris Lattner | 77defba | 2006-02-07 07:00:41 +0000 | [diff] [blame] | 1368 | AddRHSHighBits &= C2->getType()->getIntegralTypeMask(); |
Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 1369 | |
| 1370 | // See if the and mask includes all of these bits. |
| 1371 | uint64_t AddRHSHighBitsAnd = AddRHSHighBits & C2->getRawValue(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1372 | |
Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 1373 | if (AddRHSHighBits == AddRHSHighBitsAnd) { |
| 1374 | // Okay, the xform is safe. Insert the new add pronto. |
| 1375 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, CRHS, |
| 1376 | LHS->getName()), I); |
| 1377 | return BinaryOperator::createAnd(NewAdd, C2); |
| 1378 | } |
| 1379 | } |
| 1380 | } |
| 1381 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1382 | // Try to fold constant add into select arguments. |
| 1383 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1384 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1385 | return R; |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1388 | return Changed ? &I : 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
Chris Lattner | bdb0ce0 | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 1391 | // isSignBit - Return true if the value represented by the constant only has the |
| 1392 | // highest order bit set. |
| 1393 | static bool isSignBit(ConstantInt *CI) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1394 | unsigned NumBits = CI->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 2f1457f | 2005-04-24 17:46:05 +0000 | [diff] [blame] | 1395 | return (CI->getRawValue() & (~0ULL >> (64-NumBits))) == (1ULL << (NumBits-1)); |
Chris Lattner | bdb0ce0 | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 1398 | /// RemoveNoopCast - Strip off nonconverting casts from the value. |
| 1399 | /// |
| 1400 | static Value *RemoveNoopCast(Value *V) { |
| 1401 | if (CastInst *CI = dyn_cast<CastInst>(V)) { |
| 1402 | const Type *CTy = CI->getType(); |
| 1403 | const Type *OpTy = CI->getOperand(0)->getType(); |
| 1404 | if (CTy->isInteger() && OpTy->isInteger()) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1405 | if (CTy->getPrimitiveSizeInBits() == OpTy->getPrimitiveSizeInBits()) |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 1406 | return RemoveNoopCast(CI->getOperand(0)); |
| 1407 | } else if (isa<PointerType>(CTy) && isa<PointerType>(OpTy)) |
| 1408 | return RemoveNoopCast(CI->getOperand(0)); |
| 1409 | } |
| 1410 | return V; |
| 1411 | } |
| 1412 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1413 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1414 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1415 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1416 | if (Op0 == Op1) // sub X, X -> 0 |
| 1417 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1418 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1419 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1420 | if (Value *V = dyn_castNegVal(Op1)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1421 | return BinaryOperator::createAdd(Op0, V); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1422 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1423 | if (isa<UndefValue>(Op0)) |
| 1424 | return ReplaceInstUsesWith(I, Op0); // undef - X -> undef |
| 1425 | if (isa<UndefValue>(Op1)) |
| 1426 | return ReplaceInstUsesWith(I, Op1); // X - undef -> undef |
| 1427 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 1428 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
| 1429 | // Replace (-1 - A) with (~A)... |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1430 | if (C->isAllOnesValue()) |
| 1431 | return BinaryOperator::createNot(Op1); |
Chris Lattner | ad3c495 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 1432 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 1433 | // C - ~X == X + (1+C) |
Reid Spencer | 4fdd96c | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 1434 | Value *X = 0; |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1435 | if (match(Op1, m_Not(m_Value(X)))) |
| 1436 | return BinaryOperator::createAdd(X, |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1437 | ConstantExpr::getAdd(C, ConstantInt::get(I.getType(), 1))); |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 1438 | // -((uint)X >> 31) -> ((int)X >> 31) |
| 1439 | // -((int)X >> 31) -> ((uint)X >> 31) |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 1440 | if (C->isNullValue()) { |
| 1441 | Value *NoopCastedRHS = RemoveNoopCast(Op1); |
| 1442 | if (ShiftInst *SI = dyn_cast<ShiftInst>(NoopCastedRHS)) |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 1443 | if (SI->getOpcode() == Instruction::Shr) |
| 1444 | if (ConstantUInt *CU = dyn_cast<ConstantUInt>(SI->getOperand(1))) { |
| 1445 | const Type *NewTy; |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 1446 | if (SI->getType()->isSigned()) |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 1447 | NewTy = SI->getType()->getUnsignedVersion(); |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 1448 | else |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 1449 | NewTy = SI->getType()->getSignedVersion(); |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 1450 | // 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] | 1451 | if (CU->getValue() == SI->getType()->getPrimitiveSizeInBits()-1) { |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 1452 | // Ok, the transformation is safe. Insert a cast of the incoming |
| 1453 | // value, then the new shift, then the new cast. |
| 1454 | Instruction *FirstCast = new CastInst(SI->getOperand(0), NewTy, |
| 1455 | SI->getOperand(0)->getName()); |
| 1456 | Value *InV = InsertNewInstBefore(FirstCast, I); |
| 1457 | Instruction *NewShift = new ShiftInst(Instruction::Shr, FirstCast, |
| 1458 | CU, SI->getName()); |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 1459 | if (NewShift->getType() == I.getType()) |
| 1460 | return NewShift; |
| 1461 | else { |
| 1462 | InV = InsertNewInstBefore(NewShift, I); |
| 1463 | return new CastInst(NewShift, I.getType()); |
| 1464 | } |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 1465 | } |
| 1466 | } |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 1467 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1468 | |
| 1469 | // Try to fold constant sub into select arguments. |
| 1470 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1471 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1472 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1473 | |
| 1474 | if (isa<PHINode>(Op0)) |
| 1475 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1476 | return NV; |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 1479 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
| 1480 | if (Op1I->getOpcode() == Instruction::Add && |
| 1481 | !Op0->getType()->isFloatingPoint()) { |
Chris Lattner | c7f3c1a | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 1482 | if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 1483 | return BinaryOperator::createNeg(Op1I->getOperand(1), I.getName()); |
Chris Lattner | c7f3c1a | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 1484 | else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 1485 | return BinaryOperator::createNeg(Op1I->getOperand(0), I.getName()); |
Chris Lattner | c7f3c1a | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 1486 | else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) { |
| 1487 | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1))) |
| 1488 | // C1-(X+C2) --> (C1-C2)-X |
| 1489 | return BinaryOperator::createSub(ConstantExpr::getSub(CI1, CI2), |
| 1490 | Op1I->getOperand(0)); |
| 1491 | } |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1494 | if (Op1I->hasOneUse()) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1495 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 1496 | // is not used by anyone else... |
| 1497 | // |
Chris Lattner | c2f0aa5 | 2004-02-02 20:09:56 +0000 | [diff] [blame] | 1498 | if (Op1I->getOpcode() == Instruction::Sub && |
| 1499 | !Op1I->getType()->isFloatingPoint()) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1500 | // Swap the two operands of the subexpr... |
| 1501 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 1502 | Op1I->setOperand(0, IIOp1); |
| 1503 | Op1I->setOperand(1, IIOp0); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1504 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1505 | // Create the new top level add instruction... |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1506 | return BinaryOperator::createAdd(Op0, Op1); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 1510 | // |
| 1511 | if (Op1I->getOpcode() == Instruction::And && |
| 1512 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 1513 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 1514 | |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 1515 | Value *NewNot = |
| 1516 | InsertNewInstBefore(BinaryOperator::createNot(OtherOp, "B.not"), I); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1517 | return BinaryOperator::createAnd(Op0, NewNot); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1518 | } |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1519 | |
Chris Lattner | 0aee4b7 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 1520 | // -(X sdiv C) -> (X sdiv -C) |
| 1521 | if (Op1I->getOpcode() == Instruction::Div) |
| 1522 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0)) |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 1523 | if (CSI->isNullValue()) |
Chris Lattner | 0aee4b7 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 1524 | if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1525 | return BinaryOperator::createDiv(Op1I->getOperand(0), |
Chris Lattner | 0aee4b7 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 1526 | ConstantExpr::getNeg(DivRHS)); |
| 1527 | |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1528 | // X - X*C --> X * (1-C) |
Reid Spencer | 4fdd96c | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 1529 | ConstantInt *C2 = 0; |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1530 | if (dyn_castFoldableMul(Op1I, C2) == Op0) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1531 | Constant *CP1 = |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1532 | ConstantExpr::getSub(ConstantInt::get(I.getType(), 1), C2); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1533 | return BinaryOperator::createMul(Op0, CP1); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1534 | } |
Chris Lattner | ad3c495 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 1535 | } |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 1536 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1537 | |
Chris Lattner | 4706046 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 1538 | if (!Op0->getType()->isFloatingPoint()) |
| 1539 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 1540 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 1541 | if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X |
| 1542 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 1543 | else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X |
| 1544 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 4706046 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 1545 | } else if (Op0I->getOpcode() == Instruction::Sub) { |
| 1546 | if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y |
| 1547 | return BinaryOperator::createNeg(Op0I->getOperand(1), I.getName()); |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 1548 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1549 | |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1550 | ConstantInt *C1; |
| 1551 | if (Value *X = dyn_castFoldableMul(Op0, C1)) { |
| 1552 | if (X == Op1) { // X*C - X --> X * (C-1) |
| 1553 | Constant *CP1 = ConstantExpr::getSub(C1, ConstantInt::get(I.getType(),1)); |
| 1554 | return BinaryOperator::createMul(Op1, CP1); |
| 1555 | } |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1556 | |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1557 | ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2) |
| 1558 | if (X == dyn_castFoldableMul(Op1, C2)) |
| 1559 | return BinaryOperator::createMul(Op1, ConstantExpr::getSub(C1, C2)); |
| 1560 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1561 | return 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 1564 | /// isSignBitCheck - Given an exploded setcc instruction, return true if it is |
| 1565 | /// really just returns true if the most significant (sign) bit is set. |
| 1566 | static bool isSignBitCheck(unsigned Opcode, Value *LHS, ConstantInt *RHS) { |
| 1567 | if (RHS->getType()->isSigned()) { |
| 1568 | // True if source is LHS < 0 or LHS <= -1 |
| 1569 | return Opcode == Instruction::SetLT && RHS->isNullValue() || |
| 1570 | Opcode == Instruction::SetLE && RHS->isAllOnesValue(); |
| 1571 | } else { |
| 1572 | ConstantUInt *RHSC = cast<ConstantUInt>(RHS); |
| 1573 | // True if source is LHS > 127 or LHS >= 128, where the constants depend on |
| 1574 | // the size of the integer type. |
| 1575 | if (Opcode == Instruction::SetGE) |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1576 | return RHSC->getValue() == |
| 1577 | 1ULL << (RHS->getType()->getPrimitiveSizeInBits()-1); |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 1578 | if (Opcode == Instruction::SetGT) |
| 1579 | return RHSC->getValue() == |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1580 | (1ULL << (RHS->getType()->getPrimitiveSizeInBits()-1))-1; |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 1581 | } |
| 1582 | return false; |
| 1583 | } |
| 1584 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1585 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1586 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1587 | Value *Op0 = I.getOperand(0); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1588 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1589 | if (isa<UndefValue>(I.getOperand(1))) // undef * X -> 0 |
| 1590 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1591 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1592 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1593 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 1594 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 1595 | |
| 1596 | // ((X << C1)*C2) == (X * (C2 << C1)) |
| 1597 | if (ShiftInst *SI = dyn_cast<ShiftInst>(Op0)) |
| 1598 | if (SI->getOpcode() == Instruction::Shl) |
| 1599 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1600 | return BinaryOperator::createMul(SI->getOperand(0), |
| 1601 | ConstantExpr::getShl(CI, ShOp)); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1602 | |
Chris Lattner | cce81be | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 1603 | if (CI->isNullValue()) |
| 1604 | return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 |
| 1605 | if (CI->equalsInt(1)) // X * 1 == X |
| 1606 | return ReplaceInstUsesWith(I, Op0); |
| 1607 | if (CI->isAllOnesValue()) // X * -1 == 0 - X |
Chris Lattner | 35236d8 | 2003-06-25 17:09:20 +0000 | [diff] [blame] | 1608 | return BinaryOperator::createNeg(Op0, I.getName()); |
Chris Lattner | 31ba129 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 1609 | |
Chris Lattner | cce81be | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 1610 | int64_t Val = (int64_t)cast<ConstantInt>(CI)->getRawValue(); |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 1611 | if (isPowerOf2_64(Val)) { // Replace X*(2^C) with X << C |
| 1612 | uint64_t C = Log2_64(Val); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1613 | return new ShiftInst(Instruction::Shl, Op0, |
| 1614 | ConstantUInt::get(Type::UByteTy, C)); |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 1615 | } |
Robert Bocchino | 7b5b86c | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 1616 | } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1617 | if (Op1F->isNullValue()) |
| 1618 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 31ba129 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 1619 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1620 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 1621 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) |
| 1622 | if (Op1F->getValue() == 1.0) |
| 1623 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
| 1624 | } |
Chris Lattner | 32c01df | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 1625 | |
| 1626 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 1627 | if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() && |
| 1628 | isa<ConstantInt>(Op0I->getOperand(1))) { |
| 1629 | // Canonicalize (X+C1)*C2 -> X*C2+C1*C2. |
| 1630 | Instruction *Add = BinaryOperator::createMul(Op0I->getOperand(0), |
| 1631 | Op1, "tmp"); |
| 1632 | InsertNewInstBefore(Add, I); |
| 1633 | Value *C1C2 = ConstantExpr::getMul(Op1, |
| 1634 | cast<Constant>(Op0I->getOperand(1))); |
| 1635 | return BinaryOperator::createAdd(Add, C1C2); |
| 1636 | |
| 1637 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1638 | |
| 1639 | // Try to fold constant mul into select arguments. |
| 1640 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1641 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1642 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1643 | |
| 1644 | if (isa<PHINode>(Op0)) |
| 1645 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1646 | return NV; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Chris Lattner | 934a64cf | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 1649 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
| 1650 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1651 | return BinaryOperator::createMul(Op0v, Op1v); |
Chris Lattner | 934a64cf | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 1652 | |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 1653 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 1654 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 1655 | // See if we can simplify things based on how the boolean was originally |
| 1656 | // formed. |
| 1657 | CastInst *BoolCast = 0; |
| 1658 | if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(0))) |
| 1659 | if (CI->getOperand(0)->getType() == Type::BoolTy) |
| 1660 | BoolCast = CI; |
| 1661 | if (!BoolCast) |
| 1662 | if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(1))) |
| 1663 | if (CI->getOperand(0)->getType() == Type::BoolTy) |
| 1664 | BoolCast = CI; |
| 1665 | if (BoolCast) { |
| 1666 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(BoolCast->getOperand(0))) { |
| 1667 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); |
| 1668 | const Type *SCOpTy = SCIOp0->getType(); |
| 1669 | |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 1670 | // If the setcc is true iff the sign bit of X is set, then convert this |
| 1671 | // multiply into a shift/and combination. |
| 1672 | if (isa<ConstantInt>(SCIOp1) && |
| 1673 | isSignBitCheck(SCI->getOpcode(), SCIOp0, cast<ConstantInt>(SCIOp1))) { |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 1674 | // Shift the X value right to turn it into "all signbits". |
| 1675 | Constant *Amt = ConstantUInt::get(Type::UByteTy, |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1676 | SCOpTy->getPrimitiveSizeInBits()-1); |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 1677 | if (SCIOp0->getType()->isUnsigned()) { |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 1678 | const Type *NewTy = SCIOp0->getType()->getSignedVersion(); |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 1679 | SCIOp0 = InsertNewInstBefore(new CastInst(SCIOp0, NewTy, |
| 1680 | SCIOp0->getName()), I); |
| 1681 | } |
| 1682 | |
| 1683 | Value *V = |
| 1684 | InsertNewInstBefore(new ShiftInst(Instruction::Shr, SCIOp0, Amt, |
| 1685 | BoolCast->getOperand(0)->getName()+ |
| 1686 | ".mask"), I); |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 1687 | |
| 1688 | // If the multiply type is not the same as the source type, sign extend |
| 1689 | // or truncate to the multiply type. |
| 1690 | if (I.getType() != V->getType()) |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 1691 | V = InsertNewInstBefore(new CastInst(V, I.getType(), V->getName()),I); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1692 | |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 1693 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1694 | return BinaryOperator::createAnd(V, OtherOp); |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 1695 | } |
| 1696 | } |
| 1697 | } |
| 1698 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1699 | return Changed ? &I : 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1702 | Instruction *InstCombiner::visitDiv(BinaryOperator &I) { |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1703 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1704 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1705 | if (isa<UndefValue>(Op0)) // undef / X -> 0 |
| 1706 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1707 | if (isa<UndefValue>(Op1)) |
| 1708 | return ReplaceInstUsesWith(I, Op1); // X / undef -> undef |
| 1709 | |
| 1710 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | e20c334 | 2004-04-26 14:01:59 +0000 | [diff] [blame] | 1711 | // div X, 1 == X |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1712 | if (RHS->equalsInt(1)) |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1713 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1714 | |
Chris Lattner | e20c334 | 2004-04-26 14:01:59 +0000 | [diff] [blame] | 1715 | // div X, -1 == -X |
| 1716 | if (RHS->isAllOnesValue()) |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1717 | return BinaryOperator::createNeg(Op0); |
Chris Lattner | e20c334 | 2004-04-26 14:01:59 +0000 | [diff] [blame] | 1718 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1719 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 1720 | if (LHS->getOpcode() == Instruction::Div) |
| 1721 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 1722 | // (X / C1) / C2 -> X / (C1*C2) |
| 1723 | return BinaryOperator::createDiv(LHS->getOperand(0), |
| 1724 | ConstantExpr::getMul(RHS, LHSRHS)); |
| 1725 | } |
| 1726 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1727 | // Check to see if this is an unsigned division with an exact power of 2, |
| 1728 | // if so, convert to a right shift. |
| 1729 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS)) |
| 1730 | if (uint64_t Val = C->getValue()) // Don't break X / 0 |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 1731 | if (isPowerOf2_64(Val)) { |
| 1732 | uint64_t C = Log2_64(Val); |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1733 | return new ShiftInst(Instruction::Shr, Op0, |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1734 | ConstantUInt::get(Type::UByteTy, C)); |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 1735 | } |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1736 | |
Chris Lattner | 4ad0835 | 2004-10-09 02:50:40 +0000 | [diff] [blame] | 1737 | // -X/C -> X/-C |
| 1738 | if (RHS->getType()->isSigned()) |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1739 | if (Value *LHSNeg = dyn_castNegVal(Op0)) |
Chris Lattner | 4ad0835 | 2004-10-09 02:50:40 +0000 | [diff] [blame] | 1740 | return BinaryOperator::createDiv(LHSNeg, ConstantExpr::getNeg(RHS)); |
| 1741 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1742 | if (!RHS->isNullValue()) { |
| 1743 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1744 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1745 | return R; |
| 1746 | if (isa<PHINode>(Op0)) |
| 1747 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1748 | return NV; |
| 1749 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1752 | // If this is 'udiv X, (Cond ? C1, C2)' where C1&C2 are powers of two, |
| 1753 | // transform this into: '(Cond ? (udiv X, C1) : (udiv X, C2))'. |
| 1754 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 1755 | if (ConstantUInt *STO = dyn_cast<ConstantUInt>(SI->getOperand(1))) |
| 1756 | if (ConstantUInt *SFO = dyn_cast<ConstantUInt>(SI->getOperand(2))) { |
| 1757 | if (STO->getValue() == 0) { // Couldn't be this argument. |
| 1758 | I.setOperand(1, SFO); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1759 | return &I; |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1760 | } else if (SFO->getValue() == 0) { |
Chris Lattner | 89dc4f1 | 2005-06-16 04:55:52 +0000 | [diff] [blame] | 1761 | I.setOperand(1, STO); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1762 | return &I; |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
Chris Lattner | 4236261 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 1765 | uint64_t TVA = STO->getValue(), FVA = SFO->getValue(); |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 1766 | if (isPowerOf2_64(TVA) && isPowerOf2_64(FVA)) { |
| 1767 | unsigned TSA = Log2_64(TVA), FSA = Log2_64(FVA); |
Chris Lattner | 4236261 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 1768 | Constant *TC = ConstantUInt::get(Type::UByteTy, TSA); |
| 1769 | Instruction *TSI = new ShiftInst(Instruction::Shr, Op0, |
| 1770 | TC, SI->getName()+".t"); |
| 1771 | TSI = InsertNewInstBefore(TSI, I); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1772 | |
Chris Lattner | 4236261 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 1773 | Constant *FC = ConstantUInt::get(Type::UByteTy, FSA); |
| 1774 | Instruction *FSI = new ShiftInst(Instruction::Shr, Op0, |
| 1775 | FC, SI->getName()+".f"); |
| 1776 | FSI = InsertNewInstBefore(FSI, I); |
| 1777 | return new SelectInst(SI->getOperand(0), TSI, FSI); |
| 1778 | } |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1779 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1780 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1781 | // 0 / X == 0, we don't need to preserve faults! |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1782 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1783 | if (LHS->equalsInt(0)) |
| 1784 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1785 | |
Chris Lattner | dd0c174 | 2005-11-05 07:40:31 +0000 | [diff] [blame] | 1786 | if (I.getType()->isSigned()) { |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 1787 | // If the sign bits of both operands are zero (i.e. we can prove they are |
Chris Lattner | dd0c174 | 2005-11-05 07:40:31 +0000 | [diff] [blame] | 1788 | // unsigned inputs), turn this into a udiv. |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 1789 | uint64_t Mask = 1ULL << (I.getType()->getPrimitiveSizeInBits()-1); |
| 1790 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
Chris Lattner | dd0c174 | 2005-11-05 07:40:31 +0000 | [diff] [blame] | 1791 | const Type *NTy = Op0->getType()->getUnsignedVersion(); |
| 1792 | Instruction *LHS = new CastInst(Op0, NTy, Op0->getName()); |
| 1793 | InsertNewInstBefore(LHS, I); |
| 1794 | Value *RHS; |
| 1795 | if (Constant *R = dyn_cast<Constant>(Op1)) |
| 1796 | RHS = ConstantExpr::getCast(R, NTy); |
| 1797 | else |
| 1798 | RHS = InsertNewInstBefore(new CastInst(Op1, NTy, Op1->getName()), I); |
| 1799 | Instruction *Div = BinaryOperator::createDiv(LHS, RHS, I.getName()); |
| 1800 | InsertNewInstBefore(Div, I); |
| 1801 | return new CastInst(Div, I.getType()); |
| 1802 | } |
Chris Lattner | 2e90b73 | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 1803 | } else { |
| 1804 | // Known to be an unsigned division. |
| 1805 | if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) { |
| 1806 | // Turn A / (C1 << N), where C1 is "1<<C2" into A >> (N+C2) [udiv only]. |
| 1807 | if (RHSI->getOpcode() == Instruction::Shl && |
| 1808 | isa<ConstantUInt>(RHSI->getOperand(0))) { |
| 1809 | unsigned C1 = cast<ConstantUInt>(RHSI->getOperand(0))->getRawValue(); |
| 1810 | if (isPowerOf2_64(C1)) { |
| 1811 | unsigned C2 = Log2_64(C1); |
| 1812 | Value *Add = RHSI->getOperand(1); |
| 1813 | if (C2) { |
| 1814 | Constant *C2V = ConstantUInt::get(Add->getType(), C2); |
| 1815 | Add = InsertNewInstBefore(BinaryOperator::createAdd(Add, C2V, |
| 1816 | "tmp"), I); |
| 1817 | } |
| 1818 | return new ShiftInst(Instruction::Shr, Op0, Add); |
| 1819 | } |
| 1820 | } |
| 1821 | } |
Chris Lattner | dd0c174 | 2005-11-05 07:40:31 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1824 | return 0; |
| 1825 | } |
| 1826 | |
| 1827 | |
Chris Lattner | 85dda9a | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 1828 | /// GetFactor - If we can prove that the specified value is at least a multiple |
| 1829 | /// of some factor, return that factor. |
| 1830 | static Constant *GetFactor(Value *V) { |
| 1831 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 1832 | return CI; |
| 1833 | |
| 1834 | // Unless we can be tricky, we know this is a multiple of 1. |
| 1835 | Constant *Result = ConstantInt::get(V->getType(), 1); |
| 1836 | |
| 1837 | Instruction *I = dyn_cast<Instruction>(V); |
| 1838 | if (!I) return Result; |
| 1839 | |
| 1840 | if (I->getOpcode() == Instruction::Mul) { |
| 1841 | // Handle multiplies by a constant, etc. |
| 1842 | return ConstantExpr::getMul(GetFactor(I->getOperand(0)), |
| 1843 | GetFactor(I->getOperand(1))); |
| 1844 | } else if (I->getOpcode() == Instruction::Shl) { |
| 1845 | // (X<<C) -> X * (1 << C) |
| 1846 | if (Constant *ShRHS = dyn_cast<Constant>(I->getOperand(1))) { |
| 1847 | ShRHS = ConstantExpr::getShl(Result, ShRHS); |
| 1848 | return ConstantExpr::getMul(GetFactor(I->getOperand(0)), ShRHS); |
| 1849 | } |
| 1850 | } else if (I->getOpcode() == Instruction::And) { |
| 1851 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1852 | // X & 0xFFF0 is known to be a multiple of 16. |
| 1853 | unsigned Zeros = CountTrailingZeros_64(RHS->getZExtValue()); |
| 1854 | if (Zeros != V->getType()->getPrimitiveSizeInBits()) |
| 1855 | return ConstantExpr::getShl(Result, |
| 1856 | ConstantUInt::get(Type::UByteTy, Zeros)); |
| 1857 | } |
| 1858 | } else if (I->getOpcode() == Instruction::Cast) { |
| 1859 | Value *Op = I->getOperand(0); |
| 1860 | // Only handle int->int casts. |
| 1861 | if (!Op->getType()->isInteger()) return Result; |
| 1862 | return ConstantExpr::getCast(GetFactor(Op), V->getType()); |
| 1863 | } |
| 1864 | return Result; |
| 1865 | } |
| 1866 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1867 | Instruction *InstCombiner::visitRem(BinaryOperator &I) { |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1868 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 0de4a8d | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 1869 | |
| 1870 | // 0 % X == 0, we don't need to preserve faults! |
| 1871 | if (Constant *LHS = dyn_cast<Constant>(Op0)) |
| 1872 | if (LHS->isNullValue()) |
| 1873 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1874 | |
| 1875 | if (isa<UndefValue>(Op0)) // undef % X -> 0 |
| 1876 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1877 | if (isa<UndefValue>(Op1)) |
| 1878 | return ReplaceInstUsesWith(I, Op1); // X % undef -> undef |
| 1879 | |
Chris Lattner | e9ff0ea | 2005-11-05 07:28:37 +0000 | [diff] [blame] | 1880 | if (I.getType()->isSigned()) { |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1881 | if (Value *RHSNeg = dyn_castNegVal(Op1)) |
Chris Lattner | 98c6bdf | 2004-07-06 07:11:42 +0000 | [diff] [blame] | 1882 | if (!isa<ConstantSInt>(RHSNeg) || |
Chris Lattner | 8e72606 | 2004-08-09 21:05:48 +0000 | [diff] [blame] | 1883 | cast<ConstantSInt>(RHSNeg)->getValue() > 0) { |
Chris Lattner | 7fd5f07 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 1884 | // X % -Y -> X % Y |
| 1885 | AddUsesToWorkList(I); |
| 1886 | I.setOperand(1, RHSNeg); |
| 1887 | return &I; |
| 1888 | } |
Chris Lattner | e9ff0ea | 2005-11-05 07:28:37 +0000 | [diff] [blame] | 1889 | |
| 1890 | // If the top bits of both operands are zero (i.e. we can prove they are |
| 1891 | // unsigned inputs), turn this into a urem. |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 1892 | uint64_t Mask = 1ULL << (I.getType()->getPrimitiveSizeInBits()-1); |
| 1893 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
Chris Lattner | e9ff0ea | 2005-11-05 07:28:37 +0000 | [diff] [blame] | 1894 | const Type *NTy = Op0->getType()->getUnsignedVersion(); |
| 1895 | Instruction *LHS = new CastInst(Op0, NTy, Op0->getName()); |
| 1896 | InsertNewInstBefore(LHS, I); |
| 1897 | Value *RHS; |
| 1898 | if (Constant *R = dyn_cast<Constant>(Op1)) |
| 1899 | RHS = ConstantExpr::getCast(R, NTy); |
| 1900 | else |
| 1901 | RHS = InsertNewInstBefore(new CastInst(Op1, NTy, Op1->getName()), I); |
| 1902 | Instruction *Rem = BinaryOperator::createRem(LHS, RHS, I.getName()); |
| 1903 | InsertNewInstBefore(Rem, I); |
| 1904 | return new CastInst(Rem, I.getType()); |
| 1905 | } |
| 1906 | } |
Chris Lattner | 7fd5f07 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 1907 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1908 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 0de4a8d | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 1909 | // X % 0 == undef, we don't need to preserve faults! |
| 1910 | if (RHS->equalsInt(0)) |
| 1911 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); |
| 1912 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1913 | if (RHS->equalsInt(1)) // X % 1 == 0 |
| 1914 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1915 | |
| 1916 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 1917 | // if so, convert to a bitwise and. |
| 1918 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS)) |
Chris Lattner | 0de4a8d | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 1919 | if (isPowerOf2_64(C->getValue())) |
| 1920 | return BinaryOperator::createAnd(Op0, SubOne(C)); |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1921 | |
Chris Lattner | b70f141 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 1922 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { |
| 1923 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { |
| 1924 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 1925 | return R; |
| 1926 | } else if (isa<PHINode>(Op0I)) { |
| 1927 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1928 | return NV; |
Chris Lattner | b70f141 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 1929 | } |
Chris Lattner | 85dda9a | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 1930 | |
| 1931 | // X*C1%C2 --> 0 iff C1%C2 == 0 |
| 1932 | if (ConstantExpr::getRem(GetFactor(Op0I), RHS)->isNullValue()) |
| 1933 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | b70f141 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 1934 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
Chris Lattner | 2e90b73 | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 1937 | if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) { |
| 1938 | // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1) [urem only]. |
| 1939 | if (I.getType()->isUnsigned() && |
| 1940 | RHSI->getOpcode() == Instruction::Shl && |
| 1941 | isa<ConstantUInt>(RHSI->getOperand(0))) { |
| 1942 | unsigned C1 = cast<ConstantUInt>(RHSI->getOperand(0))->getRawValue(); |
| 1943 | if (isPowerOf2_64(C1)) { |
| 1944 | Constant *N1 = ConstantInt::getAllOnesValue(I.getType()); |
| 1945 | Value *Add = InsertNewInstBefore(BinaryOperator::createAdd(RHSI, N1, |
| 1946 | "tmp"), I); |
| 1947 | return BinaryOperator::createAnd(Op0, Add); |
| 1948 | } |
| 1949 | } |
Chris Lattner | 0de4a8d | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 1950 | |
| 1951 | // If this is 'urem X, (Cond ? C1, C2)' where C1&C2 are powers of two, |
| 1952 | // transform this into: '(Cond ? (urem X, C1) : (urem X, C2))'. |
| 1953 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 1954 | if (ConstantUInt *STO = dyn_cast<ConstantUInt>(SI->getOperand(1))) |
| 1955 | if (ConstantUInt *SFO = dyn_cast<ConstantUInt>(SI->getOperand(2))) { |
| 1956 | if (STO->getValue() == 0) { // Couldn't be this argument. |
| 1957 | I.setOperand(1, SFO); |
| 1958 | return &I; |
| 1959 | } else if (SFO->getValue() == 0) { |
| 1960 | I.setOperand(1, STO); |
| 1961 | return &I; |
| 1962 | } |
| 1963 | |
| 1964 | if (isPowerOf2_64(STO->getValue()) && isPowerOf2_64(SFO->getValue())){ |
| 1965 | Value *TrueAnd = InsertNewInstBefore(BinaryOperator::createAnd(Op0, |
| 1966 | SubOne(STO), SI->getName()+".t"), I); |
| 1967 | Value *FalseAnd = InsertNewInstBefore(BinaryOperator::createAnd(Op0, |
| 1968 | SubOne(SFO), SI->getName()+".f"), I); |
| 1969 | return new SelectInst(SI->getOperand(0), TrueAnd, FalseAnd); |
| 1970 | } |
| 1971 | } |
Chris Lattner | 2e90b73 | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1974 | return 0; |
| 1975 | } |
| 1976 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1977 | // isMaxValueMinusOne - return true if this is Max-1 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1978 | static bool isMaxValueMinusOne(const ConstantInt *C) { |
Chris Lattner | 77defba | 2006-02-07 07:00:41 +0000 | [diff] [blame] | 1979 | if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) |
| 1980 | return CU->getValue() == C->getType()->getIntegralTypeMask()-1; |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1981 | |
| 1982 | const ConstantSInt *CS = cast<ConstantSInt>(C); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1983 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1984 | // Calculate 0111111111..11111 |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1985 | unsigned TypeBits = C->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1986 | int64_t Val = INT64_MAX; // All ones |
| 1987 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 1988 | return CS->getValue() == Val-1; |
| 1989 | } |
| 1990 | |
| 1991 | // isMinValuePlusOne - return true if this is Min+1 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1992 | static bool isMinValuePlusOne(const ConstantInt *C) { |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1993 | if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) |
| 1994 | return CU->getValue() == 1; |
| 1995 | |
| 1996 | const ConstantSInt *CS = cast<ConstantSInt>(C); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1997 | |
| 1998 | // Calculate 1111111111000000000000 |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1999 | unsigned TypeBits = C->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2000 | int64_t Val = -1; // All ones |
| 2001 | Val <<= TypeBits-1; // Shift over to the right spot |
| 2002 | return CS->getValue() == Val+1; |
| 2003 | } |
| 2004 | |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2005 | // isOneBitSet - Return true if there is exactly one bit set in the specified |
| 2006 | // constant. |
| 2007 | static bool isOneBitSet(const ConstantInt *CI) { |
| 2008 | uint64_t V = CI->getRawValue(); |
| 2009 | return V && (V & (V-1)) == 0; |
| 2010 | } |
| 2011 | |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2012 | #if 0 // Currently unused |
| 2013 | // isLowOnes - Return true if the constant is of the form 0+1+. |
| 2014 | static bool isLowOnes(const ConstantInt *CI) { |
| 2015 | uint64_t V = CI->getRawValue(); |
| 2016 | |
| 2017 | // There won't be bits set in parts that the type doesn't contain. |
| 2018 | V &= ConstantInt::getAllOnesValue(CI->getType())->getRawValue(); |
| 2019 | |
| 2020 | uint64_t U = V+1; // If it is low ones, this should be a power of two. |
| 2021 | return U && V && (U & V) == 0; |
| 2022 | } |
| 2023 | #endif |
| 2024 | |
| 2025 | // isHighOnes - Return true if the constant is of the form 1+0+. |
| 2026 | // This is the same as lowones(~X). |
| 2027 | static bool isHighOnes(const ConstantInt *CI) { |
| 2028 | uint64_t V = ~CI->getRawValue(); |
Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 2029 | if (~V == 0) return false; // 0's does not match "1+" |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2030 | |
| 2031 | // There won't be bits set in parts that the type doesn't contain. |
| 2032 | V &= ConstantInt::getAllOnesValue(CI->getType())->getRawValue(); |
| 2033 | |
| 2034 | uint64_t U = V+1; // If it is low ones, this should be a power of two. |
| 2035 | return U && V && (U & V) == 0; |
| 2036 | } |
| 2037 | |
| 2038 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2039 | /// getSetCondCode - Encode a setcc opcode into a three bit mask. These bits |
| 2040 | /// are carefully arranged to allow folding of expressions such as: |
| 2041 | /// |
| 2042 | /// (A < B) | (A > B) --> (A != B) |
| 2043 | /// |
| 2044 | /// Bit value '4' represents that the comparison is true if A > B, bit value '2' |
| 2045 | /// represents that the comparison is true if A == B, and bit value '1' is true |
| 2046 | /// if A < B. |
| 2047 | /// |
| 2048 | static unsigned getSetCondCode(const SetCondInst *SCI) { |
| 2049 | switch (SCI->getOpcode()) { |
| 2050 | // False -> 0 |
| 2051 | case Instruction::SetGT: return 1; |
| 2052 | case Instruction::SetEQ: return 2; |
| 2053 | case Instruction::SetGE: return 3; |
| 2054 | case Instruction::SetLT: return 4; |
| 2055 | case Instruction::SetNE: return 5; |
| 2056 | case Instruction::SetLE: return 6; |
| 2057 | // True -> 7 |
| 2058 | default: |
| 2059 | assert(0 && "Invalid SetCC opcode!"); |
| 2060 | return 0; |
| 2061 | } |
| 2062 | } |
| 2063 | |
| 2064 | /// getSetCCValue - This is the complement of getSetCondCode, which turns an |
| 2065 | /// opcode and two operands into either a constant true or false, or a brand new |
| 2066 | /// SetCC instruction. |
| 2067 | static Value *getSetCCValue(unsigned Opcode, Value *LHS, Value *RHS) { |
| 2068 | switch (Opcode) { |
| 2069 | case 0: return ConstantBool::False; |
| 2070 | case 1: return new SetCondInst(Instruction::SetGT, LHS, RHS); |
| 2071 | case 2: return new SetCondInst(Instruction::SetEQ, LHS, RHS); |
| 2072 | case 3: return new SetCondInst(Instruction::SetGE, LHS, RHS); |
| 2073 | case 4: return new SetCondInst(Instruction::SetLT, LHS, RHS); |
| 2074 | case 5: return new SetCondInst(Instruction::SetNE, LHS, RHS); |
| 2075 | case 6: return new SetCondInst(Instruction::SetLE, LHS, RHS); |
| 2076 | case 7: return ConstantBool::True; |
| 2077 | default: assert(0 && "Illegal SetCCCode!"); return 0; |
| 2078 | } |
| 2079 | } |
| 2080 | |
| 2081 | // FoldSetCCLogical - Implements (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B) |
| 2082 | struct FoldSetCCLogical { |
| 2083 | InstCombiner &IC; |
| 2084 | Value *LHS, *RHS; |
| 2085 | FoldSetCCLogical(InstCombiner &ic, SetCondInst *SCI) |
| 2086 | : IC(ic), LHS(SCI->getOperand(0)), RHS(SCI->getOperand(1)) {} |
| 2087 | bool shouldApply(Value *V) const { |
| 2088 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(V)) |
| 2089 | return (SCI->getOperand(0) == LHS && SCI->getOperand(1) == RHS || |
| 2090 | SCI->getOperand(0) == RHS && SCI->getOperand(1) == LHS); |
| 2091 | return false; |
| 2092 | } |
| 2093 | Instruction *apply(BinaryOperator &Log) const { |
| 2094 | SetCondInst *SCI = cast<SetCondInst>(Log.getOperand(0)); |
| 2095 | if (SCI->getOperand(0) != LHS) { |
| 2096 | assert(SCI->getOperand(1) == LHS); |
| 2097 | SCI->swapOperands(); // Swap the LHS and RHS of the SetCC |
| 2098 | } |
| 2099 | |
| 2100 | unsigned LHSCode = getSetCondCode(SCI); |
| 2101 | unsigned RHSCode = getSetCondCode(cast<SetCondInst>(Log.getOperand(1))); |
| 2102 | unsigned Code; |
| 2103 | switch (Log.getOpcode()) { |
| 2104 | case Instruction::And: Code = LHSCode & RHSCode; break; |
| 2105 | case Instruction::Or: Code = LHSCode | RHSCode; break; |
| 2106 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; |
Chris Lattner | 2caaaba | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 2107 | default: assert(0 && "Illegal logical opcode!"); return 0; |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | Value *RV = getSetCCValue(Code, LHS, RHS); |
| 2111 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 2112 | return I; |
| 2113 | // Otherwise, it's a constant boolean value... |
| 2114 | return IC.ReplaceInstUsesWith(Log, RV); |
| 2115 | } |
| 2116 | }; |
| 2117 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2118 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
| 2119 | // the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'. Op is |
| 2120 | // guaranteed to be either a shift instruction or a binary operator. |
| 2121 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
| 2122 | ConstantIntegral *OpRHS, |
| 2123 | ConstantIntegral *AndRHS, |
| 2124 | BinaryOperator &TheAnd) { |
| 2125 | Value *X = Op->getOperand(0); |
Chris Lattner | fcf21a7 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 2126 | Constant *Together = 0; |
| 2127 | if (!isa<ShiftInst>(Op)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2128 | Together = ConstantExpr::getAnd(AndRHS, OpRHS); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2129 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2130 | switch (Op->getOpcode()) { |
| 2131 | case Instruction::Xor: |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2132 | if (Op->hasOneUse()) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2133 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
| 2134 | std::string OpName = Op->getName(); Op->setName(""); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2135 | Instruction *And = BinaryOperator::createAnd(X, AndRHS, OpName); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2136 | InsertNewInstBefore(And, TheAnd); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2137 | return BinaryOperator::createXor(And, Together); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2138 | } |
| 2139 | break; |
| 2140 | case Instruction::Or: |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2141 | if (Together == AndRHS) // (X | C) & C --> C |
| 2142 | return ReplaceInstUsesWith(TheAnd, AndRHS); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2143 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2144 | if (Op->hasOneUse() && Together != OpRHS) { |
| 2145 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 |
| 2146 | std::string Op0Name = Op->getName(); Op->setName(""); |
| 2147 | Instruction *Or = BinaryOperator::createOr(X, Together, Op0Name); |
| 2148 | InsertNewInstBefore(Or, TheAnd); |
| 2149 | return BinaryOperator::createAnd(Or, AndRHS); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2150 | } |
| 2151 | break; |
| 2152 | case Instruction::Add: |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2153 | if (Op->hasOneUse()) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2154 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 2155 | // of the bit. First thing to check is to see if this AND is with a |
| 2156 | // single bit constant. |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2157 | uint64_t AndRHSV = cast<ConstantInt>(AndRHS)->getRawValue(); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2158 | |
| 2159 | // Clear bits that are not part of the constant. |
Chris Lattner | 77defba | 2006-02-07 07:00:41 +0000 | [diff] [blame] | 2160 | AndRHSV &= AndRHS->getType()->getIntegralTypeMask(); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2161 | |
| 2162 | // If there is only one bit set... |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2163 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2164 | // Ok, at this point, we know that we are masking the result of the |
| 2165 | // ADD down to exactly one bit. If the constant we are adding has |
| 2166 | // no bits set below this bit, then we can eliminate the ADD. |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2167 | uint64_t AddRHS = cast<ConstantInt>(OpRHS)->getRawValue(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2168 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2169 | // Check to see if any bits below the one bit set in AndRHSV are set. |
| 2170 | if ((AddRHS & (AndRHSV-1)) == 0) { |
| 2171 | // If not, the only thing that can effect the output of the AND is |
| 2172 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 2173 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 2174 | // no effect. |
| 2175 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop |
| 2176 | TheAnd.setOperand(0, X); |
| 2177 | return &TheAnd; |
| 2178 | } else { |
| 2179 | std::string Name = Op->getName(); Op->setName(""); |
| 2180 | // Pull the XOR out of the AND. |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2181 | Instruction *NewAnd = BinaryOperator::createAnd(X, AndRHS, Name); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2182 | InsertNewInstBefore(NewAnd, TheAnd); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2183 | return BinaryOperator::createXor(NewAnd, AndRHS); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2184 | } |
| 2185 | } |
| 2186 | } |
| 2187 | } |
| 2188 | break; |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2189 | |
| 2190 | case Instruction::Shl: { |
| 2191 | // We know that the AND will not produce any of the bits shifted in, so if |
| 2192 | // the anded constant includes them, clear them now! |
| 2193 | // |
| 2194 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2195 | Constant *ShlMask = ConstantExpr::getShl(AllOne, OpRHS); |
| 2196 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShlMask); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2197 | |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2198 | if (CI == ShlMask) { // Masking out bits that the shift already masks |
| 2199 | return ReplaceInstUsesWith(TheAnd, Op); // No need for the and. |
| 2200 | } else if (CI != AndRHS) { // Reducing bits set in and. |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2201 | TheAnd.setOperand(1, CI); |
| 2202 | return &TheAnd; |
| 2203 | } |
| 2204 | break; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2205 | } |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2206 | case Instruction::Shr: |
| 2207 | // We know that the AND will not produce any of the bits shifted in, so if |
| 2208 | // the anded constant includes them, clear them now! This only applies to |
| 2209 | // unsigned shifts, because a signed shr may bring in set bits! |
| 2210 | // |
| 2211 | if (AndRHS->getType()->isUnsigned()) { |
| 2212 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2213 | Constant *ShrMask = ConstantExpr::getShr(AllOne, OpRHS); |
| 2214 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask); |
| 2215 | |
| 2216 | if (CI == ShrMask) { // Masking out bits that the shift already masks. |
| 2217 | return ReplaceInstUsesWith(TheAnd, Op); |
| 2218 | } else if (CI != AndRHS) { |
| 2219 | TheAnd.setOperand(1, CI); // Reduce bits set in and cst. |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2220 | return &TheAnd; |
| 2221 | } |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2222 | } else { // Signed shr. |
| 2223 | // See if this is shifting in some sign extension, then masking it out |
| 2224 | // with an and. |
| 2225 | if (Op->hasOneUse()) { |
| 2226 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
| 2227 | Constant *ShrMask = ConstantExpr::getUShr(AllOne, OpRHS); |
| 2228 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask); |
Chris Lattner | 5c3c21e | 2004-10-22 04:53:16 +0000 | [diff] [blame] | 2229 | if (CI == AndRHS) { // Masking out bits shifted in. |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2230 | // Make the argument unsigned. |
| 2231 | Value *ShVal = Op->getOperand(0); |
| 2232 | ShVal = InsertCastBefore(ShVal, |
| 2233 | ShVal->getType()->getUnsignedVersion(), |
| 2234 | TheAnd); |
| 2235 | ShVal = InsertNewInstBefore(new ShiftInst(Instruction::Shr, ShVal, |
| 2236 | OpRHS, Op->getName()), |
| 2237 | TheAnd); |
Chris Lattner | 70c2039 | 2004-10-27 05:57:15 +0000 | [diff] [blame] | 2238 | Value *AndRHS2 = ConstantExpr::getCast(AndRHS, ShVal->getType()); |
| 2239 | ShVal = InsertNewInstBefore(BinaryOperator::createAnd(ShVal, AndRHS2, |
| 2240 | TheAnd.getName()), |
| 2241 | TheAnd); |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2242 | return new CastInst(ShVal, Op->getType()); |
| 2243 | } |
| 2244 | } |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2245 | } |
| 2246 | break; |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2247 | } |
| 2248 | return 0; |
| 2249 | } |
| 2250 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2251 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2252 | /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is |
| 2253 | /// true, otherwise (V < Lo || V >= Hi). In pratice, we emit the more efficient |
| 2254 | /// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. IB is the location to |
| 2255 | /// insert new instructions. |
| 2256 | Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
| 2257 | bool Inside, Instruction &IB) { |
| 2258 | assert(cast<ConstantBool>(ConstantExpr::getSetLE(Lo, Hi))->getValue() && |
| 2259 | "Lo is not <= Hi in range emission code!"); |
| 2260 | if (Inside) { |
| 2261 | if (Lo == Hi) // Trivially false. |
| 2262 | return new SetCondInst(Instruction::SetNE, V, V); |
| 2263 | if (cast<ConstantIntegral>(Lo)->isMinValue()) |
| 2264 | return new SetCondInst(Instruction::SetLT, V, Hi); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2265 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2266 | Constant *AddCST = ConstantExpr::getNeg(Lo); |
| 2267 | Instruction *Add = BinaryOperator::createAdd(V, AddCST,V->getName()+".off"); |
| 2268 | InsertNewInstBefore(Add, IB); |
| 2269 | // Convert to unsigned for the comparison. |
| 2270 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 2271 | Value *OffsetVal = InsertCastBefore(Add, UnsType, IB); |
| 2272 | AddCST = ConstantExpr::getAdd(AddCST, Hi); |
| 2273 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 2274 | return new SetCondInst(Instruction::SetLT, OffsetVal, AddCST); |
| 2275 | } |
| 2276 | |
| 2277 | if (Lo == Hi) // Trivially true. |
| 2278 | return new SetCondInst(Instruction::SetEQ, V, V); |
| 2279 | |
| 2280 | Hi = SubOne(cast<ConstantInt>(Hi)); |
| 2281 | if (cast<ConstantIntegral>(Lo)->isMinValue()) // V < 0 || V >= Hi ->'V > Hi-1' |
| 2282 | return new SetCondInst(Instruction::SetGT, V, Hi); |
| 2283 | |
| 2284 | // Emit X-Lo > Hi-Lo-1 |
| 2285 | Constant *AddCST = ConstantExpr::getNeg(Lo); |
| 2286 | Instruction *Add = BinaryOperator::createAdd(V, AddCST, V->getName()+".off"); |
| 2287 | InsertNewInstBefore(Add, IB); |
| 2288 | // Convert to unsigned for the comparison. |
| 2289 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 2290 | Value *OffsetVal = InsertCastBefore(Add, UnsType, IB); |
| 2291 | AddCST = ConstantExpr::getAdd(AddCST, Hi); |
| 2292 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 2293 | return new SetCondInst(Instruction::SetGT, OffsetVal, AddCST); |
| 2294 | } |
| 2295 | |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2296 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 2297 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 2298 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 2299 | // not, since all 1s are not contiguous. |
| 2300 | static bool isRunOfOnes(ConstantIntegral *Val, unsigned &MB, unsigned &ME) { |
| 2301 | uint64_t V = Val->getRawValue(); |
| 2302 | if (!isShiftedMask_64(V)) return false; |
| 2303 | |
| 2304 | // look for the first zero bit after the run of ones |
| 2305 | MB = 64-CountLeadingZeros_64((V - 1) ^ V); |
| 2306 | // look for the first non-zero bit |
| 2307 | ME = 64-CountLeadingZeros_64(V); |
| 2308 | return true; |
| 2309 | } |
| 2310 | |
| 2311 | |
| 2312 | |
| 2313 | /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask, |
| 2314 | /// where isSub determines whether the operator is a sub. If we can fold one of |
| 2315 | /// the following xforms: |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 2316 | /// |
| 2317 | /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask |
| 2318 | /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 2319 | /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 2320 | /// |
| 2321 | /// return (A +/- B). |
| 2322 | /// |
| 2323 | Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS, |
| 2324 | ConstantIntegral *Mask, bool isSub, |
| 2325 | Instruction &I) { |
| 2326 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
| 2327 | if (!LHSI || LHSI->getNumOperands() != 2 || |
| 2328 | !isa<ConstantInt>(LHSI->getOperand(1))) return 0; |
| 2329 | |
| 2330 | ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1)); |
| 2331 | |
| 2332 | switch (LHSI->getOpcode()) { |
| 2333 | default: return 0; |
| 2334 | case Instruction::And: |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2335 | if (ConstantExpr::getAnd(N, Mask) == Mask) { |
| 2336 | // If the AndRHS is a power of two minus one (0+1+), this is simple. |
| 2337 | if ((Mask->getRawValue() & Mask->getRawValue()+1) == 0) |
| 2338 | break; |
| 2339 | |
| 2340 | // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+ |
| 2341 | // part, we don't need any explicit masks to take them out of A. If that |
| 2342 | // is all N is, ignore it. |
| 2343 | unsigned MB, ME; |
| 2344 | if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 2345 | uint64_t Mask = RHS->getType()->getIntegralTypeMask(); |
| 2346 | Mask >>= 64-MB+1; |
| 2347 | if (MaskedValueIsZero(RHS, Mask)) |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2348 | break; |
| 2349 | } |
| 2350 | } |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 2351 | return 0; |
| 2352 | case Instruction::Or: |
| 2353 | case Instruction::Xor: |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2354 | // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0 |
| 2355 | if ((Mask->getRawValue() & Mask->getRawValue()+1) == 0 && |
| 2356 | ConstantExpr::getAnd(N, Mask)->isNullValue()) |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 2357 | break; |
| 2358 | return 0; |
| 2359 | } |
| 2360 | |
| 2361 | Instruction *New; |
| 2362 | if (isSub) |
| 2363 | New = BinaryOperator::createSub(LHSI->getOperand(0), RHS, "fold"); |
| 2364 | else |
| 2365 | New = BinaryOperator::createAdd(LHSI->getOperand(0), RHS, "fold"); |
| 2366 | return InsertNewInstBefore(New, I); |
| 2367 | } |
| 2368 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2369 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2370 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2371 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2372 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2373 | if (isa<UndefValue>(Op1)) // X & undef -> 0 |
| 2374 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2375 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2376 | // and X, X = X |
| 2377 | if (Op0 == Op1) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2378 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2379 | |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 2380 | // See if we can simplify any instructions used by the instruction whose sole |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 2381 | // purpose is to compute bits we don't care about. |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 2382 | uint64_t KnownZero, KnownOne; |
Chris Lattner | d70d9f5 | 2006-03-25 21:58:26 +0000 | [diff] [blame] | 2383 | if (!isa<PackedType>(I.getType()) && |
| 2384 | SimplifyDemandedBits(&I, I.getType()->getIntegralTypeMask(), |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 2385 | KnownZero, KnownOne)) |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 2386 | return &I; |
| 2387 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2388 | if (ConstantIntegral *AndRHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | ab2dc4d | 2006-02-08 07:34:50 +0000 | [diff] [blame] | 2389 | uint64_t AndRHSMask = AndRHS->getZExtValue(); |
| 2390 | uint64_t TypeMask = Op0->getType()->getIntegralTypeMask(); |
Chris Lattner | ab2dc4d | 2006-02-08 07:34:50 +0000 | [diff] [blame] | 2391 | uint64_t NotAndRHS = AndRHSMask^TypeMask; |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2392 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2393 | // Optimize a variety of ((val OP C1) & C2) combinations... |
| 2394 | if (isa<BinaryOperator>(Op0) || isa<ShiftInst>(Op0)) { |
| 2395 | Instruction *Op0I = cast<Instruction>(Op0); |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2396 | Value *Op0LHS = Op0I->getOperand(0); |
| 2397 | Value *Op0RHS = Op0I->getOperand(1); |
| 2398 | switch (Op0I->getOpcode()) { |
| 2399 | case Instruction::Xor: |
| 2400 | case Instruction::Or: |
Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 2401 | // If the mask is only needed on one incoming arm, push it up. |
| 2402 | if (Op0I->hasOneUse()) { |
| 2403 | if (MaskedValueIsZero(Op0LHS, NotAndRHS)) { |
| 2404 | // Not masking anything out for the LHS, move to RHS. |
| 2405 | Instruction *NewRHS = BinaryOperator::createAnd(Op0RHS, AndRHS, |
| 2406 | Op0RHS->getName()+".masked"); |
| 2407 | InsertNewInstBefore(NewRHS, I); |
| 2408 | return BinaryOperator::create( |
| 2409 | cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2410 | } |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 2411 | if (!isa<Constant>(Op0RHS) && |
Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 2412 | MaskedValueIsZero(Op0RHS, NotAndRHS)) { |
| 2413 | // Not masking anything out for the RHS, move to LHS. |
| 2414 | Instruction *NewLHS = BinaryOperator::createAnd(Op0LHS, AndRHS, |
| 2415 | Op0LHS->getName()+".masked"); |
| 2416 | InsertNewInstBefore(NewLHS, I); |
| 2417 | return BinaryOperator::create( |
| 2418 | cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS); |
| 2419 | } |
| 2420 | } |
| 2421 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2422 | break; |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 2423 | case Instruction::Add: |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2424 | // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS. |
| 2425 | // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 2426 | // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 2427 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I)) |
| 2428 | return BinaryOperator::createAnd(V, AndRHS); |
| 2429 | if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I)) |
| 2430 | return BinaryOperator::createAnd(V, AndRHS); // Add commutes |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 2431 | break; |
| 2432 | |
| 2433 | case Instruction::Sub: |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2434 | // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS. |
| 2435 | // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 2436 | // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 2437 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I)) |
| 2438 | return BinaryOperator::createAnd(V, AndRHS); |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 2439 | break; |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
Chris Lattner | 16464b3 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 2442 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2443 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2444 | return Res; |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2445 | } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
| 2446 | const Type *SrcTy = CI->getOperand(0)->getType(); |
| 2447 | |
Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 2448 | // If this is an integer truncation or change from signed-to-unsigned, and |
| 2449 | // if the source is an and/or with immediate, transform it. This |
| 2450 | // frequently occurs for bitfield accesses. |
| 2451 | if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) { |
| 2452 | if (SrcTy->getPrimitiveSizeInBits() >= |
| 2453 | I.getType()->getPrimitiveSizeInBits() && |
| 2454 | CastOp->getNumOperands() == 2) |
Chris Lattner | ab2dc4d | 2006-02-08 07:34:50 +0000 | [diff] [blame] | 2455 | if (ConstantInt *AndCI = dyn_cast<ConstantInt>(CastOp->getOperand(1))) |
Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 2456 | if (CastOp->getOpcode() == Instruction::And) { |
| 2457 | // Change: and (cast (and X, C1) to T), C2 |
| 2458 | // into : and (cast X to T), trunc(C1)&C2 |
| 2459 | // This will folds the two ands together, which may allow other |
| 2460 | // simplifications. |
| 2461 | Instruction *NewCast = |
| 2462 | new CastInst(CastOp->getOperand(0), I.getType(), |
| 2463 | CastOp->getName()+".shrunk"); |
| 2464 | NewCast = InsertNewInstBefore(NewCast, I); |
| 2465 | |
| 2466 | Constant *C3=ConstantExpr::getCast(AndCI, I.getType());//trunc(C1) |
| 2467 | C3 = ConstantExpr::getAnd(C3, AndRHS); // trunc(C1)&C2 |
| 2468 | return BinaryOperator::createAnd(NewCast, C3); |
| 2469 | } else if (CastOp->getOpcode() == Instruction::Or) { |
| 2470 | // Change: and (cast (or X, C1) to T), C2 |
| 2471 | // into : trunc(C1)&C2 iff trunc(C1)&C2 == C2 |
| 2472 | Constant *C3=ConstantExpr::getCast(AndCI, I.getType());//trunc(C1) |
| 2473 | if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS) // trunc(C1)&C2 |
| 2474 | return ReplaceInstUsesWith(I, AndRHS); |
| 2475 | } |
| 2476 | } |
Chris Lattner | 33217db | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 2477 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2478 | |
| 2479 | // Try to fold constant and into select arguments. |
| 2480 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2481 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2482 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2483 | if (isa<PHINode>(Op0)) |
| 2484 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2485 | return NV; |
Chris Lattner | 49b47ae | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2488 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 2489 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2490 | |
Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 2491 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
| 2492 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2493 | |
Misha Brukman | 9c003d8 | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 2494 | // (~A & ~B) == (~(A | B)) - De Morgan's Law |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2495 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2496 | Instruction *Or = BinaryOperator::createOr(Op0NotVal, Op1NotVal, |
| 2497 | I.getName()+".demorgan"); |
Chris Lattner | 49b47ae | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 2498 | InsertNewInstBefore(Or, I); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2499 | return BinaryOperator::createNot(Or); |
| 2500 | } |
Chris Lattner | 8b10ab3 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 2501 | |
| 2502 | { |
| 2503 | Value *A = 0, *B = 0; |
| 2504 | ConstantInt *C1 = 0, *C2 = 0; |
| 2505 | if (match(Op0, m_Or(m_Value(A), m_Value(B)))) |
| 2506 | if (A == Op1 || B == Op1) // (A | ?) & A --> A |
| 2507 | return ReplaceInstUsesWith(I, Op1); |
| 2508 | if (match(Op1, m_Or(m_Value(A), m_Value(B)))) |
| 2509 | if (A == Op0 || B == Op0) // A & (A | ?) --> A |
| 2510 | return ReplaceInstUsesWith(I, Op0); |
| 2511 | } |
| 2512 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2513 | |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 2514 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(Op1)) { |
| 2515 | // (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B) |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2516 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 2517 | return R; |
| 2518 | |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 2519 | Value *LHSVal, *RHSVal; |
| 2520 | ConstantInt *LHSCst, *RHSCst; |
| 2521 | Instruction::BinaryOps LHSCC, RHSCC; |
| 2522 | if (match(Op0, m_SetCond(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 2523 | if (match(RHS, m_SetCond(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 2524 | if (LHSVal == RHSVal && // Found (X setcc C1) & (X setcc C2) |
| 2525 | // Set[GL]E X, CST is folded to Set[GL]T elsewhere. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2526 | LHSCC != Instruction::SetGE && LHSCC != Instruction::SetLE && |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 2527 | RHSCC != Instruction::SetGE && RHSCC != Instruction::SetLE) { |
| 2528 | // Ensure that the larger constant is on the RHS. |
| 2529 | Constant *Cmp = ConstantExpr::getSetGT(LHSCst, RHSCst); |
| 2530 | SetCondInst *LHS = cast<SetCondInst>(Op0); |
| 2531 | if (cast<ConstantBool>(Cmp)->getValue()) { |
| 2532 | std::swap(LHS, RHS); |
| 2533 | std::swap(LHSCst, RHSCst); |
| 2534 | std::swap(LHSCC, RHSCC); |
| 2535 | } |
| 2536 | |
| 2537 | // At this point, we know we have have two setcc instructions |
| 2538 | // comparing a value against two constants and and'ing the result |
| 2539 | // together. Because of the above check, we know that we only have |
| 2540 | // SetEQ, SetNE, SetLT, and SetGT here. We also know (from the |
| 2541 | // FoldSetCCLogical check above), that the two constants are not |
| 2542 | // equal. |
| 2543 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 2544 | |
| 2545 | switch (LHSCC) { |
| 2546 | default: assert(0 && "Unknown integer condition code!"); |
| 2547 | case Instruction::SetEQ: |
| 2548 | switch (RHSCC) { |
| 2549 | default: assert(0 && "Unknown integer condition code!"); |
| 2550 | case Instruction::SetEQ: // (X == 13 & X == 15) -> false |
| 2551 | case Instruction::SetGT: // (X == 13 & X > 15) -> false |
| 2552 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2553 | case Instruction::SetNE: // (X == 13 & X != 15) -> X == 13 |
| 2554 | case Instruction::SetLT: // (X == 13 & X < 15) -> X == 13 |
| 2555 | return ReplaceInstUsesWith(I, LHS); |
| 2556 | } |
| 2557 | case Instruction::SetNE: |
| 2558 | switch (RHSCC) { |
| 2559 | default: assert(0 && "Unknown integer condition code!"); |
| 2560 | case Instruction::SetLT: |
| 2561 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X < 14) -> X < 13 |
| 2562 | return new SetCondInst(Instruction::SetLT, LHSVal, LHSCst); |
| 2563 | break; // (X != 13 & X < 15) -> no change |
| 2564 | case Instruction::SetEQ: // (X != 13 & X == 15) -> X == 15 |
| 2565 | case Instruction::SetGT: // (X != 13 & X > 15) -> X > 15 |
| 2566 | return ReplaceInstUsesWith(I, RHS); |
| 2567 | case Instruction::SetNE: |
| 2568 | if (LHSCst == SubOne(RHSCst)) {// (X != 13 & X != 14) -> X-13 >u 1 |
| 2569 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 2570 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 2571 | LHSVal->getName()+".off"); |
| 2572 | InsertNewInstBefore(Add, I); |
| 2573 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 2574 | Value *OffsetVal = InsertCastBefore(Add, UnsType, I); |
| 2575 | AddCST = ConstantExpr::getSub(RHSCst, LHSCst); |
| 2576 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 2577 | return new SetCondInst(Instruction::SetGT, OffsetVal, AddCST); |
| 2578 | } |
| 2579 | break; // (X != 13 & X != 15) -> no change |
| 2580 | } |
| 2581 | break; |
| 2582 | case Instruction::SetLT: |
| 2583 | switch (RHSCC) { |
| 2584 | default: assert(0 && "Unknown integer condition code!"); |
| 2585 | case Instruction::SetEQ: // (X < 13 & X == 15) -> false |
| 2586 | case Instruction::SetGT: // (X < 13 & X > 15) -> false |
| 2587 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2588 | case Instruction::SetNE: // (X < 13 & X != 15) -> X < 13 |
| 2589 | case Instruction::SetLT: // (X < 13 & X < 15) -> X < 13 |
| 2590 | return ReplaceInstUsesWith(I, LHS); |
| 2591 | } |
| 2592 | case Instruction::SetGT: |
| 2593 | switch (RHSCC) { |
| 2594 | default: assert(0 && "Unknown integer condition code!"); |
| 2595 | case Instruction::SetEQ: // (X > 13 & X == 15) -> X > 13 |
| 2596 | return ReplaceInstUsesWith(I, LHS); |
| 2597 | case Instruction::SetGT: // (X > 13 & X > 15) -> X > 15 |
| 2598 | return ReplaceInstUsesWith(I, RHS); |
| 2599 | case Instruction::SetNE: |
| 2600 | if (RHSCst == AddOne(LHSCst)) // (X > 13 & X != 14) -> X > 14 |
| 2601 | return new SetCondInst(Instruction::SetGT, LHSVal, RHSCst); |
| 2602 | break; // (X > 13 & X != 15) -> no change |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2603 | case Instruction::SetLT: // (X > 13 & X < 15) -> (X-14) <u 1 |
| 2604 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, true, I); |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 2605 | } |
| 2606 | } |
| 2607 | } |
| 2608 | } |
| 2609 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2610 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2613 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2614 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2615 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2616 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2617 | if (isa<UndefValue>(Op1)) |
| 2618 | return ReplaceInstUsesWith(I, // X | undef -> -1 |
| 2619 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 2620 | |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 2621 | // or X, X = X |
| 2622 | if (Op0 == Op1) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2623 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2624 | |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 2625 | // See if we can simplify any instructions used by the instruction whose sole |
| 2626 | // purpose is to compute bits we don't care about. |
| 2627 | uint64_t KnownZero, KnownOne; |
Chris Lattner | d70d9f5 | 2006-03-25 21:58:26 +0000 | [diff] [blame] | 2628 | if (!isa<PackedType>(I.getType()) && |
| 2629 | SimplifyDemandedBits(&I, I.getType()->getIntegralTypeMask(), |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 2630 | KnownZero, KnownOne)) |
| 2631 | return &I; |
| 2632 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2633 | // or X, -1 == -1 |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 2634 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2635 | ConstantInt *C1 = 0; Value *X = 0; |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2636 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
| 2637 | 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] | 2638 | Instruction *Or = BinaryOperator::createOr(X, RHS, Op0->getName()); |
| 2639 | Op0->setName(""); |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2640 | InsertNewInstBefore(Or, I); |
| 2641 | return BinaryOperator::createAnd(Or, ConstantExpr::getOr(RHS, C1)); |
| 2642 | } |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 2643 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2644 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
| 2645 | if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { |
| 2646 | std::string Op0Name = Op0->getName(); Op0->setName(""); |
| 2647 | Instruction *Or = BinaryOperator::createOr(X, RHS, Op0Name); |
| 2648 | InsertNewInstBefore(Or, I); |
| 2649 | return BinaryOperator::createXor(Or, |
| 2650 | ConstantExpr::getAnd(C1, ConstantExpr::getNot(RHS))); |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 2651 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2652 | |
| 2653 | // Try to fold constant and into select arguments. |
| 2654 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2655 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2656 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2657 | if (isa<PHINode>(Op0)) |
| 2658 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2659 | return NV; |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 2660 | } |
| 2661 | |
Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2662 | Value *A = 0, *B = 0; |
| 2663 | ConstantInt *C1 = 0, *C2 = 0; |
Chris Lattner | 4294cec | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 2664 | |
| 2665 | if (match(Op0, m_And(m_Value(A), m_Value(B)))) |
| 2666 | if (A == Op1 || B == Op1) // (A & ?) | A --> A |
| 2667 | return ReplaceInstUsesWith(I, Op1); |
| 2668 | if (match(Op1, m_And(m_Value(A), m_Value(B)))) |
| 2669 | if (A == Op0 || B == Op0) // A | (A & ?) --> A |
| 2670 | return ReplaceInstUsesWith(I, Op0); |
| 2671 | |
Chris Lattner | b62f508 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 2672 | // (X^C)|Y -> (X|Y)^C iff Y&C == 0 |
| 2673 | if (Op0->hasOneUse() && match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) && |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 2674 | MaskedValueIsZero(Op1, C1->getZExtValue())) { |
Chris Lattner | b62f508 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 2675 | Instruction *NOr = BinaryOperator::createOr(A, Op1, Op0->getName()); |
| 2676 | Op0->setName(""); |
| 2677 | return BinaryOperator::createXor(InsertNewInstBefore(NOr, I), C1); |
| 2678 | } |
| 2679 | |
| 2680 | // Y|(X^C) -> (X|Y)^C iff Y&C == 0 |
| 2681 | if (Op1->hasOneUse() && match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) && |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 2682 | MaskedValueIsZero(Op0, C1->getZExtValue())) { |
Chris Lattner | b62f508 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 2683 | Instruction *NOr = BinaryOperator::createOr(A, Op0, Op1->getName()); |
| 2684 | Op0->setName(""); |
| 2685 | return BinaryOperator::createXor(InsertNewInstBefore(NOr, I), C1); |
| 2686 | } |
| 2687 | |
Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 2688 | // (A & C1)|(B & C2) |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2689 | if (match(Op0, m_And(m_Value(A), m_ConstantInt(C1))) && |
Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 2690 | match(Op1, m_And(m_Value(B), m_ConstantInt(C2)))) { |
| 2691 | |
| 2692 | if (A == B) // (A & C1)|(A & C2) == A & (C1|C2) |
| 2693 | return BinaryOperator::createAnd(A, ConstantExpr::getOr(C1, C2)); |
| 2694 | |
| 2695 | |
Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 2696 | // If we have: ((V + N) & C1) | (V & C2) |
| 2697 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 2698 | // replace with V+N. |
| 2699 | if (C1 == ConstantExpr::getNot(C2)) { |
Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2700 | Value *V1 = 0, *V2 = 0; |
Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 2701 | if ((C2->getRawValue() & (C2->getRawValue()+1)) == 0 && // C2 == 0+1+ |
| 2702 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { |
| 2703 | // Add commutes, try both ways. |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 2704 | if (V1 == B && MaskedValueIsZero(V2, C2->getZExtValue())) |
Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 2705 | return ReplaceInstUsesWith(I, A); |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 2706 | if (V2 == B && MaskedValueIsZero(V1, C2->getZExtValue())) |
Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 2707 | return ReplaceInstUsesWith(I, A); |
| 2708 | } |
| 2709 | // Or commutes, try both ways. |
| 2710 | if ((C1->getRawValue() & (C1->getRawValue()+1)) == 0 && |
| 2711 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { |
| 2712 | // Add commutes, try both ways. |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 2713 | if (V1 == A && MaskedValueIsZero(V2, C1->getZExtValue())) |
Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 2714 | return ReplaceInstUsesWith(I, B); |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 2715 | if (V2 == A && MaskedValueIsZero(V1, C1->getZExtValue())) |
Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 2716 | return ReplaceInstUsesWith(I, B); |
Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 2717 | } |
| 2718 | } |
| 2719 | } |
Chris Lattner | 812aab7 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 2720 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2721 | if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1 |
| 2722 | if (A == Op1) // ~A | A == -1 |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2723 | return ReplaceInstUsesWith(I, |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2724 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 2725 | } else { |
| 2726 | A = 0; |
| 2727 | } |
Chris Lattner | 4294cec | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 2728 | // Note, A is still live here! |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2729 | if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B |
| 2730 | if (Op0 == B) |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2731 | return ReplaceInstUsesWith(I, |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2732 | ConstantIntegral::getAllOnesValue(I.getType())); |
Chris Lattner | 3e327a4 | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 2733 | |
Misha Brukman | 9c003d8 | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 2734 | // (~A | ~B) == (~(A & B)) - De Morgan's Law |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2735 | if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
| 2736 | Value *And = InsertNewInstBefore(BinaryOperator::createAnd(A, B, |
| 2737 | I.getName()+".demorgan"), I); |
| 2738 | return BinaryOperator::createNot(And); |
| 2739 | } |
Chris Lattner | 3e327a4 | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 2740 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2741 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2742 | // (setcc1 A, B) | (setcc2 A, B) --> (setcc3 A, B) |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2743 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1))) { |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2744 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 2745 | return R; |
| 2746 | |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2747 | Value *LHSVal, *RHSVal; |
| 2748 | ConstantInt *LHSCst, *RHSCst; |
| 2749 | Instruction::BinaryOps LHSCC, RHSCC; |
| 2750 | if (match(Op0, m_SetCond(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 2751 | if (match(RHS, m_SetCond(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 2752 | if (LHSVal == RHSVal && // Found (X setcc C1) | (X setcc C2) |
| 2753 | // Set[GL]E X, CST is folded to Set[GL]T elsewhere. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2754 | LHSCC != Instruction::SetGE && LHSCC != Instruction::SetLE && |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2755 | RHSCC != Instruction::SetGE && RHSCC != Instruction::SetLE) { |
| 2756 | // Ensure that the larger constant is on the RHS. |
| 2757 | Constant *Cmp = ConstantExpr::getSetGT(LHSCst, RHSCst); |
| 2758 | SetCondInst *LHS = cast<SetCondInst>(Op0); |
| 2759 | if (cast<ConstantBool>(Cmp)->getValue()) { |
| 2760 | std::swap(LHS, RHS); |
| 2761 | std::swap(LHSCst, RHSCst); |
| 2762 | std::swap(LHSCC, RHSCC); |
| 2763 | } |
| 2764 | |
| 2765 | // At this point, we know we have have two setcc instructions |
| 2766 | // comparing a value against two constants and or'ing the result |
| 2767 | // together. Because of the above check, we know that we only have |
| 2768 | // SetEQ, SetNE, SetLT, and SetGT here. We also know (from the |
| 2769 | // FoldSetCCLogical check above), that the two constants are not |
| 2770 | // equal. |
| 2771 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 2772 | |
| 2773 | switch (LHSCC) { |
| 2774 | default: assert(0 && "Unknown integer condition code!"); |
| 2775 | case Instruction::SetEQ: |
| 2776 | switch (RHSCC) { |
| 2777 | default: assert(0 && "Unknown integer condition code!"); |
| 2778 | case Instruction::SetEQ: |
| 2779 | if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13 <u 2 |
| 2780 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 2781 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 2782 | LHSVal->getName()+".off"); |
| 2783 | InsertNewInstBefore(Add, I); |
| 2784 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 2785 | Value *OffsetVal = InsertCastBefore(Add, UnsType, I); |
| 2786 | AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst); |
| 2787 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 2788 | return new SetCondInst(Instruction::SetLT, OffsetVal, AddCST); |
| 2789 | } |
| 2790 | break; // (X == 13 | X == 15) -> no change |
| 2791 | |
Chris Lattner | 5c21946 | 2005-04-19 06:04:18 +0000 | [diff] [blame] | 2792 | case Instruction::SetGT: // (X == 13 | X > 14) -> no change |
| 2793 | break; |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2794 | case Instruction::SetNE: // (X == 13 | X != 15) -> X != 15 |
| 2795 | case Instruction::SetLT: // (X == 13 | X < 15) -> X < 15 |
| 2796 | return ReplaceInstUsesWith(I, RHS); |
| 2797 | } |
| 2798 | break; |
| 2799 | case Instruction::SetNE: |
| 2800 | switch (RHSCC) { |
| 2801 | default: assert(0 && "Unknown integer condition code!"); |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2802 | case Instruction::SetEQ: // (X != 13 | X == 15) -> X != 13 |
| 2803 | case Instruction::SetGT: // (X != 13 | X > 15) -> X != 13 |
| 2804 | return ReplaceInstUsesWith(I, LHS); |
| 2805 | case Instruction::SetNE: // (X != 13 | X != 15) -> true |
Chris Lattner | 2ceb6ee | 2005-06-17 03:59:17 +0000 | [diff] [blame] | 2806 | case Instruction::SetLT: // (X != 13 | X < 15) -> true |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2807 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2808 | } |
| 2809 | break; |
| 2810 | case Instruction::SetLT: |
| 2811 | switch (RHSCC) { |
| 2812 | default: assert(0 && "Unknown integer condition code!"); |
| 2813 | case Instruction::SetEQ: // (X < 13 | X == 14) -> no change |
| 2814 | break; |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2815 | case Instruction::SetGT: // (X < 13 | X > 15) -> (X-13) > 2 |
| 2816 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false, I); |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 2817 | case Instruction::SetNE: // (X < 13 | X != 15) -> X != 15 |
| 2818 | case Instruction::SetLT: // (X < 13 | X < 15) -> X < 15 |
| 2819 | return ReplaceInstUsesWith(I, RHS); |
| 2820 | } |
| 2821 | break; |
| 2822 | case Instruction::SetGT: |
| 2823 | switch (RHSCC) { |
| 2824 | default: assert(0 && "Unknown integer condition code!"); |
| 2825 | case Instruction::SetEQ: // (X > 13 | X == 15) -> X > 13 |
| 2826 | case Instruction::SetGT: // (X > 13 | X > 15) -> X > 13 |
| 2827 | return ReplaceInstUsesWith(I, LHS); |
| 2828 | case Instruction::SetNE: // (X > 13 | X != 15) -> true |
| 2829 | case Instruction::SetLT: // (X > 13 | X < 15) -> true |
| 2830 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2831 | } |
| 2832 | } |
| 2833 | } |
| 2834 | } |
Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 2835 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2836 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 2839 | // XorSelf - Implements: X ^ X --> 0 |
| 2840 | struct XorSelf { |
| 2841 | Value *RHS; |
| 2842 | XorSelf(Value *rhs) : RHS(rhs) {} |
| 2843 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 2844 | Instruction *apply(BinaryOperator &Xor) const { |
| 2845 | return &Xor; |
| 2846 | } |
| 2847 | }; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2848 | |
| 2849 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2850 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2851 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2852 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2853 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2854 | if (isa<UndefValue>(Op1)) |
| 2855 | return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef |
| 2856 | |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 2857 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
| 2858 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { |
| 2859 | assert(Result == &I && "AssociativeOpt didn't work?"); |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2860 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 2861 | } |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 2862 | |
| 2863 | // See if we can simplify any instructions used by the instruction whose sole |
| 2864 | // purpose is to compute bits we don't care about. |
| 2865 | uint64_t KnownZero, KnownOne; |
Chris Lattner | d70d9f5 | 2006-03-25 21:58:26 +0000 | [diff] [blame] | 2866 | if (!isa<PackedType>(I.getType()) && |
| 2867 | SimplifyDemandedBits(&I, I.getType()->getIntegralTypeMask(), |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 2868 | KnownZero, KnownOne)) |
| 2869 | return &I; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2870 | |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2871 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2872 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 2873 | // xor (setcc A, B), true = not (setcc A, B) = setncc A, B |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2874 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(Op0I)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2875 | if (RHS == ConstantBool::True && SCI->hasOneUse()) |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 2876 | return new SetCondInst(SCI->getInverseCondition(), |
| 2877 | SCI->getOperand(0), SCI->getOperand(1)); |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 2878 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2879 | // ~(c-X) == X-c-1 == X+(-c-1) |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2880 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
| 2881 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2882 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); |
| 2883 | Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2884 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2885 | return BinaryOperator::createAdd(Op0I->getOperand(1), ConstantRHS); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2886 | } |
Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 2887 | |
| 2888 | // ~(~X & Y) --> (X | ~Y) |
| 2889 | if (Op0I->getOpcode() == Instruction::And && RHS->isAllOnesValue()) { |
| 2890 | if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands(); |
| 2891 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { |
| 2892 | Instruction *NotY = |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2893 | BinaryOperator::createNot(Op0I->getOperand(1), |
Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 2894 | Op0I->getOperand(1)->getName()+".not"); |
| 2895 | InsertNewInstBefore(NotY, I); |
| 2896 | return BinaryOperator::createOr(Op0NotVal, NotY); |
| 2897 | } |
| 2898 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2899 | |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2900 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 2901 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 0f68fa6 | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 2902 | // ~(X-c) --> (-c-1)-X |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2903 | if (RHS->isAllOnesValue()) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2904 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); |
| 2905 | return BinaryOperator::createSub( |
| 2906 | ConstantExpr::getSub(NegOp0CI, |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2907 | ConstantInt::get(I.getType(), 1)), |
Chris Lattner | 0f68fa6 | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 2908 | Op0I->getOperand(0)); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2909 | } |
Chris Lattner | f78df7c | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 2910 | } else if (Op0I->getOpcode() == Instruction::Or) { |
| 2911 | // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0 |
| 2912 | if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getZExtValue())) { |
| 2913 | Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS); |
| 2914 | // Anything in both C1 and C2 is known to be zero, remove it from |
| 2915 | // NewRHS. |
| 2916 | Constant *CommonBits = ConstantExpr::getAnd(Op0CI, RHS); |
| 2917 | NewRHS = ConstantExpr::getAnd(NewRHS, |
| 2918 | ConstantExpr::getNot(CommonBits)); |
| 2919 | WorkList.push_back(Op0I); |
| 2920 | I.setOperand(0, Op0I->getOperand(0)); |
| 2921 | I.setOperand(1, NewRHS); |
| 2922 | return &I; |
| 2923 | } |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2924 | } |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 2925 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2926 | |
| 2927 | // Try to fold constant and into select arguments. |
| 2928 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2929 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2930 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2931 | if (isa<PHINode>(Op0)) |
| 2932 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2933 | return NV; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2934 | } |
| 2935 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2936 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2937 | if (X == Op1) |
| 2938 | return ReplaceInstUsesWith(I, |
| 2939 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 2940 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2941 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2942 | if (X == Op0) |
| 2943 | return ReplaceInstUsesWith(I, |
| 2944 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 2945 | |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2946 | if (Instruction *Op1I = dyn_cast<Instruction>(Op1)) |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 2947 | if (Op1I->getOpcode() == Instruction::Or) { |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2948 | if (Op1I->getOperand(0) == Op0) { // B^(B|A) == (A|B)^B |
| 2949 | cast<BinaryOperator>(Op1I)->swapOperands(); |
| 2950 | I.swapOperands(); |
| 2951 | std::swap(Op0, Op1); |
| 2952 | } else if (Op1I->getOperand(1) == Op0) { // B^(A|B) == (A|B)^B |
| 2953 | I.swapOperands(); |
| 2954 | std::swap(Op0, Op1); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2955 | } |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 2956 | } else if (Op1I->getOpcode() == Instruction::Xor) { |
| 2957 | if (Op0 == Op1I->getOperand(0)) // A^(A^B) == B |
| 2958 | return ReplaceInstUsesWith(I, Op1I->getOperand(1)); |
| 2959 | else if (Op0 == Op1I->getOperand(1)) // A^(B^A) == B |
| 2960 | return ReplaceInstUsesWith(I, Op1I->getOperand(0)); |
| 2961 | } |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2962 | |
| 2963 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2964 | if (Op0I->getOpcode() == Instruction::Or && Op0I->hasOneUse()) { |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2965 | if (Op0I->getOperand(0) == Op1) // (B|A)^B == (A|B)^B |
| 2966 | cast<BinaryOperator>(Op0I)->swapOperands(); |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2967 | if (Op0I->getOperand(1) == Op1) { // (A|B)^B == A & ~B |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 2968 | Value *NotB = InsertNewInstBefore(BinaryOperator::createNot(Op1, |
| 2969 | Op1->getName()+".not"), I); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2970 | return BinaryOperator::createAnd(Op0I->getOperand(0), NotB); |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2971 | } |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 2972 | } else if (Op0I->getOpcode() == Instruction::Xor) { |
| 2973 | if (Op1 == Op0I->getOperand(0)) // (A^B)^A == B |
| 2974 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 2975 | else if (Op1 == Op0I->getOperand(1)) // (B^A)^A == B |
| 2976 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2979 | // (setcc1 A, B) ^ (setcc2 A, B) --> (setcc3 A, B) |
| 2980 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1))) |
| 2981 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 2982 | return R; |
| 2983 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2984 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2985 | } |
| 2986 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2987 | /// MulWithOverflow - Compute Result = In1*In2, returning true if the result |
| 2988 | /// overflowed for this type. |
| 2989 | static bool MulWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
| 2990 | ConstantInt *In2) { |
| 2991 | Result = cast<ConstantInt>(ConstantExpr::getMul(In1, In2)); |
| 2992 | return !In2->isNullValue() && ConstantExpr::getDiv(Result, In2) != In1; |
| 2993 | } |
| 2994 | |
| 2995 | static bool isPositive(ConstantInt *C) { |
| 2996 | return cast<ConstantSInt>(C)->getValue() >= 0; |
| 2997 | } |
| 2998 | |
| 2999 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result |
| 3000 | /// overflowed for this type. |
| 3001 | static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
| 3002 | ConstantInt *In2) { |
| 3003 | Result = cast<ConstantInt>(ConstantExpr::getAdd(In1, In2)); |
| 3004 | |
| 3005 | if (In1->getType()->isUnsigned()) |
| 3006 | return cast<ConstantUInt>(Result)->getValue() < |
| 3007 | cast<ConstantUInt>(In1)->getValue(); |
| 3008 | if (isPositive(In1) != isPositive(In2)) |
| 3009 | return false; |
| 3010 | if (isPositive(In1)) |
| 3011 | return cast<ConstantSInt>(Result)->getValue() < |
| 3012 | cast<ConstantSInt>(In1)->getValue(); |
| 3013 | return cast<ConstantSInt>(Result)->getValue() > |
| 3014 | cast<ConstantSInt>(In1)->getValue(); |
| 3015 | } |
| 3016 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3017 | /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the |
| 3018 | /// code necessary to compute the offset from the base pointer (without adding |
| 3019 | /// in the base pointer). Return the result as a signed integer of intptr size. |
| 3020 | static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { |
| 3021 | TargetData &TD = IC.getTargetData(); |
| 3022 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 3023 | const Type *UIntPtrTy = TD.getIntPtrType(); |
| 3024 | const Type *SIntPtrTy = UIntPtrTy->getSignedVersion(); |
| 3025 | Value *Result = Constant::getNullValue(SIntPtrTy); |
| 3026 | |
| 3027 | // Build a mask for high order bits. |
Chris Lattner | 77defba | 2006-02-07 07:00:41 +0000 | [diff] [blame] | 3028 | uint64_t PtrSizeMask = ~0ULL >> (64-TD.getPointerSize()*8); |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3029 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3030 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { |
| 3031 | Value *Op = GEP->getOperand(i); |
Chris Lattner | d35d210 | 2005-01-13 23:26:48 +0000 | [diff] [blame] | 3032 | uint64_t Size = TD.getTypeSize(GTI.getIndexedType()) & PtrSizeMask; |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3033 | Constant *Scale = ConstantExpr::getCast(ConstantUInt::get(UIntPtrTy, Size), |
| 3034 | SIntPtrTy); |
| 3035 | if (Constant *OpC = dyn_cast<Constant>(Op)) { |
| 3036 | if (!OpC->isNullValue()) { |
Chris Lattner | 4cb9fa3 | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 3037 | OpC = ConstantExpr::getCast(OpC, SIntPtrTy); |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3038 | Scale = ConstantExpr::getMul(OpC, Scale); |
| 3039 | if (Constant *RC = dyn_cast<Constant>(Result)) |
| 3040 | Result = ConstantExpr::getAdd(RC, Scale); |
| 3041 | else { |
| 3042 | // Emit an add instruction. |
| 3043 | Result = IC.InsertNewInstBefore( |
| 3044 | BinaryOperator::createAdd(Result, Scale, |
| 3045 | GEP->getName()+".offs"), I); |
| 3046 | } |
| 3047 | } |
| 3048 | } else { |
Chris Lattner | 7aa41cf | 2005-01-14 17:17:59 +0000 | [diff] [blame] | 3049 | // Convert to correct type. |
| 3050 | Op = IC.InsertNewInstBefore(new CastInst(Op, SIntPtrTy, |
| 3051 | Op->getName()+".c"), I); |
| 3052 | if (Size != 1) |
Chris Lattner | 4cb9fa3 | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 3053 | // We'll let instcombine(mul) convert this to a shl if possible. |
| 3054 | Op = IC.InsertNewInstBefore(BinaryOperator::createMul(Op, Scale, |
| 3055 | GEP->getName()+".idx"), I); |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3056 | |
| 3057 | // Emit an add instruction. |
Chris Lattner | 4cb9fa3 | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 3058 | Result = IC.InsertNewInstBefore(BinaryOperator::createAdd(Op, Result, |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3059 | GEP->getName()+".offs"), I); |
| 3060 | } |
| 3061 | } |
| 3062 | return Result; |
| 3063 | } |
| 3064 | |
| 3065 | /// FoldGEPSetCC - Fold comparisons between a GEP instruction and something |
| 3066 | /// else. At this point we know that the GEP is on the LHS of the comparison. |
| 3067 | Instruction *InstCombiner::FoldGEPSetCC(User *GEPLHS, Value *RHS, |
| 3068 | Instruction::BinaryOps Cond, |
| 3069 | Instruction &I) { |
| 3070 | assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!"); |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 3071 | |
| 3072 | if (CastInst *CI = dyn_cast<CastInst>(RHS)) |
| 3073 | if (isa<PointerType>(CI->getOperand(0)->getType())) |
| 3074 | RHS = CI->getOperand(0); |
| 3075 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3076 | Value *PtrBase = GEPLHS->getOperand(0); |
| 3077 | if (PtrBase == RHS) { |
| 3078 | // As an optimization, we don't actually have to compute the actual value of |
| 3079 | // OFFSET if this is a seteq or setne comparison, just return whether each |
| 3080 | // index is zero or not. |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 3081 | if (Cond == Instruction::SetEQ || Cond == Instruction::SetNE) { |
| 3082 | Instruction *InVal = 0; |
Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 3083 | gep_type_iterator GTI = gep_type_begin(GEPLHS); |
| 3084 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i, ++GTI) { |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 3085 | bool EmitIt = true; |
| 3086 | if (Constant *C = dyn_cast<Constant>(GEPLHS->getOperand(i))) { |
| 3087 | if (isa<UndefValue>(C)) // undef index -> undef. |
| 3088 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); |
| 3089 | if (C->isNullValue()) |
| 3090 | EmitIt = false; |
Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 3091 | else if (TD->getTypeSize(GTI.getIndexedType()) == 0) { |
| 3092 | EmitIt = false; // This is indexing into a zero sized array? |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3093 | } else if (isa<ConstantInt>(C)) |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 3094 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
| 3095 | ConstantBool::get(Cond == Instruction::SetNE)); |
| 3096 | } |
| 3097 | |
| 3098 | if (EmitIt) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3099 | Instruction *Comp = |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 3100 | new SetCondInst(Cond, GEPLHS->getOperand(i), |
| 3101 | Constant::getNullValue(GEPLHS->getOperand(i)->getType())); |
| 3102 | if (InVal == 0) |
| 3103 | InVal = Comp; |
| 3104 | else { |
| 3105 | InVal = InsertNewInstBefore(InVal, I); |
| 3106 | InsertNewInstBefore(Comp, I); |
| 3107 | if (Cond == Instruction::SetNE) // True if any are unequal |
| 3108 | InVal = BinaryOperator::createOr(InVal, Comp); |
| 3109 | else // True if all are equal |
| 3110 | InVal = BinaryOperator::createAnd(InVal, Comp); |
| 3111 | } |
| 3112 | } |
| 3113 | } |
| 3114 | |
| 3115 | if (InVal) |
| 3116 | return InVal; |
| 3117 | else |
| 3118 | ReplaceInstUsesWith(I, // No comparison is needed here, all indexes = 0 |
| 3119 | ConstantBool::get(Cond == Instruction::SetEQ)); |
| 3120 | } |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3121 | |
| 3122 | // Only lower this if the setcc is the only user of the GEP or if we expect |
| 3123 | // the result to fold to a constant! |
| 3124 | if (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) { |
| 3125 | // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). |
| 3126 | Value *Offset = EmitGEPOffset(GEPLHS, I, *this); |
| 3127 | return new SetCondInst(Cond, Offset, |
| 3128 | Constant::getNullValue(Offset->getType())); |
| 3129 | } |
| 3130 | } else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) { |
Chris Lattner | a21bf8d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 3131 | // If the base pointers are different, but the indices are the same, just |
| 3132 | // compare the base pointer. |
| 3133 | if (PtrBase != GEPRHS->getOperand(0)) { |
| 3134 | bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands(); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3135 | IndicesTheSame &= GEPLHS->getOperand(0)->getType() == |
Chris Lattner | bd43b9d | 2005-04-26 14:40:41 +0000 | [diff] [blame] | 3136 | GEPRHS->getOperand(0)->getType(); |
Chris Lattner | a21bf8d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 3137 | if (IndicesTheSame) |
| 3138 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 3139 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 3140 | IndicesTheSame = false; |
| 3141 | break; |
| 3142 | } |
| 3143 | |
| 3144 | // If all indices are the same, just compare the base pointers. |
| 3145 | if (IndicesTheSame) |
| 3146 | return new SetCondInst(Cond, GEPLHS->getOperand(0), |
| 3147 | GEPRHS->getOperand(0)); |
| 3148 | |
| 3149 | // Otherwise, the base pointers are different and the indices are |
| 3150 | // different, bail out. |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3151 | return 0; |
Chris Lattner | a21bf8d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 3152 | } |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3153 | |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 3154 | // If one of the GEPs has all zero indices, recurse. |
| 3155 | bool AllZeros = true; |
| 3156 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 3157 | if (!isa<Constant>(GEPLHS->getOperand(i)) || |
| 3158 | !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) { |
| 3159 | AllZeros = false; |
| 3160 | break; |
| 3161 | } |
| 3162 | if (AllZeros) |
| 3163 | return FoldGEPSetCC(GEPRHS, GEPLHS->getOperand(0), |
| 3164 | SetCondInst::getSwappedCondition(Cond), I); |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 3165 | |
| 3166 | // If the other GEP has all zero indices, recurse. |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 3167 | AllZeros = true; |
| 3168 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 3169 | if (!isa<Constant>(GEPRHS->getOperand(i)) || |
| 3170 | !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) { |
| 3171 | AllZeros = false; |
| 3172 | break; |
| 3173 | } |
| 3174 | if (AllZeros) |
| 3175 | return FoldGEPSetCC(GEPLHS, GEPRHS->getOperand(0), Cond, I); |
| 3176 | |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 3177 | if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) { |
| 3178 | // If the GEPs only differ by one index, compare it. |
| 3179 | unsigned NumDifferences = 0; // Keep track of # differences. |
| 3180 | unsigned DiffOperand = 0; // The operand that differs. |
| 3181 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 3182 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3183 | if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() != |
| 3184 | GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | fc4429e | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 3185 | // Irreconcilable differences. |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 3186 | NumDifferences = 2; |
| 3187 | break; |
| 3188 | } else { |
| 3189 | if (NumDifferences++) break; |
| 3190 | DiffOperand = i; |
| 3191 | } |
| 3192 | } |
| 3193 | |
| 3194 | if (NumDifferences == 0) // SAME GEP? |
| 3195 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
| 3196 | ConstantBool::get(Cond == Instruction::SetEQ)); |
| 3197 | else if (NumDifferences == 1) { |
Chris Lattner | fc4429e | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 3198 | Value *LHSV = GEPLHS->getOperand(DiffOperand); |
| 3199 | Value *RHSV = GEPRHS->getOperand(DiffOperand); |
Chris Lattner | 247aef8 | 2005-07-18 23:07:33 +0000 | [diff] [blame] | 3200 | |
| 3201 | // Convert the operands to signed values to make sure to perform a |
| 3202 | // signed comparison. |
| 3203 | const Type *NewTy = LHSV->getType()->getSignedVersion(); |
| 3204 | if (LHSV->getType() != NewTy) |
| 3205 | LHSV = InsertNewInstBefore(new CastInst(LHSV, NewTy, |
| 3206 | LHSV->getName()), I); |
| 3207 | if (RHSV->getType() != NewTy) |
| 3208 | RHSV = InsertNewInstBefore(new CastInst(RHSV, NewTy, |
| 3209 | RHSV->getName()), I); |
| 3210 | return new SetCondInst(Cond, LHSV, RHSV); |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 3211 | } |
| 3212 | } |
| 3213 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3214 | // Only lower this if the setcc is the only user of the GEP or if we expect |
| 3215 | // the result to fold to a constant! |
| 3216 | if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) && |
| 3217 | (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) { |
| 3218 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) |
| 3219 | Value *L = EmitGEPOffset(GEPLHS, I, *this); |
| 3220 | Value *R = EmitGEPOffset(GEPRHS, I, *this); |
| 3221 | return new SetCondInst(Cond, L, R); |
| 3222 | } |
| 3223 | } |
| 3224 | return 0; |
| 3225 | } |
| 3226 | |
| 3227 | |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3228 | Instruction *InstCombiner::visitSetCondInst(SetCondInst &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3229 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3230 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 3231 | const Type *Ty = Op0->getType(); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3232 | |
| 3233 | // setcc X, X |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3234 | if (Op0 == Op1) |
| 3235 | return ReplaceInstUsesWith(I, ConstantBool::get(isTrueWhenEqual(I))); |
Chris Lattner | 1fc23f3 | 2002-05-09 20:11:54 +0000 | [diff] [blame] | 3236 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3237 | if (isa<UndefValue>(Op1)) // X setcc undef -> undef |
| 3238 | return ReplaceInstUsesWith(I, UndefValue::get(Type::BoolTy)); |
| 3239 | |
Chris Lattner | 15ff1e1 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 3240 | // setcc <global/alloca*/null>, <global/alloca*/null> - Global/Stack value |
| 3241 | // addresses never equal each other! We already know that Op0 != Op1. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3242 | if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) || |
| 3243 | isa<ConstantPointerNull>(Op0)) && |
| 3244 | (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) || |
Chris Lattner | 15ff1e1 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 3245 | isa<ConstantPointerNull>(Op1))) |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3246 | return ReplaceInstUsesWith(I, ConstantBool::get(!isTrueWhenEqual(I))); |
| 3247 | |
| 3248 | // setcc's with boolean values can always be turned into bitwise operations |
| 3249 | if (Ty == Type::BoolTy) { |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 3250 | switch (I.getOpcode()) { |
| 3251 | default: assert(0 && "Invalid setcc instruction!"); |
| 3252 | case Instruction::SetEQ: { // seteq bool %A, %B -> ~(A^B) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3253 | Instruction *Xor = BinaryOperator::createXor(Op0, Op1, I.getName()+"tmp"); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3254 | InsertNewInstBefore(Xor, I); |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 3255 | return BinaryOperator::createNot(Xor); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3256 | } |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 3257 | case Instruction::SetNE: |
| 3258 | return BinaryOperator::createXor(Op0, Op1); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3259 | |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 3260 | case Instruction::SetGT: |
| 3261 | std::swap(Op0, Op1); // Change setgt -> setlt |
| 3262 | // FALL THROUGH |
| 3263 | case Instruction::SetLT: { // setlt bool A, B -> ~X & Y |
| 3264 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 3265 | InsertNewInstBefore(Not, I); |
| 3266 | return BinaryOperator::createAnd(Not, Op1); |
| 3267 | } |
| 3268 | case Instruction::SetGE: |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3269 | std::swap(Op0, Op1); // Change setge -> setle |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 3270 | // FALL THROUGH |
| 3271 | case Instruction::SetLE: { // setle bool %A, %B -> ~A | B |
| 3272 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 3273 | InsertNewInstBefore(Not, I); |
| 3274 | return BinaryOperator::createOr(Not, Op1); |
| 3275 | } |
| 3276 | } |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3277 | } |
| 3278 | |
Chris Lattner | 2dd0174 | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 3279 | // See if we are doing a comparison between a constant and an instruction that |
| 3280 | // can be folded into the comparison. |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3281 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3282 | // Check to see if we are comparing against the minimum or maximum value... |
| 3283 | if (CI->isMinValue()) { |
| 3284 | if (I.getOpcode() == Instruction::SetLT) // A < MIN -> FALSE |
| 3285 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 3286 | if (I.getOpcode() == Instruction::SetGE) // A >= MIN -> TRUE |
| 3287 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 3288 | if (I.getOpcode() == Instruction::SetLE) // A <= MIN -> A == MIN |
| 3289 | return BinaryOperator::createSetEQ(Op0, Op1); |
| 3290 | if (I.getOpcode() == Instruction::SetGT) // A > MIN -> A != MIN |
| 3291 | return BinaryOperator::createSetNE(Op0, Op1); |
| 3292 | |
| 3293 | } else if (CI->isMaxValue()) { |
| 3294 | if (I.getOpcode() == Instruction::SetGT) // A > MAX -> FALSE |
| 3295 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 3296 | if (I.getOpcode() == Instruction::SetLE) // A <= MAX -> TRUE |
| 3297 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 3298 | if (I.getOpcode() == Instruction::SetGE) // A >= MAX -> A == MAX |
| 3299 | return BinaryOperator::createSetEQ(Op0, Op1); |
| 3300 | if (I.getOpcode() == Instruction::SetLT) // A < MAX -> A != MAX |
| 3301 | return BinaryOperator::createSetNE(Op0, Op1); |
| 3302 | |
| 3303 | // Comparing against a value really close to min or max? |
| 3304 | } else if (isMinValuePlusOne(CI)) { |
| 3305 | if (I.getOpcode() == Instruction::SetLT) // A < MIN+1 -> A == MIN |
| 3306 | return BinaryOperator::createSetEQ(Op0, SubOne(CI)); |
| 3307 | if (I.getOpcode() == Instruction::SetGE) // A >= MIN-1 -> A != MIN |
| 3308 | return BinaryOperator::createSetNE(Op0, SubOne(CI)); |
| 3309 | |
| 3310 | } else if (isMaxValueMinusOne(CI)) { |
| 3311 | if (I.getOpcode() == Instruction::SetGT) // A > MAX-1 -> A == MAX |
| 3312 | return BinaryOperator::createSetEQ(Op0, AddOne(CI)); |
| 3313 | if (I.getOpcode() == Instruction::SetLE) // A <= MAX-1 -> A != MAX |
| 3314 | return BinaryOperator::createSetNE(Op0, AddOne(CI)); |
| 3315 | } |
| 3316 | |
| 3317 | // If we still have a setle or setge instruction, turn it into the |
| 3318 | // appropriate setlt or setgt instruction. Since the border cases have |
| 3319 | // already been handled above, this requires little checking. |
| 3320 | // |
| 3321 | if (I.getOpcode() == Instruction::SetLE) |
| 3322 | return BinaryOperator::createSetLT(Op0, AddOne(CI)); |
| 3323 | if (I.getOpcode() == Instruction::SetGE) |
| 3324 | return BinaryOperator::createSetGT(Op0, SubOne(CI)); |
| 3325 | |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 3326 | |
| 3327 | // See if we can fold the comparison based on bits known to be zero or one |
| 3328 | // in the input. |
| 3329 | uint64_t KnownZero, KnownOne; |
| 3330 | if (SimplifyDemandedBits(Op0, Ty->getIntegralTypeMask(), |
| 3331 | KnownZero, KnownOne, 0)) |
| 3332 | return &I; |
| 3333 | |
| 3334 | // Given the known and unknown bits, compute a range that the LHS could be |
| 3335 | // in. |
| 3336 | if (KnownOne | KnownZero) { |
| 3337 | if (Ty->isUnsigned()) { // Unsigned comparison. |
| 3338 | uint64_t Min, Max; |
| 3339 | uint64_t RHSVal = CI->getZExtValue(); |
| 3340 | ComputeUnsignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, |
| 3341 | Min, Max); |
| 3342 | switch (I.getOpcode()) { // LE/GE have been folded already. |
| 3343 | default: assert(0 && "Unknown setcc opcode!"); |
| 3344 | case Instruction::SetEQ: |
| 3345 | if (Max < RHSVal || Min > RHSVal) |
| 3346 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 3347 | break; |
| 3348 | case Instruction::SetNE: |
| 3349 | if (Max < RHSVal || Min > RHSVal) |
| 3350 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 3351 | break; |
| 3352 | case Instruction::SetLT: |
| 3353 | if (Max < RHSVal) return ReplaceInstUsesWith(I, ConstantBool::True); |
| 3354 | if (Min > RHSVal) return ReplaceInstUsesWith(I, ConstantBool::False); |
| 3355 | break; |
| 3356 | case Instruction::SetGT: |
| 3357 | if (Min > RHSVal) return ReplaceInstUsesWith(I, ConstantBool::True); |
| 3358 | if (Max < RHSVal) return ReplaceInstUsesWith(I, ConstantBool::False); |
| 3359 | break; |
| 3360 | } |
| 3361 | } else { // Signed comparison. |
| 3362 | int64_t Min, Max; |
| 3363 | int64_t RHSVal = CI->getSExtValue(); |
| 3364 | ComputeSignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, |
| 3365 | Min, Max); |
| 3366 | switch (I.getOpcode()) { // LE/GE have been folded already. |
| 3367 | default: assert(0 && "Unknown setcc opcode!"); |
| 3368 | case Instruction::SetEQ: |
| 3369 | if (Max < RHSVal || Min > RHSVal) |
| 3370 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 3371 | break; |
| 3372 | case Instruction::SetNE: |
| 3373 | if (Max < RHSVal || Min > RHSVal) |
| 3374 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 3375 | break; |
| 3376 | case Instruction::SetLT: |
| 3377 | if (Max < RHSVal) return ReplaceInstUsesWith(I, ConstantBool::True); |
| 3378 | if (Min > RHSVal) return ReplaceInstUsesWith(I, ConstantBool::False); |
| 3379 | break; |
| 3380 | case Instruction::SetGT: |
| 3381 | if (Min > RHSVal) return ReplaceInstUsesWith(I, ConstantBool::True); |
| 3382 | if (Max < RHSVal) return ReplaceInstUsesWith(I, ConstantBool::False); |
| 3383 | break; |
| 3384 | } |
| 3385 | } |
| 3386 | } |
| 3387 | |
| 3388 | |
Chris Lattner | e1e10e1 | 2004-05-25 06:32:08 +0000 | [diff] [blame] | 3389 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 3390 | switch (LHSI->getOpcode()) { |
| 3391 | case Instruction::And: |
| 3392 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && |
| 3393 | LHSI->getOperand(0)->hasOneUse()) { |
| 3394 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare |
| 3395 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This |
| 3396 | // happens a LOT in code produced by the C front-end, for bitfield |
| 3397 | // access. |
| 3398 | ShiftInst *Shift = dyn_cast<ShiftInst>(LHSI->getOperand(0)); |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 3399 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); |
| 3400 | |
| 3401 | // Check to see if there is a noop-cast between the shift and the and. |
| 3402 | if (!Shift) { |
| 3403 | if (CastInst *CI = dyn_cast<CastInst>(LHSI->getOperand(0))) |
| 3404 | if (CI->getOperand(0)->getType()->isIntegral() && |
| 3405 | CI->getOperand(0)->getType()->getPrimitiveSizeInBits() == |
| 3406 | CI->getType()->getPrimitiveSizeInBits()) |
| 3407 | Shift = dyn_cast<ShiftInst>(CI->getOperand(0)); |
| 3408 | } |
| 3409 | |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 3410 | ConstantUInt *ShAmt; |
| 3411 | ShAmt = Shift ? dyn_cast<ConstantUInt>(Shift->getOperand(1)) : 0; |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 3412 | const Type *Ty = Shift ? Shift->getType() : 0; // Type of the shift. |
| 3413 | const Type *AndTy = AndCST->getType(); // Type of the and. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3414 | |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 3415 | // We can fold this as long as we can't shift unknown bits |
| 3416 | // into the mask. This can only happen with signed shift |
| 3417 | // rights, as they sign-extend. |
| 3418 | if (ShAmt) { |
| 3419 | bool CanFold = Shift->getOpcode() != Instruction::Shr || |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 3420 | Ty->isUnsigned(); |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 3421 | if (!CanFold) { |
| 3422 | // To test for the bad case of the signed shr, see if any |
| 3423 | // of the bits shifted in could be tested after the mask. |
Chris Lattner | c53cb9d | 2005-06-17 01:29:28 +0000 | [diff] [blame] | 3424 | int ShAmtVal = Ty->getPrimitiveSizeInBits()-ShAmt->getValue(); |
| 3425 | if (ShAmtVal < 0) ShAmtVal = 0; // Out of range shift. |
| 3426 | |
| 3427 | Constant *OShAmt = ConstantUInt::get(Type::UByteTy, ShAmtVal); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3428 | Constant *ShVal = |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 3429 | ConstantExpr::getShl(ConstantInt::getAllOnesValue(AndTy), |
| 3430 | OShAmt); |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 3431 | if (ConstantExpr::getAnd(ShVal, AndCST)->isNullValue()) |
| 3432 | CanFold = true; |
| 3433 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3434 | |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 3435 | if (CanFold) { |
Chris Lattner | 6afc02f | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 3436 | Constant *NewCst; |
| 3437 | if (Shift->getOpcode() == Instruction::Shl) |
| 3438 | NewCst = ConstantExpr::getUShr(CI, ShAmt); |
| 3439 | else |
| 3440 | NewCst = ConstantExpr::getShl(CI, ShAmt); |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 3441 | |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 3442 | // Check to see if we are shifting out any of the bits being |
| 3443 | // compared. |
| 3444 | if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != CI){ |
| 3445 | // If we shifted bits out, the fold is not going to work out. |
| 3446 | // As a special case, check to see if this means that the |
| 3447 | // result is always true or false now. |
| 3448 | if (I.getOpcode() == Instruction::SetEQ) |
| 3449 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 3450 | if (I.getOpcode() == Instruction::SetNE) |
| 3451 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 3452 | } else { |
| 3453 | I.setOperand(1, NewCst); |
Chris Lattner | 6afc02f | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 3454 | Constant *NewAndCST; |
| 3455 | if (Shift->getOpcode() == Instruction::Shl) |
| 3456 | NewAndCST = ConstantExpr::getUShr(AndCST, ShAmt); |
| 3457 | else |
| 3458 | NewAndCST = ConstantExpr::getShl(AndCST, ShAmt); |
| 3459 | LHSI->setOperand(1, NewAndCST); |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 3460 | if (AndTy == Ty) |
| 3461 | LHSI->setOperand(0, Shift->getOperand(0)); |
| 3462 | else { |
| 3463 | Value *NewCast = InsertCastBefore(Shift->getOperand(0), AndTy, |
| 3464 | *Shift); |
| 3465 | LHSI->setOperand(0, NewCast); |
| 3466 | } |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 3467 | WorkList.push_back(Shift); // Shift is dead. |
| 3468 | AddUsesToWorkList(I); |
| 3469 | return &I; |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 3470 | } |
| 3471 | } |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3472 | } |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 3473 | } |
| 3474 | break; |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 3475 | |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 3476 | case Instruction::Shl: // (setcc (shl X, ShAmt), CI) |
| 3477 | if (ConstantUInt *ShAmt = dyn_cast<ConstantUInt>(LHSI->getOperand(1))) { |
| 3478 | switch (I.getOpcode()) { |
| 3479 | default: break; |
| 3480 | case Instruction::SetEQ: |
| 3481 | case Instruction::SetNE: { |
Chris Lattner | 19b57f5 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 3482 | unsigned TypeBits = CI->getType()->getPrimitiveSizeInBits(); |
| 3483 | |
| 3484 | // Check that the shift amount is in range. If not, don't perform |
| 3485 | // undefined shifts. When the shift is visited it will be |
| 3486 | // simplified. |
| 3487 | if (ShAmt->getValue() >= TypeBits) |
| 3488 | break; |
| 3489 | |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 3490 | // If we are comparing against bits always shifted out, the |
| 3491 | // comparison cannot succeed. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3492 | Constant *Comp = |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 3493 | ConstantExpr::getShl(ConstantExpr::getShr(CI, ShAmt), ShAmt); |
| 3494 | if (Comp != CI) {// Comparing against a bit that we know is zero. |
| 3495 | bool IsSetNE = I.getOpcode() == Instruction::SetNE; |
| 3496 | Constant *Cst = ConstantBool::get(IsSetNE); |
| 3497 | return ReplaceInstUsesWith(I, Cst); |
| 3498 | } |
| 3499 | |
| 3500 | if (LHSI->hasOneUse()) { |
| 3501 | // Otherwise strength reduce the shift into an and. |
Chris Lattner | fdfe3e49 | 2005-01-08 19:42:22 +0000 | [diff] [blame] | 3502 | unsigned ShAmtVal = (unsigned)ShAmt->getValue(); |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 3503 | uint64_t Val = (1ULL << (TypeBits-ShAmtVal))-1; |
| 3504 | |
| 3505 | Constant *Mask; |
| 3506 | if (CI->getType()->isUnsigned()) { |
| 3507 | Mask = ConstantUInt::get(CI->getType(), Val); |
| 3508 | } else if (ShAmtVal != 0) { |
| 3509 | Mask = ConstantSInt::get(CI->getType(), Val); |
| 3510 | } else { |
| 3511 | Mask = ConstantInt::getAllOnesValue(CI->getType()); |
| 3512 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3513 | |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 3514 | Instruction *AndI = |
| 3515 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 3516 | Mask, LHSI->getName()+".mask"); |
| 3517 | Value *And = InsertNewInstBefore(AndI, I); |
| 3518 | return new SetCondInst(I.getOpcode(), And, |
| 3519 | ConstantExpr::getUShr(CI, ShAmt)); |
| 3520 | } |
| 3521 | } |
| 3522 | } |
| 3523 | } |
| 3524 | break; |
| 3525 | |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 3526 | case Instruction::Shr: // (setcc (shr X, ShAmt), CI) |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 3527 | if (ConstantUInt *ShAmt = dyn_cast<ConstantUInt>(LHSI->getOperand(1))) { |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 3528 | switch (I.getOpcode()) { |
| 3529 | default: break; |
| 3530 | case Instruction::SetEQ: |
| 3531 | case Instruction::SetNE: { |
Chris Lattner | 19b57f5 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 3532 | |
| 3533 | // Check that the shift amount is in range. If not, don't perform |
| 3534 | // undefined shifts. When the shift is visited it will be |
| 3535 | // simplified. |
Chris Lattner | 104002b | 2005-06-16 01:52:07 +0000 | [diff] [blame] | 3536 | unsigned TypeBits = CI->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 19b57f5 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 3537 | if (ShAmt->getValue() >= TypeBits) |
| 3538 | break; |
| 3539 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 3540 | // If we are comparing against bits always shifted out, the |
| 3541 | // comparison cannot succeed. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3542 | Constant *Comp = |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 3543 | ConstantExpr::getShr(ConstantExpr::getShl(CI, ShAmt), ShAmt); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3544 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 3545 | if (Comp != CI) {// Comparing against a bit that we know is zero. |
| 3546 | bool IsSetNE = I.getOpcode() == Instruction::SetNE; |
| 3547 | Constant *Cst = ConstantBool::get(IsSetNE); |
| 3548 | return ReplaceInstUsesWith(I, Cst); |
| 3549 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3550 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 3551 | if (LHSI->hasOneUse() || CI->isNullValue()) { |
Chris Lattner | fdfe3e49 | 2005-01-08 19:42:22 +0000 | [diff] [blame] | 3552 | unsigned ShAmtVal = (unsigned)ShAmt->getValue(); |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 3553 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 3554 | // Otherwise strength reduce the shift into an and. |
| 3555 | uint64_t Val = ~0ULL; // All ones. |
| 3556 | Val <<= ShAmtVal; // Shift over to the right spot. |
| 3557 | |
| 3558 | Constant *Mask; |
| 3559 | if (CI->getType()->isUnsigned()) { |
Chris Lattner | 2f1457f | 2005-04-24 17:46:05 +0000 | [diff] [blame] | 3560 | Val &= ~0ULL >> (64-TypeBits); |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 3561 | Mask = ConstantUInt::get(CI->getType(), Val); |
| 3562 | } else { |
| 3563 | Mask = ConstantSInt::get(CI->getType(), Val); |
| 3564 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3565 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 3566 | Instruction *AndI = |
| 3567 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 3568 | Mask, LHSI->getName()+".mask"); |
| 3569 | Value *And = InsertNewInstBefore(AndI, I); |
| 3570 | return new SetCondInst(I.getOpcode(), And, |
| 3571 | ConstantExpr::getShl(CI, ShAmt)); |
| 3572 | } |
| 3573 | break; |
| 3574 | } |
| 3575 | } |
| 3576 | } |
| 3577 | break; |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3578 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3579 | case Instruction::Div: |
| 3580 | // Fold: (div X, C1) op C2 -> range check |
| 3581 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
| 3582 | // Fold this div into the comparison, producing a range check. |
| 3583 | // Determine, based on the divide type, what the range is being |
| 3584 | // checked. If there is an overflow on the low or high side, remember |
| 3585 | // it, otherwise compute the range [low, hi) bounding the new value. |
| 3586 | bool LoOverflow = false, HiOverflow = 0; |
| 3587 | ConstantInt *LoBound = 0, *HiBound = 0; |
| 3588 | |
| 3589 | ConstantInt *Prod; |
| 3590 | bool ProdOV = MulWithOverflow(Prod, CI, DivRHS); |
| 3591 | |
Chris Lattner | a92af96 | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 3592 | Instruction::BinaryOps Opcode = I.getOpcode(); |
| 3593 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3594 | if (DivRHS->isNullValue()) { // Don't hack on divide by zeros. |
| 3595 | } else if (LHSI->getType()->isUnsigned()) { // udiv |
| 3596 | LoBound = Prod; |
| 3597 | LoOverflow = ProdOV; |
| 3598 | HiOverflow = ProdOV || AddWithOverflow(HiBound, LoBound, DivRHS); |
| 3599 | } else if (isPositive(DivRHS)) { // Divisor is > 0. |
| 3600 | if (CI->isNullValue()) { // (X / pos) op 0 |
| 3601 | // Can't overflow. |
| 3602 | LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS))); |
| 3603 | HiBound = DivRHS; |
| 3604 | } else if (isPositive(CI)) { // (X / pos) op pos |
| 3605 | LoBound = Prod; |
| 3606 | LoOverflow = ProdOV; |
| 3607 | HiOverflow = ProdOV || AddWithOverflow(HiBound, Prod, DivRHS); |
| 3608 | } else { // (X / pos) op neg |
| 3609 | Constant *DivRHSH = ConstantExpr::getNeg(SubOne(DivRHS)); |
| 3610 | LoOverflow = AddWithOverflow(LoBound, Prod, |
| 3611 | cast<ConstantInt>(DivRHSH)); |
| 3612 | HiBound = Prod; |
| 3613 | HiOverflow = ProdOV; |
| 3614 | } |
| 3615 | } else { // Divisor is < 0. |
| 3616 | if (CI->isNullValue()) { // (X / neg) op 0 |
| 3617 | LoBound = AddOne(DivRHS); |
| 3618 | HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS)); |
Chris Lattner | 73bcba5 | 2005-06-17 02:05:55 +0000 | [diff] [blame] | 3619 | if (HiBound == DivRHS) |
| 3620 | LoBound = 0; // - INTMIN = INTMIN |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3621 | } else if (isPositive(CI)) { // (X / neg) op pos |
| 3622 | HiOverflow = LoOverflow = ProdOV; |
| 3623 | if (!LoOverflow) |
| 3624 | LoOverflow = AddWithOverflow(LoBound, Prod, AddOne(DivRHS)); |
| 3625 | HiBound = AddOne(Prod); |
| 3626 | } else { // (X / neg) op neg |
| 3627 | LoBound = Prod; |
| 3628 | LoOverflow = HiOverflow = ProdOV; |
| 3629 | HiBound = cast<ConstantInt>(ConstantExpr::getSub(Prod, DivRHS)); |
| 3630 | } |
Chris Lattner | 0b41e86 | 2004-10-08 19:15:44 +0000 | [diff] [blame] | 3631 | |
Chris Lattner | a92af96 | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 3632 | // Dividing by a negate swaps the condition. |
| 3633 | Opcode = SetCondInst::getSwappedCondition(Opcode); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3634 | } |
| 3635 | |
| 3636 | if (LoBound) { |
| 3637 | Value *X = LHSI->getOperand(0); |
Chris Lattner | a92af96 | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 3638 | switch (Opcode) { |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3639 | default: assert(0 && "Unhandled setcc opcode!"); |
| 3640 | case Instruction::SetEQ: |
| 3641 | if (LoOverflow && HiOverflow) |
| 3642 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 3643 | else if (HiOverflow) |
| 3644 | return new SetCondInst(Instruction::SetGE, X, LoBound); |
| 3645 | else if (LoOverflow) |
| 3646 | return new SetCondInst(Instruction::SetLT, X, HiBound); |
| 3647 | else |
| 3648 | return InsertRangeTest(X, LoBound, HiBound, true, I); |
| 3649 | case Instruction::SetNE: |
| 3650 | if (LoOverflow && HiOverflow) |
| 3651 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 3652 | else if (HiOverflow) |
| 3653 | return new SetCondInst(Instruction::SetLT, X, LoBound); |
| 3654 | else if (LoOverflow) |
| 3655 | return new SetCondInst(Instruction::SetGE, X, HiBound); |
| 3656 | else |
| 3657 | return InsertRangeTest(X, LoBound, HiBound, false, I); |
| 3658 | case Instruction::SetLT: |
| 3659 | if (LoOverflow) |
| 3660 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 3661 | return new SetCondInst(Instruction::SetLT, X, LoBound); |
| 3662 | case Instruction::SetGT: |
| 3663 | if (HiOverflow) |
| 3664 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 3665 | return new SetCondInst(Instruction::SetGE, X, HiBound); |
| 3666 | } |
| 3667 | } |
| 3668 | } |
| 3669 | break; |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 3670 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3671 | |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 3672 | // Simplify seteq and setne instructions... |
| 3673 | if (I.getOpcode() == Instruction::SetEQ || |
| 3674 | I.getOpcode() == Instruction::SetNE) { |
| 3675 | bool isSetNE = I.getOpcode() == Instruction::SetNE; |
| 3676 | |
Chris Lattner | cfbce7c | 2003-07-23 17:26:36 +0000 | [diff] [blame] | 3677 | // 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] | 3678 | // operand is a constant, simplify a bit. |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3679 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) { |
| 3680 | switch (BO->getOpcode()) { |
Chris Lattner | 23b47b6 | 2004-07-06 07:38:18 +0000 | [diff] [blame] | 3681 | case Instruction::Rem: |
| 3682 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. |
| 3683 | if (CI->isNullValue() && isa<ConstantSInt>(BO->getOperand(1)) && |
| 3684 | BO->hasOneUse() && |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 3685 | cast<ConstantSInt>(BO->getOperand(1))->getValue() > 1) { |
| 3686 | int64_t V = cast<ConstantSInt>(BO->getOperand(1))->getValue(); |
| 3687 | if (isPowerOf2_64(V)) { |
| 3688 | unsigned L2 = Log2_64(V); |
Chris Lattner | 23b47b6 | 2004-07-06 07:38:18 +0000 | [diff] [blame] | 3689 | const Type *UTy = BO->getType()->getUnsignedVersion(); |
| 3690 | Value *NewX = InsertNewInstBefore(new CastInst(BO->getOperand(0), |
| 3691 | UTy, "tmp"), I); |
| 3692 | Constant *RHSCst = ConstantUInt::get(UTy, 1ULL << L2); |
| 3693 | Value *NewRem =InsertNewInstBefore(BinaryOperator::createRem(NewX, |
| 3694 | RHSCst, BO->getName()), I); |
| 3695 | return BinaryOperator::create(I.getOpcode(), NewRem, |
| 3696 | Constant::getNullValue(UTy)); |
| 3697 | } |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 3698 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3699 | break; |
Chris Lattner | 23b47b6 | 2004-07-06 07:38:18 +0000 | [diff] [blame] | 3700 | |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3701 | case Instruction::Add: |
Chris Lattner | 6e07936 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 3702 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. |
| 3703 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | b121ae1 | 2004-09-21 21:35:23 +0000 | [diff] [blame] | 3704 | if (BO->hasOneUse()) |
| 3705 | return new SetCondInst(I.getOpcode(), BO->getOperand(0), |
| 3706 | ConstantExpr::getSub(CI, BOp1C)); |
Chris Lattner | 6e07936 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 3707 | } else if (CI->isNullValue()) { |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3708 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 3709 | // efficiently invertible, or if the add has just this one use. |
| 3710 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3711 | |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3712 | if (Value *NegVal = dyn_castNegVal(BOp1)) |
| 3713 | return new SetCondInst(I.getOpcode(), BOp0, NegVal); |
| 3714 | else if (Value *NegVal = dyn_castNegVal(BOp0)) |
| 3715 | return new SetCondInst(I.getOpcode(), NegVal, BOp1); |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 3716 | else if (BO->hasOneUse()) { |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3717 | Instruction *Neg = BinaryOperator::createNeg(BOp1, BO->getName()); |
| 3718 | BO->setName(""); |
| 3719 | InsertNewInstBefore(Neg, I); |
| 3720 | return new SetCondInst(I.getOpcode(), BOp0, Neg); |
| 3721 | } |
| 3722 | } |
| 3723 | break; |
| 3724 | case Instruction::Xor: |
| 3725 | // For the xor case, we can xor two constants together, eliminating |
| 3726 | // the explicit xor. |
| 3727 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) |
| 3728 | return BinaryOperator::create(I.getOpcode(), BO->getOperand(0), |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3729 | ConstantExpr::getXor(CI, BOC)); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3730 | |
| 3731 | // FALLTHROUGH |
| 3732 | case Instruction::Sub: |
| 3733 | // Replace (([sub|xor] A, B) != 0) with (A != B) |
| 3734 | if (CI->isNullValue()) |
| 3735 | return new SetCondInst(I.getOpcode(), BO->getOperand(0), |
| 3736 | BO->getOperand(1)); |
| 3737 | break; |
| 3738 | |
| 3739 | case Instruction::Or: |
| 3740 | // If bits are being or'd in that are not present in the constant we |
| 3741 | // are comparing against, then the comparison could never succeed! |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3742 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 3743 | Constant *NotCI = ConstantExpr::getNot(CI); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3744 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 3745 | return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE)); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3746 | } |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3747 | break; |
| 3748 | |
| 3749 | case Instruction::And: |
| 3750 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 3751 | // If bits are being compared against that are and'd out, then the |
| 3752 | // comparison can never succeed! |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 3753 | if (!ConstantExpr::getAnd(CI, |
| 3754 | ConstantExpr::getNot(BOC))->isNullValue()) |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 3755 | return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE)); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3756 | |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3757 | // If we have ((X & C) == C), turn it into ((X & C) != 0). |
Chris Lattner | ee59d4b | 2004-06-10 02:33:20 +0000 | [diff] [blame] | 3758 | if (CI == BOC && isOneBitSet(CI)) |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3759 | return new SetCondInst(isSetNE ? Instruction::SetEQ : |
| 3760 | Instruction::SetNE, Op0, |
| 3761 | Constant::getNullValue(CI->getType())); |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3762 | |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3763 | // Replace (and X, (1 << size(X)-1) != 0) with x < 0, converting X |
| 3764 | // to be a signed value as appropriate. |
| 3765 | if (isSignBit(BOC)) { |
| 3766 | Value *X = BO->getOperand(0); |
| 3767 | // If 'X' is not signed, insert a cast now... |
| 3768 | if (!BOC->getType()->isSigned()) { |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 3769 | const Type *DestTy = BOC->getType()->getSignedVersion(); |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 3770 | X = InsertCastBefore(X, DestTy, I); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3771 | } |
| 3772 | return new SetCondInst(isSetNE ? Instruction::SetLT : |
| 3773 | Instruction::SetGE, X, |
| 3774 | Constant::getNullValue(X->getType())); |
| 3775 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3776 | |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 3777 | // ((X & ~7) == 0) --> X < 8 |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3778 | if (CI->isNullValue() && isHighOnes(BOC)) { |
| 3779 | Value *X = BO->getOperand(0); |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 3780 | Constant *NegX = ConstantExpr::getNeg(BOC); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3781 | |
| 3782 | // If 'X' is signed, insert a cast now. |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 3783 | if (NegX->getType()->isSigned()) { |
| 3784 | const Type *DestTy = NegX->getType()->getUnsignedVersion(); |
| 3785 | X = InsertCastBefore(X, DestTy, I); |
| 3786 | NegX = ConstantExpr::getCast(NegX, DestTy); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3787 | } |
| 3788 | |
| 3789 | return new SetCondInst(isSetNE ? Instruction::SetGE : |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 3790 | Instruction::SetLT, X, NegX); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3791 | } |
| 3792 | |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 3793 | } |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 3794 | default: break; |
| 3795 | } |
| 3796 | } |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3797 | } else { // Not a SetEQ/SetNE |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3798 | // 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] | 3799 | if (CastInst *Cast = dyn_cast<CastInst>(Op0)) { |
| 3800 | Value *CastOp = Cast->getOperand(0); |
| 3801 | const Type *SrcTy = CastOp->getType(); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3802 | unsigned SrcTySize = SrcTy->getPrimitiveSizeInBits(); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3803 | if (SrcTy != Cast->getType() && SrcTy->isInteger() && |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3804 | SrcTySize == Cast->getType()->getPrimitiveSizeInBits()) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3805 | assert((SrcTy->isSigned() ^ Cast->getType()->isSigned()) && |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3806 | "Source and destination signednesses should differ!"); |
| 3807 | if (Cast->getType()->isSigned()) { |
| 3808 | // If this is a signed comparison, check for comparisons in the |
| 3809 | // vicinity of zero. |
| 3810 | if (I.getOpcode() == Instruction::SetLT && CI->isNullValue()) |
| 3811 | // X < 0 => x > 127 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3812 | return BinaryOperator::createSetGT(CastOp, |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3813 | ConstantUInt::get(SrcTy, (1ULL << (SrcTySize-1))-1)); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3814 | else if (I.getOpcode() == Instruction::SetGT && |
| 3815 | cast<ConstantSInt>(CI)->getValue() == -1) |
| 3816 | // X > -1 => x < 128 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3817 | return BinaryOperator::createSetLT(CastOp, |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3818 | ConstantUInt::get(SrcTy, 1ULL << (SrcTySize-1))); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3819 | } else { |
| 3820 | ConstantUInt *CUI = cast<ConstantUInt>(CI); |
| 3821 | if (I.getOpcode() == Instruction::SetLT && |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3822 | CUI->getValue() == 1ULL << (SrcTySize-1)) |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3823 | // X < 128 => X > -1 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3824 | return BinaryOperator::createSetGT(CastOp, |
| 3825 | ConstantSInt::get(SrcTy, -1)); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3826 | else if (I.getOpcode() == Instruction::SetGT && |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3827 | CUI->getValue() == (1ULL << (SrcTySize-1))-1) |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3828 | // X > 127 => X < 0 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3829 | return BinaryOperator::createSetLT(CastOp, |
| 3830 | Constant::getNullValue(SrcTy)); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 3831 | } |
| 3832 | } |
| 3833 | } |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 3834 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3835 | } |
| 3836 | |
Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 3837 | // Handle setcc with constant RHS's that can be integer, FP or pointer. |
| 3838 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 3839 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 3840 | switch (LHSI->getOpcode()) { |
Chris Lattner | a816eee | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 3841 | case Instruction::GetElementPtr: |
| 3842 | if (RHSC->isNullValue()) { |
| 3843 | // Transform setcc GEP P, int 0, int 0, int 0, null -> setcc P, null |
| 3844 | bool isAllZeros = true; |
| 3845 | for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i) |
| 3846 | if (!isa<Constant>(LHSI->getOperand(i)) || |
| 3847 | !cast<Constant>(LHSI->getOperand(i))->isNullValue()) { |
| 3848 | isAllZeros = false; |
| 3849 | break; |
| 3850 | } |
| 3851 | if (isAllZeros) |
| 3852 | return new SetCondInst(I.getOpcode(), LHSI->getOperand(0), |
| 3853 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
| 3854 | } |
| 3855 | break; |
| 3856 | |
Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 3857 | case Instruction::PHI: |
| 3858 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3859 | return NV; |
| 3860 | break; |
| 3861 | case Instruction::Select: |
| 3862 | // If either operand of the select is a constant, we can fold the |
| 3863 | // comparison into the select arms, which will cause one to be |
| 3864 | // constant folded and the select turned into a bitwise or. |
| 3865 | Value *Op1 = 0, *Op2 = 0; |
| 3866 | if (LHSI->hasOneUse()) { |
| 3867 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 3868 | // Fold the known value into the constant operand. |
| 3869 | Op1 = ConstantExpr::get(I.getOpcode(), C, RHSC); |
| 3870 | // Insert a new SetCC of the other select operand. |
| 3871 | Op2 = InsertNewInstBefore(new SetCondInst(I.getOpcode(), |
| 3872 | LHSI->getOperand(2), RHSC, |
| 3873 | I.getName()), I); |
| 3874 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 3875 | // Fold the known value into the constant operand. |
| 3876 | Op2 = ConstantExpr::get(I.getOpcode(), C, RHSC); |
| 3877 | // Insert a new SetCC of the other select operand. |
| 3878 | Op1 = InsertNewInstBefore(new SetCondInst(I.getOpcode(), |
| 3879 | LHSI->getOperand(1), RHSC, |
| 3880 | I.getName()), I); |
| 3881 | } |
| 3882 | } |
Jeff Cohen | 8263985 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 3883 | |
Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 3884 | if (Op1) |
| 3885 | return new SelectInst(LHSI->getOperand(0), Op1, Op2); |
| 3886 | break; |
| 3887 | } |
| 3888 | } |
| 3889 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3890 | // If we can optimize a 'setcc GEP, P' or 'setcc P, GEP', do so now. |
| 3891 | if (User *GEP = dyn_castGetElementPtr(Op0)) |
| 3892 | if (Instruction *NI = FoldGEPSetCC(GEP, Op1, I.getOpcode(), I)) |
| 3893 | return NI; |
| 3894 | if (User *GEP = dyn_castGetElementPtr(Op1)) |
| 3895 | if (Instruction *NI = FoldGEPSetCC(GEP, Op0, |
| 3896 | SetCondInst::getSwappedCondition(I.getOpcode()), I)) |
| 3897 | return NI; |
| 3898 | |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 3899 | // Test to see if the operands of the setcc are casted versions of other |
| 3900 | // 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] | 3901 | if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
| 3902 | Value *CastOp0 = CI->getOperand(0); |
| 3903 | if (CastOp0->getType()->isLosslesslyConvertibleTo(CI->getType()) && |
Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 3904 | (isa<Constant>(Op1) || isa<CastInst>(Op1)) && |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 3905 | (I.getOpcode() == Instruction::SetEQ || |
| 3906 | I.getOpcode() == Instruction::SetNE)) { |
| 3907 | // We keep moving the cast from the left operand over to the right |
| 3908 | // operand, where it can often be eliminated completely. |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 3909 | Op0 = CastOp0; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3910 | |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 3911 | // If operand #1 is a cast instruction, see if we can eliminate it as |
| 3912 | // well. |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 3913 | if (CastInst *CI2 = dyn_cast<CastInst>(Op1)) |
| 3914 | if (CI2->getOperand(0)->getType()->isLosslesslyConvertibleTo( |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 3915 | Op0->getType())) |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 3916 | Op1 = CI2->getOperand(0); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3917 | |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 3918 | // If Op1 is a constant, we can fold the cast into the constant. |
| 3919 | if (Op1->getType() != Op0->getType()) |
| 3920 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
| 3921 | Op1 = ConstantExpr::getCast(Op1C, Op0->getType()); |
| 3922 | } else { |
| 3923 | // Otherwise, cast the RHS right before the setcc |
| 3924 | Op1 = new CastInst(Op1, Op0->getType(), Op1->getName()); |
| 3925 | InsertNewInstBefore(cast<Instruction>(Op1), I); |
| 3926 | } |
| 3927 | return BinaryOperator::create(I.getOpcode(), Op0, Op1); |
| 3928 | } |
| 3929 | |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 3930 | // Handle the special case of: setcc (cast bool to X), <cst> |
| 3931 | // This comes up when you have code like |
| 3932 | // int X = A < B; |
| 3933 | // if (X) ... |
| 3934 | // For generality, we handle any zero-extension of any operand comparison |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3935 | // with a constant or another cast from the same type. |
| 3936 | if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1)) |
| 3937 | if (Instruction *R = visitSetCondInstWithCastAndCast(I)) |
| 3938 | return R; |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 3939 | } |
Chris Lattner | f5c8a0b | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 3940 | |
| 3941 | if (I.getOpcode() == Instruction::SetNE || |
| 3942 | I.getOpcode() == Instruction::SetEQ) { |
| 3943 | Value *A, *B; |
| 3944 | if (match(Op0, m_Xor(m_Value(A), m_Value(B))) && |
| 3945 | (A == Op1 || B == Op1)) { |
| 3946 | // (A^B) == A -> B == 0 |
| 3947 | Value *OtherVal = A == Op1 ? B : A; |
| 3948 | return BinaryOperator::create(I.getOpcode(), OtherVal, |
| 3949 | Constant::getNullValue(A->getType())); |
| 3950 | } else if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && |
| 3951 | (A == Op0 || B == Op0)) { |
| 3952 | // A == (A^B) -> B == 0 |
| 3953 | Value *OtherVal = A == Op0 ? B : A; |
| 3954 | return BinaryOperator::create(I.getOpcode(), OtherVal, |
| 3955 | Constant::getNullValue(A->getType())); |
| 3956 | } else if (match(Op0, m_Sub(m_Value(A), m_Value(B))) && A == Op1) { |
| 3957 | // (A-B) == A -> B == 0 |
| 3958 | return BinaryOperator::create(I.getOpcode(), B, |
| 3959 | Constant::getNullValue(B->getType())); |
| 3960 | } else if (match(Op1, m_Sub(m_Value(A), m_Value(B))) && A == Op0) { |
| 3961 | // A == (A-B) -> B == 0 |
| 3962 | return BinaryOperator::create(I.getOpcode(), B, |
| 3963 | Constant::getNullValue(B->getType())); |
| 3964 | } |
| 3965 | } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3966 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3967 | } |
| 3968 | |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3969 | // visitSetCondInstWithCastAndCast - Handle setcond (cast x to y), (cast/cst). |
| 3970 | // We only handle extending casts so far. |
| 3971 | // |
| 3972 | Instruction *InstCombiner::visitSetCondInstWithCastAndCast(SetCondInst &SCI) { |
| 3973 | Value *LHSCIOp = cast<CastInst>(SCI.getOperand(0))->getOperand(0); |
| 3974 | const Type *SrcTy = LHSCIOp->getType(); |
| 3975 | const Type *DestTy = SCI.getOperand(0)->getType(); |
| 3976 | Value *RHSCIOp; |
| 3977 | |
| 3978 | if (!DestTy->isIntegral() || !SrcTy->isIntegral()) |
Chris Lattner | 03f06f1 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 3979 | return 0; |
| 3980 | |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3981 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); |
| 3982 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); |
| 3983 | if (SrcBits >= DestBits) return 0; // Only handle extending cast. |
| 3984 | |
| 3985 | // Is this a sign or zero extension? |
| 3986 | bool isSignSrc = SrcTy->isSigned(); |
| 3987 | bool isSignDest = DestTy->isSigned(); |
| 3988 | |
| 3989 | if (CastInst *CI = dyn_cast<CastInst>(SCI.getOperand(1))) { |
| 3990 | // Not an extension from the same type? |
| 3991 | RHSCIOp = CI->getOperand(0); |
| 3992 | if (RHSCIOp->getType() != LHSCIOp->getType()) return 0; |
| 3993 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(SCI.getOperand(1))) { |
| 3994 | // Compute the constant that would happen if we truncated to SrcTy then |
| 3995 | // reextended to DestTy. |
| 3996 | Constant *Res = ConstantExpr::getCast(CI, SrcTy); |
| 3997 | |
| 3998 | if (ConstantExpr::getCast(Res, DestTy) == CI) { |
| 3999 | RHSCIOp = Res; |
| 4000 | } else { |
| 4001 | // If the value cannot be represented in the shorter type, we cannot emit |
| 4002 | // a simple comparison. |
| 4003 | if (SCI.getOpcode() == Instruction::SetEQ) |
| 4004 | return ReplaceInstUsesWith(SCI, ConstantBool::False); |
| 4005 | if (SCI.getOpcode() == Instruction::SetNE) |
| 4006 | return ReplaceInstUsesWith(SCI, ConstantBool::True); |
| 4007 | |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4008 | // Evaluate the comparison for LT. |
| 4009 | Value *Result; |
| 4010 | if (DestTy->isSigned()) { |
| 4011 | // We're performing a signed comparison. |
| 4012 | if (isSignSrc) { |
| 4013 | // Signed extend and signed comparison. |
| 4014 | if (cast<ConstantSInt>(CI)->getValue() < 0) // X < (small) --> false |
| 4015 | Result = ConstantBool::False; |
| 4016 | else |
| 4017 | Result = ConstantBool::True; // X < (large) --> true |
| 4018 | } else { |
| 4019 | // Unsigned extend and signed comparison. |
| 4020 | if (cast<ConstantSInt>(CI)->getValue() < 0) |
| 4021 | Result = ConstantBool::False; |
| 4022 | else |
| 4023 | Result = ConstantBool::True; |
| 4024 | } |
| 4025 | } else { |
| 4026 | // We're performing an unsigned comparison. |
| 4027 | if (!isSignSrc) { |
| 4028 | // Unsigned extend & compare -> always true. |
| 4029 | Result = ConstantBool::True; |
| 4030 | } else { |
| 4031 | // We're performing an unsigned comp with a sign extended value. |
| 4032 | // This is true if the input is >= 0. [aka >s -1] |
| 4033 | Constant *NegOne = ConstantIntegral::getAllOnesValue(SrcTy); |
| 4034 | Result = InsertNewInstBefore(BinaryOperator::createSetGT(LHSCIOp, |
| 4035 | NegOne, SCI.getName()), SCI); |
| 4036 | } |
Reid Spencer | 279fa25 | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 4037 | } |
Chris Lattner | 03f06f1 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 4038 | |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4039 | // Finally, return the value computed. |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4040 | if (SCI.getOpcode() == Instruction::SetLT) { |
| 4041 | return ReplaceInstUsesWith(SCI, Result); |
| 4042 | } else { |
| 4043 | assert(SCI.getOpcode()==Instruction::SetGT &&"SetCC should be folded!"); |
| 4044 | if (Constant *CI = dyn_cast<Constant>(Result)) |
| 4045 | return ReplaceInstUsesWith(SCI, ConstantExpr::getNot(CI)); |
| 4046 | else |
| 4047 | return BinaryOperator::createNot(Result); |
| 4048 | } |
Chris Lattner | 03f06f1 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 4049 | } |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4050 | } else { |
| 4051 | return 0; |
Reid Spencer | 279fa25 | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 4052 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4053 | |
Chris Lattner | 252a845 | 2005-06-16 03:00:08 +0000 | [diff] [blame] | 4054 | // Okay, just insert a compare of the reduced operands now! |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4055 | return BinaryOperator::create(SCI.getOpcode(), LHSCIOp, RHSCIOp); |
| 4056 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4057 | |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 4058 | Instruction *InstCombiner::visitShiftInst(ShiftInst &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4059 | assert(I.getOperand(1)->getType() == Type::UByteTy); |
| 4060 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 4061 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4062 | |
| 4063 | // shl X, 0 == X and shr X, 0 == X |
| 4064 | // shl 0, X == 0 and shr 0, X == 0 |
| 4065 | if (Op1 == Constant::getNullValue(Type::UByteTy) || |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 4066 | Op0 == Constant::getNullValue(Op0->getType())) |
| 4067 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f5b4ef7 | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 4068 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4069 | if (isa<UndefValue>(Op0)) { // undef >>s X -> undef |
| 4070 | if (!isLeftShift && I.getType()->isSigned()) |
Chris Lattner | 67f0545 | 2004-10-16 23:28:04 +0000 | [diff] [blame] | 4071 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4072 | else // undef << X -> 0 AND undef >>u X -> 0 |
| 4073 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 4074 | } |
| 4075 | if (isa<UndefValue>(Op1)) { |
Chris Lattner | 18aa4d8 | 2005-07-20 18:49:28 +0000 | [diff] [blame] | 4076 | if (isLeftShift || I.getType()->isUnsigned())// X << undef, X >>u undef -> 0 |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4077 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 4078 | else |
| 4079 | return ReplaceInstUsesWith(I, Op0); // X >>s undef -> X |
| 4080 | } |
| 4081 | |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 4082 | // shr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 4083 | if (!isLeftShift) |
| 4084 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0)) |
| 4085 | if (CSI->isAllOnesValue()) |
| 4086 | return ReplaceInstUsesWith(I, CSI); |
| 4087 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4088 | // Try to fold constant and into select arguments. |
| 4089 | if (isa<Constant>(Op0)) |
| 4090 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4091 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4092 | return R; |
| 4093 | |
Chris Lattner | b18dbbf | 2005-05-08 17:34:56 +0000 | [diff] [blame] | 4094 | // See if we can turn a signed shr into an unsigned shr. |
| 4095 | if (!isLeftShift && I.getType()->isSigned()) { |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 4096 | if (MaskedValueIsZero(Op0, |
| 4097 | 1ULL << (I.getType()->getPrimitiveSizeInBits()-1))) { |
Chris Lattner | b18dbbf | 2005-05-08 17:34:56 +0000 | [diff] [blame] | 4098 | Value *V = InsertCastBefore(Op0, I.getType()->getUnsignedVersion(), I); |
| 4099 | V = InsertNewInstBefore(new ShiftInst(Instruction::Shr, V, Op1, |
| 4100 | I.getName()), I); |
| 4101 | return new CastInst(V, I.getType()); |
| 4102 | } |
| 4103 | } |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4104 | |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4105 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) |
| 4106 | if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I)) |
| 4107 | return Res; |
| 4108 | return 0; |
| 4109 | } |
| 4110 | |
| 4111 | Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantUInt *Op1, |
| 4112 | ShiftInst &I) { |
| 4113 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | b330939 | 2006-01-06 07:22:22 +0000 | [diff] [blame] | 4114 | bool isSignedShift = Op0->getType()->isSigned(); |
| 4115 | bool isUnsignedShift = !isSignedShift; |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4116 | |
Chris Lattner | f5b4ef7 | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 4117 | // See if we can simplify any instructions used by the instruction whose sole |
| 4118 | // purpose is to compute bits we don't care about. |
| 4119 | uint64_t KnownZero, KnownOne; |
| 4120 | if (SimplifyDemandedBits(&I, I.getType()->getIntegralTypeMask(), |
| 4121 | KnownZero, KnownOne)) |
| 4122 | return &I; |
| 4123 | |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4124 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr |
| 4125 | // of a signed value. |
| 4126 | // |
| 4127 | unsigned TypeBits = Op0->getType()->getPrimitiveSizeInBits(); |
| 4128 | if (Op1->getValue() >= TypeBits) { |
Chris Lattner | b330939 | 2006-01-06 07:22:22 +0000 | [diff] [blame] | 4129 | if (isUnsignedShift || isLeftShift) |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4130 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
| 4131 | else { |
| 4132 | I.setOperand(1, ConstantUInt::get(Type::UByteTy, TypeBits-1)); |
| 4133 | return &I; |
Chris Lattner | f5ce254 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 4134 | } |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
| 4137 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 4138 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 4139 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 4140 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
| 4141 | return BinaryOperator::createMul(BO->getOperand(0), |
| 4142 | ConstantExpr::getShl(BOOp, Op1)); |
| 4143 | |
| 4144 | // Try to fold constant and into select arguments. |
| 4145 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 4146 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 4147 | return R; |
| 4148 | if (isa<PHINode>(Op0)) |
| 4149 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 4150 | return NV; |
| 4151 | |
| 4152 | if (Op0->hasOneUse()) { |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4153 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) { |
| 4154 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
| 4155 | Value *V1, *V2; |
| 4156 | ConstantInt *CC; |
| 4157 | switch (Op0BO->getOpcode()) { |
Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 4158 | default: break; |
| 4159 | case Instruction::Add: |
| 4160 | case Instruction::And: |
| 4161 | case Instruction::Or: |
| 4162 | case Instruction::Xor: |
| 4163 | // These operators commute. |
| 4164 | // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4165 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
| 4166 | match(Op0BO->getOperand(1), |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4167 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4168 | Instruction *YS = new ShiftInst(Instruction::Shl, |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4169 | Op0BO->getOperand(0), Op1, |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4170 | Op0BO->getName()); |
| 4171 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 24cd2fa | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 4172 | Instruction *X = |
| 4173 | BinaryOperator::create(Op0BO->getOpcode(), YS, V1, |
| 4174 | Op0BO->getOperand(1)->getName()); |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4175 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
| 4176 | Constant *C2 = ConstantInt::getAllOnesValue(X->getType()); |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4177 | C2 = ConstantExpr::getShl(C2, Op1); |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4178 | return BinaryOperator::createAnd(X, C2); |
| 4179 | } |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4180 | |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4181 | // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C)) |
| 4182 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
| 4183 | match(Op0BO->getOperand(1), |
| 4184 | m_And(m_Shr(m_Value(V1), m_Value(V2)), |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4185 | m_ConstantInt(CC))) && V2 == Op1 && |
Chris Lattner | 24cd2fa | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 4186 | cast<BinaryOperator>(Op0BO->getOperand(1))->getOperand(0)->hasOneUse()) { |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4187 | Instruction *YS = new ShiftInst(Instruction::Shl, |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4188 | Op0BO->getOperand(0), Op1, |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4189 | Op0BO->getName()); |
| 4190 | InsertNewInstBefore(YS, I); // (Y << C) |
| 4191 | Instruction *XM = |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4192 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4193 | V1->getName()+".mask"); |
| 4194 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 4195 | |
| 4196 | return BinaryOperator::create(Op0BO->getOpcode(), YS, XM); |
| 4197 | } |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4198 | |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4199 | // FALL THROUGH. |
Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 4200 | case Instruction::Sub: |
| 4201 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4202 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 4203 | match(Op0BO->getOperand(0), |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4204 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4205 | Instruction *YS = new ShiftInst(Instruction::Shl, |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4206 | Op0BO->getOperand(1), Op1, |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4207 | Op0BO->getName()); |
| 4208 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 24cd2fa | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 4209 | Instruction *X = |
| 4210 | BinaryOperator::create(Op0BO->getOpcode(), YS, V1, |
| 4211 | Op0BO->getOperand(0)->getName()); |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4212 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
| 4213 | Constant *C2 = ConstantInt::getAllOnesValue(X->getType()); |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4214 | C2 = ConstantExpr::getShl(C2, Op1); |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4215 | return BinaryOperator::createAnd(X, C2); |
| 4216 | } |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4217 | |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4218 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 4219 | match(Op0BO->getOperand(0), |
| 4220 | m_And(m_Shr(m_Value(V1), m_Value(V2)), |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4221 | m_ConstantInt(CC))) && V2 == Op1 && |
Chris Lattner | 24cd2fa | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 4222 | cast<BinaryOperator>(Op0BO->getOperand(0)) |
| 4223 | ->getOperand(0)->hasOneUse()) { |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4224 | Instruction *YS = new ShiftInst(Instruction::Shl, |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4225 | Op0BO->getOperand(1), Op1, |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4226 | Op0BO->getName()); |
| 4227 | InsertNewInstBefore(YS, I); // (Y << C) |
| 4228 | Instruction *XM = |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4229 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4230 | V1->getName()+".mask"); |
| 4231 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 4232 | |
| 4233 | return BinaryOperator::create(Op0BO->getOpcode(), YS, XM); |
| 4234 | } |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4235 | |
Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 4236 | break; |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4237 | } |
| 4238 | |
| 4239 | |
| 4240 | // If the operand is an bitwise operator with a constant RHS, and the |
| 4241 | // shift is the only use, we can pull it out of the shift. |
| 4242 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 4243 | bool isValid = true; // Valid only for And, Or, Xor |
| 4244 | bool highBitSet = false; // Transform if high bit of constant set? |
| 4245 | |
| 4246 | switch (Op0BO->getOpcode()) { |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 4247 | default: isValid = false; break; // Do not perform transform! |
Chris Lattner | 44bd392 | 2004-10-08 03:46:20 +0000 | [diff] [blame] | 4248 | case Instruction::Add: |
| 4249 | isValid = isLeftShift; |
| 4250 | break; |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 4251 | case Instruction::Or: |
| 4252 | case Instruction::Xor: |
| 4253 | highBitSet = false; |
| 4254 | break; |
| 4255 | case Instruction::And: |
| 4256 | highBitSet = true; |
| 4257 | break; |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4258 | } |
| 4259 | |
| 4260 | // If this is a signed shift right, and the high bit is modified |
| 4261 | // by the logical operation, do not perform the transformation. |
| 4262 | // The highBitSet boolean indicates the value of the high bit of |
| 4263 | // the constant which would cause it to be modified for this |
| 4264 | // operation. |
| 4265 | // |
Chris Lattner | b330939 | 2006-01-06 07:22:22 +0000 | [diff] [blame] | 4266 | if (isValid && !isLeftShift && isSignedShift) { |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4267 | uint64_t Val = Op0C->getRawValue(); |
| 4268 | isValid = ((Val & (1 << (TypeBits-1))) != 0) == highBitSet; |
| 4269 | } |
| 4270 | |
| 4271 | if (isValid) { |
| 4272 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1); |
| 4273 | |
| 4274 | Instruction *NewShift = |
| 4275 | new ShiftInst(I.getOpcode(), Op0BO->getOperand(0), Op1, |
| 4276 | Op0BO->getName()); |
| 4277 | Op0BO->setName(""); |
| 4278 | InsertNewInstBefore(NewShift, I); |
| 4279 | |
| 4280 | return BinaryOperator::create(Op0BO->getOpcode(), NewShift, |
| 4281 | NewRHS); |
| 4282 | } |
| 4283 | } |
| 4284 | } |
| 4285 | } |
| 4286 | |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 4287 | // Find out if this is a shift of a shift by a constant. |
| 4288 | ShiftInst *ShiftOp = 0; |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4289 | if (ShiftInst *Op0SI = dyn_cast<ShiftInst>(Op0)) |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 4290 | ShiftOp = Op0SI; |
| 4291 | else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
| 4292 | // If this is a noop-integer case of a shift instruction, use the shift. |
| 4293 | if (CI->getOperand(0)->getType()->isInteger() && |
| 4294 | CI->getOperand(0)->getType()->getPrimitiveSizeInBits() == |
| 4295 | CI->getType()->getPrimitiveSizeInBits() && |
| 4296 | isa<ShiftInst>(CI->getOperand(0))) { |
| 4297 | ShiftOp = cast<ShiftInst>(CI->getOperand(0)); |
| 4298 | } |
| 4299 | } |
| 4300 | |
| 4301 | if (ShiftOp && isa<ConstantUInt>(ShiftOp->getOperand(1))) { |
| 4302 | // Find the operands and properties of the input shift. Note that the |
| 4303 | // signedness of the input shift may differ from the current shift if there |
| 4304 | // is a noop cast between the two. |
| 4305 | bool isShiftOfLeftShift = ShiftOp->getOpcode() == Instruction::Shl; |
| 4306 | bool isShiftOfSignedShift = ShiftOp->getType()->isSigned(); |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 4307 | bool isShiftOfUnsignedShift = !isShiftOfSignedShift; |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 4308 | |
| 4309 | ConstantUInt *ShiftAmt1C = cast<ConstantUInt>(ShiftOp->getOperand(1)); |
| 4310 | |
| 4311 | unsigned ShiftAmt1 = (unsigned)ShiftAmt1C->getValue(); |
| 4312 | unsigned ShiftAmt2 = (unsigned)Op1->getValue(); |
| 4313 | |
| 4314 | // Check for (A << c1) << c2 and (A >> c1) >> c2. |
| 4315 | if (isLeftShift == isShiftOfLeftShift) { |
| 4316 | // Do not fold these shifts if the first one is signed and the second one |
| 4317 | // is unsigned and this is a right shift. Further, don't do any folding |
| 4318 | // on them. |
| 4319 | if (isShiftOfSignedShift && isUnsignedShift && !isLeftShift) |
| 4320 | return 0; |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4321 | |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 4322 | unsigned Amt = ShiftAmt1+ShiftAmt2; // Fold into one big shift. |
| 4323 | if (Amt > Op0->getType()->getPrimitiveSizeInBits()) |
| 4324 | Amt = Op0->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4325 | |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 4326 | Value *Op = ShiftOp->getOperand(0); |
| 4327 | if (isShiftOfSignedShift != isSignedShift) |
| 4328 | Op = InsertNewInstBefore(new CastInst(Op, I.getType(), "tmp"), I); |
| 4329 | return new ShiftInst(I.getOpcode(), Op, |
| 4330 | ConstantUInt::get(Type::UByteTy, Amt)); |
| 4331 | } |
| 4332 | |
| 4333 | // Check for (A << c1) >> c2 or (A >> c1) << c2. If we are dealing with |
| 4334 | // signed types, we can only support the (A >> c1) << c2 configuration, |
| 4335 | // because it can not turn an arbitrary bit of A into a sign bit. |
| 4336 | if (isUnsignedShift || isLeftShift) { |
| 4337 | // Calculate bitmask for what gets shifted off the edge. |
| 4338 | Constant *C = ConstantIntegral::getAllOnesValue(I.getType()); |
| 4339 | if (isLeftShift) |
| 4340 | C = ConstantExpr::getShl(C, ShiftAmt1C); |
| 4341 | else |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 4342 | C = ConstantExpr::getUShr(C, ShiftAmt1C); |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 4343 | |
| 4344 | Value *Op = ShiftOp->getOperand(0); |
| 4345 | if (isShiftOfSignedShift != isSignedShift) |
| 4346 | Op = InsertNewInstBefore(new CastInst(Op, I.getType(),Op->getName()),I); |
| 4347 | |
| 4348 | Instruction *Mask = |
| 4349 | BinaryOperator::createAnd(Op, C, Op->getName()+".mask"); |
| 4350 | InsertNewInstBefore(Mask, I); |
| 4351 | |
| 4352 | // Figure out what flavor of shift we should use... |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 4353 | if (ShiftAmt1 == ShiftAmt2) { |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 4354 | return ReplaceInstUsesWith(I, Mask); // (A << c) >> c === A & c2 |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 4355 | } else if (ShiftAmt1 < ShiftAmt2) { |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 4356 | return new ShiftInst(I.getOpcode(), Mask, |
| 4357 | ConstantUInt::get(Type::UByteTy, ShiftAmt2-ShiftAmt1)); |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 4358 | } else if (isShiftOfUnsignedShift || isShiftOfLeftShift) { |
| 4359 | if (isShiftOfUnsignedShift && !isShiftOfLeftShift && isSignedShift) { |
| 4360 | // Make sure to emit an unsigned shift right, not a signed one. |
| 4361 | Mask = InsertNewInstBefore(new CastInst(Mask, |
| 4362 | Mask->getType()->getUnsignedVersion(), |
| 4363 | Op->getName()), I); |
| 4364 | Mask = new ShiftInst(Instruction::Shr, Mask, |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 4365 | ConstantUInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2)); |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 4366 | InsertNewInstBefore(Mask, I); |
| 4367 | return new CastInst(Mask, I.getType()); |
| 4368 | } else { |
| 4369 | return new ShiftInst(ShiftOp->getOpcode(), Mask, |
| 4370 | ConstantUInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2)); |
| 4371 | } |
| 4372 | } else { |
| 4373 | // (X >>s C1) << C2 where C1 > C2 === (X >>s (C1-C2)) & mask |
| 4374 | Op = InsertNewInstBefore(new CastInst(Mask, |
| 4375 | I.getType()->getSignedVersion(), |
| 4376 | Mask->getName()), I); |
| 4377 | Instruction *Shift = |
| 4378 | new ShiftInst(ShiftOp->getOpcode(), Op, |
| 4379 | ConstantUInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2)); |
| 4380 | InsertNewInstBefore(Shift, I); |
| 4381 | |
| 4382 | C = ConstantIntegral::getAllOnesValue(Shift->getType()); |
| 4383 | C = ConstantExpr::getShl(C, Op1); |
| 4384 | Mask = BinaryOperator::createAnd(Shift, C, Op->getName()+".mask"); |
| 4385 | InsertNewInstBefore(Mask, I); |
| 4386 | return new CastInst(Mask, I.getType()); |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 4387 | } |
| 4388 | } else { |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 4389 | // We can handle signed (X << C1) >>s C2 if it's a sign extend. In |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 4390 | // this case, C1 == C2 and C1 is 8, 16, or 32. |
| 4391 | if (ShiftAmt1 == ShiftAmt2) { |
| 4392 | const Type *SExtType = 0; |
| 4393 | switch (ShiftAmt1) { |
| 4394 | case 8 : SExtType = Type::SByteTy; break; |
| 4395 | case 16: SExtType = Type::ShortTy; break; |
| 4396 | case 32: SExtType = Type::IntTy; break; |
| 4397 | } |
| 4398 | |
| 4399 | if (SExtType) { |
| 4400 | Instruction *NewTrunc = new CastInst(ShiftOp->getOperand(0), |
| 4401 | SExtType, "sext"); |
| 4402 | InsertNewInstBefore(NewTrunc, I); |
| 4403 | return new CastInst(NewTrunc, I.getType()); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 4404 | } |
Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 4405 | } |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4406 | } |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 4407 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4408 | return 0; |
| 4409 | } |
| 4410 | |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 4411 | enum CastType { |
| 4412 | Noop = 0, |
| 4413 | Truncate = 1, |
| 4414 | Signext = 2, |
| 4415 | Zeroext = 3 |
| 4416 | }; |
| 4417 | |
| 4418 | /// getCastType - In the future, we will split the cast instruction into these |
| 4419 | /// various types. Until then, we have to do the analysis here. |
| 4420 | static CastType getCastType(const Type *Src, const Type *Dest) { |
| 4421 | assert(Src->isIntegral() && Dest->isIntegral() && |
| 4422 | "Only works on integral types!"); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4423 | unsigned SrcSize = Src->getPrimitiveSizeInBits(); |
| 4424 | unsigned DestSize = Dest->getPrimitiveSizeInBits(); |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 4425 | |
| 4426 | if (SrcSize == DestSize) return Noop; |
| 4427 | if (SrcSize > DestSize) return Truncate; |
| 4428 | if (Src->isSigned()) return Signext; |
| 4429 | return Zeroext; |
| 4430 | } |
| 4431 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4432 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4433 | // isEliminableCastOfCast - Return true if it is valid to eliminate the CI |
| 4434 | // instruction. |
| 4435 | // |
Chris Lattner | e154abf | 2006-01-19 07:40:22 +0000 | [diff] [blame] | 4436 | static bool isEliminableCastOfCast(const Type *SrcTy, const Type *MidTy, |
| 4437 | const Type *DstTy, TargetData *TD) { |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4438 | |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 4439 | // 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] | 4440 | // are identical and the bits don't get reinterpreted (for example |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 4441 | // int->float->int would not be allowed). |
Misha Brukman | e5838c4 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 4442 | if (SrcTy == DstTy && SrcTy->isLosslesslyConvertibleTo(MidTy)) |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 4443 | return true; |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4444 | |
Chris Lattner | 4fbad96 | 2004-07-21 04:27:24 +0000 | [diff] [blame] | 4445 | // If we are casting between pointer and integer types, treat pointers as |
| 4446 | // integers of the appropriate size for the code below. |
| 4447 | if (isa<PointerType>(SrcTy)) SrcTy = TD->getIntPtrType(); |
| 4448 | if (isa<PointerType>(MidTy)) MidTy = TD->getIntPtrType(); |
| 4449 | if (isa<PointerType>(DstTy)) DstTy = TD->getIntPtrType(); |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 4450 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4451 | // Allow free casting and conversion of sizes as long as the sign doesn't |
| 4452 | // change... |
Chris Lattner | b0b412e | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 4453 | if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral()) { |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 4454 | CastType FirstCast = getCastType(SrcTy, MidTy); |
| 4455 | CastType SecondCast = getCastType(MidTy, DstTy); |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 4456 | |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 4457 | // Capture the effect of these two casts. If the result is a legal cast, |
| 4458 | // the CastType is stored here, otherwise a special code is used. |
| 4459 | static const unsigned CastResult[] = { |
| 4460 | // First cast is noop |
| 4461 | 0, 1, 2, 3, |
| 4462 | // First cast is a truncate |
| 4463 | 1, 1, 4, 4, // trunc->extend is not safe to eliminate |
| 4464 | // First cast is a sign ext |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 4465 | 2, 5, 2, 4, // signext->zeroext never ok |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 4466 | // First cast is a zero ext |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 4467 | 3, 5, 3, 3, |
Chris Lattner | 4e2dbc6 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 4468 | }; |
| 4469 | |
| 4470 | unsigned Result = CastResult[FirstCast*4+SecondCast]; |
| 4471 | switch (Result) { |
| 4472 | default: assert(0 && "Illegal table value!"); |
| 4473 | case 0: |
| 4474 | case 1: |
| 4475 | case 2: |
| 4476 | case 3: |
| 4477 | // FIXME: in the future, when LLVM has explicit sign/zeroextends and |
| 4478 | // truncates, we could eliminate more casts. |
| 4479 | return (unsigned)getCastType(SrcTy, DstTy) == Result; |
| 4480 | case 4: |
| 4481 | return false; // Not possible to eliminate this here. |
| 4482 | case 5: |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 4483 | // Sign or zero extend followed by truncate is always ok if the result |
| 4484 | // is a truncate or noop. |
| 4485 | CastType ResultCast = getCastType(SrcTy, DstTy); |
| 4486 | if (ResultCast == Noop || ResultCast == Truncate) |
| 4487 | return true; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4488 | // 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] | 4489 | // result will match the sign/zeroextendness of the result. |
| 4490 | return ResultCast == FirstCast; |
Chris Lattner | 3732aca | 2002-08-15 16:15:25 +0000 | [diff] [blame] | 4491 | } |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 4492 | } |
Chris Lattner | e154abf | 2006-01-19 07:40:22 +0000 | [diff] [blame] | 4493 | |
| 4494 | // If this is a cast from 'float -> double -> integer', cast from |
| 4495 | // 'float -> integer' directly, as the value isn't changed by the |
| 4496 | // float->double conversion. |
| 4497 | if (SrcTy->isFloatingPoint() && MidTy->isFloatingPoint() && |
| 4498 | DstTy->isIntegral() && |
| 4499 | SrcTy->getPrimitiveSize() < MidTy->getPrimitiveSize()) |
| 4500 | return true; |
| 4501 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4502 | return false; |
| 4503 | } |
| 4504 | |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 4505 | static bool ValueRequiresCast(const Value *V, const Type *Ty, TargetData *TD) { |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 4506 | if (V->getType() == Ty || isa<Constant>(V)) return false; |
| 4507 | if (const CastInst *CI = dyn_cast<CastInst>(V)) |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 4508 | if (isEliminableCastOfCast(CI->getOperand(0)->getType(), CI->getType(), Ty, |
| 4509 | TD)) |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 4510 | return false; |
| 4511 | return true; |
| 4512 | } |
| 4513 | |
| 4514 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 4515 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 4516 | /// casts that are known to not do anything... |
| 4517 | /// |
| 4518 | Value *InstCombiner::InsertOperandCastBefore(Value *V, const Type *DestTy, |
| 4519 | Instruction *InsertBefore) { |
| 4520 | if (V->getType() == DestTy) return V; |
| 4521 | if (Constant *C = dyn_cast<Constant>(V)) |
| 4522 | return ConstantExpr::getCast(C, DestTy); |
| 4523 | |
| 4524 | CastInst *CI = new CastInst(V, DestTy, V->getName()); |
| 4525 | InsertNewInstBefore(CI, *InsertBefore); |
| 4526 | return CI; |
| 4527 | } |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4528 | |
Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 4529 | /// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear |
| 4530 | /// expression. If so, decompose it, returning some value X, such that Val is |
| 4531 | /// X*Scale+Offset. |
| 4532 | /// |
| 4533 | static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale, |
| 4534 | unsigned &Offset) { |
| 4535 | assert(Val->getType() == Type::UIntTy && "Unexpected allocation size type!"); |
| 4536 | if (ConstantUInt *CI = dyn_cast<ConstantUInt>(Val)) { |
| 4537 | Offset = CI->getValue(); |
| 4538 | Scale = 1; |
| 4539 | return ConstantUInt::get(Type::UIntTy, 0); |
| 4540 | } else if (Instruction *I = dyn_cast<Instruction>(Val)) { |
| 4541 | if (I->getNumOperands() == 2) { |
| 4542 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(I->getOperand(1))) { |
| 4543 | if (I->getOpcode() == Instruction::Shl) { |
| 4544 | // This is a value scaled by '1 << the shift amt'. |
| 4545 | Scale = 1U << CUI->getValue(); |
| 4546 | Offset = 0; |
| 4547 | return I->getOperand(0); |
| 4548 | } else if (I->getOpcode() == Instruction::Mul) { |
| 4549 | // This value is scaled by 'CUI'. |
| 4550 | Scale = CUI->getValue(); |
| 4551 | Offset = 0; |
| 4552 | return I->getOperand(0); |
| 4553 | } else if (I->getOpcode() == Instruction::Add) { |
| 4554 | // We have X+C. Check to see if we really have (X*C2)+C1, where C1 is |
| 4555 | // divisible by C2. |
| 4556 | unsigned SubScale; |
| 4557 | Value *SubVal = DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, |
| 4558 | Offset); |
| 4559 | Offset += CUI->getValue(); |
| 4560 | if (SubScale > 1 && (Offset % SubScale == 0)) { |
| 4561 | Scale = SubScale; |
| 4562 | return SubVal; |
| 4563 | } |
| 4564 | } |
| 4565 | } |
| 4566 | } |
| 4567 | } |
| 4568 | |
| 4569 | // Otherwise, we can't look past this. |
| 4570 | Scale = 1; |
| 4571 | Offset = 0; |
| 4572 | return Val; |
| 4573 | } |
| 4574 | |
| 4575 | |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 4576 | /// PromoteCastOfAllocation - If we find a cast of an allocation instruction, |
| 4577 | /// try to eliminate the cast by moving the type information into the alloc. |
| 4578 | Instruction *InstCombiner::PromoteCastOfAllocation(CastInst &CI, |
| 4579 | AllocationInst &AI) { |
| 4580 | const PointerType *PTy = dyn_cast<PointerType>(CI.getType()); |
Chris Lattner | bb17180 | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 4581 | if (!PTy) return 0; // Not casting the allocation to a pointer type. |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 4582 | |
Chris Lattner | ac87beb | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 4583 | // Remove any uses of AI that are dead. |
| 4584 | assert(!CI.use_empty() && "Dead instructions should be removed earlier!"); |
| 4585 | std::vector<Instruction*> DeadUsers; |
| 4586 | for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) { |
| 4587 | Instruction *User = cast<Instruction>(*UI++); |
| 4588 | if (isInstructionTriviallyDead(User)) { |
| 4589 | while (UI != E && *UI == User) |
| 4590 | ++UI; // If this instruction uses AI more than once, don't break UI. |
| 4591 | |
| 4592 | // Add operands to the worklist. |
| 4593 | AddUsesToWorkList(*User); |
| 4594 | ++NumDeadInst; |
| 4595 | DEBUG(std::cerr << "IC: DCE: " << *User); |
| 4596 | |
| 4597 | User->eraseFromParent(); |
| 4598 | removeFromWorkList(User); |
| 4599 | } |
| 4600 | } |
| 4601 | |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 4602 | // Get the type really allocated and the type casted to. |
| 4603 | const Type *AllocElTy = AI.getAllocatedType(); |
| 4604 | const Type *CastElTy = PTy->getElementType(); |
| 4605 | if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0; |
Chris Lattner | 355ecc0 | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 4606 | |
| 4607 | unsigned AllocElTyAlign = TD->getTypeSize(AllocElTy); |
| 4608 | unsigned CastElTyAlign = TD->getTypeSize(CastElTy); |
| 4609 | if (CastElTyAlign < AllocElTyAlign) return 0; |
| 4610 | |
Chris Lattner | 46705b2 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 4611 | // If the allocation has multiple uses, only promote it if we are strictly |
| 4612 | // increasing the alignment of the resultant allocation. If we keep it the |
| 4613 | // same, we open the door to infinite loops of various kinds. |
| 4614 | if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0; |
| 4615 | |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 4616 | uint64_t AllocElTySize = TD->getTypeSize(AllocElTy); |
| 4617 | uint64_t CastElTySize = TD->getTypeSize(CastElTy); |
Chris Lattner | bb17180 | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 4618 | if (CastElTySize == 0 || AllocElTySize == 0) return 0; |
Chris Lattner | 355ecc0 | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 4619 | |
Chris Lattner | 8270c33 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 4620 | // See if we can satisfy the modulus by pulling a scale out of the array |
| 4621 | // size argument. |
Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 4622 | unsigned ArraySizeScale, ArrayOffset; |
| 4623 | Value *NumElements = // See if the array size is a decomposable linear expr. |
| 4624 | DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset); |
| 4625 | |
Chris Lattner | 8270c33 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 4626 | // If we can now satisfy the modulus, by using a non-1 scale, we really can |
| 4627 | // do the xform. |
Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 4628 | if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 || |
| 4629 | (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0; |
Chris Lattner | b3ecf96 | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 4630 | |
Chris Lattner | 8270c33 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 4631 | unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize; |
| 4632 | Value *Amt = 0; |
| 4633 | if (Scale == 1) { |
| 4634 | Amt = NumElements; |
| 4635 | } else { |
| 4636 | Amt = ConstantUInt::get(Type::UIntTy, Scale); |
| 4637 | if (ConstantUInt *CI = dyn_cast<ConstantUInt>(NumElements)) |
| 4638 | Amt = ConstantExpr::getMul(CI, cast<ConstantUInt>(Amt)); |
| 4639 | else if (Scale != 1) { |
| 4640 | Instruction *Tmp = BinaryOperator::createMul(Amt, NumElements, "tmp"); |
| 4641 | Amt = InsertNewInstBefore(Tmp, AI); |
Chris Lattner | b3ecf96 | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 4642 | } |
Chris Lattner | bb17180 | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 4643 | } |
| 4644 | |
Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 4645 | if (unsigned Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { |
| 4646 | Value *Off = ConstantUInt::get(Type::UIntTy, Offset); |
| 4647 | Instruction *Tmp = BinaryOperator::createAdd(Amt, Off, "tmp"); |
| 4648 | Amt = InsertNewInstBefore(Tmp, AI); |
| 4649 | } |
| 4650 | |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 4651 | std::string Name = AI.getName(); AI.setName(""); |
| 4652 | AllocationInst *New; |
| 4653 | if (isa<MallocInst>(AI)) |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 4654 | New = new MallocInst(CastElTy, Amt, AI.getAlignment(), Name); |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 4655 | else |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 4656 | New = new AllocaInst(CastElTy, Amt, AI.getAlignment(), Name); |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 4657 | InsertNewInstBefore(New, AI); |
Chris Lattner | 46705b2 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 4658 | |
| 4659 | // If the allocation has multiple uses, insert a cast and change all things |
| 4660 | // that used it to use the new cast. This will also hack on CI, but it will |
| 4661 | // die soon. |
| 4662 | if (!AI.hasOneUse()) { |
| 4663 | AddUsesToWorkList(AI); |
| 4664 | CastInst *NewCast = new CastInst(New, AI.getType(), "tmpcast"); |
| 4665 | InsertNewInstBefore(NewCast, AI); |
| 4666 | AI.replaceAllUsesWith(NewCast); |
| 4667 | } |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 4668 | return ReplaceInstUsesWith(CI, New); |
| 4669 | } |
| 4670 | |
| 4671 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4672 | // CastInst simplification |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 4673 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4674 | Instruction *InstCombiner::visitCastInst(CastInst &CI) { |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 4675 | Value *Src = CI.getOperand(0); |
| 4676 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4677 | // If the user is casting a value to the same type, eliminate this cast |
| 4678 | // instruction... |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 4679 | if (CI.getType() == Src->getType()) |
| 4680 | return ReplaceInstUsesWith(CI, Src); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4681 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4682 | if (isa<UndefValue>(Src)) // cast undef -> undef |
| 4683 | return ReplaceInstUsesWith(CI, UndefValue::get(CI.getType())); |
| 4684 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4685 | // If casting the result of another cast instruction, try to eliminate this |
| 4686 | // one! |
| 4687 | // |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4688 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
| 4689 | Value *A = CSrc->getOperand(0); |
| 4690 | if (isEliminableCastOfCast(A->getType(), CSrc->getType(), |
| 4691 | CI.getType(), TD)) { |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4692 | // This instruction now refers directly to the cast's src operand. This |
| 4693 | // has a good chance of making CSrc dead. |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4694 | CI.setOperand(0, CSrc->getOperand(0)); |
| 4695 | return &CI; |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4696 | } |
| 4697 | |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 4698 | // If this is an A->B->A cast, and we are dealing with integral types, try |
| 4699 | // to convert this into a logical 'and' instruction. |
| 4700 | // |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4701 | if (A->getType()->isInteger() && |
Chris Lattner | b0b412e | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 4702 | CI.getType()->isInteger() && CSrc->getType()->isInteger() && |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4703 | CSrc->getType()->isUnsigned() && // B->A cast must zero extend |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4704 | CSrc->getType()->getPrimitiveSizeInBits() < |
| 4705 | CI.getType()->getPrimitiveSizeInBits()&& |
| 4706 | A->getType()->getPrimitiveSizeInBits() == |
| 4707 | CI.getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 4708 | assert(CSrc->getType() != Type::ULongTy && |
| 4709 | "Cannot have type bigger than ulong!"); |
Chris Lattner | 77defba | 2006-02-07 07:00:41 +0000 | [diff] [blame] | 4710 | uint64_t AndValue = CSrc->getType()->getIntegralTypeMask(); |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4711 | Constant *AndOp = ConstantUInt::get(A->getType()->getUnsignedVersion(), |
| 4712 | AndValue); |
| 4713 | AndOp = ConstantExpr::getCast(AndOp, A->getType()); |
| 4714 | Instruction *And = BinaryOperator::createAnd(CSrc->getOperand(0), AndOp); |
| 4715 | if (And->getType() != CI.getType()) { |
| 4716 | And->setName(CSrc->getName()+".mask"); |
| 4717 | InsertNewInstBefore(And, CI); |
| 4718 | And = new CastInst(And, CI.getType()); |
| 4719 | } |
| 4720 | return And; |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 4721 | } |
| 4722 | } |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 4723 | |
Chris Lattner | 0384165 | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 4724 | // If this is a cast to bool, turn it into the appropriate setne instruction. |
| 4725 | if (CI.getType() == Type::BoolTy) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4726 | return BinaryOperator::createSetNE(CI.getOperand(0), |
Chris Lattner | 0384165 | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 4727 | Constant::getNullValue(CI.getOperand(0)->getType())); |
| 4728 | |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 4729 | // See if we can simplify any instructions used by the LHS whose sole |
| 4730 | // purpose is to compute bits we don't care about. |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 4731 | if (CI.getType()->isInteger() && CI.getOperand(0)->getType()->isIntegral()) { |
| 4732 | uint64_t KnownZero, KnownOne; |
| 4733 | if (SimplifyDemandedBits(&CI, CI.getType()->getIntegralTypeMask(), |
| 4734 | KnownZero, KnownOne)) |
| 4735 | return &CI; |
| 4736 | } |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 4737 | |
Chris Lattner | d0d5160 | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 4738 | // If casting the result of a getelementptr instruction with no offset, turn |
| 4739 | // this into a cast of the original pointer! |
| 4740 | // |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 4741 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
Chris Lattner | d0d5160 | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 4742 | bool AllZeroOperands = true; |
| 4743 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) |
| 4744 | if (!isa<Constant>(GEP->getOperand(i)) || |
| 4745 | !cast<Constant>(GEP->getOperand(i))->isNullValue()) { |
| 4746 | AllZeroOperands = false; |
| 4747 | break; |
| 4748 | } |
| 4749 | if (AllZeroOperands) { |
| 4750 | CI.setOperand(0, GEP->getOperand(0)); |
| 4751 | return &CI; |
| 4752 | } |
| 4753 | } |
| 4754 | |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 4755 | // If we are casting a malloc or alloca to a pointer to a type of the same |
| 4756 | // size, rewrite the allocation instruction to allocate the "right" type. |
| 4757 | // |
| 4758 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 4759 | if (Instruction *V = PromoteCastOfAllocation(CI, *AI)) |
| 4760 | return V; |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 4761 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4762 | if (SelectInst *SI = dyn_cast<SelectInst>(Src)) |
| 4763 | if (Instruction *NV = FoldOpIntoSelect(CI, SI, this)) |
| 4764 | return NV; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 4765 | if (isa<PHINode>(Src)) |
| 4766 | if (Instruction *NV = FoldOpIntoPhi(CI)) |
| 4767 | return NV; |
| 4768 | |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 4769 | // If the source value is an instruction with only this use, we can attempt to |
| 4770 | // propagate the cast into the instruction. Also, only handle integral types |
| 4771 | // for now. |
| 4772 | if (Instruction *SrcI = dyn_cast<Instruction>(Src)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 4773 | if (SrcI->hasOneUse() && Src->getType()->isIntegral() && |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 4774 | CI.getType()->isInteger()) { // Don't mess with casts to bool here |
| 4775 | const Type *DestTy = CI.getType(); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4776 | unsigned SrcBitSize = Src->getType()->getPrimitiveSizeInBits(); |
| 4777 | unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 4778 | |
| 4779 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; |
| 4780 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; |
| 4781 | |
| 4782 | switch (SrcI->getOpcode()) { |
| 4783 | case Instruction::Add: |
| 4784 | case Instruction::Mul: |
| 4785 | case Instruction::And: |
| 4786 | case Instruction::Or: |
| 4787 | case Instruction::Xor: |
| 4788 | // If we are discarding information, or just changing the sign, rewrite. |
| 4789 | if (DestBitSize <= SrcBitSize && DestBitSize != 1) { |
| 4790 | // Don't insert two casts if they cannot be eliminated. We allow two |
| 4791 | // casts to be inserted if the sizes are the same. This could only be |
| 4792 | // converting signedness, which is a noop. |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 4793 | if (DestBitSize == SrcBitSize || !ValueRequiresCast(Op1, DestTy,TD) || |
| 4794 | !ValueRequiresCast(Op0, DestTy, TD)) { |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 4795 | Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI); |
| 4796 | Value *Op1c = InsertOperandCastBefore(Op1, DestTy, SrcI); |
| 4797 | return BinaryOperator::create(cast<BinaryOperator>(SrcI) |
| 4798 | ->getOpcode(), Op0c, Op1c); |
| 4799 | } |
| 4800 | } |
Chris Lattner | 7208616 | 2005-05-06 02:07:39 +0000 | [diff] [blame] | 4801 | |
| 4802 | // cast (xor bool X, true) to int --> xor (cast bool X to int), 1 |
| 4803 | if (SrcBitSize == 1 && SrcI->getOpcode() == Instruction::Xor && |
| 4804 | Op1 == ConstantBool::True && |
| 4805 | (!Op0->hasOneUse() || !isa<SetCondInst>(Op0))) { |
| 4806 | Value *New = InsertOperandCastBefore(Op0, DestTy, &CI); |
| 4807 | return BinaryOperator::createXor(New, |
| 4808 | ConstantInt::get(CI.getType(), 1)); |
| 4809 | } |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 4810 | break; |
| 4811 | case Instruction::Shl: |
| 4812 | // Allow changing the sign of the source operand. Do not allow changing |
| 4813 | // the size of the shift, UNLESS the shift amount is a constant. We |
| 4814 | // mush not change variable sized shifts to a smaller size, because it |
| 4815 | // is undefined to shift more bits out than exist in the value. |
| 4816 | if (DestBitSize == SrcBitSize || |
| 4817 | (DestBitSize < SrcBitSize && isa<Constant>(Op1))) { |
| 4818 | Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI); |
| 4819 | return new ShiftInst(Instruction::Shl, Op0c, Op1); |
| 4820 | } |
| 4821 | break; |
Chris Lattner | 8738041 | 2005-05-06 04:18:52 +0000 | [diff] [blame] | 4822 | case Instruction::Shr: |
| 4823 | // If this is a signed shr, and if all bits shifted in are about to be |
| 4824 | // truncated off, turn it into an unsigned shr to allow greater |
| 4825 | // simplifications. |
| 4826 | if (DestBitSize < SrcBitSize && Src->getType()->isSigned() && |
| 4827 | isa<ConstantInt>(Op1)) { |
| 4828 | unsigned ShiftAmt = cast<ConstantUInt>(Op1)->getValue(); |
| 4829 | if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) { |
| 4830 | // Convert to unsigned. |
| 4831 | Value *N1 = InsertOperandCastBefore(Op0, |
| 4832 | Op0->getType()->getUnsignedVersion(), &CI); |
| 4833 | // Insert the new shift, which is now unsigned. |
| 4834 | N1 = InsertNewInstBefore(new ShiftInst(Instruction::Shr, N1, |
| 4835 | Op1, Src->getName()), CI); |
| 4836 | return new CastInst(N1, CI.getType()); |
| 4837 | } |
| 4838 | } |
| 4839 | break; |
| 4840 | |
Chris Lattner | c7bfed0 | 2006-02-27 02:38:23 +0000 | [diff] [blame] | 4841 | case Instruction::SetEQ: |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 4842 | case Instruction::SetNE: |
Chris Lattner | c7bfed0 | 2006-02-27 02:38:23 +0000 | [diff] [blame] | 4843 | // We if we are just checking for a seteq of a single bit and casting it |
| 4844 | // to an integer. If so, shift the bit to the appropriate place then |
| 4845 | // cast to integer to avoid the comparison. |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 4846 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | c7bfed0 | 2006-02-27 02:38:23 +0000 | [diff] [blame] | 4847 | uint64_t Op1CV = Op1C->getZExtValue(); |
| 4848 | // cast (X == 0) to int --> X^1 iff X has only the low bit set. |
| 4849 | // cast (X == 0) to int --> (X>>1)^1 iff X has only the 2nd bit set. |
| 4850 | // cast (X == 1) to int --> X iff X has only the low bit set. |
| 4851 | // cast (X == 2) to int --> X>>1 iff X has only the 2nd bit set. |
| 4852 | // cast (X != 0) to int --> X iff X has only the low bit set. |
| 4853 | // cast (X != 0) to int --> X>>1 iff X has only the 2nd bit set. |
| 4854 | // cast (X != 1) to int --> X^1 iff X has only the low bit set. |
| 4855 | // cast (X != 2) to int --> (X>>1)^1 iff X has only the 2nd bit set. |
| 4856 | if (Op1CV == 0 || isPowerOf2_64(Op1CV)) { |
| 4857 | // If Op1C some other power of two, convert: |
| 4858 | uint64_t KnownZero, KnownOne; |
| 4859 | uint64_t TypeMask = Op1->getType()->getIntegralTypeMask(); |
| 4860 | ComputeMaskedBits(Op0, TypeMask, KnownZero, KnownOne); |
| 4861 | |
| 4862 | if (isPowerOf2_64(KnownZero^TypeMask)) { // Exactly one possible 1? |
| 4863 | bool isSetNE = SrcI->getOpcode() == Instruction::SetNE; |
| 4864 | if (Op1CV && (Op1CV != (KnownZero^TypeMask))) { |
| 4865 | // (X&4) == 2 --> false |
| 4866 | // (X&4) != 2 --> true |
Chris Lattner | c5b6c9a | 2006-02-28 19:47:20 +0000 | [diff] [blame] | 4867 | Constant *Res = ConstantBool::get(isSetNE); |
| 4868 | Res = ConstantExpr::getCast(Res, CI.getType()); |
| 4869 | return ReplaceInstUsesWith(CI, Res); |
Chris Lattner | c7bfed0 | 2006-02-27 02:38:23 +0000 | [diff] [blame] | 4870 | } |
| 4871 | |
| 4872 | unsigned ShiftAmt = Log2_64(KnownZero^TypeMask); |
| 4873 | Value *In = Op0; |
| 4874 | if (ShiftAmt) { |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 4875 | // Perform an unsigned shr by shiftamt. Convert input to |
| 4876 | // unsigned if it is signed. |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 4877 | if (In->getType()->isSigned()) |
| 4878 | In = InsertNewInstBefore(new CastInst(In, |
| 4879 | In->getType()->getUnsignedVersion(), In->getName()),CI); |
| 4880 | // Insert the shift to put the result in the low bit. |
| 4881 | In = InsertNewInstBefore(new ShiftInst(Instruction::Shr, In, |
Chris Lattner | c7bfed0 | 2006-02-27 02:38:23 +0000 | [diff] [blame] | 4882 | ConstantInt::get(Type::UByteTy, ShiftAmt), |
| 4883 | In->getName()+".lobit"), CI); |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 4884 | } |
Chris Lattner | c7bfed0 | 2006-02-27 02:38:23 +0000 | [diff] [blame] | 4885 | |
| 4886 | if ((Op1CV != 0) == isSetNE) { // Toggle the low bit. |
| 4887 | Constant *One = ConstantInt::get(In->getType(), 1); |
| 4888 | In = BinaryOperator::createXor(In, One, "tmp"); |
| 4889 | InsertNewInstBefore(cast<Instruction>(In), CI); |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 4890 | } |
Chris Lattner | c7bfed0 | 2006-02-27 02:38:23 +0000 | [diff] [blame] | 4891 | |
| 4892 | if (CI.getType() == In->getType()) |
| 4893 | return ReplaceInstUsesWith(CI, In); |
| 4894 | else |
| 4895 | return new CastInst(In, CI.getType()); |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 4896 | } |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 4897 | } |
| 4898 | } |
| 4899 | break; |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 4900 | } |
| 4901 | } |
Chris Lattner | bb17180 | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 4902 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 4903 | return 0; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 4906 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
| 4907 | /// %C = or %A, %B |
| 4908 | /// %D = select %cond, %C, %A |
| 4909 | /// into: |
| 4910 | /// %C = select %cond, %B, 0 |
| 4911 | /// %D = or %A, %C |
| 4912 | /// |
| 4913 | /// Assuming that the specified instruction is an operand to the select, return |
| 4914 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 4915 | /// equal the other incoming value of the select. |
| 4916 | /// |
| 4917 | static unsigned GetSelectFoldableOperands(Instruction *I) { |
| 4918 | switch (I->getOpcode()) { |
| 4919 | case Instruction::Add: |
| 4920 | case Instruction::Mul: |
| 4921 | case Instruction::And: |
| 4922 | case Instruction::Or: |
| 4923 | case Instruction::Xor: |
| 4924 | return 3; // Can fold through either operand. |
| 4925 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 4926 | case Instruction::Shl: // Can only fold on the shift amount. |
| 4927 | case Instruction::Shr: |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4928 | return 1; |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 4929 | default: |
| 4930 | return 0; // Cannot fold |
| 4931 | } |
| 4932 | } |
| 4933 | |
| 4934 | /// GetSelectFoldableConstant - For the same transformation as the previous |
| 4935 | /// function, return the identity constant that goes into the select. |
| 4936 | static Constant *GetSelectFoldableConstant(Instruction *I) { |
| 4937 | switch (I->getOpcode()) { |
| 4938 | default: assert(0 && "This cannot happen!"); abort(); |
| 4939 | case Instruction::Add: |
| 4940 | case Instruction::Sub: |
| 4941 | case Instruction::Or: |
| 4942 | case Instruction::Xor: |
| 4943 | return Constant::getNullValue(I->getType()); |
| 4944 | case Instruction::Shl: |
| 4945 | case Instruction::Shr: |
| 4946 | return Constant::getNullValue(Type::UByteTy); |
| 4947 | case Instruction::And: |
| 4948 | return ConstantInt::getAllOnesValue(I->getType()); |
| 4949 | case Instruction::Mul: |
| 4950 | return ConstantInt::get(I->getType(), 1); |
| 4951 | } |
| 4952 | } |
| 4953 | |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 4954 | /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI |
| 4955 | /// have the same opcode and only one use each. Try to simplify this. |
| 4956 | Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 4957 | Instruction *FI) { |
| 4958 | if (TI->getNumOperands() == 1) { |
| 4959 | // If this is a non-volatile load or a cast from the same type, |
| 4960 | // merge. |
| 4961 | if (TI->getOpcode() == Instruction::Cast) { |
| 4962 | if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType()) |
| 4963 | return 0; |
| 4964 | } else { |
| 4965 | return 0; // unknown unary op. |
| 4966 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4967 | |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 4968 | // Fold this by inserting a select from the input values. |
| 4969 | SelectInst *NewSI = new SelectInst(SI.getCondition(), TI->getOperand(0), |
| 4970 | FI->getOperand(0), SI.getName()+".v"); |
| 4971 | InsertNewInstBefore(NewSI, SI); |
| 4972 | return new CastInst(NewSI, TI->getType()); |
| 4973 | } |
| 4974 | |
| 4975 | // Only handle binary operators here. |
| 4976 | if (!isa<ShiftInst>(TI) && !isa<BinaryOperator>(TI)) |
| 4977 | return 0; |
| 4978 | |
| 4979 | // Figure out if the operations have any operands in common. |
| 4980 | Value *MatchOp, *OtherOpT, *OtherOpF; |
| 4981 | bool MatchIsOpZero; |
| 4982 | if (TI->getOperand(0) == FI->getOperand(0)) { |
| 4983 | MatchOp = TI->getOperand(0); |
| 4984 | OtherOpT = TI->getOperand(1); |
| 4985 | OtherOpF = FI->getOperand(1); |
| 4986 | MatchIsOpZero = true; |
| 4987 | } else if (TI->getOperand(1) == FI->getOperand(1)) { |
| 4988 | MatchOp = TI->getOperand(1); |
| 4989 | OtherOpT = TI->getOperand(0); |
| 4990 | OtherOpF = FI->getOperand(0); |
| 4991 | MatchIsOpZero = false; |
| 4992 | } else if (!TI->isCommutative()) { |
| 4993 | return 0; |
| 4994 | } else if (TI->getOperand(0) == FI->getOperand(1)) { |
| 4995 | MatchOp = TI->getOperand(0); |
| 4996 | OtherOpT = TI->getOperand(1); |
| 4997 | OtherOpF = FI->getOperand(0); |
| 4998 | MatchIsOpZero = true; |
| 4999 | } else if (TI->getOperand(1) == FI->getOperand(0)) { |
| 5000 | MatchOp = TI->getOperand(1); |
| 5001 | OtherOpT = TI->getOperand(0); |
| 5002 | OtherOpF = FI->getOperand(1); |
| 5003 | MatchIsOpZero = true; |
| 5004 | } else { |
| 5005 | return 0; |
| 5006 | } |
| 5007 | |
| 5008 | // If we reach here, they do have operations in common. |
| 5009 | SelectInst *NewSI = new SelectInst(SI.getCondition(), OtherOpT, |
| 5010 | OtherOpF, SI.getName()+".v"); |
| 5011 | InsertNewInstBefore(NewSI, SI); |
| 5012 | |
| 5013 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) { |
| 5014 | if (MatchIsOpZero) |
| 5015 | return BinaryOperator::create(BO->getOpcode(), MatchOp, NewSI); |
| 5016 | else |
| 5017 | return BinaryOperator::create(BO->getOpcode(), NewSI, MatchOp); |
| 5018 | } else { |
| 5019 | if (MatchIsOpZero) |
| 5020 | return new ShiftInst(cast<ShiftInst>(TI)->getOpcode(), MatchOp, NewSI); |
| 5021 | else |
| 5022 | return new ShiftInst(cast<ShiftInst>(TI)->getOpcode(), NewSI, MatchOp); |
| 5023 | } |
| 5024 | } |
| 5025 | |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 5026 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 5027 | Value *CondVal = SI.getCondition(); |
| 5028 | Value *TrueVal = SI.getTrueValue(); |
| 5029 | Value *FalseVal = SI.getFalseValue(); |
| 5030 | |
| 5031 | // select true, X, Y -> X |
| 5032 | // select false, X, Y -> Y |
| 5033 | if (ConstantBool *C = dyn_cast<ConstantBool>(CondVal)) |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 5034 | if (C == ConstantBool::True) |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 5035 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 5036 | else { |
| 5037 | assert(C == ConstantBool::False); |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 5038 | return ReplaceInstUsesWith(SI, FalseVal); |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 5039 | } |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 5040 | |
| 5041 | // select C, X, X -> X |
| 5042 | if (TrueVal == FalseVal) |
| 5043 | return ReplaceInstUsesWith(SI, TrueVal); |
| 5044 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5045 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 5046 | return ReplaceInstUsesWith(SI, FalseVal); |
| 5047 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 5048 | return ReplaceInstUsesWith(SI, TrueVal); |
| 5049 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 5050 | if (isa<Constant>(TrueVal)) |
| 5051 | return ReplaceInstUsesWith(SI, TrueVal); |
| 5052 | else |
| 5053 | return ReplaceInstUsesWith(SI, FalseVal); |
| 5054 | } |
| 5055 | |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 5056 | if (SI.getType() == Type::BoolTy) |
| 5057 | if (ConstantBool *C = dyn_cast<ConstantBool>(TrueVal)) { |
| 5058 | if (C == ConstantBool::True) { |
| 5059 | // Change: A = select B, true, C --> A = or B, C |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 5060 | return BinaryOperator::createOr(CondVal, FalseVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 5061 | } else { |
| 5062 | // Change: A = select B, false, C --> A = and !B, C |
| 5063 | Value *NotCond = |
| 5064 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 5065 | "not."+CondVal->getName()), SI); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 5066 | return BinaryOperator::createAnd(NotCond, FalseVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 5067 | } |
| 5068 | } else if (ConstantBool *C = dyn_cast<ConstantBool>(FalseVal)) { |
| 5069 | if (C == ConstantBool::False) { |
| 5070 | // Change: A = select B, C, false --> A = and B, C |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 5071 | return BinaryOperator::createAnd(CondVal, TrueVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 5072 | } else { |
| 5073 | // Change: A = select B, C, true --> A = or !B, C |
| 5074 | Value *NotCond = |
| 5075 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 5076 | "not."+CondVal->getName()), SI); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 5077 | return BinaryOperator::createOr(NotCond, TrueVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 5078 | } |
| 5079 | } |
| 5080 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 5081 | // Selecting between two integer constants? |
| 5082 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) |
| 5083 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { |
| 5084 | // select C, 1, 0 -> cast C to int |
| 5085 | if (FalseValC->isNullValue() && TrueValC->getRawValue() == 1) { |
| 5086 | return new CastInst(CondVal, SI.getType()); |
| 5087 | } else if (TrueValC->isNullValue() && FalseValC->getRawValue() == 1) { |
| 5088 | // select C, 0, 1 -> cast !C to int |
| 5089 | Value *NotCond = |
| 5090 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
Chris Lattner | cf7baf3 | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 5091 | "not."+CondVal->getName()), SI); |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 5092 | return new CastInst(NotCond, SI.getType()); |
Chris Lattner | cf7baf3 | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 5093 | } |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 5094 | |
| 5095 | // If one of the constants is zero (we know they can't both be) and we |
| 5096 | // have a setcc instruction with zero, and we have an 'and' with the |
| 5097 | // non-constant value, eliminate this whole mess. This corresponds to |
| 5098 | // cases like this: ((X & 27) ? 27 : 0) |
| 5099 | if (TrueValC->isNullValue() || FalseValC->isNullValue()) |
| 5100 | if (Instruction *IC = dyn_cast<Instruction>(SI.getCondition())) |
| 5101 | if ((IC->getOpcode() == Instruction::SetEQ || |
| 5102 | IC->getOpcode() == Instruction::SetNE) && |
| 5103 | isa<ConstantInt>(IC->getOperand(1)) && |
| 5104 | cast<Constant>(IC->getOperand(1))->isNullValue()) |
| 5105 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) |
| 5106 | if (ICA->getOpcode() == Instruction::And && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5107 | isa<ConstantInt>(ICA->getOperand(1)) && |
| 5108 | (ICA->getOperand(1) == TrueValC || |
| 5109 | ICA->getOperand(1) == FalseValC) && |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 5110 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { |
| 5111 | // Okay, now we know that everything is set up, we just don't |
| 5112 | // know whether we have a setne or seteq and whether the true or |
| 5113 | // false val is the zero. |
| 5114 | bool ShouldNotVal = !TrueValC->isNullValue(); |
| 5115 | ShouldNotVal ^= IC->getOpcode() == Instruction::SetNE; |
| 5116 | Value *V = ICA; |
| 5117 | if (ShouldNotVal) |
| 5118 | V = InsertNewInstBefore(BinaryOperator::create( |
| 5119 | Instruction::Xor, V, ICA->getOperand(1)), SI); |
| 5120 | return ReplaceInstUsesWith(SI, V); |
| 5121 | } |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 5122 | } |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 5123 | |
| 5124 | // See if we are selecting two values based on a comparison of the two values. |
| 5125 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(CondVal)) { |
| 5126 | if (SCI->getOperand(0) == TrueVal && SCI->getOperand(1) == FalseVal) { |
| 5127 | // Transform (X == Y) ? X : Y -> Y |
| 5128 | if (SCI->getOpcode() == Instruction::SetEQ) |
| 5129 | return ReplaceInstUsesWith(SI, FalseVal); |
| 5130 | // Transform (X != Y) ? X : Y -> X |
| 5131 | if (SCI->getOpcode() == Instruction::SetNE) |
| 5132 | return ReplaceInstUsesWith(SI, TrueVal); |
| 5133 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 5134 | |
| 5135 | } else if (SCI->getOperand(0) == FalseVal && SCI->getOperand(1) == TrueVal){ |
| 5136 | // Transform (X == Y) ? Y : X -> X |
| 5137 | if (SCI->getOpcode() == Instruction::SetEQ) |
Chris Lattner | 24cf020 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 5138 | return ReplaceInstUsesWith(SI, FalseVal); |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 5139 | // Transform (X != Y) ? Y : X -> Y |
| 5140 | if (SCI->getOpcode() == Instruction::SetNE) |
Chris Lattner | 24cf020 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 5141 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 5142 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 5143 | } |
| 5144 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5145 | |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 5146 | if (Instruction *TI = dyn_cast<Instruction>(TrueVal)) |
| 5147 | if (Instruction *FI = dyn_cast<Instruction>(FalseVal)) |
| 5148 | if (TI->hasOneUse() && FI->hasOneUse()) { |
| 5149 | bool isInverse = false; |
| 5150 | Instruction *AddOp = 0, *SubOp = 0; |
| 5151 | |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 5152 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) |
| 5153 | if (TI->getOpcode() == FI->getOpcode()) |
| 5154 | if (Instruction *IV = FoldSelectOpOp(SI, TI, FI)) |
| 5155 | return IV; |
| 5156 | |
| 5157 | // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is |
| 5158 | // even legal for FP. |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 5159 | if (TI->getOpcode() == Instruction::Sub && |
| 5160 | FI->getOpcode() == Instruction::Add) { |
| 5161 | AddOp = FI; SubOp = TI; |
| 5162 | } else if (FI->getOpcode() == Instruction::Sub && |
| 5163 | TI->getOpcode() == Instruction::Add) { |
| 5164 | AddOp = TI; SubOp = FI; |
| 5165 | } |
| 5166 | |
| 5167 | if (AddOp) { |
| 5168 | Value *OtherAddOp = 0; |
| 5169 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { |
| 5170 | OtherAddOp = AddOp->getOperand(1); |
| 5171 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { |
| 5172 | OtherAddOp = AddOp->getOperand(0); |
| 5173 | } |
| 5174 | |
| 5175 | if (OtherAddOp) { |
Chris Lattner | b580d26 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 5176 | // So at this point we know we have (Y -> OtherAddOp): |
| 5177 | // select C, (add X, Y), (sub X, Z) |
| 5178 | Value *NegVal; // Compute -Z |
| 5179 | if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) { |
| 5180 | NegVal = ConstantExpr::getNeg(C); |
| 5181 | } else { |
| 5182 | NegVal = InsertNewInstBefore( |
| 5183 | BinaryOperator::createNeg(SubOp->getOperand(1), "tmp"), SI); |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 5184 | } |
Chris Lattner | b580d26 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 5185 | |
| 5186 | Value *NewTrueOp = OtherAddOp; |
| 5187 | Value *NewFalseOp = NegVal; |
| 5188 | if (AddOp != TI) |
| 5189 | std::swap(NewTrueOp, NewFalseOp); |
| 5190 | Instruction *NewSel = |
| 5191 | new SelectInst(CondVal, NewTrueOp,NewFalseOp,SI.getName()+".p"); |
| 5192 | |
| 5193 | NewSel = InsertNewInstBefore(NewSel, SI); |
| 5194 | return BinaryOperator::createAdd(SubOp->getOperand(0), NewSel); |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 5195 | } |
| 5196 | } |
| 5197 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5198 | |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 5199 | // See if we can fold the select into one of our operands. |
| 5200 | if (SI.getType()->isInteger()) { |
| 5201 | // See the comment above GetSelectFoldableOperands for a description of the |
| 5202 | // transformation we are doing here. |
| 5203 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) |
| 5204 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && |
| 5205 | !isa<Constant>(FalseVal)) |
| 5206 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { |
| 5207 | unsigned OpToFold = 0; |
| 5208 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 5209 | OpToFold = 1; |
| 5210 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
| 5211 | OpToFold = 2; |
| 5212 | } |
| 5213 | |
| 5214 | if (OpToFold) { |
| 5215 | Constant *C = GetSelectFoldableConstant(TVI); |
| 5216 | std::string Name = TVI->getName(); TVI->setName(""); |
| 5217 | Instruction *NewSel = |
| 5218 | new SelectInst(SI.getCondition(), TVI->getOperand(2-OpToFold), C, |
| 5219 | Name); |
| 5220 | InsertNewInstBefore(NewSel, SI); |
| 5221 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) |
| 5222 | return BinaryOperator::create(BO->getOpcode(), FalseVal, NewSel); |
| 5223 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(TVI)) |
| 5224 | return new ShiftInst(SI->getOpcode(), FalseVal, NewSel); |
| 5225 | else { |
| 5226 | assert(0 && "Unknown instruction!!"); |
| 5227 | } |
| 5228 | } |
| 5229 | } |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5230 | |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 5231 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) |
| 5232 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && |
| 5233 | !isa<Constant>(TrueVal)) |
| 5234 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { |
| 5235 | unsigned OpToFold = 0; |
| 5236 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 5237 | OpToFold = 1; |
| 5238 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
| 5239 | OpToFold = 2; |
| 5240 | } |
| 5241 | |
| 5242 | if (OpToFold) { |
| 5243 | Constant *C = GetSelectFoldableConstant(FVI); |
| 5244 | std::string Name = FVI->getName(); FVI->setName(""); |
| 5245 | Instruction *NewSel = |
| 5246 | new SelectInst(SI.getCondition(), C, FVI->getOperand(2-OpToFold), |
| 5247 | Name); |
| 5248 | InsertNewInstBefore(NewSel, SI); |
| 5249 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) |
| 5250 | return BinaryOperator::create(BO->getOpcode(), TrueVal, NewSel); |
| 5251 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(FVI)) |
| 5252 | return new ShiftInst(SI->getOpcode(), TrueVal, NewSel); |
| 5253 | else { |
| 5254 | assert(0 && "Unknown instruction!!"); |
| 5255 | } |
| 5256 | } |
| 5257 | } |
| 5258 | } |
Chris Lattner | d6f636a | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 5259 | |
| 5260 | if (BinaryOperator::isNot(CondVal)) { |
| 5261 | SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); |
| 5262 | SI.setOperand(1, FalseVal); |
| 5263 | SI.setOperand(2, TrueVal); |
| 5264 | return &SI; |
| 5265 | } |
| 5266 | |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 5267 | return 0; |
| 5268 | } |
| 5269 | |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 5270 | /// GetKnownAlignment - If the specified pointer has an alignment that we can |
| 5271 | /// determine, return it, otherwise return 0. |
| 5272 | static unsigned GetKnownAlignment(Value *V, TargetData *TD) { |
| 5273 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 5274 | unsigned Align = GV->getAlignment(); |
| 5275 | if (Align == 0 && TD) |
| 5276 | Align = TD->getTypeAlignment(GV->getType()->getElementType()); |
| 5277 | return Align; |
| 5278 | } else if (AllocationInst *AI = dyn_cast<AllocationInst>(V)) { |
| 5279 | unsigned Align = AI->getAlignment(); |
| 5280 | if (Align == 0 && TD) { |
| 5281 | if (isa<AllocaInst>(AI)) |
| 5282 | Align = TD->getTypeAlignment(AI->getType()->getElementType()); |
| 5283 | else if (isa<MallocInst>(AI)) { |
| 5284 | // Malloc returns maximally aligned memory. |
| 5285 | Align = TD->getTypeAlignment(AI->getType()->getElementType()); |
| 5286 | Align = std::max(Align, (unsigned)TD->getTypeAlignment(Type::DoubleTy)); |
| 5287 | Align = std::max(Align, (unsigned)TD->getTypeAlignment(Type::LongTy)); |
| 5288 | } |
| 5289 | } |
| 5290 | return Align; |
Chris Lattner | 53ef5a0 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 5291 | } else if (isa<CastInst>(V) || |
| 5292 | (isa<ConstantExpr>(V) && |
| 5293 | cast<ConstantExpr>(V)->getOpcode() == Instruction::Cast)) { |
| 5294 | User *CI = cast<User>(V); |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 5295 | if (isa<PointerType>(CI->getOperand(0)->getType())) |
| 5296 | return GetKnownAlignment(CI->getOperand(0), TD); |
| 5297 | return 0; |
Chris Lattner | 53ef5a0 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 5298 | } else if (isa<GetElementPtrInst>(V) || |
| 5299 | (isa<ConstantExpr>(V) && |
| 5300 | cast<ConstantExpr>(V)->getOpcode()==Instruction::GetElementPtr)) { |
| 5301 | User *GEPI = cast<User>(V); |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 5302 | unsigned BaseAlignment = GetKnownAlignment(GEPI->getOperand(0), TD); |
| 5303 | if (BaseAlignment == 0) return 0; |
| 5304 | |
| 5305 | // If all indexes are zero, it is just the alignment of the base pointer. |
| 5306 | bool AllZeroOperands = true; |
| 5307 | for (unsigned i = 1, e = GEPI->getNumOperands(); i != e; ++i) |
| 5308 | if (!isa<Constant>(GEPI->getOperand(i)) || |
| 5309 | !cast<Constant>(GEPI->getOperand(i))->isNullValue()) { |
| 5310 | AllZeroOperands = false; |
| 5311 | break; |
| 5312 | } |
| 5313 | if (AllZeroOperands) |
| 5314 | return BaseAlignment; |
| 5315 | |
| 5316 | // Otherwise, if the base alignment is >= the alignment we expect for the |
| 5317 | // base pointer type, then we know that the resultant pointer is aligned at |
| 5318 | // least as much as its type requires. |
| 5319 | if (!TD) return 0; |
| 5320 | |
| 5321 | const Type *BasePtrTy = GEPI->getOperand(0)->getType(); |
| 5322 | if (TD->getTypeAlignment(cast<PointerType>(BasePtrTy)->getElementType()) |
Chris Lattner | 53ef5a0 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 5323 | <= BaseAlignment) { |
| 5324 | const Type *GEPTy = GEPI->getType(); |
| 5325 | return TD->getTypeAlignment(cast<PointerType>(GEPTy)->getElementType()); |
| 5326 | } |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 5327 | return 0; |
| 5328 | } |
| 5329 | return 0; |
| 5330 | } |
| 5331 | |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 5332 | |
Chris Lattner | c66b223 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 5333 | /// visitCallInst - CallInst simplification. This mostly only handles folding |
| 5334 | /// of intrinsic instructions. For normal calls, it allows visitCallSite to do |
| 5335 | /// the heavy lifting. |
| 5336 | /// |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 5337 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Chris Lattner | c66b223 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 5338 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI); |
| 5339 | if (!II) return visitCallSite(&CI); |
| 5340 | |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 5341 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 5342 | // visitCallSite. |
Chris Lattner | c66b223 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 5343 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) { |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 5344 | bool Changed = false; |
| 5345 | |
| 5346 | // memmove/cpy/set of zero bytes is a noop. |
| 5347 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { |
| 5348 | if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); |
| 5349 | |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 5350 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
| 5351 | if (CI->getRawValue() == 1) { |
| 5352 | // Replace the instruction with just byte operations. We would |
| 5353 | // transform other cases to loads/stores, but we don't know if |
| 5354 | // alignment is sufficient. |
| 5355 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 5356 | } |
| 5357 | |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 5358 | // If we have a memmove and the source operation is a constant global, |
| 5359 | // then the source and dest pointers can't alias, so we can change this |
| 5360 | // into a call to memcpy. |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 5361 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(II)) { |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 5362 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
| 5363 | if (GVSrc->isConstant()) { |
| 5364 | Module *M = CI.getParent()->getParent()->getParent(); |
Chris Lattner | 681ef2f | 2006-03-03 01:34:17 +0000 | [diff] [blame] | 5365 | const char *Name; |
| 5366 | if (CI.getCalledFunction()->getFunctionType()->getParamType(3) == |
| 5367 | Type::UIntTy) |
| 5368 | Name = "llvm.memcpy.i32"; |
| 5369 | else |
| 5370 | Name = "llvm.memcpy.i64"; |
| 5371 | Function *MemCpy = M->getOrInsertFunction(Name, |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 5372 | CI.getCalledFunction()->getFunctionType()); |
| 5373 | CI.setOperand(0, MemCpy); |
| 5374 | Changed = true; |
| 5375 | } |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 5376 | } |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 5377 | |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 5378 | // If we can determine a pointer alignment that is bigger than currently |
| 5379 | // set, update the alignment. |
| 5380 | if (isa<MemCpyInst>(MI) || isa<MemMoveInst>(MI)) { |
| 5381 | unsigned Alignment1 = GetKnownAlignment(MI->getOperand(1), TD); |
| 5382 | unsigned Alignment2 = GetKnownAlignment(MI->getOperand(2), TD); |
| 5383 | unsigned Align = std::min(Alignment1, Alignment2); |
| 5384 | if (MI->getAlignment()->getRawValue() < Align) { |
| 5385 | MI->setAlignment(ConstantUInt::get(Type::UIntTy, Align)); |
| 5386 | Changed = true; |
| 5387 | } |
| 5388 | } else if (isa<MemSetInst>(MI)) { |
| 5389 | unsigned Alignment = GetKnownAlignment(MI->getDest(), TD); |
| 5390 | if (MI->getAlignment()->getRawValue() < Alignment) { |
| 5391 | MI->setAlignment(ConstantUInt::get(Type::UIntTy, Alignment)); |
| 5392 | Changed = true; |
| 5393 | } |
| 5394 | } |
| 5395 | |
Chris Lattner | c66b223 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 5396 | if (Changed) return II; |
Chris Lattner | 503221f | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 5397 | } else { |
| 5398 | switch (II->getIntrinsicID()) { |
| 5399 | default: break; |
| 5400 | case Intrinsic::stackrestore: { |
| 5401 | // If the save is right next to the restore, remove the restore. This can |
| 5402 | // happen when variable allocas are DCE'd. |
| 5403 | if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) { |
| 5404 | if (SS->getIntrinsicID() == Intrinsic::stacksave) { |
| 5405 | BasicBlock::iterator BI = SS; |
| 5406 | if (&*++BI == II) |
| 5407 | return EraseInstFromFunction(CI); |
| 5408 | } |
| 5409 | } |
| 5410 | |
| 5411 | // If the stack restore is in a return/unwind block and if there are no |
| 5412 | // allocas or calls between the restore and the return, nuke the restore. |
| 5413 | TerminatorInst *TI = II->getParent()->getTerminator(); |
| 5414 | if (isa<ReturnInst>(TI) || isa<UnwindInst>(TI)) { |
| 5415 | BasicBlock::iterator BI = II; |
| 5416 | bool CannotRemove = false; |
| 5417 | for (++BI; &*BI != TI; ++BI) { |
| 5418 | if (isa<AllocaInst>(BI) || |
| 5419 | (isa<CallInst>(BI) && !isa<IntrinsicInst>(BI))) { |
| 5420 | CannotRemove = true; |
| 5421 | break; |
| 5422 | } |
| 5423 | } |
| 5424 | if (!CannotRemove) |
| 5425 | return EraseInstFromFunction(CI); |
| 5426 | } |
| 5427 | break; |
| 5428 | } |
| 5429 | } |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 5430 | } |
| 5431 | |
Chris Lattner | c66b223 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 5432 | return visitCallSite(II); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 5433 | } |
| 5434 | |
| 5435 | // InvokeInst simplification |
| 5436 | // |
| 5437 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 5438 | return visitCallSite(&II); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 5439 | } |
| 5440 | |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 5441 | // visitCallSite - Improvements for call and invoke instructions. |
| 5442 | // |
| 5443 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 5444 | bool Changed = false; |
| 5445 | |
| 5446 | // If the callee is a constexpr cast of a function, attempt to move the cast |
| 5447 | // to the arguments of the call/invoke. |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 5448 | if (transformConstExprCastCall(CS)) return 0; |
| 5449 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 5450 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5451 | |
Chris Lattner | 61d9d81 | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 5452 | if (Function *CalleeF = dyn_cast<Function>(Callee)) |
| 5453 | if (CalleeF->getCallingConv() != CS.getCallingConv()) { |
| 5454 | Instruction *OldCall = CS.getInstruction(); |
| 5455 | // If the call and callee calling conventions don't match, this call must |
| 5456 | // be unreachable, as the call is undefined. |
| 5457 | new StoreInst(ConstantBool::True, |
| 5458 | UndefValue::get(PointerType::get(Type::BoolTy)), OldCall); |
| 5459 | if (!OldCall->use_empty()) |
| 5460 | OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); |
| 5461 | if (isa<CallInst>(OldCall)) // Not worth removing an invoke here. |
| 5462 | return EraseInstFromFunction(*OldCall); |
| 5463 | return 0; |
| 5464 | } |
| 5465 | |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 5466 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
| 5467 | // This instruction is not reachable, just remove it. We insert a store to |
| 5468 | // undef so that we know that this code is not reachable, despite the fact |
| 5469 | // that we can't modify the CFG here. |
| 5470 | new StoreInst(ConstantBool::True, |
| 5471 | UndefValue::get(PointerType::get(Type::BoolTy)), |
| 5472 | CS.getInstruction()); |
| 5473 | |
| 5474 | if (!CS.getInstruction()->use_empty()) |
| 5475 | CS.getInstruction()-> |
| 5476 | replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); |
| 5477 | |
| 5478 | if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { |
| 5479 | // Don't break the CFG, insert a dummy cond branch. |
| 5480 | new BranchInst(II->getNormalDest(), II->getUnwindDest(), |
| 5481 | ConstantBool::True, II); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5482 | } |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 5483 | return EraseInstFromFunction(*CS.getInstruction()); |
| 5484 | } |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5485 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 5486 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 5487 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 5488 | if (FTy->isVarArg()) { |
| 5489 | // See if we can optimize any arguments passed through the varargs area of |
| 5490 | // the call. |
| 5491 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
| 5492 | E = CS.arg_end(); I != E; ++I) |
| 5493 | if (CastInst *CI = dyn_cast<CastInst>(*I)) { |
| 5494 | // If this cast does not effect the value passed through the varargs |
| 5495 | // area, we can eliminate the use of the cast. |
| 5496 | Value *Op = CI->getOperand(0); |
| 5497 | if (CI->getType()->isLosslesslyConvertibleTo(Op->getType())) { |
| 5498 | *I = Op; |
| 5499 | Changed = true; |
| 5500 | } |
| 5501 | } |
| 5502 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5503 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 5504 | return Changed ? CS.getInstruction() : 0; |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 5505 | } |
| 5506 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 5507 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 5508 | // attempt to move the cast to the arguments of the call/invoke. |
| 5509 | // |
| 5510 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 5511 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 5512 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
Chris Lattner | f3edc49 | 2004-07-18 18:59:44 +0000 | [diff] [blame] | 5513 | if (CE->getOpcode() != Instruction::Cast || !isa<Function>(CE->getOperand(0))) |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 5514 | return false; |
Reid Spencer | 8743687 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 5515 | Function *Callee = cast<Function>(CE->getOperand(0)); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 5516 | Instruction *Caller = CS.getInstruction(); |
| 5517 | |
| 5518 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 5519 | // would cause a type conversion of one of our arguments, change this call to |
| 5520 | // be a direct call with arguments casted to the appropriate types. |
| 5521 | // |
| 5522 | const FunctionType *FT = Callee->getFunctionType(); |
| 5523 | const Type *OldRetTy = Caller->getType(); |
| 5524 | |
Chris Lattner | 1f7942f | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 5525 | // Check to see if we are changing the return type... |
| 5526 | if (OldRetTy != FT->getReturnType()) { |
| 5527 | if (Callee->isExternal() && |
| 5528 | !OldRetTy->isLosslesslyConvertibleTo(FT->getReturnType()) && |
| 5529 | !Caller->use_empty()) |
| 5530 | return false; // Cannot transform this return value... |
| 5531 | |
| 5532 | // If the callsite is an invoke instruction, and the return value is used by |
| 5533 | // a PHI node in a successor, we cannot change the return type of the call |
| 5534 | // because there is no place to put the cast instruction (without breaking |
| 5535 | // the critical edge). Bail out in this case. |
| 5536 | if (!Caller->use_empty()) |
| 5537 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 5538 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 5539 | UI != E; ++UI) |
| 5540 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 5541 | if (PN->getParent() == II->getNormalDest() || |
Chris Lattner | fae8ab3 | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 5542 | PN->getParent() == II->getUnwindDest()) |
Chris Lattner | 1f7942f | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 5543 | return false; |
| 5544 | } |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 5545 | |
| 5546 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 5547 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5548 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 5549 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 5550 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 5551 | const Type *ParamTy = FT->getParamType(i); |
| 5552 | bool isConvertible = (*AI)->getType()->isLosslesslyConvertibleTo(ParamTy); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5553 | if (Callee->isExternal() && !isConvertible) return false; |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 5554 | } |
| 5555 | |
| 5556 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
| 5557 | Callee->isExternal()) |
| 5558 | return false; // Do not delete arguments unless we have a function body... |
| 5559 | |
| 5560 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 5561 | // inserting cast instructions as necessary... |
| 5562 | std::vector<Value*> Args; |
| 5563 | Args.reserve(NumActualArgs); |
| 5564 | |
| 5565 | AI = CS.arg_begin(); |
| 5566 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 5567 | const Type *ParamTy = FT->getParamType(i); |
| 5568 | if ((*AI)->getType() == ParamTy) { |
| 5569 | Args.push_back(*AI); |
| 5570 | } else { |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 5571 | Args.push_back(InsertNewInstBefore(new CastInst(*AI, ParamTy, "tmp"), |
| 5572 | *Caller)); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 5573 | } |
| 5574 | } |
| 5575 | |
| 5576 | // If the function takes more arguments than the call was taking, add them |
| 5577 | // now... |
| 5578 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 5579 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 5580 | |
| 5581 | // If we are removing arguments to the function, emit an obnoxious warning... |
| 5582 | if (FT->getNumParams() < NumActualArgs) |
| 5583 | if (!FT->isVarArg()) { |
| 5584 | std::cerr << "WARNING: While resolving call to function '" |
| 5585 | << Callee->getName() << "' arguments were dropped!\n"; |
| 5586 | } else { |
| 5587 | // Add all of the arguments in their promoted form to the arg list... |
| 5588 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 5589 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 5590 | if (PTy != (*AI)->getType()) { |
| 5591 | // Must promote to pass through va_arg area! |
| 5592 | Instruction *Cast = new CastInst(*AI, PTy, "tmp"); |
| 5593 | InsertNewInstBefore(Cast, *Caller); |
| 5594 | Args.push_back(Cast); |
| 5595 | } else { |
| 5596 | Args.push_back(*AI); |
| 5597 | } |
| 5598 | } |
| 5599 | } |
| 5600 | |
| 5601 | if (FT->getReturnType() == Type::VoidTy) |
| 5602 | Caller->setName(""); // Void type should not have a name... |
| 5603 | |
| 5604 | Instruction *NC; |
| 5605 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Chris Lattner | fae8ab3 | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 5606 | NC = new InvokeInst(Callee, II->getNormalDest(), II->getUnwindDest(), |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 5607 | Args, Caller->getName(), Caller); |
Chris Lattner | 05c703e | 2005-05-14 12:25:32 +0000 | [diff] [blame] | 5608 | cast<InvokeInst>(II)->setCallingConv(II->getCallingConv()); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 5609 | } else { |
| 5610 | NC = new CallInst(Callee, Args, Caller->getName(), Caller); |
Chris Lattner | 6aacb0f | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 5611 | if (cast<CallInst>(Caller)->isTailCall()) |
| 5612 | cast<CallInst>(NC)->setTailCall(); |
Chris Lattner | 05c703e | 2005-05-14 12:25:32 +0000 | [diff] [blame] | 5613 | cast<CallInst>(NC)->setCallingConv(cast<CallInst>(Caller)->getCallingConv()); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 5614 | } |
| 5615 | |
| 5616 | // Insert a cast of the return type as necessary... |
| 5617 | Value *NV = NC; |
| 5618 | if (Caller->getType() != NV->getType() && !Caller->use_empty()) { |
| 5619 | if (NV->getType() != Type::VoidTy) { |
| 5620 | NV = NC = new CastInst(NC, Caller->getType(), "tmp"); |
Chris Lattner | 686767f | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 5621 | |
| 5622 | // If this is an invoke instruction, we should insert it after the first |
| 5623 | // non-phi, instruction in the normal successor block. |
| 5624 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 5625 | BasicBlock::iterator I = II->getNormalDest()->begin(); |
| 5626 | while (isa<PHINode>(I)) ++I; |
| 5627 | InsertNewInstBefore(NC, *I); |
| 5628 | } else { |
| 5629 | // Otherwise, it's a call, just insert cast right after the call instr |
| 5630 | InsertNewInstBefore(NC, *Caller); |
| 5631 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 5632 | AddUsersToWorkList(*Caller); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 5633 | } else { |
Chris Lattner | e29d634 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 5634 | NV = UndefValue::get(Caller->getType()); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 5635 | } |
| 5636 | } |
| 5637 | |
| 5638 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 5639 | Caller->replaceAllUsesWith(NV); |
| 5640 | Caller->getParent()->getInstList().erase(Caller); |
| 5641 | removeFromWorkList(Caller); |
| 5642 | return true; |
| 5643 | } |
| 5644 | |
| 5645 | |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 5646 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 5647 | // operator and they all are only used by the PHI, PHI together their |
| 5648 | // inputs, and do the operation once, to the result of the PHI. |
| 5649 | Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { |
| 5650 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
| 5651 | |
| 5652 | // Scan the instruction, looking for input operations that can be folded away. |
| 5653 | // If all input operands to the phi are the same instruction (e.g. a cast from |
| 5654 | // the same type or "+42") we can pull the operation through the PHI, reducing |
| 5655 | // code size and simplifying code. |
| 5656 | Constant *ConstantOp = 0; |
| 5657 | const Type *CastSrcTy = 0; |
| 5658 | if (isa<CastInst>(FirstInst)) { |
| 5659 | CastSrcTy = FirstInst->getOperand(0)->getType(); |
| 5660 | } else if (isa<BinaryOperator>(FirstInst) || isa<ShiftInst>(FirstInst)) { |
| 5661 | // Can fold binop or shift if the RHS is a constant. |
| 5662 | ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1)); |
| 5663 | if (ConstantOp == 0) return 0; |
| 5664 | } else { |
| 5665 | return 0; // Cannot fold this operation. |
| 5666 | } |
| 5667 | |
| 5668 | // Check to see if all arguments are the same operation. |
| 5669 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 5670 | if (!isa<Instruction>(PN.getIncomingValue(i))) return 0; |
| 5671 | Instruction *I = cast<Instruction>(PN.getIncomingValue(i)); |
| 5672 | if (!I->hasOneUse() || I->getOpcode() != FirstInst->getOpcode()) |
| 5673 | return 0; |
| 5674 | if (CastSrcTy) { |
| 5675 | if (I->getOperand(0)->getType() != CastSrcTy) |
| 5676 | return 0; // Cast operation must match. |
| 5677 | } else if (I->getOperand(1) != ConstantOp) { |
| 5678 | return 0; |
| 5679 | } |
| 5680 | } |
| 5681 | |
| 5682 | // Okay, they are all the same operation. Create a new PHI node of the |
| 5683 | // correct type, and PHI together all of the LHS's of the instructions. |
| 5684 | PHINode *NewPN = new PHINode(FirstInst->getOperand(0)->getType(), |
| 5685 | PN.getName()+".in"); |
Chris Lattner | d8e2018 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 5686 | NewPN->reserveOperandSpace(PN.getNumOperands()/2); |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 5687 | |
| 5688 | Value *InVal = FirstInst->getOperand(0); |
| 5689 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 5690 | |
| 5691 | // Add all operands to the new PHI. |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 5692 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 5693 | Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 5694 | if (NewInVal != InVal) |
| 5695 | InVal = 0; |
| 5696 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); |
| 5697 | } |
| 5698 | |
| 5699 | Value *PhiVal; |
| 5700 | if (InVal) { |
| 5701 | // The new PHI unions all of the same values together. This is really |
| 5702 | // common, so we handle it intelligently here for compile-time speed. |
| 5703 | PhiVal = InVal; |
| 5704 | delete NewPN; |
| 5705 | } else { |
| 5706 | InsertNewInstBefore(NewPN, PN); |
| 5707 | PhiVal = NewPN; |
| 5708 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5709 | |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 5710 | // Insert and return the new operation. |
| 5711 | if (isa<CastInst>(FirstInst)) |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 5712 | return new CastInst(PhiVal, PN.getType()); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 5713 | else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 5714 | return BinaryOperator::create(BinOp->getOpcode(), PhiVal, ConstantOp); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 5715 | else |
| 5716 | return new ShiftInst(cast<ShiftInst>(FirstInst)->getOpcode(), |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 5717 | PhiVal, ConstantOp); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 5718 | } |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 5719 | |
Chris Lattner | 7153643 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 5720 | /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle |
| 5721 | /// that is dead. |
| 5722 | static bool DeadPHICycle(PHINode *PN, std::set<PHINode*> &PotentiallyDeadPHIs) { |
| 5723 | if (PN->use_empty()) return true; |
| 5724 | if (!PN->hasOneUse()) return false; |
| 5725 | |
| 5726 | // Remember this node, and if we find the cycle, return. |
| 5727 | if (!PotentiallyDeadPHIs.insert(PN).second) |
| 5728 | return true; |
| 5729 | |
| 5730 | if (PHINode *PU = dyn_cast<PHINode>(PN->use_back())) |
| 5731 | return DeadPHICycle(PU, PotentiallyDeadPHIs); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5732 | |
Chris Lattner | 7153643 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 5733 | return false; |
| 5734 | } |
| 5735 | |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 5736 | // PHINode simplification |
| 5737 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5738 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Chris Lattner | 9f9c260 | 2005-08-05 01:04:30 +0000 | [diff] [blame] | 5739 | if (Value *V = PN.hasConstantValue()) |
| 5740 | return ReplaceInstUsesWith(PN, V); |
Chris Lattner | 4db2d22 | 2004-02-16 05:07:08 +0000 | [diff] [blame] | 5741 | |
| 5742 | // If the only user of this instruction is a cast instruction, and all of the |
| 5743 | // incoming values are constants, change this PHI to merge together the casted |
| 5744 | // constants. |
| 5745 | if (PN.hasOneUse()) |
| 5746 | if (CastInst *CI = dyn_cast<CastInst>(PN.use_back())) |
| 5747 | if (CI->getType() != PN.getType()) { // noop casts will be folded |
| 5748 | bool AllConstant = true; |
| 5749 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 5750 | if (!isa<Constant>(PN.getIncomingValue(i))) { |
| 5751 | AllConstant = false; |
| 5752 | break; |
| 5753 | } |
| 5754 | if (AllConstant) { |
| 5755 | // Make a new PHI with all casted values. |
| 5756 | PHINode *New = new PHINode(CI->getType(), PN.getName(), &PN); |
| 5757 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 5758 | Constant *OldArg = cast<Constant>(PN.getIncomingValue(i)); |
| 5759 | New->addIncoming(ConstantExpr::getCast(OldArg, New->getType()), |
| 5760 | PN.getIncomingBlock(i)); |
| 5761 | } |
| 5762 | |
| 5763 | // Update the cast instruction. |
| 5764 | CI->setOperand(0, New); |
| 5765 | WorkList.push_back(CI); // revisit the cast instruction to fold. |
| 5766 | WorkList.push_back(New); // Make sure to revisit the new Phi |
| 5767 | return &PN; // PN is now dead! |
| 5768 | } |
| 5769 | } |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 5770 | |
| 5771 | // If all PHI operands are the same operation, pull them through the PHI, |
| 5772 | // reducing code size. |
| 5773 | if (isa<Instruction>(PN.getIncomingValue(0)) && |
| 5774 | PN.getIncomingValue(0)->hasOneUse()) |
| 5775 | if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) |
| 5776 | return Result; |
| 5777 | |
Chris Lattner | 7153643 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 5778 | // If this is a trivial cycle in the PHI node graph, remove it. Basically, if |
| 5779 | // this PHI only has a single use (a PHI), and if that PHI only has one use (a |
| 5780 | // PHI)... break the cycle. |
| 5781 | if (PN.hasOneUse()) |
| 5782 | if (PHINode *PU = dyn_cast<PHINode>(PN.use_back())) { |
| 5783 | std::set<PHINode*> PotentiallyDeadPHIs; |
| 5784 | PotentiallyDeadPHIs.insert(&PN); |
| 5785 | if (DeadPHICycle(PU, PotentiallyDeadPHIs)) |
| 5786 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 5787 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5788 | |
Chris Lattner | 91daeb5 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 5789 | return 0; |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 5790 | } |
| 5791 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 5792 | static Value *InsertSignExtendToPtrTy(Value *V, const Type *DTy, |
| 5793 | Instruction *InsertPoint, |
| 5794 | InstCombiner *IC) { |
| 5795 | unsigned PS = IC->getTargetData().getPointerSize(); |
| 5796 | const Type *VTy = V->getType(); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 5797 | if (!VTy->isSigned() && VTy->getPrimitiveSize() < PS) |
| 5798 | // We must insert a cast to ensure we sign-extend. |
| 5799 | V = IC->InsertNewInstBefore(new CastInst(V, VTy->getSignedVersion(), |
| 5800 | V->getName()), *InsertPoint); |
| 5801 | return IC->InsertNewInstBefore(new CastInst(V, DTy, V->getName()), |
| 5802 | *InsertPoint); |
| 5803 | } |
| 5804 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 5805 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5806 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 5807 | Value *PtrOp = GEP.getOperand(0); |
Chris Lattner | 471bd76 | 2003-05-22 19:07:21 +0000 | [diff] [blame] | 5808 | // Is it 'getelementptr %P, long 0' or 'getelementptr %P' |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5809 | // If so, eliminate the noop. |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 5810 | if (GEP.getNumOperands() == 1) |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 5811 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 5812 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5813 | if (isa<UndefValue>(GEP.getOperand(0))) |
| 5814 | return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType())); |
| 5815 | |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 5816 | bool HasZeroPointerIndex = false; |
| 5817 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) |
| 5818 | HasZeroPointerIndex = C->isNullValue(); |
| 5819 | |
| 5820 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 5821 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 5822 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 5823 | // Eliminate unneeded casts for indices. |
| 5824 | bool MadeChange = false; |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 5825 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 5826 | for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI) |
| 5827 | if (isa<SequentialType>(*GTI)) { |
| 5828 | if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) { |
| 5829 | Value *Src = CI->getOperand(0); |
| 5830 | const Type *SrcTy = Src->getType(); |
| 5831 | const Type *DestTy = CI->getType(); |
| 5832 | if (Src->getType()->isInteger()) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5833 | if (SrcTy->getPrimitiveSizeInBits() == |
| 5834 | DestTy->getPrimitiveSizeInBits()) { |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 5835 | // We can always eliminate a cast from ulong or long to the other. |
| 5836 | // We can always eliminate a cast from uint to int or the other on |
| 5837 | // 32-bit pointer platforms. |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5838 | if (DestTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()){ |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 5839 | MadeChange = true; |
| 5840 | GEP.setOperand(i, Src); |
| 5841 | } |
| 5842 | } else if (SrcTy->getPrimitiveSize() < DestTy->getPrimitiveSize() && |
| 5843 | SrcTy->getPrimitiveSize() == 4) { |
| 5844 | // We can always eliminate a cast from int to [u]long. We can |
| 5845 | // eliminate a cast from uint to [u]long iff the target is a 32-bit |
| 5846 | // pointer target. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5847 | if (SrcTy->isSigned() || |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5848 | SrcTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()) { |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 5849 | MadeChange = true; |
| 5850 | GEP.setOperand(i, Src); |
| 5851 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 5852 | } |
| 5853 | } |
| 5854 | } |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 5855 | // If we are using a wider index than needed for this platform, shrink it |
| 5856 | // to what we need. If the incoming value needs a cast instruction, |
| 5857 | // insert it. This explicit cast can make subsequent optimizations more |
| 5858 | // obvious. |
| 5859 | Value *Op = GEP.getOperand(i); |
| 5860 | if (Op->getType()->getPrimitiveSize() > TD->getPointerSize()) |
Chris Lattner | 1e9ac1a | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 5861 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | 44d0b95 | 2004-07-20 01:48:15 +0000 | [diff] [blame] | 5862 | GEP.setOperand(i, ConstantExpr::getCast(C, |
| 5863 | TD->getIntPtrType()->getSignedVersion())); |
Chris Lattner | 1e9ac1a | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 5864 | MadeChange = true; |
| 5865 | } else { |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 5866 | Op = InsertNewInstBefore(new CastInst(Op, TD->getIntPtrType(), |
| 5867 | Op->getName()), GEP); |
| 5868 | GEP.setOperand(i, Op); |
| 5869 | MadeChange = true; |
| 5870 | } |
Chris Lattner | 44d0b95 | 2004-07-20 01:48:15 +0000 | [diff] [blame] | 5871 | |
| 5872 | // If this is a constant idx, make sure to canonicalize it to be a signed |
| 5873 | // operand, otherwise CSE and other optimizations are pessimized. |
| 5874 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op)) { |
| 5875 | GEP.setOperand(i, ConstantExpr::getCast(CUI, |
| 5876 | CUI->getType()->getSignedVersion())); |
| 5877 | MadeChange = true; |
| 5878 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 5879 | } |
| 5880 | if (MadeChange) return &GEP; |
| 5881 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 5882 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 5883 | // is a getelementptr instruction, combine the indices of the two |
| 5884 | // getelementptr instructions into a single instruction. |
| 5885 | // |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 5886 | std::vector<Value*> SrcGEPOperands; |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5887 | if (User *Src = dyn_castGetElementPtr(PtrOp)) |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 5888 | SrcGEPOperands.assign(Src->op_begin(), Src->op_end()); |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 5889 | |
| 5890 | if (!SrcGEPOperands.empty()) { |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 5891 | // Note that if our source is a gep chain itself that we wait for that |
| 5892 | // chain to be resolved before we perform this transformation. This |
| 5893 | // avoids us creating a TON of code in some cases. |
| 5894 | // |
| 5895 | if (isa<GetElementPtrInst>(SrcGEPOperands[0]) && |
| 5896 | cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2) |
| 5897 | return 0; // Wait until our source is folded to completion. |
| 5898 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 5899 | std::vector<Value *> Indices; |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 5900 | |
| 5901 | // Find out whether the last index in the source GEP is a sequential idx. |
| 5902 | bool EndsWithSequential = false; |
| 5903 | for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)), |
| 5904 | E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I) |
Chris Lattner | 8ec5f88 | 2004-05-08 22:41:42 +0000 | [diff] [blame] | 5905 | EndsWithSequential = !isa<StructType>(*I); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5906 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 5907 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 5908 | if (EndsWithSequential) { |
Chris Lattner | 235af56 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 5909 | // Replace: gep (gep %P, long B), long A, ... |
| 5910 | // With: T = long A+B; gep %P, T, ... |
| 5911 | // |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 5912 | Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 5913 | if (SO1 == Constant::getNullValue(SO1->getType())) { |
| 5914 | Sum = GO1; |
| 5915 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { |
| 5916 | Sum = SO1; |
| 5917 | } else { |
| 5918 | // If they aren't the same type, convert both to an integer of the |
| 5919 | // target's pointer size. |
| 5920 | if (SO1->getType() != GO1->getType()) { |
| 5921 | if (Constant *SO1C = dyn_cast<Constant>(SO1)) { |
| 5922 | SO1 = ConstantExpr::getCast(SO1C, GO1->getType()); |
| 5923 | } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) { |
| 5924 | GO1 = ConstantExpr::getCast(GO1C, SO1->getType()); |
| 5925 | } else { |
| 5926 | unsigned PS = TD->getPointerSize(); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 5927 | if (SO1->getType()->getPrimitiveSize() == PS) { |
| 5928 | // Convert GO1 to SO1's type. |
| 5929 | GO1 = InsertSignExtendToPtrTy(GO1, SO1->getType(), &GEP, this); |
| 5930 | |
| 5931 | } else if (GO1->getType()->getPrimitiveSize() == PS) { |
| 5932 | // Convert SO1 to GO1's type. |
| 5933 | SO1 = InsertSignExtendToPtrTy(SO1, GO1->getType(), &GEP, this); |
| 5934 | } else { |
| 5935 | const Type *PT = TD->getIntPtrType(); |
| 5936 | SO1 = InsertSignExtendToPtrTy(SO1, PT, &GEP, this); |
| 5937 | GO1 = InsertSignExtendToPtrTy(GO1, PT, &GEP, this); |
| 5938 | } |
| 5939 | } |
| 5940 | } |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 5941 | if (isa<Constant>(SO1) && isa<Constant>(GO1)) |
| 5942 | Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1)); |
| 5943 | else { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 5944 | Sum = BinaryOperator::createAdd(SO1, GO1, PtrOp->getName()+".sum"); |
| 5945 | InsertNewInstBefore(cast<Instruction>(Sum), GEP); |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 5946 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 5947 | } |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 5948 | |
| 5949 | // Recycle the GEP we already have if possible. |
| 5950 | if (SrcGEPOperands.size() == 2) { |
| 5951 | GEP.setOperand(0, SrcGEPOperands[0]); |
| 5952 | GEP.setOperand(1, Sum); |
| 5953 | return &GEP; |
| 5954 | } else { |
| 5955 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 5956 | SrcGEPOperands.end()-1); |
| 5957 | Indices.push_back(Sum); |
| 5958 | Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end()); |
| 5959 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5960 | } else if (isa<Constant>(*GEP.idx_begin()) && |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 5961 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5962 | SrcGEPOperands.size() != 1) { |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 5963 | // 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] | 5964 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 5965 | SrcGEPOperands.end()); |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 5966 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
| 5967 | } |
| 5968 | |
| 5969 | if (!Indices.empty()) |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 5970 | return new GetElementPtrInst(SrcGEPOperands[0], Indices, GEP.getName()); |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 5971 | |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 5972 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) { |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 5973 | // GEP of global variable. If all of the indices for this GEP are |
| 5974 | // constants, we can promote this to a constexpr instead of an instruction. |
| 5975 | |
| 5976 | // Scan for nonconstants... |
| 5977 | std::vector<Constant*> Indices; |
| 5978 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
| 5979 | for (; I != E && isa<Constant>(*I); ++I) |
| 5980 | Indices.push_back(cast<Constant>(*I)); |
| 5981 | |
| 5982 | if (I == E) { // If they are all constants... |
Chris Lattner | f3edc49 | 2004-07-18 18:59:44 +0000 | [diff] [blame] | 5983 | Constant *CE = ConstantExpr::getGetElementPtr(GV, Indices); |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 5984 | |
| 5985 | // Replace all uses of the GEP with the new constexpr... |
| 5986 | return ReplaceInstUsesWith(GEP, CE); |
| 5987 | } |
Chris Lattner | 567b81f | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 5988 | } else if (Value *X = isCast(PtrOp)) { // Is the operand a cast? |
| 5989 | if (!isa<PointerType>(X->getType())) { |
| 5990 | // Not interesting. Source pointer must be a cast from pointer. |
| 5991 | } else if (HasZeroPointerIndex) { |
| 5992 | // transform: GEP (cast [10 x ubyte]* X to [0 x ubyte]*), long 0, ... |
| 5993 | // into : GEP [10 x ubyte]* X, long 0, ... |
| 5994 | // |
| 5995 | // This occurs when the program declares an array extern like "int X[];" |
| 5996 | // |
| 5997 | const PointerType *CPTy = cast<PointerType>(PtrOp->getType()); |
| 5998 | const PointerType *XTy = cast<PointerType>(X->getType()); |
| 5999 | if (const ArrayType *XATy = |
| 6000 | dyn_cast<ArrayType>(XTy->getElementType())) |
| 6001 | if (const ArrayType *CATy = |
| 6002 | dyn_cast<ArrayType>(CPTy->getElementType())) |
| 6003 | if (CATy->getElementType() == XATy->getElementType()) { |
| 6004 | // At this point, we know that the cast source type is a pointer |
| 6005 | // to an array of the same type as the destination pointer |
| 6006 | // array. Because the array type is never stepped over (there |
| 6007 | // is a leading zero) we can fold the cast into this GEP. |
| 6008 | GEP.setOperand(0, X); |
| 6009 | return &GEP; |
| 6010 | } |
| 6011 | } else if (GEP.getNumOperands() == 2) { |
| 6012 | // Transform things like: |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 6013 | // %t = getelementptr ubyte* cast ([2 x int]* %str to uint*), uint %V |
| 6014 | // into: %t1 = getelementptr [2 x int*]* %str, int 0, uint %V; cast |
Chris Lattner | 567b81f | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 6015 | const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); |
| 6016 | const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType(); |
| 6017 | if (isa<ArrayType>(SrcElTy) && |
| 6018 | TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()) == |
| 6019 | TD->getTypeSize(ResElTy)) { |
| 6020 | Value *V = InsertNewInstBefore( |
| 6021 | new GetElementPtrInst(X, Constant::getNullValue(Type::IntTy), |
| 6022 | GEP.getOperand(1), GEP.getName()), GEP); |
| 6023 | return new CastInst(V, GEP.getType()); |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 6024 | } |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 6025 | |
| 6026 | // Transform things like: |
| 6027 | // getelementptr sbyte* cast ([100 x double]* X to sbyte*), int %tmp |
| 6028 | // (where tmp = 8*tmp2) into: |
| 6029 | // getelementptr [100 x double]* %arr, int 0, int %tmp.2 |
| 6030 | |
| 6031 | if (isa<ArrayType>(SrcElTy) && |
| 6032 | (ResElTy == Type::SByteTy || ResElTy == Type::UByteTy)) { |
| 6033 | uint64_t ArrayEltSize = |
| 6034 | TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()); |
| 6035 | |
| 6036 | // Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We |
| 6037 | // allow either a mul, shift, or constant here. |
| 6038 | Value *NewIdx = 0; |
| 6039 | ConstantInt *Scale = 0; |
| 6040 | if (ArrayEltSize == 1) { |
| 6041 | NewIdx = GEP.getOperand(1); |
| 6042 | Scale = ConstantInt::get(NewIdx->getType(), 1); |
| 6043 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) { |
Chris Lattner | a393e4d | 2005-09-14 17:32:56 +0000 | [diff] [blame] | 6044 | NewIdx = ConstantInt::get(CI->getType(), 1); |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 6045 | Scale = CI; |
| 6046 | } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){ |
| 6047 | if (Inst->getOpcode() == Instruction::Shl && |
| 6048 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 6049 | unsigned ShAmt =cast<ConstantUInt>(Inst->getOperand(1))->getValue(); |
| 6050 | if (Inst->getType()->isSigned()) |
| 6051 | Scale = ConstantSInt::get(Inst->getType(), 1ULL << ShAmt); |
| 6052 | else |
| 6053 | Scale = ConstantUInt::get(Inst->getType(), 1ULL << ShAmt); |
| 6054 | NewIdx = Inst->getOperand(0); |
| 6055 | } else if (Inst->getOpcode() == Instruction::Mul && |
| 6056 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 6057 | Scale = cast<ConstantInt>(Inst->getOperand(1)); |
| 6058 | NewIdx = Inst->getOperand(0); |
| 6059 | } |
| 6060 | } |
| 6061 | |
| 6062 | // If the index will be to exactly the right offset with the scale taken |
| 6063 | // out, perform the transformation. |
| 6064 | if (Scale && Scale->getRawValue() % ArrayEltSize == 0) { |
| 6065 | if (ConstantSInt *C = dyn_cast<ConstantSInt>(Scale)) |
| 6066 | Scale = ConstantSInt::get(C->getType(), |
Chris Lattner | a393e4d | 2005-09-14 17:32:56 +0000 | [diff] [blame] | 6067 | (int64_t)C->getRawValue() / |
| 6068 | (int64_t)ArrayEltSize); |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 6069 | else |
| 6070 | Scale = ConstantUInt::get(Scale->getType(), |
| 6071 | Scale->getRawValue() / ArrayEltSize); |
| 6072 | if (Scale->getRawValue() != 1) { |
| 6073 | Constant *C = ConstantExpr::getCast(Scale, NewIdx->getType()); |
| 6074 | Instruction *Sc = BinaryOperator::createMul(NewIdx, C, "idxscale"); |
| 6075 | NewIdx = InsertNewInstBefore(Sc, GEP); |
| 6076 | } |
| 6077 | |
| 6078 | // Insert the new GEP instruction. |
| 6079 | Instruction *Idx = |
| 6080 | new GetElementPtrInst(X, Constant::getNullValue(Type::IntTy), |
| 6081 | NewIdx, GEP.getName()); |
| 6082 | Idx = InsertNewInstBefore(Idx, GEP); |
| 6083 | return new CastInst(Idx, GEP.getType()); |
| 6084 | } |
| 6085 | } |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 6086 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 6087 | } |
| 6088 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 6089 | return 0; |
| 6090 | } |
| 6091 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 6092 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
| 6093 | // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1 |
| 6094 | if (AI.isArrayAllocation()) // Check C != 1 |
| 6095 | if (const ConstantUInt *C = dyn_cast<ConstantUInt>(AI.getArraySize())) { |
| 6096 | const Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getValue()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 6097 | AllocationInst *New = 0; |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 6098 | |
| 6099 | // Create and insert the replacement instruction... |
| 6100 | if (isa<MallocInst>(AI)) |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 6101 | New = new MallocInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 6102 | else { |
| 6103 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 6104 | New = new AllocaInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 6105 | } |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 6106 | |
| 6107 | InsertNewInstBefore(New, AI); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6108 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 6109 | // Scan to the end of the allocation instructions, to skip over a block of |
| 6110 | // allocas if possible... |
| 6111 | // |
| 6112 | BasicBlock::iterator It = New; |
| 6113 | while (isa<AllocationInst>(*It)) ++It; |
| 6114 | |
| 6115 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 6116 | // insert our getelementptr instruction... |
| 6117 | // |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 6118 | Value *NullIdx = Constant::getNullValue(Type::IntTy); |
| 6119 | Value *V = new GetElementPtrInst(New, NullIdx, NullIdx, |
| 6120 | New->getName()+".sub", It); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 6121 | |
| 6122 | // Now make everything use the getelementptr instead of the original |
| 6123 | // allocation. |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 6124 | return ReplaceInstUsesWith(AI, V); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6125 | } else if (isa<UndefValue>(AI.getArraySize())) { |
| 6126 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 6127 | } |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 6128 | |
| 6129 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. |
| 6130 | // Note that we only do this for alloca's, because malloc should allocate and |
| 6131 | // return a unique pointer, even for a zero byte allocation. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6132 | if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() && |
Chris Lattner | 49df6ce | 2004-07-02 22:55:47 +0000 | [diff] [blame] | 6133 | TD->getTypeSize(AI.getAllocatedType()) == 0) |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 6134 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
| 6135 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 6136 | return 0; |
| 6137 | } |
| 6138 | |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 6139 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { |
| 6140 | Value *Op = FI.getOperand(0); |
| 6141 | |
| 6142 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X |
| 6143 | if (CastInst *CI = dyn_cast<CastInst>(Op)) |
| 6144 | if (isa<PointerType>(CI->getOperand(0)->getType())) { |
| 6145 | FI.setOperand(0, CI->getOperand(0)); |
| 6146 | return &FI; |
| 6147 | } |
| 6148 | |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 6149 | // free undef -> unreachable. |
| 6150 | if (isa<UndefValue>(Op)) { |
| 6151 | // Insert a new store to null because we cannot modify the CFG here. |
| 6152 | new StoreInst(ConstantBool::True, |
| 6153 | UndefValue::get(PointerType::get(Type::BoolTy)), &FI); |
| 6154 | return EraseInstFromFunction(FI); |
| 6155 | } |
| 6156 | |
Chris Lattner | f3a3660 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 6157 | // If we have 'free null' delete the instruction. This can happen in stl code |
| 6158 | // when lots of inlining happens. |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 6159 | if (isa<ConstantPointerNull>(Op)) |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 6160 | return EraseInstFromFunction(FI); |
Chris Lattner | f3a3660 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 6161 | |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 6162 | return 0; |
| 6163 | } |
| 6164 | |
| 6165 | |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 6166 | /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible. |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 6167 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI) { |
| 6168 | User *CI = cast<User>(LI.getOperand(0)); |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 6169 | Value *CastOp = CI->getOperand(0); |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 6170 | |
| 6171 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 6172 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 6173 | const Type *SrcPTy = SrcTy->getElementType(); |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 6174 | |
| 6175 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) { |
| 6176 | // If the source is an array, the code below will not succeed. Check to |
| 6177 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 6178 | // constants. |
| 6179 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 6180 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 6181 | if (ASrcTy->getNumElements() != 0) { |
| 6182 | std::vector<Value*> Idxs(2, Constant::getNullValue(Type::IntTy)); |
| 6183 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs); |
| 6184 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 6185 | SrcPTy = SrcTy->getElementType(); |
| 6186 | } |
| 6187 | |
| 6188 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
Chris Lattner | ecfa9b5 | 2005-03-29 06:37:47 +0000 | [diff] [blame] | 6189 | // Do not allow turning this into a load of an integer, which is then |
| 6190 | // casted to a pointer, this pessimizes pointer analysis a lot. |
| 6191 | (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6192 | IC.getTargetData().getTypeSize(SrcPTy) == |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 6193 | IC.getTargetData().getTypeSize(DestPTy)) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6194 | |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 6195 | // Okay, we are casting from one integer or pointer type to another of |
| 6196 | // the same size. Instead of casting the pointer before the load, cast |
| 6197 | // the result of the loaded value. |
| 6198 | Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp, |
| 6199 | CI->getName(), |
| 6200 | LI.isVolatile()),LI); |
| 6201 | // Now cast the result of the load. |
| 6202 | return new CastInst(NewLoad, LI.getType()); |
| 6203 | } |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 6204 | } |
| 6205 | } |
| 6206 | return 0; |
| 6207 | } |
| 6208 | |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 6209 | /// isSafeToLoadUnconditionally - Return true if we know that executing a load |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 6210 | /// from this value cannot trap. If it is not obviously safe to load from the |
| 6211 | /// specified pointer, we do a quick local scan of the basic block containing |
| 6212 | /// ScanFrom, to determine if the address is already accessed. |
| 6213 | static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) { |
| 6214 | // If it is an alloca or global variable, it is always safe to load from. |
| 6215 | if (isa<AllocaInst>(V) || isa<GlobalVariable>(V)) return true; |
| 6216 | |
| 6217 | // Otherwise, be a little bit agressive by scanning the local block where we |
| 6218 | // 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] | 6219 | // from/to. If so, the previous load or store would have already trapped, |
| 6220 | // so there is no harm doing an extra load (also, CSE will later eliminate |
| 6221 | // the load entirely). |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 6222 | BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); |
| 6223 | |
Alkis Evlogimenos | d59cebf | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 6224 | while (BBI != E) { |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 6225 | --BBI; |
| 6226 | |
| 6227 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 6228 | if (LI->getOperand(0) == V) return true; |
| 6229 | } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 6230 | if (SI->getOperand(1) == V) return true; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6231 | |
Alkis Evlogimenos | d59cebf | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 6232 | } |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 6233 | return false; |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 6234 | } |
| 6235 | |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 6236 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 6237 | Value *Op = LI.getOperand(0); |
Chris Lattner | 7e8af38 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 6238 | |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 6239 | // load (cast X) --> cast (load X) iff safe |
| 6240 | if (CastInst *CI = dyn_cast<CastInst>(Op)) |
| 6241 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 6242 | return Res; |
| 6243 | |
| 6244 | // None of the following transforms are legal for volatile loads. |
| 6245 | if (LI.isVolatile()) return 0; |
Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 6246 | |
Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 6247 | if (&LI.getParent()->front() != &LI) { |
| 6248 | BasicBlock::iterator BBI = &LI; --BBI; |
Chris Lattner | e0bfdf1 | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 6249 | // If the instruction immediately before this is a store to the same |
| 6250 | // address, do a simple form of store->load forwarding. |
Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 6251 | if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 6252 | if (SI->getOperand(1) == LI.getOperand(0)) |
| 6253 | return ReplaceInstUsesWith(LI, SI->getOperand(0)); |
Chris Lattner | e0bfdf1 | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 6254 | if (LoadInst *LIB = dyn_cast<LoadInst>(BBI)) |
| 6255 | if (LIB->getOperand(0) == LI.getOperand(0)) |
| 6256 | return ReplaceInstUsesWith(LI, LIB); |
Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 6257 | } |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 6258 | |
| 6259 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) |
| 6260 | if (isa<ConstantPointerNull>(GEPI->getOperand(0)) || |
| 6261 | isa<UndefValue>(GEPI->getOperand(0))) { |
| 6262 | // Insert a new store to null instruction before the load to indicate |
| 6263 | // that this code is not reachable. We do this instead of inserting |
| 6264 | // an unreachable instruction directly because we cannot modify the |
| 6265 | // CFG. |
| 6266 | new StoreInst(UndefValue::get(LI.getType()), |
| 6267 | Constant::getNullValue(Op->getType()), &LI); |
| 6268 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 6269 | } |
| 6270 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6271 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 6272 | // load null/undef -> undef |
| 6273 | if ((C->isNullValue() || isa<UndefValue>(C))) { |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 6274 | // Insert a new store to null instruction before the load to indicate that |
| 6275 | // this code is not reachable. We do this instead of inserting an |
| 6276 | // unreachable instruction directly because we cannot modify the CFG. |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 6277 | new StoreInst(UndefValue::get(LI.getType()), |
| 6278 | Constant::getNullValue(Op->getType()), &LI); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6279 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 6280 | } |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 6281 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6282 | // Instcombine load (constant global) into the value loaded. |
| 6283 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) |
| 6284 | if (GV->isConstant() && !GV->isExternal()) |
| 6285 | return ReplaceInstUsesWith(LI, GV->getInitializer()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6286 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6287 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded. |
| 6288 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) |
| 6289 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 6290 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
| 6291 | if (GV->isConstant() && !GV->isExternal()) |
Chris Lattner | 0b011ec | 2005-09-26 05:28:06 +0000 | [diff] [blame] | 6292 | if (Constant *V = |
| 6293 | ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6294 | return ReplaceInstUsesWith(LI, V); |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 6295 | if (CE->getOperand(0)->isNullValue()) { |
| 6296 | // Insert a new store to null instruction before the load to indicate |
| 6297 | // that this code is not reachable. We do this instead of inserting |
| 6298 | // an unreachable instruction directly because we cannot modify the |
| 6299 | // CFG. |
| 6300 | new StoreInst(UndefValue::get(LI.getType()), |
| 6301 | Constant::getNullValue(Op->getType()), &LI); |
| 6302 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 6303 | } |
| 6304 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6305 | } else if (CE->getOpcode() == Instruction::Cast) { |
| 6306 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 6307 | return Res; |
| 6308 | } |
| 6309 | } |
Chris Lattner | e228ee5 | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 6310 | |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 6311 | if (Op->hasOneUse()) { |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 6312 | // Change select and PHI nodes to select values instead of addresses: this |
| 6313 | // helps alias analysis out a lot, allows many others simplifications, and |
| 6314 | // exposes redundancy in the code. |
| 6315 | // |
| 6316 | // Note that we cannot do the transformation unless we know that the |
| 6317 | // introduced loads cannot trap! Something like this is valid as long as |
| 6318 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 6319 | // but it would not be valid if we transformed it to load from null |
| 6320 | // unconditionally. |
| 6321 | // |
| 6322 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 6323 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 6324 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && |
| 6325 | isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 6326 | Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1), |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 6327 | SI->getOperand(1)->getName()+".val"), LI); |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 6328 | Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2), |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 6329 | SI->getOperand(2)->getName()+".val"), LI); |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 6330 | return new SelectInst(SI->getCondition(), V1, V2); |
| 6331 | } |
| 6332 | |
Chris Lattner | bdcf41a | 2004-09-23 15:46:00 +0000 | [diff] [blame] | 6333 | // load (select (cond, null, P)) -> load P |
| 6334 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) |
| 6335 | if (C->isNullValue()) { |
| 6336 | LI.setOperand(0, SI->getOperand(2)); |
| 6337 | return &LI; |
| 6338 | } |
| 6339 | |
| 6340 | // load (select (cond, P, null)) -> load P |
| 6341 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) |
| 6342 | if (C->isNullValue()) { |
| 6343 | LI.setOperand(0, SI->getOperand(1)); |
| 6344 | return &LI; |
| 6345 | } |
| 6346 | |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 6347 | } else if (PHINode *PN = dyn_cast<PHINode>(Op)) { |
| 6348 | // load (phi (&V1, &V2, &V3)) --> phi(load &V1, load &V2, load &V3) |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 6349 | bool Safe = PN->getParent() == LI.getParent(); |
| 6350 | |
| 6351 | // Scan all of the instructions between the PHI and the load to make |
| 6352 | // sure there are no instructions that might possibly alter the value |
| 6353 | // loaded from the PHI. |
| 6354 | if (Safe) { |
| 6355 | BasicBlock::iterator I = &LI; |
| 6356 | for (--I; !isa<PHINode>(I); --I) |
| 6357 | if (isa<StoreInst>(I) || isa<CallInst>(I)) { |
| 6358 | Safe = false; |
| 6359 | break; |
| 6360 | } |
| 6361 | } |
| 6362 | |
| 6363 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e && Safe; ++i) |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 6364 | if (!isSafeToLoadUnconditionally(PN->getIncomingValue(i), |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 6365 | PN->getIncomingBlock(i)->getTerminator())) |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 6366 | Safe = false; |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 6367 | |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 6368 | if (Safe) { |
| 6369 | // Create the PHI. |
| 6370 | PHINode *NewPN = new PHINode(LI.getType(), PN->getName()); |
| 6371 | InsertNewInstBefore(NewPN, *PN); |
| 6372 | std::map<BasicBlock*,Value*> LoadMap; // Don't insert duplicate loads |
| 6373 | |
| 6374 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 6375 | BasicBlock *BB = PN->getIncomingBlock(i); |
| 6376 | Value *&TheLoad = LoadMap[BB]; |
| 6377 | if (TheLoad == 0) { |
| 6378 | Value *InVal = PN->getIncomingValue(i); |
| 6379 | TheLoad = InsertNewInstBefore(new LoadInst(InVal, |
| 6380 | InVal->getName()+".val"), |
| 6381 | *BB->getTerminator()); |
| 6382 | } |
| 6383 | NewPN->addIncoming(TheLoad, BB); |
| 6384 | } |
| 6385 | return ReplaceInstUsesWith(LI, NewPN); |
| 6386 | } |
| 6387 | } |
| 6388 | } |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 6389 | return 0; |
| 6390 | } |
| 6391 | |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 6392 | /// InstCombineStoreToCast - Fold 'store V, (cast P)' -> store (cast V), P' |
| 6393 | /// when possible. |
| 6394 | static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { |
| 6395 | User *CI = cast<User>(SI.getOperand(1)); |
| 6396 | Value *CastOp = CI->getOperand(0); |
| 6397 | |
| 6398 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
| 6399 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
| 6400 | const Type *SrcPTy = SrcTy->getElementType(); |
| 6401 | |
| 6402 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) { |
| 6403 | // If the source is an array, the code below will not succeed. Check to |
| 6404 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 6405 | // constants. |
| 6406 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 6407 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 6408 | if (ASrcTy->getNumElements() != 0) { |
| 6409 | std::vector<Value*> Idxs(2, Constant::getNullValue(Type::IntTy)); |
| 6410 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs); |
| 6411 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 6412 | SrcPTy = SrcTy->getElementType(); |
| 6413 | } |
| 6414 | |
| 6415 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6416 | IC.getTargetData().getTypeSize(SrcPTy) == |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 6417 | IC.getTargetData().getTypeSize(DestPTy)) { |
| 6418 | |
| 6419 | // Okay, we are casting from one integer or pointer type to another of |
| 6420 | // the same size. Instead of casting the pointer before the store, cast |
| 6421 | // the value to be stored. |
| 6422 | Value *NewCast; |
| 6423 | if (Constant *C = dyn_cast<Constant>(SI.getOperand(0))) |
| 6424 | NewCast = ConstantExpr::getCast(C, SrcPTy); |
| 6425 | else |
| 6426 | NewCast = IC.InsertNewInstBefore(new CastInst(SI.getOperand(0), |
| 6427 | SrcPTy, |
| 6428 | SI.getOperand(0)->getName()+".c"), SI); |
| 6429 | |
| 6430 | return new StoreInst(NewCast, CastOp); |
| 6431 | } |
| 6432 | } |
| 6433 | } |
| 6434 | return 0; |
| 6435 | } |
| 6436 | |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 6437 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
| 6438 | Value *Val = SI.getOperand(0); |
| 6439 | Value *Ptr = SI.getOperand(1); |
| 6440 | |
| 6441 | if (isa<UndefValue>(Ptr)) { // store X, undef -> noop (even if volatile) |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 6442 | EraseInstFromFunction(SI); |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 6443 | ++NumCombined; |
| 6444 | return 0; |
| 6445 | } |
| 6446 | |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 6447 | // Do really simple DSE, to catch cases where there are several consequtive |
| 6448 | // stores to the same location, separated by a few arithmetic operations. This |
| 6449 | // situation often occurs with bitfield accesses. |
| 6450 | BasicBlock::iterator BBI = &SI; |
| 6451 | for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts; |
| 6452 | --ScanInsts) { |
| 6453 | --BBI; |
| 6454 | |
| 6455 | if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) { |
| 6456 | // Prev store isn't volatile, and stores to the same location? |
| 6457 | if (!PrevSI->isVolatile() && PrevSI->getOperand(1) == SI.getOperand(1)) { |
| 6458 | ++NumDeadStore; |
| 6459 | ++BBI; |
| 6460 | EraseInstFromFunction(*PrevSI); |
| 6461 | continue; |
| 6462 | } |
| 6463 | break; |
| 6464 | } |
| 6465 | |
| 6466 | // Don't skip over loads or things that can modify memory. |
| 6467 | if (BBI->mayWriteToMemory() || isa<LoadInst>(BBI)) |
| 6468 | break; |
| 6469 | } |
| 6470 | |
| 6471 | |
| 6472 | if (SI.isVolatile()) return 0; // Don't hack volatile stores. |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 6473 | |
| 6474 | // store X, null -> turns into 'unreachable' in SimplifyCFG |
| 6475 | if (isa<ConstantPointerNull>(Ptr)) { |
| 6476 | if (!isa<UndefValue>(Val)) { |
| 6477 | SI.setOperand(0, UndefValue::get(Val->getType())); |
| 6478 | if (Instruction *U = dyn_cast<Instruction>(Val)) |
| 6479 | WorkList.push_back(U); // Dropped a use. |
| 6480 | ++NumCombined; |
| 6481 | } |
| 6482 | return 0; // Do not modify these! |
| 6483 | } |
| 6484 | |
| 6485 | // store undef, Ptr -> noop |
| 6486 | if (isa<UndefValue>(Val)) { |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 6487 | EraseInstFromFunction(SI); |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 6488 | ++NumCombined; |
| 6489 | return 0; |
| 6490 | } |
| 6491 | |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 6492 | // If the pointer destination is a cast, see if we can fold the cast into the |
| 6493 | // source instead. |
| 6494 | if (CastInst *CI = dyn_cast<CastInst>(Ptr)) |
| 6495 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 6496 | return Res; |
| 6497 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
| 6498 | if (CE->getOpcode() == Instruction::Cast) |
| 6499 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 6500 | return Res; |
| 6501 | |
Chris Lattner | 219175c | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 6502 | |
| 6503 | // If this store is the last instruction in the basic block, and if the block |
| 6504 | // ends with an unconditional branch, try to move it to the successor block. |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 6505 | BBI = &SI; ++BBI; |
Chris Lattner | 219175c | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 6506 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) |
| 6507 | if (BI->isUnconditional()) { |
| 6508 | // Check to see if the successor block has exactly two incoming edges. If |
| 6509 | // so, see if the other predecessor contains a store to the same location. |
| 6510 | // if so, insert a PHI node (if needed) and move the stores down. |
| 6511 | BasicBlock *Dest = BI->getSuccessor(0); |
| 6512 | |
| 6513 | pred_iterator PI = pred_begin(Dest); |
| 6514 | BasicBlock *Other = 0; |
| 6515 | if (*PI != BI->getParent()) |
| 6516 | Other = *PI; |
| 6517 | ++PI; |
| 6518 | if (PI != pred_end(Dest)) { |
| 6519 | if (*PI != BI->getParent()) |
| 6520 | if (Other) |
| 6521 | Other = 0; |
| 6522 | else |
| 6523 | Other = *PI; |
| 6524 | if (++PI != pred_end(Dest)) |
| 6525 | Other = 0; |
| 6526 | } |
| 6527 | if (Other) { // If only one other pred... |
| 6528 | BBI = Other->getTerminator(); |
| 6529 | // Make sure this other block ends in an unconditional branch and that |
| 6530 | // there is an instruction before the branch. |
| 6531 | if (isa<BranchInst>(BBI) && cast<BranchInst>(BBI)->isUnconditional() && |
| 6532 | BBI != Other->begin()) { |
| 6533 | --BBI; |
| 6534 | StoreInst *OtherStore = dyn_cast<StoreInst>(BBI); |
| 6535 | |
| 6536 | // If this instruction is a store to the same location. |
| 6537 | if (OtherStore && OtherStore->getOperand(1) == SI.getOperand(1)) { |
| 6538 | // Okay, we know we can perform this transformation. Insert a PHI |
| 6539 | // node now if we need it. |
| 6540 | Value *MergedVal = OtherStore->getOperand(0); |
| 6541 | if (MergedVal != SI.getOperand(0)) { |
| 6542 | PHINode *PN = new PHINode(MergedVal->getType(), "storemerge"); |
| 6543 | PN->reserveOperandSpace(2); |
| 6544 | PN->addIncoming(SI.getOperand(0), SI.getParent()); |
| 6545 | PN->addIncoming(OtherStore->getOperand(0), Other); |
| 6546 | MergedVal = InsertNewInstBefore(PN, Dest->front()); |
| 6547 | } |
| 6548 | |
| 6549 | // Advance to a place where it is safe to insert the new store and |
| 6550 | // insert it. |
| 6551 | BBI = Dest->begin(); |
| 6552 | while (isa<PHINode>(BBI)) ++BBI; |
| 6553 | InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1), |
| 6554 | OtherStore->isVolatile()), *BBI); |
| 6555 | |
| 6556 | // Nuke the old stores. |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 6557 | EraseInstFromFunction(SI); |
| 6558 | EraseInstFromFunction(*OtherStore); |
Chris Lattner | 219175c | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 6559 | ++NumCombined; |
| 6560 | return 0; |
| 6561 | } |
| 6562 | } |
| 6563 | } |
| 6564 | } |
| 6565 | |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 6566 | return 0; |
| 6567 | } |
| 6568 | |
| 6569 | |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 6570 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 6571 | // 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] | 6572 | Value *X = 0; |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 6573 | BasicBlock *TrueDest; |
| 6574 | BasicBlock *FalseDest; |
| 6575 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && |
| 6576 | !isa<Constant>(X)) { |
| 6577 | // Swap Destinations and condition... |
| 6578 | BI.setCondition(X); |
| 6579 | BI.setSuccessor(0, FalseDest); |
| 6580 | BI.setSuccessor(1, TrueDest); |
| 6581 | return &BI; |
| 6582 | } |
| 6583 | |
| 6584 | // Cannonicalize setne -> seteq |
| 6585 | Instruction::BinaryOps Op; Value *Y; |
| 6586 | if (match(&BI, m_Br(m_SetCond(Op, m_Value(X), m_Value(Y)), |
| 6587 | TrueDest, FalseDest))) |
| 6588 | if ((Op == Instruction::SetNE || Op == Instruction::SetLE || |
| 6589 | Op == Instruction::SetGE) && BI.getCondition()->hasOneUse()) { |
| 6590 | SetCondInst *I = cast<SetCondInst>(BI.getCondition()); |
| 6591 | std::string Name = I->getName(); I->setName(""); |
| 6592 | Instruction::BinaryOps NewOpcode = SetCondInst::getInverseCondition(Op); |
| 6593 | Value *NewSCC = BinaryOperator::create(NewOpcode, X, Y, Name, I); |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 6594 | // Swap Destinations and condition... |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 6595 | BI.setCondition(NewSCC); |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 6596 | BI.setSuccessor(0, FalseDest); |
| 6597 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 6598 | removeFromWorkList(I); |
| 6599 | I->getParent()->getInstList().erase(I); |
| 6600 | WorkList.push_back(cast<Instruction>(NewSCC)); |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 6601 | return &BI; |
| 6602 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6603 | |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 6604 | return 0; |
| 6605 | } |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 6606 | |
Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 6607 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
| 6608 | Value *Cond = SI.getCondition(); |
| 6609 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { |
| 6610 | if (I->getOpcode() == Instruction::Add) |
| 6611 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 6612 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' |
| 6613 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6614 | SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), |
Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 6615 | AddRHS)); |
| 6616 | SI.setOperand(0, I->getOperand(0)); |
| 6617 | WorkList.push_back(I); |
| 6618 | return &SI; |
| 6619 | } |
| 6620 | } |
| 6621 | return 0; |
| 6622 | } |
| 6623 | |
Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 6624 | /// CheapToScalarize - Return true if the value is cheaper to scalarize than it |
| 6625 | /// is to leave as a vector operation. |
| 6626 | static bool CheapToScalarize(Value *V, bool isConstant) { |
| 6627 | if (isa<ConstantAggregateZero>(V)) |
| 6628 | return true; |
| 6629 | if (ConstantPacked *C = dyn_cast<ConstantPacked>(V)) { |
| 6630 | if (isConstant) return true; |
| 6631 | // If all elts are the same, we can extract. |
| 6632 | Constant *Op0 = C->getOperand(0); |
| 6633 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
| 6634 | if (C->getOperand(i) != Op0) |
| 6635 | return false; |
| 6636 | return true; |
| 6637 | } |
| 6638 | Instruction *I = dyn_cast<Instruction>(V); |
| 6639 | if (!I) return false; |
| 6640 | |
| 6641 | // Insert element gets simplified to the inserted element or is deleted if |
| 6642 | // this is constant idx extract element and its a constant idx insertelt. |
| 6643 | if (I->getOpcode() == Instruction::InsertElement && isConstant && |
| 6644 | isa<ConstantInt>(I->getOperand(2))) |
| 6645 | return true; |
| 6646 | if (I->getOpcode() == Instruction::Load && I->hasOneUse()) |
| 6647 | return true; |
| 6648 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) |
| 6649 | if (BO->hasOneUse() && |
| 6650 | (CheapToScalarize(BO->getOperand(0), isConstant) || |
| 6651 | CheapToScalarize(BO->getOperand(1), isConstant))) |
| 6652 | return true; |
| 6653 | |
| 6654 | return false; |
| 6655 | } |
| 6656 | |
Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame^] | 6657 | /// FindScalarElement - Given a vector and an element number, see if the scalar |
| 6658 | /// value is already around as a register, for example if it were inserted then |
| 6659 | /// extracted from the vector. |
| 6660 | static Value *FindScalarElement(Value *V, unsigned EltNo) { |
| 6661 | assert(isa<PackedType>(V->getType()) && "Not looking at a vector?"); |
| 6662 | const PackedType *PTy = cast<PackedType>(V->getType()); |
| 6663 | if (EltNo >= PTy->getNumElements()) // Out of range access. |
| 6664 | return UndefValue::get(PTy->getElementType()); |
| 6665 | |
| 6666 | if (isa<UndefValue>(V)) |
| 6667 | return UndefValue::get(PTy->getElementType()); |
| 6668 | else if (isa<ConstantAggregateZero>(V)) |
| 6669 | return Constant::getNullValue(PTy->getElementType()); |
| 6670 | else if (ConstantPacked *CP = dyn_cast<ConstantPacked>(V)) |
| 6671 | return CP->getOperand(EltNo); |
| 6672 | else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) { |
| 6673 | // If this is an insert to a variable element, we don't know what it is. |
| 6674 | if (!isa<ConstantUInt>(III->getOperand(2))) return 0; |
| 6675 | unsigned IIElt = cast<ConstantUInt>(III->getOperand(2))->getValue(); |
| 6676 | |
| 6677 | // If this is an insert to the element we are looking for, return the |
| 6678 | // inserted value. |
| 6679 | if (EltNo == IIElt) return III->getOperand(1); |
| 6680 | |
| 6681 | // Otherwise, the insertelement doesn't modify the value, recurse on its |
| 6682 | // vector input. |
| 6683 | return FindScalarElement(III->getOperand(0), EltNo); |
| 6684 | } |
| 6685 | |
| 6686 | // Otherwise, we don't know. |
| 6687 | return 0; |
| 6688 | } |
| 6689 | |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 6690 | Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { |
Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame^] | 6691 | |
Chris Lattner | 92346c3 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 6692 | // If packed val is undef, replace extract with scalar undef. |
| 6693 | if (isa<UndefValue>(EI.getOperand(0))) |
| 6694 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
| 6695 | |
| 6696 | // If packed val is constant 0, replace extract with scalar 0. |
| 6697 | if (isa<ConstantAggregateZero>(EI.getOperand(0))) |
| 6698 | return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType())); |
| 6699 | |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 6700 | if (ConstantPacked *C = dyn_cast<ConstantPacked>(EI.getOperand(0))) { |
| 6701 | // If packed val is constant with uniform operands, replace EI |
| 6702 | // with that operand |
Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 6703 | Constant *op0 = C->getOperand(0); |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 6704 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 6705 | if (C->getOperand(i) != op0) { |
| 6706 | op0 = 0; |
| 6707 | break; |
| 6708 | } |
| 6709 | if (op0) |
| 6710 | return ReplaceInstUsesWith(EI, op0); |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 6711 | } |
Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 6712 | |
Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame^] | 6713 | // If extracting a specified index from the vector, see if we can recursively |
| 6714 | // find a previously computed scalar that was inserted into the vector. |
| 6715 | if (ConstantUInt *IdxC = dyn_cast<ConstantUInt>(EI.getOperand(1))) |
| 6716 | if (Value *Elt = FindScalarElement(EI.getOperand(0), IdxC->getValue())) |
| 6717 | return ReplaceInstUsesWith(EI, Elt); |
| 6718 | |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 6719 | if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) |
| 6720 | if (I->hasOneUse()) { |
| 6721 | // Push extractelement into predecessor operation if legal and |
| 6722 | // profitable to do so |
| 6723 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 6724 | bool isConstantElt = isa<ConstantInt>(EI.getOperand(1)); |
| 6725 | if (CheapToScalarize(BO, isConstantElt)) { |
| 6726 | ExtractElementInst *newEI0 = |
| 6727 | new ExtractElementInst(BO->getOperand(0), EI.getOperand(1), |
| 6728 | EI.getName()+".lhs"); |
| 6729 | ExtractElementInst *newEI1 = |
| 6730 | new ExtractElementInst(BO->getOperand(1), EI.getOperand(1), |
| 6731 | EI.getName()+".rhs"); |
| 6732 | InsertNewInstBefore(newEI0, EI); |
| 6733 | InsertNewInstBefore(newEI1, EI); |
| 6734 | return BinaryOperator::create(BO->getOpcode(), newEI0, newEI1); |
| 6735 | } |
| 6736 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 6737 | Value *Ptr = InsertCastBefore(I->getOperand(0), |
| 6738 | PointerType::get(EI.getType()), EI); |
| 6739 | GetElementPtrInst *GEP = |
| 6740 | new GetElementPtrInst(Ptr, EI.getOperand(1), |
| 6741 | I->getName() + ".gep"); |
| 6742 | InsertNewInstBefore(GEP, EI); |
| 6743 | return new LoadInst(GEP); |
Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 6744 | } else if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) { |
| 6745 | // Extracting the inserted element? |
| 6746 | if (IE->getOperand(2) == EI.getOperand(1)) |
| 6747 | return ReplaceInstUsesWith(EI, IE->getOperand(1)); |
| 6748 | // If the inserted and extracted elements are constants, they must not |
Chris Lattner | 612fa8e | 2006-03-30 22:02:40 +0000 | [diff] [blame] | 6749 | // be the same value, extract from the pre-inserted value instead. |
| 6750 | if (isa<Constant>(IE->getOperand(2)) && |
| 6751 | isa<Constant>(EI.getOperand(1))) { |
| 6752 | AddUsesToWorkList(EI); |
| 6753 | EI.setOperand(0, IE->getOperand(0)); |
| 6754 | return &EI; |
| 6755 | } |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 6756 | } |
| 6757 | } |
| 6758 | return 0; |
| 6759 | } |
| 6760 | |
| 6761 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 6762 | void InstCombiner::removeFromWorkList(Instruction *I) { |
| 6763 | WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I), |
| 6764 | WorkList.end()); |
| 6765 | } |
| 6766 | |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 6767 | |
| 6768 | /// TryToSinkInstruction - Try to move the specified instruction from its |
| 6769 | /// current block into the beginning of DestBlock, which can only happen if it's |
| 6770 | /// safe to move the instruction past all of the instructions between it and the |
| 6771 | /// end of its block. |
| 6772 | static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { |
| 6773 | assert(I->hasOneUse() && "Invariants didn't hold!"); |
| 6774 | |
Chris Lattner | c4f67e6 | 2005-10-27 17:13:11 +0000 | [diff] [blame] | 6775 | // Cannot move control-flow-involving, volatile loads, vaarg, etc. |
| 6776 | if (isa<PHINode>(I) || I->mayWriteToMemory()) return false; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6777 | |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 6778 | // Do not sink alloca instructions out of the entry block. |
| 6779 | if (isa<AllocaInst>(I) && I->getParent() == &DestBlock->getParent()->front()) |
| 6780 | return false; |
| 6781 | |
Chris Lattner | f17a2fb | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 6782 | // We can only sink load instructions if there is nothing between the load and |
| 6783 | // the end of block that could change the value. |
| 6784 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Chris Lattner | f17a2fb | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 6785 | for (BasicBlock::iterator Scan = LI, E = LI->getParent()->end(); |
| 6786 | Scan != E; ++Scan) |
| 6787 | if (Scan->mayWriteToMemory()) |
| 6788 | return false; |
Chris Lattner | f17a2fb | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 6789 | } |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 6790 | |
| 6791 | BasicBlock::iterator InsertPos = DestBlock->begin(); |
| 6792 | while (isa<PHINode>(InsertPos)) ++InsertPos; |
| 6793 | |
Chris Lattner | 9f269e4 | 2005-08-08 19:11:57 +0000 | [diff] [blame] | 6794 | I->moveBefore(InsertPos); |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 6795 | ++NumSunkInst; |
| 6796 | return true; |
| 6797 | } |
| 6798 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 6799 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 6800 | bool Changed = false; |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 6801 | TD = &getAnalysis<TargetData>(); |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 6802 | |
Chris Lattner | 4ed40f7 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 6803 | { |
| 6804 | // Populate the worklist with the reachable instructions. |
| 6805 | std::set<BasicBlock*> Visited; |
| 6806 | for (df_ext_iterator<BasicBlock*> BB = df_ext_begin(&F.front(), Visited), |
| 6807 | E = df_ext_end(&F.front(), Visited); BB != E; ++BB) |
| 6808 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 6809 | WorkList.push_back(I); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 6810 | |
Chris Lattner | 4ed40f7 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 6811 | // Do a quick scan over the function. If we find any blocks that are |
| 6812 | // unreachable, remove any instructions inside of them. This prevents |
| 6813 | // the instcombine code from having to deal with some bad special cases. |
| 6814 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 6815 | if (!Visited.count(BB)) { |
| 6816 | Instruction *Term = BB->getTerminator(); |
| 6817 | while (Term != BB->begin()) { // Remove instrs bottom-up |
| 6818 | BasicBlock::iterator I = Term; --I; |
Chris Lattner | 2d3a7a6 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 6819 | |
Chris Lattner | 4ed40f7 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 6820 | DEBUG(std::cerr << "IC: DCE: " << *I); |
| 6821 | ++NumDeadInst; |
| 6822 | |
| 6823 | if (!I->use_empty()) |
| 6824 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 6825 | I->eraseFromParent(); |
| 6826 | } |
| 6827 | } |
| 6828 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 6829 | |
| 6830 | while (!WorkList.empty()) { |
| 6831 | Instruction *I = WorkList.back(); // Get an instruction from the worklist |
| 6832 | WorkList.pop_back(); |
| 6833 | |
Misha Brukman | 632df28 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 6834 | // Check to see if we can DCE or ConstantPropagate the instruction... |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 6835 | // Check to see if we can DIE the instruction... |
| 6836 | if (isInstructionTriviallyDead(I)) { |
| 6837 | // Add operands to the worklist... |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 6838 | if (I->getNumOperands() < 4) |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 6839 | AddUsesToWorkList(*I); |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 6840 | ++NumDeadInst; |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 6841 | |
Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 6842 | DEBUG(std::cerr << "IC: DCE: " << *I); |
| 6843 | |
| 6844 | I->eraseFromParent(); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 6845 | removeFromWorkList(I); |
| 6846 | continue; |
| 6847 | } |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 6848 | |
Misha Brukman | 632df28 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 6849 | // Instruction isn't dead, see if we can constant propagate it... |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 6850 | if (Constant *C = ConstantFoldInstruction(I)) { |
Alkis Evlogimenos | a1291a0 | 2004-12-08 23:10:30 +0000 | [diff] [blame] | 6851 | Value* Ptr = I->getOperand(0); |
Chris Lattner | 6580e09 | 2004-10-16 19:44:59 +0000 | [diff] [blame] | 6852 | if (isa<GetElementPtrInst>(I) && |
Alkis Evlogimenos | a1291a0 | 2004-12-08 23:10:30 +0000 | [diff] [blame] | 6853 | cast<Constant>(Ptr)->isNullValue() && |
| 6854 | !isa<ConstantPointerNull>(C) && |
| 6855 | cast<PointerType>(Ptr->getType())->getElementType()->isSized()) { |
Chris Lattner | 6580e09 | 2004-10-16 19:44:59 +0000 | [diff] [blame] | 6856 | // If this is a constant expr gep that is effectively computing an |
| 6857 | // "offsetof", fold it into 'cast int X to T*' instead of 'gep 0, 0, 12' |
| 6858 | bool isFoldableGEP = true; |
| 6859 | for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i) |
| 6860 | if (!isa<ConstantInt>(I->getOperand(i))) |
| 6861 | isFoldableGEP = false; |
| 6862 | if (isFoldableGEP) { |
Alkis Evlogimenos | a1291a0 | 2004-12-08 23:10:30 +0000 | [diff] [blame] | 6863 | uint64_t Offset = TD->getIndexedOffset(Ptr->getType(), |
Chris Lattner | 6580e09 | 2004-10-16 19:44:59 +0000 | [diff] [blame] | 6864 | std::vector<Value*>(I->op_begin()+1, I->op_end())); |
| 6865 | C = ConstantUInt::get(Type::ULongTy, Offset); |
Chris Lattner | 684c5c6 | 2004-10-16 19:46:33 +0000 | [diff] [blame] | 6866 | C = ConstantExpr::getCast(C, TD->getIntPtrType()); |
Chris Lattner | 6580e09 | 2004-10-16 19:44:59 +0000 | [diff] [blame] | 6867 | C = ConstantExpr::getCast(C, I->getType()); |
| 6868 | } |
| 6869 | } |
| 6870 | |
Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 6871 | DEBUG(std::cerr << "IC: ConstFold to: " << *C << " from: " << *I); |
| 6872 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 6873 | // Add operands to the worklist... |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 6874 | AddUsesToWorkList(*I); |
Chris Lattner | c6509f4 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 6875 | ReplaceInstUsesWith(*I, C); |
| 6876 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 6877 | ++NumConstProp; |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 6878 | I->getParent()->getInstList().erase(I); |
Chris Lattner | 800aaaf | 2003-10-07 15:17:02 +0000 | [diff] [blame] | 6879 | removeFromWorkList(I); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 6880 | continue; |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 6881 | } |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 6882 | |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 6883 | // See if we can trivially sink this instruction to a successor basic block. |
| 6884 | if (I->hasOneUse()) { |
| 6885 | BasicBlock *BB = I->getParent(); |
| 6886 | BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent(); |
| 6887 | if (UserParent != BB) { |
| 6888 | bool UserIsSuccessor = false; |
| 6889 | // See if the user is one of our successors. |
| 6890 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) |
| 6891 | if (*SI == UserParent) { |
| 6892 | UserIsSuccessor = true; |
| 6893 | break; |
| 6894 | } |
| 6895 | |
| 6896 | // If the user is one of our immediate successors, and if that successor |
| 6897 | // only has us as a predecessors (we'd have to split the critical edge |
| 6898 | // otherwise), we can keep going. |
| 6899 | if (UserIsSuccessor && !isa<PHINode>(I->use_back()) && |
| 6900 | next(pred_begin(UserParent)) == pred_end(UserParent)) |
| 6901 | // Okay, the CFG is simple enough, try to sink this instruction. |
| 6902 | Changed |= TryToSinkInstruction(I, UserParent); |
| 6903 | } |
| 6904 | } |
| 6905 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 6906 | // Now that we have an instruction, try combining it to simplify it... |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 6907 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 6908 | ++NumCombined; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 6909 | // Should we replace the old instruction with a new one? |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 6910 | if (Result != I) { |
Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 6911 | DEBUG(std::cerr << "IC: Old = " << *I |
| 6912 | << " New = " << *Result); |
| 6913 | |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 6914 | // Everything uses the new instruction now. |
| 6915 | I->replaceAllUsesWith(Result); |
| 6916 | |
| 6917 | // Push the new instruction and any users onto the worklist. |
| 6918 | WorkList.push_back(Result); |
| 6919 | AddUsersToWorkList(*Result); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 6920 | |
| 6921 | // Move the name to the new instruction first... |
| 6922 | std::string OldName = I->getName(); I->setName(""); |
Chris Lattner | 950fc78 | 2003-10-07 22:58:41 +0000 | [diff] [blame] | 6923 | Result->setName(OldName); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 6924 | |
| 6925 | // Insert the new instruction into the basic block... |
| 6926 | BasicBlock *InstParent = I->getParent(); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 6927 | BasicBlock::iterator InsertPos = I; |
| 6928 | |
| 6929 | if (!isa<PHINode>(Result)) // If combining a PHI, don't insert |
| 6930 | while (isa<PHINode>(InsertPos)) // middle of a block of PHIs. |
| 6931 | ++InsertPos; |
| 6932 | |
| 6933 | InstParent->getInstList().insert(InsertPos, Result); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 6934 | |
Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 6935 | // Make sure that we reprocess all operands now that we reduced their |
| 6936 | // use counts. |
Chris Lattner | b643a9e | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 6937 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 6938 | if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i))) |
| 6939 | WorkList.push_back(OpI); |
| 6940 | |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 6941 | // Instructions can end up on the worklist more than once. Make sure |
| 6942 | // we do not process an instruction that has been deleted. |
| 6943 | removeFromWorkList(I); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 6944 | |
| 6945 | // Erase the old instruction. |
| 6946 | InstParent->getInstList().erase(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 6947 | } else { |
Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 6948 | DEBUG(std::cerr << "IC: MOD = " << *I); |
| 6949 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 6950 | // If the instruction was modified, it's possible that it is now dead. |
| 6951 | // if so, remove it. |
Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 6952 | if (isInstructionTriviallyDead(I)) { |
| 6953 | // Make sure we process all operands now that we are reducing their |
| 6954 | // use counts. |
| 6955 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 6956 | if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i))) |
| 6957 | WorkList.push_back(OpI); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6958 | |
Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 6959 | // Instructions may end up in the worklist more than once. Erase all |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 6960 | // occurrences of this instruction. |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 6961 | removeFromWorkList(I); |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 6962 | I->eraseFromParent(); |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 6963 | } else { |
| 6964 | WorkList.push_back(Result); |
| 6965 | AddUsersToWorkList(*Result); |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 6966 | } |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 6967 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 6968 | Changed = true; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 6969 | } |
| 6970 | } |
| 6971 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 6972 | return Changed; |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 6973 | } |
| 6974 | |
Brian Gaeke | 38b79e8 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 6975 | FunctionPass *llvm::createInstructionCombiningPass() { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 6976 | return new InstCombiner(); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 6977 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 6978 | |