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 | 3d27be1 | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 51 | #include "llvm/Support/Compiler.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 | 4a4c7fe | 2006-06-28 22:08:15 +0000 | [diff] [blame] | 66 | class VISIBILITY_HIDDEN InstCombiner |
| 67 | : public FunctionPass, |
| 68 | public InstVisitor<InstCombiner, Instruction*> { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 69 | // Worklist of all of the instructions that need to be simplified. |
| 70 | std::vector<Instruction*> WorkList; |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 71 | TargetData *TD; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 73 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
| 74 | /// the instruction to the work lists because they might get more simplified |
| 75 | /// now. |
| 76 | /// |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 77 | void AddUsersToWorkList(Value &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 78 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 79 | UI != UE; ++UI) |
| 80 | WorkList.push_back(cast<Instruction>(*UI)); |
| 81 | } |
| 82 | |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 83 | /// AddUsesToWorkList - When an instruction is simplified, add operands to |
| 84 | /// the work lists because they might get more simplified now. |
| 85 | /// |
| 86 | void AddUsesToWorkList(Instruction &I) { |
| 87 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 88 | if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) |
| 89 | WorkList.push_back(Op); |
| 90 | } |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 91 | |
| 92 | /// AddSoonDeadInstToWorklist - The specified instruction is about to become |
| 93 | /// dead. Add all of its operands to the worklist, turning them into |
| 94 | /// undef's to reduce the number of uses of those instructions. |
| 95 | /// |
| 96 | /// Return the specified operand before it is turned into an undef. |
| 97 | /// |
| 98 | Value *AddSoonDeadInstToWorklist(Instruction &I, unsigned op) { |
| 99 | Value *R = I.getOperand(op); |
| 100 | |
| 101 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 102 | if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) { |
| 103 | WorkList.push_back(Op); |
| 104 | // Set the operand to undef to drop the use. |
| 105 | I.setOperand(i, UndefValue::get(Op->getType())); |
| 106 | } |
| 107 | |
| 108 | return R; |
| 109 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 111 | // removeFromWorkList - remove all instances of I from the worklist. |
| 112 | void removeFromWorkList(Instruction *I); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 113 | public: |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 114 | virtual bool runOnFunction(Function &F); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 115 | |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 116 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 117 | AU.addRequired<TargetData>(); |
Owen Anderson | a6968f8 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 118 | AU.addPreservedID(LCSSAID); |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 119 | AU.setPreservesCFG(); |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 122 | TargetData &getTargetData() const { return *TD; } |
| 123 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 124 | // Visitation implementation - Implement instruction combining for different |
| 125 | // instruction types. The semantics are as follows: |
| 126 | // Return Value: |
| 127 | // null - No change was made |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 128 | // 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] | 129 | // otherwise - Change was made, replace I with returned instruction |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 130 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 131 | Instruction *visitAdd(BinaryOperator &I); |
| 132 | Instruction *visitSub(BinaryOperator &I); |
| 133 | Instruction *visitMul(BinaryOperator &I); |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 134 | Instruction *visitURem(BinaryOperator &I); |
| 135 | Instruction *visitSRem(BinaryOperator &I); |
| 136 | Instruction *visitFRem(BinaryOperator &I); |
| 137 | Instruction *commonRemTransforms(BinaryOperator &I); |
| 138 | Instruction *commonIRemTransforms(BinaryOperator &I); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 139 | Instruction *commonDivTransforms(BinaryOperator &I); |
| 140 | Instruction *commonIDivTransforms(BinaryOperator &I); |
| 141 | Instruction *visitUDiv(BinaryOperator &I); |
| 142 | Instruction *visitSDiv(BinaryOperator &I); |
| 143 | Instruction *visitFDiv(BinaryOperator &I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 144 | Instruction *visitAnd(BinaryOperator &I); |
| 145 | Instruction *visitOr (BinaryOperator &I); |
| 146 | Instruction *visitXor(BinaryOperator &I); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 147 | Instruction *visitSetCondInst(SetCondInst &I); |
| 148 | Instruction *visitSetCondInstWithCastAndCast(SetCondInst &SCI); |
| 149 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 150 | Instruction *FoldGEPSetCC(User *GEPLHS, Value *RHS, |
| 151 | Instruction::BinaryOps Cond, Instruction &I); |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 152 | Instruction *visitShiftInst(ShiftInst &I); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 153 | Instruction *FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 154 | ShiftInst &I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 155 | Instruction *visitCastInst(CastInst &CI); |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 156 | Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 157 | Instruction *FI); |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 158 | Instruction *visitSelectInst(SelectInst &CI); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 159 | Instruction *visitCallInst(CallInst &CI); |
| 160 | Instruction *visitInvokeInst(InvokeInst &II); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 161 | Instruction *visitPHINode(PHINode &PN); |
| 162 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 163 | Instruction *visitAllocationInst(AllocationInst &AI); |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 164 | Instruction *visitFreeInst(FreeInst &FI); |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 165 | Instruction *visitLoadInst(LoadInst &LI); |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 166 | Instruction *visitStoreInst(StoreInst &SI); |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 167 | Instruction *visitBranchInst(BranchInst &BI); |
Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 168 | Instruction *visitSwitchInst(SwitchInst &SI); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 169 | Instruction *visitInsertElementInst(InsertElementInst &IE); |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 170 | Instruction *visitExtractElementInst(ExtractElementInst &EI); |
Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 171 | Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 172 | |
| 173 | // visitInstruction - Specify what to return for unhandled instructions... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 174 | Instruction *visitInstruction(Instruction &I) { return 0; } |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 175 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 176 | private: |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 177 | Instruction *visitCallSite(CallSite CS); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 178 | bool transformConstExprCastCall(CallSite CS); |
| 179 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 180 | public: |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 181 | // InsertNewInstBefore - insert an instruction New before instruction Old |
| 182 | // in the program. Add the new instruction to the worklist. |
| 183 | // |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 184 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Chris Lattner | 65217ff | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 185 | assert(New && New->getParent() == 0 && |
| 186 | "New instruction already inserted into a basic block!"); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 187 | BasicBlock *BB = Old.getParent(); |
| 188 | BB->getInstList().insert(&Old, New); // Insert inst |
| 189 | WorkList.push_back(New); // Add to worklist |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 190 | return New; |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 193 | /// InsertCastBefore - Insert a cast of V to TY before the instruction POS. |
| 194 | /// This also adds the cast to the worklist. Finally, this returns the |
| 195 | /// cast. |
| 196 | Value *InsertCastBefore(Value *V, const Type *Ty, Instruction &Pos) { |
| 197 | if (V->getType() == Ty) return V; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 198 | |
Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 199 | if (Constant *CV = dyn_cast<Constant>(V)) |
| 200 | return ConstantExpr::getCast(CV, Ty); |
| 201 | |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 202 | Instruction *C = new CastInst(V, Ty, V->getName(), &Pos); |
| 203 | WorkList.push_back(C); |
| 204 | return C; |
| 205 | } |
| 206 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 207 | // ReplaceInstUsesWith - This method is to be used when an instruction is |
| 208 | // found to be dead, replacable with another preexisting expression. Here |
| 209 | // we add all uses of I to the worklist, replace all uses of I with the new |
| 210 | // value, then return I, so that the inst combiner will know that I was |
| 211 | // modified. |
| 212 | // |
| 213 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 214 | AddUsersToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 8953b90 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 215 | if (&I != V) { |
| 216 | I.replaceAllUsesWith(V); |
| 217 | return &I; |
| 218 | } else { |
| 219 | // If we are replacing the instruction with itself, this must be in a |
| 220 | // segment of unreachable code, so just clobber the instruction. |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 221 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
Chris Lattner | 8953b90 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 222 | return &I; |
| 223 | } |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 224 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 225 | |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 226 | // UpdateValueUsesWith - This method is to be used when an value is |
| 227 | // found to be replacable with another preexisting expression or was |
| 228 | // updated. Here we add all uses of I to the worklist, replace all uses of |
| 229 | // I with the new value (unless the instruction was just updated), then |
| 230 | // return true, so that the inst combiner will know that I was modified. |
| 231 | // |
| 232 | bool UpdateValueUsesWith(Value *Old, Value *New) { |
| 233 | AddUsersToWorkList(*Old); // Add all modified instrs to worklist |
| 234 | if (Old != New) |
| 235 | Old->replaceAllUsesWith(New); |
| 236 | if (Instruction *I = dyn_cast<Instruction>(Old)) |
| 237 | WorkList.push_back(I); |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 238 | if (Instruction *I = dyn_cast<Instruction>(New)) |
| 239 | WorkList.push_back(I); |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 240 | return true; |
| 241 | } |
| 242 | |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 243 | // EraseInstFromFunction - When dealing with an instruction that has side |
| 244 | // effects or produces a void value, we can't rely on DCE to delete the |
| 245 | // instruction. Instead, visit methods should return the value returned by |
| 246 | // this function. |
| 247 | Instruction *EraseInstFromFunction(Instruction &I) { |
| 248 | assert(I.use_empty() && "Cannot erase instruction that is used!"); |
| 249 | AddUsesToWorkList(I); |
| 250 | removeFromWorkList(&I); |
Chris Lattner | 9530754 | 2004-11-18 21:41:39 +0000 | [diff] [blame] | 251 | I.eraseFromParent(); |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 252 | return 0; // Don't do anything with FI |
| 253 | } |
| 254 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 255 | private: |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 256 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 257 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 258 | /// casts that are known to not do anything... |
| 259 | /// |
| 260 | Value *InsertOperandCastBefore(Value *V, const Type *DestTy, |
| 261 | Instruction *InsertBefore); |
| 262 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 263 | // SimplifyCommutative - This performs a few simplifications for commutative |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 264 | // operators. |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 265 | bool SimplifyCommutative(BinaryOperator &I); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 266 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 267 | bool SimplifyDemandedBits(Value *V, uint64_t Mask, |
| 268 | uint64_t &KnownZero, uint64_t &KnownOne, |
| 269 | unsigned Depth = 0); |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 270 | |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 271 | Value *SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, |
| 272 | uint64_t &UndefElts, unsigned Depth = 0); |
| 273 | |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 274 | // FoldOpIntoPhi - Given a binary operator or cast instruction which has a |
| 275 | // PHI node as operand #0, see if we can fold the instruction into the PHI |
| 276 | // (which is only possible if all operands to the PHI are constants). |
| 277 | Instruction *FoldOpIntoPhi(Instruction &I); |
| 278 | |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 279 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 280 | // operator and they all are only used by the PHI, PHI together their |
| 281 | // inputs, and do the operation once, to the result of the PHI. |
| 282 | Instruction *FoldPHIArgOpIntoPHI(PHINode &PN); |
Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 283 | Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN); |
| 284 | |
| 285 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 286 | Instruction *OptAndOp(Instruction *Op, ConstantIntegral *OpRHS, |
| 287 | ConstantIntegral *AndRHS, BinaryOperator &TheAnd); |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 288 | |
| 289 | Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantIntegral *Mask, |
| 290 | bool isSub, Instruction &I); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 291 | Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
| 292 | bool Inside, Instruction &IB); |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 293 | Instruction *PromoteCastOfAllocation(CastInst &CI, AllocationInst &AI); |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 294 | Instruction *MatchBSwap(BinaryOperator &I); |
| 295 | |
Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 296 | Value *EvaluateInDifferentType(Value *V, const Type *Ty); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 297 | }; |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 298 | |
Chris Lattner | c2d3d31 | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 299 | RegisterPass<InstCombiner> X("instcombine", "Combine redundant instructions"); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 302 | // getComplexity: Assign a complexity or rank value to LLVM Values... |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 303 | // 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 304 | static unsigned getComplexity(Value *V) { |
| 305 | if (isa<Instruction>(V)) { |
| 306 | if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V)) |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 307 | return 3; |
| 308 | return 4; |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 309 | } |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 310 | if (isa<Argument>(V)) return 3; |
| 311 | return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2; |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 312 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 313 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 314 | // isOnlyUse - Return true if this instruction will be deleted if we stop using |
| 315 | // it. |
| 316 | static bool isOnlyUse(Value *V) { |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 317 | return V->hasOneUse() || isa<Constant>(V); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 320 | // getPromotedType - Return the specified type promoted as it would be to pass |
| 321 | // though a va_arg area... |
| 322 | static const Type *getPromotedType(const Type *Ty) { |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 323 | switch (Ty->getTypeID()) { |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 324 | case Type::SByteTyID: |
| 325 | case Type::ShortTyID: return Type::IntTy; |
| 326 | case Type::UByteTyID: |
| 327 | case Type::UShortTyID: return Type::UIntTy; |
| 328 | case Type::FloatTyID: return Type::DoubleTy; |
| 329 | default: return Ty; |
| 330 | } |
| 331 | } |
| 332 | |
Chris Lattner | 567b81f | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 333 | /// isCast - If the specified operand is a CastInst or a constant expr cast, |
| 334 | /// return the operand value, otherwise return null. |
| 335 | static Value *isCast(Value *V) { |
| 336 | if (CastInst *I = dyn_cast<CastInst>(V)) |
| 337 | return I->getOperand(0); |
| 338 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 339 | if (CE->getOpcode() == Instruction::Cast) |
| 340 | return CE->getOperand(0); |
| 341 | return 0; |
| 342 | } |
| 343 | |
Chris Lattner | 1d441ad | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 344 | enum CastType { |
| 345 | Noop = 0, |
| 346 | Truncate = 1, |
| 347 | Signext = 2, |
| 348 | Zeroext = 3 |
| 349 | }; |
| 350 | |
| 351 | /// getCastType - In the future, we will split the cast instruction into these |
| 352 | /// various types. Until then, we have to do the analysis here. |
| 353 | static CastType getCastType(const Type *Src, const Type *Dest) { |
| 354 | assert(Src->isIntegral() && Dest->isIntegral() && |
| 355 | "Only works on integral types!"); |
| 356 | unsigned SrcSize = Src->getPrimitiveSizeInBits(); |
| 357 | unsigned DestSize = Dest->getPrimitiveSizeInBits(); |
| 358 | |
| 359 | if (SrcSize == DestSize) return Noop; |
| 360 | if (SrcSize > DestSize) return Truncate; |
| 361 | if (Src->isSigned()) return Signext; |
| 362 | return Zeroext; |
| 363 | } |
| 364 | |
| 365 | |
| 366 | // isEliminableCastOfCast - Return true if it is valid to eliminate the CI |
| 367 | // instruction. |
| 368 | // |
| 369 | static bool isEliminableCastOfCast(const Type *SrcTy, const Type *MidTy, |
| 370 | const Type *DstTy, TargetData *TD) { |
| 371 | |
| 372 | // It is legal to eliminate the instruction if casting A->B->A if the sizes |
| 373 | // are identical and the bits don't get reinterpreted (for example |
| 374 | // int->float->int would not be allowed). |
| 375 | if (SrcTy == DstTy && SrcTy->isLosslesslyConvertibleTo(MidTy)) |
| 376 | return true; |
| 377 | |
| 378 | // If we are casting between pointer and integer types, treat pointers as |
| 379 | // integers of the appropriate size for the code below. |
| 380 | if (isa<PointerType>(SrcTy)) SrcTy = TD->getIntPtrType(); |
| 381 | if (isa<PointerType>(MidTy)) MidTy = TD->getIntPtrType(); |
| 382 | if (isa<PointerType>(DstTy)) DstTy = TD->getIntPtrType(); |
| 383 | |
| 384 | // Allow free casting and conversion of sizes as long as the sign doesn't |
| 385 | // change... |
| 386 | if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral()) { |
| 387 | CastType FirstCast = getCastType(SrcTy, MidTy); |
| 388 | CastType SecondCast = getCastType(MidTy, DstTy); |
| 389 | |
| 390 | // Capture the effect of these two casts. If the result is a legal cast, |
| 391 | // the CastType is stored here, otherwise a special code is used. |
| 392 | static const unsigned CastResult[] = { |
| 393 | // First cast is noop |
| 394 | 0, 1, 2, 3, |
| 395 | // First cast is a truncate |
| 396 | 1, 1, 4, 4, // trunc->extend is not safe to eliminate |
| 397 | // First cast is a sign ext |
| 398 | 2, 5, 2, 4, // signext->zeroext never ok |
| 399 | // First cast is a zero ext |
| 400 | 3, 5, 3, 3, |
| 401 | }; |
| 402 | |
| 403 | unsigned Result = CastResult[FirstCast*4+SecondCast]; |
| 404 | switch (Result) { |
| 405 | default: assert(0 && "Illegal table value!"); |
| 406 | case 0: |
| 407 | case 1: |
| 408 | case 2: |
| 409 | case 3: |
| 410 | // FIXME: in the future, when LLVM has explicit sign/zeroextends and |
| 411 | // truncates, we could eliminate more casts. |
| 412 | return (unsigned)getCastType(SrcTy, DstTy) == Result; |
| 413 | case 4: |
| 414 | return false; // Not possible to eliminate this here. |
| 415 | case 5: |
| 416 | // Sign or zero extend followed by truncate is always ok if the result |
| 417 | // is a truncate or noop. |
| 418 | CastType ResultCast = getCastType(SrcTy, DstTy); |
| 419 | if (ResultCast == Noop || ResultCast == Truncate) |
| 420 | return true; |
| 421 | // Otherwise we are still growing the value, we are only safe if the |
| 422 | // result will match the sign/zeroextendness of the result. |
| 423 | return ResultCast == FirstCast; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | // If this is a cast from 'float -> double -> integer', cast from |
| 428 | // 'float -> integer' directly, as the value isn't changed by the |
| 429 | // float->double conversion. |
| 430 | if (SrcTy->isFloatingPoint() && MidTy->isFloatingPoint() && |
| 431 | DstTy->isIntegral() && |
| 432 | SrcTy->getPrimitiveSize() < MidTy->getPrimitiveSize()) |
| 433 | return true; |
| 434 | |
| 435 | // Packed type conversions don't modify bits. |
| 436 | if (isa<PackedType>(SrcTy) && isa<PackedType>(MidTy) &&isa<PackedType>(DstTy)) |
| 437 | return true; |
| 438 | |
| 439 | return false; |
| 440 | } |
| 441 | |
| 442 | /// ValueRequiresCast - Return true if the cast from "V to Ty" actually results |
| 443 | /// in any code being generated. It does not require codegen if V is simple |
| 444 | /// enough or if the cast can be folded into other casts. |
| 445 | static bool ValueRequiresCast(const Value *V, const Type *Ty, TargetData *TD) { |
| 446 | if (V->getType() == Ty || isa<Constant>(V)) return false; |
| 447 | |
| 448 | // If this is a noop cast, it isn't real codegen. |
| 449 | if (V->getType()->isLosslesslyConvertibleTo(Ty)) |
| 450 | return false; |
| 451 | |
Chris Lattner | 99155be | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 452 | // If this is another cast that can be eliminated, it isn't codegen either. |
Chris Lattner | 1d441ad | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 453 | if (const CastInst *CI = dyn_cast<CastInst>(V)) |
| 454 | if (isEliminableCastOfCast(CI->getOperand(0)->getType(), CI->getType(), Ty, |
| 455 | TD)) |
| 456 | return false; |
| 457 | return true; |
| 458 | } |
| 459 | |
| 460 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 461 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 462 | /// casts that are known to not do anything... |
| 463 | /// |
| 464 | Value *InstCombiner::InsertOperandCastBefore(Value *V, const Type *DestTy, |
| 465 | Instruction *InsertBefore) { |
| 466 | if (V->getType() == DestTy) return V; |
| 467 | if (Constant *C = dyn_cast<Constant>(V)) |
| 468 | return ConstantExpr::getCast(C, DestTy); |
| 469 | |
Reid Spencer | 00c482b | 2006-10-26 19:19:06 +0000 | [diff] [blame] | 470 | return InsertCastBefore(V, DestTy, *InsertBefore); |
Chris Lattner | 1d441ad | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 473 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 474 | // operators: |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 475 | // |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 476 | // 1. Order operands such that they are listed from right (least complex) to |
| 477 | // left (most complex). This puts constants before unary operators before |
| 478 | // binary operators. |
| 479 | // |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 480 | // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) |
| 481 | // 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] | 482 | // |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 483 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 484 | bool Changed = false; |
| 485 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) |
| 486 | Changed = !I.swapOperands(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 487 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 488 | if (!I.isAssociative()) return Changed; |
| 489 | Instruction::BinaryOps Opcode = I.getOpcode(); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 490 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) |
| 491 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { |
| 492 | if (isa<Constant>(I.getOperand(1))) { |
Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 493 | Constant *Folded = ConstantExpr::get(I.getOpcode(), |
| 494 | cast<Constant>(I.getOperand(1)), |
| 495 | cast<Constant>(Op->getOperand(1))); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 496 | I.setOperand(0, Op->getOperand(0)); |
| 497 | I.setOperand(1, Folded); |
| 498 | return true; |
| 499 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) |
| 500 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && |
| 501 | isOnlyUse(Op) && isOnlyUse(Op1)) { |
| 502 | Constant *C1 = cast<Constant>(Op->getOperand(1)); |
| 503 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); |
| 504 | |
| 505 | // 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] | 506 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 507 | Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0), |
| 508 | Op1->getOperand(0), |
| 509 | Op1->getName(), &I); |
| 510 | WorkList.push_back(New); |
| 511 | I.setOperand(0, New); |
| 512 | I.setOperand(1, Folded); |
| 513 | return true; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 514 | } |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 515 | } |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 516 | return Changed; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 517 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 518 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 519 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
| 520 | // if the LHS is a constant zero (which is the 'negate' form). |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 521 | // |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 522 | static inline Value *dyn_castNegVal(Value *V) { |
| 523 | if (BinaryOperator::isNeg(V)) |
Chris Lattner | d6f636a | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 524 | return BinaryOperator::getNegArgument(V); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 525 | |
Chris Lattner | 9ad0d55 | 2004-12-14 20:08:06 +0000 | [diff] [blame] | 526 | // Constants can be considered to be negated values if they can be folded. |
| 527 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
| 528 | return ConstantExpr::getNeg(C); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 529 | return 0; |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 532 | static inline Value *dyn_castNotVal(Value *V) { |
| 533 | if (BinaryOperator::isNot(V)) |
Chris Lattner | d6f636a | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 534 | return BinaryOperator::getNotArgument(V); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 535 | |
| 536 | // Constants can be considered to be not'ed values... |
Chris Lattner | dd65d86 | 2003-04-30 22:34:06 +0000 | [diff] [blame] | 537 | if (ConstantIntegral *C = dyn_cast<ConstantIntegral>(V)) |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 538 | return ConstantExpr::getNot(C); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 539 | return 0; |
| 540 | } |
| 541 | |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 542 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
| 543 | // other computations (because it has a constant operand), return the |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 544 | // non-constant operand of the multiply, and set CST to point to the multiplier. |
| 545 | // Otherwise, return null. |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 546 | // |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 547 | static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) { |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 548 | if (V->hasOneUse() && V->getType()->isInteger()) |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 549 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 550 | if (I->getOpcode() == Instruction::Mul) |
Chris Lattner | 97013636 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 551 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 552 | return I->getOperand(0); |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 553 | if (I->getOpcode() == Instruction::Shl) |
Chris Lattner | 97013636 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 554 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) { |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 555 | // The multiplier is really 1 << CST. |
| 556 | Constant *One = ConstantInt::get(V->getType(), 1); |
| 557 | CST = cast<ConstantInt>(ConstantExpr::getShl(One, CST)); |
| 558 | return I->getOperand(0); |
| 559 | } |
| 560 | } |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 561 | return 0; |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 562 | } |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 563 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 564 | /// dyn_castGetElementPtr - If this is a getelementptr instruction or constant |
| 565 | /// expression, return it. |
| 566 | static User *dyn_castGetElementPtr(Value *V) { |
| 567 | if (isa<GetElementPtrInst>(V)) return cast<User>(V); |
| 568 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 569 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 570 | return cast<User>(V); |
| 571 | return false; |
| 572 | } |
| 573 | |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 574 | // AddOne, SubOne - Add or subtract a constant one from an integer constant... |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 575 | static ConstantInt *AddOne(ConstantInt *C) { |
| 576 | return cast<ConstantInt>(ConstantExpr::getAdd(C, |
| 577 | ConstantInt::get(C->getType(), 1))); |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 578 | } |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 579 | static ConstantInt *SubOne(ConstantInt *C) { |
| 580 | return cast<ConstantInt>(ConstantExpr::getSub(C, |
| 581 | ConstantInt::get(C->getType(), 1))); |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 584 | /// GetConstantInType - Return a ConstantInt with the specified type and value. |
| 585 | /// |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 586 | static ConstantIntegral *GetConstantInType(const Type *Ty, uint64_t Val) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 587 | if (Ty->isUnsigned()) |
| 588 | return ConstantInt::get(Ty, Val); |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 589 | else if (Ty->getTypeID() == Type::BoolTyID) |
| 590 | return ConstantBool::get(Val); |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 591 | int64_t SVal = Val; |
| 592 | SVal <<= 64-Ty->getPrimitiveSizeInBits(); |
| 593 | SVal >>= 64-Ty->getPrimitiveSizeInBits(); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 594 | return ConstantInt::get(Ty, SVal); |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 598 | /// ComputeMaskedBits - Determine which of the bits specified in Mask are |
| 599 | /// known to be either zero or one and return them in the KnownZero/KnownOne |
| 600 | /// bitsets. This code only analyzes bits in Mask, in order to short-circuit |
| 601 | /// processing. |
| 602 | static void ComputeMaskedBits(Value *V, uint64_t Mask, uint64_t &KnownZero, |
| 603 | uint64_t &KnownOne, unsigned Depth = 0) { |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 604 | // Note, we cannot consider 'undef' to be "IsZero" here. The problem is that |
| 605 | // 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] | 606 | // 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] | 607 | // optimized based on the contradictory assumption that it is non-zero. |
| 608 | // Because instcombine aggressively folds operations with undef args anyway, |
| 609 | // this won't lose us code quality. |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 610 | if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V)) { |
| 611 | // We know all of the bits for a constant! |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 612 | KnownOne = CI->getZExtValue() & Mask; |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 613 | KnownZero = ~KnownOne & Mask; |
| 614 | return; |
| 615 | } |
| 616 | |
| 617 | KnownZero = KnownOne = 0; // Don't know anything. |
Chris Lattner | 92a6865 | 2006-02-07 08:05:22 +0000 | [diff] [blame] | 618 | if (Depth == 6 || Mask == 0) |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 619 | return; // Limit search depth. |
| 620 | |
| 621 | uint64_t KnownZero2, KnownOne2; |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 622 | Instruction *I = dyn_cast<Instruction>(V); |
| 623 | if (!I) return; |
| 624 | |
Chris Lattner | fb29692 | 2006-05-04 17:33:35 +0000 | [diff] [blame] | 625 | Mask &= V->getType()->getIntegralTypeMask(); |
| 626 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 627 | switch (I->getOpcode()) { |
| 628 | case Instruction::And: |
| 629 | // If either the LHS or the RHS are Zero, the result is zero. |
| 630 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
| 631 | Mask &= ~KnownZero; |
| 632 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); |
| 633 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 634 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 635 | |
| 636 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 637 | KnownOne &= KnownOne2; |
| 638 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 639 | KnownZero |= KnownZero2; |
| 640 | return; |
| 641 | case Instruction::Or: |
| 642 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
| 643 | Mask &= ~KnownOne; |
| 644 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); |
| 645 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 646 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 647 | |
| 648 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 649 | KnownZero &= KnownZero2; |
| 650 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 651 | KnownOne |= KnownOne2; |
| 652 | return; |
| 653 | case Instruction::Xor: { |
| 654 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
| 655 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); |
| 656 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 657 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 658 | |
| 659 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 660 | uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); |
| 661 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 662 | KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); |
| 663 | KnownZero = KnownZeroOut; |
| 664 | return; |
| 665 | } |
| 666 | case Instruction::Select: |
| 667 | ComputeMaskedBits(I->getOperand(2), Mask, KnownZero, KnownOne, Depth+1); |
| 668 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1); |
| 669 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 670 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 671 | |
| 672 | // Only known if known in both the LHS and RHS. |
| 673 | KnownOne &= KnownOne2; |
| 674 | KnownZero &= KnownZero2; |
| 675 | return; |
| 676 | case Instruction::Cast: { |
| 677 | const Type *SrcTy = I->getOperand(0)->getType(); |
| 678 | if (!SrcTy->isIntegral()) return; |
| 679 | |
| 680 | // If this is an integer truncate or noop, just look in the input. |
| 681 | if (SrcTy->getPrimitiveSizeInBits() >= |
| 682 | I->getType()->getPrimitiveSizeInBits()) { |
| 683 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 684 | return; |
| 685 | } |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 686 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 687 | // Sign or Zero extension. Compute the bits in the result that are not |
| 688 | // present in the input. |
| 689 | uint64_t NotIn = ~SrcTy->getIntegralTypeMask(); |
| 690 | uint64_t NewBits = I->getType()->getIntegralTypeMask() & NotIn; |
Chris Lattner | 62010c4 | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 691 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 692 | // Handle zero extension. |
| 693 | if (!SrcTy->isSigned()) { |
| 694 | Mask &= SrcTy->getIntegralTypeMask(); |
| 695 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
| 696 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 697 | // The top bits are known to be zero. |
| 698 | KnownZero |= NewBits; |
| 699 | } else { |
| 700 | // Sign extension. |
| 701 | Mask &= SrcTy->getIntegralTypeMask(); |
| 702 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
| 703 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Chris Lattner | 92a6865 | 2006-02-07 08:05:22 +0000 | [diff] [blame] | 704 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 705 | // If the sign bit of the input is known set or clear, then we know the |
| 706 | // top bits of the result. |
| 707 | uint64_t InSignBit = 1ULL << (SrcTy->getPrimitiveSizeInBits()-1); |
| 708 | if (KnownZero & InSignBit) { // Input sign bit known zero |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 709 | KnownZero |= NewBits; |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 710 | KnownOne &= ~NewBits; |
| 711 | } else if (KnownOne & InSignBit) { // Input sign bit known set |
| 712 | KnownOne |= NewBits; |
| 713 | KnownZero &= ~NewBits; |
| 714 | } else { // Input sign bit unknown |
| 715 | KnownZero &= ~NewBits; |
| 716 | KnownOne &= ~NewBits; |
| 717 | } |
| 718 | } |
| 719 | return; |
| 720 | } |
| 721 | case Instruction::Shl: |
| 722 | // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 723 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 724 | uint64_t ShiftAmt = SA->getZExtValue(); |
| 725 | Mask >>= ShiftAmt; |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 726 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
| 727 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 728 | KnownZero <<= ShiftAmt; |
| 729 | KnownOne <<= ShiftAmt; |
| 730 | KnownZero |= (1ULL << ShiftAmt)-1; // low bits known zero. |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 731 | return; |
| 732 | } |
| 733 | break; |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 734 | case Instruction::LShr: |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 735 | // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 736 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 737 | // Compute the new bits that are at the top now. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 738 | uint64_t ShiftAmt = SA->getZExtValue(); |
| 739 | uint64_t HighBits = (1ULL << ShiftAmt)-1; |
| 740 | HighBits <<= I->getType()->getPrimitiveSizeInBits()-ShiftAmt; |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 741 | |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 742 | // Unsigned shift right. |
| 743 | Mask <<= ShiftAmt; |
| 744 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero,KnownOne,Depth+1); |
| 745 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 746 | KnownZero >>= ShiftAmt; |
| 747 | KnownOne >>= ShiftAmt; |
| 748 | KnownZero |= HighBits; // high bits known zero. |
| 749 | return; |
| 750 | } |
| 751 | break; |
| 752 | case Instruction::AShr: |
| 753 | // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
| 754 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 755 | // Compute the new bits that are at the top now. |
| 756 | uint64_t ShiftAmt = SA->getZExtValue(); |
| 757 | uint64_t HighBits = (1ULL << ShiftAmt)-1; |
| 758 | HighBits <<= I->getType()->getPrimitiveSizeInBits()-ShiftAmt; |
| 759 | |
| 760 | // Signed shift right. |
| 761 | Mask <<= ShiftAmt; |
| 762 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero,KnownOne,Depth+1); |
| 763 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 764 | KnownZero >>= ShiftAmt; |
| 765 | KnownOne >>= ShiftAmt; |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 766 | |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 767 | // Handle the sign bits. |
| 768 | uint64_t SignBit = 1ULL << (I->getType()->getPrimitiveSizeInBits()-1); |
| 769 | SignBit >>= ShiftAmt; // Adjust to where it is now in the mask. |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 770 | |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 771 | if (KnownZero & SignBit) { // New bits are known zero. |
| 772 | KnownZero |= HighBits; |
| 773 | } else if (KnownOne & SignBit) { // New bits are known one. |
| 774 | KnownOne |= HighBits; |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 775 | } |
| 776 | return; |
Chris Lattner | 62010c4 | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 777 | } |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 778 | break; |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 779 | } |
Chris Lattner | 92a6865 | 2006-02-07 08:05:22 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use |
| 783 | /// this predicate to simplify operations downstream. Mask is known to be zero |
| 784 | /// for bits that V cannot have. |
| 785 | static bool MaskedValueIsZero(Value *V, uint64_t Mask, unsigned Depth = 0) { |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 786 | uint64_t KnownZero, KnownOne; |
| 787 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne, Depth); |
| 788 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 789 | return (KnownZero & Mask) == Mask; |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 792 | /// ShrinkDemandedConstant - Check to see if the specified operand of the |
| 793 | /// specified instruction is a constant integer. If so, check to see if there |
| 794 | /// are any bits set in the constant that are not demanded. If so, shrink the |
| 795 | /// constant and return true. |
| 796 | static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, |
| 797 | uint64_t Demanded) { |
| 798 | ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo)); |
| 799 | if (!OpC) return false; |
| 800 | |
| 801 | // If there are no bits set that aren't demanded, nothing to do. |
| 802 | if ((~Demanded & OpC->getZExtValue()) == 0) |
| 803 | return false; |
| 804 | |
| 805 | // This is producing any bits that are not needed, shrink the RHS. |
| 806 | uint64_t Val = Demanded & OpC->getZExtValue(); |
| 807 | I->setOperand(OpNo, GetConstantInType(OpC->getType(), Val)); |
| 808 | return true; |
| 809 | } |
| 810 | |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 811 | // ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a |
| 812 | // set of known zero and one bits, compute the maximum and minimum values that |
| 813 | // could have the specified known zero and known one bits, returning them in |
| 814 | // min/max. |
| 815 | static void ComputeSignedMinMaxValuesFromKnownBits(const Type *Ty, |
| 816 | uint64_t KnownZero, |
| 817 | uint64_t KnownOne, |
| 818 | int64_t &Min, int64_t &Max) { |
| 819 | uint64_t TypeBits = Ty->getIntegralTypeMask(); |
| 820 | uint64_t UnknownBits = ~(KnownZero|KnownOne) & TypeBits; |
| 821 | |
| 822 | uint64_t SignBit = 1ULL << (Ty->getPrimitiveSizeInBits()-1); |
| 823 | |
| 824 | // The minimum value is when all unknown bits are zeros, EXCEPT for the sign |
| 825 | // bit if it is unknown. |
| 826 | Min = KnownOne; |
| 827 | Max = KnownOne|UnknownBits; |
| 828 | |
| 829 | if (SignBit & UnknownBits) { // Sign bit is unknown |
| 830 | Min |= SignBit; |
| 831 | Max &= ~SignBit; |
| 832 | } |
| 833 | |
| 834 | // Sign extend the min/max values. |
| 835 | int ShAmt = 64-Ty->getPrimitiveSizeInBits(); |
| 836 | Min = (Min << ShAmt) >> ShAmt; |
| 837 | Max = (Max << ShAmt) >> ShAmt; |
| 838 | } |
| 839 | |
| 840 | // ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and |
| 841 | // a set of known zero and one bits, compute the maximum and minimum values that |
| 842 | // could have the specified known zero and known one bits, returning them in |
| 843 | // min/max. |
| 844 | static void ComputeUnsignedMinMaxValuesFromKnownBits(const Type *Ty, |
| 845 | uint64_t KnownZero, |
| 846 | uint64_t KnownOne, |
| 847 | uint64_t &Min, |
| 848 | uint64_t &Max) { |
| 849 | uint64_t TypeBits = Ty->getIntegralTypeMask(); |
| 850 | uint64_t UnknownBits = ~(KnownZero|KnownOne) & TypeBits; |
| 851 | |
| 852 | // The minimum value is when the unknown bits are all zeros. |
| 853 | Min = KnownOne; |
| 854 | // The maximum value is when the unknown bits are all ones. |
| 855 | Max = KnownOne|UnknownBits; |
| 856 | } |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 857 | |
| 858 | |
| 859 | /// SimplifyDemandedBits - Look at V. At this point, we know that only the |
| 860 | /// DemandedMask bits of the result of V are ever used downstream. If we can |
| 861 | /// use this information to simplify V, do so and return true. Otherwise, |
| 862 | /// analyze the expression and return a mask of KnownOne and KnownZero bits for |
| 863 | /// the expression (used to simplify the caller). The KnownZero/One bits may |
| 864 | /// only be accurate for those bits in the DemandedMask. |
| 865 | bool InstCombiner::SimplifyDemandedBits(Value *V, uint64_t DemandedMask, |
| 866 | uint64_t &KnownZero, uint64_t &KnownOne, |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 867 | unsigned Depth) { |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 868 | if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V)) { |
| 869 | // We know all of the bits for a constant! |
| 870 | KnownOne = CI->getZExtValue() & DemandedMask; |
| 871 | KnownZero = ~KnownOne & DemandedMask; |
| 872 | return false; |
| 873 | } |
| 874 | |
| 875 | KnownZero = KnownOne = 0; |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 876 | if (!V->hasOneUse()) { // Other users may use these bits. |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 877 | if (Depth != 0) { // Not at the root. |
| 878 | // Just compute the KnownZero/KnownOne bits to simplify things downstream. |
| 879 | ComputeMaskedBits(V, DemandedMask, KnownZero, KnownOne, Depth); |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 880 | return false; |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 881 | } |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 882 | // 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] | 883 | // just set the DemandedMask to all bits. |
| 884 | DemandedMask = V->getType()->getIntegralTypeMask(); |
| 885 | } else if (DemandedMask == 0) { // Not demanding any bits from V. |
Chris Lattner | 92a6865 | 2006-02-07 08:05:22 +0000 | [diff] [blame] | 886 | if (V != UndefValue::get(V->getType())) |
| 887 | return UpdateValueUsesWith(V, UndefValue::get(V->getType())); |
| 888 | return false; |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 889 | } else if (Depth == 6) { // Limit search depth. |
| 890 | return false; |
| 891 | } |
| 892 | |
| 893 | Instruction *I = dyn_cast<Instruction>(V); |
| 894 | if (!I) return false; // Only analyze instructions. |
| 895 | |
Chris Lattner | fb29692 | 2006-05-04 17:33:35 +0000 | [diff] [blame] | 896 | DemandedMask &= V->getType()->getIntegralTypeMask(); |
| 897 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 898 | uint64_t KnownZero2, KnownOne2; |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 899 | switch (I->getOpcode()) { |
| 900 | default: break; |
| 901 | case Instruction::And: |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 902 | // If either the LHS or the RHS are Zero, the result is zero. |
| 903 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 904 | KnownZero, KnownOne, Depth+1)) |
| 905 | return true; |
| 906 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 907 | |
| 908 | // If something is known zero on the RHS, the bits aren't demanded on the |
| 909 | // LHS. |
| 910 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~KnownZero, |
| 911 | KnownZero2, KnownOne2, Depth+1)) |
| 912 | return true; |
| 913 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 914 | |
| 915 | // If all of the demanded bits are known one on one side, return the other. |
| 916 | // These bits cannot contribute to the result of the 'and'. |
| 917 | if ((DemandedMask & ~KnownZero2 & KnownOne) == (DemandedMask & ~KnownZero2)) |
| 918 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 919 | if ((DemandedMask & ~KnownZero & KnownOne2) == (DemandedMask & ~KnownZero)) |
| 920 | return UpdateValueUsesWith(I, I->getOperand(1)); |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 921 | |
| 922 | // If all of the demanded bits in the inputs are known zeros, return zero. |
| 923 | if ((DemandedMask & (KnownZero|KnownZero2)) == DemandedMask) |
| 924 | return UpdateValueUsesWith(I, Constant::getNullValue(I->getType())); |
| 925 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 926 | // If the RHS is a constant, see if we can simplify it. |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 927 | if (ShrinkDemandedConstant(I, 1, DemandedMask & ~KnownZero2)) |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 928 | return UpdateValueUsesWith(I, I); |
| 929 | |
| 930 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 931 | KnownOne &= KnownOne2; |
| 932 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 933 | KnownZero |= KnownZero2; |
| 934 | break; |
| 935 | case Instruction::Or: |
| 936 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 937 | KnownZero, KnownOne, Depth+1)) |
| 938 | return true; |
| 939 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 940 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~KnownOne, |
| 941 | KnownZero2, KnownOne2, Depth+1)) |
| 942 | return true; |
| 943 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 944 | |
| 945 | // If all of the demanded bits are known zero on one side, return the other. |
| 946 | // These bits cannot contribute to the result of the 'or'. |
Jeff Cohen | 0add83e | 2006-02-18 03:20:33 +0000 | [diff] [blame] | 947 | if ((DemandedMask & ~KnownOne2 & KnownZero) == (DemandedMask & ~KnownOne2)) |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 948 | return UpdateValueUsesWith(I, I->getOperand(0)); |
Jeff Cohen | 0add83e | 2006-02-18 03:20:33 +0000 | [diff] [blame] | 949 | if ((DemandedMask & ~KnownOne & KnownZero2) == (DemandedMask & ~KnownOne)) |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 950 | return UpdateValueUsesWith(I, I->getOperand(1)); |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 951 | |
| 952 | // If all of the potentially set bits on one side are known to be set on |
| 953 | // the other side, just use the 'other' side. |
| 954 | if ((DemandedMask & (~KnownZero) & KnownOne2) == |
| 955 | (DemandedMask & (~KnownZero))) |
| 956 | return UpdateValueUsesWith(I, I->getOperand(0)); |
Nate Begeman | 8a77efe | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 957 | if ((DemandedMask & (~KnownZero2) & KnownOne) == |
| 958 | (DemandedMask & (~KnownZero2))) |
| 959 | return UpdateValueUsesWith(I, I->getOperand(1)); |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 960 | |
| 961 | // If the RHS is a constant, see if we can simplify it. |
| 962 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 963 | return UpdateValueUsesWith(I, I); |
| 964 | |
| 965 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 966 | KnownZero &= KnownZero2; |
| 967 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 968 | KnownOne |= KnownOne2; |
| 969 | break; |
| 970 | case Instruction::Xor: { |
| 971 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 972 | KnownZero, KnownOne, Depth+1)) |
| 973 | return true; |
| 974 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 975 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 976 | KnownZero2, KnownOne2, Depth+1)) |
| 977 | return true; |
| 978 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 979 | |
| 980 | // If all of the demanded bits are known zero on one side, return the other. |
| 981 | // These bits cannot contribute to the result of the 'xor'. |
| 982 | if ((DemandedMask & KnownZero) == DemandedMask) |
| 983 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 984 | if ((DemandedMask & KnownZero2) == DemandedMask) |
| 985 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 986 | |
| 987 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 988 | uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); |
| 989 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 990 | uint64_t KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); |
| 991 | |
| 992 | // If all of the unknown bits are known to be zero on one side or the other |
| 993 | // (but not both) turn this into an *inclusive* or. |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 994 | // 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] | 995 | if (uint64_t UnknownBits = DemandedMask & ~(KnownZeroOut|KnownOneOut)) { |
| 996 | if ((UnknownBits & (KnownZero|KnownZero2)) == UnknownBits) { |
| 997 | Instruction *Or = |
| 998 | BinaryOperator::createOr(I->getOperand(0), I->getOperand(1), |
| 999 | I->getName()); |
| 1000 | InsertNewInstBefore(Or, *I); |
| 1001 | return UpdateValueUsesWith(I, Or); |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1002 | } |
| 1003 | } |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1004 | |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 1005 | // If all of the demanded bits on one side are known, and all of the set |
| 1006 | // bits on that side are also known to be set on the other side, turn this |
| 1007 | // into an AND, as we know the bits will be cleared. |
| 1008 | // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 |
| 1009 | if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask) { // all known |
| 1010 | if ((KnownOne & KnownOne2) == KnownOne) { |
| 1011 | Constant *AndC = GetConstantInType(I->getType(), |
| 1012 | ~KnownOne & DemandedMask); |
| 1013 | Instruction *And = |
| 1014 | BinaryOperator::createAnd(I->getOperand(0), AndC, "tmp"); |
| 1015 | InsertNewInstBefore(And, *I); |
| 1016 | return UpdateValueUsesWith(I, And); |
| 1017 | } |
| 1018 | } |
| 1019 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1020 | // If the RHS is a constant, see if we can simplify it. |
| 1021 | // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1. |
| 1022 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 1023 | return UpdateValueUsesWith(I, I); |
| 1024 | |
| 1025 | KnownZero = KnownZeroOut; |
| 1026 | KnownOne = KnownOneOut; |
| 1027 | break; |
| 1028 | } |
| 1029 | case Instruction::Select: |
| 1030 | if (SimplifyDemandedBits(I->getOperand(2), DemandedMask, |
| 1031 | KnownZero, KnownOne, Depth+1)) |
| 1032 | return true; |
| 1033 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 1034 | KnownZero2, KnownOne2, Depth+1)) |
| 1035 | return true; |
| 1036 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1037 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 1038 | |
| 1039 | // If the operands are constants, see if we can simplify them. |
| 1040 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 1041 | return UpdateValueUsesWith(I, I); |
| 1042 | if (ShrinkDemandedConstant(I, 2, DemandedMask)) |
| 1043 | return UpdateValueUsesWith(I, I); |
| 1044 | |
| 1045 | // Only known if known in both the LHS and RHS. |
| 1046 | KnownOne &= KnownOne2; |
| 1047 | KnownZero &= KnownZero2; |
| 1048 | break; |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1049 | case Instruction::Cast: { |
| 1050 | const Type *SrcTy = I->getOperand(0)->getType(); |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1051 | if (!SrcTy->isIntegral()) return false; |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1052 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1053 | // If this is an integer truncate or noop, just look in the input. |
| 1054 | if (SrcTy->getPrimitiveSizeInBits() >= |
| 1055 | I->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 850465d | 2006-09-16 03:14:10 +0000 | [diff] [blame] | 1056 | // Cast to bool is a comparison against 0, which demands all bits. We |
| 1057 | // can't propagate anything useful up. |
| 1058 | if (I->getType() == Type::BoolTy) |
| 1059 | break; |
| 1060 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1061 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1062 | KnownZero, KnownOne, Depth+1)) |
| 1063 | return true; |
| 1064 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1065 | break; |
| 1066 | } |
| 1067 | |
| 1068 | // Sign or Zero extension. Compute the bits in the result that are not |
| 1069 | // present in the input. |
| 1070 | uint64_t NotIn = ~SrcTy->getIntegralTypeMask(); |
| 1071 | uint64_t NewBits = I->getType()->getIntegralTypeMask() & NotIn; |
| 1072 | |
| 1073 | // Handle zero extension. |
| 1074 | if (!SrcTy->isSigned()) { |
| 1075 | DemandedMask &= SrcTy->getIntegralTypeMask(); |
| 1076 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1077 | KnownZero, KnownOne, Depth+1)) |
| 1078 | return true; |
| 1079 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1080 | // The top bits are known to be zero. |
| 1081 | KnownZero |= NewBits; |
| 1082 | } else { |
| 1083 | // Sign extension. |
Chris Lattner | 7d85228 | 2006-02-13 22:41:07 +0000 | [diff] [blame] | 1084 | uint64_t InSignBit = 1ULL << (SrcTy->getPrimitiveSizeInBits()-1); |
| 1085 | int64_t InputDemandedBits = DemandedMask & SrcTy->getIntegralTypeMask(); |
| 1086 | |
| 1087 | // If any of the sign extended bits are demanded, we know that the sign |
| 1088 | // bit is demanded. |
| 1089 | if (NewBits & DemandedMask) |
| 1090 | InputDemandedBits |= InSignBit; |
| 1091 | |
| 1092 | if (SimplifyDemandedBits(I->getOperand(0), InputDemandedBits, |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1093 | KnownZero, KnownOne, Depth+1)) |
| 1094 | return true; |
| 1095 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1096 | |
| 1097 | // If the sign bit of the input is known set or clear, then we know the |
| 1098 | // top bits of the result. |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1099 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1100 | // If the input sign bit is known zero, or if the NewBits are not demanded |
| 1101 | // convert this into a zero extension. |
| 1102 | if ((KnownZero & InSignBit) || (NewBits & ~DemandedMask) == NewBits) { |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1103 | // Convert to unsigned first. |
Reid Spencer | 00c482b | 2006-10-26 19:19:06 +0000 | [diff] [blame] | 1104 | Value *NewVal = |
| 1105 | InsertCastBefore(I->getOperand(0), SrcTy->getUnsignedVersion(), *I); |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1106 | // Then cast that to the destination type. |
Chris Lattner | 4431482 | 2006-02-07 19:07:40 +0000 | [diff] [blame] | 1107 | NewVal = new CastInst(NewVal, I->getType(), I->getName()); |
Reid Spencer | 00c482b | 2006-10-26 19:19:06 +0000 | [diff] [blame] | 1108 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1109 | return UpdateValueUsesWith(I, NewVal); |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1110 | } else if (KnownOne & InSignBit) { // Input sign bit known set |
| 1111 | KnownOne |= NewBits; |
| 1112 | KnownZero &= ~NewBits; |
| 1113 | } else { // Input sign bit unknown |
| 1114 | KnownZero &= ~NewBits; |
| 1115 | KnownOne &= ~NewBits; |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1116 | } |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1117 | } |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1118 | break; |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1119 | } |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1120 | case Instruction::Shl: |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1121 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1122 | uint64_t ShiftAmt = SA->getZExtValue(); |
| 1123 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask >> ShiftAmt, |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1124 | KnownZero, KnownOne, Depth+1)) |
| 1125 | return true; |
| 1126 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1127 | KnownZero <<= ShiftAmt; |
| 1128 | KnownOne <<= ShiftAmt; |
| 1129 | KnownZero |= (1ULL << ShiftAmt) - 1; // low bits known zero. |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1130 | } |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1131 | break; |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1132 | case Instruction::LShr: |
| 1133 | // For a logical shift right |
| 1134 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1135 | unsigned ShiftAmt = SA->getZExtValue(); |
| 1136 | |
| 1137 | // Compute the new bits that are at the top now. |
| 1138 | uint64_t HighBits = (1ULL << ShiftAmt)-1; |
| 1139 | HighBits <<= I->getType()->getPrimitiveSizeInBits() - ShiftAmt; |
| 1140 | uint64_t TypeMask = I->getType()->getIntegralTypeMask(); |
| 1141 | // Unsigned shift right. |
| 1142 | if (SimplifyDemandedBits(I->getOperand(0), |
| 1143 | (DemandedMask << ShiftAmt) & TypeMask, |
| 1144 | KnownZero, KnownOne, Depth+1)) |
| 1145 | return true; |
| 1146 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1147 | KnownZero &= TypeMask; |
| 1148 | KnownOne &= TypeMask; |
| 1149 | KnownZero >>= ShiftAmt; |
| 1150 | KnownOne >>= ShiftAmt; |
| 1151 | KnownZero |= HighBits; // high bits known zero. |
| 1152 | } |
| 1153 | break; |
| 1154 | case Instruction::AShr: |
Chris Lattner | 420c4bc | 2006-09-18 04:31:40 +0000 | [diff] [blame] | 1155 | // If this is an arithmetic shift right and only the low-bit is set, we can |
| 1156 | // always convert this into a logical shr, even if the shift amount is |
| 1157 | // variable. The low bit of the shift cannot be an input sign bit unless |
| 1158 | // the shift amount is >= the size of the datatype, which is undefined. |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1159 | if (DemandedMask == 1) { |
| 1160 | // Perform the logical shift right. |
| 1161 | Value *NewVal = new ShiftInst(Instruction::LShr, I->getOperand(0), |
| 1162 | I->getOperand(1), I->getName()); |
Reid Spencer | 00c482b | 2006-10-26 19:19:06 +0000 | [diff] [blame] | 1163 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); |
Chris Lattner | 420c4bc | 2006-09-18 04:31:40 +0000 | [diff] [blame] | 1164 | return UpdateValueUsesWith(I, NewVal); |
| 1165 | } |
| 1166 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1167 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1168 | unsigned ShiftAmt = SA->getZExtValue(); |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1169 | |
| 1170 | // Compute the new bits that are at the top now. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1171 | uint64_t HighBits = (1ULL << ShiftAmt)-1; |
| 1172 | HighBits <<= I->getType()->getPrimitiveSizeInBits() - ShiftAmt; |
Chris Lattner | 68e7475 | 2006-02-13 06:09:08 +0000 | [diff] [blame] | 1173 | uint64_t TypeMask = I->getType()->getIntegralTypeMask(); |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1174 | // Signed shift right. |
| 1175 | if (SimplifyDemandedBits(I->getOperand(0), |
| 1176 | (DemandedMask << ShiftAmt) & TypeMask, |
| 1177 | KnownZero, KnownOne, Depth+1)) |
| 1178 | return true; |
| 1179 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1180 | KnownZero &= TypeMask; |
| 1181 | KnownOne &= TypeMask; |
| 1182 | KnownZero >>= ShiftAmt; |
| 1183 | KnownOne >>= ShiftAmt; |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1184 | |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1185 | // Handle the sign bits. |
| 1186 | uint64_t SignBit = 1ULL << (I->getType()->getPrimitiveSizeInBits()-1); |
| 1187 | SignBit >>= ShiftAmt; // Adjust to where it is now in the mask. |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1188 | |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1189 | // If the input sign bit is known to be zero, or if none of the top bits |
| 1190 | // are demanded, turn this into an unsigned shift right. |
| 1191 | if ((KnownZero & SignBit) || (HighBits & ~DemandedMask) == HighBits) { |
| 1192 | // Perform the logical shift right. |
| 1193 | Value *NewVal = new ShiftInst(Instruction::LShr, I->getOperand(0), |
| 1194 | SA, I->getName()); |
| 1195 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); |
| 1196 | return UpdateValueUsesWith(I, NewVal); |
| 1197 | } else if (KnownOne & SignBit) { // New bits are known one. |
| 1198 | KnownOne |= HighBits; |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1199 | } |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1200 | } |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1201 | break; |
| 1202 | } |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1203 | |
| 1204 | // If the client is only demanding bits that we know, return the known |
| 1205 | // constant. |
| 1206 | if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask) |
| 1207 | return UpdateValueUsesWith(I, GetConstantInType(I->getType(), KnownOne)); |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1208 | return false; |
| 1209 | } |
| 1210 | |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1211 | |
| 1212 | /// SimplifyDemandedVectorElts - The specified value producecs a vector with |
| 1213 | /// 64 or fewer elements. DemandedElts contains the set of elements that are |
| 1214 | /// actually used by the caller. This method analyzes which elements of the |
| 1215 | /// operand are undef and returns that information in UndefElts. |
| 1216 | /// |
| 1217 | /// If the information about demanded elements can be used to simplify the |
| 1218 | /// operation, the operation is simplified, then the resultant value is |
| 1219 | /// returned. This returns null if no change was made. |
| 1220 | Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, |
| 1221 | uint64_t &UndefElts, |
| 1222 | unsigned Depth) { |
| 1223 | unsigned VWidth = cast<PackedType>(V->getType())->getNumElements(); |
| 1224 | assert(VWidth <= 64 && "Vector too wide to analyze!"); |
| 1225 | uint64_t EltMask = ~0ULL >> (64-VWidth); |
| 1226 | assert(DemandedElts != EltMask && (DemandedElts & ~EltMask) == 0 && |
| 1227 | "Invalid DemandedElts!"); |
| 1228 | |
| 1229 | if (isa<UndefValue>(V)) { |
| 1230 | // If the entire vector is undefined, just return this info. |
| 1231 | UndefElts = EltMask; |
| 1232 | return 0; |
| 1233 | } else if (DemandedElts == 0) { // If nothing is demanded, provide undef. |
| 1234 | UndefElts = EltMask; |
| 1235 | return UndefValue::get(V->getType()); |
| 1236 | } |
| 1237 | |
| 1238 | UndefElts = 0; |
| 1239 | if (ConstantPacked *CP = dyn_cast<ConstantPacked>(V)) { |
| 1240 | const Type *EltTy = cast<PackedType>(V->getType())->getElementType(); |
| 1241 | Constant *Undef = UndefValue::get(EltTy); |
| 1242 | |
| 1243 | std::vector<Constant*> Elts; |
| 1244 | for (unsigned i = 0; i != VWidth; ++i) |
| 1245 | if (!(DemandedElts & (1ULL << i))) { // If not demanded, set to undef. |
| 1246 | Elts.push_back(Undef); |
| 1247 | UndefElts |= (1ULL << i); |
| 1248 | } else if (isa<UndefValue>(CP->getOperand(i))) { // Already undef. |
| 1249 | Elts.push_back(Undef); |
| 1250 | UndefElts |= (1ULL << i); |
| 1251 | } else { // Otherwise, defined. |
| 1252 | Elts.push_back(CP->getOperand(i)); |
| 1253 | } |
| 1254 | |
| 1255 | // If we changed the constant, return it. |
| 1256 | Constant *NewCP = ConstantPacked::get(Elts); |
| 1257 | return NewCP != CP ? NewCP : 0; |
| 1258 | } else if (isa<ConstantAggregateZero>(V)) { |
| 1259 | // Simplify the CAZ to a ConstantPacked where the non-demanded elements are |
| 1260 | // set to undef. |
| 1261 | const Type *EltTy = cast<PackedType>(V->getType())->getElementType(); |
| 1262 | Constant *Zero = Constant::getNullValue(EltTy); |
| 1263 | Constant *Undef = UndefValue::get(EltTy); |
| 1264 | std::vector<Constant*> Elts; |
| 1265 | for (unsigned i = 0; i != VWidth; ++i) |
| 1266 | Elts.push_back((DemandedElts & (1ULL << i)) ? Zero : Undef); |
| 1267 | UndefElts = DemandedElts ^ EltMask; |
| 1268 | return ConstantPacked::get(Elts); |
| 1269 | } |
| 1270 | |
| 1271 | if (!V->hasOneUse()) { // Other users may use these bits. |
| 1272 | if (Depth != 0) { // Not at the root. |
| 1273 | // TODO: Just compute the UndefElts information recursively. |
| 1274 | return false; |
| 1275 | } |
| 1276 | return false; |
| 1277 | } else if (Depth == 10) { // Limit search depth. |
| 1278 | return false; |
| 1279 | } |
| 1280 | |
| 1281 | Instruction *I = dyn_cast<Instruction>(V); |
| 1282 | if (!I) return false; // Only analyze instructions. |
| 1283 | |
| 1284 | bool MadeChange = false; |
| 1285 | uint64_t UndefElts2; |
| 1286 | Value *TmpV; |
| 1287 | switch (I->getOpcode()) { |
| 1288 | default: break; |
| 1289 | |
| 1290 | case Instruction::InsertElement: { |
| 1291 | // If this is a variable index, we don't know which element it overwrites. |
| 1292 | // demand exactly the same input as we produce. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1293 | ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2)); |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1294 | if (Idx == 0) { |
| 1295 | // Note that we can't propagate undef elt info, because we don't know |
| 1296 | // which elt is getting updated. |
| 1297 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1298 | UndefElts2, Depth+1); |
| 1299 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1300 | break; |
| 1301 | } |
| 1302 | |
| 1303 | // If this is inserting an element that isn't demanded, remove this |
| 1304 | // insertelement. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1305 | unsigned IdxNo = Idx->getZExtValue(); |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1306 | if (IdxNo >= VWidth || (DemandedElts & (1ULL << IdxNo)) == 0) |
| 1307 | return AddSoonDeadInstToWorklist(*I, 0); |
| 1308 | |
| 1309 | // Otherwise, the element inserted overwrites whatever was there, so the |
| 1310 | // input demanded set is simpler than the output set. |
| 1311 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), |
| 1312 | DemandedElts & ~(1ULL << IdxNo), |
| 1313 | UndefElts, Depth+1); |
| 1314 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1315 | |
| 1316 | // The inserted element is defined. |
| 1317 | UndefElts |= 1ULL << IdxNo; |
| 1318 | break; |
| 1319 | } |
| 1320 | |
| 1321 | case Instruction::And: |
| 1322 | case Instruction::Or: |
| 1323 | case Instruction::Xor: |
| 1324 | case Instruction::Add: |
| 1325 | case Instruction::Sub: |
| 1326 | case Instruction::Mul: |
| 1327 | // div/rem demand all inputs, because they don't want divide by zero. |
| 1328 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1329 | UndefElts, Depth+1); |
| 1330 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1331 | TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts, |
| 1332 | UndefElts2, Depth+1); |
| 1333 | if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; } |
| 1334 | |
| 1335 | // Output elements are undefined if both are undefined. Consider things |
| 1336 | // like undef&0. The result is known zero, not undef. |
| 1337 | UndefElts &= UndefElts2; |
| 1338 | break; |
| 1339 | |
| 1340 | case Instruction::Call: { |
| 1341 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(I); |
| 1342 | if (!II) break; |
| 1343 | switch (II->getIntrinsicID()) { |
| 1344 | default: break; |
| 1345 | |
| 1346 | // Binary vector operations that work column-wise. A dest element is a |
| 1347 | // function of the corresponding input elements from the two inputs. |
| 1348 | case Intrinsic::x86_sse_sub_ss: |
| 1349 | case Intrinsic::x86_sse_mul_ss: |
| 1350 | case Intrinsic::x86_sse_min_ss: |
| 1351 | case Intrinsic::x86_sse_max_ss: |
| 1352 | case Intrinsic::x86_sse2_sub_sd: |
| 1353 | case Intrinsic::x86_sse2_mul_sd: |
| 1354 | case Intrinsic::x86_sse2_min_sd: |
| 1355 | case Intrinsic::x86_sse2_max_sd: |
| 1356 | TmpV = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts, |
| 1357 | UndefElts, Depth+1); |
| 1358 | if (TmpV) { II->setOperand(1, TmpV); MadeChange = true; } |
| 1359 | TmpV = SimplifyDemandedVectorElts(II->getOperand(2), DemandedElts, |
| 1360 | UndefElts2, Depth+1); |
| 1361 | if (TmpV) { II->setOperand(2, TmpV); MadeChange = true; } |
| 1362 | |
| 1363 | // If only the low elt is demanded and this is a scalarizable intrinsic, |
| 1364 | // scalarize it now. |
| 1365 | if (DemandedElts == 1) { |
| 1366 | switch (II->getIntrinsicID()) { |
| 1367 | default: break; |
| 1368 | case Intrinsic::x86_sse_sub_ss: |
| 1369 | case Intrinsic::x86_sse_mul_ss: |
| 1370 | case Intrinsic::x86_sse2_sub_sd: |
| 1371 | case Intrinsic::x86_sse2_mul_sd: |
| 1372 | // TODO: Lower MIN/MAX/ABS/etc |
| 1373 | Value *LHS = II->getOperand(1); |
| 1374 | Value *RHS = II->getOperand(2); |
| 1375 | // Extract the element as scalars. |
| 1376 | LHS = InsertNewInstBefore(new ExtractElementInst(LHS, 0U,"tmp"), *II); |
| 1377 | RHS = InsertNewInstBefore(new ExtractElementInst(RHS, 0U,"tmp"), *II); |
| 1378 | |
| 1379 | switch (II->getIntrinsicID()) { |
| 1380 | default: assert(0 && "Case stmts out of sync!"); |
| 1381 | case Intrinsic::x86_sse_sub_ss: |
| 1382 | case Intrinsic::x86_sse2_sub_sd: |
| 1383 | TmpV = InsertNewInstBefore(BinaryOperator::createSub(LHS, RHS, |
| 1384 | II->getName()), *II); |
| 1385 | break; |
| 1386 | case Intrinsic::x86_sse_mul_ss: |
| 1387 | case Intrinsic::x86_sse2_mul_sd: |
| 1388 | TmpV = InsertNewInstBefore(BinaryOperator::createMul(LHS, RHS, |
| 1389 | II->getName()), *II); |
| 1390 | break; |
| 1391 | } |
| 1392 | |
| 1393 | Instruction *New = |
| 1394 | new InsertElementInst(UndefValue::get(II->getType()), TmpV, 0U, |
| 1395 | II->getName()); |
| 1396 | InsertNewInstBefore(New, *II); |
| 1397 | AddSoonDeadInstToWorklist(*II, 0); |
| 1398 | return New; |
| 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | // Output elements are undefined if both are undefined. Consider things |
| 1403 | // like undef&0. The result is known zero, not undef. |
| 1404 | UndefElts &= UndefElts2; |
| 1405 | break; |
| 1406 | } |
| 1407 | break; |
| 1408 | } |
| 1409 | } |
| 1410 | return MadeChange ? I : 0; |
| 1411 | } |
| 1412 | |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 1413 | // isTrueWhenEqual - Return true if the specified setcondinst instruction is |
| 1414 | // true when both operands are equal... |
| 1415 | // |
| 1416 | static bool isTrueWhenEqual(Instruction &I) { |
| 1417 | return I.getOpcode() == Instruction::SetEQ || |
| 1418 | I.getOpcode() == Instruction::SetGE || |
| 1419 | I.getOpcode() == Instruction::SetLE; |
| 1420 | } |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1421 | |
| 1422 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
| 1423 | /// function is designed to check a chain of associative operators for a |
| 1424 | /// potential to apply a certain optimization. Since the optimization may be |
| 1425 | /// applicable if the expression was reassociated, this checks the chain, then |
| 1426 | /// reassociates the expression as necessary to expose the optimization |
| 1427 | /// opportunity. This makes use of a special Functor, which must define |
| 1428 | /// 'shouldApply' and 'apply' methods. |
| 1429 | /// |
| 1430 | template<typename Functor> |
| 1431 | Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { |
| 1432 | unsigned Opcode = Root.getOpcode(); |
| 1433 | Value *LHS = Root.getOperand(0); |
| 1434 | |
| 1435 | // Quick check, see if the immediate LHS matches... |
| 1436 | if (F.shouldApply(LHS)) |
| 1437 | return F.apply(Root); |
| 1438 | |
| 1439 | // Otherwise, if the LHS is not of the same opcode as the root, return. |
| 1440 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1441 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1442 | // Should we apply this transform to the RHS? |
| 1443 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); |
| 1444 | |
| 1445 | // If not to the RHS, check to see if we should apply to the LHS... |
| 1446 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { |
| 1447 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS |
| 1448 | ShouldApply = true; |
| 1449 | } |
| 1450 | |
| 1451 | // If the functor wants to apply the optimization to the RHS of LHSI, |
| 1452 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) |
| 1453 | if (ShouldApply) { |
| 1454 | BasicBlock *BB = Root.getParent(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1455 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1456 | // Now all of the instructions are in the current basic block, go ahead |
| 1457 | // and perform the reassociation. |
| 1458 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); |
| 1459 | |
| 1460 | // First move the selected RHS to the LHS of the root... |
| 1461 | Root.setOperand(0, LHSI->getOperand(1)); |
| 1462 | |
| 1463 | // Make what used to be the LHS of the root be the user of the root... |
| 1464 | Value *ExtraOperand = TmpLHSI->getOperand(1); |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1465 | if (&Root == TmpLHSI) { |
Chris Lattner | 8953b90 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 1466 | Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); |
| 1467 | return 0; |
| 1468 | } |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1469 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1470 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1471 | TmpLHSI->getParent()->getInstList().remove(TmpLHSI); |
| 1472 | BasicBlock::iterator ARI = &Root; ++ARI; |
| 1473 | BB->getInstList().insert(ARI, TmpLHSI); // Move TmpLHSI to after Root |
| 1474 | ARI = Root; |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1475 | |
| 1476 | // Now propagate the ExtraOperand down the chain of instructions until we |
| 1477 | // get to LHSI. |
| 1478 | while (TmpLHSI != LHSI) { |
| 1479 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1480 | // Move the instruction to immediately before the chain we are |
| 1481 | // constructing to avoid breaking dominance properties. |
| 1482 | NextLHSI->getParent()->getInstList().remove(NextLHSI); |
| 1483 | BB->getInstList().insert(ARI, NextLHSI); |
| 1484 | ARI = NextLHSI; |
| 1485 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1486 | Value *NextOp = NextLHSI->getOperand(1); |
| 1487 | NextLHSI->setOperand(1, ExtraOperand); |
| 1488 | TmpLHSI = NextLHSI; |
| 1489 | ExtraOperand = NextOp; |
| 1490 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1491 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1492 | // Now that the instructions are reassociated, have the functor perform |
| 1493 | // the transformation... |
| 1494 | return F.apply(Root); |
| 1495 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1496 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1497 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
| 1498 | } |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
| 1502 | |
| 1503 | // AddRHS - Implements: X + X --> X << 1 |
| 1504 | struct AddRHS { |
| 1505 | Value *RHS; |
| 1506 | AddRHS(Value *rhs) : RHS(rhs) {} |
| 1507 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 1508 | Instruction *apply(BinaryOperator &Add) const { |
| 1509 | return new ShiftInst(Instruction::Shl, Add.getOperand(0), |
| 1510 | ConstantInt::get(Type::UByteTy, 1)); |
| 1511 | } |
| 1512 | }; |
| 1513 | |
| 1514 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) |
| 1515 | // iff C1&C2 == 0 |
| 1516 | struct AddMaskingAnd { |
| 1517 | Constant *C2; |
| 1518 | AddMaskingAnd(Constant *c) : C2(c) {} |
| 1519 | bool shouldApply(Value *LHS) const { |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1520 | ConstantInt *C1; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1521 | return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1522 | ConstantExpr::getAnd(C1, C2)->isNullValue(); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1523 | } |
| 1524 | Instruction *apply(BinaryOperator &Add) const { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1525 | return BinaryOperator::createOr(Add.getOperand(0), Add.getOperand(1)); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1526 | } |
| 1527 | }; |
| 1528 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1529 | static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1530 | InstCombiner *IC) { |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1531 | if (isa<CastInst>(I)) { |
| 1532 | if (Constant *SOC = dyn_cast<Constant>(SO)) |
| 1533 | return ConstantExpr::getCast(SOC, I.getType()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1534 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1535 | return IC->InsertNewInstBefore(new CastInst(SO, I.getType(), |
| 1536 | SO->getName() + ".cast"), I); |
| 1537 | } |
| 1538 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1539 | // Figure out if the constant is the left or the right argument. |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1540 | bool ConstIsRHS = isa<Constant>(I.getOperand(1)); |
| 1541 | Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS)); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1542 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1543 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
| 1544 | if (ConstIsRHS) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1545 | return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand); |
| 1546 | return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC); |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | Value *Op0 = SO, *Op1 = ConstOperand; |
| 1550 | if (!ConstIsRHS) |
| 1551 | std::swap(Op0, Op1); |
| 1552 | Instruction *New; |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1553 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| 1554 | New = BinaryOperator::create(BO->getOpcode(), Op0, Op1,SO->getName()+".op"); |
| 1555 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(&I)) |
| 1556 | New = new ShiftInst(SI->getOpcode(), Op0, Op1, SO->getName()+".sh"); |
Chris Lattner | f9d9665 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 1557 | else { |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1558 | assert(0 && "Unknown binary instruction type!"); |
Chris Lattner | f9d9665 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 1559 | abort(); |
| 1560 | } |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1561 | return IC->InsertNewInstBefore(New, I); |
| 1562 | } |
| 1563 | |
| 1564 | // FoldOpIntoSelect - Given an instruction with a select as one operand and a |
| 1565 | // constant as the other operand, try to fold the binary operator into the |
| 1566 | // select arguments. This also works for Cast instructions, which obviously do |
| 1567 | // not have a second operand. |
| 1568 | static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI, |
| 1569 | InstCombiner *IC) { |
| 1570 | // Don't modify shared select instructions |
| 1571 | if (!SI->hasOneUse()) return 0; |
| 1572 | Value *TV = SI->getOperand(1); |
| 1573 | Value *FV = SI->getOperand(2); |
| 1574 | |
| 1575 | if (isa<Constant>(TV) || isa<Constant>(FV)) { |
Chris Lattner | 374e659 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 1576 | // Bool selects with constant operands can be folded to logical ops. |
| 1577 | if (SI->getType() == Type::BoolTy) return 0; |
| 1578 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1579 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC); |
| 1580 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC); |
| 1581 | |
| 1582 | return new SelectInst(SI->getCondition(), SelectTrueVal, |
| 1583 | SelectFalseVal); |
| 1584 | } |
| 1585 | return 0; |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1588 | |
| 1589 | /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI |
| 1590 | /// node as operand #0, see if we can fold the instruction into the PHI (which |
| 1591 | /// is only possible if all operands to the PHI are constants). |
| 1592 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { |
| 1593 | PHINode *PN = cast<PHINode>(I.getOperand(0)); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1594 | unsigned NumPHIValues = PN->getNumIncomingValues(); |
Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1595 | if (!PN->hasOneUse() || NumPHIValues == 0) return 0; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1596 | |
Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1597 | // Check to see if all of the operands of the PHI are constants. If there is |
| 1598 | // one non-constant value, remember the BB it is. If there is more than one |
| 1599 | // bail out. |
| 1600 | BasicBlock *NonConstBB = 0; |
| 1601 | for (unsigned i = 0; i != NumPHIValues; ++i) |
| 1602 | if (!isa<Constant>(PN->getIncomingValue(i))) { |
| 1603 | if (NonConstBB) return 0; // More than one non-const value. |
| 1604 | NonConstBB = PN->getIncomingBlock(i); |
| 1605 | |
| 1606 | // If the incoming non-constant value is in I's block, we have an infinite |
| 1607 | // loop. |
| 1608 | if (NonConstBB == I.getParent()) |
| 1609 | return 0; |
| 1610 | } |
| 1611 | |
| 1612 | // If there is exactly one non-constant value, we can insert a copy of the |
| 1613 | // operation in that block. However, if this is a critical edge, we would be |
| 1614 | // inserting the computation one some other paths (e.g. inside a loop). Only |
| 1615 | // do this if the pred block is unconditionally branching into the phi block. |
| 1616 | if (NonConstBB) { |
| 1617 | BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator()); |
| 1618 | if (!BI || !BI->isUnconditional()) return 0; |
| 1619 | } |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1620 | |
| 1621 | // Okay, we can do the transformation: create the new PHI node. |
| 1622 | PHINode *NewPN = new PHINode(I.getType(), I.getName()); |
| 1623 | I.setName(""); |
Chris Lattner | d8e2018 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 1624 | NewPN->reserveOperandSpace(PN->getNumOperands()/2); |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1625 | InsertNewInstBefore(NewPN, *PN); |
| 1626 | |
| 1627 | // Next, add all of the operands to the PHI. |
| 1628 | if (I.getNumOperands() == 2) { |
| 1629 | Constant *C = cast<Constant>(I.getOperand(1)); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1630 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1631 | Value *InV; |
| 1632 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
| 1633 | InV = ConstantExpr::get(I.getOpcode(), InC, C); |
| 1634 | } else { |
| 1635 | assert(PN->getIncomingBlock(i) == NonConstBB); |
| 1636 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| 1637 | InV = BinaryOperator::create(BO->getOpcode(), |
| 1638 | PN->getIncomingValue(i), C, "phitmp", |
| 1639 | NonConstBB->getTerminator()); |
| 1640 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(&I)) |
| 1641 | InV = new ShiftInst(SI->getOpcode(), |
| 1642 | PN->getIncomingValue(i), C, "phitmp", |
| 1643 | NonConstBB->getTerminator()); |
| 1644 | else |
| 1645 | assert(0 && "Unknown binop!"); |
| 1646 | |
| 1647 | WorkList.push_back(cast<Instruction>(InV)); |
| 1648 | } |
| 1649 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1650 | } |
| 1651 | } else { |
| 1652 | assert(isa<CastInst>(I) && "Unary op should be a cast!"); |
| 1653 | const Type *RetTy = I.getType(); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1654 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1655 | Value *InV; |
| 1656 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
| 1657 | InV = ConstantExpr::getCast(InC, RetTy); |
| 1658 | } else { |
| 1659 | assert(PN->getIncomingBlock(i) == NonConstBB); |
| 1660 | InV = new CastInst(PN->getIncomingValue(i), I.getType(), "phitmp", |
| 1661 | NonConstBB->getTerminator()); |
| 1662 | WorkList.push_back(cast<Instruction>(InV)); |
| 1663 | } |
| 1664 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1665 | } |
| 1666 | } |
| 1667 | return ReplaceInstUsesWith(I, NewPN); |
| 1668 | } |
| 1669 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1670 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1671 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1672 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1673 | |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1674 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1675 | // X + undef -> undef |
| 1676 | if (isa<UndefValue>(RHS)) |
| 1677 | return ReplaceInstUsesWith(I, RHS); |
| 1678 | |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1679 | // X + 0 --> X |
Chris Lattner | 7fde91e | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 1680 | if (!I.getType()->isFloatingPoint()) { // NOTE: -0 + +0 = +0. |
| 1681 | if (RHSC->isNullValue()) |
| 1682 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | da1b152 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 1683 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
| 1684 | if (CFP->isExactlyValue(-0.0)) |
| 1685 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 7fde91e | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 1686 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1687 | |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1688 | // X + (signbit) --> X ^ signbit |
| 1689 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { |
Chris Lattner | 92a6865 | 2006-02-07 08:05:22 +0000 | [diff] [blame] | 1690 | uint64_t Val = CI->getZExtValue(); |
Chris Lattner | 77defba | 2006-02-07 07:00:41 +0000 | [diff] [blame] | 1691 | if (Val == (1ULL << (CI->getType()->getPrimitiveSizeInBits()-1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1692 | return BinaryOperator::createXor(LHS, RHS); |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1693 | } |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1694 | |
| 1695 | if (isa<PHINode>(LHS)) |
| 1696 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1697 | return NV; |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1698 | |
Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 1699 | ConstantInt *XorRHS = 0; |
| 1700 | Value *XorLHS = 0; |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1701 | if (match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) { |
| 1702 | unsigned TySizeBits = I.getType()->getPrimitiveSizeInBits(); |
| 1703 | int64_t RHSSExt = cast<ConstantInt>(RHSC)->getSExtValue(); |
| 1704 | uint64_t RHSZExt = cast<ConstantInt>(RHSC)->getZExtValue(); |
| 1705 | |
| 1706 | uint64_t C0080Val = 1ULL << 31; |
| 1707 | int64_t CFF80Val = -C0080Val; |
| 1708 | unsigned Size = 32; |
| 1709 | do { |
| 1710 | if (TySizeBits > Size) { |
| 1711 | bool Found = false; |
| 1712 | // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext. |
| 1713 | // If we have ADD(XOR(AND(X, 0xFF), 0xF..F80), 0x80), it's a sext. |
| 1714 | if (RHSSExt == CFF80Val) { |
| 1715 | if (XorRHS->getZExtValue() == C0080Val) |
| 1716 | Found = true; |
| 1717 | } else if (RHSZExt == C0080Val) { |
| 1718 | if (XorRHS->getSExtValue() == CFF80Val) |
| 1719 | Found = true; |
| 1720 | } |
| 1721 | if (Found) { |
| 1722 | // This is a sign extend if the top bits are known zero. |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 1723 | uint64_t Mask = ~0ULL; |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 1724 | Mask <<= 64-(TySizeBits-Size); |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 1725 | Mask &= XorLHS->getType()->getIntegralTypeMask(); |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 1726 | if (!MaskedValueIsZero(XorLHS, Mask)) |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1727 | Size = 0; // Not a sign ext, but can't be any others either. |
| 1728 | goto FoundSExt; |
| 1729 | } |
| 1730 | } |
| 1731 | Size >>= 1; |
| 1732 | C0080Val >>= Size; |
| 1733 | CFF80Val >>= Size; |
| 1734 | } while (Size >= 8); |
| 1735 | |
| 1736 | FoundSExt: |
| 1737 | const Type *MiddleType = 0; |
| 1738 | switch (Size) { |
| 1739 | default: break; |
| 1740 | case 32: MiddleType = Type::IntTy; break; |
| 1741 | case 16: MiddleType = Type::ShortTy; break; |
| 1742 | case 8: MiddleType = Type::SByteTy; break; |
| 1743 | } |
| 1744 | if (MiddleType) { |
| 1745 | Instruction *NewTrunc = new CastInst(XorLHS, MiddleType, "sext"); |
| 1746 | InsertNewInstBefore(NewTrunc, I); |
| 1747 | return new CastInst(NewTrunc, I.getType()); |
| 1748 | } |
| 1749 | } |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1750 | } |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1751 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1752 | // X + X --> X << 1 |
Robert Bocchino | 7b5b86c | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 1753 | if (I.getType()->isInteger()) { |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1754 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; |
Chris Lattner | 4706046 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 1755 | |
| 1756 | if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) { |
| 1757 | if (RHSI->getOpcode() == Instruction::Sub) |
| 1758 | if (LHS == RHSI->getOperand(1)) // A + (B - A) --> B |
| 1759 | return ReplaceInstUsesWith(I, RHSI->getOperand(0)); |
| 1760 | } |
| 1761 | if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) { |
| 1762 | if (LHSI->getOpcode() == Instruction::Sub) |
| 1763 | if (RHS == LHSI->getOperand(1)) // (B - A) + A --> B |
| 1764 | return ReplaceInstUsesWith(I, LHSI->getOperand(0)); |
| 1765 | } |
Robert Bocchino | 7b5b86c | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 1766 | } |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 1767 | |
Chris Lattner | 147e975 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 1768 | // -A + B --> B - A |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1769 | if (Value *V = dyn_castNegVal(LHS)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1770 | return BinaryOperator::createSub(RHS, V); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1771 | |
| 1772 | // A + -B --> A - B |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1773 | if (!isa<Constant>(RHS)) |
| 1774 | if (Value *V = dyn_castNegVal(RHS)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1775 | return BinaryOperator::createSub(LHS, V); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1776 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1777 | |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1778 | ConstantInt *C2; |
| 1779 | if (Value *X = dyn_castFoldableMul(LHS, C2)) { |
| 1780 | if (X == RHS) // X*C + X --> X * (C+1) |
| 1781 | return BinaryOperator::createMul(RHS, AddOne(C2)); |
| 1782 | |
| 1783 | // X*C1 + X*C2 --> X * (C1+C2) |
| 1784 | ConstantInt *C1; |
| 1785 | if (X == dyn_castFoldableMul(RHS, C1)) |
| 1786 | return BinaryOperator::createMul(X, ConstantExpr::getAdd(C1, C2)); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | // X + X*C --> X * (C+1) |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1790 | if (dyn_castFoldableMul(RHS, C2) == LHS) |
| 1791 | return BinaryOperator::createMul(LHS, AddOne(C2)); |
| 1792 | |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1793 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1794 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1795 | if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1796 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) return R; |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 1797 | |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 1798 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 1799 | Value *X = 0; |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1800 | if (match(LHS, m_Not(m_Value(X)))) { // ~X + C --> (C-1) - X |
| 1801 | Constant *C= ConstantExpr::getSub(CRHS, ConstantInt::get(I.getType(), 1)); |
| 1802 | return BinaryOperator::createSub(C, X); |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 1803 | } |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1804 | |
Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 1805 | // (X & FF00) + xx00 -> (X+xx00) & FF00 |
| 1806 | if (LHS->hasOneUse() && match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) { |
| 1807 | Constant *Anded = ConstantExpr::getAnd(CRHS, C2); |
| 1808 | if (Anded == CRHS) { |
| 1809 | // See if all bits from the first bit set in the Add RHS up are included |
| 1810 | // in the mask. First, get the rightmost bit. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1811 | uint64_t AddRHSV = CRHS->getZExtValue(); |
Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 1812 | |
| 1813 | // Form a mask of all bits from the lowest bit added through the top. |
| 1814 | uint64_t AddRHSHighBits = ~((AddRHSV & -AddRHSV)-1); |
Chris Lattner | 77defba | 2006-02-07 07:00:41 +0000 | [diff] [blame] | 1815 | AddRHSHighBits &= C2->getType()->getIntegralTypeMask(); |
Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 1816 | |
| 1817 | // See if the and mask includes all of these bits. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1818 | uint64_t AddRHSHighBitsAnd = AddRHSHighBits & C2->getZExtValue(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1819 | |
Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 1820 | if (AddRHSHighBits == AddRHSHighBitsAnd) { |
| 1821 | // Okay, the xform is safe. Insert the new add pronto. |
| 1822 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, CRHS, |
| 1823 | LHS->getName()), I); |
| 1824 | return BinaryOperator::createAnd(NewAdd, C2); |
| 1825 | } |
| 1826 | } |
| 1827 | } |
| 1828 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1829 | // Try to fold constant add into select arguments. |
| 1830 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1831 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1832 | return R; |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1835 | // add (cast *A to intptrtype) B -> |
| 1836 | // cast (GEP (cast *A to sbyte*) B) -> |
| 1837 | // intptrtype |
Andrew Lenharth | 4f339be | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 1838 | { |
| 1839 | CastInst* CI = dyn_cast<CastInst>(LHS); |
| 1840 | Value* Other = RHS; |
| 1841 | if (!CI) { |
| 1842 | CI = dyn_cast<CastInst>(RHS); |
| 1843 | Other = LHS; |
| 1844 | } |
Andrew Lenharth | 44cb67a | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 1845 | if (CI && CI->getType()->isSized() && |
| 1846 | (CI->getType()->getPrimitiveSize() == |
| 1847 | TD->getIntPtrType()->getPrimitiveSize()) |
| 1848 | && isa<PointerType>(CI->getOperand(0)->getType())) { |
| 1849 | Value* I2 = InsertCastBefore(CI->getOperand(0), |
| 1850 | PointerType::get(Type::SByteTy), I); |
| 1851 | I2 = InsertNewInstBefore(new GetElementPtrInst(I2, Other, "ctg2"), I); |
| 1852 | return new CastInst(I2, CI->getType()); |
Andrew Lenharth | 4f339be | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 1853 | } |
| 1854 | } |
| 1855 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1856 | return Changed ? &I : 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
Chris Lattner | bdb0ce0 | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 1859 | // isSignBit - Return true if the value represented by the constant only has the |
| 1860 | // highest order bit set. |
| 1861 | static bool isSignBit(ConstantInt *CI) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1862 | unsigned NumBits = CI->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1863 | return (CI->getZExtValue() & (~0ULL >> (64-NumBits))) == (1ULL << (NumBits-1)); |
Chris Lattner | bdb0ce0 | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 1866 | /// RemoveNoopCast - Strip off nonconverting casts from the value. |
| 1867 | /// |
| 1868 | static Value *RemoveNoopCast(Value *V) { |
| 1869 | if (CastInst *CI = dyn_cast<CastInst>(V)) { |
| 1870 | const Type *CTy = CI->getType(); |
| 1871 | const Type *OpTy = CI->getOperand(0)->getType(); |
| 1872 | if (CTy->isInteger() && OpTy->isInteger()) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1873 | if (CTy->getPrimitiveSizeInBits() == OpTy->getPrimitiveSizeInBits()) |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 1874 | return RemoveNoopCast(CI->getOperand(0)); |
| 1875 | } else if (isa<PointerType>(CTy) && isa<PointerType>(OpTy)) |
| 1876 | return RemoveNoopCast(CI->getOperand(0)); |
| 1877 | } |
| 1878 | return V; |
| 1879 | } |
| 1880 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1881 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1882 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1883 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1884 | if (Op0 == Op1) // sub X, X -> 0 |
| 1885 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1886 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1887 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1888 | if (Value *V = dyn_castNegVal(Op1)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1889 | return BinaryOperator::createAdd(Op0, V); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1890 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1891 | if (isa<UndefValue>(Op0)) |
| 1892 | return ReplaceInstUsesWith(I, Op0); // undef - X -> undef |
| 1893 | if (isa<UndefValue>(Op1)) |
| 1894 | return ReplaceInstUsesWith(I, Op1); // X - undef -> undef |
| 1895 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 1896 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
| 1897 | // Replace (-1 - A) with (~A)... |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1898 | if (C->isAllOnesValue()) |
| 1899 | return BinaryOperator::createNot(Op1); |
Chris Lattner | ad3c495 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 1900 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 1901 | // C - ~X == X + (1+C) |
Reid Spencer | 4fdd96c | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 1902 | Value *X = 0; |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1903 | if (match(Op1, m_Not(m_Value(X)))) |
| 1904 | return BinaryOperator::createAdd(X, |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1905 | ConstantExpr::getAdd(C, ConstantInt::get(I.getType(), 1))); |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 1906 | // -((uint)X >> 31) -> ((int)X >> 31) |
| 1907 | // -((int)X >> 31) -> ((uint)X >> 31) |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 1908 | if (C->isNullValue()) { |
| 1909 | Value *NoopCastedRHS = RemoveNoopCast(Op1); |
| 1910 | if (ShiftInst *SI = dyn_cast<ShiftInst>(NoopCastedRHS)) |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1911 | if (SI->getOpcode() == Instruction::LShr) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1912 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 1913 | // Check to see if we are shifting out everything but the sign bit. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1914 | if (CU->getZExtValue() == |
| 1915 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1916 | // Ok, the transformation is safe. Insert AShr. |
| 1917 | return new ShiftInst(Instruction::AShr, SI->getOperand(0), |
| 1918 | CU, SI->getName()); |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 1919 | } |
| 1920 | } |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1921 | } |
| 1922 | else if (SI->getOpcode() == Instruction::AShr) { |
| 1923 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
| 1924 | // Check to see if we are shifting out everything but the sign bit. |
| 1925 | if (CU->getZExtValue() == |
| 1926 | SI->getType()->getPrimitiveSizeInBits()-1) { |
| 1927 | // Ok, the transformation is safe. Insert LShr. |
| 1928 | return new ShiftInst(Instruction::LShr, SI->getOperand(0), |
| 1929 | CU, SI->getName()); |
| 1930 | } |
| 1931 | } |
| 1932 | } |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 1933 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1934 | |
| 1935 | // Try to fold constant sub into select arguments. |
| 1936 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1937 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1938 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1939 | |
| 1940 | if (isa<PHINode>(Op0)) |
| 1941 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1942 | return NV; |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 1945 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
| 1946 | if (Op1I->getOpcode() == Instruction::Add && |
| 1947 | !Op0->getType()->isFloatingPoint()) { |
Chris Lattner | c7f3c1a | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 1948 | if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 1949 | return BinaryOperator::createNeg(Op1I->getOperand(1), I.getName()); |
Chris Lattner | c7f3c1a | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 1950 | else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 1951 | return BinaryOperator::createNeg(Op1I->getOperand(0), I.getName()); |
Chris Lattner | c7f3c1a | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 1952 | else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) { |
| 1953 | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1))) |
| 1954 | // C1-(X+C2) --> (C1-C2)-X |
| 1955 | return BinaryOperator::createSub(ConstantExpr::getSub(CI1, CI2), |
| 1956 | Op1I->getOperand(0)); |
| 1957 | } |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1960 | if (Op1I->hasOneUse()) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1961 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 1962 | // is not used by anyone else... |
| 1963 | // |
Chris Lattner | c2f0aa5 | 2004-02-02 20:09:56 +0000 | [diff] [blame] | 1964 | if (Op1I->getOpcode() == Instruction::Sub && |
| 1965 | !Op1I->getType()->isFloatingPoint()) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1966 | // Swap the two operands of the subexpr... |
| 1967 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 1968 | Op1I->setOperand(0, IIOp1); |
| 1969 | Op1I->setOperand(1, IIOp0); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1970 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1971 | // Create the new top level add instruction... |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1972 | return BinaryOperator::createAdd(Op0, Op1); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
| 1975 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 1976 | // |
| 1977 | if (Op1I->getOpcode() == Instruction::And && |
| 1978 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 1979 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 1980 | |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 1981 | Value *NewNot = |
| 1982 | InsertNewInstBefore(BinaryOperator::createNot(OtherOp, "B.not"), I); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1983 | return BinaryOperator::createAnd(Op0, NewNot); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1984 | } |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1985 | |
Reid Spencer | 3c51495 | 2006-10-16 23:08:08 +0000 | [diff] [blame] | 1986 | // 0 - (X sdiv C) -> (X sdiv -C) |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1987 | if (Op1I->getOpcode() == Instruction::SDiv) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1988 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1989 | if (CSI->isNullValue()) |
Chris Lattner | 0aee4b7 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 1990 | if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1991 | return BinaryOperator::createSDiv(Op1I->getOperand(0), |
Chris Lattner | 0aee4b7 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 1992 | ConstantExpr::getNeg(DivRHS)); |
| 1993 | |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1994 | // X - X*C --> X * (1-C) |
Reid Spencer | 4fdd96c | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 1995 | ConstantInt *C2 = 0; |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1996 | if (dyn_castFoldableMul(Op1I, C2) == Op0) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1997 | Constant *CP1 = |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1998 | ConstantExpr::getSub(ConstantInt::get(I.getType(), 1), C2); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1999 | return BinaryOperator::createMul(Op0, CP1); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2000 | } |
Chris Lattner | ad3c495 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2001 | } |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2002 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2003 | |
Chris Lattner | 4706046 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2004 | if (!Op0->getType()->isFloatingPoint()) |
| 2005 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 2006 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2007 | if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X |
| 2008 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 2009 | else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X |
| 2010 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 4706046 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2011 | } else if (Op0I->getOpcode() == Instruction::Sub) { |
| 2012 | if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y |
| 2013 | return BinaryOperator::createNeg(Op0I->getOperand(1), I.getName()); |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2014 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2015 | |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2016 | ConstantInt *C1; |
| 2017 | if (Value *X = dyn_castFoldableMul(Op0, C1)) { |
| 2018 | if (X == Op1) { // X*C - X --> X * (C-1) |
| 2019 | Constant *CP1 = ConstantExpr::getSub(C1, ConstantInt::get(I.getType(),1)); |
| 2020 | return BinaryOperator::createMul(Op1, CP1); |
| 2021 | } |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2022 | |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2023 | ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2) |
| 2024 | if (X == dyn_castFoldableMul(Op1, C2)) |
| 2025 | return BinaryOperator::createMul(Op1, ConstantExpr::getSub(C1, C2)); |
| 2026 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2027 | return 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2030 | /// isSignBitCheck - Given an exploded setcc instruction, return true if it is |
| 2031 | /// really just returns true if the most significant (sign) bit is set. |
| 2032 | static bool isSignBitCheck(unsigned Opcode, Value *LHS, ConstantInt *RHS) { |
| 2033 | if (RHS->getType()->isSigned()) { |
| 2034 | // True if source is LHS < 0 or LHS <= -1 |
| 2035 | return Opcode == Instruction::SetLT && RHS->isNullValue() || |
| 2036 | Opcode == Instruction::SetLE && RHS->isAllOnesValue(); |
| 2037 | } else { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2038 | ConstantInt *RHSC = cast<ConstantInt>(RHS); |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2039 | // True if source is LHS > 127 or LHS >= 128, where the constants depend on |
| 2040 | // the size of the integer type. |
| 2041 | if (Opcode == Instruction::SetGE) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2042 | return RHSC->getZExtValue() == |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2043 | 1ULL << (RHS->getType()->getPrimitiveSizeInBits()-1); |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2044 | if (Opcode == Instruction::SetGT) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2045 | return RHSC->getZExtValue() == |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2046 | (1ULL << (RHS->getType()->getPrimitiveSizeInBits()-1))-1; |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2047 | } |
| 2048 | return false; |
| 2049 | } |
| 2050 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2051 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2052 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2053 | Value *Op0 = I.getOperand(0); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2054 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2055 | if (isa<UndefValue>(I.getOperand(1))) // undef * X -> 0 |
| 2056 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2057 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2058 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2059 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 2060 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2061 | |
| 2062 | // ((X << C1)*C2) == (X * (C2 << C1)) |
| 2063 | if (ShiftInst *SI = dyn_cast<ShiftInst>(Op0)) |
| 2064 | if (SI->getOpcode() == Instruction::Shl) |
| 2065 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2066 | return BinaryOperator::createMul(SI->getOperand(0), |
| 2067 | ConstantExpr::getShl(CI, ShOp)); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2068 | |
Chris Lattner | cce81be | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 2069 | if (CI->isNullValue()) |
| 2070 | return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 |
| 2071 | if (CI->equalsInt(1)) // X * 1 == X |
| 2072 | return ReplaceInstUsesWith(I, Op0); |
| 2073 | if (CI->isAllOnesValue()) // X * -1 == 0 - X |
Chris Lattner | 35236d8 | 2003-06-25 17:09:20 +0000 | [diff] [blame] | 2074 | return BinaryOperator::createNeg(Op0, I.getName()); |
Chris Lattner | 31ba129 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2075 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2076 | int64_t Val = (int64_t)cast<ConstantInt>(CI)->getZExtValue(); |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 2077 | if (isPowerOf2_64(Val)) { // Replace X*(2^C) with X << C |
| 2078 | uint64_t C = Log2_64(Val); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2079 | return new ShiftInst(Instruction::Shl, Op0, |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2080 | ConstantInt::get(Type::UByteTy, C)); |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 2081 | } |
Robert Bocchino | 7b5b86c | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2082 | } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2083 | if (Op1F->isNullValue()) |
| 2084 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 31ba129 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2085 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2086 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 2087 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) |
| 2088 | if (Op1F->getValue() == 1.0) |
| 2089 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
| 2090 | } |
Chris Lattner | 32c01df | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2091 | |
| 2092 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 2093 | if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() && |
| 2094 | isa<ConstantInt>(Op0I->getOperand(1))) { |
| 2095 | // Canonicalize (X+C1)*C2 -> X*C2+C1*C2. |
| 2096 | Instruction *Add = BinaryOperator::createMul(Op0I->getOperand(0), |
| 2097 | Op1, "tmp"); |
| 2098 | InsertNewInstBefore(Add, I); |
| 2099 | Value *C1C2 = ConstantExpr::getMul(Op1, |
| 2100 | cast<Constant>(Op0I->getOperand(1))); |
| 2101 | return BinaryOperator::createAdd(Add, C1C2); |
| 2102 | |
| 2103 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2104 | |
| 2105 | // Try to fold constant mul into select arguments. |
| 2106 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2107 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2108 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2109 | |
| 2110 | if (isa<PHINode>(Op0)) |
| 2111 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2112 | return NV; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
Chris Lattner | 934a64cf | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2115 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
| 2116 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2117 | return BinaryOperator::createMul(Op0v, Op1v); |
Chris Lattner | 934a64cf | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2118 | |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2119 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 2120 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 2121 | // See if we can simplify things based on how the boolean was originally |
| 2122 | // formed. |
| 2123 | CastInst *BoolCast = 0; |
| 2124 | if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(0))) |
| 2125 | if (CI->getOperand(0)->getType() == Type::BoolTy) |
| 2126 | BoolCast = CI; |
| 2127 | if (!BoolCast) |
| 2128 | if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(1))) |
| 2129 | if (CI->getOperand(0)->getType() == Type::BoolTy) |
| 2130 | BoolCast = CI; |
| 2131 | if (BoolCast) { |
| 2132 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(BoolCast->getOperand(0))) { |
| 2133 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); |
| 2134 | const Type *SCOpTy = SCIOp0->getType(); |
| 2135 | |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2136 | // If the setcc is true iff the sign bit of X is set, then convert this |
| 2137 | // multiply into a shift/and combination. |
| 2138 | if (isa<ConstantInt>(SCIOp1) && |
| 2139 | isSignBitCheck(SCI->getOpcode(), SCIOp0, cast<ConstantInt>(SCIOp1))) { |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2140 | // Shift the X value right to turn it into "all signbits". |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2141 | Constant *Amt = ConstantInt::get(Type::UByteTy, |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2142 | SCOpTy->getPrimitiveSizeInBits()-1); |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2143 | if (SCIOp0->getType()->isUnsigned()) { |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 2144 | const Type *NewTy = SCIOp0->getType()->getSignedVersion(); |
Reid Spencer | 00c482b | 2006-10-26 19:19:06 +0000 | [diff] [blame] | 2145 | SCIOp0 = InsertCastBefore(SCIOp0, NewTy, I); |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | Value *V = |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2149 | InsertNewInstBefore(new ShiftInst(Instruction::AShr, SCIOp0, Amt, |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2150 | BoolCast->getOperand(0)->getName()+ |
| 2151 | ".mask"), I); |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2152 | |
| 2153 | // If the multiply type is not the same as the source type, sign extend |
| 2154 | // or truncate to the multiply type. |
| 2155 | if (I.getType() != V->getType()) |
Reid Spencer | 00c482b | 2006-10-26 19:19:06 +0000 | [diff] [blame] | 2156 | V = InsertCastBefore(V, I.getType(), I); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2157 | |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2158 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2159 | return BinaryOperator::createAnd(V, OtherOp); |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2160 | } |
| 2161 | } |
| 2162 | } |
| 2163 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2164 | return Changed ? &I : 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2167 | /// This function implements the transforms on div instructions that work |
| 2168 | /// regardless of the kind of div instruction it is (udiv, sdiv, or fdiv). It is |
| 2169 | /// used by the visitors to those instructions. |
| 2170 | /// @brief Transforms common to all three div instructions |
| 2171 | Instruction* InstCombiner::commonDivTransforms(BinaryOperator &I) { |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2172 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2173 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2174 | // undef / X -> 0 |
| 2175 | if (isa<UndefValue>(Op0)) |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2176 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2177 | |
| 2178 | // X / undef -> undef |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2179 | if (isa<UndefValue>(Op1)) |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2180 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2181 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2182 | // Handle cases involving: div X, (select Cond, Y, Z) |
Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2183 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2184 | // div X, (Cond ? 0 : Y) -> div X, Y. If the div and the select are in the |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2185 | // same basic block, then we replace the select with Y, and the condition |
| 2186 | // of the select with false (if the cond value is in the same BB). If the |
Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2187 | // select has uses other than the div, this allows them to be simplified |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2188 | // also. Note that div X, Y is just as good as div X, 0 (undef) |
Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2189 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) |
| 2190 | if (ST->isNullValue()) { |
| 2191 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2192 | if (CondI && CondI->getParent() == I.getParent()) |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 2193 | UpdateValueUsesWith(CondI, ConstantBool::getFalse()); |
Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2194 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2195 | I.setOperand(1, SI->getOperand(2)); |
| 2196 | else |
| 2197 | UpdateValueUsesWith(SI, SI->getOperand(2)); |
| 2198 | return &I; |
| 2199 | } |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2200 | |
Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2201 | // Likewise for: div X, (Cond ? Y : 0) -> div X, Y |
| 2202 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) |
| 2203 | if (ST->isNullValue()) { |
| 2204 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2205 | if (CondI && CondI->getParent() == I.getParent()) |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 2206 | UpdateValueUsesWith(CondI, ConstantBool::getTrue()); |
Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2207 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2208 | I.setOperand(1, SI->getOperand(1)); |
| 2209 | else |
| 2210 | UpdateValueUsesWith(SI, SI->getOperand(1)); |
| 2211 | return &I; |
| 2212 | } |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2213 | } |
Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2214 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2215 | return 0; |
| 2216 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2217 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2218 | /// This function implements the transforms common to both integer division |
| 2219 | /// instructions (udiv and sdiv). It is called by the visitors to those integer |
| 2220 | /// division instructions. |
| 2221 | /// @brief Common integer divide transforms |
| 2222 | Instruction* InstCombiner::commonIDivTransforms(BinaryOperator &I) { |
| 2223 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2224 | |
| 2225 | if (Instruction *Common = commonDivTransforms(I)) |
| 2226 | return Common; |
| 2227 | |
| 2228 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2229 | // div X, 1 == X |
| 2230 | if (RHS->equalsInt(1)) |
| 2231 | return ReplaceInstUsesWith(I, Op0); |
| 2232 | |
| 2233 | // (X / C1) / C2 -> X / (C1*C2) |
| 2234 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) |
| 2235 | if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode()) |
| 2236 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { |
| 2237 | return BinaryOperator::create(I.getOpcode(), LHS->getOperand(0), |
| 2238 | ConstantExpr::getMul(RHS, LHSRHS)); |
Chris Lattner | 4236261 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 2239 | } |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2240 | |
| 2241 | if (!RHS->isNullValue()) { // avoid X udiv 0 |
| 2242 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 2243 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 2244 | return R; |
| 2245 | if (isa<PHINode>(Op0)) |
| 2246 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2247 | return NV; |
| 2248 | } |
Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2249 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2250 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2251 | // 0 / X == 0, we don't need to preserve faults! |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2252 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2253 | if (LHS->equalsInt(0)) |
| 2254 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2255 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2256 | return 0; |
| 2257 | } |
| 2258 | |
| 2259 | Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { |
| 2260 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2261 | |
| 2262 | // Handle the integer div common cases |
| 2263 | if (Instruction *Common = commonIDivTransforms(I)) |
| 2264 | return Common; |
| 2265 | |
| 2266 | // X udiv C^2 -> X >> C |
| 2267 | // Check to see if this is an unsigned division with an exact power of 2, |
| 2268 | // if so, convert to a right shift. |
| 2269 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) { |
| 2270 | if (uint64_t Val = C->getZExtValue()) // Don't break X / 0 |
| 2271 | if (isPowerOf2_64(Val)) { |
| 2272 | uint64_t ShiftAmt = Log2_64(Val); |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2273 | return new ShiftInst(Instruction::LShr, Op0, |
| 2274 | ConstantInt::get(Type::UByteTy, ShiftAmt)); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2275 | } |
| 2276 | } |
| 2277 | |
| 2278 | // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) |
| 2279 | if (ShiftInst *RHSI = dyn_cast<ShiftInst>(I.getOperand(1))) { |
| 2280 | if (RHSI->getOpcode() == Instruction::Shl && |
| 2281 | isa<ConstantInt>(RHSI->getOperand(0))) { |
| 2282 | uint64_t C1 = cast<ConstantInt>(RHSI->getOperand(0))->getZExtValue(); |
| 2283 | if (isPowerOf2_64(C1)) { |
| 2284 | Value *N = RHSI->getOperand(1); |
| 2285 | const Type* NTy = N->getType(); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2286 | if (uint64_t C2 = Log2_64(C1)) { |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2287 | Constant *C2V = ConstantInt::get(NTy, C2); |
| 2288 | N = InsertNewInstBefore(BinaryOperator::createAdd(N, C2V, "tmp"), I); |
Chris Lattner | 2e90b73 | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2289 | } |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2290 | return new ShiftInst(Instruction::LShr, Op0, N); |
Chris Lattner | 2e90b73 | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2291 | } |
| 2292 | } |
Chris Lattner | dd0c174 | 2005-11-05 07:40:31 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2295 | // udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2) |
| 2296 | // where C1&C2 are powers of two. |
| 2297 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2298 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
| 2299 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) |
| 2300 | if (!STO->isNullValue() && !STO->isNullValue()) { |
| 2301 | uint64_t TVA = STO->getZExtValue(), FVA = SFO->getZExtValue(); |
| 2302 | if (isPowerOf2_64(TVA) && isPowerOf2_64(FVA)) { |
| 2303 | // Compute the shift amounts |
| 2304 | unsigned TSA = Log2_64(TVA), FSA = Log2_64(FVA); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2305 | // Construct the "on true" case of the select |
| 2306 | Constant *TC = ConstantInt::get(Type::UByteTy, TSA); |
| 2307 | Instruction *TSI = |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2308 | new ShiftInst(Instruction::LShr, Op0, TC, SI->getName()+".t"); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2309 | TSI = InsertNewInstBefore(TSI, I); |
| 2310 | |
| 2311 | // Construct the "on false" case of the select |
| 2312 | Constant *FC = ConstantInt::get(Type::UByteTy, FSA); |
| 2313 | Instruction *FSI = |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2314 | new ShiftInst(Instruction::LShr, Op0, FC, SI->getName()+".f"); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2315 | FSI = InsertNewInstBefore(FSI, I); |
| 2316 | |
| 2317 | // construct the select instruction and return it. |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2318 | return new SelectInst(SI->getOperand(0), TSI, FSI, SI->getName()); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2319 | } |
| 2320 | } |
| 2321 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2322 | return 0; |
| 2323 | } |
| 2324 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2325 | Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { |
| 2326 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2327 | |
| 2328 | // Handle the integer div common cases |
| 2329 | if (Instruction *Common = commonIDivTransforms(I)) |
| 2330 | return Common; |
| 2331 | |
| 2332 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2333 | // sdiv X, -1 == -X |
| 2334 | if (RHS->isAllOnesValue()) |
| 2335 | return BinaryOperator::createNeg(Op0); |
| 2336 | |
| 2337 | // -X/C -> X/-C |
| 2338 | if (Value *LHSNeg = dyn_castNegVal(Op0)) |
| 2339 | return BinaryOperator::createSDiv(LHSNeg, ConstantExpr::getNeg(RHS)); |
| 2340 | } |
| 2341 | |
| 2342 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| 2343 | // unsigned inputs), turn this into a udiv. |
| 2344 | if (I.getType()->isInteger()) { |
| 2345 | uint64_t Mask = 1ULL << (I.getType()->getPrimitiveSizeInBits()-1); |
| 2346 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
| 2347 | return BinaryOperator::createUDiv(Op0, Op1, I.getName()); |
| 2348 | } |
| 2349 | } |
| 2350 | |
| 2351 | return 0; |
| 2352 | } |
| 2353 | |
| 2354 | Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { |
| 2355 | return commonDivTransforms(I); |
| 2356 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2357 | |
Chris Lattner | 85dda9a | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2358 | /// GetFactor - If we can prove that the specified value is at least a multiple |
| 2359 | /// of some factor, return that factor. |
| 2360 | static Constant *GetFactor(Value *V) { |
| 2361 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 2362 | return CI; |
| 2363 | |
| 2364 | // Unless we can be tricky, we know this is a multiple of 1. |
| 2365 | Constant *Result = ConstantInt::get(V->getType(), 1); |
| 2366 | |
| 2367 | Instruction *I = dyn_cast<Instruction>(V); |
| 2368 | if (!I) return Result; |
| 2369 | |
| 2370 | if (I->getOpcode() == Instruction::Mul) { |
| 2371 | // Handle multiplies by a constant, etc. |
| 2372 | return ConstantExpr::getMul(GetFactor(I->getOperand(0)), |
| 2373 | GetFactor(I->getOperand(1))); |
| 2374 | } else if (I->getOpcode() == Instruction::Shl) { |
| 2375 | // (X<<C) -> X * (1 << C) |
| 2376 | if (Constant *ShRHS = dyn_cast<Constant>(I->getOperand(1))) { |
| 2377 | ShRHS = ConstantExpr::getShl(Result, ShRHS); |
| 2378 | return ConstantExpr::getMul(GetFactor(I->getOperand(0)), ShRHS); |
| 2379 | } |
| 2380 | } else if (I->getOpcode() == Instruction::And) { |
| 2381 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 2382 | // X & 0xFFF0 is known to be a multiple of 16. |
| 2383 | unsigned Zeros = CountTrailingZeros_64(RHS->getZExtValue()); |
| 2384 | if (Zeros != V->getType()->getPrimitiveSizeInBits()) |
| 2385 | return ConstantExpr::getShl(Result, |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2386 | ConstantInt::get(Type::UByteTy, Zeros)); |
Chris Lattner | 85dda9a | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2387 | } |
| 2388 | } else if (I->getOpcode() == Instruction::Cast) { |
| 2389 | Value *Op = I->getOperand(0); |
| 2390 | // Only handle int->int casts. |
| 2391 | if (!Op->getType()->isInteger()) return Result; |
| 2392 | return ConstantExpr::getCast(GetFactor(Op), V->getType()); |
| 2393 | } |
| 2394 | return Result; |
| 2395 | } |
| 2396 | |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2397 | /// This function implements the transforms on rem instructions that work |
| 2398 | /// regardless of the kind of rem instruction it is (urem, srem, or frem). It |
| 2399 | /// is used by the visitors to those instructions. |
| 2400 | /// @brief Transforms common to all three rem instructions |
| 2401 | Instruction *InstCombiner::commonRemTransforms(BinaryOperator &I) { |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2402 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2403 | |
Chris Lattner | 0de4a8d | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2404 | // 0 % X == 0, we don't need to preserve faults! |
| 2405 | if (Constant *LHS = dyn_cast<Constant>(Op0)) |
| 2406 | if (LHS->isNullValue()) |
| 2407 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2408 | |
| 2409 | if (isa<UndefValue>(Op0)) // undef % X -> 0 |
| 2410 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2411 | if (isa<UndefValue>(Op1)) |
| 2412 | return ReplaceInstUsesWith(I, Op1); // X % undef -> undef |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2413 | |
| 2414 | // Handle cases involving: rem X, (select Cond, Y, Z) |
| 2415 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2416 | // rem X, (Cond ? 0 : Y) -> rem X, Y. If the rem and the select are in |
| 2417 | // the same basic block, then we replace the select with Y, and the |
| 2418 | // condition of the select with false (if the cond value is in the same |
| 2419 | // BB). If the select has uses other than the div, this allows them to be |
| 2420 | // simplified also. |
| 2421 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) |
| 2422 | if (ST->isNullValue()) { |
| 2423 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2424 | if (CondI && CondI->getParent() == I.getParent()) |
| 2425 | UpdateValueUsesWith(CondI, ConstantBool::getFalse()); |
| 2426 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2427 | I.setOperand(1, SI->getOperand(2)); |
| 2428 | else |
| 2429 | UpdateValueUsesWith(SI, SI->getOperand(2)); |
Chris Lattner | 7fd5f07 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 2430 | return &I; |
| 2431 | } |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2432 | // Likewise for: rem X, (Cond ? Y : 0) -> rem X, Y |
| 2433 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) |
| 2434 | if (ST->isNullValue()) { |
| 2435 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2436 | if (CondI && CondI->getParent() == I.getParent()) |
| 2437 | UpdateValueUsesWith(CondI, ConstantBool::getTrue()); |
| 2438 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2439 | I.setOperand(1, SI->getOperand(1)); |
| 2440 | else |
| 2441 | UpdateValueUsesWith(SI, SI->getOperand(1)); |
| 2442 | return &I; |
| 2443 | } |
Chris Lattner | e9ff0ea | 2005-11-05 07:28:37 +0000 | [diff] [blame] | 2444 | } |
Chris Lattner | 7fd5f07 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 2445 | |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2446 | return 0; |
| 2447 | } |
| 2448 | |
| 2449 | /// This function implements the transforms common to both integer remainder |
| 2450 | /// instructions (urem and srem). It is called by the visitors to those integer |
| 2451 | /// remainder instructions. |
| 2452 | /// @brief Common integer remainder transforms |
| 2453 | Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { |
| 2454 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2455 | |
| 2456 | if (Instruction *common = commonRemTransforms(I)) |
| 2457 | return common; |
| 2458 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2459 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 0de4a8d | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2460 | // X % 0 == undef, we don't need to preserve faults! |
| 2461 | if (RHS->equalsInt(0)) |
| 2462 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); |
| 2463 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2464 | if (RHS->equalsInt(1)) // X % 1 == 0 |
| 2465 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2466 | |
Chris Lattner | b70f141 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2467 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { |
| 2468 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { |
| 2469 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 2470 | return R; |
| 2471 | } else if (isa<PHINode>(Op0I)) { |
| 2472 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2473 | return NV; |
Chris Lattner | b70f141 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2474 | } |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2475 | // (X * C1) % C2 --> 0 iff C1 % C2 == 0 |
| 2476 | if (ConstantExpr::getSRem(GetFactor(Op0I), RHS)->isNullValue()) |
Chris Lattner | 85dda9a | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2477 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | b70f141 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2478 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2481 | return 0; |
| 2482 | } |
| 2483 | |
| 2484 | Instruction *InstCombiner::visitURem(BinaryOperator &I) { |
| 2485 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2486 | |
| 2487 | if (Instruction *common = commonIRemTransforms(I)) |
| 2488 | return common; |
| 2489 | |
| 2490 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2491 | // X urem C^2 -> X and C |
| 2492 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 2493 | // if so, convert to a bitwise and. |
| 2494 | if (ConstantInt *C = dyn_cast<ConstantInt>(RHS)) |
| 2495 | if (isPowerOf2_64(C->getZExtValue())) |
| 2496 | return BinaryOperator::createAnd(Op0, SubOne(C)); |
| 2497 | } |
| 2498 | |
Chris Lattner | 2e90b73 | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2499 | if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) { |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2500 | // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1) |
| 2501 | if (RHSI->getOpcode() == Instruction::Shl && |
| 2502 | isa<ConstantInt>(RHSI->getOperand(0))) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2503 | unsigned C1 = cast<ConstantInt>(RHSI->getOperand(0))->getZExtValue(); |
Chris Lattner | 2e90b73 | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2504 | if (isPowerOf2_64(C1)) { |
| 2505 | Constant *N1 = ConstantInt::getAllOnesValue(I.getType()); |
| 2506 | Value *Add = InsertNewInstBefore(BinaryOperator::createAdd(RHSI, N1, |
| 2507 | "tmp"), I); |
| 2508 | return BinaryOperator::createAnd(Op0, Add); |
| 2509 | } |
| 2510 | } |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2511 | } |
Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2512 | |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2513 | // urem X, (select Cond, 2^C1, 2^C2) --> select Cond, (and X, C1), (and X, C2) |
| 2514 | // where C1&C2 are powers of two. |
| 2515 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2516 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
| 2517 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
| 2518 | // STO == 0 and SFO == 0 handled above. |
| 2519 | if (isPowerOf2_64(STO->getZExtValue()) && |
| 2520 | isPowerOf2_64(SFO->getZExtValue())) { |
| 2521 | Value *TrueAnd = InsertNewInstBefore( |
| 2522 | BinaryOperator::createAnd(Op0, SubOne(STO), SI->getName()+".t"), I); |
| 2523 | Value *FalseAnd = InsertNewInstBefore( |
| 2524 | BinaryOperator::createAnd(Op0, SubOne(SFO), SI->getName()+".f"), I); |
| 2525 | return new SelectInst(SI->getOperand(0), TrueAnd, FalseAnd); |
| 2526 | } |
| 2527 | } |
Chris Lattner | 2e90b73 | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2530 | return 0; |
| 2531 | } |
| 2532 | |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2533 | Instruction *InstCombiner::visitSRem(BinaryOperator &I) { |
| 2534 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2535 | |
| 2536 | if (Instruction *common = commonIRemTransforms(I)) |
| 2537 | return common; |
| 2538 | |
| 2539 | if (Value *RHSNeg = dyn_castNegVal(Op1)) |
| 2540 | if (!isa<ConstantInt>(RHSNeg) || |
| 2541 | cast<ConstantInt>(RHSNeg)->getSExtValue() > 0) { |
| 2542 | // X % -Y -> X % Y |
| 2543 | AddUsesToWorkList(I); |
| 2544 | I.setOperand(1, RHSNeg); |
| 2545 | return &I; |
| 2546 | } |
| 2547 | |
| 2548 | // If the top bits of both operands are zero (i.e. we can prove they are |
| 2549 | // unsigned inputs), turn this into a urem. |
| 2550 | uint64_t Mask = 1ULL << (I.getType()->getPrimitiveSizeInBits()-1); |
| 2551 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
| 2552 | // X srem Y -> X urem Y, iff X and Y don't have sign bit set |
| 2553 | return BinaryOperator::createURem(Op0, Op1, I.getName()); |
| 2554 | } |
| 2555 | |
| 2556 | return 0; |
| 2557 | } |
| 2558 | |
| 2559 | Instruction *InstCombiner::visitFRem(BinaryOperator &I) { |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2560 | return commonRemTransforms(I); |
| 2561 | } |
| 2562 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2563 | // isMaxValueMinusOne - return true if this is Max-1 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2564 | static bool isMaxValueMinusOne(const ConstantInt *C) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2565 | if (C->getType()->isUnsigned()) |
| 2566 | return C->getZExtValue() == C->getType()->getIntegralTypeMask()-1; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2567 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2568 | // Calculate 0111111111..11111 |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2569 | unsigned TypeBits = C->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2570 | int64_t Val = INT64_MAX; // All ones |
| 2571 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2572 | return C->getSExtValue() == Val-1; |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2573 | } |
| 2574 | |
| 2575 | // isMinValuePlusOne - return true if this is Min+1 |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2576 | static bool isMinValuePlusOne(const ConstantInt *C) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2577 | if (C->getType()->isUnsigned()) |
| 2578 | return C->getZExtValue() == 1; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2579 | |
| 2580 | // Calculate 1111111111000000000000 |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2581 | unsigned TypeBits = C->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2582 | int64_t Val = -1; // All ones |
| 2583 | Val <<= TypeBits-1; // Shift over to the right spot |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2584 | return C->getSExtValue() == Val+1; |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2587 | // isOneBitSet - Return true if there is exactly one bit set in the specified |
| 2588 | // constant. |
| 2589 | static bool isOneBitSet(const ConstantInt *CI) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2590 | uint64_t V = CI->getZExtValue(); |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2591 | return V && (V & (V-1)) == 0; |
| 2592 | } |
| 2593 | |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2594 | #if 0 // Currently unused |
| 2595 | // isLowOnes - Return true if the constant is of the form 0+1+. |
| 2596 | static bool isLowOnes(const ConstantInt *CI) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2597 | uint64_t V = CI->getZExtValue(); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2598 | |
| 2599 | // There won't be bits set in parts that the type doesn't contain. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2600 | V &= ConstantInt::getAllOnesValue(CI->getType())->getZExtValue(); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2601 | |
| 2602 | uint64_t U = V+1; // If it is low ones, this should be a power of two. |
| 2603 | return U && V && (U & V) == 0; |
| 2604 | } |
| 2605 | #endif |
| 2606 | |
| 2607 | // isHighOnes - Return true if the constant is of the form 1+0+. |
| 2608 | // This is the same as lowones(~X). |
| 2609 | static bool isHighOnes(const ConstantInt *CI) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2610 | uint64_t V = ~CI->getZExtValue(); |
Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 2611 | if (~V == 0) return false; // 0's does not match "1+" |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2612 | |
| 2613 | // There won't be bits set in parts that the type doesn't contain. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2614 | V &= ConstantInt::getAllOnesValue(CI->getType())->getZExtValue(); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2615 | |
| 2616 | uint64_t U = V+1; // If it is low ones, this should be a power of two. |
| 2617 | return U && V && (U & V) == 0; |
| 2618 | } |
| 2619 | |
| 2620 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2621 | /// getSetCondCode - Encode a setcc opcode into a three bit mask. These bits |
| 2622 | /// are carefully arranged to allow folding of expressions such as: |
| 2623 | /// |
| 2624 | /// (A < B) | (A > B) --> (A != B) |
| 2625 | /// |
| 2626 | /// Bit value '4' represents that the comparison is true if A > B, bit value '2' |
| 2627 | /// represents that the comparison is true if A == B, and bit value '1' is true |
| 2628 | /// if A < B. |
| 2629 | /// |
| 2630 | static unsigned getSetCondCode(const SetCondInst *SCI) { |
| 2631 | switch (SCI->getOpcode()) { |
| 2632 | // False -> 0 |
| 2633 | case Instruction::SetGT: return 1; |
| 2634 | case Instruction::SetEQ: return 2; |
| 2635 | case Instruction::SetGE: return 3; |
| 2636 | case Instruction::SetLT: return 4; |
| 2637 | case Instruction::SetNE: return 5; |
| 2638 | case Instruction::SetLE: return 6; |
| 2639 | // True -> 7 |
| 2640 | default: |
| 2641 | assert(0 && "Invalid SetCC opcode!"); |
| 2642 | return 0; |
| 2643 | } |
| 2644 | } |
| 2645 | |
| 2646 | /// getSetCCValue - This is the complement of getSetCondCode, which turns an |
| 2647 | /// opcode and two operands into either a constant true or false, or a brand new |
| 2648 | /// SetCC instruction. |
| 2649 | static Value *getSetCCValue(unsigned Opcode, Value *LHS, Value *RHS) { |
| 2650 | switch (Opcode) { |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 2651 | case 0: return ConstantBool::getFalse(); |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2652 | case 1: return new SetCondInst(Instruction::SetGT, LHS, RHS); |
| 2653 | case 2: return new SetCondInst(Instruction::SetEQ, LHS, RHS); |
| 2654 | case 3: return new SetCondInst(Instruction::SetGE, LHS, RHS); |
| 2655 | case 4: return new SetCondInst(Instruction::SetLT, LHS, RHS); |
| 2656 | case 5: return new SetCondInst(Instruction::SetNE, LHS, RHS); |
| 2657 | case 6: return new SetCondInst(Instruction::SetLE, LHS, RHS); |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 2658 | case 7: return ConstantBool::getTrue(); |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2659 | default: assert(0 && "Illegal SetCCCode!"); return 0; |
| 2660 | } |
| 2661 | } |
| 2662 | |
| 2663 | // FoldSetCCLogical - Implements (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B) |
| 2664 | struct FoldSetCCLogical { |
| 2665 | InstCombiner &IC; |
| 2666 | Value *LHS, *RHS; |
| 2667 | FoldSetCCLogical(InstCombiner &ic, SetCondInst *SCI) |
| 2668 | : IC(ic), LHS(SCI->getOperand(0)), RHS(SCI->getOperand(1)) {} |
| 2669 | bool shouldApply(Value *V) const { |
| 2670 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(V)) |
| 2671 | return (SCI->getOperand(0) == LHS && SCI->getOperand(1) == RHS || |
| 2672 | SCI->getOperand(0) == RHS && SCI->getOperand(1) == LHS); |
| 2673 | return false; |
| 2674 | } |
| 2675 | Instruction *apply(BinaryOperator &Log) const { |
| 2676 | SetCondInst *SCI = cast<SetCondInst>(Log.getOperand(0)); |
| 2677 | if (SCI->getOperand(0) != LHS) { |
| 2678 | assert(SCI->getOperand(1) == LHS); |
| 2679 | SCI->swapOperands(); // Swap the LHS and RHS of the SetCC |
| 2680 | } |
| 2681 | |
| 2682 | unsigned LHSCode = getSetCondCode(SCI); |
| 2683 | unsigned RHSCode = getSetCondCode(cast<SetCondInst>(Log.getOperand(1))); |
| 2684 | unsigned Code; |
| 2685 | switch (Log.getOpcode()) { |
| 2686 | case Instruction::And: Code = LHSCode & RHSCode; break; |
| 2687 | case Instruction::Or: Code = LHSCode | RHSCode; break; |
| 2688 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; |
Chris Lattner | 2caaaba | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 2689 | default: assert(0 && "Illegal logical opcode!"); return 0; |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
| 2692 | Value *RV = getSetCCValue(Code, LHS, RHS); |
| 2693 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 2694 | return I; |
| 2695 | // Otherwise, it's a constant boolean value... |
| 2696 | return IC.ReplaceInstUsesWith(Log, RV); |
| 2697 | } |
| 2698 | }; |
| 2699 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2700 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
| 2701 | // the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'. Op is |
| 2702 | // guaranteed to be either a shift instruction or a binary operator. |
| 2703 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
| 2704 | ConstantIntegral *OpRHS, |
| 2705 | ConstantIntegral *AndRHS, |
| 2706 | BinaryOperator &TheAnd) { |
| 2707 | Value *X = Op->getOperand(0); |
Chris Lattner | fcf21a7 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 2708 | Constant *Together = 0; |
| 2709 | if (!isa<ShiftInst>(Op)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2710 | Together = ConstantExpr::getAnd(AndRHS, OpRHS); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2711 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2712 | switch (Op->getOpcode()) { |
| 2713 | case Instruction::Xor: |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2714 | if (Op->hasOneUse()) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2715 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
| 2716 | std::string OpName = Op->getName(); Op->setName(""); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2717 | Instruction *And = BinaryOperator::createAnd(X, AndRHS, OpName); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2718 | InsertNewInstBefore(And, TheAnd); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2719 | return BinaryOperator::createXor(And, Together); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2720 | } |
| 2721 | break; |
| 2722 | case Instruction::Or: |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2723 | if (Together == AndRHS) // (X | C) & C --> C |
| 2724 | return ReplaceInstUsesWith(TheAnd, AndRHS); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2725 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2726 | if (Op->hasOneUse() && Together != OpRHS) { |
| 2727 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 |
| 2728 | std::string Op0Name = Op->getName(); Op->setName(""); |
| 2729 | Instruction *Or = BinaryOperator::createOr(X, Together, Op0Name); |
| 2730 | InsertNewInstBefore(Or, TheAnd); |
| 2731 | return BinaryOperator::createAnd(Or, AndRHS); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2732 | } |
| 2733 | break; |
| 2734 | case Instruction::Add: |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2735 | if (Op->hasOneUse()) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2736 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 2737 | // of the bit. First thing to check is to see if this AND is with a |
| 2738 | // single bit constant. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2739 | uint64_t AndRHSV = cast<ConstantInt>(AndRHS)->getZExtValue(); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2740 | |
| 2741 | // Clear bits that are not part of the constant. |
Chris Lattner | 77defba | 2006-02-07 07:00:41 +0000 | [diff] [blame] | 2742 | AndRHSV &= AndRHS->getType()->getIntegralTypeMask(); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2743 | |
| 2744 | // If there is only one bit set... |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2745 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2746 | // Ok, at this point, we know that we are masking the result of the |
| 2747 | // ADD down to exactly one bit. If the constant we are adding has |
| 2748 | // no bits set below this bit, then we can eliminate the ADD. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2749 | uint64_t AddRHS = cast<ConstantInt>(OpRHS)->getZExtValue(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2750 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2751 | // Check to see if any bits below the one bit set in AndRHSV are set. |
| 2752 | if ((AddRHS & (AndRHSV-1)) == 0) { |
| 2753 | // If not, the only thing that can effect the output of the AND is |
| 2754 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 2755 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 2756 | // no effect. |
| 2757 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop |
| 2758 | TheAnd.setOperand(0, X); |
| 2759 | return &TheAnd; |
| 2760 | } else { |
| 2761 | std::string Name = Op->getName(); Op->setName(""); |
| 2762 | // Pull the XOR out of the AND. |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2763 | Instruction *NewAnd = BinaryOperator::createAnd(X, AndRHS, Name); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2764 | InsertNewInstBefore(NewAnd, TheAnd); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2765 | return BinaryOperator::createXor(NewAnd, AndRHS); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2766 | } |
| 2767 | } |
| 2768 | } |
| 2769 | } |
| 2770 | break; |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2771 | |
| 2772 | case Instruction::Shl: { |
| 2773 | // We know that the AND will not produce any of the bits shifted in, so if |
| 2774 | // the anded constant includes them, clear them now! |
| 2775 | // |
| 2776 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2777 | Constant *ShlMask = ConstantExpr::getShl(AllOne, OpRHS); |
| 2778 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShlMask); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2779 | |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2780 | if (CI == ShlMask) { // Masking out bits that the shift already masks |
| 2781 | return ReplaceInstUsesWith(TheAnd, Op); // No need for the and. |
| 2782 | } else if (CI != AndRHS) { // Reducing bits set in and. |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2783 | TheAnd.setOperand(1, CI); |
| 2784 | return &TheAnd; |
| 2785 | } |
| 2786 | break; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2787 | } |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2788 | case Instruction::LShr: |
| 2789 | { |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2790 | // We know that the AND will not produce any of the bits shifted in, so if |
| 2791 | // the anded constant includes them, clear them now! This only applies to |
| 2792 | // unsigned shifts, because a signed shr may bring in set bits! |
| 2793 | // |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2794 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
| 2795 | Constant *ShrMask = ConstantExpr::getLShr(AllOne, OpRHS); |
| 2796 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask); |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2797 | |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2798 | if (CI == ShrMask) { // Masking out bits that the shift already masks. |
| 2799 | return ReplaceInstUsesWith(TheAnd, Op); |
| 2800 | } else if (CI != AndRHS) { |
| 2801 | TheAnd.setOperand(1, CI); // Reduce bits set in and cst. |
| 2802 | return &TheAnd; |
| 2803 | } |
| 2804 | break; |
| 2805 | } |
| 2806 | case Instruction::AShr: |
| 2807 | // Signed shr. |
| 2808 | // See if this is shifting in some sign extension, then masking it out |
| 2809 | // with an and. |
| 2810 | if (Op->hasOneUse()) { |
| 2811 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
| 2812 | Constant *ShrMask = ConstantExpr::getLShr(AllOne, OpRHS); |
| 2813 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask); |
| 2814 | if (CI == AndRHS) { // Masking out bits shifted in. |
| 2815 | // Make the argument unsigned. |
| 2816 | Value *ShVal = Op->getOperand(0); |
| 2817 | ShVal = InsertNewInstBefore(new ShiftInst(Instruction::LShr, ShVal, |
| 2818 | OpRHS, Op->getName()), |
| 2819 | TheAnd); |
| 2820 | Value *AndRHS2 = ConstantExpr::getCast(AndRHS, ShVal->getType()); |
| 2821 | return BinaryOperator::createAnd(ShVal, AndRHS2, TheAnd.getName()); |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2822 | } |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2823 | } |
| 2824 | break; |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2825 | } |
| 2826 | return 0; |
| 2827 | } |
| 2828 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2829 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2830 | /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is |
| 2831 | /// true, otherwise (V < Lo || V >= Hi). In pratice, we emit the more efficient |
| 2832 | /// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. IB is the location to |
| 2833 | /// insert new instructions. |
| 2834 | Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
| 2835 | bool Inside, Instruction &IB) { |
| 2836 | assert(cast<ConstantBool>(ConstantExpr::getSetLE(Lo, Hi))->getValue() && |
| 2837 | "Lo is not <= Hi in range emission code!"); |
| 2838 | if (Inside) { |
| 2839 | if (Lo == Hi) // Trivially false. |
| 2840 | return new SetCondInst(Instruction::SetNE, V, V); |
| 2841 | if (cast<ConstantIntegral>(Lo)->isMinValue()) |
| 2842 | return new SetCondInst(Instruction::SetLT, V, Hi); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2843 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2844 | Constant *AddCST = ConstantExpr::getNeg(Lo); |
| 2845 | Instruction *Add = BinaryOperator::createAdd(V, AddCST,V->getName()+".off"); |
| 2846 | InsertNewInstBefore(Add, IB); |
| 2847 | // Convert to unsigned for the comparison. |
| 2848 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 2849 | Value *OffsetVal = InsertCastBefore(Add, UnsType, IB); |
| 2850 | AddCST = ConstantExpr::getAdd(AddCST, Hi); |
| 2851 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 2852 | return new SetCondInst(Instruction::SetLT, OffsetVal, AddCST); |
| 2853 | } |
| 2854 | |
| 2855 | if (Lo == Hi) // Trivially true. |
| 2856 | return new SetCondInst(Instruction::SetEQ, V, V); |
| 2857 | |
| 2858 | Hi = SubOne(cast<ConstantInt>(Hi)); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2859 | |
| 2860 | // V < 0 || V >= Hi ->'V > Hi-1' |
| 2861 | if (cast<ConstantIntegral>(Lo)->isMinValue()) |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2862 | return new SetCondInst(Instruction::SetGT, V, Hi); |
| 2863 | |
| 2864 | // Emit X-Lo > Hi-Lo-1 |
| 2865 | Constant *AddCST = ConstantExpr::getNeg(Lo); |
| 2866 | Instruction *Add = BinaryOperator::createAdd(V, AddCST, V->getName()+".off"); |
| 2867 | InsertNewInstBefore(Add, IB); |
| 2868 | // Convert to unsigned for the comparison. |
| 2869 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 2870 | Value *OffsetVal = InsertCastBefore(Add, UnsType, IB); |
| 2871 | AddCST = ConstantExpr::getAdd(AddCST, Hi); |
| 2872 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 2873 | return new SetCondInst(Instruction::SetGT, OffsetVal, AddCST); |
| 2874 | } |
| 2875 | |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2876 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 2877 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 2878 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 2879 | // not, since all 1s are not contiguous. |
| 2880 | static bool isRunOfOnes(ConstantIntegral *Val, unsigned &MB, unsigned &ME) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2881 | uint64_t V = Val->getZExtValue(); |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2882 | if (!isShiftedMask_64(V)) return false; |
| 2883 | |
| 2884 | // look for the first zero bit after the run of ones |
| 2885 | MB = 64-CountLeadingZeros_64((V - 1) ^ V); |
| 2886 | // look for the first non-zero bit |
| 2887 | ME = 64-CountLeadingZeros_64(V); |
| 2888 | return true; |
| 2889 | } |
| 2890 | |
| 2891 | |
| 2892 | |
| 2893 | /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask, |
| 2894 | /// where isSub determines whether the operator is a sub. If we can fold one of |
| 2895 | /// the following xforms: |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 2896 | /// |
| 2897 | /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask |
| 2898 | /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 2899 | /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 2900 | /// |
| 2901 | /// return (A +/- B). |
| 2902 | /// |
| 2903 | Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS, |
| 2904 | ConstantIntegral *Mask, bool isSub, |
| 2905 | Instruction &I) { |
| 2906 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
| 2907 | if (!LHSI || LHSI->getNumOperands() != 2 || |
| 2908 | !isa<ConstantInt>(LHSI->getOperand(1))) return 0; |
| 2909 | |
| 2910 | ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1)); |
| 2911 | |
| 2912 | switch (LHSI->getOpcode()) { |
| 2913 | default: return 0; |
| 2914 | case Instruction::And: |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2915 | if (ConstantExpr::getAnd(N, Mask) == Mask) { |
| 2916 | // If the AndRHS is a power of two minus one (0+1+), this is simple. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2917 | if ((Mask->getZExtValue() & Mask->getZExtValue()+1) == 0) |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2918 | break; |
| 2919 | |
| 2920 | // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+ |
| 2921 | // part, we don't need any explicit masks to take them out of A. If that |
| 2922 | // is all N is, ignore it. |
| 2923 | unsigned MB, ME; |
| 2924 | if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 2925 | uint64_t Mask = RHS->getType()->getIntegralTypeMask(); |
| 2926 | Mask >>= 64-MB+1; |
| 2927 | if (MaskedValueIsZero(RHS, Mask)) |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2928 | break; |
| 2929 | } |
| 2930 | } |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 2931 | return 0; |
| 2932 | case Instruction::Or: |
| 2933 | case Instruction::Xor: |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2934 | // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0 |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2935 | if ((Mask->getZExtValue() & Mask->getZExtValue()+1) == 0 && |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2936 | ConstantExpr::getAnd(N, Mask)->isNullValue()) |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 2937 | break; |
| 2938 | return 0; |
| 2939 | } |
| 2940 | |
| 2941 | Instruction *New; |
| 2942 | if (isSub) |
| 2943 | New = BinaryOperator::createSub(LHSI->getOperand(0), RHS, "fold"); |
| 2944 | else |
| 2945 | New = BinaryOperator::createAdd(LHSI->getOperand(0), RHS, "fold"); |
| 2946 | return InsertNewInstBefore(New, I); |
| 2947 | } |
| 2948 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2949 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2950 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2951 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2952 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2953 | if (isa<UndefValue>(Op1)) // X & undef -> 0 |
| 2954 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2955 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2956 | // and X, X = X |
| 2957 | if (Op0 == Op1) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2958 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2959 | |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 2960 | // 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] | 2961 | // purpose is to compute bits we don't care about. |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 2962 | uint64_t KnownZero, KnownOne; |
Chris Lattner | d70d9f5 | 2006-03-25 21:58:26 +0000 | [diff] [blame] | 2963 | if (!isa<PackedType>(I.getType()) && |
| 2964 | SimplifyDemandedBits(&I, I.getType()->getIntegralTypeMask(), |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 2965 | KnownZero, KnownOne)) |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 2966 | return &I; |
| 2967 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2968 | if (ConstantIntegral *AndRHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | ab2dc4d | 2006-02-08 07:34:50 +0000 | [diff] [blame] | 2969 | uint64_t AndRHSMask = AndRHS->getZExtValue(); |
| 2970 | uint64_t TypeMask = Op0->getType()->getIntegralTypeMask(); |
Chris Lattner | ab2dc4d | 2006-02-08 07:34:50 +0000 | [diff] [blame] | 2971 | uint64_t NotAndRHS = AndRHSMask^TypeMask; |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2972 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2973 | // Optimize a variety of ((val OP C1) & C2) combinations... |
| 2974 | if (isa<BinaryOperator>(Op0) || isa<ShiftInst>(Op0)) { |
| 2975 | Instruction *Op0I = cast<Instruction>(Op0); |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2976 | Value *Op0LHS = Op0I->getOperand(0); |
| 2977 | Value *Op0RHS = Op0I->getOperand(1); |
| 2978 | switch (Op0I->getOpcode()) { |
| 2979 | case Instruction::Xor: |
| 2980 | case Instruction::Or: |
Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 2981 | // If the mask is only needed on one incoming arm, push it up. |
| 2982 | if (Op0I->hasOneUse()) { |
| 2983 | if (MaskedValueIsZero(Op0LHS, NotAndRHS)) { |
| 2984 | // Not masking anything out for the LHS, move to RHS. |
| 2985 | Instruction *NewRHS = BinaryOperator::createAnd(Op0RHS, AndRHS, |
| 2986 | Op0RHS->getName()+".masked"); |
| 2987 | InsertNewInstBefore(NewRHS, I); |
| 2988 | return BinaryOperator::create( |
| 2989 | cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2990 | } |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 2991 | if (!isa<Constant>(Op0RHS) && |
Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 2992 | MaskedValueIsZero(Op0RHS, NotAndRHS)) { |
| 2993 | // Not masking anything out for the RHS, move to LHS. |
| 2994 | Instruction *NewLHS = BinaryOperator::createAnd(Op0LHS, AndRHS, |
| 2995 | Op0LHS->getName()+".masked"); |
| 2996 | InsertNewInstBefore(NewLHS, I); |
| 2997 | return BinaryOperator::create( |
| 2998 | cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS); |
| 2999 | } |
| 3000 | } |
| 3001 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3002 | break; |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3003 | case Instruction::Add: |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3004 | // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS. |
| 3005 | // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 3006 | // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 3007 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I)) |
| 3008 | return BinaryOperator::createAnd(V, AndRHS); |
| 3009 | if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I)) |
| 3010 | return BinaryOperator::createAnd(V, AndRHS); // Add commutes |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3011 | break; |
| 3012 | |
| 3013 | case Instruction::Sub: |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3014 | // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS. |
| 3015 | // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 3016 | // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 3017 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I)) |
| 3018 | return BinaryOperator::createAnd(V, AndRHS); |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3019 | break; |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
Chris Lattner | 16464b3 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 3022 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3023 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3024 | return Res; |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3025 | } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
| 3026 | const Type *SrcTy = CI->getOperand(0)->getType(); |
| 3027 | |
Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3028 | // If this is an integer truncation or change from signed-to-unsigned, and |
| 3029 | // if the source is an and/or with immediate, transform it. This |
| 3030 | // frequently occurs for bitfield accesses. |
| 3031 | if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) { |
| 3032 | if (SrcTy->getPrimitiveSizeInBits() >= |
| 3033 | I.getType()->getPrimitiveSizeInBits() && |
| 3034 | CastOp->getNumOperands() == 2) |
Chris Lattner | ab2dc4d | 2006-02-08 07:34:50 +0000 | [diff] [blame] | 3035 | if (ConstantInt *AndCI = dyn_cast<ConstantInt>(CastOp->getOperand(1))) |
Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3036 | if (CastOp->getOpcode() == Instruction::And) { |
| 3037 | // Change: and (cast (and X, C1) to T), C2 |
| 3038 | // into : and (cast X to T), trunc(C1)&C2 |
| 3039 | // This will folds the two ands together, which may allow other |
| 3040 | // simplifications. |
| 3041 | Instruction *NewCast = |
| 3042 | new CastInst(CastOp->getOperand(0), I.getType(), |
| 3043 | CastOp->getName()+".shrunk"); |
| 3044 | NewCast = InsertNewInstBefore(NewCast, I); |
| 3045 | |
| 3046 | Constant *C3=ConstantExpr::getCast(AndCI, I.getType());//trunc(C1) |
| 3047 | C3 = ConstantExpr::getAnd(C3, AndRHS); // trunc(C1)&C2 |
| 3048 | return BinaryOperator::createAnd(NewCast, C3); |
| 3049 | } else if (CastOp->getOpcode() == Instruction::Or) { |
| 3050 | // Change: and (cast (or X, C1) to T), C2 |
| 3051 | // into : trunc(C1)&C2 iff trunc(C1)&C2 == C2 |
| 3052 | Constant *C3=ConstantExpr::getCast(AndCI, I.getType());//trunc(C1) |
| 3053 | if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS) // trunc(C1)&C2 |
| 3054 | return ReplaceInstUsesWith(I, AndRHS); |
| 3055 | } |
| 3056 | } |
Chris Lattner | 33217db | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 3057 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3058 | |
| 3059 | // Try to fold constant and into select arguments. |
| 3060 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3061 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3062 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3063 | if (isa<PHINode>(Op0)) |
| 3064 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3065 | return NV; |
Chris Lattner | 49b47ae | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3066 | } |
| 3067 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3068 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 3069 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3070 | |
Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 3071 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
| 3072 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3073 | |
Misha Brukman | 9c003d8 | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 3074 | // (~A & ~B) == (~(A | B)) - De Morgan's Law |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3075 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3076 | Instruction *Or = BinaryOperator::createOr(Op0NotVal, Op1NotVal, |
| 3077 | I.getName()+".demorgan"); |
Chris Lattner | 49b47ae | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3078 | InsertNewInstBefore(Or, I); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3079 | return BinaryOperator::createNot(Or); |
| 3080 | } |
Chris Lattner | 8b10ab3 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3081 | |
| 3082 | { |
| 3083 | Value *A = 0, *B = 0; |
Chris Lattner | 8b10ab3 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3084 | if (match(Op0, m_Or(m_Value(A), m_Value(B)))) |
| 3085 | if (A == Op1 || B == Op1) // (A | ?) & A --> A |
| 3086 | return ReplaceInstUsesWith(I, Op1); |
| 3087 | if (match(Op1, m_Or(m_Value(A), m_Value(B)))) |
| 3088 | if (A == Op0 || B == Op0) // A & (A | ?) --> A |
| 3089 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3090 | |
| 3091 | if (Op0->hasOneUse() && |
| 3092 | match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 3093 | if (A == Op1) { // (A^B)&A -> A&(A^B) |
| 3094 | I.swapOperands(); // Simplify below |
| 3095 | std::swap(Op0, Op1); |
| 3096 | } else if (B == Op1) { // (A^B)&B -> B&(B^A) |
| 3097 | cast<BinaryOperator>(Op0)->swapOperands(); |
| 3098 | I.swapOperands(); // Simplify below |
| 3099 | std::swap(Op0, Op1); |
| 3100 | } |
| 3101 | } |
| 3102 | if (Op1->hasOneUse() && |
| 3103 | match(Op1, m_Xor(m_Value(A), m_Value(B)))) { |
| 3104 | if (B == Op0) { // B&(A^B) -> B&(B^A) |
| 3105 | cast<BinaryOperator>(Op1)->swapOperands(); |
| 3106 | std::swap(A, B); |
| 3107 | } |
| 3108 | if (A == Op0) { // A&(A^B) -> A & ~B |
| 3109 | Instruction *NotB = BinaryOperator::createNot(B, "tmp"); |
| 3110 | InsertNewInstBefore(NotB, I); |
| 3111 | return BinaryOperator::createAnd(A, NotB); |
| 3112 | } |
| 3113 | } |
Chris Lattner | 8b10ab3 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3116 | |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3117 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(Op1)) { |
| 3118 | // (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B) |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3119 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 3120 | return R; |
| 3121 | |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3122 | Value *LHSVal, *RHSVal; |
| 3123 | ConstantInt *LHSCst, *RHSCst; |
| 3124 | Instruction::BinaryOps LHSCC, RHSCC; |
| 3125 | if (match(Op0, m_SetCond(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 3126 | if (match(RHS, m_SetCond(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 3127 | if (LHSVal == RHSVal && // Found (X setcc C1) & (X setcc C2) |
| 3128 | // Set[GL]E X, CST is folded to Set[GL]T elsewhere. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3129 | LHSCC != Instruction::SetGE && LHSCC != Instruction::SetLE && |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3130 | RHSCC != Instruction::SetGE && RHSCC != Instruction::SetLE) { |
| 3131 | // Ensure that the larger constant is on the RHS. |
| 3132 | Constant *Cmp = ConstantExpr::getSetGT(LHSCst, RHSCst); |
| 3133 | SetCondInst *LHS = cast<SetCondInst>(Op0); |
| 3134 | if (cast<ConstantBool>(Cmp)->getValue()) { |
| 3135 | std::swap(LHS, RHS); |
| 3136 | std::swap(LHSCst, RHSCst); |
| 3137 | std::swap(LHSCC, RHSCC); |
| 3138 | } |
| 3139 | |
| 3140 | // At this point, we know we have have two setcc instructions |
| 3141 | // comparing a value against two constants and and'ing the result |
| 3142 | // together. Because of the above check, we know that we only have |
| 3143 | // SetEQ, SetNE, SetLT, and SetGT here. We also know (from the |
| 3144 | // FoldSetCCLogical check above), that the two constants are not |
| 3145 | // equal. |
| 3146 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 3147 | |
| 3148 | switch (LHSCC) { |
| 3149 | default: assert(0 && "Unknown integer condition code!"); |
| 3150 | case Instruction::SetEQ: |
| 3151 | switch (RHSCC) { |
| 3152 | default: assert(0 && "Unknown integer condition code!"); |
| 3153 | case Instruction::SetEQ: // (X == 13 & X == 15) -> false |
| 3154 | case Instruction::SetGT: // (X == 13 & X > 15) -> false |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 3155 | return ReplaceInstUsesWith(I, ConstantBool::getFalse()); |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3156 | case Instruction::SetNE: // (X == 13 & X != 15) -> X == 13 |
| 3157 | case Instruction::SetLT: // (X == 13 & X < 15) -> X == 13 |
| 3158 | return ReplaceInstUsesWith(I, LHS); |
| 3159 | } |
| 3160 | case Instruction::SetNE: |
| 3161 | switch (RHSCC) { |
| 3162 | default: assert(0 && "Unknown integer condition code!"); |
| 3163 | case Instruction::SetLT: |
| 3164 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X < 14) -> X < 13 |
| 3165 | return new SetCondInst(Instruction::SetLT, LHSVal, LHSCst); |
| 3166 | break; // (X != 13 & X < 15) -> no change |
| 3167 | case Instruction::SetEQ: // (X != 13 & X == 15) -> X == 15 |
| 3168 | case Instruction::SetGT: // (X != 13 & X > 15) -> X > 15 |
| 3169 | return ReplaceInstUsesWith(I, RHS); |
| 3170 | case Instruction::SetNE: |
| 3171 | if (LHSCst == SubOne(RHSCst)) {// (X != 13 & X != 14) -> X-13 >u 1 |
| 3172 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 3173 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 3174 | LHSVal->getName()+".off"); |
| 3175 | InsertNewInstBefore(Add, I); |
| 3176 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 3177 | Value *OffsetVal = InsertCastBefore(Add, UnsType, I); |
| 3178 | AddCST = ConstantExpr::getSub(RHSCst, LHSCst); |
| 3179 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 3180 | return new SetCondInst(Instruction::SetGT, OffsetVal, AddCST); |
| 3181 | } |
| 3182 | break; // (X != 13 & X != 15) -> no change |
| 3183 | } |
| 3184 | break; |
| 3185 | case Instruction::SetLT: |
| 3186 | switch (RHSCC) { |
| 3187 | default: assert(0 && "Unknown integer condition code!"); |
| 3188 | case Instruction::SetEQ: // (X < 13 & X == 15) -> false |
| 3189 | case Instruction::SetGT: // (X < 13 & X > 15) -> false |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 3190 | return ReplaceInstUsesWith(I, ConstantBool::getFalse()); |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3191 | case Instruction::SetNE: // (X < 13 & X != 15) -> X < 13 |
| 3192 | case Instruction::SetLT: // (X < 13 & X < 15) -> X < 13 |
| 3193 | return ReplaceInstUsesWith(I, LHS); |
| 3194 | } |
| 3195 | case Instruction::SetGT: |
| 3196 | switch (RHSCC) { |
| 3197 | default: assert(0 && "Unknown integer condition code!"); |
| 3198 | case Instruction::SetEQ: // (X > 13 & X == 15) -> X > 13 |
| 3199 | return ReplaceInstUsesWith(I, LHS); |
| 3200 | case Instruction::SetGT: // (X > 13 & X > 15) -> X > 15 |
| 3201 | return ReplaceInstUsesWith(I, RHS); |
| 3202 | case Instruction::SetNE: |
| 3203 | if (RHSCst == AddOne(LHSCst)) // (X > 13 & X != 14) -> X > 14 |
| 3204 | return new SetCondInst(Instruction::SetGT, LHSVal, RHSCst); |
| 3205 | break; // (X > 13 & X != 15) -> no change |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3206 | case Instruction::SetLT: // (X > 13 & X < 15) -> (X-14) <u 1 |
| 3207 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, true, I); |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3208 | } |
| 3209 | } |
| 3210 | } |
| 3211 | } |
| 3212 | |
Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3213 | // fold (and (cast A), (cast B)) -> (cast (and A, B)) |
| 3214 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | e745c7d | 2006-05-05 20:51:30 +0000 | [diff] [blame] | 3215 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3216 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Chris Lattner | e745c7d | 2006-05-05 20:51:30 +0000 | [diff] [blame] | 3217 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isIntegral() && |
Chris Lattner | 1d441ad | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 3218 | // Only do this if the casts both really cause code to be generated. |
| 3219 | ValueRequiresCast(Op0C->getOperand(0), I.getType(), TD) && |
| 3220 | ValueRequiresCast(Op1C->getOperand(0), I.getType(), TD)) { |
Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3221 | Instruction *NewOp = BinaryOperator::createAnd(Op0C->getOperand(0), |
| 3222 | Op1C->getOperand(0), |
| 3223 | I.getName()); |
| 3224 | InsertNewInstBefore(NewOp, I); |
| 3225 | return new CastInst(NewOp, I.getType()); |
| 3226 | } |
| 3227 | } |
| 3228 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3229 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3230 | } |
| 3231 | |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3232 | /// CollectBSwapParts - Look to see if the specified value defines a single byte |
| 3233 | /// in the result. If it does, and if the specified byte hasn't been filled in |
| 3234 | /// yet, fill it in and return false. |
| 3235 | static bool CollectBSwapParts(Value *V, std::vector<Value*> &ByteValues) { |
| 3236 | Instruction *I = dyn_cast<Instruction>(V); |
| 3237 | if (I == 0) return true; |
| 3238 | |
| 3239 | // If this is an or instruction, it is an inner node of the bswap. |
| 3240 | if (I->getOpcode() == Instruction::Or) |
| 3241 | return CollectBSwapParts(I->getOperand(0), ByteValues) || |
| 3242 | CollectBSwapParts(I->getOperand(1), ByteValues); |
| 3243 | |
| 3244 | // If this is a shift by a constant int, and it is "24", then its operand |
| 3245 | // defines a byte. We only handle unsigned types here. |
| 3246 | if (isa<ShiftInst>(I) && isa<ConstantInt>(I->getOperand(1))) { |
| 3247 | // Not shifting the entire input by N-1 bytes? |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3248 | if (cast<ConstantInt>(I->getOperand(1))->getZExtValue() != |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3249 | 8*(ByteValues.size()-1)) |
| 3250 | return true; |
| 3251 | |
| 3252 | unsigned DestNo; |
| 3253 | if (I->getOpcode() == Instruction::Shl) { |
| 3254 | // X << 24 defines the top byte with the lowest of the input bytes. |
| 3255 | DestNo = ByteValues.size()-1; |
| 3256 | } else { |
| 3257 | // X >>u 24 defines the low byte with the highest of the input bytes. |
| 3258 | DestNo = 0; |
| 3259 | } |
| 3260 | |
| 3261 | // If the destination byte value is already defined, the values are or'd |
| 3262 | // together, which isn't a bswap (unless it's an or of the same bits). |
| 3263 | if (ByteValues[DestNo] && ByteValues[DestNo] != I->getOperand(0)) |
| 3264 | return true; |
| 3265 | ByteValues[DestNo] = I->getOperand(0); |
| 3266 | return false; |
| 3267 | } |
| 3268 | |
| 3269 | // Otherwise, we can only handle and(shift X, imm), imm). Bail out of if we |
| 3270 | // don't have this. |
| 3271 | Value *Shift = 0, *ShiftLHS = 0; |
| 3272 | ConstantInt *AndAmt = 0, *ShiftAmt = 0; |
| 3273 | if (!match(I, m_And(m_Value(Shift), m_ConstantInt(AndAmt))) || |
| 3274 | !match(Shift, m_Shift(m_Value(ShiftLHS), m_ConstantInt(ShiftAmt)))) |
| 3275 | return true; |
| 3276 | Instruction *SI = cast<Instruction>(Shift); |
| 3277 | |
| 3278 | // Make sure that the shift amount is by a multiple of 8 and isn't too big. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3279 | if (ShiftAmt->getZExtValue() & 7 || |
| 3280 | ShiftAmt->getZExtValue() > 8*ByteValues.size()) |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3281 | return true; |
| 3282 | |
| 3283 | // Turn 0xFF -> 0, 0xFF00 -> 1, 0xFF0000 -> 2, etc. |
| 3284 | unsigned DestByte; |
| 3285 | for (DestByte = 0; DestByte != ByteValues.size(); ++DestByte) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3286 | if (AndAmt->getZExtValue() == uint64_t(0xFF) << 8*DestByte) |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3287 | break; |
| 3288 | // Unknown mask for bswap. |
| 3289 | if (DestByte == ByteValues.size()) return true; |
| 3290 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3291 | unsigned ShiftBytes = ShiftAmt->getZExtValue()/8; |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3292 | unsigned SrcByte; |
| 3293 | if (SI->getOpcode() == Instruction::Shl) |
| 3294 | SrcByte = DestByte - ShiftBytes; |
| 3295 | else |
| 3296 | SrcByte = DestByte + ShiftBytes; |
| 3297 | |
| 3298 | // If the SrcByte isn't a bswapped value from the DestByte, reject it. |
| 3299 | if (SrcByte != ByteValues.size()-DestByte-1) |
| 3300 | return true; |
| 3301 | |
| 3302 | // If the destination byte value is already defined, the values are or'd |
| 3303 | // together, which isn't a bswap (unless it's an or of the same bits). |
| 3304 | if (ByteValues[DestByte] && ByteValues[DestByte] != SI->getOperand(0)) |
| 3305 | return true; |
| 3306 | ByteValues[DestByte] = SI->getOperand(0); |
| 3307 | return false; |
| 3308 | } |
| 3309 | |
| 3310 | /// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom. |
| 3311 | /// If so, insert the new bswap intrinsic and return it. |
| 3312 | Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) { |
| 3313 | // We can only handle bswap of unsigned integers, and cannot bswap one byte. |
| 3314 | if (!I.getType()->isUnsigned() || I.getType() == Type::UByteTy) |
| 3315 | return 0; |
| 3316 | |
| 3317 | /// ByteValues - For each byte of the result, we keep track of which value |
| 3318 | /// defines each byte. |
| 3319 | std::vector<Value*> ByteValues; |
| 3320 | ByteValues.resize(I.getType()->getPrimitiveSize()); |
| 3321 | |
| 3322 | // Try to find all the pieces corresponding to the bswap. |
| 3323 | if (CollectBSwapParts(I.getOperand(0), ByteValues) || |
| 3324 | CollectBSwapParts(I.getOperand(1), ByteValues)) |
| 3325 | return 0; |
| 3326 | |
| 3327 | // Check to see if all of the bytes come from the same value. |
| 3328 | Value *V = ByteValues[0]; |
| 3329 | if (V == 0) return 0; // Didn't find a byte? Must be zero. |
| 3330 | |
| 3331 | // Check to make sure that all of the bytes come from the same value. |
| 3332 | for (unsigned i = 1, e = ByteValues.size(); i != e; ++i) |
| 3333 | if (ByteValues[i] != V) |
| 3334 | return 0; |
| 3335 | |
| 3336 | // If they do then *success* we can turn this into a bswap. Figure out what |
| 3337 | // bswap to make it into. |
| 3338 | Module *M = I.getParent()->getParent()->getParent(); |
Chris Lattner | 091b6ea | 2006-07-11 18:31:26 +0000 | [diff] [blame] | 3339 | const char *FnName = 0; |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3340 | if (I.getType() == Type::UShortTy) |
| 3341 | FnName = "llvm.bswap.i16"; |
| 3342 | else if (I.getType() == Type::UIntTy) |
| 3343 | FnName = "llvm.bswap.i32"; |
| 3344 | else if (I.getType() == Type::ULongTy) |
| 3345 | FnName = "llvm.bswap.i64"; |
| 3346 | else |
| 3347 | assert(0 && "Unknown integer type!"); |
| 3348 | Function *F = M->getOrInsertFunction(FnName, I.getType(), I.getType(), NULL); |
| 3349 | |
| 3350 | return new CallInst(F, V); |
| 3351 | } |
| 3352 | |
| 3353 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3354 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3355 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3356 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3357 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3358 | if (isa<UndefValue>(Op1)) |
| 3359 | return ReplaceInstUsesWith(I, // X | undef -> -1 |
| 3360 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 3361 | |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3362 | // or X, X = X |
| 3363 | if (Op0 == Op1) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3364 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3365 | |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3366 | // See if we can simplify any instructions used by the instruction whose sole |
| 3367 | // purpose is to compute bits we don't care about. |
| 3368 | uint64_t KnownZero, KnownOne; |
Chris Lattner | d70d9f5 | 2006-03-25 21:58:26 +0000 | [diff] [blame] | 3369 | if (!isa<PackedType>(I.getType()) && |
| 3370 | SimplifyDemandedBits(&I, I.getType()->getIntegralTypeMask(), |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3371 | KnownZero, KnownOne)) |
| 3372 | return &I; |
| 3373 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3374 | // or X, -1 == -1 |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3375 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3376 | ConstantInt *C1 = 0; Value *X = 0; |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3377 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
| 3378 | 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] | 3379 | Instruction *Or = BinaryOperator::createOr(X, RHS, Op0->getName()); |
| 3380 | Op0->setName(""); |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3381 | InsertNewInstBefore(Or, I); |
| 3382 | return BinaryOperator::createAnd(Or, ConstantExpr::getOr(RHS, C1)); |
| 3383 | } |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3384 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3385 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
| 3386 | if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { |
| 3387 | std::string Op0Name = Op0->getName(); Op0->setName(""); |
| 3388 | Instruction *Or = BinaryOperator::createOr(X, RHS, Op0Name); |
| 3389 | InsertNewInstBefore(Or, I); |
| 3390 | return BinaryOperator::createXor(Or, |
| 3391 | ConstantExpr::getAnd(C1, ConstantExpr::getNot(RHS))); |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3392 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3393 | |
| 3394 | // Try to fold constant and into select arguments. |
| 3395 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3396 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3397 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3398 | if (isa<PHINode>(Op0)) |
| 3399 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3400 | return NV; |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3401 | } |
| 3402 | |
Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3403 | Value *A = 0, *B = 0; |
| 3404 | ConstantInt *C1 = 0, *C2 = 0; |
Chris Lattner | 4294cec | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 3405 | |
| 3406 | if (match(Op0, m_And(m_Value(A), m_Value(B)))) |
| 3407 | if (A == Op1 || B == Op1) // (A & ?) | A --> A |
| 3408 | return ReplaceInstUsesWith(I, Op1); |
| 3409 | if (match(Op1, m_And(m_Value(A), m_Value(B)))) |
| 3410 | if (A == Op0 || B == Op0) // A | (A & ?) --> A |
| 3411 | return ReplaceInstUsesWith(I, Op0); |
| 3412 | |
Chris Lattner | b7845d6 | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 3413 | // (A | B) | C and A | (B | C) -> bswap if possible. |
| 3414 | // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible. |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3415 | if (match(Op0, m_Or(m_Value(), m_Value())) || |
Chris Lattner | b7845d6 | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 3416 | match(Op1, m_Or(m_Value(), m_Value())) || |
| 3417 | (match(Op0, m_Shift(m_Value(), m_Value())) && |
| 3418 | match(Op1, m_Shift(m_Value(), m_Value())))) { |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3419 | if (Instruction *BSwap = MatchBSwap(I)) |
| 3420 | return BSwap; |
| 3421 | } |
| 3422 | |
Chris Lattner | b62f508 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 3423 | // (X^C)|Y -> (X|Y)^C iff Y&C == 0 |
| 3424 | 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] | 3425 | MaskedValueIsZero(Op1, C1->getZExtValue())) { |
Chris Lattner | b62f508 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 3426 | Instruction *NOr = BinaryOperator::createOr(A, Op1, Op0->getName()); |
| 3427 | Op0->setName(""); |
| 3428 | return BinaryOperator::createXor(InsertNewInstBefore(NOr, I), C1); |
| 3429 | } |
| 3430 | |
| 3431 | // Y|(X^C) -> (X|Y)^C iff Y&C == 0 |
| 3432 | 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] | 3433 | MaskedValueIsZero(Op0, C1->getZExtValue())) { |
Chris Lattner | b62f508 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 3434 | Instruction *NOr = BinaryOperator::createOr(A, Op0, Op1->getName()); |
| 3435 | Op0->setName(""); |
| 3436 | return BinaryOperator::createXor(InsertNewInstBefore(NOr, I), C1); |
| 3437 | } |
| 3438 | |
Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 3439 | // (A & C1)|(B & C2) |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3440 | if (match(Op0, m_And(m_Value(A), m_ConstantInt(C1))) && |
Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 3441 | match(Op1, m_And(m_Value(B), m_ConstantInt(C2)))) { |
| 3442 | |
| 3443 | if (A == B) // (A & C1)|(A & C2) == A & (C1|C2) |
| 3444 | return BinaryOperator::createAnd(A, ConstantExpr::getOr(C1, C2)); |
| 3445 | |
| 3446 | |
Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3447 | // If we have: ((V + N) & C1) | (V & C2) |
| 3448 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 3449 | // replace with V+N. |
| 3450 | if (C1 == ConstantExpr::getNot(C2)) { |
Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3451 | Value *V1 = 0, *V2 = 0; |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3452 | if ((C2->getZExtValue() & (C2->getZExtValue()+1)) == 0 && // C2 == 0+1+ |
Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3453 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { |
| 3454 | // Add commutes, try both ways. |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3455 | if (V1 == B && MaskedValueIsZero(V2, C2->getZExtValue())) |
Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3456 | return ReplaceInstUsesWith(I, A); |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3457 | if (V2 == B && MaskedValueIsZero(V1, C2->getZExtValue())) |
Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3458 | return ReplaceInstUsesWith(I, A); |
| 3459 | } |
| 3460 | // Or commutes, try both ways. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3461 | if ((C1->getZExtValue() & (C1->getZExtValue()+1)) == 0 && |
Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3462 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { |
| 3463 | // Add commutes, try both ways. |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3464 | if (V1 == A && MaskedValueIsZero(V2, C1->getZExtValue())) |
Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3465 | return ReplaceInstUsesWith(I, B); |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3466 | if (V2 == A && MaskedValueIsZero(V1, C1->getZExtValue())) |
Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3467 | return ReplaceInstUsesWith(I, B); |
Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 3468 | } |
| 3469 | } |
| 3470 | } |
Chris Lattner | 812aab7 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 3471 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3472 | if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1 |
| 3473 | if (A == Op1) // ~A | A == -1 |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3474 | return ReplaceInstUsesWith(I, |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3475 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 3476 | } else { |
| 3477 | A = 0; |
| 3478 | } |
Chris Lattner | 4294cec | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 3479 | // Note, A is still live here! |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3480 | if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B |
| 3481 | if (Op0 == B) |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3482 | return ReplaceInstUsesWith(I, |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3483 | ConstantIntegral::getAllOnesValue(I.getType())); |
Chris Lattner | 3e327a4 | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 3484 | |
Misha Brukman | 9c003d8 | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 3485 | // (~A | ~B) == (~(A & B)) - De Morgan's Law |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3486 | if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
| 3487 | Value *And = InsertNewInstBefore(BinaryOperator::createAnd(A, B, |
| 3488 | I.getName()+".demorgan"), I); |
| 3489 | return BinaryOperator::createNot(And); |
| 3490 | } |
Chris Lattner | 3e327a4 | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 3491 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3492 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3493 | // (setcc1 A, B) | (setcc2 A, B) --> (setcc3 A, B) |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3494 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1))) { |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3495 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 3496 | return R; |
| 3497 | |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3498 | Value *LHSVal, *RHSVal; |
| 3499 | ConstantInt *LHSCst, *RHSCst; |
| 3500 | Instruction::BinaryOps LHSCC, RHSCC; |
| 3501 | if (match(Op0, m_SetCond(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 3502 | if (match(RHS, m_SetCond(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 3503 | if (LHSVal == RHSVal && // Found (X setcc C1) | (X setcc C2) |
| 3504 | // Set[GL]E X, CST is folded to Set[GL]T elsewhere. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3505 | LHSCC != Instruction::SetGE && LHSCC != Instruction::SetLE && |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3506 | RHSCC != Instruction::SetGE && RHSCC != Instruction::SetLE) { |
| 3507 | // Ensure that the larger constant is on the RHS. |
| 3508 | Constant *Cmp = ConstantExpr::getSetGT(LHSCst, RHSCst); |
| 3509 | SetCondInst *LHS = cast<SetCondInst>(Op0); |
| 3510 | if (cast<ConstantBool>(Cmp)->getValue()) { |
| 3511 | std::swap(LHS, RHS); |
| 3512 | std::swap(LHSCst, RHSCst); |
| 3513 | std::swap(LHSCC, RHSCC); |
| 3514 | } |
| 3515 | |
| 3516 | // At this point, we know we have have two setcc instructions |
| 3517 | // comparing a value against two constants and or'ing the result |
| 3518 | // together. Because of the above check, we know that we only have |
| 3519 | // SetEQ, SetNE, SetLT, and SetGT here. We also know (from the |
| 3520 | // FoldSetCCLogical check above), that the two constants are not |
| 3521 | // equal. |
| 3522 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 3523 | |
| 3524 | switch (LHSCC) { |
| 3525 | default: assert(0 && "Unknown integer condition code!"); |
| 3526 | case Instruction::SetEQ: |
| 3527 | switch (RHSCC) { |
| 3528 | default: assert(0 && "Unknown integer condition code!"); |
| 3529 | case Instruction::SetEQ: |
| 3530 | if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13 <u 2 |
| 3531 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 3532 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 3533 | LHSVal->getName()+".off"); |
| 3534 | InsertNewInstBefore(Add, I); |
| 3535 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 3536 | Value *OffsetVal = InsertCastBefore(Add, UnsType, I); |
| 3537 | AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst); |
| 3538 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 3539 | return new SetCondInst(Instruction::SetLT, OffsetVal, AddCST); |
| 3540 | } |
| 3541 | break; // (X == 13 | X == 15) -> no change |
| 3542 | |
Chris Lattner | 5c21946 | 2005-04-19 06:04:18 +0000 | [diff] [blame] | 3543 | case Instruction::SetGT: // (X == 13 | X > 14) -> no change |
| 3544 | break; |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3545 | case Instruction::SetNE: // (X == 13 | X != 15) -> X != 15 |
| 3546 | case Instruction::SetLT: // (X == 13 | X < 15) -> X < 15 |
| 3547 | return ReplaceInstUsesWith(I, RHS); |
| 3548 | } |
| 3549 | break; |
| 3550 | case Instruction::SetNE: |
| 3551 | switch (RHSCC) { |
| 3552 | default: assert(0 && "Unknown integer condition code!"); |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3553 | case Instruction::SetEQ: // (X != 13 | X == 15) -> X != 13 |
| 3554 | case Instruction::SetGT: // (X != 13 | X > 15) -> X != 13 |
| 3555 | return ReplaceInstUsesWith(I, LHS); |
| 3556 | case Instruction::SetNE: // (X != 13 | X != 15) -> true |
Chris Lattner | 2ceb6ee | 2005-06-17 03:59:17 +0000 | [diff] [blame] | 3557 | case Instruction::SetLT: // (X != 13 | X < 15) -> true |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 3558 | return ReplaceInstUsesWith(I, ConstantBool::getTrue()); |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3559 | } |
| 3560 | break; |
| 3561 | case Instruction::SetLT: |
| 3562 | switch (RHSCC) { |
| 3563 | default: assert(0 && "Unknown integer condition code!"); |
| 3564 | case Instruction::SetEQ: // (X < 13 | X == 14) -> no change |
| 3565 | break; |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3566 | case Instruction::SetGT: // (X < 13 | X > 15) -> (X-13) > 2 |
| 3567 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false, I); |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3568 | case Instruction::SetNE: // (X < 13 | X != 15) -> X != 15 |
| 3569 | case Instruction::SetLT: // (X < 13 | X < 15) -> X < 15 |
| 3570 | return ReplaceInstUsesWith(I, RHS); |
| 3571 | } |
| 3572 | break; |
| 3573 | case Instruction::SetGT: |
| 3574 | switch (RHSCC) { |
| 3575 | default: assert(0 && "Unknown integer condition code!"); |
| 3576 | case Instruction::SetEQ: // (X > 13 | X == 15) -> X > 13 |
| 3577 | case Instruction::SetGT: // (X > 13 | X > 15) -> X > 13 |
| 3578 | return ReplaceInstUsesWith(I, LHS); |
| 3579 | case Instruction::SetNE: // (X > 13 | X != 15) -> true |
| 3580 | case Instruction::SetLT: // (X > 13 | X < 15) -> true |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 3581 | return ReplaceInstUsesWith(I, ConstantBool::getTrue()); |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3582 | } |
| 3583 | } |
| 3584 | } |
| 3585 | } |
Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3586 | |
| 3587 | // fold (or (cast A), (cast B)) -> (cast (or A, B)) |
| 3588 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | e745c7d | 2006-05-05 20:51:30 +0000 | [diff] [blame] | 3589 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3590 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Chris Lattner | e745c7d | 2006-05-05 20:51:30 +0000 | [diff] [blame] | 3591 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isIntegral() && |
Chris Lattner | 1d441ad | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 3592 | // Only do this if the casts both really cause code to be generated. |
| 3593 | ValueRequiresCast(Op0C->getOperand(0), I.getType(), TD) && |
| 3594 | ValueRequiresCast(Op1C->getOperand(0), I.getType(), TD)) { |
Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3595 | Instruction *NewOp = BinaryOperator::createOr(Op0C->getOperand(0), |
| 3596 | Op1C->getOperand(0), |
| 3597 | I.getName()); |
| 3598 | InsertNewInstBefore(NewOp, I); |
| 3599 | return new CastInst(NewOp, I.getType()); |
| 3600 | } |
| 3601 | } |
| 3602 | |
Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 3603 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3604 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3605 | } |
| 3606 | |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 3607 | // XorSelf - Implements: X ^ X --> 0 |
| 3608 | struct XorSelf { |
| 3609 | Value *RHS; |
| 3610 | XorSelf(Value *rhs) : RHS(rhs) {} |
| 3611 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 3612 | Instruction *apply(BinaryOperator &Xor) const { |
| 3613 | return &Xor; |
| 3614 | } |
| 3615 | }; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3616 | |
| 3617 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3618 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3619 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3620 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3621 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3622 | if (isa<UndefValue>(Op1)) |
| 3623 | return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef |
| 3624 | |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 3625 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
| 3626 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { |
| 3627 | assert(Result == &I && "AssociativeOpt didn't work?"); |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3628 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 3629 | } |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3630 | |
| 3631 | // See if we can simplify any instructions used by the instruction whose sole |
| 3632 | // purpose is to compute bits we don't care about. |
| 3633 | uint64_t KnownZero, KnownOne; |
Chris Lattner | d70d9f5 | 2006-03-25 21:58:26 +0000 | [diff] [blame] | 3634 | if (!isa<PackedType>(I.getType()) && |
| 3635 | SimplifyDemandedBits(&I, I.getType()->getIntegralTypeMask(), |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3636 | KnownZero, KnownOne)) |
| 3637 | return &I; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3638 | |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 3639 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 3640 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 3641 | // xor (setcc A, B), true = not (setcc A, B) = setncc A, B |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 3642 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(Op0I)) |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 3643 | if (RHS == ConstantBool::getTrue() && SCI->hasOneUse()) |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 3644 | return new SetCondInst(SCI->getInverseCondition(), |
| 3645 | SCI->getOperand(0), SCI->getOperand(1)); |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 3646 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 3647 | // ~(c-X) == X-c-1 == X+(-c-1) |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3648 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
| 3649 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3650 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); |
| 3651 | Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3652 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3653 | return BinaryOperator::createAdd(Op0I->getOperand(1), ConstantRHS); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3654 | } |
Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 3655 | |
| 3656 | // ~(~X & Y) --> (X | ~Y) |
| 3657 | if (Op0I->getOpcode() == Instruction::And && RHS->isAllOnesValue()) { |
| 3658 | if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands(); |
| 3659 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { |
| 3660 | Instruction *NotY = |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3661 | BinaryOperator::createNot(Op0I->getOperand(1), |
Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 3662 | Op0I->getOperand(1)->getName()+".not"); |
| 3663 | InsertNewInstBefore(NotY, I); |
| 3664 | return BinaryOperator::createOr(Op0NotVal, NotY); |
| 3665 | } |
| 3666 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3667 | |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 3668 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3669 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 0f68fa6 | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 3670 | // ~(X-c) --> (-c-1)-X |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3671 | if (RHS->isAllOnesValue()) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3672 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); |
| 3673 | return BinaryOperator::createSub( |
| 3674 | ConstantExpr::getSub(NegOp0CI, |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3675 | ConstantInt::get(I.getType(), 1)), |
Chris Lattner | 0f68fa6 | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 3676 | Op0I->getOperand(0)); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3677 | } |
Chris Lattner | f78df7c | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 3678 | } else if (Op0I->getOpcode() == Instruction::Or) { |
| 3679 | // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0 |
| 3680 | if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getZExtValue())) { |
| 3681 | Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS); |
| 3682 | // Anything in both C1 and C2 is known to be zero, remove it from |
| 3683 | // NewRHS. |
| 3684 | Constant *CommonBits = ConstantExpr::getAnd(Op0CI, RHS); |
| 3685 | NewRHS = ConstantExpr::getAnd(NewRHS, |
| 3686 | ConstantExpr::getNot(CommonBits)); |
| 3687 | WorkList.push_back(Op0I); |
| 3688 | I.setOperand(0, Op0I->getOperand(0)); |
| 3689 | I.setOperand(1, NewRHS); |
| 3690 | return &I; |
| 3691 | } |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 3692 | } |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 3693 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3694 | |
| 3695 | // Try to fold constant and into select arguments. |
| 3696 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3697 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3698 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3699 | if (isa<PHINode>(Op0)) |
| 3700 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3701 | return NV; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3702 | } |
| 3703 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3704 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3705 | if (X == Op1) |
| 3706 | return ReplaceInstUsesWith(I, |
| 3707 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 3708 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3709 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3710 | if (X == Op0) |
| 3711 | return ReplaceInstUsesWith(I, |
| 3712 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 3713 | |
Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3714 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 3715 | if (Op1I->getOpcode() == Instruction::Or) { |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 3716 | if (Op1I->getOperand(0) == Op0) { // B^(B|A) == (A|B)^B |
Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3717 | Op1I->swapOperands(); |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 3718 | I.swapOperands(); |
| 3719 | std::swap(Op0, Op1); |
| 3720 | } else if (Op1I->getOperand(1) == Op0) { // B^(A|B) == (A|B)^B |
Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3721 | I.swapOperands(); // Simplified below. |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 3722 | std::swap(Op0, Op1); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3723 | } |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 3724 | } else if (Op1I->getOpcode() == Instruction::Xor) { |
| 3725 | if (Op0 == Op1I->getOperand(0)) // A^(A^B) == B |
| 3726 | return ReplaceInstUsesWith(I, Op1I->getOperand(1)); |
| 3727 | else if (Op0 == Op1I->getOperand(1)) // A^(B^A) == B |
| 3728 | return ReplaceInstUsesWith(I, Op1I->getOperand(0)); |
Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3729 | } else if (Op1I->getOpcode() == Instruction::And && Op1I->hasOneUse()) { |
| 3730 | if (Op1I->getOperand(0) == Op0) // A^(A&B) -> A^(B&A) |
| 3731 | Op1I->swapOperands(); |
| 3732 | if (Op0 == Op1I->getOperand(1)) { // A^(B&A) -> (B&A)^A |
| 3733 | I.swapOperands(); // Simplified below. |
| 3734 | std::swap(Op0, Op1); |
| 3735 | } |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 3736 | } |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 3737 | |
Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3738 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 3739 | if (Op0I->getOpcode() == Instruction::Or && Op0I->hasOneUse()) { |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 3740 | if (Op0I->getOperand(0) == Op1) // (B|A)^B == (A|B)^B |
Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3741 | Op0I->swapOperands(); |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3742 | if (Op0I->getOperand(1) == Op1) { // (A|B)^B == A & ~B |
Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3743 | Instruction *NotB = BinaryOperator::createNot(Op1, "tmp"); |
| 3744 | InsertNewInstBefore(NotB, I); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3745 | return BinaryOperator::createAnd(Op0I->getOperand(0), NotB); |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 3746 | } |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 3747 | } else if (Op0I->getOpcode() == Instruction::Xor) { |
| 3748 | if (Op1 == Op0I->getOperand(0)) // (A^B)^A == B |
| 3749 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 3750 | else if (Op1 == Op0I->getOperand(1)) // (B^A)^A == B |
| 3751 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3752 | } else if (Op0I->getOpcode() == Instruction::And && Op0I->hasOneUse()) { |
| 3753 | if (Op0I->getOperand(0) == Op1) // (A&B)^A -> (B&A)^A |
| 3754 | Op0I->swapOperands(); |
Chris Lattner | 6cf4914 | 2006-04-01 22:05:01 +0000 | [diff] [blame] | 3755 | if (Op0I->getOperand(1) == Op1 && // (B&A)^A == ~B & A |
| 3756 | !isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C |
Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3757 | Instruction *N = BinaryOperator::createNot(Op0I->getOperand(0), "tmp"); |
| 3758 | InsertNewInstBefore(N, I); |
| 3759 | return BinaryOperator::createAnd(N, Op1); |
| 3760 | } |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 3761 | } |
| 3762 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3763 | // (setcc1 A, B) ^ (setcc2 A, B) --> (setcc3 A, B) |
| 3764 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1))) |
| 3765 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 3766 | return R; |
| 3767 | |
Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3768 | // fold (xor (cast A), (cast B)) -> (cast (xor A, B)) |
| 3769 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | e745c7d | 2006-05-05 20:51:30 +0000 | [diff] [blame] | 3770 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3771 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Chris Lattner | e745c7d | 2006-05-05 20:51:30 +0000 | [diff] [blame] | 3772 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isIntegral() && |
Chris Lattner | 1d441ad | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 3773 | // Only do this if the casts both really cause code to be generated. |
| 3774 | ValueRequiresCast(Op0C->getOperand(0), I.getType(), TD) && |
| 3775 | ValueRequiresCast(Op1C->getOperand(0), I.getType(), TD)) { |
Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3776 | Instruction *NewOp = BinaryOperator::createXor(Op0C->getOperand(0), |
| 3777 | Op1C->getOperand(0), |
| 3778 | I.getName()); |
| 3779 | InsertNewInstBefore(NewOp, I); |
| 3780 | return new CastInst(NewOp, I.getType()); |
| 3781 | } |
| 3782 | } |
| 3783 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3784 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3785 | } |
| 3786 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3787 | static bool isPositive(ConstantInt *C) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3788 | return C->getSExtValue() >= 0; |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3789 | } |
| 3790 | |
| 3791 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result |
| 3792 | /// overflowed for this type. |
| 3793 | static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
| 3794 | ConstantInt *In2) { |
| 3795 | Result = cast<ConstantInt>(ConstantExpr::getAdd(In1, In2)); |
| 3796 | |
| 3797 | if (In1->getType()->isUnsigned()) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3798 | return cast<ConstantInt>(Result)->getZExtValue() < |
| 3799 | cast<ConstantInt>(In1)->getZExtValue(); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3800 | if (isPositive(In1) != isPositive(In2)) |
| 3801 | return false; |
| 3802 | if (isPositive(In1)) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3803 | return cast<ConstantInt>(Result)->getSExtValue() < |
| 3804 | cast<ConstantInt>(In1)->getSExtValue(); |
| 3805 | return cast<ConstantInt>(Result)->getSExtValue() > |
| 3806 | cast<ConstantInt>(In1)->getSExtValue(); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3807 | } |
| 3808 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3809 | /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the |
| 3810 | /// code necessary to compute the offset from the base pointer (without adding |
| 3811 | /// in the base pointer). Return the result as a signed integer of intptr size. |
| 3812 | static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { |
| 3813 | TargetData &TD = IC.getTargetData(); |
| 3814 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 3815 | const Type *UIntPtrTy = TD.getIntPtrType(); |
| 3816 | const Type *SIntPtrTy = UIntPtrTy->getSignedVersion(); |
| 3817 | Value *Result = Constant::getNullValue(SIntPtrTy); |
| 3818 | |
| 3819 | // Build a mask for high order bits. |
Chris Lattner | 77defba | 2006-02-07 07:00:41 +0000 | [diff] [blame] | 3820 | uint64_t PtrSizeMask = ~0ULL >> (64-TD.getPointerSize()*8); |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3821 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3822 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { |
| 3823 | Value *Op = GEP->getOperand(i); |
Chris Lattner | d35d210 | 2005-01-13 23:26:48 +0000 | [diff] [blame] | 3824 | uint64_t Size = TD.getTypeSize(GTI.getIndexedType()) & PtrSizeMask; |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3825 | Constant *Scale = ConstantExpr::getCast(ConstantInt::get(UIntPtrTy, Size), |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3826 | SIntPtrTy); |
| 3827 | if (Constant *OpC = dyn_cast<Constant>(Op)) { |
| 3828 | if (!OpC->isNullValue()) { |
Chris Lattner | 4cb9fa3 | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 3829 | OpC = ConstantExpr::getCast(OpC, SIntPtrTy); |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3830 | Scale = ConstantExpr::getMul(OpC, Scale); |
| 3831 | if (Constant *RC = dyn_cast<Constant>(Result)) |
| 3832 | Result = ConstantExpr::getAdd(RC, Scale); |
| 3833 | else { |
| 3834 | // Emit an add instruction. |
| 3835 | Result = IC.InsertNewInstBefore( |
| 3836 | BinaryOperator::createAdd(Result, Scale, |
| 3837 | GEP->getName()+".offs"), I); |
| 3838 | } |
| 3839 | } |
| 3840 | } else { |
Chris Lattner | 7aa41cf | 2005-01-14 17:17:59 +0000 | [diff] [blame] | 3841 | // Convert to correct type. |
| 3842 | Op = IC.InsertNewInstBefore(new CastInst(Op, SIntPtrTy, |
| 3843 | Op->getName()+".c"), I); |
| 3844 | if (Size != 1) |
Chris Lattner | 4cb9fa3 | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 3845 | // We'll let instcombine(mul) convert this to a shl if possible. |
| 3846 | Op = IC.InsertNewInstBefore(BinaryOperator::createMul(Op, Scale, |
| 3847 | GEP->getName()+".idx"), I); |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3848 | |
| 3849 | // Emit an add instruction. |
Chris Lattner | 4cb9fa3 | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 3850 | Result = IC.InsertNewInstBefore(BinaryOperator::createAdd(Op, Result, |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3851 | GEP->getName()+".offs"), I); |
| 3852 | } |
| 3853 | } |
| 3854 | return Result; |
| 3855 | } |
| 3856 | |
| 3857 | /// FoldGEPSetCC - Fold comparisons between a GEP instruction and something |
| 3858 | /// else. At this point we know that the GEP is on the LHS of the comparison. |
| 3859 | Instruction *InstCombiner::FoldGEPSetCC(User *GEPLHS, Value *RHS, |
| 3860 | Instruction::BinaryOps Cond, |
| 3861 | Instruction &I) { |
| 3862 | assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!"); |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 3863 | |
| 3864 | if (CastInst *CI = dyn_cast<CastInst>(RHS)) |
| 3865 | if (isa<PointerType>(CI->getOperand(0)->getType())) |
| 3866 | RHS = CI->getOperand(0); |
| 3867 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3868 | Value *PtrBase = GEPLHS->getOperand(0); |
| 3869 | if (PtrBase == RHS) { |
| 3870 | // As an optimization, we don't actually have to compute the actual value of |
| 3871 | // OFFSET if this is a seteq or setne comparison, just return whether each |
| 3872 | // index is zero or not. |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 3873 | if (Cond == Instruction::SetEQ || Cond == Instruction::SetNE) { |
| 3874 | Instruction *InVal = 0; |
Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 3875 | gep_type_iterator GTI = gep_type_begin(GEPLHS); |
| 3876 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i, ++GTI) { |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 3877 | bool EmitIt = true; |
| 3878 | if (Constant *C = dyn_cast<Constant>(GEPLHS->getOperand(i))) { |
| 3879 | if (isa<UndefValue>(C)) // undef index -> undef. |
| 3880 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); |
| 3881 | if (C->isNullValue()) |
| 3882 | EmitIt = false; |
Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 3883 | else if (TD->getTypeSize(GTI.getIndexedType()) == 0) { |
| 3884 | EmitIt = false; // This is indexing into a zero sized array? |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3885 | } else if (isa<ConstantInt>(C)) |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 3886 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
| 3887 | ConstantBool::get(Cond == Instruction::SetNE)); |
| 3888 | } |
| 3889 | |
| 3890 | if (EmitIt) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3891 | Instruction *Comp = |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 3892 | new SetCondInst(Cond, GEPLHS->getOperand(i), |
| 3893 | Constant::getNullValue(GEPLHS->getOperand(i)->getType())); |
| 3894 | if (InVal == 0) |
| 3895 | InVal = Comp; |
| 3896 | else { |
| 3897 | InVal = InsertNewInstBefore(InVal, I); |
| 3898 | InsertNewInstBefore(Comp, I); |
| 3899 | if (Cond == Instruction::SetNE) // True if any are unequal |
| 3900 | InVal = BinaryOperator::createOr(InVal, Comp); |
| 3901 | else // True if all are equal |
| 3902 | InVal = BinaryOperator::createAnd(InVal, Comp); |
| 3903 | } |
| 3904 | } |
| 3905 | } |
| 3906 | |
| 3907 | if (InVal) |
| 3908 | return InVal; |
| 3909 | else |
| 3910 | ReplaceInstUsesWith(I, // No comparison is needed here, all indexes = 0 |
| 3911 | ConstantBool::get(Cond == Instruction::SetEQ)); |
| 3912 | } |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3913 | |
| 3914 | // Only lower this if the setcc is the only user of the GEP or if we expect |
| 3915 | // the result to fold to a constant! |
| 3916 | if (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) { |
| 3917 | // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). |
| 3918 | Value *Offset = EmitGEPOffset(GEPLHS, I, *this); |
| 3919 | return new SetCondInst(Cond, Offset, |
| 3920 | Constant::getNullValue(Offset->getType())); |
| 3921 | } |
| 3922 | } else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) { |
Chris Lattner | a21bf8d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 3923 | // If the base pointers are different, but the indices are the same, just |
| 3924 | // compare the base pointer. |
| 3925 | if (PtrBase != GEPRHS->getOperand(0)) { |
| 3926 | bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands(); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3927 | IndicesTheSame &= GEPLHS->getOperand(0)->getType() == |
Chris Lattner | bd43b9d | 2005-04-26 14:40:41 +0000 | [diff] [blame] | 3928 | GEPRHS->getOperand(0)->getType(); |
Chris Lattner | a21bf8d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 3929 | if (IndicesTheSame) |
| 3930 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 3931 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 3932 | IndicesTheSame = false; |
| 3933 | break; |
| 3934 | } |
| 3935 | |
| 3936 | // If all indices are the same, just compare the base pointers. |
| 3937 | if (IndicesTheSame) |
| 3938 | return new SetCondInst(Cond, GEPLHS->getOperand(0), |
| 3939 | GEPRHS->getOperand(0)); |
| 3940 | |
| 3941 | // Otherwise, the base pointers are different and the indices are |
| 3942 | // different, bail out. |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3943 | return 0; |
Chris Lattner | a21bf8d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 3944 | } |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 3945 | |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 3946 | // If one of the GEPs has all zero indices, recurse. |
| 3947 | bool AllZeros = true; |
| 3948 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 3949 | if (!isa<Constant>(GEPLHS->getOperand(i)) || |
| 3950 | !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) { |
| 3951 | AllZeros = false; |
| 3952 | break; |
| 3953 | } |
| 3954 | if (AllZeros) |
| 3955 | return FoldGEPSetCC(GEPRHS, GEPLHS->getOperand(0), |
| 3956 | SetCondInst::getSwappedCondition(Cond), I); |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 3957 | |
| 3958 | // If the other GEP has all zero indices, recurse. |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 3959 | AllZeros = true; |
| 3960 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 3961 | if (!isa<Constant>(GEPRHS->getOperand(i)) || |
| 3962 | !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) { |
| 3963 | AllZeros = false; |
| 3964 | break; |
| 3965 | } |
| 3966 | if (AllZeros) |
| 3967 | return FoldGEPSetCC(GEPLHS, GEPRHS->getOperand(0), Cond, I); |
| 3968 | |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 3969 | if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) { |
| 3970 | // If the GEPs only differ by one index, compare it. |
| 3971 | unsigned NumDifferences = 0; // Keep track of # differences. |
| 3972 | unsigned DiffOperand = 0; // The operand that differs. |
| 3973 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 3974 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3975 | if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() != |
| 3976 | GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | fc4429e | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 3977 | // Irreconcilable differences. |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 3978 | NumDifferences = 2; |
| 3979 | break; |
| 3980 | } else { |
| 3981 | if (NumDifferences++) break; |
| 3982 | DiffOperand = i; |
| 3983 | } |
| 3984 | } |
| 3985 | |
| 3986 | if (NumDifferences == 0) // SAME GEP? |
| 3987 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
| 3988 | ConstantBool::get(Cond == Instruction::SetEQ)); |
| 3989 | else if (NumDifferences == 1) { |
Chris Lattner | fc4429e | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 3990 | Value *LHSV = GEPLHS->getOperand(DiffOperand); |
| 3991 | Value *RHSV = GEPRHS->getOperand(DiffOperand); |
Chris Lattner | 247aef8 | 2005-07-18 23:07:33 +0000 | [diff] [blame] | 3992 | |
| 3993 | // Convert the operands to signed values to make sure to perform a |
| 3994 | // signed comparison. |
| 3995 | const Type *NewTy = LHSV->getType()->getSignedVersion(); |
| 3996 | if (LHSV->getType() != NewTy) |
Reid Spencer | 00c482b | 2006-10-26 19:19:06 +0000 | [diff] [blame] | 3997 | LHSV = InsertCastBefore(LHSV, NewTy, I); |
Chris Lattner | 247aef8 | 2005-07-18 23:07:33 +0000 | [diff] [blame] | 3998 | if (RHSV->getType() != NewTy) |
Reid Spencer | 00c482b | 2006-10-26 19:19:06 +0000 | [diff] [blame] | 3999 | RHSV = InsertCastBefore(RHSV, NewTy, I); |
Chris Lattner | 247aef8 | 2005-07-18 23:07:33 +0000 | [diff] [blame] | 4000 | return new SetCondInst(Cond, LHSV, RHSV); |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4001 | } |
| 4002 | } |
| 4003 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4004 | // Only lower this if the setcc is the only user of the GEP or if we expect |
| 4005 | // the result to fold to a constant! |
| 4006 | if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) && |
| 4007 | (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) { |
| 4008 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) |
| 4009 | Value *L = EmitGEPOffset(GEPLHS, I, *this); |
| 4010 | Value *R = EmitGEPOffset(GEPRHS, I, *this); |
| 4011 | return new SetCondInst(Cond, L, R); |
| 4012 | } |
| 4013 | } |
| 4014 | return 0; |
| 4015 | } |
| 4016 | |
| 4017 | |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4018 | Instruction *InstCombiner::visitSetCondInst(SetCondInst &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 4019 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4020 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 4021 | const Type *Ty = Op0->getType(); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4022 | |
| 4023 | // setcc X, X |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4024 | if (Op0 == Op1) |
| 4025 | return ReplaceInstUsesWith(I, ConstantBool::get(isTrueWhenEqual(I))); |
Chris Lattner | 1fc23f3 | 2002-05-09 20:11:54 +0000 | [diff] [blame] | 4026 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4027 | if (isa<UndefValue>(Op1)) // X setcc undef -> undef |
| 4028 | return ReplaceInstUsesWith(I, UndefValue::get(Type::BoolTy)); |
| 4029 | |
Chris Lattner | 15ff1e1 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 4030 | // setcc <global/alloca*/null>, <global/alloca*/null> - Global/Stack value |
| 4031 | // addresses never equal each other! We already know that Op0 != Op1. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4032 | if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) || |
| 4033 | isa<ConstantPointerNull>(Op0)) && |
| 4034 | (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) || |
Chris Lattner | 15ff1e1 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 4035 | isa<ConstantPointerNull>(Op1))) |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4036 | return ReplaceInstUsesWith(I, ConstantBool::get(!isTrueWhenEqual(I))); |
| 4037 | |
| 4038 | // setcc's with boolean values can always be turned into bitwise operations |
| 4039 | if (Ty == Type::BoolTy) { |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4040 | switch (I.getOpcode()) { |
| 4041 | default: assert(0 && "Invalid setcc instruction!"); |
| 4042 | case Instruction::SetEQ: { // seteq bool %A, %B -> ~(A^B) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4043 | Instruction *Xor = BinaryOperator::createXor(Op0, Op1, I.getName()+"tmp"); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4044 | InsertNewInstBefore(Xor, I); |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4045 | return BinaryOperator::createNot(Xor); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4046 | } |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4047 | case Instruction::SetNE: |
| 4048 | return BinaryOperator::createXor(Op0, Op1); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4049 | |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4050 | case Instruction::SetGT: |
| 4051 | std::swap(Op0, Op1); // Change setgt -> setlt |
| 4052 | // FALL THROUGH |
| 4053 | case Instruction::SetLT: { // setlt bool A, B -> ~X & Y |
| 4054 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 4055 | InsertNewInstBefore(Not, I); |
| 4056 | return BinaryOperator::createAnd(Not, Op1); |
| 4057 | } |
| 4058 | case Instruction::SetGE: |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4059 | std::swap(Op0, Op1); // Change setge -> setle |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4060 | // FALL THROUGH |
| 4061 | case Instruction::SetLE: { // setle bool %A, %B -> ~A | B |
| 4062 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 4063 | InsertNewInstBefore(Not, I); |
| 4064 | return BinaryOperator::createOr(Not, Op1); |
| 4065 | } |
| 4066 | } |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4067 | } |
| 4068 | |
Chris Lattner | 2dd0174 | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 4069 | // See if we are doing a comparison between a constant and an instruction that |
| 4070 | // can be folded into the comparison. |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4071 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4072 | // Check to see if we are comparing against the minimum or maximum value... |
| 4073 | if (CI->isMinValue()) { |
| 4074 | if (I.getOpcode() == Instruction::SetLT) // A < MIN -> FALSE |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 4075 | return ReplaceInstUsesWith(I, ConstantBool::getFalse()); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4076 | if (I.getOpcode() == Instruction::SetGE) // A >= MIN -> TRUE |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 4077 | return ReplaceInstUsesWith(I, ConstantBool::getTrue()); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4078 | if (I.getOpcode() == Instruction::SetLE) // A <= MIN -> A == MIN |
| 4079 | return BinaryOperator::createSetEQ(Op0, Op1); |
| 4080 | if (I.getOpcode() == Instruction::SetGT) // A > MIN -> A != MIN |
| 4081 | return BinaryOperator::createSetNE(Op0, Op1); |
| 4082 | |
| 4083 | } else if (CI->isMaxValue()) { |
| 4084 | if (I.getOpcode() == Instruction::SetGT) // A > MAX -> FALSE |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 4085 | return ReplaceInstUsesWith(I, ConstantBool::getFalse()); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4086 | if (I.getOpcode() == Instruction::SetLE) // A <= MAX -> TRUE |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 4087 | return ReplaceInstUsesWith(I, ConstantBool::getTrue()); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4088 | if (I.getOpcode() == Instruction::SetGE) // A >= MAX -> A == MAX |
| 4089 | return BinaryOperator::createSetEQ(Op0, Op1); |
| 4090 | if (I.getOpcode() == Instruction::SetLT) // A < MAX -> A != MAX |
| 4091 | return BinaryOperator::createSetNE(Op0, Op1); |
| 4092 | |
| 4093 | // Comparing against a value really close to min or max? |
| 4094 | } else if (isMinValuePlusOne(CI)) { |
| 4095 | if (I.getOpcode() == Instruction::SetLT) // A < MIN+1 -> A == MIN |
| 4096 | return BinaryOperator::createSetEQ(Op0, SubOne(CI)); |
| 4097 | if (I.getOpcode() == Instruction::SetGE) // A >= MIN-1 -> A != MIN |
| 4098 | return BinaryOperator::createSetNE(Op0, SubOne(CI)); |
| 4099 | |
| 4100 | } else if (isMaxValueMinusOne(CI)) { |
| 4101 | if (I.getOpcode() == Instruction::SetGT) // A > MAX-1 -> A == MAX |
| 4102 | return BinaryOperator::createSetEQ(Op0, AddOne(CI)); |
| 4103 | if (I.getOpcode() == Instruction::SetLE) // A <= MAX-1 -> A != MAX |
| 4104 | return BinaryOperator::createSetNE(Op0, AddOne(CI)); |
| 4105 | } |
| 4106 | |
| 4107 | // If we still have a setle or setge instruction, turn it into the |
| 4108 | // appropriate setlt or setgt instruction. Since the border cases have |
| 4109 | // already been handled above, this requires little checking. |
| 4110 | // |
| 4111 | if (I.getOpcode() == Instruction::SetLE) |
| 4112 | return BinaryOperator::createSetLT(Op0, AddOne(CI)); |
| 4113 | if (I.getOpcode() == Instruction::SetGE) |
| 4114 | return BinaryOperator::createSetGT(Op0, SubOne(CI)); |
| 4115 | |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4116 | |
| 4117 | // See if we can fold the comparison based on bits known to be zero or one |
| 4118 | // in the input. |
| 4119 | uint64_t KnownZero, KnownOne; |
| 4120 | if (SimplifyDemandedBits(Op0, Ty->getIntegralTypeMask(), |
| 4121 | KnownZero, KnownOne, 0)) |
| 4122 | return &I; |
| 4123 | |
| 4124 | // Given the known and unknown bits, compute a range that the LHS could be |
| 4125 | // in. |
| 4126 | if (KnownOne | KnownZero) { |
| 4127 | if (Ty->isUnsigned()) { // Unsigned comparison. |
| 4128 | uint64_t Min, Max; |
| 4129 | uint64_t RHSVal = CI->getZExtValue(); |
| 4130 | ComputeUnsignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, |
| 4131 | Min, Max); |
| 4132 | switch (I.getOpcode()) { // LE/GE have been folded already. |
| 4133 | default: assert(0 && "Unknown setcc opcode!"); |
| 4134 | case Instruction::SetEQ: |
| 4135 | if (Max < RHSVal || Min > RHSVal) |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 4136 | return ReplaceInstUsesWith(I, ConstantBool::getFalse()); |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4137 | break; |
| 4138 | case Instruction::SetNE: |
| 4139 | if (Max < RHSVal || Min > RHSVal) |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 4140 | return ReplaceInstUsesWith(I, ConstantBool::getTrue()); |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4141 | break; |
| 4142 | case Instruction::SetLT: |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 4143 | if (Max < RHSVal) |
| 4144 | return ReplaceInstUsesWith(I, ConstantBool::getTrue()); |
| 4145 | if (Min > RHSVal) |
| 4146 | return ReplaceInstUsesWith(I, ConstantBool::getFalse()); |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4147 | break; |
| 4148 | case Instruction::SetGT: |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 4149 | if (Min > RHSVal) |
| 4150 | return ReplaceInstUsesWith(I, ConstantBool::getTrue()); |
| 4151 | if (Max < RHSVal) |
| 4152 | return ReplaceInstUsesWith(I, ConstantBool::getFalse()); |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4153 | break; |
| 4154 | } |
| 4155 | } else { // Signed comparison. |
| 4156 | int64_t Min, Max; |
| 4157 | int64_t RHSVal = CI->getSExtValue(); |
| 4158 | ComputeSignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, |
| 4159 | Min, Max); |
| 4160 | switch (I.getOpcode()) { // LE/GE have been folded already. |
| 4161 | default: assert(0 && "Unknown setcc opcode!"); |
| 4162 | case Instruction::SetEQ: |
| 4163 | if (Max < RHSVal || Min > RHSVal) |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 4164 | return ReplaceInstUsesWith(I, ConstantBool::getFalse()); |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4165 | break; |
| 4166 | case Instruction::SetNE: |
| 4167 | if (Max < RHSVal || Min > RHSVal) |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 4168 | return ReplaceInstUsesWith(I, ConstantBool::getTrue()); |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4169 | break; |
| 4170 | case Instruction::SetLT: |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 4171 | if (Max < RHSVal) |
| 4172 | return ReplaceInstUsesWith(I, ConstantBool::getTrue()); |
| 4173 | if (Min > RHSVal) |
| 4174 | return ReplaceInstUsesWith(I, ConstantBool::getFalse()); |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4175 | break; |
| 4176 | case Instruction::SetGT: |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 4177 | if (Min > RHSVal) |
| 4178 | return ReplaceInstUsesWith(I, ConstantBool::getTrue()); |
| 4179 | if (Max < RHSVal) |
| 4180 | return ReplaceInstUsesWith(I, ConstantBool::getFalse()); |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4181 | break; |
| 4182 | } |
| 4183 | } |
| 4184 | } |
| 4185 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4186 | // Since the RHS is a constantInt (CI), if the left hand side is an |
| 4187 | // instruction, see if that instruction also has constants so that the |
| 4188 | // instruction can be folded into the setcc |
Chris Lattner | e1e10e1 | 2004-05-25 06:32:08 +0000 | [diff] [blame] | 4189 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4190 | switch (LHSI->getOpcode()) { |
| 4191 | case Instruction::And: |
| 4192 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && |
| 4193 | LHSI->getOperand(0)->hasOneUse()) { |
Chris Lattner | 4922a0e | 2006-09-18 05:27:43 +0000 | [diff] [blame] | 4194 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); |
| 4195 | |
| 4196 | // If an operand is an AND of a truncating cast, we can widen the |
| 4197 | // and/compare to be the input width without changing the value |
| 4198 | // produced, eliminating a cast. |
| 4199 | if (CastInst *Cast = dyn_cast<CastInst>(LHSI->getOperand(0))) { |
| 4200 | // We can do this transformation if either the AND constant does not |
| 4201 | // have its sign bit set or if it is an equality comparison. |
| 4202 | // Extending a relational comparison when we're checking the sign |
| 4203 | // bit would not work. |
| 4204 | if (Cast->hasOneUse() && Cast->isTruncIntCast() && |
| 4205 | (I.isEquality() || |
| 4206 | (AndCST->getZExtValue() == (uint64_t)AndCST->getSExtValue()) && |
| 4207 | (CI->getZExtValue() == (uint64_t)CI->getSExtValue()))) { |
| 4208 | ConstantInt *NewCST; |
| 4209 | ConstantInt *NewCI; |
| 4210 | if (Cast->getOperand(0)->getType()->isSigned()) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4211 | NewCST = ConstantInt::get(Cast->getOperand(0)->getType(), |
Chris Lattner | 4922a0e | 2006-09-18 05:27:43 +0000 | [diff] [blame] | 4212 | AndCST->getZExtValue()); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4213 | NewCI = ConstantInt::get(Cast->getOperand(0)->getType(), |
Chris Lattner | 4922a0e | 2006-09-18 05:27:43 +0000 | [diff] [blame] | 4214 | CI->getZExtValue()); |
| 4215 | } else { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4216 | NewCST = ConstantInt::get(Cast->getOperand(0)->getType(), |
Chris Lattner | 4922a0e | 2006-09-18 05:27:43 +0000 | [diff] [blame] | 4217 | AndCST->getZExtValue()); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4218 | NewCI = ConstantInt::get(Cast->getOperand(0)->getType(), |
Chris Lattner | 4922a0e | 2006-09-18 05:27:43 +0000 | [diff] [blame] | 4219 | CI->getZExtValue()); |
| 4220 | } |
| 4221 | Instruction *NewAnd = |
| 4222 | BinaryOperator::createAnd(Cast->getOperand(0), NewCST, |
| 4223 | LHSI->getName()); |
| 4224 | InsertNewInstBefore(NewAnd, I); |
| 4225 | return new SetCondInst(I.getOpcode(), NewAnd, NewCI); |
| 4226 | } |
| 4227 | } |
| 4228 | |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4229 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare |
| 4230 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This |
| 4231 | // happens a LOT in code produced by the C front-end, for bitfield |
| 4232 | // access. |
| 4233 | ShiftInst *Shift = dyn_cast<ShiftInst>(LHSI->getOperand(0)); |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4234 | |
| 4235 | // Check to see if there is a noop-cast between the shift and the and. |
| 4236 | if (!Shift) { |
| 4237 | if (CastInst *CI = dyn_cast<CastInst>(LHSI->getOperand(0))) |
| 4238 | if (CI->getOperand(0)->getType()->isIntegral() && |
| 4239 | CI->getOperand(0)->getType()->getPrimitiveSizeInBits() == |
| 4240 | CI->getType()->getPrimitiveSizeInBits()) |
| 4241 | Shift = dyn_cast<ShiftInst>(CI->getOperand(0)); |
| 4242 | } |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4243 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4244 | ConstantInt *ShAmt; |
| 4245 | ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : 0; |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4246 | const Type *Ty = Shift ? Shift->getType() : 0; // Type of the shift. |
| 4247 | const Type *AndTy = AndCST->getType(); // Type of the and. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4248 | |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4249 | // We can fold this as long as we can't shift unknown bits |
| 4250 | // into the mask. This can only happen with signed shift |
| 4251 | // rights, as they sign-extend. |
| 4252 | if (ShAmt) { |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4253 | bool CanFold = Shift->isLogicalShift(); |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4254 | if (!CanFold) { |
| 4255 | // To test for the bad case of the signed shr, see if any |
| 4256 | // of the bits shifted in could be tested after the mask. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4257 | int ShAmtVal = Ty->getPrimitiveSizeInBits()-ShAmt->getZExtValue(); |
Chris Lattner | c53cb9d | 2005-06-17 01:29:28 +0000 | [diff] [blame] | 4258 | if (ShAmtVal < 0) ShAmtVal = 0; // Out of range shift. |
| 4259 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4260 | Constant *OShAmt = ConstantInt::get(Type::UByteTy, ShAmtVal); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4261 | Constant *ShVal = |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4262 | ConstantExpr::getShl(ConstantInt::getAllOnesValue(AndTy), |
| 4263 | OShAmt); |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4264 | if (ConstantExpr::getAnd(ShVal, AndCST)->isNullValue()) |
| 4265 | CanFold = true; |
| 4266 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4267 | |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4268 | if (CanFold) { |
Chris Lattner | 6afc02f | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 4269 | Constant *NewCst; |
| 4270 | if (Shift->getOpcode() == Instruction::Shl) |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4271 | NewCst = ConstantExpr::getLShr(CI, ShAmt); |
Chris Lattner | 6afc02f | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 4272 | else |
| 4273 | NewCst = ConstantExpr::getShl(CI, ShAmt); |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 4274 | |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4275 | // Check to see if we are shifting out any of the bits being |
| 4276 | // compared. |
| 4277 | if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != CI){ |
| 4278 | // If we shifted bits out, the fold is not going to work out. |
| 4279 | // As a special case, check to see if this means that the |
| 4280 | // result is always true or false now. |
| 4281 | if (I.getOpcode() == Instruction::SetEQ) |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 4282 | return ReplaceInstUsesWith(I, ConstantBool::getFalse()); |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4283 | if (I.getOpcode() == Instruction::SetNE) |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 4284 | return ReplaceInstUsesWith(I, ConstantBool::getTrue()); |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4285 | } else { |
| 4286 | I.setOperand(1, NewCst); |
Chris Lattner | 6afc02f | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 4287 | Constant *NewAndCST; |
| 4288 | if (Shift->getOpcode() == Instruction::Shl) |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4289 | NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt); |
Chris Lattner | 6afc02f | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 4290 | else |
| 4291 | NewAndCST = ConstantExpr::getShl(AndCST, ShAmt); |
| 4292 | LHSI->setOperand(1, NewAndCST); |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4293 | if (AndTy == Ty) |
| 4294 | LHSI->setOperand(0, Shift->getOperand(0)); |
| 4295 | else { |
| 4296 | Value *NewCast = InsertCastBefore(Shift->getOperand(0), AndTy, |
| 4297 | *Shift); |
| 4298 | LHSI->setOperand(0, NewCast); |
| 4299 | } |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4300 | WorkList.push_back(Shift); // Shift is dead. |
| 4301 | AddUsesToWorkList(I); |
| 4302 | return &I; |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 4303 | } |
| 4304 | } |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 4305 | } |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4306 | |
| 4307 | // Turn ((X >> Y) & C) == 0 into (X & (C << Y)) == 0. The later is |
| 4308 | // preferable because it allows the C<<Y expression to be hoisted out |
| 4309 | // of a loop if Y is invariant and X is not. |
| 4310 | if (Shift && Shift->hasOneUse() && CI->isNullValue() && |
Chris Lattner | de07792 | 2006-09-18 18:27:05 +0000 | [diff] [blame] | 4311 | I.isEquality() && !Shift->isArithmeticShift() && |
| 4312 | isa<Instruction>(Shift->getOperand(0))) { |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4313 | // Compute C << Y. |
| 4314 | Value *NS; |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4315 | if (Shift->getOpcode() == Instruction::LShr) { |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4316 | NS = new ShiftInst(Instruction::Shl, AndCST, Shift->getOperand(1), |
| 4317 | "tmp"); |
| 4318 | } else { |
| 4319 | // Make sure we insert a logical shift. |
Chris Lattner | 4922a0e | 2006-09-18 05:27:43 +0000 | [diff] [blame] | 4320 | Constant *NewAndCST = AndCST; |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4321 | if (AndCST->getType()->isSigned()) |
Chris Lattner | 4922a0e | 2006-09-18 05:27:43 +0000 | [diff] [blame] | 4322 | NewAndCST = ConstantExpr::getCast(AndCST, |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4323 | AndCST->getType()->getUnsignedVersion()); |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4324 | NS = new ShiftInst(Instruction::LShr, NewAndCST, |
Chris Lattner | 4922a0e | 2006-09-18 05:27:43 +0000 | [diff] [blame] | 4325 | Shift->getOperand(1), "tmp"); |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4326 | } |
| 4327 | InsertNewInstBefore(cast<Instruction>(NS), I); |
| 4328 | |
| 4329 | // If C's sign doesn't agree with the and, insert a cast now. |
| 4330 | if (NS->getType() != LHSI->getType()) |
| 4331 | NS = InsertCastBefore(NS, LHSI->getType(), I); |
| 4332 | |
| 4333 | Value *ShiftOp = Shift->getOperand(0); |
| 4334 | if (ShiftOp->getType() != LHSI->getType()) |
| 4335 | ShiftOp = InsertCastBefore(ShiftOp, LHSI->getType(), I); |
| 4336 | |
| 4337 | // Compute X & (C << Y). |
| 4338 | Instruction *NewAnd = |
| 4339 | BinaryOperator::createAnd(ShiftOp, NS, LHSI->getName()); |
| 4340 | InsertNewInstBefore(NewAnd, I); |
| 4341 | |
| 4342 | I.setOperand(0, NewAnd); |
| 4343 | return &I; |
| 4344 | } |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4345 | } |
| 4346 | break; |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 4347 | |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4348 | case Instruction::Shl: // (setcc (shl X, ShAmt), CI) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4349 | if (ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4350 | if (I.isEquality()) { |
Chris Lattner | 19b57f5 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 4351 | unsigned TypeBits = CI->getType()->getPrimitiveSizeInBits(); |
| 4352 | |
| 4353 | // Check that the shift amount is in range. If not, don't perform |
| 4354 | // undefined shifts. When the shift is visited it will be |
| 4355 | // simplified. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4356 | if (ShAmt->getZExtValue() >= TypeBits) |
Chris Lattner | 19b57f5 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 4357 | break; |
| 4358 | |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4359 | // If we are comparing against bits always shifted out, the |
| 4360 | // comparison cannot succeed. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4361 | Constant *Comp = |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4362 | ConstantExpr::getShl(ConstantExpr::getLShr(CI, ShAmt), ShAmt); |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4363 | if (Comp != CI) {// Comparing against a bit that we know is zero. |
| 4364 | bool IsSetNE = I.getOpcode() == Instruction::SetNE; |
| 4365 | Constant *Cst = ConstantBool::get(IsSetNE); |
| 4366 | return ReplaceInstUsesWith(I, Cst); |
| 4367 | } |
| 4368 | |
| 4369 | if (LHSI->hasOneUse()) { |
| 4370 | // Otherwise strength reduce the shift into an and. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4371 | unsigned ShAmtVal = (unsigned)ShAmt->getZExtValue(); |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4372 | uint64_t Val = (1ULL << (TypeBits-ShAmtVal))-1; |
| 4373 | |
| 4374 | Constant *Mask; |
| 4375 | if (CI->getType()->isUnsigned()) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4376 | Mask = ConstantInt::get(CI->getType(), Val); |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4377 | } else if (ShAmtVal != 0) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4378 | Mask = ConstantInt::get(CI->getType(), Val); |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4379 | } else { |
| 4380 | Mask = ConstantInt::getAllOnesValue(CI->getType()); |
| 4381 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4382 | |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4383 | Instruction *AndI = |
| 4384 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 4385 | Mask, LHSI->getName()+".mask"); |
| 4386 | Value *And = InsertNewInstBefore(AndI, I); |
| 4387 | return new SetCondInst(I.getOpcode(), And, |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4388 | ConstantExpr::getLShr(CI, ShAmt)); |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4389 | } |
| 4390 | } |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4391 | } |
| 4392 | break; |
| 4393 | |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4394 | case Instruction::LShr: // (setcc (shr X, ShAmt), CI) |
| 4395 | case Instruction::AShr: |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4396 | if (ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4397 | if (I.isEquality()) { |
Chris Lattner | 19b57f5 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 4398 | // Check that the shift amount is in range. If not, don't perform |
| 4399 | // undefined shifts. When the shift is visited it will be |
| 4400 | // simplified. |
Chris Lattner | 104002b | 2005-06-16 01:52:07 +0000 | [diff] [blame] | 4401 | unsigned TypeBits = CI->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4402 | if (ShAmt->getZExtValue() >= TypeBits) |
Chris Lattner | 19b57f5 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 4403 | break; |
| 4404 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4405 | // If we are comparing against bits always shifted out, the |
| 4406 | // comparison cannot succeed. |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4407 | Constant *Comp; |
| 4408 | if (CI->getType()->isUnsigned()) |
| 4409 | Comp = ConstantExpr::getLShr(ConstantExpr::getShl(CI, ShAmt), |
| 4410 | ShAmt); |
| 4411 | else |
| 4412 | Comp = ConstantExpr::getAShr(ConstantExpr::getShl(CI, ShAmt), |
| 4413 | ShAmt); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4414 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4415 | if (Comp != CI) {// Comparing against a bit that we know is zero. |
| 4416 | bool IsSetNE = I.getOpcode() == Instruction::SetNE; |
| 4417 | Constant *Cst = ConstantBool::get(IsSetNE); |
| 4418 | return ReplaceInstUsesWith(I, Cst); |
| 4419 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4420 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4421 | if (LHSI->hasOneUse() || CI->isNullValue()) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4422 | unsigned ShAmtVal = (unsigned)ShAmt->getZExtValue(); |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4423 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4424 | // Otherwise strength reduce the shift into an and. |
| 4425 | uint64_t Val = ~0ULL; // All ones. |
| 4426 | Val <<= ShAmtVal; // Shift over to the right spot. |
| 4427 | |
| 4428 | Constant *Mask; |
| 4429 | if (CI->getType()->isUnsigned()) { |
Chris Lattner | 2f1457f | 2005-04-24 17:46:05 +0000 | [diff] [blame] | 4430 | Val &= ~0ULL >> (64-TypeBits); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4431 | Mask = ConstantInt::get(CI->getType(), Val); |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4432 | } else { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4433 | Mask = ConstantInt::get(CI->getType(), Val); |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4434 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4435 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4436 | Instruction *AndI = |
| 4437 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 4438 | Mask, LHSI->getName()+".mask"); |
| 4439 | Value *And = InsertNewInstBefore(AndI, I); |
| 4440 | return new SetCondInst(I.getOpcode(), And, |
| 4441 | ConstantExpr::getShl(CI, ShAmt)); |
| 4442 | } |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4443 | } |
| 4444 | } |
| 4445 | break; |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 4446 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4447 | case Instruction::SDiv: |
| 4448 | case Instruction::UDiv: |
| 4449 | // Fold: setcc ([us]div X, C1), C2 -> range test |
| 4450 | // Fold this div into the comparison, producing a range check. |
| 4451 | // Determine, based on the divide type, what the range is being |
| 4452 | // checked. If there is an overflow on the low or high side, remember |
| 4453 | // it, otherwise compute the range [low, hi) bounding the new value. |
| 4454 | // See: InsertRangeTest above for the kinds of replacements possible. |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4455 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4456 | // FIXME: If the operand types don't match the type of the divide |
| 4457 | // then don't attempt this transform. The code below doesn't have the |
| 4458 | // logic to deal with a signed divide and an unsigned compare (and |
| 4459 | // vice versa). This is because (x /s C1) <s C2 produces different |
| 4460 | // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even |
| 4461 | // (x /u C1) <u C2. Simply casting the operands and result won't |
| 4462 | // work. :( The if statement below tests that condition and bails |
| 4463 | // if it finds it. |
| 4464 | const Type* DivRHSTy = DivRHS->getType(); |
| 4465 | unsigned DivOpCode = LHSI->getOpcode(); |
| 4466 | if (I.isEquality() && |
| 4467 | ((DivOpCode == Instruction::SDiv && DivRHSTy->isUnsigned()) || |
| 4468 | (DivOpCode == Instruction::UDiv && DivRHSTy->isSigned()))) |
| 4469 | break; |
| 4470 | |
| 4471 | // Initialize the variables that will indicate the nature of the |
| 4472 | // range check. |
| 4473 | bool LoOverflow = false, HiOverflow = false; |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4474 | ConstantInt *LoBound = 0, *HiBound = 0; |
| 4475 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4476 | // Compute Prod = CI * DivRHS. We are essentially solving an equation |
| 4477 | // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and |
| 4478 | // C2 (CI). By solving for X we can turn this into a range check |
| 4479 | // instead of computing a divide. |
| 4480 | ConstantInt *Prod = |
| 4481 | cast<ConstantInt>(ConstantExpr::getMul(CI, DivRHS)); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4482 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4483 | // Determine if the product overflows by seeing if the product is |
| 4484 | // not equal to the divide. Make sure we do the same kind of divide |
| 4485 | // as in the LHS instruction that we're folding. |
| 4486 | bool ProdOV = !DivRHS->isNullValue() && |
| 4487 | (DivOpCode == Instruction::SDiv ? |
| 4488 | ConstantExpr::getSDiv(Prod, DivRHS) : |
| 4489 | ConstantExpr::getUDiv(Prod, DivRHS)) != CI; |
| 4490 | |
| 4491 | // Get the SetCC opcode |
Chris Lattner | a92af96 | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 4492 | Instruction::BinaryOps Opcode = I.getOpcode(); |
| 4493 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4494 | if (DivRHS->isNullValue()) { |
| 4495 | // Don't hack on divide by zeros! |
| 4496 | } else if (DivOpCode == Instruction::UDiv) { // udiv |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4497 | LoBound = Prod; |
| 4498 | LoOverflow = ProdOV; |
| 4499 | HiOverflow = ProdOV || AddWithOverflow(HiBound, LoBound, DivRHS); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4500 | } else if (isPositive(DivRHS)) { // Divisor is > 0. |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4501 | if (CI->isNullValue()) { // (X / pos) op 0 |
| 4502 | // Can't overflow. |
| 4503 | LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS))); |
| 4504 | HiBound = DivRHS; |
| 4505 | } else if (isPositive(CI)) { // (X / pos) op pos |
| 4506 | LoBound = Prod; |
| 4507 | LoOverflow = ProdOV; |
| 4508 | HiOverflow = ProdOV || AddWithOverflow(HiBound, Prod, DivRHS); |
| 4509 | } else { // (X / pos) op neg |
| 4510 | Constant *DivRHSH = ConstantExpr::getNeg(SubOne(DivRHS)); |
| 4511 | LoOverflow = AddWithOverflow(LoBound, Prod, |
| 4512 | cast<ConstantInt>(DivRHSH)); |
| 4513 | HiBound = Prod; |
| 4514 | HiOverflow = ProdOV; |
| 4515 | } |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4516 | } else { // Divisor is < 0. |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4517 | if (CI->isNullValue()) { // (X / neg) op 0 |
| 4518 | LoBound = AddOne(DivRHS); |
| 4519 | HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS)); |
Chris Lattner | 73bcba5 | 2005-06-17 02:05:55 +0000 | [diff] [blame] | 4520 | if (HiBound == DivRHS) |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4521 | LoBound = 0; // - INTMIN = INTMIN |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4522 | } else if (isPositive(CI)) { // (X / neg) op pos |
| 4523 | HiOverflow = LoOverflow = ProdOV; |
| 4524 | if (!LoOverflow) |
| 4525 | LoOverflow = AddWithOverflow(LoBound, Prod, AddOne(DivRHS)); |
| 4526 | HiBound = AddOne(Prod); |
| 4527 | } else { // (X / neg) op neg |
| 4528 | LoBound = Prod; |
| 4529 | LoOverflow = HiOverflow = ProdOV; |
| 4530 | HiBound = cast<ConstantInt>(ConstantExpr::getSub(Prod, DivRHS)); |
| 4531 | } |
Chris Lattner | 0b41e86 | 2004-10-08 19:15:44 +0000 | [diff] [blame] | 4532 | |
Chris Lattner | a92af96 | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 4533 | // Dividing by a negate swaps the condition. |
| 4534 | Opcode = SetCondInst::getSwappedCondition(Opcode); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4535 | } |
| 4536 | |
| 4537 | if (LoBound) { |
| 4538 | Value *X = LHSI->getOperand(0); |
Chris Lattner | a92af96 | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 4539 | switch (Opcode) { |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4540 | default: assert(0 && "Unhandled setcc opcode!"); |
| 4541 | case Instruction::SetEQ: |
| 4542 | if (LoOverflow && HiOverflow) |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 4543 | return ReplaceInstUsesWith(I, ConstantBool::getFalse()); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4544 | else if (HiOverflow) |
| 4545 | return new SetCondInst(Instruction::SetGE, X, LoBound); |
| 4546 | else if (LoOverflow) |
| 4547 | return new SetCondInst(Instruction::SetLT, X, HiBound); |
| 4548 | else |
| 4549 | return InsertRangeTest(X, LoBound, HiBound, true, I); |
| 4550 | case Instruction::SetNE: |
| 4551 | if (LoOverflow && HiOverflow) |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 4552 | return ReplaceInstUsesWith(I, ConstantBool::getTrue()); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4553 | else if (HiOverflow) |
| 4554 | return new SetCondInst(Instruction::SetLT, X, LoBound); |
| 4555 | else if (LoOverflow) |
| 4556 | return new SetCondInst(Instruction::SetGE, X, HiBound); |
| 4557 | else |
| 4558 | return InsertRangeTest(X, LoBound, HiBound, false, I); |
| 4559 | case Instruction::SetLT: |
| 4560 | if (LoOverflow) |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 4561 | return ReplaceInstUsesWith(I, ConstantBool::getFalse()); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4562 | return new SetCondInst(Instruction::SetLT, X, LoBound); |
| 4563 | case Instruction::SetGT: |
| 4564 | if (HiOverflow) |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 4565 | return ReplaceInstUsesWith(I, ConstantBool::getFalse()); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4566 | return new SetCondInst(Instruction::SetGE, X, HiBound); |
| 4567 | } |
| 4568 | } |
| 4569 | } |
| 4570 | break; |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4571 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4572 | |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 4573 | // Simplify seteq and setne instructions... |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4574 | if (I.isEquality()) { |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 4575 | bool isSetNE = I.getOpcode() == Instruction::SetNE; |
| 4576 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4577 | // If the first operand is (add|sub|and|or|xor|rem) with a constant, and |
| 4578 | // the second operand is a constant, simplify a bit. |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4579 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) { |
| 4580 | switch (BO->getOpcode()) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4581 | case Instruction::SRem: |
| 4582 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. |
| 4583 | if (CI->isNullValue() && isa<ConstantInt>(BO->getOperand(1)) && |
| 4584 | BO->hasOneUse()) { |
| 4585 | int64_t V = cast<ConstantInt>(BO->getOperand(1))->getSExtValue(); |
| 4586 | if (V > 1 && isPowerOf2_64(V)) { |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 4587 | Value *NewRem = InsertNewInstBefore(BinaryOperator::createURem( |
| 4588 | BO->getOperand(0), BO->getOperand(1), BO->getName()), I); |
Chris Lattner | 23b47b6 | 2004-07-06 07:38:18 +0000 | [diff] [blame] | 4589 | return BinaryOperator::create(I.getOpcode(), NewRem, |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 4590 | Constant::getNullValue(BO->getType())); |
Chris Lattner | 23b47b6 | 2004-07-06 07:38:18 +0000 | [diff] [blame] | 4591 | } |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 4592 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4593 | break; |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4594 | case Instruction::Add: |
Chris Lattner | 6e07936 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 4595 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. |
| 4596 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | b121ae1 | 2004-09-21 21:35:23 +0000 | [diff] [blame] | 4597 | if (BO->hasOneUse()) |
| 4598 | return new SetCondInst(I.getOpcode(), BO->getOperand(0), |
| 4599 | ConstantExpr::getSub(CI, BOp1C)); |
Chris Lattner | 6e07936 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 4600 | } else if (CI->isNullValue()) { |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4601 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 4602 | // efficiently invertible, or if the add has just this one use. |
| 4603 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4604 | |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4605 | if (Value *NegVal = dyn_castNegVal(BOp1)) |
| 4606 | return new SetCondInst(I.getOpcode(), BOp0, NegVal); |
| 4607 | else if (Value *NegVal = dyn_castNegVal(BOp0)) |
| 4608 | return new SetCondInst(I.getOpcode(), NegVal, BOp1); |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 4609 | else if (BO->hasOneUse()) { |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4610 | Instruction *Neg = BinaryOperator::createNeg(BOp1, BO->getName()); |
| 4611 | BO->setName(""); |
| 4612 | InsertNewInstBefore(Neg, I); |
| 4613 | return new SetCondInst(I.getOpcode(), BOp0, Neg); |
| 4614 | } |
| 4615 | } |
| 4616 | break; |
| 4617 | case Instruction::Xor: |
| 4618 | // For the xor case, we can xor two constants together, eliminating |
| 4619 | // the explicit xor. |
| 4620 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) |
| 4621 | return BinaryOperator::create(I.getOpcode(), BO->getOperand(0), |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4622 | ConstantExpr::getXor(CI, BOC)); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4623 | |
| 4624 | // FALLTHROUGH |
| 4625 | case Instruction::Sub: |
| 4626 | // Replace (([sub|xor] A, B) != 0) with (A != B) |
| 4627 | if (CI->isNullValue()) |
| 4628 | return new SetCondInst(I.getOpcode(), BO->getOperand(0), |
| 4629 | BO->getOperand(1)); |
| 4630 | break; |
| 4631 | |
| 4632 | case Instruction::Or: |
| 4633 | // If bits are being or'd in that are not present in the constant we |
| 4634 | // are comparing against, then the comparison could never succeed! |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4635 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 4636 | Constant *NotCI = ConstantExpr::getNot(CI); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4637 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 4638 | return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE)); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4639 | } |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4640 | break; |
| 4641 | |
| 4642 | case Instruction::And: |
| 4643 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 4644 | // If bits are being compared against that are and'd out, then the |
| 4645 | // comparison can never succeed! |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 4646 | if (!ConstantExpr::getAnd(CI, |
| 4647 | ConstantExpr::getNot(BOC))->isNullValue()) |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 4648 | return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE)); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4649 | |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 4650 | // If we have ((X & C) == C), turn it into ((X & C) != 0). |
Chris Lattner | ee59d4b | 2004-06-10 02:33:20 +0000 | [diff] [blame] | 4651 | if (CI == BOC && isOneBitSet(CI)) |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 4652 | return new SetCondInst(isSetNE ? Instruction::SetEQ : |
| 4653 | Instruction::SetNE, Op0, |
| 4654 | Constant::getNullValue(CI->getType())); |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 4655 | |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4656 | // Replace (and X, (1 << size(X)-1) != 0) with x < 0, converting X |
| 4657 | // to be a signed value as appropriate. |
| 4658 | if (isSignBit(BOC)) { |
| 4659 | Value *X = BO->getOperand(0); |
| 4660 | // If 'X' is not signed, insert a cast now... |
| 4661 | if (!BOC->getType()->isSigned()) { |
Chris Lattner | 97bfcea | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 4662 | const Type *DestTy = BOC->getType()->getSignedVersion(); |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 4663 | X = InsertCastBefore(X, DestTy, I); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4664 | } |
| 4665 | return new SetCondInst(isSetNE ? Instruction::SetLT : |
| 4666 | Instruction::SetGE, X, |
| 4667 | Constant::getNullValue(X->getType())); |
| 4668 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4669 | |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 4670 | // ((X & ~7) == 0) --> X < 8 |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 4671 | if (CI->isNullValue() && isHighOnes(BOC)) { |
| 4672 | Value *X = BO->getOperand(0); |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 4673 | Constant *NegX = ConstantExpr::getNeg(BOC); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 4674 | |
| 4675 | // If 'X' is signed, insert a cast now. |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 4676 | if (NegX->getType()->isSigned()) { |
| 4677 | const Type *DestTy = NegX->getType()->getUnsignedVersion(); |
| 4678 | X = InsertCastBefore(X, DestTy, I); |
| 4679 | NegX = ConstantExpr::getCast(NegX, DestTy); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 4680 | } |
| 4681 | |
| 4682 | return new SetCondInst(isSetNE ? Instruction::SetGE : |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 4683 | Instruction::SetLT, X, NegX); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 4684 | } |
| 4685 | |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 4686 | } |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4687 | default: break; |
| 4688 | } |
| 4689 | } |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 4690 | } else { // Not a SetEQ/SetNE |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4691 | // 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] | 4692 | if (CastInst *Cast = dyn_cast<CastInst>(Op0)) { |
| 4693 | Value *CastOp = Cast->getOperand(0); |
| 4694 | const Type *SrcTy = CastOp->getType(); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4695 | unsigned SrcTySize = SrcTy->getPrimitiveSizeInBits(); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 4696 | if (SrcTy != Cast->getType() && SrcTy->isInteger() && |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4697 | SrcTySize == Cast->getType()->getPrimitiveSizeInBits()) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4698 | assert((SrcTy->isSigned() ^ Cast->getType()->isSigned()) && |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 4699 | "Source and destination signednesses should differ!"); |
| 4700 | if (Cast->getType()->isSigned()) { |
| 4701 | // If this is a signed comparison, check for comparisons in the |
| 4702 | // vicinity of zero. |
| 4703 | if (I.getOpcode() == Instruction::SetLT && CI->isNullValue()) |
| 4704 | // X < 0 => x > 127 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4705 | return BinaryOperator::createSetGT(CastOp, |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4706 | ConstantInt::get(SrcTy, (1ULL << (SrcTySize-1))-1)); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 4707 | else if (I.getOpcode() == Instruction::SetGT && |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4708 | cast<ConstantInt>(CI)->getSExtValue() == -1) |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 4709 | // X > -1 => x < 128 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4710 | return BinaryOperator::createSetLT(CastOp, |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4711 | ConstantInt::get(SrcTy, 1ULL << (SrcTySize-1))); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 4712 | } else { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4713 | ConstantInt *CUI = cast<ConstantInt>(CI); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 4714 | if (I.getOpcode() == Instruction::SetLT && |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4715 | CUI->getZExtValue() == 1ULL << (SrcTySize-1)) |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 4716 | // X < 128 => X > -1 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4717 | return BinaryOperator::createSetGT(CastOp, |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4718 | ConstantInt::get(SrcTy, -1)); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 4719 | else if (I.getOpcode() == Instruction::SetGT && |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4720 | CUI->getZExtValue() == (1ULL << (SrcTySize-1))-1) |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 4721 | // X > 127 => X < 0 |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4722 | return BinaryOperator::createSetLT(CastOp, |
| 4723 | Constant::getNullValue(SrcTy)); |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 4724 | } |
| 4725 | } |
| 4726 | } |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 4727 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4728 | } |
| 4729 | |
Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 4730 | // Handle setcc with constant RHS's that can be integer, FP or pointer. |
| 4731 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 4732 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 4733 | switch (LHSI->getOpcode()) { |
Chris Lattner | a816eee | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 4734 | case Instruction::GetElementPtr: |
| 4735 | if (RHSC->isNullValue()) { |
| 4736 | // Transform setcc GEP P, int 0, int 0, int 0, null -> setcc P, null |
| 4737 | bool isAllZeros = true; |
| 4738 | for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i) |
| 4739 | if (!isa<Constant>(LHSI->getOperand(i)) || |
| 4740 | !cast<Constant>(LHSI->getOperand(i))->isNullValue()) { |
| 4741 | isAllZeros = false; |
| 4742 | break; |
| 4743 | } |
| 4744 | if (isAllZeros) |
| 4745 | return new SetCondInst(I.getOpcode(), LHSI->getOperand(0), |
| 4746 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
| 4747 | } |
| 4748 | break; |
| 4749 | |
Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 4750 | case Instruction::PHI: |
| 4751 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 4752 | return NV; |
| 4753 | break; |
| 4754 | case Instruction::Select: |
| 4755 | // If either operand of the select is a constant, we can fold the |
| 4756 | // comparison into the select arms, which will cause one to be |
| 4757 | // constant folded and the select turned into a bitwise or. |
| 4758 | Value *Op1 = 0, *Op2 = 0; |
| 4759 | if (LHSI->hasOneUse()) { |
| 4760 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 4761 | // Fold the known value into the constant operand. |
| 4762 | Op1 = ConstantExpr::get(I.getOpcode(), C, RHSC); |
| 4763 | // Insert a new SetCC of the other select operand. |
| 4764 | Op2 = InsertNewInstBefore(new SetCondInst(I.getOpcode(), |
| 4765 | LHSI->getOperand(2), RHSC, |
| 4766 | I.getName()), I); |
| 4767 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 4768 | // Fold the known value into the constant operand. |
| 4769 | Op2 = ConstantExpr::get(I.getOpcode(), C, RHSC); |
| 4770 | // Insert a new SetCC of the other select operand. |
| 4771 | Op1 = InsertNewInstBefore(new SetCondInst(I.getOpcode(), |
| 4772 | LHSI->getOperand(1), RHSC, |
| 4773 | I.getName()), I); |
| 4774 | } |
| 4775 | } |
Jeff Cohen | 8263985 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 4776 | |
Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 4777 | if (Op1) |
| 4778 | return new SelectInst(LHSI->getOperand(0), Op1, Op2); |
| 4779 | break; |
| 4780 | } |
| 4781 | } |
| 4782 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4783 | // If we can optimize a 'setcc GEP, P' or 'setcc P, GEP', do so now. |
| 4784 | if (User *GEP = dyn_castGetElementPtr(Op0)) |
| 4785 | if (Instruction *NI = FoldGEPSetCC(GEP, Op1, I.getOpcode(), I)) |
| 4786 | return NI; |
| 4787 | if (User *GEP = dyn_castGetElementPtr(Op1)) |
| 4788 | if (Instruction *NI = FoldGEPSetCC(GEP, Op0, |
| 4789 | SetCondInst::getSwappedCondition(I.getOpcode()), I)) |
| 4790 | return NI; |
| 4791 | |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4792 | // Test to see if the operands of the setcc are casted versions of other |
| 4793 | // 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] | 4794 | if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
| 4795 | Value *CastOp0 = CI->getOperand(0); |
| 4796 | if (CastOp0->getType()->isLosslesslyConvertibleTo(CI->getType()) && |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4797 | (isa<Constant>(Op1) || isa<CastInst>(Op1)) && I.isEquality()) { |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4798 | // We keep moving the cast from the left operand over to the right |
| 4799 | // operand, where it can often be eliminated completely. |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 4800 | Op0 = CastOp0; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4801 | |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4802 | // If operand #1 is a cast instruction, see if we can eliminate it as |
| 4803 | // well. |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 4804 | if (CastInst *CI2 = dyn_cast<CastInst>(Op1)) |
| 4805 | if (CI2->getOperand(0)->getType()->isLosslesslyConvertibleTo( |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4806 | Op0->getType())) |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 4807 | Op1 = CI2->getOperand(0); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4808 | |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4809 | // If Op1 is a constant, we can fold the cast into the constant. |
| 4810 | if (Op1->getType() != Op0->getType()) |
| 4811 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
| 4812 | Op1 = ConstantExpr::getCast(Op1C, Op0->getType()); |
| 4813 | } else { |
| 4814 | // Otherwise, cast the RHS right before the setcc |
Reid Spencer | 00c482b | 2006-10-26 19:19:06 +0000 | [diff] [blame] | 4815 | Op1 = InsertCastBefore(Op1, Op0->getType(), I); |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4816 | } |
| 4817 | return BinaryOperator::create(I.getOpcode(), Op0, Op1); |
| 4818 | } |
| 4819 | |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 4820 | // Handle the special case of: setcc (cast bool to X), <cst> |
| 4821 | // This comes up when you have code like |
| 4822 | // int X = A < B; |
| 4823 | // if (X) ... |
| 4824 | // For generality, we handle any zero-extension of any operand comparison |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4825 | // with a constant or another cast from the same type. |
| 4826 | if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1)) |
| 4827 | if (Instruction *R = visitSetCondInstWithCastAndCast(I)) |
| 4828 | return R; |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 4829 | } |
Chris Lattner | f5c8a0b | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 4830 | |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4831 | if (I.isEquality()) { |
Chris Lattner | f5c8a0b | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 4832 | Value *A, *B; |
| 4833 | if (match(Op0, m_Xor(m_Value(A), m_Value(B))) && |
| 4834 | (A == Op1 || B == Op1)) { |
| 4835 | // (A^B) == A -> B == 0 |
| 4836 | Value *OtherVal = A == Op1 ? B : A; |
| 4837 | return BinaryOperator::create(I.getOpcode(), OtherVal, |
| 4838 | Constant::getNullValue(A->getType())); |
| 4839 | } else if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && |
| 4840 | (A == Op0 || B == Op0)) { |
| 4841 | // A == (A^B) -> B == 0 |
| 4842 | Value *OtherVal = A == Op0 ? B : A; |
| 4843 | return BinaryOperator::create(I.getOpcode(), OtherVal, |
| 4844 | Constant::getNullValue(A->getType())); |
| 4845 | } else if (match(Op0, m_Sub(m_Value(A), m_Value(B))) && A == Op1) { |
| 4846 | // (A-B) == A -> B == 0 |
| 4847 | return BinaryOperator::create(I.getOpcode(), B, |
| 4848 | Constant::getNullValue(B->getType())); |
| 4849 | } else if (match(Op1, m_Sub(m_Value(A), m_Value(B))) && A == Op0) { |
| 4850 | // A == (A-B) -> B == 0 |
| 4851 | return BinaryOperator::create(I.getOpcode(), B, |
| 4852 | Constant::getNullValue(B->getType())); |
| 4853 | } |
| 4854 | } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4855 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4856 | } |
| 4857 | |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4858 | // visitSetCondInstWithCastAndCast - Handle setcond (cast x to y), (cast/cst). |
| 4859 | // We only handle extending casts so far. |
| 4860 | // |
| 4861 | Instruction *InstCombiner::visitSetCondInstWithCastAndCast(SetCondInst &SCI) { |
| 4862 | Value *LHSCIOp = cast<CastInst>(SCI.getOperand(0))->getOperand(0); |
| 4863 | const Type *SrcTy = LHSCIOp->getType(); |
| 4864 | const Type *DestTy = SCI.getOperand(0)->getType(); |
| 4865 | Value *RHSCIOp; |
| 4866 | |
| 4867 | if (!DestTy->isIntegral() || !SrcTy->isIntegral()) |
Chris Lattner | 03f06f1 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 4868 | return 0; |
| 4869 | |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4870 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); |
| 4871 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); |
| 4872 | if (SrcBits >= DestBits) return 0; // Only handle extending cast. |
| 4873 | |
| 4874 | // Is this a sign or zero extension? |
| 4875 | bool isSignSrc = SrcTy->isSigned(); |
| 4876 | bool isSignDest = DestTy->isSigned(); |
| 4877 | |
| 4878 | if (CastInst *CI = dyn_cast<CastInst>(SCI.getOperand(1))) { |
| 4879 | // Not an extension from the same type? |
| 4880 | RHSCIOp = CI->getOperand(0); |
| 4881 | if (RHSCIOp->getType() != LHSCIOp->getType()) return 0; |
| 4882 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(SCI.getOperand(1))) { |
| 4883 | // Compute the constant that would happen if we truncated to SrcTy then |
| 4884 | // reextended to DestTy. |
| 4885 | Constant *Res = ConstantExpr::getCast(CI, SrcTy); |
| 4886 | |
| 4887 | if (ConstantExpr::getCast(Res, DestTy) == CI) { |
Devang Patel | b42aef4 | 2006-10-19 18:54:08 +0000 | [diff] [blame] | 4888 | // Make sure that src sign and dest sign match. For example, |
| 4889 | // |
| 4890 | // %A = cast short %X to uint |
| 4891 | // %B = setgt uint %A, 1330 |
| 4892 | // |
Devang Patel | 88afd00 | 2006-10-19 19:21:36 +0000 | [diff] [blame] | 4893 | // It is incorrect to transform this into |
Devang Patel | b42aef4 | 2006-10-19 18:54:08 +0000 | [diff] [blame] | 4894 | // |
| 4895 | // %B = setgt short %X, 1330 |
| 4896 | // |
| 4897 | // because %A may have negative value. |
Devang Patel | 5d6df95 | 2006-10-19 20:59:13 +0000 | [diff] [blame] | 4898 | // However, it is OK if SrcTy is bool (See cast-set.ll testcase) |
| 4899 | // OR operation is EQ/NE. |
| 4900 | if (isSignSrc == isSignDest || SrcTy == Type::BoolTy || SCI.isEquality()) |
Devang Patel | b42aef4 | 2006-10-19 18:54:08 +0000 | [diff] [blame] | 4901 | RHSCIOp = Res; |
| 4902 | else |
| 4903 | return 0; |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4904 | } else { |
| 4905 | // If the value cannot be represented in the shorter type, we cannot emit |
| 4906 | // a simple comparison. |
| 4907 | if (SCI.getOpcode() == Instruction::SetEQ) |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 4908 | return ReplaceInstUsesWith(SCI, ConstantBool::getFalse()); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4909 | if (SCI.getOpcode() == Instruction::SetNE) |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 4910 | return ReplaceInstUsesWith(SCI, ConstantBool::getTrue()); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4911 | |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4912 | // Evaluate the comparison for LT. |
| 4913 | Value *Result; |
| 4914 | if (DestTy->isSigned()) { |
| 4915 | // We're performing a signed comparison. |
| 4916 | if (isSignSrc) { |
| 4917 | // Signed extend and signed comparison. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4918 | if (cast<ConstantInt>(CI)->getSExtValue() < 0)// X < (small) --> false |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 4919 | Result = ConstantBool::getFalse(); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4920 | else |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4921 | Result = ConstantBool::getTrue(); // X < (large) --> true |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4922 | } else { |
| 4923 | // Unsigned extend and signed comparison. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4924 | if (cast<ConstantInt>(CI)->getSExtValue() < 0) |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 4925 | Result = ConstantBool::getFalse(); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4926 | else |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 4927 | Result = ConstantBool::getTrue(); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4928 | } |
| 4929 | } else { |
| 4930 | // We're performing an unsigned comparison. |
| 4931 | if (!isSignSrc) { |
| 4932 | // Unsigned extend & compare -> always true. |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 4933 | Result = ConstantBool::getTrue(); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4934 | } else { |
| 4935 | // We're performing an unsigned comp with a sign extended value. |
| 4936 | // This is true if the input is >= 0. [aka >s -1] |
| 4937 | Constant *NegOne = ConstantIntegral::getAllOnesValue(SrcTy); |
| 4938 | Result = InsertNewInstBefore(BinaryOperator::createSetGT(LHSCIOp, |
| 4939 | NegOne, SCI.getName()), SCI); |
| 4940 | } |
Reid Spencer | 279fa25 | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 4941 | } |
Chris Lattner | 03f06f1 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 4942 | |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4943 | // Finally, return the value computed. |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4944 | if (SCI.getOpcode() == Instruction::SetLT) { |
| 4945 | return ReplaceInstUsesWith(SCI, Result); |
| 4946 | } else { |
| 4947 | assert(SCI.getOpcode()==Instruction::SetGT &&"SetCC should be folded!"); |
| 4948 | if (Constant *CI = dyn_cast<Constant>(Result)) |
| 4949 | return ReplaceInstUsesWith(SCI, ConstantExpr::getNot(CI)); |
| 4950 | else |
| 4951 | return BinaryOperator::createNot(Result); |
| 4952 | } |
Chris Lattner | 03f06f1 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 4953 | } |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4954 | } else { |
| 4955 | return 0; |
Reid Spencer | 279fa25 | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 4956 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4957 | |
Chris Lattner | 252a845 | 2005-06-16 03:00:08 +0000 | [diff] [blame] | 4958 | // Okay, just insert a compare of the reduced operands now! |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4959 | return BinaryOperator::create(SCI.getOpcode(), LHSCIOp, RHSCIOp); |
| 4960 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4961 | |
Chris Lattner | e8d6c60 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 4962 | Instruction *InstCombiner::visitShiftInst(ShiftInst &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4963 | assert(I.getOperand(1)->getType() == Type::UByteTy); |
| 4964 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 4965 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4966 | |
| 4967 | // shl X, 0 == X and shr X, 0 == X |
| 4968 | // shl 0, X == 0 and shr 0, X == 0 |
| 4969 | if (Op1 == Constant::getNullValue(Type::UByteTy) || |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 4970 | Op0 == Constant::getNullValue(Op0->getType())) |
| 4971 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f5b4ef7 | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 4972 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4973 | if (isa<UndefValue>(Op0)) { // undef >>s X -> undef |
| 4974 | if (!isLeftShift && I.getType()->isSigned()) |
Chris Lattner | 67f0545 | 2004-10-16 23:28:04 +0000 | [diff] [blame] | 4975 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4976 | else // undef << X -> 0 AND undef >>u X -> 0 |
| 4977 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 4978 | } |
| 4979 | if (isa<UndefValue>(Op1)) { |
Chris Lattner | 18aa4d8 | 2005-07-20 18:49:28 +0000 | [diff] [blame] | 4980 | if (isLeftShift || I.getType()->isUnsigned())// X << undef, X >>u undef -> 0 |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4981 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 4982 | else |
| 4983 | return ReplaceInstUsesWith(I, Op0); // X >>s undef -> X |
| 4984 | } |
| 4985 | |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 4986 | // shr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 4987 | if (!isLeftShift) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4988 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | 5dee3b2 | 2006-10-20 18:20:21 +0000 | [diff] [blame] | 4989 | if (CSI->isAllOnesValue() && Op0->getType()->isSigned()) |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 4990 | return ReplaceInstUsesWith(I, CSI); |
| 4991 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4992 | // Try to fold constant and into select arguments. |
| 4993 | if (isa<Constant>(Op0)) |
| 4994 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4995 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4996 | return R; |
| 4997 | |
Chris Lattner | b18dbbf | 2005-05-08 17:34:56 +0000 | [diff] [blame] | 4998 | // See if we can turn a signed shr into an unsigned shr. |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4999 | if (I.isArithmeticShift()) { |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 5000 | if (MaskedValueIsZero(Op0, |
| 5001 | 1ULL << (I.getType()->getPrimitiveSizeInBits()-1))) { |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 5002 | return new ShiftInst(Instruction::LShr, Op0, Op1, I.getName()); |
Chris Lattner | b18dbbf | 2005-05-08 17:34:56 +0000 | [diff] [blame] | 5003 | } |
| 5004 | } |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 5005 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5006 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1)) |
| 5007 | if (CUI->getType()->isUnsigned()) |
| 5008 | if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I)) |
| 5009 | return Res; |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5010 | return 0; |
| 5011 | } |
| 5012 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5013 | Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5014 | ShiftInst &I) { |
| 5015 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 5016 | bool isSignedShift = isLeftShift ? Op0->getType()->isSigned() : |
| 5017 | I.getOpcode() == Instruction::AShr; |
Chris Lattner | b330939 | 2006-01-06 07:22:22 +0000 | [diff] [blame] | 5018 | bool isUnsignedShift = !isSignedShift; |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5019 | |
Chris Lattner | f5b4ef7 | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 5020 | // See if we can simplify any instructions used by the instruction whose sole |
| 5021 | // purpose is to compute bits we don't care about. |
| 5022 | uint64_t KnownZero, KnownOne; |
| 5023 | if (SimplifyDemandedBits(&I, I.getType()->getIntegralTypeMask(), |
| 5024 | KnownZero, KnownOne)) |
| 5025 | return &I; |
| 5026 | |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5027 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr |
| 5028 | // of a signed value. |
| 5029 | // |
| 5030 | unsigned TypeBits = Op0->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5031 | if (Op1->getZExtValue() >= TypeBits) { |
Chris Lattner | b330939 | 2006-01-06 07:22:22 +0000 | [diff] [blame] | 5032 | if (isUnsignedShift || isLeftShift) |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5033 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
| 5034 | else { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5035 | I.setOperand(1, ConstantInt::get(Type::UByteTy, TypeBits-1)); |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5036 | return &I; |
Chris Lattner | f5ce254 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 5037 | } |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5038 | } |
| 5039 | |
| 5040 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 5041 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 5042 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 5043 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
| 5044 | return BinaryOperator::createMul(BO->getOperand(0), |
| 5045 | ConstantExpr::getShl(BOOp, Op1)); |
| 5046 | |
| 5047 | // Try to fold constant and into select arguments. |
| 5048 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 5049 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 5050 | return R; |
| 5051 | if (isa<PHINode>(Op0)) |
| 5052 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 5053 | return NV; |
| 5054 | |
| 5055 | if (Op0->hasOneUse()) { |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5056 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) { |
| 5057 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
| 5058 | Value *V1, *V2; |
| 5059 | ConstantInt *CC; |
| 5060 | switch (Op0BO->getOpcode()) { |
Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5061 | default: break; |
| 5062 | case Instruction::Add: |
| 5063 | case Instruction::And: |
| 5064 | case Instruction::Or: |
| 5065 | case Instruction::Xor: |
| 5066 | // These operators commute. |
| 5067 | // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5068 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
| 5069 | match(Op0BO->getOperand(1), |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5070 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5071 | Instruction *YS = new ShiftInst(Instruction::Shl, |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5072 | Op0BO->getOperand(0), Op1, |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5073 | Op0BO->getName()); |
| 5074 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 24cd2fa | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5075 | Instruction *X = |
| 5076 | BinaryOperator::create(Op0BO->getOpcode(), YS, V1, |
| 5077 | Op0BO->getOperand(1)->getName()); |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5078 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
| 5079 | Constant *C2 = ConstantInt::getAllOnesValue(X->getType()); |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5080 | C2 = ConstantExpr::getShl(C2, Op1); |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5081 | return BinaryOperator::createAnd(X, C2); |
| 5082 | } |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5083 | |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5084 | // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C)) |
| 5085 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
| 5086 | match(Op0BO->getOperand(1), |
| 5087 | m_And(m_Shr(m_Value(V1), m_Value(V2)), |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5088 | m_ConstantInt(CC))) && V2 == Op1 && |
Chris Lattner | 24cd2fa | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5089 | cast<BinaryOperator>(Op0BO->getOperand(1))->getOperand(0)->hasOneUse()) { |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5090 | Instruction *YS = new ShiftInst(Instruction::Shl, |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5091 | Op0BO->getOperand(0), Op1, |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5092 | Op0BO->getName()); |
| 5093 | InsertNewInstBefore(YS, I); // (Y << C) |
| 5094 | Instruction *XM = |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5095 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5096 | V1->getName()+".mask"); |
| 5097 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 5098 | |
| 5099 | return BinaryOperator::create(Op0BO->getOpcode(), YS, XM); |
| 5100 | } |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5101 | |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5102 | // FALL THROUGH. |
Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5103 | case Instruction::Sub: |
| 5104 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5105 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 5106 | match(Op0BO->getOperand(0), |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5107 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5108 | Instruction *YS = new ShiftInst(Instruction::Shl, |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5109 | Op0BO->getOperand(1), Op1, |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5110 | Op0BO->getName()); |
| 5111 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 24cd2fa | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5112 | Instruction *X = |
Chris Lattner | 1df0e98 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 5113 | BinaryOperator::create(Op0BO->getOpcode(), V1, YS, |
Chris Lattner | 24cd2fa | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5114 | Op0BO->getOperand(0)->getName()); |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5115 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
| 5116 | Constant *C2 = ConstantInt::getAllOnesValue(X->getType()); |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5117 | C2 = ConstantExpr::getShl(C2, Op1); |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5118 | return BinaryOperator::createAnd(X, C2); |
| 5119 | } |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5120 | |
Chris Lattner | 1df0e98 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 5121 | // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C) |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5122 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 5123 | match(Op0BO->getOperand(0), |
| 5124 | m_And(m_Shr(m_Value(V1), m_Value(V2)), |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5125 | m_ConstantInt(CC))) && V2 == Op1 && |
Chris Lattner | 24cd2fa | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5126 | cast<BinaryOperator>(Op0BO->getOperand(0)) |
| 5127 | ->getOperand(0)->hasOneUse()) { |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5128 | Instruction *YS = new ShiftInst(Instruction::Shl, |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5129 | Op0BO->getOperand(1), Op1, |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5130 | Op0BO->getName()); |
| 5131 | InsertNewInstBefore(YS, I); // (Y << C) |
| 5132 | Instruction *XM = |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5133 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5134 | V1->getName()+".mask"); |
| 5135 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 5136 | |
Chris Lattner | 1df0e98 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 5137 | return BinaryOperator::create(Op0BO->getOpcode(), XM, YS); |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5138 | } |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5139 | |
Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5140 | break; |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5141 | } |
| 5142 | |
| 5143 | |
| 5144 | // If the operand is an bitwise operator with a constant RHS, and the |
| 5145 | // shift is the only use, we can pull it out of the shift. |
| 5146 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 5147 | bool isValid = true; // Valid only for And, Or, Xor |
| 5148 | bool highBitSet = false; // Transform if high bit of constant set? |
| 5149 | |
| 5150 | switch (Op0BO->getOpcode()) { |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 5151 | default: isValid = false; break; // Do not perform transform! |
Chris Lattner | 44bd392 | 2004-10-08 03:46:20 +0000 | [diff] [blame] | 5152 | case Instruction::Add: |
| 5153 | isValid = isLeftShift; |
| 5154 | break; |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 5155 | case Instruction::Or: |
| 5156 | case Instruction::Xor: |
| 5157 | highBitSet = false; |
| 5158 | break; |
| 5159 | case Instruction::And: |
| 5160 | highBitSet = true; |
| 5161 | break; |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5162 | } |
| 5163 | |
| 5164 | // If this is a signed shift right, and the high bit is modified |
| 5165 | // by the logical operation, do not perform the transformation. |
| 5166 | // The highBitSet boolean indicates the value of the high bit of |
| 5167 | // the constant which would cause it to be modified for this |
| 5168 | // operation. |
| 5169 | // |
Chris Lattner | b330939 | 2006-01-06 07:22:22 +0000 | [diff] [blame] | 5170 | if (isValid && !isLeftShift && isSignedShift) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5171 | uint64_t Val = Op0C->getZExtValue(); |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5172 | isValid = ((Val & (1 << (TypeBits-1))) != 0) == highBitSet; |
| 5173 | } |
| 5174 | |
| 5175 | if (isValid) { |
| 5176 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1); |
| 5177 | |
| 5178 | Instruction *NewShift = |
| 5179 | new ShiftInst(I.getOpcode(), Op0BO->getOperand(0), Op1, |
| 5180 | Op0BO->getName()); |
| 5181 | Op0BO->setName(""); |
| 5182 | InsertNewInstBefore(NewShift, I); |
| 5183 | |
| 5184 | return BinaryOperator::create(Op0BO->getOpcode(), NewShift, |
| 5185 | NewRHS); |
| 5186 | } |
| 5187 | } |
| 5188 | } |
| 5189 | } |
| 5190 | |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5191 | // Find out if this is a shift of a shift by a constant. |
| 5192 | ShiftInst *ShiftOp = 0; |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5193 | if (ShiftInst *Op0SI = dyn_cast<ShiftInst>(Op0)) |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5194 | ShiftOp = Op0SI; |
| 5195 | else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
| 5196 | // If this is a noop-integer case of a shift instruction, use the shift. |
| 5197 | if (CI->getOperand(0)->getType()->isInteger() && |
| 5198 | CI->getOperand(0)->getType()->getPrimitiveSizeInBits() == |
| 5199 | CI->getType()->getPrimitiveSizeInBits() && |
| 5200 | isa<ShiftInst>(CI->getOperand(0))) { |
| 5201 | ShiftOp = cast<ShiftInst>(CI->getOperand(0)); |
| 5202 | } |
| 5203 | } |
| 5204 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5205 | if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) { |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5206 | // Find the operands and properties of the input shift. Note that the |
| 5207 | // signedness of the input shift may differ from the current shift if there |
| 5208 | // is a noop cast between the two. |
| 5209 | bool isShiftOfLeftShift = ShiftOp->getOpcode() == Instruction::Shl; |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 5210 | bool isShiftOfSignedShift = isShiftOfLeftShift ? |
| 5211 | ShiftOp->getType()->isSigned() : |
| 5212 | ShiftOp->getOpcode() == Instruction::AShr; |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 5213 | bool isShiftOfUnsignedShift = !isShiftOfSignedShift; |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5214 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5215 | ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1)); |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5216 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5217 | unsigned ShiftAmt1 = (unsigned)ShiftAmt1C->getZExtValue(); |
| 5218 | unsigned ShiftAmt2 = (unsigned)Op1->getZExtValue(); |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5219 | |
| 5220 | // Check for (A << c1) << c2 and (A >> c1) >> c2. |
| 5221 | if (isLeftShift == isShiftOfLeftShift) { |
| 5222 | // Do not fold these shifts if the first one is signed and the second one |
| 5223 | // is unsigned and this is a right shift. Further, don't do any folding |
| 5224 | // on them. |
| 5225 | if (isShiftOfSignedShift && isUnsignedShift && !isLeftShift) |
| 5226 | return 0; |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5227 | |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5228 | unsigned Amt = ShiftAmt1+ShiftAmt2; // Fold into one big shift. |
| 5229 | if (Amt > Op0->getType()->getPrimitiveSizeInBits()) |
| 5230 | Amt = Op0->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5231 | |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5232 | Value *Op = ShiftOp->getOperand(0); |
| 5233 | if (isShiftOfSignedShift != isSignedShift) |
| 5234 | Op = InsertNewInstBefore(new CastInst(Op, I.getType(), "tmp"), I); |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 5235 | ShiftInst* ShiftResult = new ShiftInst(I.getOpcode(), Op, |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5236 | ConstantInt::get(Type::UByteTy, Amt)); |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 5237 | if (I.getType() == ShiftResult->getType()) |
| 5238 | return ShiftResult; |
| 5239 | InsertNewInstBefore(ShiftResult, I); |
| 5240 | return new CastInst(ShiftResult, I.getType()); |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5241 | } |
| 5242 | |
| 5243 | // Check for (A << c1) >> c2 or (A >> c1) << c2. If we are dealing with |
| 5244 | // signed types, we can only support the (A >> c1) << c2 configuration, |
| 5245 | // because it can not turn an arbitrary bit of A into a sign bit. |
| 5246 | if (isUnsignedShift || isLeftShift) { |
| 5247 | // Calculate bitmask for what gets shifted off the edge. |
| 5248 | Constant *C = ConstantIntegral::getAllOnesValue(I.getType()); |
| 5249 | if (isLeftShift) |
| 5250 | C = ConstantExpr::getShl(C, ShiftAmt1C); |
| 5251 | else |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 5252 | C = ConstantExpr::getLShr(C, ShiftAmt1C); |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5253 | |
| 5254 | Value *Op = ShiftOp->getOperand(0); |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 5255 | if (Op->getType() != C->getType()) |
Reid Spencer | 00c482b | 2006-10-26 19:19:06 +0000 | [diff] [blame] | 5256 | Op = InsertCastBefore(Op, I.getType(), I); |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5257 | |
| 5258 | Instruction *Mask = |
| 5259 | BinaryOperator::createAnd(Op, C, Op->getName()+".mask"); |
| 5260 | InsertNewInstBefore(Mask, I); |
| 5261 | |
| 5262 | // Figure out what flavor of shift we should use... |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 5263 | if (ShiftAmt1 == ShiftAmt2) { |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5264 | return ReplaceInstUsesWith(I, Mask); // (A << c) >> c === A & c2 |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 5265 | } else if (ShiftAmt1 < ShiftAmt2) { |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5266 | return new ShiftInst(I.getOpcode(), Mask, |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5267 | ConstantInt::get(Type::UByteTy, ShiftAmt2-ShiftAmt1)); |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 5268 | } else if (isShiftOfUnsignedShift || isShiftOfLeftShift) { |
| 5269 | if (isShiftOfUnsignedShift && !isShiftOfLeftShift && isSignedShift) { |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 5270 | return new ShiftInst(Instruction::LShr, Mask, |
| 5271 | ConstantInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2)); |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 5272 | } else { |
| 5273 | return new ShiftInst(ShiftOp->getOpcode(), Mask, |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5274 | ConstantInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2)); |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 5275 | } |
| 5276 | } else { |
| 5277 | // (X >>s C1) << C2 where C1 > C2 === (X >>s (C1-C2)) & mask |
Reid Spencer | 00c482b | 2006-10-26 19:19:06 +0000 | [diff] [blame] | 5278 | Op = InsertCastBefore(Mask, I.getType()->getSignedVersion(), I); |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 5279 | Instruction *Shift = |
| 5280 | new ShiftInst(ShiftOp->getOpcode(), Op, |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5281 | ConstantInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2)); |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 5282 | InsertNewInstBefore(Shift, I); |
| 5283 | |
| 5284 | C = ConstantIntegral::getAllOnesValue(Shift->getType()); |
| 5285 | C = ConstantExpr::getShl(C, Op1); |
| 5286 | Mask = BinaryOperator::createAnd(Shift, C, Op->getName()+".mask"); |
| 5287 | InsertNewInstBefore(Mask, I); |
| 5288 | return new CastInst(Mask, I.getType()); |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5289 | } |
| 5290 | } else { |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 5291 | // 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] | 5292 | // this case, C1 == C2 and C1 is 8, 16, or 32. |
| 5293 | if (ShiftAmt1 == ShiftAmt2) { |
| 5294 | const Type *SExtType = 0; |
Chris Lattner | 655d08f | 2006-04-28 22:21:41 +0000 | [diff] [blame] | 5295 | switch (Op0->getType()->getPrimitiveSizeInBits() - ShiftAmt1) { |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5296 | case 8 : SExtType = Type::SByteTy; break; |
| 5297 | case 16: SExtType = Type::ShortTy; break; |
| 5298 | case 32: SExtType = Type::IntTy; break; |
| 5299 | } |
| 5300 | |
| 5301 | if (SExtType) { |
| 5302 | Instruction *NewTrunc = new CastInst(ShiftOp->getOperand(0), |
| 5303 | SExtType, "sext"); |
| 5304 | InsertNewInstBefore(NewTrunc, I); |
| 5305 | return new CastInst(NewTrunc, I.getType()); |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 5306 | } |
Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5307 | } |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 5308 | } |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5309 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5310 | return 0; |
| 5311 | } |
| 5312 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 5313 | |
Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5314 | /// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear |
| 5315 | /// expression. If so, decompose it, returning some value X, such that Val is |
| 5316 | /// X*Scale+Offset. |
| 5317 | /// |
| 5318 | static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale, |
| 5319 | unsigned &Offset) { |
| 5320 | assert(Val->getType() == Type::UIntTy && "Unexpected allocation size type!"); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5321 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { |
| 5322 | if (CI->getType()->isUnsigned()) { |
| 5323 | Offset = CI->getZExtValue(); |
| 5324 | Scale = 1; |
| 5325 | return ConstantInt::get(Type::UIntTy, 0); |
| 5326 | } |
Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5327 | } else if (Instruction *I = dyn_cast<Instruction>(Val)) { |
| 5328 | if (I->getNumOperands() == 2) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5329 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 5330 | if (CUI->getType()->isUnsigned()) { |
| 5331 | if (I->getOpcode() == Instruction::Shl) { |
| 5332 | // This is a value scaled by '1 << the shift amt'. |
| 5333 | Scale = 1U << CUI->getZExtValue(); |
| 5334 | Offset = 0; |
| 5335 | return I->getOperand(0); |
| 5336 | } else if (I->getOpcode() == Instruction::Mul) { |
| 5337 | // This value is scaled by 'CUI'. |
| 5338 | Scale = CUI->getZExtValue(); |
| 5339 | Offset = 0; |
| 5340 | return I->getOperand(0); |
| 5341 | } else if (I->getOpcode() == Instruction::Add) { |
| 5342 | // We have X+C. Check to see if we really have (X*C2)+C1, |
| 5343 | // where C1 is divisible by C2. |
| 5344 | unsigned SubScale; |
| 5345 | Value *SubVal = |
| 5346 | DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset); |
| 5347 | Offset += CUI->getZExtValue(); |
| 5348 | if (SubScale > 1 && (Offset % SubScale == 0)) { |
| 5349 | Scale = SubScale; |
| 5350 | return SubVal; |
| 5351 | } |
Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5352 | } |
| 5353 | } |
| 5354 | } |
| 5355 | } |
| 5356 | } |
| 5357 | |
| 5358 | // Otherwise, we can't look past this. |
| 5359 | Scale = 1; |
| 5360 | Offset = 0; |
| 5361 | return Val; |
| 5362 | } |
| 5363 | |
| 5364 | |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5365 | /// PromoteCastOfAllocation - If we find a cast of an allocation instruction, |
| 5366 | /// try to eliminate the cast by moving the type information into the alloc. |
| 5367 | Instruction *InstCombiner::PromoteCastOfAllocation(CastInst &CI, |
| 5368 | AllocationInst &AI) { |
| 5369 | const PointerType *PTy = dyn_cast<PointerType>(CI.getType()); |
Chris Lattner | bb17180 | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 5370 | if (!PTy) return 0; // Not casting the allocation to a pointer type. |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5371 | |
Chris Lattner | ac87beb | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 5372 | // Remove any uses of AI that are dead. |
| 5373 | assert(!CI.use_empty() && "Dead instructions should be removed earlier!"); |
| 5374 | std::vector<Instruction*> DeadUsers; |
| 5375 | for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) { |
| 5376 | Instruction *User = cast<Instruction>(*UI++); |
| 5377 | if (isInstructionTriviallyDead(User)) { |
| 5378 | while (UI != E && *UI == User) |
| 5379 | ++UI; // If this instruction uses AI more than once, don't break UI. |
| 5380 | |
| 5381 | // Add operands to the worklist. |
| 5382 | AddUsesToWorkList(*User); |
| 5383 | ++NumDeadInst; |
| 5384 | DEBUG(std::cerr << "IC: DCE: " << *User); |
| 5385 | |
| 5386 | User->eraseFromParent(); |
| 5387 | removeFromWorkList(User); |
| 5388 | } |
| 5389 | } |
| 5390 | |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5391 | // Get the type really allocated and the type casted to. |
| 5392 | const Type *AllocElTy = AI.getAllocatedType(); |
| 5393 | const Type *CastElTy = PTy->getElementType(); |
| 5394 | if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0; |
Chris Lattner | 355ecc0 | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 5395 | |
Chris Lattner | 7d19067 | 2006-10-01 19:40:58 +0000 | [diff] [blame] | 5396 | unsigned AllocElTyAlign = TD->getTypeAlignment(AllocElTy); |
| 5397 | unsigned CastElTyAlign = TD->getTypeAlignment(CastElTy); |
Chris Lattner | 355ecc0 | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 5398 | if (CastElTyAlign < AllocElTyAlign) return 0; |
| 5399 | |
Chris Lattner | 46705b2 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 5400 | // If the allocation has multiple uses, only promote it if we are strictly |
| 5401 | // increasing the alignment of the resultant allocation. If we keep it the |
| 5402 | // same, we open the door to infinite loops of various kinds. |
| 5403 | if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0; |
| 5404 | |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5405 | uint64_t AllocElTySize = TD->getTypeSize(AllocElTy); |
| 5406 | uint64_t CastElTySize = TD->getTypeSize(CastElTy); |
Chris Lattner | bb17180 | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 5407 | if (CastElTySize == 0 || AllocElTySize == 0) return 0; |
Chris Lattner | 355ecc0 | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 5408 | |
Chris Lattner | 8270c33 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 5409 | // See if we can satisfy the modulus by pulling a scale out of the array |
| 5410 | // size argument. |
Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5411 | unsigned ArraySizeScale, ArrayOffset; |
| 5412 | Value *NumElements = // See if the array size is a decomposable linear expr. |
| 5413 | DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset); |
| 5414 | |
Chris Lattner | 8270c33 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 5415 | // If we can now satisfy the modulus, by using a non-1 scale, we really can |
| 5416 | // do the xform. |
Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5417 | if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 || |
| 5418 | (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0; |
Chris Lattner | b3ecf96 | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 5419 | |
Chris Lattner | 8270c33 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 5420 | unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize; |
| 5421 | Value *Amt = 0; |
| 5422 | if (Scale == 1) { |
| 5423 | Amt = NumElements; |
| 5424 | } else { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5425 | // If the allocation size is constant, form a constant mul expression |
| 5426 | Amt = ConstantInt::get(Type::UIntTy, Scale); |
| 5427 | if (isa<ConstantInt>(NumElements) && NumElements->getType()->isUnsigned()) |
| 5428 | Amt = ConstantExpr::getMul( |
| 5429 | cast<ConstantInt>(NumElements), cast<ConstantInt>(Amt)); |
| 5430 | // otherwise multiply the amount and the number of elements |
Chris Lattner | 8270c33 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 5431 | else if (Scale != 1) { |
| 5432 | Instruction *Tmp = BinaryOperator::createMul(Amt, NumElements, "tmp"); |
| 5433 | Amt = InsertNewInstBefore(Tmp, AI); |
Chris Lattner | b3ecf96 | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 5434 | } |
Chris Lattner | bb17180 | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 5435 | } |
| 5436 | |
Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5437 | if (unsigned Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5438 | Value *Off = ConstantInt::get(Type::UIntTy, Offset); |
Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5439 | Instruction *Tmp = BinaryOperator::createAdd(Amt, Off, "tmp"); |
| 5440 | Amt = InsertNewInstBefore(Tmp, AI); |
| 5441 | } |
| 5442 | |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5443 | std::string Name = AI.getName(); AI.setName(""); |
| 5444 | AllocationInst *New; |
| 5445 | if (isa<MallocInst>(AI)) |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 5446 | New = new MallocInst(CastElTy, Amt, AI.getAlignment(), Name); |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5447 | else |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 5448 | New = new AllocaInst(CastElTy, Amt, AI.getAlignment(), Name); |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5449 | InsertNewInstBefore(New, AI); |
Chris Lattner | 46705b2 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 5450 | |
| 5451 | // If the allocation has multiple uses, insert a cast and change all things |
| 5452 | // that used it to use the new cast. This will also hack on CI, but it will |
| 5453 | // die soon. |
| 5454 | if (!AI.hasOneUse()) { |
| 5455 | AddUsesToWorkList(AI); |
| 5456 | CastInst *NewCast = new CastInst(New, AI.getType(), "tmpcast"); |
| 5457 | InsertNewInstBefore(NewCast, AI); |
| 5458 | AI.replaceAllUsesWith(NewCast); |
| 5459 | } |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5460 | return ReplaceInstUsesWith(CI, New); |
| 5461 | } |
| 5462 | |
Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5463 | /// CanEvaluateInDifferentType - Return true if we can take the specified value |
| 5464 | /// and return it without inserting any new casts. This is used by code that |
| 5465 | /// tries to decide whether promoting or shrinking integer operations to wider |
| 5466 | /// or smaller types will allow us to eliminate a truncate or extend. |
| 5467 | static bool CanEvaluateInDifferentType(Value *V, const Type *Ty, |
| 5468 | int &NumCastsRemoved) { |
| 5469 | if (isa<Constant>(V)) return true; |
| 5470 | |
| 5471 | Instruction *I = dyn_cast<Instruction>(V); |
| 5472 | if (!I || !I->hasOneUse()) return false; |
| 5473 | |
| 5474 | switch (I->getOpcode()) { |
| 5475 | case Instruction::And: |
| 5476 | case Instruction::Or: |
| 5477 | case Instruction::Xor: |
| 5478 | // These operators can all arbitrarily be extended or truncated. |
| 5479 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, NumCastsRemoved) && |
| 5480 | CanEvaluateInDifferentType(I->getOperand(1), Ty, NumCastsRemoved); |
| 5481 | case Instruction::Cast: |
| 5482 | // If this is a cast from the destination type, we can trivially eliminate |
| 5483 | // it, and this will remove a cast overall. |
| 5484 | if (I->getOperand(0)->getType() == Ty) { |
Chris Lattner | 3fda386 | 2006-06-28 17:34:50 +0000 | [diff] [blame] | 5485 | // If the first operand is itself a cast, and is eliminable, do not count |
| 5486 | // this as an eliminable cast. We would prefer to eliminate those two |
| 5487 | // casts first. |
Reid Spencer | de46e48 | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 5488 | if (isa<CastInst>(I->getOperand(0))) |
Chris Lattner | 3fda386 | 2006-06-28 17:34:50 +0000 | [diff] [blame] | 5489 | return true; |
| 5490 | |
Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5491 | ++NumCastsRemoved; |
| 5492 | return true; |
| 5493 | } |
| 5494 | // TODO: Can handle more cases here. |
| 5495 | break; |
| 5496 | } |
| 5497 | |
| 5498 | return false; |
| 5499 | } |
| 5500 | |
| 5501 | /// EvaluateInDifferentType - Given an expression that |
| 5502 | /// CanEvaluateInDifferentType returns true for, actually insert the code to |
| 5503 | /// evaluate the expression. |
| 5504 | Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty) { |
| 5505 | if (Constant *C = dyn_cast<Constant>(V)) |
| 5506 | return ConstantExpr::getCast(C, Ty); |
| 5507 | |
| 5508 | // Otherwise, it must be an instruction. |
| 5509 | Instruction *I = cast<Instruction>(V); |
Chris Lattner | d0622b6 | 2006-05-20 23:14:03 +0000 | [diff] [blame] | 5510 | Instruction *Res = 0; |
Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5511 | switch (I->getOpcode()) { |
| 5512 | case Instruction::And: |
| 5513 | case Instruction::Or: |
| 5514 | case Instruction::Xor: { |
| 5515 | Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty); |
| 5516 | Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty); |
| 5517 | Res = BinaryOperator::create((Instruction::BinaryOps)I->getOpcode(), |
| 5518 | LHS, RHS, I->getName()); |
| 5519 | break; |
| 5520 | } |
| 5521 | case Instruction::Cast: |
| 5522 | // If this is a cast from the destination type, return the input. |
| 5523 | if (I->getOperand(0)->getType() == Ty) |
| 5524 | return I->getOperand(0); |
| 5525 | |
| 5526 | // TODO: Can handle more cases here. |
| 5527 | assert(0 && "Unreachable!"); |
| 5528 | break; |
| 5529 | } |
| 5530 | |
| 5531 | return InsertNewInstBefore(Res, *I); |
| 5532 | } |
| 5533 | |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5534 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 5535 | // CastInst simplification |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 5536 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5537 | Instruction *InstCombiner::visitCastInst(CastInst &CI) { |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 5538 | Value *Src = CI.getOperand(0); |
| 5539 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 5540 | // If the user is casting a value to the same type, eliminate this cast |
| 5541 | // instruction... |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 5542 | if (CI.getType() == Src->getType()) |
| 5543 | return ReplaceInstUsesWith(CI, Src); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 5544 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5545 | if (isa<UndefValue>(Src)) // cast undef -> undef |
| 5546 | return ReplaceInstUsesWith(CI, UndefValue::get(CI.getType())); |
| 5547 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 5548 | // If casting the result of another cast instruction, try to eliminate this |
| 5549 | // one! |
| 5550 | // |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 5551 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
| 5552 | Value *A = CSrc->getOperand(0); |
| 5553 | if (isEliminableCastOfCast(A->getType(), CSrc->getType(), |
| 5554 | CI.getType(), TD)) { |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 5555 | // This instruction now refers directly to the cast's src operand. This |
| 5556 | // has a good chance of making CSrc dead. |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5557 | CI.setOperand(0, CSrc->getOperand(0)); |
| 5558 | return &CI; |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 5559 | } |
| 5560 | |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 5561 | // If this is an A->B->A cast, and we are dealing with integral types, try |
| 5562 | // to convert this into a logical 'and' instruction. |
| 5563 | // |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5564 | if (A->getType()->isInteger() && |
Chris Lattner | b0b412e | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 5565 | CI.getType()->isInteger() && CSrc->getType()->isInteger() && |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 5566 | CSrc->getType()->isUnsigned() && // B->A cast must zero extend |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5567 | CSrc->getType()->getPrimitiveSizeInBits() < |
| 5568 | CI.getType()->getPrimitiveSizeInBits()&& |
| 5569 | A->getType()->getPrimitiveSizeInBits() == |
| 5570 | CI.getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 5571 | assert(CSrc->getType() != Type::ULongTy && |
| 5572 | "Cannot have type bigger than ulong!"); |
Chris Lattner | 77defba | 2006-02-07 07:00:41 +0000 | [diff] [blame] | 5573 | uint64_t AndValue = CSrc->getType()->getIntegralTypeMask(); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5574 | Constant *AndOp = ConstantInt::get(A->getType()->getUnsignedVersion(), |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 5575 | AndValue); |
| 5576 | AndOp = ConstantExpr::getCast(AndOp, A->getType()); |
| 5577 | Instruction *And = BinaryOperator::createAnd(CSrc->getOperand(0), AndOp); |
| 5578 | if (And->getType() != CI.getType()) { |
| 5579 | And->setName(CSrc->getName()+".mask"); |
| 5580 | InsertNewInstBefore(And, CI); |
| 5581 | And = new CastInst(And, CI.getType()); |
| 5582 | } |
| 5583 | return And; |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 5584 | } |
| 5585 | } |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 5586 | |
Chris Lattner | 0384165 | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 5587 | // If this is a cast to bool, turn it into the appropriate setne instruction. |
| 5588 | if (CI.getType() == Type::BoolTy) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 5589 | return BinaryOperator::createSetNE(CI.getOperand(0), |
Chris Lattner | 0384165 | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 5590 | Constant::getNullValue(CI.getOperand(0)->getType())); |
| 5591 | |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 5592 | // See if we can simplify any instructions used by the LHS whose sole |
| 5593 | // purpose is to compute bits we don't care about. |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 5594 | if (CI.getType()->isInteger() && CI.getOperand(0)->getType()->isIntegral()) { |
| 5595 | uint64_t KnownZero, KnownOne; |
| 5596 | if (SimplifyDemandedBits(&CI, CI.getType()->getIntegralTypeMask(), |
| 5597 | KnownZero, KnownOne)) |
| 5598 | return &CI; |
| 5599 | } |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 5600 | |
Chris Lattner | d0d5160 | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 5601 | // If casting the result of a getelementptr instruction with no offset, turn |
| 5602 | // this into a cast of the original pointer! |
| 5603 | // |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 5604 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
Chris Lattner | d0d5160 | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 5605 | bool AllZeroOperands = true; |
| 5606 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) |
| 5607 | if (!isa<Constant>(GEP->getOperand(i)) || |
| 5608 | !cast<Constant>(GEP->getOperand(i))->isNullValue()) { |
| 5609 | AllZeroOperands = false; |
| 5610 | break; |
| 5611 | } |
| 5612 | if (AllZeroOperands) { |
| 5613 | CI.setOperand(0, GEP->getOperand(0)); |
| 5614 | return &CI; |
| 5615 | } |
| 5616 | } |
| 5617 | |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 5618 | // If we are casting a malloc or alloca to a pointer to a type of the same |
| 5619 | // size, rewrite the allocation instruction to allocate the "right" type. |
| 5620 | // |
| 5621 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5622 | if (Instruction *V = PromoteCastOfAllocation(CI, *AI)) |
| 5623 | return V; |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 5624 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 5625 | if (SelectInst *SI = dyn_cast<SelectInst>(Src)) |
| 5626 | if (Instruction *NV = FoldOpIntoSelect(CI, SI, this)) |
| 5627 | return NV; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 5628 | if (isa<PHINode>(Src)) |
| 5629 | if (Instruction *NV = FoldOpIntoPhi(CI)) |
| 5630 | return NV; |
Chris Lattner | b19a5c6 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 5631 | |
| 5632 | // If the source and destination are pointers, and this cast is equivalent to |
| 5633 | // a getelementptr X, 0, 0, 0... turn it into the appropriate getelementptr. |
| 5634 | // This can enhance SROA and other transforms that want type-safe pointers. |
| 5635 | if (const PointerType *DstPTy = dyn_cast<PointerType>(CI.getType())) |
| 5636 | if (const PointerType *SrcPTy = dyn_cast<PointerType>(Src->getType())) { |
| 5637 | const Type *DstTy = DstPTy->getElementType(); |
| 5638 | const Type *SrcTy = SrcPTy->getElementType(); |
| 5639 | |
| 5640 | Constant *ZeroUInt = Constant::getNullValue(Type::UIntTy); |
| 5641 | unsigned NumZeros = 0; |
| 5642 | while (SrcTy != DstTy && |
Chris Lattner | d286270 | 2006-09-11 21:43:16 +0000 | [diff] [blame] | 5643 | isa<CompositeType>(SrcTy) && !isa<PointerType>(SrcTy) && |
| 5644 | SrcTy->getNumContainedTypes() /* not "{}" */) { |
Chris Lattner | b19a5c6 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 5645 | SrcTy = cast<CompositeType>(SrcTy)->getTypeAtIndex(ZeroUInt); |
| 5646 | ++NumZeros; |
| 5647 | } |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 5648 | |
Chris Lattner | b19a5c6 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 5649 | // If we found a path from the src to dest, create the getelementptr now. |
| 5650 | if (SrcTy == DstTy) { |
| 5651 | std::vector<Value*> Idxs(NumZeros+1, ZeroUInt); |
| 5652 | return new GetElementPtrInst(Src, Idxs); |
| 5653 | } |
| 5654 | } |
| 5655 | |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 5656 | // If the source value is an instruction with only this use, we can attempt to |
| 5657 | // propagate the cast into the instruction. Also, only handle integral types |
| 5658 | // for now. |
Chris Lattner | 99155be | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 5659 | if (Instruction *SrcI = dyn_cast<Instruction>(Src)) { |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 5660 | if (SrcI->hasOneUse() && Src->getType()->isIntegral() && |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 5661 | CI.getType()->isInteger()) { // Don't mess with casts to bool here |
Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5662 | |
| 5663 | int NumCastsRemoved = 0; |
| 5664 | if (CanEvaluateInDifferentType(SrcI, CI.getType(), NumCastsRemoved)) { |
| 5665 | // If this cast is a truncate, evaluting in a different type always |
| 5666 | // eliminates the cast, so it is always a win. If this is a noop-cast |
| 5667 | // this just removes a noop cast which isn't pointful, but simplifies |
| 5668 | // the code. If this is a zero-extension, we need to do an AND to |
| 5669 | // maintain the clear top-part of the computation, so we require that |
| 5670 | // the input have eliminated at least one cast. If this is a sign |
| 5671 | // extension, we insert two new casts (to do the extension) so we |
| 5672 | // require that two casts have been eliminated. |
| 5673 | bool DoXForm; |
| 5674 | switch (getCastType(Src->getType(), CI.getType())) { |
| 5675 | default: assert(0 && "Unknown cast type!"); |
| 5676 | case Noop: |
| 5677 | case Truncate: |
| 5678 | DoXForm = true; |
| 5679 | break; |
| 5680 | case Zeroext: |
| 5681 | DoXForm = NumCastsRemoved >= 1; |
| 5682 | break; |
| 5683 | case Signext: |
| 5684 | DoXForm = NumCastsRemoved >= 2; |
| 5685 | break; |
| 5686 | } |
| 5687 | |
| 5688 | if (DoXForm) { |
| 5689 | Value *Res = EvaluateInDifferentType(SrcI, CI.getType()); |
| 5690 | assert(Res->getType() == CI.getType()); |
| 5691 | switch (getCastType(Src->getType(), CI.getType())) { |
| 5692 | default: assert(0 && "Unknown cast type!"); |
| 5693 | case Noop: |
| 5694 | case Truncate: |
| 5695 | // Just replace this cast with the result. |
| 5696 | return ReplaceInstUsesWith(CI, Res); |
| 5697 | case Zeroext: { |
| 5698 | // We need to emit an AND to clear the high bits. |
| 5699 | unsigned SrcBitSize = Src->getType()->getPrimitiveSizeInBits(); |
| 5700 | unsigned DestBitSize = CI.getType()->getPrimitiveSizeInBits(); |
| 5701 | assert(SrcBitSize < DestBitSize && "Not a zext?"); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5702 | Constant *C = |
| 5703 | ConstantInt::get(Type::ULongTy, (1ULL << SrcBitSize)-1); |
Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5704 | C = ConstantExpr::getCast(C, CI.getType()); |
| 5705 | return BinaryOperator::createAnd(Res, C); |
| 5706 | } |
| 5707 | case Signext: |
| 5708 | // We need to emit a cast to truncate, then a cast to sext. |
| 5709 | return new CastInst(InsertCastBefore(Res, Src->getType(), CI), |
| 5710 | CI.getType()); |
| 5711 | } |
| 5712 | } |
| 5713 | } |
| 5714 | |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 5715 | const Type *DestTy = CI.getType(); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5716 | unsigned SrcBitSize = Src->getType()->getPrimitiveSizeInBits(); |
| 5717 | unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 5718 | |
| 5719 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; |
| 5720 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; |
| 5721 | |
| 5722 | switch (SrcI->getOpcode()) { |
| 5723 | case Instruction::Add: |
| 5724 | case Instruction::Mul: |
| 5725 | case Instruction::And: |
| 5726 | case Instruction::Or: |
| 5727 | case Instruction::Xor: |
| 5728 | // If we are discarding information, or just changing the sign, rewrite. |
| 5729 | if (DestBitSize <= SrcBitSize && DestBitSize != 1) { |
| 5730 | // Don't insert two casts if they cannot be eliminated. We allow two |
| 5731 | // casts to be inserted if the sizes are the same. This could only be |
| 5732 | // converting signedness, which is a noop. |
Chris Lattner | 11ffd59 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 5733 | if (DestBitSize == SrcBitSize || !ValueRequiresCast(Op1, DestTy,TD) || |
| 5734 | !ValueRequiresCast(Op0, DestTy, TD)) { |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 5735 | Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI); |
| 5736 | Value *Op1c = InsertOperandCastBefore(Op1, DestTy, SrcI); |
| 5737 | return BinaryOperator::create(cast<BinaryOperator>(SrcI) |
| 5738 | ->getOpcode(), Op0c, Op1c); |
| 5739 | } |
| 5740 | } |
Chris Lattner | 7208616 | 2005-05-06 02:07:39 +0000 | [diff] [blame] | 5741 | |
| 5742 | // cast (xor bool X, true) to int --> xor (cast bool X to int), 1 |
| 5743 | if (SrcBitSize == 1 && SrcI->getOpcode() == Instruction::Xor && |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 5744 | Op1 == ConstantBool::getTrue() && |
Chris Lattner | 7208616 | 2005-05-06 02:07:39 +0000 | [diff] [blame] | 5745 | (!Op0->hasOneUse() || !isa<SetCondInst>(Op0))) { |
| 5746 | Value *New = InsertOperandCastBefore(Op0, DestTy, &CI); |
| 5747 | return BinaryOperator::createXor(New, |
| 5748 | ConstantInt::get(CI.getType(), 1)); |
| 5749 | } |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 5750 | break; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 5751 | case Instruction::SDiv: |
| 5752 | case Instruction::UDiv: |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 5753 | case Instruction::SRem: |
| 5754 | case Instruction::URem: |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 5755 | // If we are just changing the sign, rewrite. |
| 5756 | if (DestBitSize == SrcBitSize) { |
| 5757 | // Don't insert two casts if they cannot be eliminated. We allow two |
| 5758 | // casts to be inserted if the sizes are the same. This could only be |
| 5759 | // converting signedness, which is a noop. |
| 5760 | if (!ValueRequiresCast(Op1, DestTy,TD) || |
| 5761 | !ValueRequiresCast(Op0, DestTy, TD)) { |
| 5762 | Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI); |
| 5763 | Value *Op1c = InsertOperandCastBefore(Op1, DestTy, SrcI); |
| 5764 | return BinaryOperator::create( |
| 5765 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
| 5766 | } |
| 5767 | } |
| 5768 | break; |
| 5769 | |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 5770 | case Instruction::Shl: |
| 5771 | // Allow changing the sign of the source operand. Do not allow changing |
| 5772 | // the size of the shift, UNLESS the shift amount is a constant. We |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 5773 | // must not change variable sized shifts to a smaller size, because it |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 5774 | // is undefined to shift more bits out than exist in the value. |
| 5775 | if (DestBitSize == SrcBitSize || |
| 5776 | (DestBitSize < SrcBitSize && isa<Constant>(Op1))) { |
| 5777 | Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI); |
| 5778 | return new ShiftInst(Instruction::Shl, Op0c, Op1); |
| 5779 | } |
| 5780 | break; |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 5781 | case Instruction::AShr: |
Chris Lattner | 8738041 | 2005-05-06 04:18:52 +0000 | [diff] [blame] | 5782 | // If this is a signed shr, and if all bits shifted in are about to be |
| 5783 | // truncated off, turn it into an unsigned shr to allow greater |
| 5784 | // simplifications. |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 5785 | if (DestBitSize < SrcBitSize && |
Chris Lattner | 8738041 | 2005-05-06 04:18:52 +0000 | [diff] [blame] | 5786 | isa<ConstantInt>(Op1)) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5787 | unsigned ShiftAmt = cast<ConstantInt>(Op1)->getZExtValue(); |
Chris Lattner | 8738041 | 2005-05-06 04:18:52 +0000 | [diff] [blame] | 5788 | if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) { |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 5789 | // Insert the new logical shift right. |
| 5790 | return new ShiftInst(Instruction::LShr, Op0, Op1); |
Chris Lattner | 8738041 | 2005-05-06 04:18:52 +0000 | [diff] [blame] | 5791 | } |
| 5792 | } |
| 5793 | break; |
| 5794 | |
Chris Lattner | c7bfed0 | 2006-02-27 02:38:23 +0000 | [diff] [blame] | 5795 | case Instruction::SetEQ: |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 5796 | case Instruction::SetNE: |
Chris Lattner | c7bfed0 | 2006-02-27 02:38:23 +0000 | [diff] [blame] | 5797 | // We if we are just checking for a seteq of a single bit and casting it |
| 5798 | // to an integer. If so, shift the bit to the appropriate place then |
| 5799 | // cast to integer to avoid the comparison. |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 5800 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | c7bfed0 | 2006-02-27 02:38:23 +0000 | [diff] [blame] | 5801 | uint64_t Op1CV = Op1C->getZExtValue(); |
| 5802 | // cast (X == 0) to int --> X^1 iff X has only the low bit set. |
| 5803 | // cast (X == 0) to int --> (X>>1)^1 iff X has only the 2nd bit set. |
| 5804 | // cast (X == 1) to int --> X iff X has only the low bit set. |
| 5805 | // cast (X == 2) to int --> X>>1 iff X has only the 2nd bit set. |
| 5806 | // cast (X != 0) to int --> X iff X has only the low bit set. |
| 5807 | // cast (X != 0) to int --> X>>1 iff X has only the 2nd bit set. |
| 5808 | // cast (X != 1) to int --> X^1 iff X has only the low bit set. |
| 5809 | // cast (X != 2) to int --> (X>>1)^1 iff X has only the 2nd bit set. |
| 5810 | if (Op1CV == 0 || isPowerOf2_64(Op1CV)) { |
| 5811 | // If Op1C some other power of two, convert: |
| 5812 | uint64_t KnownZero, KnownOne; |
| 5813 | uint64_t TypeMask = Op1->getType()->getIntegralTypeMask(); |
| 5814 | ComputeMaskedBits(Op0, TypeMask, KnownZero, KnownOne); |
| 5815 | |
| 5816 | if (isPowerOf2_64(KnownZero^TypeMask)) { // Exactly one possible 1? |
| 5817 | bool isSetNE = SrcI->getOpcode() == Instruction::SetNE; |
| 5818 | if (Op1CV && (Op1CV != (KnownZero^TypeMask))) { |
| 5819 | // (X&4) == 2 --> false |
| 5820 | // (X&4) != 2 --> true |
Chris Lattner | c5b6c9a | 2006-02-28 19:47:20 +0000 | [diff] [blame] | 5821 | Constant *Res = ConstantBool::get(isSetNE); |
| 5822 | Res = ConstantExpr::getCast(Res, CI.getType()); |
| 5823 | return ReplaceInstUsesWith(CI, Res); |
Chris Lattner | c7bfed0 | 2006-02-27 02:38:23 +0000 | [diff] [blame] | 5824 | } |
| 5825 | |
| 5826 | unsigned ShiftAmt = Log2_64(KnownZero^TypeMask); |
| 5827 | Value *In = Op0; |
| 5828 | if (ShiftAmt) { |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 5829 | // Perform a logical shr by shiftamt. |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 5830 | // Insert the shift to put the result in the low bit. |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 5831 | In = InsertNewInstBefore(new ShiftInst(Instruction::LShr, In, |
Chris Lattner | c7bfed0 | 2006-02-27 02:38:23 +0000 | [diff] [blame] | 5832 | ConstantInt::get(Type::UByteTy, ShiftAmt), |
| 5833 | In->getName()+".lobit"), CI); |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 5834 | } |
Chris Lattner | c7bfed0 | 2006-02-27 02:38:23 +0000 | [diff] [blame] | 5835 | |
| 5836 | if ((Op1CV != 0) == isSetNE) { // Toggle the low bit. |
| 5837 | Constant *One = ConstantInt::get(In->getType(), 1); |
| 5838 | In = BinaryOperator::createXor(In, One, "tmp"); |
| 5839 | InsertNewInstBefore(cast<Instruction>(In), CI); |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 5840 | } |
Chris Lattner | c7bfed0 | 2006-02-27 02:38:23 +0000 | [diff] [blame] | 5841 | |
| 5842 | if (CI.getType() == In->getType()) |
| 5843 | return ReplaceInstUsesWith(CI, In); |
| 5844 | else |
| 5845 | return new CastInst(In, CI.getType()); |
Chris Lattner | 4c2d378 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 5846 | } |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 5847 | } |
| 5848 | } |
| 5849 | break; |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 5850 | } |
| 5851 | } |
Chris Lattner | 99155be | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 5852 | |
| 5853 | if (SrcI->hasOneUse()) { |
| 5854 | if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(SrcI)) { |
| 5855 | // Okay, we have (cast (shuffle ..)). We know this cast is a bitconvert |
| 5856 | // because the inputs are known to be a vector. Check to see if this is |
| 5857 | // a cast to a vector with the same # elts. |
| 5858 | if (isa<PackedType>(CI.getType()) && |
| 5859 | cast<PackedType>(CI.getType())->getNumElements() == |
| 5860 | SVI->getType()->getNumElements()) { |
| 5861 | CastInst *Tmp; |
| 5862 | // If either of the operands is a cast from CI.getType(), then |
| 5863 | // evaluating the shuffle in the casted destination's type will allow |
| 5864 | // us to eliminate at least one cast. |
| 5865 | if (((Tmp = dyn_cast<CastInst>(SVI->getOperand(0))) && |
| 5866 | Tmp->getOperand(0)->getType() == CI.getType()) || |
| 5867 | ((Tmp = dyn_cast<CastInst>(SVI->getOperand(1))) && |
Chris Lattner | 95cebb0 | 2006-06-06 22:26:02 +0000 | [diff] [blame] | 5868 | Tmp->getOperand(0)->getType() == CI.getType())) { |
Chris Lattner | 99155be | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 5869 | Value *LHS = InsertOperandCastBefore(SVI->getOperand(0), |
| 5870 | CI.getType(), &CI); |
| 5871 | Value *RHS = InsertOperandCastBefore(SVI->getOperand(1), |
| 5872 | CI.getType(), &CI); |
| 5873 | // Return a new shuffle vector. Use the same element ID's, as we |
| 5874 | // know the vector types match #elts. |
| 5875 | return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2)); |
| 5876 | } |
| 5877 | } |
| 5878 | } |
| 5879 | } |
| 5880 | } |
Chris Lattner | bb17180 | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 5881 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 5882 | return 0; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 5883 | } |
| 5884 | |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 5885 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
| 5886 | /// %C = or %A, %B |
| 5887 | /// %D = select %cond, %C, %A |
| 5888 | /// into: |
| 5889 | /// %C = select %cond, %B, 0 |
| 5890 | /// %D = or %A, %C |
| 5891 | /// |
| 5892 | /// Assuming that the specified instruction is an operand to the select, return |
| 5893 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 5894 | /// equal the other incoming value of the select. |
| 5895 | /// |
| 5896 | static unsigned GetSelectFoldableOperands(Instruction *I) { |
| 5897 | switch (I->getOpcode()) { |
| 5898 | case Instruction::Add: |
| 5899 | case Instruction::Mul: |
| 5900 | case Instruction::And: |
| 5901 | case Instruction::Or: |
| 5902 | case Instruction::Xor: |
| 5903 | return 3; // Can fold through either operand. |
| 5904 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 5905 | case Instruction::Shl: // Can only fold on the shift amount. |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 5906 | case Instruction::LShr: |
| 5907 | case Instruction::AShr: |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5908 | return 1; |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 5909 | default: |
| 5910 | return 0; // Cannot fold |
| 5911 | } |
| 5912 | } |
| 5913 | |
| 5914 | /// GetSelectFoldableConstant - For the same transformation as the previous |
| 5915 | /// function, return the identity constant that goes into the select. |
| 5916 | static Constant *GetSelectFoldableConstant(Instruction *I) { |
| 5917 | switch (I->getOpcode()) { |
| 5918 | default: assert(0 && "This cannot happen!"); abort(); |
| 5919 | case Instruction::Add: |
| 5920 | case Instruction::Sub: |
| 5921 | case Instruction::Or: |
| 5922 | case Instruction::Xor: |
| 5923 | return Constant::getNullValue(I->getType()); |
| 5924 | case Instruction::Shl: |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 5925 | case Instruction::LShr: |
| 5926 | case Instruction::AShr: |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 5927 | return Constant::getNullValue(Type::UByteTy); |
| 5928 | case Instruction::And: |
| 5929 | return ConstantInt::getAllOnesValue(I->getType()); |
| 5930 | case Instruction::Mul: |
| 5931 | return ConstantInt::get(I->getType(), 1); |
| 5932 | } |
| 5933 | } |
| 5934 | |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 5935 | /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI |
| 5936 | /// have the same opcode and only one use each. Try to simplify this. |
| 5937 | Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 5938 | Instruction *FI) { |
| 5939 | if (TI->getNumOperands() == 1) { |
| 5940 | // If this is a non-volatile load or a cast from the same type, |
| 5941 | // merge. |
| 5942 | if (TI->getOpcode() == Instruction::Cast) { |
| 5943 | if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType()) |
| 5944 | return 0; |
| 5945 | } else { |
| 5946 | return 0; // unknown unary op. |
| 5947 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5948 | |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 5949 | // Fold this by inserting a select from the input values. |
| 5950 | SelectInst *NewSI = new SelectInst(SI.getCondition(), TI->getOperand(0), |
| 5951 | FI->getOperand(0), SI.getName()+".v"); |
| 5952 | InsertNewInstBefore(NewSI, SI); |
| 5953 | return new CastInst(NewSI, TI->getType()); |
| 5954 | } |
| 5955 | |
| 5956 | // Only handle binary operators here. |
| 5957 | if (!isa<ShiftInst>(TI) && !isa<BinaryOperator>(TI)) |
| 5958 | return 0; |
| 5959 | |
| 5960 | // Figure out if the operations have any operands in common. |
| 5961 | Value *MatchOp, *OtherOpT, *OtherOpF; |
| 5962 | bool MatchIsOpZero; |
| 5963 | if (TI->getOperand(0) == FI->getOperand(0)) { |
| 5964 | MatchOp = TI->getOperand(0); |
| 5965 | OtherOpT = TI->getOperand(1); |
| 5966 | OtherOpF = FI->getOperand(1); |
| 5967 | MatchIsOpZero = true; |
| 5968 | } else if (TI->getOperand(1) == FI->getOperand(1)) { |
| 5969 | MatchOp = TI->getOperand(1); |
| 5970 | OtherOpT = TI->getOperand(0); |
| 5971 | OtherOpF = FI->getOperand(0); |
| 5972 | MatchIsOpZero = false; |
| 5973 | } else if (!TI->isCommutative()) { |
| 5974 | return 0; |
| 5975 | } else if (TI->getOperand(0) == FI->getOperand(1)) { |
| 5976 | MatchOp = TI->getOperand(0); |
| 5977 | OtherOpT = TI->getOperand(1); |
| 5978 | OtherOpF = FI->getOperand(0); |
| 5979 | MatchIsOpZero = true; |
| 5980 | } else if (TI->getOperand(1) == FI->getOperand(0)) { |
| 5981 | MatchOp = TI->getOperand(1); |
| 5982 | OtherOpT = TI->getOperand(0); |
| 5983 | OtherOpF = FI->getOperand(1); |
| 5984 | MatchIsOpZero = true; |
| 5985 | } else { |
| 5986 | return 0; |
| 5987 | } |
| 5988 | |
| 5989 | // If we reach here, they do have operations in common. |
| 5990 | SelectInst *NewSI = new SelectInst(SI.getCondition(), OtherOpT, |
| 5991 | OtherOpF, SI.getName()+".v"); |
| 5992 | InsertNewInstBefore(NewSI, SI); |
| 5993 | |
| 5994 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) { |
| 5995 | if (MatchIsOpZero) |
| 5996 | return BinaryOperator::create(BO->getOpcode(), MatchOp, NewSI); |
| 5997 | else |
| 5998 | return BinaryOperator::create(BO->getOpcode(), NewSI, MatchOp); |
| 5999 | } else { |
| 6000 | if (MatchIsOpZero) |
| 6001 | return new ShiftInst(cast<ShiftInst>(TI)->getOpcode(), MatchOp, NewSI); |
| 6002 | else |
| 6003 | return new ShiftInst(cast<ShiftInst>(TI)->getOpcode(), NewSI, MatchOp); |
| 6004 | } |
| 6005 | } |
| 6006 | |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 6007 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 6008 | Value *CondVal = SI.getCondition(); |
| 6009 | Value *TrueVal = SI.getTrueValue(); |
| 6010 | Value *FalseVal = SI.getFalseValue(); |
| 6011 | |
| 6012 | // select true, X, Y -> X |
| 6013 | // select false, X, Y -> Y |
| 6014 | if (ConstantBool *C = dyn_cast<ConstantBool>(CondVal)) |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 6015 | return ReplaceInstUsesWith(SI, C->getValue() ? TrueVal : FalseVal); |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 6016 | |
| 6017 | // select C, X, X -> X |
| 6018 | if (TrueVal == FalseVal) |
| 6019 | return ReplaceInstUsesWith(SI, TrueVal); |
| 6020 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6021 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 6022 | return ReplaceInstUsesWith(SI, FalseVal); |
| 6023 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 6024 | return ReplaceInstUsesWith(SI, TrueVal); |
| 6025 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 6026 | if (isa<Constant>(TrueVal)) |
| 6027 | return ReplaceInstUsesWith(SI, TrueVal); |
| 6028 | else |
| 6029 | return ReplaceInstUsesWith(SI, FalseVal); |
| 6030 | } |
| 6031 | |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6032 | if (SI.getType() == Type::BoolTy) |
| 6033 | if (ConstantBool *C = dyn_cast<ConstantBool>(TrueVal)) { |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 6034 | if (C->getValue()) { |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6035 | // Change: A = select B, true, C --> A = or B, C |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 6036 | return BinaryOperator::createOr(CondVal, FalseVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6037 | } else { |
| 6038 | // Change: A = select B, false, C --> A = and !B, C |
| 6039 | Value *NotCond = |
| 6040 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 6041 | "not."+CondVal->getName()), SI); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 6042 | return BinaryOperator::createAnd(NotCond, FalseVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6043 | } |
| 6044 | } else if (ConstantBool *C = dyn_cast<ConstantBool>(FalseVal)) { |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 6045 | if (C->getValue() == false) { |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6046 | // Change: A = select B, C, false --> A = and B, C |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 6047 | return BinaryOperator::createAnd(CondVal, TrueVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6048 | } else { |
| 6049 | // Change: A = select B, C, true --> A = or !B, C |
| 6050 | Value *NotCond = |
| 6051 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 6052 | "not."+CondVal->getName()), SI); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 6053 | return BinaryOperator::createOr(NotCond, TrueVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6054 | } |
| 6055 | } |
| 6056 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6057 | // Selecting between two integer constants? |
| 6058 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) |
| 6059 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { |
| 6060 | // select C, 1, 0 -> cast C to int |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6061 | if (FalseValC->isNullValue() && TrueValC->getZExtValue() == 1) { |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6062 | return new CastInst(CondVal, SI.getType()); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6063 | } else if (TrueValC->isNullValue() && FalseValC->getZExtValue() == 1) { |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6064 | // select C, 0, 1 -> cast !C to int |
| 6065 | Value *NotCond = |
| 6066 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
Chris Lattner | cf7baf3 | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 6067 | "not."+CondVal->getName()), SI); |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6068 | return new CastInst(NotCond, SI.getType()); |
Chris Lattner | cf7baf3 | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 6069 | } |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 6070 | |
Chris Lattner | 380c7e9 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6071 | if (SetCondInst *IC = dyn_cast<SetCondInst>(SI.getCondition())) { |
| 6072 | |
| 6073 | // (x <s 0) ? -1 : 0 -> sra x, 31 |
| 6074 | // (x >u 2147483647) ? -1 : 0 -> sra x, 31 |
| 6075 | if (TrueValC->isAllOnesValue() && FalseValC->isNullValue()) |
| 6076 | if (ConstantInt *CmpCst = dyn_cast<ConstantInt>(IC->getOperand(1))) { |
| 6077 | bool CanXForm = false; |
| 6078 | if (CmpCst->getType()->isSigned()) |
| 6079 | CanXForm = CmpCst->isNullValue() && |
| 6080 | IC->getOpcode() == Instruction::SetLT; |
| 6081 | else { |
| 6082 | unsigned Bits = CmpCst->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6083 | CanXForm = (CmpCst->getZExtValue() == ~0ULL >> (64-Bits+1)) && |
Chris Lattner | 380c7e9 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6084 | IC->getOpcode() == Instruction::SetGT; |
| 6085 | } |
| 6086 | |
| 6087 | if (CanXForm) { |
| 6088 | // The comparison constant and the result are not neccessarily the |
| 6089 | // same width. In any case, the first step to do is make sure |
| 6090 | // that X is signed. |
| 6091 | Value *X = IC->getOperand(0); |
| 6092 | if (!X->getType()->isSigned()) |
| 6093 | X = InsertCastBefore(X, X->getType()->getSignedVersion(), SI); |
| 6094 | |
| 6095 | // Now that X is signed, we have to make the all ones value. Do |
| 6096 | // this by inserting a new SRA. |
| 6097 | unsigned Bits = X->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6098 | Constant *ShAmt = ConstantInt::get(Type::UByteTy, Bits-1); |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 6099 | Instruction *SRA = new ShiftInst(Instruction::AShr, X, |
Chris Lattner | 380c7e9 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6100 | ShAmt, "ones"); |
| 6101 | InsertNewInstBefore(SRA, SI); |
| 6102 | |
| 6103 | // Finally, convert to the type of the select RHS. If this is |
| 6104 | // smaller than the compare value, it will truncate the ones to |
| 6105 | // fit. If it is larger, it will sext the ones to fit. |
| 6106 | return new CastInst(SRA, SI.getType()); |
| 6107 | } |
| 6108 | } |
| 6109 | |
| 6110 | |
| 6111 | // If one of the constants is zero (we know they can't both be) and we |
| 6112 | // have a setcc instruction with zero, and we have an 'and' with the |
| 6113 | // non-constant value, eliminate this whole mess. This corresponds to |
| 6114 | // cases like this: ((X & 27) ? 27 : 0) |
| 6115 | if (TrueValC->isNullValue() || FalseValC->isNullValue()) |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 6116 | if (IC->isEquality() && isa<ConstantInt>(IC->getOperand(1)) && |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 6117 | cast<Constant>(IC->getOperand(1))->isNullValue()) |
| 6118 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) |
| 6119 | if (ICA->getOpcode() == Instruction::And && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6120 | isa<ConstantInt>(ICA->getOperand(1)) && |
| 6121 | (ICA->getOperand(1) == TrueValC || |
| 6122 | ICA->getOperand(1) == FalseValC) && |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 6123 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { |
| 6124 | // Okay, now we know that everything is set up, we just don't |
| 6125 | // know whether we have a setne or seteq and whether the true or |
| 6126 | // false val is the zero. |
| 6127 | bool ShouldNotVal = !TrueValC->isNullValue(); |
| 6128 | ShouldNotVal ^= IC->getOpcode() == Instruction::SetNE; |
| 6129 | Value *V = ICA; |
| 6130 | if (ShouldNotVal) |
| 6131 | V = InsertNewInstBefore(BinaryOperator::create( |
| 6132 | Instruction::Xor, V, ICA->getOperand(1)), SI); |
| 6133 | return ReplaceInstUsesWith(SI, V); |
| 6134 | } |
Chris Lattner | 380c7e9 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6135 | } |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 6136 | } |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6137 | |
| 6138 | // See if we are selecting two values based on a comparison of the two values. |
| 6139 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(CondVal)) { |
| 6140 | if (SCI->getOperand(0) == TrueVal && SCI->getOperand(1) == FalseVal) { |
| 6141 | // Transform (X == Y) ? X : Y -> Y |
| 6142 | if (SCI->getOpcode() == Instruction::SetEQ) |
| 6143 | return ReplaceInstUsesWith(SI, FalseVal); |
| 6144 | // Transform (X != Y) ? X : Y -> X |
| 6145 | if (SCI->getOpcode() == Instruction::SetNE) |
| 6146 | return ReplaceInstUsesWith(SI, TrueVal); |
| 6147 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 6148 | |
| 6149 | } else if (SCI->getOperand(0) == FalseVal && SCI->getOperand(1) == TrueVal){ |
| 6150 | // Transform (X == Y) ? Y : X -> X |
| 6151 | if (SCI->getOpcode() == Instruction::SetEQ) |
Chris Lattner | 24cf020 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 6152 | return ReplaceInstUsesWith(SI, FalseVal); |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6153 | // Transform (X != Y) ? Y : X -> Y |
| 6154 | if (SCI->getOpcode() == Instruction::SetNE) |
Chris Lattner | 24cf020 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 6155 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6156 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 6157 | } |
| 6158 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6159 | |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 6160 | if (Instruction *TI = dyn_cast<Instruction>(TrueVal)) |
| 6161 | if (Instruction *FI = dyn_cast<Instruction>(FalseVal)) |
| 6162 | if (TI->hasOneUse() && FI->hasOneUse()) { |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 6163 | Instruction *AddOp = 0, *SubOp = 0; |
| 6164 | |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6165 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) |
| 6166 | if (TI->getOpcode() == FI->getOpcode()) |
| 6167 | if (Instruction *IV = FoldSelectOpOp(SI, TI, FI)) |
| 6168 | return IV; |
| 6169 | |
| 6170 | // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is |
| 6171 | // even legal for FP. |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 6172 | if (TI->getOpcode() == Instruction::Sub && |
| 6173 | FI->getOpcode() == Instruction::Add) { |
| 6174 | AddOp = FI; SubOp = TI; |
| 6175 | } else if (FI->getOpcode() == Instruction::Sub && |
| 6176 | TI->getOpcode() == Instruction::Add) { |
| 6177 | AddOp = TI; SubOp = FI; |
| 6178 | } |
| 6179 | |
| 6180 | if (AddOp) { |
| 6181 | Value *OtherAddOp = 0; |
| 6182 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { |
| 6183 | OtherAddOp = AddOp->getOperand(1); |
| 6184 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { |
| 6185 | OtherAddOp = AddOp->getOperand(0); |
| 6186 | } |
| 6187 | |
| 6188 | if (OtherAddOp) { |
Chris Lattner | b580d26 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 6189 | // So at this point we know we have (Y -> OtherAddOp): |
| 6190 | // select C, (add X, Y), (sub X, Z) |
| 6191 | Value *NegVal; // Compute -Z |
| 6192 | if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) { |
| 6193 | NegVal = ConstantExpr::getNeg(C); |
| 6194 | } else { |
| 6195 | NegVal = InsertNewInstBefore( |
| 6196 | BinaryOperator::createNeg(SubOp->getOperand(1), "tmp"), SI); |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 6197 | } |
Chris Lattner | b580d26 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 6198 | |
| 6199 | Value *NewTrueOp = OtherAddOp; |
| 6200 | Value *NewFalseOp = NegVal; |
| 6201 | if (AddOp != TI) |
| 6202 | std::swap(NewTrueOp, NewFalseOp); |
| 6203 | Instruction *NewSel = |
| 6204 | new SelectInst(CondVal, NewTrueOp,NewFalseOp,SI.getName()+".p"); |
| 6205 | |
| 6206 | NewSel = InsertNewInstBefore(NewSel, SI); |
| 6207 | return BinaryOperator::createAdd(SubOp->getOperand(0), NewSel); |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 6208 | } |
| 6209 | } |
| 6210 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6211 | |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6212 | // See if we can fold the select into one of our operands. |
| 6213 | if (SI.getType()->isInteger()) { |
| 6214 | // See the comment above GetSelectFoldableOperands for a description of the |
| 6215 | // transformation we are doing here. |
| 6216 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) |
| 6217 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && |
| 6218 | !isa<Constant>(FalseVal)) |
| 6219 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { |
| 6220 | unsigned OpToFold = 0; |
| 6221 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 6222 | OpToFold = 1; |
| 6223 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
| 6224 | OpToFold = 2; |
| 6225 | } |
| 6226 | |
| 6227 | if (OpToFold) { |
| 6228 | Constant *C = GetSelectFoldableConstant(TVI); |
| 6229 | std::string Name = TVI->getName(); TVI->setName(""); |
| 6230 | Instruction *NewSel = |
| 6231 | new SelectInst(SI.getCondition(), TVI->getOperand(2-OpToFold), C, |
| 6232 | Name); |
| 6233 | InsertNewInstBefore(NewSel, SI); |
| 6234 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) |
| 6235 | return BinaryOperator::create(BO->getOpcode(), FalseVal, NewSel); |
| 6236 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(TVI)) |
| 6237 | return new ShiftInst(SI->getOpcode(), FalseVal, NewSel); |
| 6238 | else { |
| 6239 | assert(0 && "Unknown instruction!!"); |
| 6240 | } |
| 6241 | } |
| 6242 | } |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 6243 | |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6244 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) |
| 6245 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && |
| 6246 | !isa<Constant>(TrueVal)) |
| 6247 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { |
| 6248 | unsigned OpToFold = 0; |
| 6249 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 6250 | OpToFold = 1; |
| 6251 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
| 6252 | OpToFold = 2; |
| 6253 | } |
| 6254 | |
| 6255 | if (OpToFold) { |
| 6256 | Constant *C = GetSelectFoldableConstant(FVI); |
| 6257 | std::string Name = FVI->getName(); FVI->setName(""); |
| 6258 | Instruction *NewSel = |
| 6259 | new SelectInst(SI.getCondition(), C, FVI->getOperand(2-OpToFold), |
| 6260 | Name); |
| 6261 | InsertNewInstBefore(NewSel, SI); |
| 6262 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) |
| 6263 | return BinaryOperator::create(BO->getOpcode(), TrueVal, NewSel); |
| 6264 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(FVI)) |
| 6265 | return new ShiftInst(SI->getOpcode(), TrueVal, NewSel); |
| 6266 | else { |
| 6267 | assert(0 && "Unknown instruction!!"); |
| 6268 | } |
| 6269 | } |
| 6270 | } |
| 6271 | } |
Chris Lattner | d6f636a | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 6272 | |
| 6273 | if (BinaryOperator::isNot(CondVal)) { |
| 6274 | SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); |
| 6275 | SI.setOperand(1, FalseVal); |
| 6276 | SI.setOperand(2, TrueVal); |
| 6277 | return &SI; |
| 6278 | } |
| 6279 | |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 6280 | return 0; |
| 6281 | } |
| 6282 | |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6283 | /// GetKnownAlignment - If the specified pointer has an alignment that we can |
| 6284 | /// determine, return it, otherwise return 0. |
| 6285 | static unsigned GetKnownAlignment(Value *V, TargetData *TD) { |
| 6286 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 6287 | unsigned Align = GV->getAlignment(); |
| 6288 | if (Align == 0 && TD) |
| 6289 | Align = TD->getTypeAlignment(GV->getType()->getElementType()); |
| 6290 | return Align; |
| 6291 | } else if (AllocationInst *AI = dyn_cast<AllocationInst>(V)) { |
| 6292 | unsigned Align = AI->getAlignment(); |
| 6293 | if (Align == 0 && TD) { |
| 6294 | if (isa<AllocaInst>(AI)) |
| 6295 | Align = TD->getTypeAlignment(AI->getType()->getElementType()); |
| 6296 | else if (isa<MallocInst>(AI)) { |
| 6297 | // Malloc returns maximally aligned memory. |
| 6298 | Align = TD->getTypeAlignment(AI->getType()->getElementType()); |
| 6299 | Align = std::max(Align, (unsigned)TD->getTypeAlignment(Type::DoubleTy)); |
| 6300 | Align = std::max(Align, (unsigned)TD->getTypeAlignment(Type::LongTy)); |
| 6301 | } |
| 6302 | } |
| 6303 | return Align; |
Chris Lattner | 53ef5a0 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 6304 | } else if (isa<CastInst>(V) || |
| 6305 | (isa<ConstantExpr>(V) && |
| 6306 | cast<ConstantExpr>(V)->getOpcode() == Instruction::Cast)) { |
| 6307 | User *CI = cast<User>(V); |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6308 | if (isa<PointerType>(CI->getOperand(0)->getType())) |
| 6309 | return GetKnownAlignment(CI->getOperand(0), TD); |
| 6310 | return 0; |
Chris Lattner | 53ef5a0 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 6311 | } else if (isa<GetElementPtrInst>(V) || |
| 6312 | (isa<ConstantExpr>(V) && |
| 6313 | cast<ConstantExpr>(V)->getOpcode()==Instruction::GetElementPtr)) { |
| 6314 | User *GEPI = cast<User>(V); |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6315 | unsigned BaseAlignment = GetKnownAlignment(GEPI->getOperand(0), TD); |
| 6316 | if (BaseAlignment == 0) return 0; |
| 6317 | |
| 6318 | // If all indexes are zero, it is just the alignment of the base pointer. |
| 6319 | bool AllZeroOperands = true; |
| 6320 | for (unsigned i = 1, e = GEPI->getNumOperands(); i != e; ++i) |
| 6321 | if (!isa<Constant>(GEPI->getOperand(i)) || |
| 6322 | !cast<Constant>(GEPI->getOperand(i))->isNullValue()) { |
| 6323 | AllZeroOperands = false; |
| 6324 | break; |
| 6325 | } |
| 6326 | if (AllZeroOperands) |
| 6327 | return BaseAlignment; |
| 6328 | |
| 6329 | // Otherwise, if the base alignment is >= the alignment we expect for the |
| 6330 | // base pointer type, then we know that the resultant pointer is aligned at |
| 6331 | // least as much as its type requires. |
| 6332 | if (!TD) return 0; |
| 6333 | |
| 6334 | const Type *BasePtrTy = GEPI->getOperand(0)->getType(); |
| 6335 | if (TD->getTypeAlignment(cast<PointerType>(BasePtrTy)->getElementType()) |
Chris Lattner | 53ef5a0 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 6336 | <= BaseAlignment) { |
| 6337 | const Type *GEPTy = GEPI->getType(); |
| 6338 | return TD->getTypeAlignment(cast<PointerType>(GEPTy)->getElementType()); |
| 6339 | } |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6340 | return 0; |
| 6341 | } |
| 6342 | return 0; |
| 6343 | } |
| 6344 | |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 6345 | |
Chris Lattner | c66b223 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 6346 | /// visitCallInst - CallInst simplification. This mostly only handles folding |
| 6347 | /// of intrinsic instructions. For normal calls, it allows visitCallSite to do |
| 6348 | /// the heavy lifting. |
| 6349 | /// |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6350 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Chris Lattner | c66b223 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 6351 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI); |
| 6352 | if (!II) return visitCallSite(&CI); |
| 6353 | |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 6354 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 6355 | // visitCallSite. |
Chris Lattner | c66b223 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 6356 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) { |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 6357 | bool Changed = false; |
| 6358 | |
| 6359 | // memmove/cpy/set of zero bytes is a noop. |
| 6360 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { |
| 6361 | if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); |
| 6362 | |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 6363 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6364 | if (CI->getZExtValue() == 1) { |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 6365 | // Replace the instruction with just byte operations. We would |
| 6366 | // transform other cases to loads/stores, but we don't know if |
| 6367 | // alignment is sufficient. |
| 6368 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 6369 | } |
| 6370 | |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 6371 | // If we have a memmove and the source operation is a constant global, |
| 6372 | // then the source and dest pointers can't alias, so we can change this |
| 6373 | // into a call to memcpy. |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6374 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(II)) { |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 6375 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
| 6376 | if (GVSrc->isConstant()) { |
| 6377 | Module *M = CI.getParent()->getParent()->getParent(); |
Chris Lattner | 681ef2f | 2006-03-03 01:34:17 +0000 | [diff] [blame] | 6378 | const char *Name; |
Andrew Lenharth | 0ebb0b0 | 2006-11-03 22:45:50 +0000 | [diff] [blame] | 6379 | if (CI.getCalledFunction()->getFunctionType()->getParamType(2) == |
Chris Lattner | 681ef2f | 2006-03-03 01:34:17 +0000 | [diff] [blame] | 6380 | Type::UIntTy) |
| 6381 | Name = "llvm.memcpy.i32"; |
| 6382 | else |
| 6383 | Name = "llvm.memcpy.i64"; |
| 6384 | Function *MemCpy = M->getOrInsertFunction(Name, |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 6385 | CI.getCalledFunction()->getFunctionType()); |
| 6386 | CI.setOperand(0, MemCpy); |
| 6387 | Changed = true; |
| 6388 | } |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6389 | } |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 6390 | |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6391 | // If we can determine a pointer alignment that is bigger than currently |
| 6392 | // set, update the alignment. |
| 6393 | if (isa<MemCpyInst>(MI) || isa<MemMoveInst>(MI)) { |
| 6394 | unsigned Alignment1 = GetKnownAlignment(MI->getOperand(1), TD); |
| 6395 | unsigned Alignment2 = GetKnownAlignment(MI->getOperand(2), TD); |
| 6396 | unsigned Align = std::min(Alignment1, Alignment2); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6397 | if (MI->getAlignment()->getZExtValue() < Align) { |
| 6398 | MI->setAlignment(ConstantInt::get(Type::UIntTy, Align)); |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6399 | Changed = true; |
| 6400 | } |
| 6401 | } else if (isa<MemSetInst>(MI)) { |
| 6402 | unsigned Alignment = GetKnownAlignment(MI->getDest(), TD); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6403 | if (MI->getAlignment()->getZExtValue() < Alignment) { |
| 6404 | MI->setAlignment(ConstantInt::get(Type::UIntTy, Alignment)); |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6405 | Changed = true; |
| 6406 | } |
| 6407 | } |
| 6408 | |
Chris Lattner | c66b223 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 6409 | if (Changed) return II; |
Chris Lattner | 503221f | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 6410 | } else { |
| 6411 | switch (II->getIntrinsicID()) { |
| 6412 | default: break; |
Chris Lattner | f42d0ae | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 6413 | case Intrinsic::ppc_altivec_lvx: |
| 6414 | case Intrinsic::ppc_altivec_lvxl: |
Chris Lattner | 36dd7c9 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 6415 | case Intrinsic::x86_sse_loadu_ps: |
| 6416 | case Intrinsic::x86_sse2_loadu_pd: |
| 6417 | case Intrinsic::x86_sse2_loadu_dq: |
| 6418 | // Turn PPC lvx -> load if the pointer is known aligned. |
| 6419 | // Turn X86 loadups -> load if the pointer is known aligned. |
Chris Lattner | f42d0ae | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 6420 | if (GetKnownAlignment(II->getOperand(1), TD) >= 16) { |
Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 6421 | Value *Ptr = InsertCastBefore(II->getOperand(1), |
| 6422 | PointerType::get(II->getType()), CI); |
Chris Lattner | f42d0ae | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 6423 | return new LoadInst(Ptr); |
| 6424 | } |
| 6425 | break; |
| 6426 | case Intrinsic::ppc_altivec_stvx: |
| 6427 | case Intrinsic::ppc_altivec_stvxl: |
| 6428 | // Turn stvx -> store if the pointer is known aligned. |
| 6429 | if (GetKnownAlignment(II->getOperand(2), TD) >= 16) { |
Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 6430 | const Type *OpPtrTy = PointerType::get(II->getOperand(1)->getType()); |
| 6431 | Value *Ptr = InsertCastBefore(II->getOperand(2), OpPtrTy, CI); |
Chris Lattner | f42d0ae | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 6432 | return new StoreInst(II->getOperand(1), Ptr); |
| 6433 | } |
| 6434 | break; |
Chris Lattner | 36dd7c9 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 6435 | case Intrinsic::x86_sse_storeu_ps: |
| 6436 | case Intrinsic::x86_sse2_storeu_pd: |
| 6437 | case Intrinsic::x86_sse2_storeu_dq: |
| 6438 | case Intrinsic::x86_sse2_storel_dq: |
| 6439 | // Turn X86 storeu -> store if the pointer is known aligned. |
| 6440 | if (GetKnownAlignment(II->getOperand(1), TD) >= 16) { |
| 6441 | const Type *OpPtrTy = PointerType::get(II->getOperand(2)->getType()); |
| 6442 | Value *Ptr = InsertCastBefore(II->getOperand(1), OpPtrTy, CI); |
| 6443 | return new StoreInst(II->getOperand(2), Ptr); |
| 6444 | } |
| 6445 | break; |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 6446 | |
| 6447 | case Intrinsic::x86_sse_cvttss2si: { |
| 6448 | // These intrinsics only demands the 0th element of its input vector. If |
| 6449 | // we can simplify the input based on that, do so now. |
| 6450 | uint64_t UndefElts; |
| 6451 | if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), 1, |
| 6452 | UndefElts)) { |
| 6453 | II->setOperand(1, V); |
| 6454 | return II; |
| 6455 | } |
| 6456 | break; |
| 6457 | } |
| 6458 | |
Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 6459 | case Intrinsic::ppc_altivec_vperm: |
| 6460 | // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant. |
| 6461 | if (ConstantPacked *Mask = dyn_cast<ConstantPacked>(II->getOperand(3))) { |
| 6462 | assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!"); |
| 6463 | |
| 6464 | // Check that all of the elements are integer constants or undefs. |
| 6465 | bool AllEltsOk = true; |
| 6466 | for (unsigned i = 0; i != 16; ++i) { |
| 6467 | if (!isa<ConstantInt>(Mask->getOperand(i)) && |
| 6468 | !isa<UndefValue>(Mask->getOperand(i))) { |
| 6469 | AllEltsOk = false; |
| 6470 | break; |
| 6471 | } |
| 6472 | } |
| 6473 | |
| 6474 | if (AllEltsOk) { |
| 6475 | // Cast the input vectors to byte vectors. |
| 6476 | Value *Op0 = InsertCastBefore(II->getOperand(1), Mask->getType(), CI); |
| 6477 | Value *Op1 = InsertCastBefore(II->getOperand(2), Mask->getType(), CI); |
| 6478 | Value *Result = UndefValue::get(Op0->getType()); |
| 6479 | |
| 6480 | // Only extract each element once. |
| 6481 | Value *ExtractedElts[32]; |
| 6482 | memset(ExtractedElts, 0, sizeof(ExtractedElts)); |
| 6483 | |
| 6484 | for (unsigned i = 0; i != 16; ++i) { |
| 6485 | if (isa<UndefValue>(Mask->getOperand(i))) |
| 6486 | continue; |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6487 | unsigned Idx =cast<ConstantInt>(Mask->getOperand(i))->getZExtValue(); |
Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 6488 | Idx &= 31; // Match the hardware behavior. |
| 6489 | |
| 6490 | if (ExtractedElts[Idx] == 0) { |
| 6491 | Instruction *Elt = |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 6492 | new ExtractElementInst(Idx < 16 ? Op0 : Op1, Idx&15, "tmp"); |
Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 6493 | InsertNewInstBefore(Elt, CI); |
| 6494 | ExtractedElts[Idx] = Elt; |
| 6495 | } |
| 6496 | |
| 6497 | // Insert this value into the result vector. |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 6498 | Result = new InsertElementInst(Result, ExtractedElts[Idx], i,"tmp"); |
Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 6499 | InsertNewInstBefore(cast<Instruction>(Result), CI); |
| 6500 | } |
| 6501 | return new CastInst(Result, CI.getType()); |
| 6502 | } |
| 6503 | } |
| 6504 | break; |
| 6505 | |
Chris Lattner | 503221f | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 6506 | case Intrinsic::stackrestore: { |
| 6507 | // If the save is right next to the restore, remove the restore. This can |
| 6508 | // happen when variable allocas are DCE'd. |
| 6509 | if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) { |
| 6510 | if (SS->getIntrinsicID() == Intrinsic::stacksave) { |
| 6511 | BasicBlock::iterator BI = SS; |
| 6512 | if (&*++BI == II) |
| 6513 | return EraseInstFromFunction(CI); |
| 6514 | } |
| 6515 | } |
| 6516 | |
| 6517 | // If the stack restore is in a return/unwind block and if there are no |
| 6518 | // allocas or calls between the restore and the return, nuke the restore. |
| 6519 | TerminatorInst *TI = II->getParent()->getTerminator(); |
| 6520 | if (isa<ReturnInst>(TI) || isa<UnwindInst>(TI)) { |
| 6521 | BasicBlock::iterator BI = II; |
| 6522 | bool CannotRemove = false; |
| 6523 | for (++BI; &*BI != TI; ++BI) { |
| 6524 | if (isa<AllocaInst>(BI) || |
| 6525 | (isa<CallInst>(BI) && !isa<IntrinsicInst>(BI))) { |
| 6526 | CannotRemove = true; |
| 6527 | break; |
| 6528 | } |
| 6529 | } |
| 6530 | if (!CannotRemove) |
| 6531 | return EraseInstFromFunction(CI); |
| 6532 | } |
| 6533 | break; |
| 6534 | } |
| 6535 | } |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 6536 | } |
| 6537 | |
Chris Lattner | c66b223 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 6538 | return visitCallSite(II); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6539 | } |
| 6540 | |
| 6541 | // InvokeInst simplification |
| 6542 | // |
| 6543 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 6544 | return visitCallSite(&II); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6545 | } |
| 6546 | |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 6547 | // visitCallSite - Improvements for call and invoke instructions. |
| 6548 | // |
| 6549 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 6550 | bool Changed = false; |
| 6551 | |
| 6552 | // If the callee is a constexpr cast of a function, attempt to move the cast |
| 6553 | // to the arguments of the call/invoke. |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 6554 | if (transformConstExprCastCall(CS)) return 0; |
| 6555 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 6556 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6557 | |
Chris Lattner | 61d9d81 | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 6558 | if (Function *CalleeF = dyn_cast<Function>(Callee)) |
| 6559 | if (CalleeF->getCallingConv() != CS.getCallingConv()) { |
| 6560 | Instruction *OldCall = CS.getInstruction(); |
| 6561 | // If the call and callee calling conventions don't match, this call must |
| 6562 | // be unreachable, as the call is undefined. |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 6563 | new StoreInst(ConstantBool::getTrue(), |
Chris Lattner | 61d9d81 | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 6564 | UndefValue::get(PointerType::get(Type::BoolTy)), OldCall); |
| 6565 | if (!OldCall->use_empty()) |
| 6566 | OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); |
| 6567 | if (isa<CallInst>(OldCall)) // Not worth removing an invoke here. |
| 6568 | return EraseInstFromFunction(*OldCall); |
| 6569 | return 0; |
| 6570 | } |
| 6571 | |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 6572 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
| 6573 | // This instruction is not reachable, just remove it. We insert a store to |
| 6574 | // undef so that we know that this code is not reachable, despite the fact |
| 6575 | // that we can't modify the CFG here. |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 6576 | new StoreInst(ConstantBool::getTrue(), |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 6577 | UndefValue::get(PointerType::get(Type::BoolTy)), |
| 6578 | CS.getInstruction()); |
| 6579 | |
| 6580 | if (!CS.getInstruction()->use_empty()) |
| 6581 | CS.getInstruction()-> |
| 6582 | replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); |
| 6583 | |
| 6584 | if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { |
| 6585 | // Don't break the CFG, insert a dummy cond branch. |
| 6586 | new BranchInst(II->getNormalDest(), II->getUnwindDest(), |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 6587 | ConstantBool::getTrue(), II); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6588 | } |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 6589 | return EraseInstFromFunction(*CS.getInstruction()); |
| 6590 | } |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6591 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 6592 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 6593 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 6594 | if (FTy->isVarArg()) { |
| 6595 | // See if we can optimize any arguments passed through the varargs area of |
| 6596 | // the call. |
| 6597 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
| 6598 | E = CS.arg_end(); I != E; ++I) |
| 6599 | if (CastInst *CI = dyn_cast<CastInst>(*I)) { |
| 6600 | // If this cast does not effect the value passed through the varargs |
| 6601 | // area, we can eliminate the use of the cast. |
| 6602 | Value *Op = CI->getOperand(0); |
| 6603 | if (CI->getType()->isLosslesslyConvertibleTo(Op->getType())) { |
| 6604 | *I = Op; |
| 6605 | Changed = true; |
| 6606 | } |
| 6607 | } |
| 6608 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6609 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 6610 | return Changed ? CS.getInstruction() : 0; |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 6611 | } |
| 6612 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6613 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 6614 | // attempt to move the cast to the arguments of the call/invoke. |
| 6615 | // |
| 6616 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 6617 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 6618 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
Chris Lattner | f3edc49 | 2004-07-18 18:59:44 +0000 | [diff] [blame] | 6619 | if (CE->getOpcode() != Instruction::Cast || !isa<Function>(CE->getOperand(0))) |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6620 | return false; |
Reid Spencer | 8743687 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 6621 | Function *Callee = cast<Function>(CE->getOperand(0)); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6622 | Instruction *Caller = CS.getInstruction(); |
| 6623 | |
| 6624 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 6625 | // would cause a type conversion of one of our arguments, change this call to |
| 6626 | // be a direct call with arguments casted to the appropriate types. |
| 6627 | // |
| 6628 | const FunctionType *FT = Callee->getFunctionType(); |
| 6629 | const Type *OldRetTy = Caller->getType(); |
| 6630 | |
Chris Lattner | 1f7942f | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 6631 | // Check to see if we are changing the return type... |
| 6632 | if (OldRetTy != FT->getReturnType()) { |
| 6633 | if (Callee->isExternal() && |
Andrew Lenharth | 61eae29 | 2006-04-20 14:56:47 +0000 | [diff] [blame] | 6634 | !(OldRetTy->isLosslesslyConvertibleTo(FT->getReturnType()) || |
| 6635 | (isa<PointerType>(FT->getReturnType()) && |
Andrew Lenharth | f89e630 | 2006-04-20 15:41:37 +0000 | [diff] [blame] | 6636 | TD->getIntPtrType()->isLosslesslyConvertibleTo(OldRetTy))) |
Andrew Lenharth | 61eae29 | 2006-04-20 14:56:47 +0000 | [diff] [blame] | 6637 | && !Caller->use_empty()) |
Chris Lattner | 1f7942f | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 6638 | return false; // Cannot transform this return value... |
| 6639 | |
| 6640 | // If the callsite is an invoke instruction, and the return value is used by |
| 6641 | // a PHI node in a successor, we cannot change the return type of the call |
| 6642 | // because there is no place to put the cast instruction (without breaking |
| 6643 | // the critical edge). Bail out in this case. |
| 6644 | if (!Caller->use_empty()) |
| 6645 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 6646 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 6647 | UI != E; ++UI) |
| 6648 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 6649 | if (PN->getParent() == II->getNormalDest() || |
Chris Lattner | fae8ab3 | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 6650 | PN->getParent() == II->getUnwindDest()) |
Chris Lattner | 1f7942f | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 6651 | return false; |
| 6652 | } |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6653 | |
| 6654 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 6655 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6656 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6657 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 6658 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 6659 | const Type *ParamTy = FT->getParamType(i); |
Andrew Lenharth | ebfa24e | 2006-06-28 01:01:52 +0000 | [diff] [blame] | 6660 | const Type *ActTy = (*AI)->getType(); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6661 | ConstantInt* c = dyn_cast<ConstantInt>(*AI); |
Andrew Lenharth | ebfa24e | 2006-06-28 01:01:52 +0000 | [diff] [blame] | 6662 | //Either we can cast directly, or we can upconvert the argument |
| 6663 | bool isConvertible = ActTy->isLosslesslyConvertibleTo(ParamTy) || |
| 6664 | (ParamTy->isIntegral() && ActTy->isIntegral() && |
| 6665 | ParamTy->isSigned() == ActTy->isSigned() && |
| 6666 | ParamTy->getPrimitiveSize() >= ActTy->getPrimitiveSize()) || |
| 6667 | (c && ParamTy->getPrimitiveSize() >= ActTy->getPrimitiveSize() && |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6668 | c->getSExtValue() > 0); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6669 | if (Callee->isExternal() && !isConvertible) return false; |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6670 | } |
| 6671 | |
| 6672 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
| 6673 | Callee->isExternal()) |
| 6674 | return false; // Do not delete arguments unless we have a function body... |
| 6675 | |
| 6676 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 6677 | // inserting cast instructions as necessary... |
| 6678 | std::vector<Value*> Args; |
| 6679 | Args.reserve(NumActualArgs); |
| 6680 | |
| 6681 | AI = CS.arg_begin(); |
| 6682 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 6683 | const Type *ParamTy = FT->getParamType(i); |
| 6684 | if ((*AI)->getType() == ParamTy) { |
| 6685 | Args.push_back(*AI); |
| 6686 | } else { |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6687 | Args.push_back(InsertNewInstBefore(new CastInst(*AI, ParamTy, "tmp"), |
| 6688 | *Caller)); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6689 | } |
| 6690 | } |
| 6691 | |
| 6692 | // If the function takes more arguments than the call was taking, add them |
| 6693 | // now... |
| 6694 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 6695 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 6696 | |
| 6697 | // If we are removing arguments to the function, emit an obnoxious warning... |
| 6698 | if (FT->getNumParams() < NumActualArgs) |
| 6699 | if (!FT->isVarArg()) { |
| 6700 | std::cerr << "WARNING: While resolving call to function '" |
| 6701 | << Callee->getName() << "' arguments were dropped!\n"; |
| 6702 | } else { |
| 6703 | // Add all of the arguments in their promoted form to the arg list... |
| 6704 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 6705 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 6706 | if (PTy != (*AI)->getType()) { |
| 6707 | // Must promote to pass through va_arg area! |
| 6708 | Instruction *Cast = new CastInst(*AI, PTy, "tmp"); |
| 6709 | InsertNewInstBefore(Cast, *Caller); |
| 6710 | Args.push_back(Cast); |
| 6711 | } else { |
| 6712 | Args.push_back(*AI); |
| 6713 | } |
| 6714 | } |
| 6715 | } |
| 6716 | |
| 6717 | if (FT->getReturnType() == Type::VoidTy) |
| 6718 | Caller->setName(""); // Void type should not have a name... |
| 6719 | |
| 6720 | Instruction *NC; |
| 6721 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Chris Lattner | fae8ab3 | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 6722 | NC = new InvokeInst(Callee, II->getNormalDest(), II->getUnwindDest(), |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6723 | Args, Caller->getName(), Caller); |
Chris Lattner | 05c703e | 2005-05-14 12:25:32 +0000 | [diff] [blame] | 6724 | cast<InvokeInst>(II)->setCallingConv(II->getCallingConv()); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6725 | } else { |
| 6726 | NC = new CallInst(Callee, Args, Caller->getName(), Caller); |
Chris Lattner | 6aacb0f | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 6727 | if (cast<CallInst>(Caller)->isTailCall()) |
| 6728 | cast<CallInst>(NC)->setTailCall(); |
Chris Lattner | 05c703e | 2005-05-14 12:25:32 +0000 | [diff] [blame] | 6729 | cast<CallInst>(NC)->setCallingConv(cast<CallInst>(Caller)->getCallingConv()); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6730 | } |
| 6731 | |
| 6732 | // Insert a cast of the return type as necessary... |
| 6733 | Value *NV = NC; |
| 6734 | if (Caller->getType() != NV->getType() && !Caller->use_empty()) { |
| 6735 | if (NV->getType() != Type::VoidTy) { |
| 6736 | NV = NC = new CastInst(NC, Caller->getType(), "tmp"); |
Chris Lattner | 686767f | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 6737 | |
| 6738 | // If this is an invoke instruction, we should insert it after the first |
| 6739 | // non-phi, instruction in the normal successor block. |
| 6740 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 6741 | BasicBlock::iterator I = II->getNormalDest()->begin(); |
| 6742 | while (isa<PHINode>(I)) ++I; |
| 6743 | InsertNewInstBefore(NC, *I); |
| 6744 | } else { |
| 6745 | // Otherwise, it's a call, just insert cast right after the call instr |
| 6746 | InsertNewInstBefore(NC, *Caller); |
| 6747 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 6748 | AddUsersToWorkList(*Caller); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6749 | } else { |
Chris Lattner | e29d634 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 6750 | NV = UndefValue::get(Caller->getType()); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6751 | } |
| 6752 | } |
| 6753 | |
| 6754 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 6755 | Caller->replaceAllUsesWith(NV); |
| 6756 | Caller->getParent()->getInstList().erase(Caller); |
| 6757 | removeFromWorkList(Caller); |
| 6758 | return true; |
| 6759 | } |
| 6760 | |
Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 6761 | /// FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(c,d)] |
| 6762 | /// and if a/b/c/d and the add's all have a single use, turn this into two phi's |
| 6763 | /// and a single binop. |
| 6764 | Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI(PHINode &PN) { |
| 6765 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
Chris Lattner | eebea43 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 6766 | assert(isa<BinaryOperator>(FirstInst) || isa<ShiftInst>(FirstInst) || |
| 6767 | isa<GetElementPtrInst>(FirstInst)); |
Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 6768 | unsigned Opc = FirstInst->getOpcode(); |
Chris Lattner | cd62f11 | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 6769 | Value *LHSVal = FirstInst->getOperand(0); |
| 6770 | Value *RHSVal = FirstInst->getOperand(1); |
| 6771 | |
| 6772 | const Type *LHSType = LHSVal->getType(); |
| 6773 | const Type *RHSType = RHSVal->getType(); |
Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 6774 | |
| 6775 | // Scan to see if all operands are the same opcode, all have one use, and all |
| 6776 | // kill their operands (i.e. the operands have one use). |
Chris Lattner | dc826fc | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 6777 | for (unsigned i = 0; i != PN.getNumIncomingValues(); ++i) { |
Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 6778 | Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i)); |
Chris Lattner | dc826fc | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 6779 | if (!I || I->getOpcode() != Opc || !I->hasOneUse() || |
| 6780 | // Verify type of the LHS matches so we don't fold setcc's of different |
Chris Lattner | eebea43 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 6781 | // types or GEP's with different index types. |
| 6782 | I->getOperand(0)->getType() != LHSType || |
| 6783 | I->getOperand(1)->getType() != RHSType) |
Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 6784 | return 0; |
Chris Lattner | cd62f11 | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 6785 | |
| 6786 | // Keep track of which operand needs a phi node. |
| 6787 | if (I->getOperand(0) != LHSVal) LHSVal = 0; |
| 6788 | if (I->getOperand(1) != RHSVal) RHSVal = 0; |
Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 6789 | } |
| 6790 | |
Chris Lattner | 4f218d5 | 2006-11-08 19:42:28 +0000 | [diff] [blame^] | 6791 | // Otherwise, this is safe to transform, determine if it is profitable. |
| 6792 | |
| 6793 | // If this is a GEP, and if the index (not the pointer) needs a PHI, bail out. |
| 6794 | // Indexes are often folded into load/store instructions, so we don't want to |
| 6795 | // hide them behind a phi. |
| 6796 | if (isa<GetElementPtrInst>(FirstInst) && RHSVal == 0) |
| 6797 | return 0; |
| 6798 | |
Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 6799 | Value *InLHS = FirstInst->getOperand(0); |
Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 6800 | Value *InRHS = FirstInst->getOperand(1); |
Chris Lattner | 4f218d5 | 2006-11-08 19:42:28 +0000 | [diff] [blame^] | 6801 | PHINode *NewLHS = 0, *NewRHS = 0; |
Chris Lattner | cd62f11 | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 6802 | if (LHSVal == 0) { |
| 6803 | NewLHS = new PHINode(LHSType, FirstInst->getOperand(0)->getName()+".pn"); |
| 6804 | NewLHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 6805 | NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0)); |
Chris Lattner | eebea43 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 6806 | InsertNewInstBefore(NewLHS, PN); |
| 6807 | LHSVal = NewLHS; |
| 6808 | } |
Chris Lattner | cd62f11 | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 6809 | |
| 6810 | if (RHSVal == 0) { |
| 6811 | NewRHS = new PHINode(RHSType, FirstInst->getOperand(1)->getName()+".pn"); |
| 6812 | NewRHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 6813 | NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0)); |
Chris Lattner | eebea43 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 6814 | InsertNewInstBefore(NewRHS, PN); |
| 6815 | RHSVal = NewRHS; |
| 6816 | } |
| 6817 | |
Chris Lattner | cd62f11 | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 6818 | // Add all operands to the new PHIs. |
| 6819 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 6820 | if (NewLHS) { |
| 6821 | Value *NewInLHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 6822 | NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i)); |
| 6823 | } |
| 6824 | if (NewRHS) { |
| 6825 | Value *NewInRHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(1); |
| 6826 | NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i)); |
| 6827 | } |
| 6828 | } |
| 6829 | |
Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 6830 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | eebea43 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 6831 | return BinaryOperator::create(BinOp->getOpcode(), LHSVal, RHSVal); |
| 6832 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(FirstInst)) |
| 6833 | return new ShiftInst(SI->getOpcode(), LHSVal, RHSVal); |
| 6834 | else { |
| 6835 | assert(isa<GetElementPtrInst>(FirstInst)); |
| 6836 | return new GetElementPtrInst(LHSVal, RHSVal); |
| 6837 | } |
Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 6838 | } |
| 6839 | |
Chris Lattner | 14f82c7 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 6840 | /// isSafeToSinkLoad - Return true if we know that it is safe sink the load out |
| 6841 | /// of the block that defines it. This means that it must be obvious the value |
| 6842 | /// of the load is not changed from the point of the load to the end of the |
| 6843 | /// block it is in. |
| 6844 | static bool isSafeToSinkLoad(LoadInst *L) { |
| 6845 | BasicBlock::iterator BBI = L, E = L->getParent()->end(); |
| 6846 | |
| 6847 | for (++BBI; BBI != E; ++BBI) |
| 6848 | if (BBI->mayWriteToMemory()) |
| 6849 | return false; |
| 6850 | return true; |
| 6851 | } |
| 6852 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6853 | |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 6854 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 6855 | // operator and they all are only used by the PHI, PHI together their |
| 6856 | // inputs, and do the operation once, to the result of the PHI. |
| 6857 | Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { |
| 6858 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
| 6859 | |
| 6860 | // Scan the instruction, looking for input operations that can be folded away. |
| 6861 | // If all input operands to the phi are the same instruction (e.g. a cast from |
| 6862 | // the same type or "+42") we can pull the operation through the PHI, reducing |
| 6863 | // code size and simplifying code. |
| 6864 | Constant *ConstantOp = 0; |
| 6865 | const Type *CastSrcTy = 0; |
Chris Lattner | 14f82c7 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 6866 | bool isVolatile = false; |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 6867 | if (isa<CastInst>(FirstInst)) { |
| 6868 | CastSrcTy = FirstInst->getOperand(0)->getType(); |
| 6869 | } else if (isa<BinaryOperator>(FirstInst) || isa<ShiftInst>(FirstInst)) { |
Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 6870 | // Can fold binop or shift here if the RHS is a constant, otherwise call |
| 6871 | // FoldPHIArgBinOpIntoPHI. |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 6872 | ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1)); |
Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 6873 | if (ConstantOp == 0) |
| 6874 | return FoldPHIArgBinOpIntoPHI(PN); |
Chris Lattner | 14f82c7 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 6875 | } else if (LoadInst *LI = dyn_cast<LoadInst>(FirstInst)) { |
| 6876 | isVolatile = LI->isVolatile(); |
| 6877 | // We can't sink the load if the loaded value could be modified between the |
| 6878 | // load and the PHI. |
| 6879 | if (LI->getParent() != PN.getIncomingBlock(0) || |
| 6880 | !isSafeToSinkLoad(LI)) |
| 6881 | return 0; |
Chris Lattner | eebea43 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 6882 | } else if (isa<GetElementPtrInst>(FirstInst)) { |
Chris Lattner | 4f218d5 | 2006-11-08 19:42:28 +0000 | [diff] [blame^] | 6883 | if (FirstInst->getNumOperands() == 2) |
Chris Lattner | eebea43 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 6884 | return FoldPHIArgBinOpIntoPHI(PN); |
| 6885 | // Can't handle general GEPs yet. |
| 6886 | return 0; |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 6887 | } else { |
| 6888 | return 0; // Cannot fold this operation. |
| 6889 | } |
| 6890 | |
| 6891 | // Check to see if all arguments are the same operation. |
| 6892 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 6893 | if (!isa<Instruction>(PN.getIncomingValue(i))) return 0; |
| 6894 | Instruction *I = cast<Instruction>(PN.getIncomingValue(i)); |
| 6895 | if (!I->hasOneUse() || I->getOpcode() != FirstInst->getOpcode()) |
| 6896 | return 0; |
| 6897 | if (CastSrcTy) { |
| 6898 | if (I->getOperand(0)->getType() != CastSrcTy) |
| 6899 | return 0; // Cast operation must match. |
Chris Lattner | 14f82c7 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 6900 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 6901 | // We can't sink the load if the loaded value could be modified between the |
| 6902 | // load and the PHI. |
| 6903 | if (LI->isVolatile() != isVolatile || |
| 6904 | LI->getParent() != PN.getIncomingBlock(i) || |
| 6905 | !isSafeToSinkLoad(LI)) |
| 6906 | return 0; |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 6907 | } else if (I->getOperand(1) != ConstantOp) { |
| 6908 | return 0; |
| 6909 | } |
| 6910 | } |
| 6911 | |
| 6912 | // Okay, they are all the same operation. Create a new PHI node of the |
| 6913 | // correct type, and PHI together all of the LHS's of the instructions. |
| 6914 | PHINode *NewPN = new PHINode(FirstInst->getOperand(0)->getType(), |
| 6915 | PN.getName()+".in"); |
Chris Lattner | d8e2018 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 6916 | NewPN->reserveOperandSpace(PN.getNumOperands()/2); |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 6917 | |
| 6918 | Value *InVal = FirstInst->getOperand(0); |
| 6919 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 6920 | |
| 6921 | // Add all operands to the new PHI. |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 6922 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 6923 | Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 6924 | if (NewInVal != InVal) |
| 6925 | InVal = 0; |
| 6926 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); |
| 6927 | } |
| 6928 | |
| 6929 | Value *PhiVal; |
| 6930 | if (InVal) { |
| 6931 | // The new PHI unions all of the same values together. This is really |
| 6932 | // common, so we handle it intelligently here for compile-time speed. |
| 6933 | PhiVal = InVal; |
| 6934 | delete NewPN; |
| 6935 | } else { |
| 6936 | InsertNewInstBefore(NewPN, PN); |
| 6937 | PhiVal = NewPN; |
| 6938 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6939 | |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 6940 | // Insert and return the new operation. |
| 6941 | if (isa<CastInst>(FirstInst)) |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 6942 | return new CastInst(PhiVal, PN.getType()); |
Reid Spencer | de46e48 | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 6943 | else if (isa<LoadInst>(FirstInst)) |
Chris Lattner | 14f82c7 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 6944 | return new LoadInst(PhiVal, "", isVolatile); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 6945 | else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 6946 | return BinaryOperator::create(BinOp->getOpcode(), PhiVal, ConstantOp); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 6947 | else |
| 6948 | return new ShiftInst(cast<ShiftInst>(FirstInst)->getOpcode(), |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 6949 | PhiVal, ConstantOp); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 6950 | } |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 6951 | |
Chris Lattner | 7153643 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 6952 | /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle |
| 6953 | /// that is dead. |
| 6954 | static bool DeadPHICycle(PHINode *PN, std::set<PHINode*> &PotentiallyDeadPHIs) { |
| 6955 | if (PN->use_empty()) return true; |
| 6956 | if (!PN->hasOneUse()) return false; |
| 6957 | |
| 6958 | // Remember this node, and if we find the cycle, return. |
| 6959 | if (!PotentiallyDeadPHIs.insert(PN).second) |
| 6960 | return true; |
| 6961 | |
| 6962 | if (PHINode *PU = dyn_cast<PHINode>(PN->use_back())) |
| 6963 | return DeadPHICycle(PU, PotentiallyDeadPHIs); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6964 | |
Chris Lattner | 7153643 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 6965 | return false; |
| 6966 | } |
| 6967 | |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 6968 | // PHINode simplification |
| 6969 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 6970 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Owen Anderson | bbf8990 | 2006-07-10 22:15:25 +0000 | [diff] [blame] | 6971 | // If LCSSA is around, don't mess with Phi nodes |
| 6972 | if (mustPreserveAnalysisID(LCSSAID)) return 0; |
Owen Anderson | a6968f8 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 6973 | |
Owen Anderson | ae8aa64 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 6974 | if (Value *V = PN.hasConstantValue()) |
| 6975 | return ReplaceInstUsesWith(PN, V); |
| 6976 | |
| 6977 | // If the only user of this instruction is a cast instruction, and all of the |
| 6978 | // incoming values are constants, change this PHI to merge together the casted |
| 6979 | // constants. |
| 6980 | if (PN.hasOneUse()) |
| 6981 | if (CastInst *CI = dyn_cast<CastInst>(PN.use_back())) |
| 6982 | if (CI->getType() != PN.getType()) { // noop casts will be folded |
| 6983 | bool AllConstant = true; |
| 6984 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 6985 | if (!isa<Constant>(PN.getIncomingValue(i))) { |
| 6986 | AllConstant = false; |
| 6987 | break; |
| 6988 | } |
| 6989 | if (AllConstant) { |
| 6990 | // Make a new PHI with all casted values. |
| 6991 | PHINode *New = new PHINode(CI->getType(), PN.getName(), &PN); |
| 6992 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 6993 | Constant *OldArg = cast<Constant>(PN.getIncomingValue(i)); |
| 6994 | New->addIncoming(ConstantExpr::getCast(OldArg, New->getType()), |
| 6995 | PN.getIncomingBlock(i)); |
| 6996 | } |
| 6997 | |
| 6998 | // Update the cast instruction. |
| 6999 | CI->setOperand(0, New); |
| 7000 | WorkList.push_back(CI); // revisit the cast instruction to fold. |
| 7001 | WorkList.push_back(New); // Make sure to revisit the new Phi |
| 7002 | return &PN; // PN is now dead! |
| 7003 | } |
| 7004 | } |
| 7005 | |
| 7006 | // If all PHI operands are the same operation, pull them through the PHI, |
| 7007 | // reducing code size. |
| 7008 | if (isa<Instruction>(PN.getIncomingValue(0)) && |
| 7009 | PN.getIncomingValue(0)->hasOneUse()) |
| 7010 | if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) |
| 7011 | return Result; |
| 7012 | |
| 7013 | // If this is a trivial cycle in the PHI node graph, remove it. Basically, if |
| 7014 | // this PHI only has a single use (a PHI), and if that PHI only has one use (a |
| 7015 | // PHI)... break the cycle. |
| 7016 | if (PN.hasOneUse()) |
| 7017 | if (PHINode *PU = dyn_cast<PHINode>(PN.use_back())) { |
| 7018 | std::set<PHINode*> PotentiallyDeadPHIs; |
| 7019 | PotentiallyDeadPHIs.insert(&PN); |
| 7020 | if (DeadPHICycle(PU, PotentiallyDeadPHIs)) |
| 7021 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 7022 | } |
| 7023 | |
Chris Lattner | 91daeb5 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 7024 | return 0; |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 7025 | } |
| 7026 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7027 | static Value *InsertSignExtendToPtrTy(Value *V, const Type *DTy, |
| 7028 | Instruction *InsertPoint, |
| 7029 | InstCombiner *IC) { |
| 7030 | unsigned PS = IC->getTargetData().getPointerSize(); |
| 7031 | const Type *VTy = V->getType(); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7032 | if (!VTy->isSigned() && VTy->getPrimitiveSize() < PS) |
| 7033 | // We must insert a cast to ensure we sign-extend. |
Reid Spencer | 00c482b | 2006-10-26 19:19:06 +0000 | [diff] [blame] | 7034 | V = IC->InsertCastBefore(V, VTy->getSignedVersion(), *InsertPoint); |
| 7035 | return IC->InsertCastBefore(V, DTy, *InsertPoint); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7036 | } |
| 7037 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 7038 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 7039 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7040 | Value *PtrOp = GEP.getOperand(0); |
Chris Lattner | 471bd76 | 2003-05-22 19:07:21 +0000 | [diff] [blame] | 7041 | // Is it 'getelementptr %P, long 0' or 'getelementptr %P' |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 7042 | // If so, eliminate the noop. |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7043 | if (GEP.getNumOperands() == 1) |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7044 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7045 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7046 | if (isa<UndefValue>(GEP.getOperand(0))) |
| 7047 | return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType())); |
| 7048 | |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7049 | bool HasZeroPointerIndex = false; |
| 7050 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) |
| 7051 | HasZeroPointerIndex = C->isNullValue(); |
| 7052 | |
| 7053 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7054 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 7055 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7056 | // Eliminate unneeded casts for indices. |
| 7057 | bool MadeChange = false; |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 7058 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 7059 | for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI) |
| 7060 | if (isa<SequentialType>(*GTI)) { |
| 7061 | if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) { |
| 7062 | Value *Src = CI->getOperand(0); |
| 7063 | const Type *SrcTy = Src->getType(); |
| 7064 | const Type *DestTy = CI->getType(); |
| 7065 | if (Src->getType()->isInteger()) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 7066 | if (SrcTy->getPrimitiveSizeInBits() == |
| 7067 | DestTy->getPrimitiveSizeInBits()) { |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 7068 | // We can always eliminate a cast from ulong or long to the other. |
| 7069 | // We can always eliminate a cast from uint to int or the other on |
| 7070 | // 32-bit pointer platforms. |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 7071 | if (DestTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()){ |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 7072 | MadeChange = true; |
| 7073 | GEP.setOperand(i, Src); |
| 7074 | } |
| 7075 | } else if (SrcTy->getPrimitiveSize() < DestTy->getPrimitiveSize() && |
| 7076 | SrcTy->getPrimitiveSize() == 4) { |
| 7077 | // We can always eliminate a cast from int to [u]long. We can |
| 7078 | // eliminate a cast from uint to [u]long iff the target is a 32-bit |
| 7079 | // pointer target. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7080 | if (SrcTy->isSigned() || |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 7081 | SrcTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()) { |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 7082 | MadeChange = true; |
| 7083 | GEP.setOperand(i, Src); |
| 7084 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7085 | } |
| 7086 | } |
| 7087 | } |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 7088 | // If we are using a wider index than needed for this platform, shrink it |
| 7089 | // to what we need. If the incoming value needs a cast instruction, |
| 7090 | // insert it. This explicit cast can make subsequent optimizations more |
| 7091 | // obvious. |
| 7092 | Value *Op = GEP.getOperand(i); |
| 7093 | if (Op->getType()->getPrimitiveSize() > TD->getPointerSize()) |
Chris Lattner | 1e9ac1a | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 7094 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | 44d0b95 | 2004-07-20 01:48:15 +0000 | [diff] [blame] | 7095 | GEP.setOperand(i, ConstantExpr::getCast(C, |
| 7096 | TD->getIntPtrType()->getSignedVersion())); |
Chris Lattner | 1e9ac1a | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 7097 | MadeChange = true; |
| 7098 | } else { |
Reid Spencer | 00c482b | 2006-10-26 19:19:06 +0000 | [diff] [blame] | 7099 | Op = InsertCastBefore(Op, TD->getIntPtrType(), GEP); |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 7100 | GEP.setOperand(i, Op); |
| 7101 | MadeChange = true; |
| 7102 | } |
Chris Lattner | 44d0b95 | 2004-07-20 01:48:15 +0000 | [diff] [blame] | 7103 | |
| 7104 | // If this is a constant idx, make sure to canonicalize it to be a signed |
| 7105 | // operand, otherwise CSE and other optimizations are pessimized. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7106 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op)) |
| 7107 | if (CUI->getType()->isUnsigned()) { |
| 7108 | GEP.setOperand(i, |
| 7109 | ConstantExpr::getCast(CUI, CUI->getType()->getSignedVersion())); |
| 7110 | MadeChange = true; |
| 7111 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7112 | } |
| 7113 | if (MadeChange) return &GEP; |
| 7114 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 7115 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 7116 | // is a getelementptr instruction, combine the indices of the two |
| 7117 | // getelementptr instructions into a single instruction. |
| 7118 | // |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 7119 | std::vector<Value*> SrcGEPOperands; |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 7120 | if (User *Src = dyn_castGetElementPtr(PtrOp)) |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 7121 | SrcGEPOperands.assign(Src->op_begin(), Src->op_end()); |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 7122 | |
| 7123 | if (!SrcGEPOperands.empty()) { |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7124 | // Note that if our source is a gep chain itself that we wait for that |
| 7125 | // chain to be resolved before we perform this transformation. This |
| 7126 | // avoids us creating a TON of code in some cases. |
| 7127 | // |
| 7128 | if (isa<GetElementPtrInst>(SrcGEPOperands[0]) && |
| 7129 | cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2) |
| 7130 | return 0; // Wait until our source is folded to completion. |
| 7131 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 7132 | std::vector<Value *> Indices; |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7133 | |
| 7134 | // Find out whether the last index in the source GEP is a sequential idx. |
| 7135 | bool EndsWithSequential = false; |
| 7136 | for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)), |
| 7137 | E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I) |
Chris Lattner | 8ec5f88 | 2004-05-08 22:41:42 +0000 | [diff] [blame] | 7138 | EndsWithSequential = !isa<StructType>(*I); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7139 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 7140 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7141 | if (EndsWithSequential) { |
Chris Lattner | 235af56 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 7142 | // Replace: gep (gep %P, long B), long A, ... |
| 7143 | // With: T = long A+B; gep %P, T, ... |
| 7144 | // |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7145 | Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7146 | if (SO1 == Constant::getNullValue(SO1->getType())) { |
| 7147 | Sum = GO1; |
| 7148 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { |
| 7149 | Sum = SO1; |
| 7150 | } else { |
| 7151 | // If they aren't the same type, convert both to an integer of the |
| 7152 | // target's pointer size. |
| 7153 | if (SO1->getType() != GO1->getType()) { |
| 7154 | if (Constant *SO1C = dyn_cast<Constant>(SO1)) { |
| 7155 | SO1 = ConstantExpr::getCast(SO1C, GO1->getType()); |
| 7156 | } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) { |
| 7157 | GO1 = ConstantExpr::getCast(GO1C, SO1->getType()); |
| 7158 | } else { |
| 7159 | unsigned PS = TD->getPointerSize(); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7160 | if (SO1->getType()->getPrimitiveSize() == PS) { |
| 7161 | // Convert GO1 to SO1's type. |
| 7162 | GO1 = InsertSignExtendToPtrTy(GO1, SO1->getType(), &GEP, this); |
| 7163 | |
| 7164 | } else if (GO1->getType()->getPrimitiveSize() == PS) { |
| 7165 | // Convert SO1 to GO1's type. |
| 7166 | SO1 = InsertSignExtendToPtrTy(SO1, GO1->getType(), &GEP, this); |
| 7167 | } else { |
| 7168 | const Type *PT = TD->getIntPtrType(); |
| 7169 | SO1 = InsertSignExtendToPtrTy(SO1, PT, &GEP, this); |
| 7170 | GO1 = InsertSignExtendToPtrTy(GO1, PT, &GEP, this); |
| 7171 | } |
| 7172 | } |
| 7173 | } |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7174 | if (isa<Constant>(SO1) && isa<Constant>(GO1)) |
| 7175 | Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1)); |
| 7176 | else { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 7177 | Sum = BinaryOperator::createAdd(SO1, GO1, PtrOp->getName()+".sum"); |
| 7178 | InsertNewInstBefore(cast<Instruction>(Sum), GEP); |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7179 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7180 | } |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7181 | |
| 7182 | // Recycle the GEP we already have if possible. |
| 7183 | if (SrcGEPOperands.size() == 2) { |
| 7184 | GEP.setOperand(0, SrcGEPOperands[0]); |
| 7185 | GEP.setOperand(1, Sum); |
| 7186 | return &GEP; |
| 7187 | } else { |
| 7188 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 7189 | SrcGEPOperands.end()-1); |
| 7190 | Indices.push_back(Sum); |
| 7191 | Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end()); |
| 7192 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7193 | } else if (isa<Constant>(*GEP.idx_begin()) && |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7194 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7195 | SrcGEPOperands.size() != 1) { |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 7196 | // 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] | 7197 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 7198 | SrcGEPOperands.end()); |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 7199 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
| 7200 | } |
| 7201 | |
| 7202 | if (!Indices.empty()) |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 7203 | return new GetElementPtrInst(SrcGEPOperands[0], Indices, GEP.getName()); |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 7204 | |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7205 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) { |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 7206 | // GEP of global variable. If all of the indices for this GEP are |
| 7207 | // constants, we can promote this to a constexpr instead of an instruction. |
| 7208 | |
| 7209 | // Scan for nonconstants... |
| 7210 | std::vector<Constant*> Indices; |
| 7211 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
| 7212 | for (; I != E && isa<Constant>(*I); ++I) |
| 7213 | Indices.push_back(cast<Constant>(*I)); |
| 7214 | |
| 7215 | if (I == E) { // If they are all constants... |
Chris Lattner | f3edc49 | 2004-07-18 18:59:44 +0000 | [diff] [blame] | 7216 | Constant *CE = ConstantExpr::getGetElementPtr(GV, Indices); |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 7217 | |
| 7218 | // Replace all uses of the GEP with the new constexpr... |
| 7219 | return ReplaceInstUsesWith(GEP, CE); |
| 7220 | } |
Chris Lattner | 567b81f | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 7221 | } else if (Value *X = isCast(PtrOp)) { // Is the operand a cast? |
| 7222 | if (!isa<PointerType>(X->getType())) { |
| 7223 | // Not interesting. Source pointer must be a cast from pointer. |
| 7224 | } else if (HasZeroPointerIndex) { |
| 7225 | // transform: GEP (cast [10 x ubyte]* X to [0 x ubyte]*), long 0, ... |
| 7226 | // into : GEP [10 x ubyte]* X, long 0, ... |
| 7227 | // |
| 7228 | // This occurs when the program declares an array extern like "int X[];" |
| 7229 | // |
| 7230 | const PointerType *CPTy = cast<PointerType>(PtrOp->getType()); |
| 7231 | const PointerType *XTy = cast<PointerType>(X->getType()); |
| 7232 | if (const ArrayType *XATy = |
| 7233 | dyn_cast<ArrayType>(XTy->getElementType())) |
| 7234 | if (const ArrayType *CATy = |
| 7235 | dyn_cast<ArrayType>(CPTy->getElementType())) |
| 7236 | if (CATy->getElementType() == XATy->getElementType()) { |
| 7237 | // At this point, we know that the cast source type is a pointer |
| 7238 | // to an array of the same type as the destination pointer |
| 7239 | // array. Because the array type is never stepped over (there |
| 7240 | // is a leading zero) we can fold the cast into this GEP. |
| 7241 | GEP.setOperand(0, X); |
| 7242 | return &GEP; |
| 7243 | } |
| 7244 | } else if (GEP.getNumOperands() == 2) { |
| 7245 | // Transform things like: |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7246 | // %t = getelementptr ubyte* cast ([2 x int]* %str to uint*), uint %V |
| 7247 | // into: %t1 = getelementptr [2 x int*]* %str, int 0, uint %V; cast |
Chris Lattner | 567b81f | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 7248 | const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); |
| 7249 | const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType(); |
| 7250 | if (isa<ArrayType>(SrcElTy) && |
| 7251 | TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()) == |
| 7252 | TD->getTypeSize(ResElTy)) { |
| 7253 | Value *V = InsertNewInstBefore( |
| 7254 | new GetElementPtrInst(X, Constant::getNullValue(Type::IntTy), |
| 7255 | GEP.getOperand(1), GEP.getName()), GEP); |
| 7256 | return new CastInst(V, GEP.getType()); |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7257 | } |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7258 | |
| 7259 | // Transform things like: |
| 7260 | // getelementptr sbyte* cast ([100 x double]* X to sbyte*), int %tmp |
| 7261 | // (where tmp = 8*tmp2) into: |
| 7262 | // getelementptr [100 x double]* %arr, int 0, int %tmp.2 |
| 7263 | |
| 7264 | if (isa<ArrayType>(SrcElTy) && |
| 7265 | (ResElTy == Type::SByteTy || ResElTy == Type::UByteTy)) { |
| 7266 | uint64_t ArrayEltSize = |
| 7267 | TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()); |
| 7268 | |
| 7269 | // Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We |
| 7270 | // allow either a mul, shift, or constant here. |
| 7271 | Value *NewIdx = 0; |
| 7272 | ConstantInt *Scale = 0; |
| 7273 | if (ArrayEltSize == 1) { |
| 7274 | NewIdx = GEP.getOperand(1); |
| 7275 | Scale = ConstantInt::get(NewIdx->getType(), 1); |
| 7276 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) { |
Chris Lattner | a393e4d | 2005-09-14 17:32:56 +0000 | [diff] [blame] | 7277 | NewIdx = ConstantInt::get(CI->getType(), 1); |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7278 | Scale = CI; |
| 7279 | } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){ |
| 7280 | if (Inst->getOpcode() == Instruction::Shl && |
| 7281 | isa<ConstantInt>(Inst->getOperand(1))) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7282 | unsigned ShAmt = |
| 7283 | cast<ConstantInt>(Inst->getOperand(1))->getZExtValue(); |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7284 | if (Inst->getType()->isSigned()) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7285 | Scale = ConstantInt::get(Inst->getType(), 1ULL << ShAmt); |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7286 | else |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7287 | Scale = ConstantInt::get(Inst->getType(), 1ULL << ShAmt); |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7288 | NewIdx = Inst->getOperand(0); |
| 7289 | } else if (Inst->getOpcode() == Instruction::Mul && |
| 7290 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 7291 | Scale = cast<ConstantInt>(Inst->getOperand(1)); |
| 7292 | NewIdx = Inst->getOperand(0); |
| 7293 | } |
| 7294 | } |
| 7295 | |
| 7296 | // If the index will be to exactly the right offset with the scale taken |
| 7297 | // out, perform the transformation. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7298 | if (Scale && Scale->getZExtValue() % ArrayEltSize == 0) { |
Reid Spencer | de46e48 | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 7299 | if (isa<ConstantInt>(Scale)) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7300 | Scale = ConstantInt::get(Scale->getType(), |
| 7301 | Scale->getZExtValue() / ArrayEltSize); |
| 7302 | if (Scale->getZExtValue() != 1) { |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7303 | Constant *C = ConstantExpr::getCast(Scale, NewIdx->getType()); |
| 7304 | Instruction *Sc = BinaryOperator::createMul(NewIdx, C, "idxscale"); |
| 7305 | NewIdx = InsertNewInstBefore(Sc, GEP); |
| 7306 | } |
| 7307 | |
| 7308 | // Insert the new GEP instruction. |
| 7309 | Instruction *Idx = |
| 7310 | new GetElementPtrInst(X, Constant::getNullValue(Type::IntTy), |
| 7311 | NewIdx, GEP.getName()); |
| 7312 | Idx = InsertNewInstBefore(Idx, GEP); |
| 7313 | return new CastInst(Idx, GEP.getType()); |
| 7314 | } |
| 7315 | } |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7316 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 7317 | } |
| 7318 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 7319 | return 0; |
| 7320 | } |
| 7321 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 7322 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
| 7323 | // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1 |
| 7324 | if (AI.isArrayAllocation()) // Check C != 1 |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7325 | if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) { |
| 7326 | const Type *NewTy = |
| 7327 | ArrayType::get(AI.getAllocatedType(), C->getZExtValue()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 7328 | AllocationInst *New = 0; |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 7329 | |
| 7330 | // Create and insert the replacement instruction... |
| 7331 | if (isa<MallocInst>(AI)) |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 7332 | New = new MallocInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 7333 | else { |
| 7334 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 7335 | New = new AllocaInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 7336 | } |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 7337 | |
| 7338 | InsertNewInstBefore(New, AI); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7339 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 7340 | // Scan to the end of the allocation instructions, to skip over a block of |
| 7341 | // allocas if possible... |
| 7342 | // |
| 7343 | BasicBlock::iterator It = New; |
| 7344 | while (isa<AllocationInst>(*It)) ++It; |
| 7345 | |
| 7346 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 7347 | // insert our getelementptr instruction... |
| 7348 | // |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 7349 | Value *NullIdx = Constant::getNullValue(Type::IntTy); |
| 7350 | Value *V = new GetElementPtrInst(New, NullIdx, NullIdx, |
| 7351 | New->getName()+".sub", It); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 7352 | |
| 7353 | // Now make everything use the getelementptr instead of the original |
| 7354 | // allocation. |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 7355 | return ReplaceInstUsesWith(AI, V); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7356 | } else if (isa<UndefValue>(AI.getArraySize())) { |
| 7357 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 7358 | } |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 7359 | |
| 7360 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. |
| 7361 | // Note that we only do this for alloca's, because malloc should allocate and |
| 7362 | // return a unique pointer, even for a zero byte allocation. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7363 | if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() && |
Chris Lattner | 49df6ce | 2004-07-02 22:55:47 +0000 | [diff] [blame] | 7364 | TD->getTypeSize(AI.getAllocatedType()) == 0) |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 7365 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
| 7366 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 7367 | return 0; |
| 7368 | } |
| 7369 | |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 7370 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { |
| 7371 | Value *Op = FI.getOperand(0); |
| 7372 | |
| 7373 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X |
| 7374 | if (CastInst *CI = dyn_cast<CastInst>(Op)) |
| 7375 | if (isa<PointerType>(CI->getOperand(0)->getType())) { |
| 7376 | FI.setOperand(0, CI->getOperand(0)); |
| 7377 | return &FI; |
| 7378 | } |
| 7379 | |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 7380 | // free undef -> unreachable. |
| 7381 | if (isa<UndefValue>(Op)) { |
| 7382 | // Insert a new store to null because we cannot modify the CFG here. |
Chris Lattner | 6ab03f6 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 7383 | new StoreInst(ConstantBool::getTrue(), |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 7384 | UndefValue::get(PointerType::get(Type::BoolTy)), &FI); |
| 7385 | return EraseInstFromFunction(FI); |
| 7386 | } |
| 7387 | |
Chris Lattner | f3a3660 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 7388 | // If we have 'free null' delete the instruction. This can happen in stl code |
| 7389 | // when lots of inlining happens. |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 7390 | if (isa<ConstantPointerNull>(Op)) |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 7391 | return EraseInstFromFunction(FI); |
Chris Lattner | f3a3660 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 7392 | |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 7393 | return 0; |
| 7394 | } |
| 7395 | |
| 7396 | |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 7397 | /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible. |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 7398 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI) { |
| 7399 | User *CI = cast<User>(LI.getOperand(0)); |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 7400 | Value *CastOp = CI->getOperand(0); |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 7401 | |
| 7402 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 7403 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 7404 | const Type *SrcPTy = SrcTy->getElementType(); |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 7405 | |
Chris Lattner | ebca476 | 2006-04-02 05:37:12 +0000 | [diff] [blame] | 7406 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy) || |
| 7407 | isa<PackedType>(DestPTy)) { |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 7408 | // If the source is an array, the code below will not succeed. Check to |
| 7409 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 7410 | // constants. |
| 7411 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 7412 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 7413 | if (ASrcTy->getNumElements() != 0) { |
| 7414 | std::vector<Value*> Idxs(2, Constant::getNullValue(Type::IntTy)); |
| 7415 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs); |
| 7416 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 7417 | SrcPTy = SrcTy->getElementType(); |
| 7418 | } |
| 7419 | |
Chris Lattner | ebca476 | 2006-04-02 05:37:12 +0000 | [diff] [blame] | 7420 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy) || |
| 7421 | isa<PackedType>(SrcPTy)) && |
Chris Lattner | ecfa9b5 | 2005-03-29 06:37:47 +0000 | [diff] [blame] | 7422 | // Do not allow turning this into a load of an integer, which is then |
| 7423 | // casted to a pointer, this pessimizes pointer analysis a lot. |
| 7424 | (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7425 | IC.getTargetData().getTypeSize(SrcPTy) == |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 7426 | IC.getTargetData().getTypeSize(DestPTy)) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7427 | |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 7428 | // Okay, we are casting from one integer or pointer type to another of |
| 7429 | // the same size. Instead of casting the pointer before the load, cast |
| 7430 | // the result of the loaded value. |
| 7431 | Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp, |
| 7432 | CI->getName(), |
| 7433 | LI.isVolatile()),LI); |
| 7434 | // Now cast the result of the load. |
| 7435 | return new CastInst(NewLoad, LI.getType()); |
| 7436 | } |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 7437 | } |
| 7438 | } |
| 7439 | return 0; |
| 7440 | } |
| 7441 | |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 7442 | /// isSafeToLoadUnconditionally - Return true if we know that executing a load |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 7443 | /// from this value cannot trap. If it is not obviously safe to load from the |
| 7444 | /// specified pointer, we do a quick local scan of the basic block containing |
| 7445 | /// ScanFrom, to determine if the address is already accessed. |
| 7446 | static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) { |
| 7447 | // If it is an alloca or global variable, it is always safe to load from. |
| 7448 | if (isa<AllocaInst>(V) || isa<GlobalVariable>(V)) return true; |
| 7449 | |
| 7450 | // Otherwise, be a little bit agressive by scanning the local block where we |
| 7451 | // 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] | 7452 | // from/to. If so, the previous load or store would have already trapped, |
| 7453 | // so there is no harm doing an extra load (also, CSE will later eliminate |
| 7454 | // the load entirely). |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 7455 | BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); |
| 7456 | |
Alkis Evlogimenos | d59cebf | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 7457 | while (BBI != E) { |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 7458 | --BBI; |
| 7459 | |
| 7460 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 7461 | if (LI->getOperand(0) == V) return true; |
| 7462 | } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 7463 | if (SI->getOperand(1) == V) return true; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7464 | |
Alkis Evlogimenos | d59cebf | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 7465 | } |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 7466 | return false; |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 7467 | } |
| 7468 | |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 7469 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 7470 | Value *Op = LI.getOperand(0); |
Chris Lattner | 7e8af38 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 7471 | |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 7472 | // load (cast X) --> cast (load X) iff safe |
Reid Spencer | de46e48 | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 7473 | if (isa<CastInst>(Op)) |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 7474 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 7475 | return Res; |
| 7476 | |
| 7477 | // None of the following transforms are legal for volatile loads. |
| 7478 | if (LI.isVolatile()) return 0; |
Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 7479 | |
Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 7480 | if (&LI.getParent()->front() != &LI) { |
| 7481 | BasicBlock::iterator BBI = &LI; --BBI; |
Chris Lattner | e0bfdf1 | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 7482 | // If the instruction immediately before this is a store to the same |
| 7483 | // address, do a simple form of store->load forwarding. |
Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 7484 | if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 7485 | if (SI->getOperand(1) == LI.getOperand(0)) |
| 7486 | return ReplaceInstUsesWith(LI, SI->getOperand(0)); |
Chris Lattner | e0bfdf1 | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 7487 | if (LoadInst *LIB = dyn_cast<LoadInst>(BBI)) |
| 7488 | if (LIB->getOperand(0) == LI.getOperand(0)) |
| 7489 | return ReplaceInstUsesWith(LI, LIB); |
Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 7490 | } |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 7491 | |
| 7492 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) |
| 7493 | if (isa<ConstantPointerNull>(GEPI->getOperand(0)) || |
| 7494 | isa<UndefValue>(GEPI->getOperand(0))) { |
| 7495 | // Insert a new store to null instruction before the load to indicate |
| 7496 | // that this code is not reachable. We do this instead of inserting |
| 7497 | // an unreachable instruction directly because we cannot modify the |
| 7498 | // CFG. |
| 7499 | new StoreInst(UndefValue::get(LI.getType()), |
| 7500 | Constant::getNullValue(Op->getType()), &LI); |
| 7501 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 7502 | } |
| 7503 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7504 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 7505 | // load null/undef -> undef |
| 7506 | if ((C->isNullValue() || isa<UndefValue>(C))) { |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 7507 | // Insert a new store to null instruction before the load to indicate that |
| 7508 | // this code is not reachable. We do this instead of inserting an |
| 7509 | // unreachable instruction directly because we cannot modify the CFG. |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 7510 | new StoreInst(UndefValue::get(LI.getType()), |
| 7511 | Constant::getNullValue(Op->getType()), &LI); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7512 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 7513 | } |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 7514 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7515 | // Instcombine load (constant global) into the value loaded. |
| 7516 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) |
| 7517 | if (GV->isConstant() && !GV->isExternal()) |
| 7518 | return ReplaceInstUsesWith(LI, GV->getInitializer()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7519 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7520 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded. |
| 7521 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) |
| 7522 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 7523 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
| 7524 | if (GV->isConstant() && !GV->isExternal()) |
Chris Lattner | 0b011ec | 2005-09-26 05:28:06 +0000 | [diff] [blame] | 7525 | if (Constant *V = |
| 7526 | ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7527 | return ReplaceInstUsesWith(LI, V); |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 7528 | if (CE->getOperand(0)->isNullValue()) { |
| 7529 | // Insert a new store to null instruction before the load to indicate |
| 7530 | // that this code is not reachable. We do this instead of inserting |
| 7531 | // an unreachable instruction directly because we cannot modify the |
| 7532 | // CFG. |
| 7533 | new StoreInst(UndefValue::get(LI.getType()), |
| 7534 | Constant::getNullValue(Op->getType()), &LI); |
| 7535 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 7536 | } |
| 7537 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7538 | } else if (CE->getOpcode() == Instruction::Cast) { |
| 7539 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 7540 | return Res; |
| 7541 | } |
| 7542 | } |
Chris Lattner | e228ee5 | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 7543 | |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 7544 | if (Op->hasOneUse()) { |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 7545 | // Change select and PHI nodes to select values instead of addresses: this |
| 7546 | // helps alias analysis out a lot, allows many others simplifications, and |
| 7547 | // exposes redundancy in the code. |
| 7548 | // |
| 7549 | // Note that we cannot do the transformation unless we know that the |
| 7550 | // introduced loads cannot trap! Something like this is valid as long as |
| 7551 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 7552 | // but it would not be valid if we transformed it to load from null |
| 7553 | // unconditionally. |
| 7554 | // |
| 7555 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 7556 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 7557 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && |
| 7558 | isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 7559 | Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1), |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 7560 | SI->getOperand(1)->getName()+".val"), LI); |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 7561 | Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2), |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 7562 | SI->getOperand(2)->getName()+".val"), LI); |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 7563 | return new SelectInst(SI->getCondition(), V1, V2); |
| 7564 | } |
| 7565 | |
Chris Lattner | bdcf41a | 2004-09-23 15:46:00 +0000 | [diff] [blame] | 7566 | // load (select (cond, null, P)) -> load P |
| 7567 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) |
| 7568 | if (C->isNullValue()) { |
| 7569 | LI.setOperand(0, SI->getOperand(2)); |
| 7570 | return &LI; |
| 7571 | } |
| 7572 | |
| 7573 | // load (select (cond, P, null)) -> load P |
| 7574 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) |
| 7575 | if (C->isNullValue()) { |
| 7576 | LI.setOperand(0, SI->getOperand(1)); |
| 7577 | return &LI; |
| 7578 | } |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 7579 | } |
| 7580 | } |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 7581 | return 0; |
| 7582 | } |
| 7583 | |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 7584 | /// InstCombineStoreToCast - Fold 'store V, (cast P)' -> store (cast V), P' |
| 7585 | /// when possible. |
| 7586 | static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { |
| 7587 | User *CI = cast<User>(SI.getOperand(1)); |
| 7588 | Value *CastOp = CI->getOperand(0); |
| 7589 | |
| 7590 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
| 7591 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
| 7592 | const Type *SrcPTy = SrcTy->getElementType(); |
| 7593 | |
| 7594 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) { |
| 7595 | // If the source is an array, the code below will not succeed. Check to |
| 7596 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 7597 | // constants. |
| 7598 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 7599 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 7600 | if (ASrcTy->getNumElements() != 0) { |
| 7601 | std::vector<Value*> Idxs(2, Constant::getNullValue(Type::IntTy)); |
| 7602 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs); |
| 7603 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 7604 | SrcPTy = SrcTy->getElementType(); |
| 7605 | } |
| 7606 | |
| 7607 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7608 | IC.getTargetData().getTypeSize(SrcPTy) == |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 7609 | IC.getTargetData().getTypeSize(DestPTy)) { |
| 7610 | |
| 7611 | // Okay, we are casting from one integer or pointer type to another of |
| 7612 | // the same size. Instead of casting the pointer before the store, cast |
| 7613 | // the value to be stored. |
| 7614 | Value *NewCast; |
| 7615 | if (Constant *C = dyn_cast<Constant>(SI.getOperand(0))) |
| 7616 | NewCast = ConstantExpr::getCast(C, SrcPTy); |
| 7617 | else |
| 7618 | NewCast = IC.InsertNewInstBefore(new CastInst(SI.getOperand(0), |
| 7619 | SrcPTy, |
| 7620 | SI.getOperand(0)->getName()+".c"), SI); |
| 7621 | |
| 7622 | return new StoreInst(NewCast, CastOp); |
| 7623 | } |
| 7624 | } |
| 7625 | } |
| 7626 | return 0; |
| 7627 | } |
| 7628 | |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 7629 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
| 7630 | Value *Val = SI.getOperand(0); |
| 7631 | Value *Ptr = SI.getOperand(1); |
| 7632 | |
| 7633 | if (isa<UndefValue>(Ptr)) { // store X, undef -> noop (even if volatile) |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 7634 | EraseInstFromFunction(SI); |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 7635 | ++NumCombined; |
| 7636 | return 0; |
| 7637 | } |
| 7638 | |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 7639 | // Do really simple DSE, to catch cases where there are several consequtive |
| 7640 | // stores to the same location, separated by a few arithmetic operations. This |
| 7641 | // situation often occurs with bitfield accesses. |
| 7642 | BasicBlock::iterator BBI = &SI; |
| 7643 | for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts; |
| 7644 | --ScanInsts) { |
| 7645 | --BBI; |
| 7646 | |
| 7647 | if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) { |
| 7648 | // Prev store isn't volatile, and stores to the same location? |
| 7649 | if (!PrevSI->isVolatile() && PrevSI->getOperand(1) == SI.getOperand(1)) { |
| 7650 | ++NumDeadStore; |
| 7651 | ++BBI; |
| 7652 | EraseInstFromFunction(*PrevSI); |
| 7653 | continue; |
| 7654 | } |
| 7655 | break; |
| 7656 | } |
| 7657 | |
Chris Lattner | dab43b2 | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 7658 | // If this is a load, we have to stop. However, if the loaded value is from |
| 7659 | // the pointer we're loading and is producing the pointer we're storing, |
| 7660 | // then *this* store is dead (X = load P; store X -> P). |
| 7661 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 7662 | if (LI == Val && LI->getOperand(0) == Ptr) { |
| 7663 | EraseInstFromFunction(SI); |
| 7664 | ++NumCombined; |
| 7665 | return 0; |
| 7666 | } |
| 7667 | // Otherwise, this is a load from some other location. Stores before it |
| 7668 | // may not be dead. |
| 7669 | break; |
| 7670 | } |
| 7671 | |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 7672 | // Don't skip over loads or things that can modify memory. |
Chris Lattner | dab43b2 | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 7673 | if (BBI->mayWriteToMemory()) |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 7674 | break; |
| 7675 | } |
| 7676 | |
| 7677 | |
| 7678 | if (SI.isVolatile()) return 0; // Don't hack volatile stores. |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 7679 | |
| 7680 | // store X, null -> turns into 'unreachable' in SimplifyCFG |
| 7681 | if (isa<ConstantPointerNull>(Ptr)) { |
| 7682 | if (!isa<UndefValue>(Val)) { |
| 7683 | SI.setOperand(0, UndefValue::get(Val->getType())); |
| 7684 | if (Instruction *U = dyn_cast<Instruction>(Val)) |
| 7685 | WorkList.push_back(U); // Dropped a use. |
| 7686 | ++NumCombined; |
| 7687 | } |
| 7688 | return 0; // Do not modify these! |
| 7689 | } |
| 7690 | |
| 7691 | // store undef, Ptr -> noop |
| 7692 | if (isa<UndefValue>(Val)) { |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 7693 | EraseInstFromFunction(SI); |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 7694 | ++NumCombined; |
| 7695 | return 0; |
| 7696 | } |
| 7697 | |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 7698 | // If the pointer destination is a cast, see if we can fold the cast into the |
| 7699 | // source instead. |
Reid Spencer | de46e48 | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 7700 | if (isa<CastInst>(Ptr)) |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 7701 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 7702 | return Res; |
| 7703 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
| 7704 | if (CE->getOpcode() == Instruction::Cast) |
| 7705 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 7706 | return Res; |
| 7707 | |
Chris Lattner | 219175c | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 7708 | |
| 7709 | // If this store is the last instruction in the basic block, and if the block |
| 7710 | // 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] | 7711 | BBI = &SI; ++BBI; |
Chris Lattner | 219175c | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 7712 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) |
| 7713 | if (BI->isUnconditional()) { |
| 7714 | // Check to see if the successor block has exactly two incoming edges. If |
| 7715 | // so, see if the other predecessor contains a store to the same location. |
| 7716 | // if so, insert a PHI node (if needed) and move the stores down. |
| 7717 | BasicBlock *Dest = BI->getSuccessor(0); |
| 7718 | |
| 7719 | pred_iterator PI = pred_begin(Dest); |
| 7720 | BasicBlock *Other = 0; |
| 7721 | if (*PI != BI->getParent()) |
| 7722 | Other = *PI; |
| 7723 | ++PI; |
| 7724 | if (PI != pred_end(Dest)) { |
| 7725 | if (*PI != BI->getParent()) |
| 7726 | if (Other) |
| 7727 | Other = 0; |
| 7728 | else |
| 7729 | Other = *PI; |
| 7730 | if (++PI != pred_end(Dest)) |
| 7731 | Other = 0; |
| 7732 | } |
| 7733 | if (Other) { // If only one other pred... |
| 7734 | BBI = Other->getTerminator(); |
| 7735 | // Make sure this other block ends in an unconditional branch and that |
| 7736 | // there is an instruction before the branch. |
| 7737 | if (isa<BranchInst>(BBI) && cast<BranchInst>(BBI)->isUnconditional() && |
| 7738 | BBI != Other->begin()) { |
| 7739 | --BBI; |
| 7740 | StoreInst *OtherStore = dyn_cast<StoreInst>(BBI); |
| 7741 | |
| 7742 | // If this instruction is a store to the same location. |
| 7743 | if (OtherStore && OtherStore->getOperand(1) == SI.getOperand(1)) { |
| 7744 | // Okay, we know we can perform this transformation. Insert a PHI |
| 7745 | // node now if we need it. |
| 7746 | Value *MergedVal = OtherStore->getOperand(0); |
| 7747 | if (MergedVal != SI.getOperand(0)) { |
| 7748 | PHINode *PN = new PHINode(MergedVal->getType(), "storemerge"); |
| 7749 | PN->reserveOperandSpace(2); |
| 7750 | PN->addIncoming(SI.getOperand(0), SI.getParent()); |
| 7751 | PN->addIncoming(OtherStore->getOperand(0), Other); |
| 7752 | MergedVal = InsertNewInstBefore(PN, Dest->front()); |
| 7753 | } |
| 7754 | |
| 7755 | // Advance to a place where it is safe to insert the new store and |
| 7756 | // insert it. |
| 7757 | BBI = Dest->begin(); |
| 7758 | while (isa<PHINode>(BBI)) ++BBI; |
| 7759 | InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1), |
| 7760 | OtherStore->isVolatile()), *BBI); |
| 7761 | |
| 7762 | // Nuke the old stores. |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 7763 | EraseInstFromFunction(SI); |
| 7764 | EraseInstFromFunction(*OtherStore); |
Chris Lattner | 219175c | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 7765 | ++NumCombined; |
| 7766 | return 0; |
| 7767 | } |
| 7768 | } |
| 7769 | } |
| 7770 | } |
| 7771 | |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 7772 | return 0; |
| 7773 | } |
| 7774 | |
| 7775 | |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 7776 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 7777 | // 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] | 7778 | Value *X = 0; |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 7779 | BasicBlock *TrueDest; |
| 7780 | BasicBlock *FalseDest; |
| 7781 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && |
| 7782 | !isa<Constant>(X)) { |
| 7783 | // Swap Destinations and condition... |
| 7784 | BI.setCondition(X); |
| 7785 | BI.setSuccessor(0, FalseDest); |
| 7786 | BI.setSuccessor(1, TrueDest); |
| 7787 | return &BI; |
| 7788 | } |
| 7789 | |
| 7790 | // Cannonicalize setne -> seteq |
| 7791 | Instruction::BinaryOps Op; Value *Y; |
| 7792 | if (match(&BI, m_Br(m_SetCond(Op, m_Value(X), m_Value(Y)), |
| 7793 | TrueDest, FalseDest))) |
| 7794 | if ((Op == Instruction::SetNE || Op == Instruction::SetLE || |
| 7795 | Op == Instruction::SetGE) && BI.getCondition()->hasOneUse()) { |
| 7796 | SetCondInst *I = cast<SetCondInst>(BI.getCondition()); |
| 7797 | std::string Name = I->getName(); I->setName(""); |
| 7798 | Instruction::BinaryOps NewOpcode = SetCondInst::getInverseCondition(Op); |
| 7799 | Value *NewSCC = BinaryOperator::create(NewOpcode, X, Y, Name, I); |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 7800 | // Swap Destinations and condition... |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 7801 | BI.setCondition(NewSCC); |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 7802 | BI.setSuccessor(0, FalseDest); |
| 7803 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 7804 | removeFromWorkList(I); |
| 7805 | I->getParent()->getInstList().erase(I); |
| 7806 | WorkList.push_back(cast<Instruction>(NewSCC)); |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 7807 | return &BI; |
| 7808 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7809 | |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 7810 | return 0; |
| 7811 | } |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 7812 | |
Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 7813 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
| 7814 | Value *Cond = SI.getCondition(); |
| 7815 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { |
| 7816 | if (I->getOpcode() == Instruction::Add) |
| 7817 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 7818 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' |
| 7819 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7820 | SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), |
Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 7821 | AddRHS)); |
| 7822 | SI.setOperand(0, I->getOperand(0)); |
| 7823 | WorkList.push_back(I); |
| 7824 | return &SI; |
| 7825 | } |
| 7826 | } |
| 7827 | return 0; |
| 7828 | } |
| 7829 | |
Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 7830 | /// CheapToScalarize - Return true if the value is cheaper to scalarize than it |
| 7831 | /// is to leave as a vector operation. |
| 7832 | static bool CheapToScalarize(Value *V, bool isConstant) { |
| 7833 | if (isa<ConstantAggregateZero>(V)) |
| 7834 | return true; |
| 7835 | if (ConstantPacked *C = dyn_cast<ConstantPacked>(V)) { |
| 7836 | if (isConstant) return true; |
| 7837 | // If all elts are the same, we can extract. |
| 7838 | Constant *Op0 = C->getOperand(0); |
| 7839 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
| 7840 | if (C->getOperand(i) != Op0) |
| 7841 | return false; |
| 7842 | return true; |
| 7843 | } |
| 7844 | Instruction *I = dyn_cast<Instruction>(V); |
| 7845 | if (!I) return false; |
| 7846 | |
| 7847 | // Insert element gets simplified to the inserted element or is deleted if |
| 7848 | // this is constant idx extract element and its a constant idx insertelt. |
| 7849 | if (I->getOpcode() == Instruction::InsertElement && isConstant && |
| 7850 | isa<ConstantInt>(I->getOperand(2))) |
| 7851 | return true; |
| 7852 | if (I->getOpcode() == Instruction::Load && I->hasOneUse()) |
| 7853 | return true; |
| 7854 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) |
| 7855 | if (BO->hasOneUse() && |
| 7856 | (CheapToScalarize(BO->getOperand(0), isConstant) || |
| 7857 | CheapToScalarize(BO->getOperand(1), isConstant))) |
| 7858 | return true; |
| 7859 | |
| 7860 | return false; |
| 7861 | } |
| 7862 | |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 7863 | /// getShuffleMask - Read and decode a shufflevector mask. It turns undef |
| 7864 | /// elements into values that are larger than the #elts in the input. |
| 7865 | static std::vector<unsigned> getShuffleMask(const ShuffleVectorInst *SVI) { |
| 7866 | unsigned NElts = SVI->getType()->getNumElements(); |
| 7867 | if (isa<ConstantAggregateZero>(SVI->getOperand(2))) |
| 7868 | return std::vector<unsigned>(NElts, 0); |
| 7869 | if (isa<UndefValue>(SVI->getOperand(2))) |
| 7870 | return std::vector<unsigned>(NElts, 2*NElts); |
| 7871 | |
| 7872 | std::vector<unsigned> Result; |
| 7873 | const ConstantPacked *CP = cast<ConstantPacked>(SVI->getOperand(2)); |
| 7874 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 7875 | if (isa<UndefValue>(CP->getOperand(i))) |
| 7876 | Result.push_back(NElts*2); // undef -> 8 |
| 7877 | else |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7878 | Result.push_back(cast<ConstantInt>(CP->getOperand(i))->getZExtValue()); |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 7879 | return Result; |
| 7880 | } |
| 7881 | |
Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 7882 | /// FindScalarElement - Given a vector and an element number, see if the scalar |
| 7883 | /// value is already around as a register, for example if it were inserted then |
| 7884 | /// extracted from the vector. |
| 7885 | static Value *FindScalarElement(Value *V, unsigned EltNo) { |
| 7886 | assert(isa<PackedType>(V->getType()) && "Not looking at a vector?"); |
| 7887 | const PackedType *PTy = cast<PackedType>(V->getType()); |
Chris Lattner | 2d37f92 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 7888 | unsigned Width = PTy->getNumElements(); |
| 7889 | if (EltNo >= Width) // Out of range access. |
Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 7890 | return UndefValue::get(PTy->getElementType()); |
| 7891 | |
| 7892 | if (isa<UndefValue>(V)) |
| 7893 | return UndefValue::get(PTy->getElementType()); |
| 7894 | else if (isa<ConstantAggregateZero>(V)) |
| 7895 | return Constant::getNullValue(PTy->getElementType()); |
| 7896 | else if (ConstantPacked *CP = dyn_cast<ConstantPacked>(V)) |
| 7897 | return CP->getOperand(EltNo); |
| 7898 | else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) { |
| 7899 | // If this is an insert to a variable element, we don't know what it is. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7900 | if (!isa<ConstantInt>(III->getOperand(2))) |
| 7901 | return 0; |
| 7902 | unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue(); |
Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 7903 | |
| 7904 | // If this is an insert to the element we are looking for, return the |
| 7905 | // inserted value. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7906 | if (EltNo == IIElt) |
| 7907 | return III->getOperand(1); |
Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 7908 | |
| 7909 | // Otherwise, the insertelement doesn't modify the value, recurse on its |
| 7910 | // vector input. |
| 7911 | return FindScalarElement(III->getOperand(0), EltNo); |
Chris Lattner | 2d37f92 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 7912 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) { |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 7913 | unsigned InEl = getShuffleMask(SVI)[EltNo]; |
| 7914 | if (InEl < Width) |
| 7915 | return FindScalarElement(SVI->getOperand(0), InEl); |
| 7916 | else if (InEl < Width*2) |
| 7917 | return FindScalarElement(SVI->getOperand(1), InEl - Width); |
| 7918 | else |
| 7919 | return UndefValue::get(PTy->getElementType()); |
Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 7920 | } |
| 7921 | |
| 7922 | // Otherwise, we don't know. |
| 7923 | return 0; |
| 7924 | } |
| 7925 | |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 7926 | Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { |
Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 7927 | |
Chris Lattner | 92346c3 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 7928 | // If packed val is undef, replace extract with scalar undef. |
| 7929 | if (isa<UndefValue>(EI.getOperand(0))) |
| 7930 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
| 7931 | |
| 7932 | // If packed val is constant 0, replace extract with scalar 0. |
| 7933 | if (isa<ConstantAggregateZero>(EI.getOperand(0))) |
| 7934 | return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType())); |
| 7935 | |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 7936 | if (ConstantPacked *C = dyn_cast<ConstantPacked>(EI.getOperand(0))) { |
| 7937 | // If packed val is constant with uniform operands, replace EI |
| 7938 | // with that operand |
Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 7939 | Constant *op0 = C->getOperand(0); |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 7940 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 7941 | if (C->getOperand(i) != op0) { |
| 7942 | op0 = 0; |
| 7943 | break; |
| 7944 | } |
| 7945 | if (op0) |
| 7946 | return ReplaceInstUsesWith(EI, op0); |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 7947 | } |
Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 7948 | |
Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 7949 | // If extracting a specified index from the vector, see if we can recursively |
| 7950 | // find a previously computed scalar that was inserted into the vector. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7951 | if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 7952 | // This instruction only demands the single element from the input vector. |
| 7953 | // If the input vector has a single use, simplify it based on this use |
| 7954 | // property. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7955 | uint64_t IndexVal = IdxC->getZExtValue(); |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 7956 | if (EI.getOperand(0)->hasOneUse()) { |
| 7957 | uint64_t UndefElts; |
| 7958 | if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0), |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7959 | 1 << IndexVal, |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 7960 | UndefElts)) { |
| 7961 | EI.setOperand(0, V); |
| 7962 | return &EI; |
| 7963 | } |
| 7964 | } |
| 7965 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7966 | if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal)) |
Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 7967 | return ReplaceInstUsesWith(EI, Elt); |
Chris Lattner | 2d37f92 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 7968 | } |
Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 7969 | |
Chris Lattner | 83f6578 | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 7970 | if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) { |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 7971 | if (I->hasOneUse()) { |
| 7972 | // Push extractelement into predecessor operation if legal and |
| 7973 | // profitable to do so |
| 7974 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 7975 | bool isConstantElt = isa<ConstantInt>(EI.getOperand(1)); |
| 7976 | if (CheapToScalarize(BO, isConstantElt)) { |
| 7977 | ExtractElementInst *newEI0 = |
| 7978 | new ExtractElementInst(BO->getOperand(0), EI.getOperand(1), |
| 7979 | EI.getName()+".lhs"); |
| 7980 | ExtractElementInst *newEI1 = |
| 7981 | new ExtractElementInst(BO->getOperand(1), EI.getOperand(1), |
| 7982 | EI.getName()+".rhs"); |
| 7983 | InsertNewInstBefore(newEI0, EI); |
| 7984 | InsertNewInstBefore(newEI1, EI); |
| 7985 | return BinaryOperator::create(BO->getOpcode(), newEI0, newEI1); |
| 7986 | } |
Reid Spencer | de46e48 | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 7987 | } else if (isa<LoadInst>(I)) { |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 7988 | Value *Ptr = InsertCastBefore(I->getOperand(0), |
| 7989 | PointerType::get(EI.getType()), EI); |
| 7990 | GetElementPtrInst *GEP = |
| 7991 | new GetElementPtrInst(Ptr, EI.getOperand(1), |
| 7992 | I->getName() + ".gep"); |
| 7993 | InsertNewInstBefore(GEP, EI); |
| 7994 | return new LoadInst(GEP); |
Chris Lattner | 83f6578 | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 7995 | } |
| 7996 | } |
| 7997 | if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) { |
| 7998 | // Extracting the inserted element? |
| 7999 | if (IE->getOperand(2) == EI.getOperand(1)) |
| 8000 | return ReplaceInstUsesWith(EI, IE->getOperand(1)); |
| 8001 | // If the inserted and extracted elements are constants, they must not |
| 8002 | // be the same value, extract from the pre-inserted value instead. |
| 8003 | if (isa<Constant>(IE->getOperand(2)) && |
| 8004 | isa<Constant>(EI.getOperand(1))) { |
| 8005 | AddUsesToWorkList(EI); |
| 8006 | EI.setOperand(0, IE->getOperand(0)); |
| 8007 | return &EI; |
| 8008 | } |
| 8009 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) { |
| 8010 | // If this is extracting an element from a shufflevector, figure out where |
| 8011 | // it came from and extract from the appropriate input element instead. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8012 | if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
| 8013 | unsigned SrcIdx = getShuffleMask(SVI)[Elt->getZExtValue()]; |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8014 | Value *Src; |
| 8015 | if (SrcIdx < SVI->getType()->getNumElements()) |
| 8016 | Src = SVI->getOperand(0); |
| 8017 | else if (SrcIdx < SVI->getType()->getNumElements()*2) { |
| 8018 | SrcIdx -= SVI->getType()->getNumElements(); |
| 8019 | Src = SVI->getOperand(1); |
| 8020 | } else { |
| 8021 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
Chris Lattner | 612fa8e | 2006-03-30 22:02:40 +0000 | [diff] [blame] | 8022 | } |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8023 | return new ExtractElementInst(Src, SrcIdx); |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8024 | } |
| 8025 | } |
Chris Lattner | 83f6578 | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 8026 | } |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8027 | return 0; |
| 8028 | } |
| 8029 | |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8030 | /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns |
| 8031 | /// elements from either LHS or RHS, return the shuffle mask and true. |
| 8032 | /// Otherwise, return false. |
| 8033 | static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, |
| 8034 | std::vector<Constant*> &Mask) { |
| 8035 | assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() && |
| 8036 | "Invalid CollectSingleShuffleElements"); |
| 8037 | unsigned NumElts = cast<PackedType>(V->getType())->getNumElements(); |
| 8038 | |
| 8039 | if (isa<UndefValue>(V)) { |
| 8040 | Mask.assign(NumElts, UndefValue::get(Type::UIntTy)); |
| 8041 | return true; |
| 8042 | } else if (V == LHS) { |
| 8043 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8044 | Mask.push_back(ConstantInt::get(Type::UIntTy, i)); |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8045 | return true; |
| 8046 | } else if (V == RHS) { |
| 8047 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8048 | Mask.push_back(ConstantInt::get(Type::UIntTy, i+NumElts)); |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8049 | return true; |
| 8050 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 8051 | // If this is an insert of an extract from some other vector, include it. |
| 8052 | Value *VecOp = IEI->getOperand(0); |
| 8053 | Value *ScalarOp = IEI->getOperand(1); |
| 8054 | Value *IdxOp = IEI->getOperand(2); |
| 8055 | |
Chris Lattner | b6cb64b | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 8056 | if (!isa<ConstantInt>(IdxOp)) |
| 8057 | return false; |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8058 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | b6cb64b | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 8059 | |
| 8060 | if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector. |
| 8061 | // Okay, we can handle this if the vector we are insertinting into is |
| 8062 | // transitively ok. |
| 8063 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 8064 | // If so, update the mask to reflect the inserted undef. |
| 8065 | Mask[InsertedIdx] = UndefValue::get(Type::UIntTy); |
| 8066 | return true; |
| 8067 | } |
| 8068 | } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){ |
| 8069 | if (isa<ConstantInt>(EI->getOperand(1)) && |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8070 | EI->getOperand(0)->getType() == V->getType()) { |
| 8071 | unsigned ExtractedIdx = |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8072 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8073 | |
| 8074 | // This must be extracting from either LHS or RHS. |
| 8075 | if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) { |
| 8076 | // Okay, we can handle this if the vector we are insertinting into is |
| 8077 | // transitively ok. |
| 8078 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 8079 | // If so, update the mask to reflect the inserted value. |
| 8080 | if (EI->getOperand(0) == LHS) { |
| 8081 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8082 | ConstantInt::get(Type::UIntTy, ExtractedIdx); |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8083 | } else { |
| 8084 | assert(EI->getOperand(0) == RHS); |
| 8085 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8086 | ConstantInt::get(Type::UIntTy, ExtractedIdx+NumElts); |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8087 | |
| 8088 | } |
| 8089 | return true; |
| 8090 | } |
| 8091 | } |
| 8092 | } |
| 8093 | } |
| 8094 | } |
| 8095 | // TODO: Handle shufflevector here! |
| 8096 | |
| 8097 | return false; |
| 8098 | } |
| 8099 | |
| 8100 | /// CollectShuffleElements - We are building a shuffle of V, using RHS as the |
| 8101 | /// RHS of the shuffle instruction, if it is not null. Return a shuffle mask |
| 8102 | /// that computes V and the LHS value of the shuffle. |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8103 | static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask, |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8104 | Value *&RHS) { |
| 8105 | assert(isa<PackedType>(V->getType()) && |
| 8106 | (RHS == 0 || V->getType() == RHS->getType()) && |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8107 | "Invalid shuffle!"); |
| 8108 | unsigned NumElts = cast<PackedType>(V->getType())->getNumElements(); |
| 8109 | |
| 8110 | if (isa<UndefValue>(V)) { |
| 8111 | Mask.assign(NumElts, UndefValue::get(Type::UIntTy)); |
| 8112 | return V; |
| 8113 | } else if (isa<ConstantAggregateZero>(V)) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8114 | Mask.assign(NumElts, ConstantInt::get(Type::UIntTy, 0)); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8115 | return V; |
| 8116 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 8117 | // If this is an insert of an extract from some other vector, include it. |
| 8118 | Value *VecOp = IEI->getOperand(0); |
| 8119 | Value *ScalarOp = IEI->getOperand(1); |
| 8120 | Value *IdxOp = IEI->getOperand(2); |
| 8121 | |
| 8122 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 8123 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 8124 | EI->getOperand(0)->getType() == V->getType()) { |
| 8125 | unsigned ExtractedIdx = |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8126 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
| 8127 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8128 | |
| 8129 | // Either the extracted from or inserted into vector must be RHSVec, |
| 8130 | // otherwise we'd end up with a shuffle of three inputs. |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8131 | if (EI->getOperand(0) == RHS || RHS == 0) { |
| 8132 | RHS = EI->getOperand(0); |
| 8133 | Value *V = CollectShuffleElements(VecOp, Mask, RHS); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8134 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8135 | ConstantInt::get(Type::UIntTy, NumElts+ExtractedIdx); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8136 | return V; |
| 8137 | } |
| 8138 | |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8139 | if (VecOp == RHS) { |
| 8140 | Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8141 | // Everything but the extracted element is replaced with the RHS. |
| 8142 | for (unsigned i = 0; i != NumElts; ++i) { |
| 8143 | if (i != InsertedIdx) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8144 | Mask[i] = ConstantInt::get(Type::UIntTy, NumElts+i); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8145 | } |
| 8146 | return V; |
| 8147 | } |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8148 | |
| 8149 | // If this insertelement is a chain that comes from exactly these two |
| 8150 | // vectors, return the vector and the effective shuffle. |
| 8151 | if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask)) |
| 8152 | return EI->getOperand(0); |
| 8153 | |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8154 | } |
| 8155 | } |
| 8156 | } |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8157 | // TODO: Handle shufflevector here! |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8158 | |
| 8159 | // Otherwise, can't do anything fancy. Return an identity vector. |
| 8160 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8161 | Mask.push_back(ConstantInt::get(Type::UIntTy, i)); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8162 | return V; |
| 8163 | } |
| 8164 | |
| 8165 | Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { |
| 8166 | Value *VecOp = IE.getOperand(0); |
| 8167 | Value *ScalarOp = IE.getOperand(1); |
| 8168 | Value *IdxOp = IE.getOperand(2); |
| 8169 | |
| 8170 | // If the inserted element was extracted from some other vector, and if the |
| 8171 | // indexes are constant, try to turn this into a shufflevector operation. |
| 8172 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 8173 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 8174 | EI->getOperand(0)->getType() == IE.getType()) { |
| 8175 | unsigned NumVectorElts = IE.getType()->getNumElements(); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8176 | unsigned ExtractedIdx=cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
| 8177 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8178 | |
| 8179 | if (ExtractedIdx >= NumVectorElts) // Out of range extract. |
| 8180 | return ReplaceInstUsesWith(IE, VecOp); |
| 8181 | |
| 8182 | if (InsertedIdx >= NumVectorElts) // Out of range insert. |
| 8183 | return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType())); |
| 8184 | |
| 8185 | // If we are extracting a value from a vector, then inserting it right |
| 8186 | // back into the same place, just use the input vector. |
| 8187 | if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx) |
| 8188 | return ReplaceInstUsesWith(IE, VecOp); |
| 8189 | |
| 8190 | // We could theoretically do this for ANY input. However, doing so could |
| 8191 | // turn chains of insertelement instructions into a chain of shufflevector |
| 8192 | // instructions, and right now we do not merge shufflevectors. As such, |
| 8193 | // only do this in a situation where it is clear that there is benefit. |
| 8194 | if (isa<UndefValue>(VecOp) || isa<ConstantAggregateZero>(VecOp)) { |
| 8195 | // Turn this into shuffle(EIOp0, VecOp, Mask). The result has all of |
| 8196 | // the values of VecOp, except then one read from EIOp0. |
| 8197 | // Build a new shuffle mask. |
| 8198 | std::vector<Constant*> Mask; |
| 8199 | if (isa<UndefValue>(VecOp)) |
| 8200 | Mask.assign(NumVectorElts, UndefValue::get(Type::UIntTy)); |
| 8201 | else { |
| 8202 | assert(isa<ConstantAggregateZero>(VecOp) && "Unknown thing"); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8203 | Mask.assign(NumVectorElts, ConstantInt::get(Type::UIntTy, |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8204 | NumVectorElts)); |
| 8205 | } |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8206 | Mask[InsertedIdx] = ConstantInt::get(Type::UIntTy, ExtractedIdx); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8207 | return new ShuffleVectorInst(EI->getOperand(0), VecOp, |
| 8208 | ConstantPacked::get(Mask)); |
| 8209 | } |
| 8210 | |
| 8211 | // If this insertelement isn't used by some other insertelement, turn it |
| 8212 | // (and any insertelements it points to), into one big shuffle. |
| 8213 | if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) { |
| 8214 | std::vector<Constant*> Mask; |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8215 | Value *RHS = 0; |
| 8216 | Value *LHS = CollectShuffleElements(&IE, Mask, RHS); |
| 8217 | if (RHS == 0) RHS = UndefValue::get(LHS->getType()); |
| 8218 | // We now have a shuffle of LHS, RHS, Mask. |
| 8219 | return new ShuffleVectorInst(LHS, RHS, ConstantPacked::get(Mask)); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8220 | } |
| 8221 | } |
| 8222 | } |
| 8223 | |
| 8224 | return 0; |
| 8225 | } |
| 8226 | |
| 8227 | |
Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8228 | Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { |
| 8229 | Value *LHS = SVI.getOperand(0); |
| 8230 | Value *RHS = SVI.getOperand(1); |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8231 | std::vector<unsigned> Mask = getShuffleMask(&SVI); |
Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8232 | |
| 8233 | bool MadeChange = false; |
| 8234 | |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8235 | // Undefined shuffle mask -> undefined value. |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8236 | if (isa<UndefValue>(SVI.getOperand(2))) |
Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8237 | return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType())); |
| 8238 | |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8239 | // TODO: If we have shuffle(x, undef, mask) and any elements of mask refer to |
| 8240 | // the undef, change them to undefs. |
| 8241 | |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8242 | // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask') |
| 8243 | // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask'). |
| 8244 | if (LHS == RHS || isa<UndefValue>(LHS)) { |
| 8245 | if (isa<UndefValue>(LHS) && LHS == RHS) { |
Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8246 | // shuffle(undef,undef,mask) -> undef. |
| 8247 | return ReplaceInstUsesWith(SVI, LHS); |
| 8248 | } |
| 8249 | |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8250 | // Remap any references to RHS to use LHS. |
| 8251 | std::vector<Constant*> Elts; |
| 8252 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 8253 | if (Mask[i] >= 2*e) |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8254 | Elts.push_back(UndefValue::get(Type::UIntTy)); |
Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 8255 | else { |
| 8256 | if ((Mask[i] >= e && isa<UndefValue>(RHS)) || |
| 8257 | (Mask[i] < e && isa<UndefValue>(LHS))) |
| 8258 | Mask[i] = 2*e; // Turn into undef. |
| 8259 | else |
| 8260 | Mask[i] &= (e-1); // Force to LHS. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8261 | Elts.push_back(ConstantInt::get(Type::UIntTy, Mask[i])); |
Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 8262 | } |
Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8263 | } |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8264 | SVI.setOperand(0, SVI.getOperand(1)); |
Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8265 | SVI.setOperand(1, UndefValue::get(RHS->getType())); |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8266 | SVI.setOperand(2, ConstantPacked::get(Elts)); |
Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 8267 | LHS = SVI.getOperand(0); |
| 8268 | RHS = SVI.getOperand(1); |
Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8269 | MadeChange = true; |
| 8270 | } |
| 8271 | |
Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 8272 | // Analyze the shuffle, are the LHS or RHS and identity shuffles? |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8273 | bool isLHSID = true, isRHSID = true; |
Chris Lattner | 34cebe7 | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 8274 | |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8275 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 8276 | if (Mask[i] >= e*2) continue; // Ignore undef values. |
| 8277 | // Is this an identity shuffle of the LHS value? |
| 8278 | isLHSID &= (Mask[i] == i); |
| 8279 | |
| 8280 | // Is this an identity shuffle of the RHS value? |
| 8281 | isRHSID &= (Mask[i]-e == i); |
Chris Lattner | 34cebe7 | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 8282 | } |
Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8283 | |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8284 | // Eliminate identity shuffles. |
| 8285 | if (isLHSID) return ReplaceInstUsesWith(SVI, LHS); |
| 8286 | if (isRHSID) return ReplaceInstUsesWith(SVI, RHS); |
Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8287 | |
Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 8288 | // If the LHS is a shufflevector itself, see if we can combine it with this |
| 8289 | // one without producing an unusual shuffle. Here we are really conservative: |
| 8290 | // we are absolutely afraid of producing a shuffle mask not in the input |
| 8291 | // program, because the code gen may not be smart enough to turn a merged |
| 8292 | // shuffle into two specific shuffles: it may produce worse code. As such, |
| 8293 | // we only merge two shuffles if the result is one of the two input shuffle |
| 8294 | // masks. In this case, merging the shuffles just removes one instruction, |
| 8295 | // which we know is safe. This is good for things like turning: |
| 8296 | // (splat(splat)) -> splat. |
| 8297 | if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) { |
| 8298 | if (isa<UndefValue>(RHS)) { |
| 8299 | std::vector<unsigned> LHSMask = getShuffleMask(LHSSVI); |
| 8300 | |
| 8301 | std::vector<unsigned> NewMask; |
| 8302 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) |
| 8303 | if (Mask[i] >= 2*e) |
| 8304 | NewMask.push_back(2*e); |
| 8305 | else |
| 8306 | NewMask.push_back(LHSMask[Mask[i]]); |
| 8307 | |
| 8308 | // If the result mask is equal to the src shuffle or this shuffle mask, do |
| 8309 | // the replacement. |
| 8310 | if (NewMask == LHSMask || NewMask == Mask) { |
| 8311 | std::vector<Constant*> Elts; |
| 8312 | for (unsigned i = 0, e = NewMask.size(); i != e; ++i) { |
| 8313 | if (NewMask[i] >= e*2) { |
| 8314 | Elts.push_back(UndefValue::get(Type::UIntTy)); |
| 8315 | } else { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8316 | Elts.push_back(ConstantInt::get(Type::UIntTy, NewMask[i])); |
Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 8317 | } |
| 8318 | } |
| 8319 | return new ShuffleVectorInst(LHSSVI->getOperand(0), |
| 8320 | LHSSVI->getOperand(1), |
| 8321 | ConstantPacked::get(Elts)); |
| 8322 | } |
| 8323 | } |
| 8324 | } |
| 8325 | |
Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8326 | return MadeChange ? &SVI : 0; |
| 8327 | } |
| 8328 | |
| 8329 | |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8330 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 8331 | void InstCombiner::removeFromWorkList(Instruction *I) { |
| 8332 | WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I), |
| 8333 | WorkList.end()); |
| 8334 | } |
| 8335 | |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 8336 | |
| 8337 | /// TryToSinkInstruction - Try to move the specified instruction from its |
| 8338 | /// current block into the beginning of DestBlock, which can only happen if it's |
| 8339 | /// safe to move the instruction past all of the instructions between it and the |
| 8340 | /// end of its block. |
| 8341 | static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { |
| 8342 | assert(I->hasOneUse() && "Invariants didn't hold!"); |
| 8343 | |
Chris Lattner | c4f67e6 | 2005-10-27 17:13:11 +0000 | [diff] [blame] | 8344 | // Cannot move control-flow-involving, volatile loads, vaarg, etc. |
| 8345 | if (isa<PHINode>(I) || I->mayWriteToMemory()) return false; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8346 | |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 8347 | // Do not sink alloca instructions out of the entry block. |
| 8348 | if (isa<AllocaInst>(I) && I->getParent() == &DestBlock->getParent()->front()) |
| 8349 | return false; |
| 8350 | |
Chris Lattner | f17a2fb | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 8351 | // We can only sink load instructions if there is nothing between the load and |
| 8352 | // the end of block that could change the value. |
| 8353 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Chris Lattner | f17a2fb | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 8354 | for (BasicBlock::iterator Scan = LI, E = LI->getParent()->end(); |
| 8355 | Scan != E; ++Scan) |
| 8356 | if (Scan->mayWriteToMemory()) |
| 8357 | return false; |
Chris Lattner | f17a2fb | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 8358 | } |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 8359 | |
| 8360 | BasicBlock::iterator InsertPos = DestBlock->begin(); |
| 8361 | while (isa<PHINode>(InsertPos)) ++InsertPos; |
| 8362 | |
Chris Lattner | 9f269e4 | 2005-08-08 19:11:57 +0000 | [diff] [blame] | 8363 | I->moveBefore(InsertPos); |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 8364 | ++NumSunkInst; |
| 8365 | return true; |
| 8366 | } |
| 8367 | |
Chris Lattner | 1443bc5 | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 8368 | /// OptimizeConstantExpr - Given a constant expression and target data layout |
| 8369 | /// information, symbolically evaluation the constant expr to something simpler |
| 8370 | /// if possible. |
| 8371 | static Constant *OptimizeConstantExpr(ConstantExpr *CE, const TargetData *TD) { |
| 8372 | if (!TD) return CE; |
| 8373 | |
| 8374 | Constant *Ptr = CE->getOperand(0); |
| 8375 | if (CE->getOpcode() == Instruction::GetElementPtr && Ptr->isNullValue() && |
| 8376 | cast<PointerType>(Ptr->getType())->getElementType()->isSized()) { |
| 8377 | // If this is a constant expr gep that is effectively computing an |
| 8378 | // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12' |
| 8379 | bool isFoldableGEP = true; |
| 8380 | for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) |
| 8381 | if (!isa<ConstantInt>(CE->getOperand(i))) |
| 8382 | isFoldableGEP = false; |
| 8383 | if (isFoldableGEP) { |
| 8384 | std::vector<Value*> Ops(CE->op_begin()+1, CE->op_end()); |
| 8385 | uint64_t Offset = TD->getIndexedOffset(Ptr->getType(), Ops); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8386 | Constant *C = ConstantInt::get(Type::ULongTy, Offset); |
Chris Lattner | 1443bc5 | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 8387 | C = ConstantExpr::getCast(C, TD->getIntPtrType()); |
| 8388 | return ConstantExpr::getCast(C, CE->getType()); |
| 8389 | } |
| 8390 | } |
| 8391 | |
| 8392 | return CE; |
| 8393 | } |
| 8394 | |
Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 8395 | |
| 8396 | /// AddReachableCodeToWorklist - Walk the function in depth-first order, adding |
| 8397 | /// all reachable code to the worklist. |
| 8398 | /// |
| 8399 | /// This has a couple of tricks to make the code faster and more powerful. In |
| 8400 | /// particular, we constant fold and DCE instructions as we go, to avoid adding |
| 8401 | /// them to the worklist (this significantly speeds up instcombine on code where |
| 8402 | /// many instructions are dead or constant). Additionally, if we find a branch |
| 8403 | /// whose condition is a known constant, we only visit the reachable successors. |
| 8404 | /// |
| 8405 | static void AddReachableCodeToWorklist(BasicBlock *BB, |
| 8406 | std::set<BasicBlock*> &Visited, |
Chris Lattner | 1443bc5 | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 8407 | std::vector<Instruction*> &WorkList, |
| 8408 | const TargetData *TD) { |
Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 8409 | // We have now visited this block! If we've already been here, bail out. |
| 8410 | if (!Visited.insert(BB).second) return; |
| 8411 | |
| 8412 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { |
| 8413 | Instruction *Inst = BBI++; |
| 8414 | |
| 8415 | // DCE instruction if trivially dead. |
| 8416 | if (isInstructionTriviallyDead(Inst)) { |
| 8417 | ++NumDeadInst; |
| 8418 | DEBUG(std::cerr << "IC: DCE: " << *Inst); |
| 8419 | Inst->eraseFromParent(); |
| 8420 | continue; |
| 8421 | } |
| 8422 | |
| 8423 | // ConstantProp instruction if trivially constant. |
| 8424 | if (Constant *C = ConstantFoldInstruction(Inst)) { |
Chris Lattner | 1443bc5 | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 8425 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) |
| 8426 | C = OptimizeConstantExpr(CE, TD); |
Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 8427 | DEBUG(std::cerr << "IC: ConstFold to: " << *C << " from: " << *Inst); |
| 8428 | Inst->replaceAllUsesWith(C); |
| 8429 | ++NumConstProp; |
| 8430 | Inst->eraseFromParent(); |
| 8431 | continue; |
| 8432 | } |
| 8433 | |
| 8434 | WorkList.push_back(Inst); |
| 8435 | } |
| 8436 | |
| 8437 | // Recursively visit successors. If this is a branch or switch on a constant, |
| 8438 | // only visit the reachable successor. |
| 8439 | TerminatorInst *TI = BB->getTerminator(); |
| 8440 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
| 8441 | if (BI->isConditional() && isa<ConstantBool>(BI->getCondition())) { |
| 8442 | bool CondVal = cast<ConstantBool>(BI->getCondition())->getValue(); |
Chris Lattner | 1443bc5 | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 8443 | AddReachableCodeToWorklist(BI->getSuccessor(!CondVal), Visited, WorkList, |
| 8444 | TD); |
Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 8445 | return; |
| 8446 | } |
| 8447 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 8448 | if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) { |
| 8449 | // See if this is an explicit destination. |
| 8450 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) |
| 8451 | if (SI->getCaseValue(i) == Cond) { |
Chris Lattner | 1443bc5 | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 8452 | AddReachableCodeToWorklist(SI->getSuccessor(i), Visited, WorkList,TD); |
Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 8453 | return; |
| 8454 | } |
| 8455 | |
| 8456 | // Otherwise it is the default destination. |
Chris Lattner | 1443bc5 | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 8457 | AddReachableCodeToWorklist(SI->getSuccessor(0), Visited, WorkList, TD); |
Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 8458 | return; |
| 8459 | } |
| 8460 | } |
| 8461 | |
| 8462 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
Chris Lattner | 1443bc5 | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 8463 | AddReachableCodeToWorklist(TI->getSuccessor(i), Visited, WorkList, TD); |
Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 8464 | } |
| 8465 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 8466 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 8467 | bool Changed = false; |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 8468 | TD = &getAnalysis<TargetData>(); |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 8469 | |
Chris Lattner | 4ed40f7 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 8470 | { |
Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 8471 | // Do a depth-first traversal of the function, populate the worklist with |
| 8472 | // the reachable instructions. Ignore blocks that are not reachable. Keep |
| 8473 | // track of which blocks we visit. |
Chris Lattner | 4ed40f7 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 8474 | std::set<BasicBlock*> Visited; |
Chris Lattner | 1443bc5 | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 8475 | AddReachableCodeToWorklist(F.begin(), Visited, WorkList, TD); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 8476 | |
Chris Lattner | 4ed40f7 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 8477 | // Do a quick scan over the function. If we find any blocks that are |
| 8478 | // unreachable, remove any instructions inside of them. This prevents |
| 8479 | // the instcombine code from having to deal with some bad special cases. |
| 8480 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 8481 | if (!Visited.count(BB)) { |
| 8482 | Instruction *Term = BB->getTerminator(); |
| 8483 | while (Term != BB->begin()) { // Remove instrs bottom-up |
| 8484 | BasicBlock::iterator I = Term; --I; |
Chris Lattner | 2d3a7a6 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 8485 | |
Chris Lattner | 4ed40f7 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 8486 | DEBUG(std::cerr << "IC: DCE: " << *I); |
| 8487 | ++NumDeadInst; |
| 8488 | |
| 8489 | if (!I->use_empty()) |
| 8490 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 8491 | I->eraseFromParent(); |
| 8492 | } |
| 8493 | } |
| 8494 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 8495 | |
| 8496 | while (!WorkList.empty()) { |
| 8497 | Instruction *I = WorkList.back(); // Get an instruction from the worklist |
| 8498 | WorkList.pop_back(); |
| 8499 | |
Chris Lattner | 1443bc5 | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 8500 | // Check to see if we can DCE the instruction. |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 8501 | if (isInstructionTriviallyDead(I)) { |
Chris Lattner | 1443bc5 | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 8502 | // Add operands to the worklist. |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 8503 | if (I->getNumOperands() < 4) |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8504 | AddUsesToWorkList(*I); |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 8505 | ++NumDeadInst; |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 8506 | |
Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 8507 | DEBUG(std::cerr << "IC: DCE: " << *I); |
| 8508 | |
| 8509 | I->eraseFromParent(); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 8510 | removeFromWorkList(I); |
| 8511 | continue; |
| 8512 | } |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 8513 | |
Chris Lattner | 1443bc5 | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 8514 | // Instruction isn't dead, see if we can constant propagate it. |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 8515 | if (Constant *C = ConstantFoldInstruction(I)) { |
Chris Lattner | 1443bc5 | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 8516 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) |
| 8517 | C = OptimizeConstantExpr(CE, TD); |
Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 8518 | DEBUG(std::cerr << "IC: ConstFold to: " << *C << " from: " << *I); |
| 8519 | |
Chris Lattner | 1443bc5 | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 8520 | // Add operands to the worklist. |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8521 | AddUsesToWorkList(*I); |
Chris Lattner | c6509f4 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 8522 | ReplaceInstUsesWith(*I, C); |
| 8523 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 8524 | ++NumConstProp; |
Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 8525 | I->eraseFromParent(); |
Chris Lattner | 800aaaf | 2003-10-07 15:17:02 +0000 | [diff] [blame] | 8526 | removeFromWorkList(I); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 8527 | continue; |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 8528 | } |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 8529 | |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 8530 | // See if we can trivially sink this instruction to a successor basic block. |
| 8531 | if (I->hasOneUse()) { |
| 8532 | BasicBlock *BB = I->getParent(); |
| 8533 | BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent(); |
| 8534 | if (UserParent != BB) { |
| 8535 | bool UserIsSuccessor = false; |
| 8536 | // See if the user is one of our successors. |
| 8537 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) |
| 8538 | if (*SI == UserParent) { |
| 8539 | UserIsSuccessor = true; |
| 8540 | break; |
| 8541 | } |
| 8542 | |
| 8543 | // If the user is one of our immediate successors, and if that successor |
| 8544 | // only has us as a predecessors (we'd have to split the critical edge |
| 8545 | // otherwise), we can keep going. |
| 8546 | if (UserIsSuccessor && !isa<PHINode>(I->use_back()) && |
| 8547 | next(pred_begin(UserParent)) == pred_end(UserParent)) |
| 8548 | // Okay, the CFG is simple enough, try to sink this instruction. |
| 8549 | Changed |= TryToSinkInstruction(I, UserParent); |
| 8550 | } |
| 8551 | } |
| 8552 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 8553 | // Now that we have an instruction, try combining it to simplify it... |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 8554 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 8555 | ++NumCombined; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 8556 | // Should we replace the old instruction with a new one? |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 8557 | if (Result != I) { |
Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 8558 | DEBUG(std::cerr << "IC: Old = " << *I |
| 8559 | << " New = " << *Result); |
| 8560 | |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 8561 | // Everything uses the new instruction now. |
| 8562 | I->replaceAllUsesWith(Result); |
| 8563 | |
| 8564 | // Push the new instruction and any users onto the worklist. |
| 8565 | WorkList.push_back(Result); |
| 8566 | AddUsersToWorkList(*Result); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 8567 | |
| 8568 | // Move the name to the new instruction first... |
| 8569 | std::string OldName = I->getName(); I->setName(""); |
Chris Lattner | 950fc78 | 2003-10-07 22:58:41 +0000 | [diff] [blame] | 8570 | Result->setName(OldName); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 8571 | |
| 8572 | // Insert the new instruction into the basic block... |
| 8573 | BasicBlock *InstParent = I->getParent(); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8574 | BasicBlock::iterator InsertPos = I; |
| 8575 | |
| 8576 | if (!isa<PHINode>(Result)) // If combining a PHI, don't insert |
| 8577 | while (isa<PHINode>(InsertPos)) // middle of a block of PHIs. |
| 8578 | ++InsertPos; |
| 8579 | |
| 8580 | InstParent->getInstList().insert(InsertPos, Result); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 8581 | |
Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 8582 | // Make sure that we reprocess all operands now that we reduced their |
| 8583 | // use counts. |
Chris Lattner | b643a9e | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 8584 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 8585 | if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i))) |
| 8586 | WorkList.push_back(OpI); |
| 8587 | |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 8588 | // Instructions can end up on the worklist more than once. Make sure |
| 8589 | // we do not process an instruction that has been deleted. |
| 8590 | removeFromWorkList(I); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 8591 | |
| 8592 | // Erase the old instruction. |
| 8593 | InstParent->getInstList().erase(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 8594 | } else { |
Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 8595 | DEBUG(std::cerr << "IC: MOD = " << *I); |
| 8596 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 8597 | // If the instruction was modified, it's possible that it is now dead. |
| 8598 | // if so, remove it. |
Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 8599 | if (isInstructionTriviallyDead(I)) { |
| 8600 | // Make sure we process all operands now that we are reducing their |
| 8601 | // use counts. |
| 8602 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 8603 | if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i))) |
| 8604 | WorkList.push_back(OpI); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8605 | |
Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 8606 | // Instructions may end up in the worklist more than once. Erase all |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8607 | // occurrences of this instruction. |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 8608 | removeFromWorkList(I); |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8609 | I->eraseFromParent(); |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 8610 | } else { |
| 8611 | WorkList.push_back(Result); |
| 8612 | AddUsersToWorkList(*Result); |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 8613 | } |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 8614 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 8615 | Changed = true; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 8616 | } |
| 8617 | } |
| 8618 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 8619 | return Changed; |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 8620 | } |
| 8621 | |
Brian Gaeke | 38b79e8 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 8622 | FunctionPass *llvm::createInstructionCombiningPass() { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 8623 | return new InstCombiner(); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 8624 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 8625 | |