Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1 | //===- InstructionCombining.cpp - Combine multiple instructions -----------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9 | // |
| 10 | // InstructionCombining - Combine instructions to form fewer, simple |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 11 | // instructions. This pass does not modify the CFG. This pass is where |
| 12 | // algebraic simplification happens. |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 13 | // |
| 14 | // This pass combines things like: |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 15 | // %Y = add i32 %X, 1 |
| 16 | // %Z = add i32 %Y, 1 |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 17 | // into: |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 18 | // %Z = add i32 %X, 2 |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 19 | // |
| 20 | // This is a simple worklist driven algorithm. |
| 21 | // |
Chris Lattner | 065a616 | 2003-09-10 05:29:43 +0000 | [diff] [blame] | 22 | // This pass guarantees that the following canonicalizations are performed on |
Chris Lattner | 2cd9196 | 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 | df17af1 | 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. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 27 | // 3. Compare instructions are converted from <,>,<=,>= to ==,!= if possible |
| 28 | // 4. All cmp instructions on boolean values are replaced with logical ops |
Chris Lattner | e92d2f4 | 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 | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 32 | // ... etc. |
Chris Lattner | 2cd9196 | 2003-07-23 21:41:57 +0000 | [diff] [blame] | 33 | // |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
| 35 | |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 36 | #define DEBUG_TYPE "instcombine" |
Chris Lattner | 022103b | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 37 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 38 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 39 | #include "llvm/Pass.h" |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 40 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 41 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 79066fa | 2007-01-30 23:46:24 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/ConstantFolding.h" |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetData.h" |
| 44 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 45 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 46 | #include "llvm/Support/CallSite.h" |
Nick Lewycky | 5be2920 | 2008-02-03 16:33:09 +0000 | [diff] [blame] | 47 | #include "llvm/Support/ConstantRange.h" |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Debug.h" |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 49 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 50 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 51 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 52 | #include "llvm/Support/PatternMatch.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 53 | #include "llvm/Support/Compiler.h" |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 54 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 55 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/SmallPtrSet.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 57 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 58 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 59 | #include <algorithm> |
Torok Edwin | 3eaee31 | 2008-04-20 08:33:11 +0000 | [diff] [blame] | 60 | #include <climits> |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 61 | #include <sstream> |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 62 | using namespace llvm; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 63 | using namespace llvm::PatternMatch; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 65 | STATISTIC(NumCombined , "Number of insts combined"); |
| 66 | STATISTIC(NumConstProp, "Number of constant folds"); |
| 67 | STATISTIC(NumDeadInst , "Number of dead inst eliminated"); |
| 68 | STATISTIC(NumDeadStore, "Number of dead stores eliminated"); |
| 69 | STATISTIC(NumSunkInst , "Number of instructions sunk"); |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 71 | namespace { |
Chris Lattner | f4b5461 | 2006-06-28 22:08:15 +0000 | [diff] [blame] | 72 | class VISIBILITY_HIDDEN InstCombiner |
| 73 | : public FunctionPass, |
| 74 | public InstVisitor<InstCombiner, Instruction*> { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 75 | // Worklist of all of the instructions that need to be simplified. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 76 | std::vector<Instruction*> Worklist; |
| 77 | DenseMap<Instruction*, unsigned> WorklistMap; |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 78 | TargetData *TD; |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 79 | bool MustPreserveLCSSA; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 80 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 81 | static char ID; // Pass identification, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 82 | InstCombiner() : FunctionPass((intptr_t)&ID) {} |
| 83 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 84 | /// AddToWorkList - Add the specified instruction to the worklist if it |
| 85 | /// isn't already in it. |
| 86 | void AddToWorkList(Instruction *I) { |
| 87 | if (WorklistMap.insert(std::make_pair(I, Worklist.size()))) |
| 88 | Worklist.push_back(I); |
| 89 | } |
| 90 | |
| 91 | // RemoveFromWorkList - remove I from the worklist if it exists. |
| 92 | void RemoveFromWorkList(Instruction *I) { |
| 93 | DenseMap<Instruction*, unsigned>::iterator It = WorklistMap.find(I); |
| 94 | if (It == WorklistMap.end()) return; // Not in worklist. |
| 95 | |
| 96 | // Don't bother moving everything down, just null out the slot. |
| 97 | Worklist[It->second] = 0; |
| 98 | |
| 99 | WorklistMap.erase(It); |
| 100 | } |
| 101 | |
| 102 | Instruction *RemoveOneFromWorkList() { |
| 103 | Instruction *I = Worklist.back(); |
| 104 | Worklist.pop_back(); |
| 105 | WorklistMap.erase(I); |
| 106 | return I; |
| 107 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 108 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 110 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
| 111 | /// the instruction to the work lists because they might get more simplified |
| 112 | /// now. |
| 113 | /// |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 114 | void AddUsersToWorkList(Value &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 115 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 116 | UI != UE; ++UI) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 117 | AddToWorkList(cast<Instruction>(*UI)); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 120 | /// AddUsesToWorkList - When an instruction is simplified, add operands to |
| 121 | /// the work lists because they might get more simplified now. |
| 122 | /// |
| 123 | void AddUsesToWorkList(Instruction &I) { |
| 124 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 125 | if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 126 | AddToWorkList(Op); |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 127 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 128 | |
| 129 | /// AddSoonDeadInstToWorklist - The specified instruction is about to become |
| 130 | /// dead. Add all of its operands to the worklist, turning them into |
| 131 | /// undef's to reduce the number of uses of those instructions. |
| 132 | /// |
| 133 | /// Return the specified operand before it is turned into an undef. |
| 134 | /// |
| 135 | Value *AddSoonDeadInstToWorklist(Instruction &I, unsigned op) { |
| 136 | Value *R = I.getOperand(op); |
| 137 | |
| 138 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 139 | if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) { |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 140 | AddToWorkList(Op); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 141 | // Set the operand to undef to drop the use. |
| 142 | I.setOperand(i, UndefValue::get(Op->getType())); |
| 143 | } |
| 144 | |
| 145 | return R; |
| 146 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 147 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 148 | public: |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 149 | virtual bool runOnFunction(Function &F); |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 150 | |
| 151 | bool DoOneIteration(Function &F, unsigned ItNum); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 152 | |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 153 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 154 | AU.addRequired<TargetData>(); |
Owen Anderson | d1b78a1 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 155 | AU.addPreservedID(LCSSAID); |
Chris Lattner | cb2610e | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 156 | AU.setPreservesCFG(); |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 159 | TargetData &getTargetData() const { return *TD; } |
| 160 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 161 | // Visitation implementation - Implement instruction combining for different |
| 162 | // instruction types. The semantics are as follows: |
| 163 | // Return Value: |
| 164 | // null - No change was made |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 165 | // I - Change was made, I is still valid, I may be dead though |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 166 | // otherwise - Change was made, replace I with returned instruction |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 167 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 168 | Instruction *visitAdd(BinaryOperator &I); |
| 169 | Instruction *visitSub(BinaryOperator &I); |
| 170 | Instruction *visitMul(BinaryOperator &I); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 171 | Instruction *visitURem(BinaryOperator &I); |
| 172 | Instruction *visitSRem(BinaryOperator &I); |
| 173 | Instruction *visitFRem(BinaryOperator &I); |
| 174 | Instruction *commonRemTransforms(BinaryOperator &I); |
| 175 | Instruction *commonIRemTransforms(BinaryOperator &I); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 176 | Instruction *commonDivTransforms(BinaryOperator &I); |
| 177 | Instruction *commonIDivTransforms(BinaryOperator &I); |
| 178 | Instruction *visitUDiv(BinaryOperator &I); |
| 179 | Instruction *visitSDiv(BinaryOperator &I); |
| 180 | Instruction *visitFDiv(BinaryOperator &I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 181 | Instruction *visitAnd(BinaryOperator &I); |
| 182 | Instruction *visitOr (BinaryOperator &I); |
| 183 | Instruction *visitXor(BinaryOperator &I); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 184 | Instruction *visitShl(BinaryOperator &I); |
| 185 | Instruction *visitAShr(BinaryOperator &I); |
| 186 | Instruction *visitLShr(BinaryOperator &I); |
| 187 | Instruction *commonShiftTransforms(BinaryOperator &I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 188 | Instruction *visitFCmpInst(FCmpInst &I); |
| 189 | Instruction *visitICmpInst(ICmpInst &I); |
| 190 | Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 191 | Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI, |
| 192 | Instruction *LHS, |
| 193 | ConstantInt *RHS); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 194 | Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, |
| 195 | ConstantInt *DivRHS); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 196 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 197 | Instruction *FoldGEPICmp(User *GEPLHS, Value *RHS, |
| 198 | ICmpInst::Predicate Cond, Instruction &I); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 199 | Instruction *FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 200 | BinaryOperator &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 201 | Instruction *commonCastTransforms(CastInst &CI); |
| 202 | Instruction *commonIntCastTransforms(CastInst &CI); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 203 | Instruction *commonPointerCastTransforms(CastInst &CI); |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 204 | Instruction *visitTrunc(TruncInst &CI); |
| 205 | Instruction *visitZExt(ZExtInst &CI); |
| 206 | Instruction *visitSExt(SExtInst &CI); |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 207 | Instruction *visitFPTrunc(FPTruncInst &CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 208 | Instruction *visitFPExt(CastInst &CI); |
| 209 | Instruction *visitFPToUI(CastInst &CI); |
| 210 | Instruction *visitFPToSI(CastInst &CI); |
| 211 | Instruction *visitUIToFP(CastInst &CI); |
| 212 | Instruction *visitSIToFP(CastInst &CI); |
| 213 | Instruction *visitPtrToInt(CastInst &CI); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 214 | Instruction *visitIntToPtr(IntToPtrInst &CI); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 215 | Instruction *visitBitCast(BitCastInst &CI); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 216 | Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 217 | Instruction *FI); |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 218 | Instruction *visitSelectInst(SelectInst &CI); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 219 | Instruction *visitCallInst(CallInst &CI); |
| 220 | Instruction *visitInvokeInst(InvokeInst &II); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 221 | Instruction *visitPHINode(PHINode &PN); |
| 222 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 223 | Instruction *visitAllocationInst(AllocationInst &AI); |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 224 | Instruction *visitFreeInst(FreeInst &FI); |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 225 | Instruction *visitLoadInst(LoadInst &LI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 226 | Instruction *visitStoreInst(StoreInst &SI); |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 227 | Instruction *visitBranchInst(BranchInst &BI); |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 228 | Instruction *visitSwitchInst(SwitchInst &SI); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 229 | Instruction *visitInsertElementInst(InsertElementInst &IE); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 230 | Instruction *visitExtractElementInst(ExtractElementInst &EI); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 231 | Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 232 | |
| 233 | // visitInstruction - Specify what to return for unhandled instructions... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 234 | Instruction *visitInstruction(Instruction &I) { return 0; } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 236 | private: |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 237 | Instruction *visitCallSite(CallSite CS); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 238 | bool transformConstExprCastCall(CallSite CS); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 239 | Instruction *transformCallThroughTrampoline(CallSite CS); |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 240 | Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI, |
| 241 | bool DoXform = true); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 243 | public: |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 244 | // InsertNewInstBefore - insert an instruction New before instruction Old |
| 245 | // in the program. Add the new instruction to the worklist. |
| 246 | // |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 247 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Chris Lattner | e6f9a91 | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 248 | assert(New && New->getParent() == 0 && |
| 249 | "New instruction already inserted into a basic block!"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 250 | BasicBlock *BB = Old.getParent(); |
| 251 | BB->getInstList().insert(&Old, New); // Insert inst |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 252 | AddToWorkList(New); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 253 | return New; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 256 | /// InsertCastBefore - Insert a cast of V to TY before the instruction POS. |
| 257 | /// This also adds the cast to the worklist. Finally, this returns the |
| 258 | /// cast. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 259 | Value *InsertCastBefore(Instruction::CastOps opc, Value *V, const Type *Ty, |
| 260 | Instruction &Pos) { |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 261 | if (V->getType() == Ty) return V; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 262 | |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 263 | if (Constant *CV = dyn_cast<Constant>(V)) |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 264 | return ConstantExpr::getCast(opc, CV, Ty); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 265 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 266 | Instruction *C = CastInst::Create(opc, V, Ty, V->getName(), &Pos); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 267 | AddToWorkList(C); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 268 | return C; |
| 269 | } |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 270 | |
| 271 | Value *InsertBitCastBefore(Value *V, const Type *Ty, Instruction &Pos) { |
| 272 | return InsertCastBefore(Instruction::BitCast, V, Ty, Pos); |
| 273 | } |
| 274 | |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 276 | // ReplaceInstUsesWith - This method is to be used when an instruction is |
| 277 | // found to be dead, replacable with another preexisting expression. Here |
| 278 | // we add all uses of I to the worklist, replace all uses of I with the new |
| 279 | // value, then return I, so that the inst combiner will know that I was |
| 280 | // modified. |
| 281 | // |
| 282 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 283 | AddUsersToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 284 | if (&I != V) { |
| 285 | I.replaceAllUsesWith(V); |
| 286 | return &I; |
| 287 | } else { |
| 288 | // If we are replacing the instruction with itself, this must be in a |
| 289 | // segment of unreachable code, so just clobber the instruction. |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 290 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 291 | return &I; |
| 292 | } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 293 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 294 | |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 295 | // UpdateValueUsesWith - This method is to be used when an value is |
| 296 | // found to be replacable with another preexisting expression or was |
| 297 | // updated. Here we add all uses of I to the worklist, replace all uses of |
| 298 | // I with the new value (unless the instruction was just updated), then |
| 299 | // return true, so that the inst combiner will know that I was modified. |
| 300 | // |
| 301 | bool UpdateValueUsesWith(Value *Old, Value *New) { |
| 302 | AddUsersToWorkList(*Old); // Add all modified instrs to worklist |
| 303 | if (Old != New) |
| 304 | Old->replaceAllUsesWith(New); |
| 305 | if (Instruction *I = dyn_cast<Instruction>(Old)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 306 | AddToWorkList(I); |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 307 | if (Instruction *I = dyn_cast<Instruction>(New)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 308 | AddToWorkList(I); |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 309 | return true; |
| 310 | } |
| 311 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 312 | // EraseInstFromFunction - When dealing with an instruction that has side |
| 313 | // effects or produces a void value, we can't rely on DCE to delete the |
| 314 | // instruction. Instead, visit methods should return the value returned by |
| 315 | // this function. |
| 316 | Instruction *EraseInstFromFunction(Instruction &I) { |
| 317 | assert(I.use_empty() && "Cannot erase instruction that is used!"); |
| 318 | AddUsesToWorkList(I); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 319 | RemoveFromWorkList(&I); |
Chris Lattner | 954f66a | 2004-11-18 21:41:39 +0000 | [diff] [blame] | 320 | I.eraseFromParent(); |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 321 | return 0; // Don't do anything with FI |
| 322 | } |
| 323 | |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 324 | private: |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 325 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 326 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 327 | /// casts that are known to not do anything... |
| 328 | /// |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 329 | Value *InsertOperandCastBefore(Instruction::CastOps opcode, |
| 330 | Value *V, const Type *DestTy, |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 331 | Instruction *InsertBefore); |
| 332 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 333 | /// SimplifyCommutative - This performs a few simplifications for |
| 334 | /// commutative operators. |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 335 | bool SimplifyCommutative(BinaryOperator &I); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 336 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 337 | /// SimplifyCompare - This reorders the operands of a CmpInst to get them in |
| 338 | /// most-complex to least-complex order. |
| 339 | bool SimplifyCompare(CmpInst &I); |
| 340 | |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 341 | /// SimplifyDemandedBits - Attempts to replace V with a simpler value based |
| 342 | /// on the demanded bits. |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 343 | bool SimplifyDemandedBits(Value *V, APInt DemandedMask, |
| 344 | APInt& KnownZero, APInt& KnownOne, |
| 345 | unsigned Depth = 0); |
| 346 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 347 | Value *SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, |
| 348 | uint64_t &UndefElts, unsigned Depth = 0); |
| 349 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 350 | // FoldOpIntoPhi - Given a binary operator or cast instruction which has a |
| 351 | // PHI node as operand #0, see if we can fold the instruction into the PHI |
| 352 | // (which is only possible if all operands to the PHI are constants). |
| 353 | Instruction *FoldOpIntoPhi(Instruction &I); |
| 354 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 355 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 356 | // operator and they all are only used by the PHI, PHI together their |
| 357 | // inputs, and do the operation once, to the result of the PHI. |
| 358 | Instruction *FoldPHIArgOpIntoPHI(PHINode &PN); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 359 | Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN); |
| 360 | |
| 361 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 362 | Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS, |
| 363 | ConstantInt *AndRHS, BinaryOperator &TheAnd); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 364 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 365 | Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask, |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 366 | bool isSub, Instruction &I); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 367 | Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 368 | bool isSigned, bool Inside, Instruction &IB); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 369 | Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocationInst &AI); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 370 | Instruction *MatchBSwap(BinaryOperator &I); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 371 | bool SimplifyStoreAtEndOfBlock(StoreInst &SI); |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 372 | Instruction *SimplifyMemTransfer(MemIntrinsic *MI); |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 373 | Instruction *SimplifyMemSet(MemSetInst *MI); |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 374 | |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 375 | |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 376 | Value *EvaluateInDifferentType(Value *V, const Type *Ty, bool isSigned); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 377 | |
| 378 | void ComputeMaskedBits(Value *V, const APInt &Mask, APInt& KnownZero, |
| 379 | APInt& KnownOne, unsigned Depth = 0); |
| 380 | bool MaskedValueIsZero(Value *V, const APInt& Mask, unsigned Depth = 0); |
| 381 | bool CanEvaluateInDifferentType(Value *V, const IntegerType *Ty, |
| 382 | unsigned CastOpc, |
| 383 | int &NumCastsRemoved); |
| 384 | unsigned GetOrEnforceKnownAlignment(Value *V, |
| 385 | unsigned PrefAlign = 0); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 386 | }; |
| 387 | } |
| 388 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 389 | char InstCombiner::ID = 0; |
| 390 | static RegisterPass<InstCombiner> |
| 391 | X("instcombine", "Combine redundant instructions"); |
| 392 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 393 | // getComplexity: Assign a complexity or rank value to LLVM Values... |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 394 | // 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 395 | static unsigned getComplexity(Value *V) { |
| 396 | if (isa<Instruction>(V)) { |
| 397 | if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 398 | return 3; |
| 399 | return 4; |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 400 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 401 | if (isa<Argument>(V)) return 3; |
| 402 | return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2; |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 403 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 404 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 405 | // isOnlyUse - Return true if this instruction will be deleted if we stop using |
| 406 | // it. |
| 407 | static bool isOnlyUse(Value *V) { |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 408 | return V->hasOneUse() || isa<Constant>(V); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 411 | // getPromotedType - Return the specified type promoted as it would be to pass |
| 412 | // though a va_arg area... |
| 413 | static const Type *getPromotedType(const Type *Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 414 | if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) { |
| 415 | if (ITy->getBitWidth() < 32) |
| 416 | return Type::Int32Ty; |
Chris Lattner | 2b7e0ad | 2007-05-23 01:17:04 +0000 | [diff] [blame] | 417 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 418 | return Ty; |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 421 | /// getBitCastOperand - If the specified operand is a CastInst or a constant |
| 422 | /// expression bitcast, return the operand value, otherwise return null. |
| 423 | static Value *getBitCastOperand(Value *V) { |
| 424 | if (BitCastInst *I = dyn_cast<BitCastInst>(V)) |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 425 | return I->getOperand(0); |
| 426 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 427 | if (CE->getOpcode() == Instruction::BitCast) |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 428 | return CE->getOperand(0); |
| 429 | return 0; |
| 430 | } |
| 431 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 432 | /// This function is a wrapper around CastInst::isEliminableCastPair. It |
| 433 | /// simply extracts arguments and returns what that function returns. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 434 | static Instruction::CastOps |
| 435 | isEliminableCastPair( |
| 436 | const CastInst *CI, ///< The first cast instruction |
| 437 | unsigned opcode, ///< The opcode of the second cast instruction |
| 438 | const Type *DstTy, ///< The target type for the second cast instruction |
| 439 | TargetData *TD ///< The target data for pointer size |
| 440 | ) { |
| 441 | |
| 442 | const Type *SrcTy = CI->getOperand(0)->getType(); // A from above |
| 443 | const Type *MidTy = CI->getType(); // B from above |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 444 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 445 | // Get the opcodes of the two Cast instructions |
| 446 | Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode()); |
| 447 | Instruction::CastOps secondOp = Instruction::CastOps(opcode); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 448 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 449 | return Instruction::CastOps( |
| 450 | CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, |
| 451 | DstTy, TD->getIntPtrType())); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /// ValueRequiresCast - Return true if the cast from "V to Ty" actually results |
| 455 | /// in any code being generated. It does not require codegen if V is simple |
| 456 | /// enough or if the cast can be folded into other casts. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 457 | static bool ValueRequiresCast(Instruction::CastOps opcode, const Value *V, |
| 458 | const Type *Ty, TargetData *TD) { |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 459 | if (V->getType() == Ty || isa<Constant>(V)) return false; |
| 460 | |
Chris Lattner | 01575b7 | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 461 | // If this is another cast that can be eliminated, it isn't codegen either. |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 462 | if (const CastInst *CI = dyn_cast<CastInst>(V)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 463 | if (isEliminableCastPair(CI, opcode, Ty, TD)) |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 464 | return false; |
| 465 | return true; |
| 466 | } |
| 467 | |
| 468 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 469 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 470 | /// casts that are known to not do anything... |
| 471 | /// |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 472 | Value *InstCombiner::InsertOperandCastBefore(Instruction::CastOps opcode, |
| 473 | Value *V, const Type *DestTy, |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 474 | Instruction *InsertBefore) { |
| 475 | if (V->getType() == DestTy) return V; |
| 476 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 477 | return ConstantExpr::getCast(opcode, C, DestTy); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 478 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 479 | return InsertCastBefore(opcode, V, DestTy, *InsertBefore); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 482 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 483 | // operators: |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 484 | // |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 485 | // 1. Order operands such that they are listed from right (least complex) to |
| 486 | // left (most complex). This puts constants before unary operators before |
| 487 | // binary operators. |
| 488 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 489 | // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) |
| 490 | // 3. Transform: (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2)) |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 491 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 492 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 493 | bool Changed = false; |
| 494 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) |
| 495 | Changed = !I.swapOperands(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 496 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 497 | if (!I.isAssociative()) return Changed; |
| 498 | Instruction::BinaryOps Opcode = I.getOpcode(); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 499 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) |
| 500 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { |
| 501 | if (isa<Constant>(I.getOperand(1))) { |
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 502 | Constant *Folded = ConstantExpr::get(I.getOpcode(), |
| 503 | cast<Constant>(I.getOperand(1)), |
| 504 | cast<Constant>(Op->getOperand(1))); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 505 | I.setOperand(0, Op->getOperand(0)); |
| 506 | I.setOperand(1, Folded); |
| 507 | return true; |
| 508 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) |
| 509 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && |
| 510 | isOnlyUse(Op) && isOnlyUse(Op1)) { |
| 511 | Constant *C1 = cast<Constant>(Op->getOperand(1)); |
| 512 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); |
| 513 | |
| 514 | // Fold (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2)) |
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 515 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 516 | Instruction *New = BinaryOperator::Create(Opcode, Op->getOperand(0), |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 517 | Op1->getOperand(0), |
| 518 | Op1->getName(), &I); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 519 | AddToWorkList(New); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 520 | I.setOperand(0, New); |
| 521 | I.setOperand(1, Folded); |
| 522 | return true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 523 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 524 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 525 | return Changed; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 526 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 527 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 528 | /// SimplifyCompare - For a CmpInst this function just orders the operands |
| 529 | /// so that theyare listed from right (least complex) to left (most complex). |
| 530 | /// This puts constants before unary operators before binary operators. |
| 531 | bool InstCombiner::SimplifyCompare(CmpInst &I) { |
| 532 | if (getComplexity(I.getOperand(0)) >= getComplexity(I.getOperand(1))) |
| 533 | return false; |
| 534 | I.swapOperands(); |
| 535 | // Compare instructions are not associative so there's nothing else we can do. |
| 536 | return true; |
| 537 | } |
| 538 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 539 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
| 540 | // if the LHS is a constant zero (which is the 'negate' form). |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 541 | // |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 542 | static inline Value *dyn_castNegVal(Value *V) { |
| 543 | if (BinaryOperator::isNeg(V)) |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 544 | return BinaryOperator::getNegArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 545 | |
Chris Lattner | 0ce8580 | 2004-12-14 20:08:06 +0000 | [diff] [blame] | 546 | // Constants can be considered to be negated values if they can be folded. |
| 547 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
| 548 | return ConstantExpr::getNeg(C); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 549 | return 0; |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 552 | static inline Value *dyn_castNotVal(Value *V) { |
| 553 | if (BinaryOperator::isNot(V)) |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 554 | return BinaryOperator::getNotArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 555 | |
| 556 | // Constants can be considered to be not'ed values... |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 557 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 558 | return ConstantInt::get(~C->getValue()); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 559 | return 0; |
| 560 | } |
| 561 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 562 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
| 563 | // other computations (because it has a constant operand), return the |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 564 | // non-constant operand of the multiply, and set CST to point to the multiplier. |
| 565 | // Otherwise, return null. |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 566 | // |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 567 | static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 568 | if (V->hasOneUse() && V->getType()->isInteger()) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 569 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 570 | if (I->getOpcode() == Instruction::Mul) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 571 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 572 | return I->getOperand(0); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 573 | if (I->getOpcode() == Instruction::Shl) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 574 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) { |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 575 | // The multiplier is really 1 << CST. |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 576 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 577 | uint32_t CSTVal = CST->getLimitedValue(BitWidth); |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 578 | CST = ConstantInt::get(APInt(BitWidth, 1).shl(CSTVal)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 579 | return I->getOperand(0); |
| 580 | } |
| 581 | } |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 582 | return 0; |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 583 | } |
Chris Lattner | af2930e | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 584 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 585 | /// dyn_castGetElementPtr - If this is a getelementptr instruction or constant |
| 586 | /// expression, return it. |
| 587 | static User *dyn_castGetElementPtr(Value *V) { |
| 588 | if (isa<GetElementPtrInst>(V)) return cast<User>(V); |
| 589 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 590 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 591 | return cast<User>(V); |
| 592 | return false; |
| 593 | } |
| 594 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 595 | /// getOpcode - If this is an Instruction or a ConstantExpr, return the |
| 596 | /// opcode value. Otherwise return UserOp1. |
| 597 | static unsigned getOpcode(User *U) { |
| 598 | if (Instruction *I = dyn_cast<Instruction>(U)) |
| 599 | return I->getOpcode(); |
| 600 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) |
| 601 | return CE->getOpcode(); |
| 602 | // Use UserOp1 to mean there's no opcode. |
| 603 | return Instruction::UserOp1; |
| 604 | } |
| 605 | |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 606 | /// AddOne - Add one to a ConstantInt |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 607 | static ConstantInt *AddOne(ConstantInt *C) { |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 608 | APInt Val(C->getValue()); |
| 609 | return ConstantInt::get(++Val); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 610 | } |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 611 | /// SubOne - Subtract one from a ConstantInt |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 612 | static ConstantInt *SubOne(ConstantInt *C) { |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 613 | APInt Val(C->getValue()); |
| 614 | return ConstantInt::get(--Val); |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 615 | } |
| 616 | /// Add - Add two ConstantInts together |
| 617 | static ConstantInt *Add(ConstantInt *C1, ConstantInt *C2) { |
| 618 | return ConstantInt::get(C1->getValue() + C2->getValue()); |
| 619 | } |
| 620 | /// And - Bitwise AND two ConstantInts together |
| 621 | static ConstantInt *And(ConstantInt *C1, ConstantInt *C2) { |
| 622 | return ConstantInt::get(C1->getValue() & C2->getValue()); |
| 623 | } |
| 624 | /// Subtract - Subtract one ConstantInt from another |
| 625 | static ConstantInt *Subtract(ConstantInt *C1, ConstantInt *C2) { |
| 626 | return ConstantInt::get(C1->getValue() - C2->getValue()); |
| 627 | } |
| 628 | /// Multiply - Multiply two ConstantInts together |
| 629 | static ConstantInt *Multiply(ConstantInt *C1, ConstantInt *C2) { |
| 630 | return ConstantInt::get(C1->getValue() * C2->getValue()); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 631 | } |
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 632 | /// MultiplyOverflows - True if the multiply can not be expressed in an int |
| 633 | /// this size. |
| 634 | static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign) { |
| 635 | uint32_t W = C1->getBitWidth(); |
| 636 | APInt LHSExt = C1->getValue(), RHSExt = C2->getValue(); |
| 637 | if (sign) { |
| 638 | LHSExt.sext(W * 2); |
| 639 | RHSExt.sext(W * 2); |
| 640 | } else { |
| 641 | LHSExt.zext(W * 2); |
| 642 | RHSExt.zext(W * 2); |
| 643 | } |
| 644 | |
| 645 | APInt MulExt = LHSExt * RHSExt; |
| 646 | |
| 647 | if (sign) { |
| 648 | APInt Min = APInt::getSignedMinValue(W).sext(W * 2); |
| 649 | APInt Max = APInt::getSignedMaxValue(W).sext(W * 2); |
| 650 | return MulExt.slt(Min) || MulExt.sgt(Max); |
| 651 | } else |
| 652 | return MulExt.ugt(APInt::getLowBitsSet(W * 2, W)); |
| 653 | } |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 654 | |
Chris Lattner | 68d5ff2 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 655 | /// ComputeMaskedBits - Determine which of the bits specified in Mask are |
| 656 | /// known to be either zero or one and return them in the KnownZero/KnownOne |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 657 | /// bit sets. This code only analyzes bits in Mask, in order to short-circuit |
| 658 | /// processing. |
| 659 | /// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that |
| 660 | /// we cannot optimize based on the assumption that it is zero without changing |
| 661 | /// it to be an explicit zero. If we don't change it to zero, other code could |
| 662 | /// optimized based on the contradictory assumption that it is non-zero. |
| 663 | /// Because instcombine aggressively folds operations with undef args anyway, |
| 664 | /// this won't lose us code quality. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 665 | void InstCombiner::ComputeMaskedBits(Value *V, const APInt &Mask, |
| 666 | APInt& KnownZero, APInt& KnownOne, |
| 667 | unsigned Depth) { |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 668 | assert(V && "No Value?"); |
| 669 | assert(Depth <= 6 && "Limit Search Depth"); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 670 | uint32_t BitWidth = Mask.getBitWidth(); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 671 | assert((V->getType()->isInteger() || isa<PointerType>(V->getType())) && |
| 672 | "Not integer or pointer type!"); |
| 673 | assert((!TD || TD->getTypeSizeInBits(V->getType()) == BitWidth) && |
| 674 | (!isa<IntegerType>(V->getType()) || |
| 675 | V->getType()->getPrimitiveSizeInBits() == BitWidth) && |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 676 | KnownZero.getBitWidth() == BitWidth && |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 677 | KnownOne.getBitWidth() == BitWidth && |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 678 | "V, Mask, KnownOne and KnownZero should have same BitWidth"); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 679 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 680 | // We know all of the bits for a constant! |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 681 | KnownOne = CI->getValue() & Mask; |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 682 | KnownZero = ~KnownOne & Mask; |
| 683 | return; |
| 684 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 685 | // Null is all-zeros. |
| 686 | if (isa<ConstantPointerNull>(V)) { |
| 687 | KnownOne.clear(); |
| 688 | KnownZero = Mask; |
| 689 | return; |
| 690 | } |
| 691 | // The address of an aligned GlobalValue has trailing zeros. |
| 692 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 693 | unsigned Align = GV->getAlignment(); |
| 694 | if (Align == 0 && TD && GV->getType()->getElementType()->isSized()) |
| 695 | Align = TD->getPrefTypeAlignment(GV->getType()->getElementType()); |
| 696 | if (Align > 0) |
| 697 | KnownZero = Mask & APInt::getLowBitsSet(BitWidth, |
| 698 | CountTrailingZeros_32(Align)); |
| 699 | else |
| 700 | KnownZero.clear(); |
| 701 | KnownOne.clear(); |
| 702 | return; |
| 703 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 704 | |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 705 | KnownZero.clear(); KnownOne.clear(); // Start out not knowing anything. |
| 706 | |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 707 | if (Depth == 6 || Mask == 0) |
| 708 | return; // Limit search depth. |
| 709 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 710 | User *I = dyn_cast<User>(V); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 711 | if (!I) return; |
| 712 | |
| 713 | APInt KnownZero2(KnownZero), KnownOne2(KnownOne); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 714 | switch (getOpcode(I)) { |
| 715 | default: break; |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 716 | case Instruction::And: { |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 717 | // If either the LHS or the RHS are Zero, the result is zero. |
| 718 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 719 | APInt Mask2(Mask & ~KnownZero); |
| 720 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 721 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 722 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 723 | |
| 724 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 725 | KnownOne &= KnownOne2; |
| 726 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 727 | KnownZero |= KnownZero2; |
| 728 | return; |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 729 | } |
| 730 | case Instruction::Or: { |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 731 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 732 | APInt Mask2(Mask & ~KnownOne); |
| 733 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 734 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 735 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 736 | |
| 737 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 738 | KnownZero &= KnownZero2; |
| 739 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 740 | KnownOne |= KnownOne2; |
| 741 | return; |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 742 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 743 | case Instruction::Xor: { |
| 744 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
| 745 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); |
| 746 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 747 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 748 | |
| 749 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 750 | APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); |
| 751 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 752 | KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); |
| 753 | KnownZero = KnownZeroOut; |
| 754 | return; |
| 755 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 756 | case Instruction::Mul: { |
| 757 | APInt Mask2 = APInt::getAllOnesValue(BitWidth); |
| 758 | ComputeMaskedBits(I->getOperand(1), Mask2, KnownZero, KnownOne, Depth+1); |
| 759 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); |
| 760 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 761 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 762 | |
| 763 | // If low bits are zero in either operand, output low known-0 bits. |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 764 | // Also compute a conserative estimate for high known-0 bits. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 765 | // More trickiness is possible, but this is sufficient for the |
| 766 | // interesting case of alignment computation. |
| 767 | KnownOne.clear(); |
| 768 | unsigned TrailZ = KnownZero.countTrailingOnes() + |
| 769 | KnownZero2.countTrailingOnes(); |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 770 | unsigned LeadZ = std::max(KnownZero.countLeadingOnes() + |
Dan Gohman | 42ac929 | 2008-05-07 00:35:55 +0000 | [diff] [blame] | 771 | KnownZero2.countLeadingOnes(), |
| 772 | BitWidth) - BitWidth; |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 773 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 774 | TrailZ = std::min(TrailZ, BitWidth); |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 775 | LeadZ = std::min(LeadZ, BitWidth); |
| 776 | KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) | |
| 777 | APInt::getHighBitsSet(BitWidth, LeadZ); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 778 | KnownZero &= Mask; |
| 779 | return; |
| 780 | } |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 781 | case Instruction::UDiv: { |
| 782 | // For the purposes of computing leading zeros we can conservatively |
| 783 | // treat a udiv as a logical right shift by the power of 2 known to |
Dan Gohman | 1d9cd50 | 2008-05-02 21:30:02 +0000 | [diff] [blame] | 784 | // be less than the denominator. |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 785 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); |
| 786 | ComputeMaskedBits(I->getOperand(0), |
| 787 | AllOnes, KnownZero2, KnownOne2, Depth+1); |
| 788 | unsigned LeadZ = KnownZero2.countLeadingOnes(); |
| 789 | |
| 790 | KnownOne2.clear(); |
| 791 | KnownZero2.clear(); |
| 792 | ComputeMaskedBits(I->getOperand(1), |
| 793 | AllOnes, KnownZero2, KnownOne2, Depth+1); |
Dan Gohman | 1d9cd50 | 2008-05-02 21:30:02 +0000 | [diff] [blame] | 794 | unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros(); |
| 795 | if (RHSUnknownLeadingOnes != BitWidth) |
| 796 | LeadZ = std::min(BitWidth, |
| 797 | LeadZ + BitWidth - RHSUnknownLeadingOnes - 1); |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 798 | |
| 799 | KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask; |
| 800 | return; |
| 801 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 802 | case Instruction::Select: |
| 803 | ComputeMaskedBits(I->getOperand(2), Mask, KnownZero, KnownOne, Depth+1); |
| 804 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1); |
| 805 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 806 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 807 | |
| 808 | // Only known if known in both the LHS and RHS. |
| 809 | KnownOne &= KnownOne2; |
| 810 | KnownZero &= KnownZero2; |
| 811 | return; |
| 812 | case Instruction::FPTrunc: |
| 813 | case Instruction::FPExt: |
| 814 | case Instruction::FPToUI: |
| 815 | case Instruction::FPToSI: |
| 816 | case Instruction::SIToFP: |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 817 | case Instruction::UIToFP: |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 818 | return; // Can't work with floating point. |
| 819 | case Instruction::PtrToInt: |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 820 | case Instruction::IntToPtr: |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 821 | // We can't handle these if we don't know the pointer size. |
| 822 | if (!TD) return; |
| 823 | // Fall through and handle them the same as zext/trunc. |
| 824 | case Instruction::ZExt: |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 825 | case Instruction::Trunc: { |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 826 | // All these have integer operands |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 827 | const Type *SrcTy = I->getOperand(0)->getType(); |
| 828 | uint32_t SrcBitWidth = TD ? |
| 829 | TD->getTypeSizeInBits(SrcTy) : |
| 830 | SrcTy->getPrimitiveSizeInBits(); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 831 | APInt MaskIn(Mask); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 832 | MaskIn.zextOrTrunc(SrcBitWidth); |
| 833 | KnownZero.zextOrTrunc(SrcBitWidth); |
| 834 | KnownOne.zextOrTrunc(SrcBitWidth); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 835 | ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, Depth+1); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 836 | KnownZero.zextOrTrunc(BitWidth); |
| 837 | KnownOne.zextOrTrunc(BitWidth); |
| 838 | // Any top bits are known to be zero. |
| 839 | if (BitWidth > SrcBitWidth) |
| 840 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 841 | return; |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 842 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 843 | case Instruction::BitCast: { |
| 844 | const Type *SrcTy = I->getOperand(0)->getType(); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 845 | if (SrcTy->isInteger() || isa<PointerType>(SrcTy)) { |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 846 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
| 847 | return; |
| 848 | } |
| 849 | break; |
| 850 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 851 | case Instruction::SExt: { |
| 852 | // Compute the bits in the result that are not present in the input. |
| 853 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 854 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 855 | |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 856 | APInt MaskIn(Mask); |
| 857 | MaskIn.trunc(SrcBitWidth); |
| 858 | KnownZero.trunc(SrcBitWidth); |
| 859 | KnownOne.trunc(SrcBitWidth); |
| 860 | ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 861 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 862 | KnownZero.zext(BitWidth); |
| 863 | KnownOne.zext(BitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 864 | |
| 865 | // If the sign bit of the input is known set or clear, then we know the |
| 866 | // top bits of the result. |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 867 | if (KnownZero[SrcBitWidth-1]) // Input sign bit known zero |
Zhou Sheng | 34a4b38 | 2007-03-28 17:38:21 +0000 | [diff] [blame] | 868 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 869 | else if (KnownOne[SrcBitWidth-1]) // Input sign bit known set |
Zhou Sheng | 34a4b38 | 2007-03-28 17:38:21 +0000 | [diff] [blame] | 870 | KnownOne |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 871 | return; |
| 872 | } |
| 873 | case Instruction::Shl: |
| 874 | // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 |
| 875 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 876 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 877 | APInt Mask2(Mask.lshr(ShiftAmt)); |
| 878 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 879 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Zhou Sheng | 430f626 | 2007-03-12 05:44:52 +0000 | [diff] [blame] | 880 | KnownZero <<= ShiftAmt; |
| 881 | KnownOne <<= ShiftAmt; |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 882 | KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); // low bits known 0 |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 883 | return; |
| 884 | } |
| 885 | break; |
| 886 | case Instruction::LShr: |
| 887 | // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
| 888 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 889 | // Compute the new bits that are at the top now. |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 890 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 891 | |
| 892 | // Unsigned shift right. |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 893 | APInt Mask2(Mask.shl(ShiftAmt)); |
| 894 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne,Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 895 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 896 | KnownZero = APIntOps::lshr(KnownZero, ShiftAmt); |
| 897 | KnownOne = APIntOps::lshr(KnownOne, ShiftAmt); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 898 | // high bits known zero. |
| 899 | KnownZero |= APInt::getHighBitsSet(BitWidth, ShiftAmt); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 900 | return; |
| 901 | } |
| 902 | break; |
| 903 | case Instruction::AShr: |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 904 | // (ashr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 905 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 906 | // Compute the new bits that are at the top now. |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 907 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 908 | |
| 909 | // Signed shift right. |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 910 | APInt Mask2(Mask.shl(ShiftAmt)); |
| 911 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne,Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 912 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 913 | KnownZero = APIntOps::lshr(KnownZero, ShiftAmt); |
| 914 | KnownOne = APIntOps::lshr(KnownOne, ShiftAmt); |
| 915 | |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 916 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
| 917 | if (KnownZero[BitWidth-ShiftAmt-1]) // New bits are known zero. |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 918 | KnownZero |= HighBits; |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 919 | else if (KnownOne[BitWidth-ShiftAmt-1]) // New bits are known one. |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 920 | KnownOne |= HighBits; |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 921 | return; |
| 922 | } |
| 923 | break; |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 924 | case Instruction::Sub: { |
| 925 | if (ConstantInt *CLHS = dyn_cast<ConstantInt>(I->getOperand(0))) { |
| 926 | // We know that the top bits of C-X are clear if X contains less bits |
| 927 | // than C (i.e. no wrap-around can happen). For example, 20-X is |
| 928 | // positive if we can prove that X is >= 0 and < 16. |
| 929 | if (!CLHS->getValue().isNegative()) { |
| 930 | unsigned NLZ = (CLHS->getValue()+1).countLeadingZeros(); |
| 931 | // NLZ can't be BitWidth with no sign bit |
| 932 | APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1); |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 933 | ComputeMaskedBits(I->getOperand(1), MaskV, KnownZero2, KnownOne2, |
| 934 | Depth+1); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 935 | |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 936 | // If all of the MaskV bits are known to be zero, then we know the |
| 937 | // output top bits are zero, because we now know that the output is |
| 938 | // from [0-C]. |
| 939 | if ((KnownZero2 & MaskV) == MaskV) { |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 940 | unsigned NLZ2 = CLHS->getValue().countLeadingZeros(); |
| 941 | // Top bits known zero. |
| 942 | KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask; |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 943 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | } |
| 947 | // fall through |
Duncan Sands | 1d57a75 | 2008-03-21 08:32:17 +0000 | [diff] [blame] | 948 | case Instruction::Add: { |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 949 | // Output known-0 bits are known if clear or set in both the low clear bits |
| 950 | // common to both LHS & RHS. For example, 8+(X<<3) is known to have the |
| 951 | // low 3 bits clear. |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 952 | APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes()); |
| 953 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); |
| 954 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 955 | unsigned KnownZeroOut = KnownZero2.countTrailingOnes(); |
| 956 | |
| 957 | ComputeMaskedBits(I->getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1); |
| 958 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 959 | KnownZeroOut = std::min(KnownZeroOut, |
| 960 | KnownZero2.countTrailingOnes()); |
| 961 | |
| 962 | KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut); |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 963 | return; |
Duncan Sands | 1d57a75 | 2008-03-21 08:32:17 +0000 | [diff] [blame] | 964 | } |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 965 | case Instruction::SRem: |
| 966 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 967 | APInt RA = Rem->getValue(); |
| 968 | if (RA.isPowerOf2() || (-RA).isPowerOf2()) { |
Dan Gohman | 23e1df8 | 2008-05-06 00:51:48 +0000 | [diff] [blame] | 969 | APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA; |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 970 | APInt Mask2 = LowBits | APInt::getSignBit(BitWidth); |
| 971 | ComputeMaskedBits(I->getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1); |
| 972 | |
| 973 | // The sign of a remainder is equal to the sign of the first |
| 974 | // operand (zero being positive). |
| 975 | if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits)) |
| 976 | KnownZero2 |= ~LowBits; |
| 977 | else if (KnownOne2[BitWidth-1]) |
| 978 | KnownOne2 |= ~LowBits; |
| 979 | |
| 980 | KnownZero |= KnownZero2 & Mask; |
| 981 | KnownOne |= KnownOne2 & Mask; |
| 982 | |
| 983 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 984 | } |
| 985 | } |
| 986 | break; |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 987 | case Instruction::URem: { |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 988 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 989 | APInt RA = Rem->getValue(); |
Dan Gohman | 23e1df8 | 2008-05-06 00:51:48 +0000 | [diff] [blame] | 990 | if (RA.isPowerOf2()) { |
| 991 | APInt LowBits = (RA - 1); |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 992 | APInt Mask2 = LowBits & Mask; |
| 993 | KnownZero |= ~LowBits & Mask; |
| 994 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne,Depth+1); |
| 995 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 996 | break; |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 997 | } |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 998 | } |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 999 | |
| 1000 | // Since the result is less than or equal to either operand, any leading |
| 1001 | // zero bits in either operand must also exist in the result. |
| 1002 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); |
| 1003 | ComputeMaskedBits(I->getOperand(0), AllOnes, KnownZero, KnownOne, |
| 1004 | Depth+1); |
| 1005 | ComputeMaskedBits(I->getOperand(1), AllOnes, KnownZero2, KnownOne2, |
| 1006 | Depth+1); |
| 1007 | |
| 1008 | uint32_t Leaders = std::max(KnownZero.countLeadingOnes(), |
| 1009 | KnownZero2.countLeadingOnes()); |
| 1010 | KnownOne.clear(); |
| 1011 | KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask; |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1012 | break; |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1013 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 1014 | |
| 1015 | case Instruction::Alloca: |
| 1016 | case Instruction::Malloc: { |
| 1017 | AllocationInst *AI = cast<AllocationInst>(V); |
| 1018 | unsigned Align = AI->getAlignment(); |
| 1019 | if (Align == 0 && TD) { |
| 1020 | if (isa<AllocaInst>(AI)) |
| 1021 | Align = TD->getPrefTypeAlignment(AI->getType()->getElementType()); |
| 1022 | else if (isa<MallocInst>(AI)) { |
| 1023 | // Malloc returns maximally aligned memory. |
| 1024 | Align = TD->getABITypeAlignment(AI->getType()->getElementType()); |
| 1025 | Align = |
| 1026 | std::max(Align, |
| 1027 | (unsigned)TD->getABITypeAlignment(Type::DoubleTy)); |
| 1028 | Align = |
| 1029 | std::max(Align, |
| 1030 | (unsigned)TD->getABITypeAlignment(Type::Int64Ty)); |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | if (Align > 0) |
| 1035 | KnownZero = Mask & APInt::getLowBitsSet(BitWidth, |
| 1036 | CountTrailingZeros_32(Align)); |
| 1037 | break; |
| 1038 | } |
| 1039 | case Instruction::GetElementPtr: { |
| 1040 | // Analyze all of the subscripts of this getelementptr instruction |
| 1041 | // to determine if we can prove known low zero bits. |
| 1042 | APInt LocalMask = APInt::getAllOnesValue(BitWidth); |
| 1043 | APInt LocalKnownZero(BitWidth, 0), LocalKnownOne(BitWidth, 0); |
| 1044 | ComputeMaskedBits(I->getOperand(0), LocalMask, |
| 1045 | LocalKnownZero, LocalKnownOne, Depth+1); |
| 1046 | unsigned TrailZ = LocalKnownZero.countTrailingOnes(); |
| 1047 | |
| 1048 | gep_type_iterator GTI = gep_type_begin(I); |
| 1049 | for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) { |
| 1050 | Value *Index = I->getOperand(i); |
| 1051 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 1052 | // Handle struct member offset arithmetic. |
| 1053 | if (!TD) return; |
| 1054 | const StructLayout *SL = TD->getStructLayout(STy); |
| 1055 | unsigned Idx = cast<ConstantInt>(Index)->getZExtValue(); |
| 1056 | uint64_t Offset = SL->getElementOffset(Idx); |
| 1057 | TrailZ = std::min(TrailZ, |
| 1058 | CountTrailingZeros_64(Offset)); |
| 1059 | } else { |
| 1060 | // Handle array index arithmetic. |
| 1061 | const Type *IndexedTy = GTI.getIndexedType(); |
| 1062 | if (!IndexedTy->isSized()) return; |
| 1063 | unsigned GEPOpiBits = Index->getType()->getPrimitiveSizeInBits(); |
| 1064 | uint64_t TypeSize = TD ? TD->getABITypeSize(IndexedTy) : 1; |
| 1065 | LocalMask = APInt::getAllOnesValue(GEPOpiBits); |
| 1066 | LocalKnownZero = LocalKnownOne = APInt(GEPOpiBits, 0); |
| 1067 | ComputeMaskedBits(Index, LocalMask, |
| 1068 | LocalKnownZero, LocalKnownOne, Depth+1); |
| 1069 | TrailZ = std::min(TrailZ, |
| 1070 | CountTrailingZeros_64(TypeSize) + |
| 1071 | LocalKnownZero.countTrailingOnes()); |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) & Mask; |
| 1076 | break; |
| 1077 | } |
| 1078 | case Instruction::PHI: { |
| 1079 | PHINode *P = cast<PHINode>(I); |
| 1080 | // Handle the case of a simple two-predecessor recurrence PHI. |
| 1081 | // There's a lot more that could theoretically be done here, but |
| 1082 | // this is sufficient to catch some interesting cases. |
| 1083 | if (P->getNumIncomingValues() == 2) { |
| 1084 | for (unsigned i = 0; i != 2; ++i) { |
| 1085 | Value *L = P->getIncomingValue(i); |
| 1086 | Value *R = P->getIncomingValue(!i); |
| 1087 | User *LU = dyn_cast<User>(L); |
| 1088 | unsigned Opcode = LU ? getOpcode(LU) : (unsigned)Instruction::UserOp1; |
| 1089 | // Check for operations that have the property that if |
| 1090 | // both their operands have low zero bits, the result |
| 1091 | // will have low zero bits. |
| 1092 | if (Opcode == Instruction::Add || |
| 1093 | Opcode == Instruction::Sub || |
| 1094 | Opcode == Instruction::And || |
| 1095 | Opcode == Instruction::Or || |
| 1096 | Opcode == Instruction::Mul) { |
| 1097 | Value *LL = LU->getOperand(0); |
| 1098 | Value *LR = LU->getOperand(1); |
| 1099 | // Find a recurrence. |
| 1100 | if (LL == I) |
| 1101 | L = LR; |
| 1102 | else if (LR == I) |
| 1103 | L = LL; |
| 1104 | else |
| 1105 | break; |
| 1106 | // Ok, we have a PHI of the form L op= R. Check for low |
| 1107 | // zero bits. |
| 1108 | APInt Mask2 = APInt::getAllOnesValue(BitWidth); |
| 1109 | ComputeMaskedBits(R, Mask2, KnownZero2, KnownOne2, Depth+1); |
| 1110 | Mask2 = APInt::getLowBitsSet(BitWidth, |
| 1111 | KnownZero2.countTrailingOnes()); |
| 1112 | KnownOne2.clear(); |
| 1113 | KnownZero2.clear(); |
| 1114 | ComputeMaskedBits(L, Mask2, KnownZero2, KnownOne2, Depth+1); |
| 1115 | KnownZero = Mask & |
| 1116 | APInt::getLowBitsSet(BitWidth, |
| 1117 | KnownZero2.countTrailingOnes()); |
| 1118 | break; |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | break; |
| 1123 | } |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1124 | case Instruction::Call: |
| 1125 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
| 1126 | switch (II->getIntrinsicID()) { |
| 1127 | default: break; |
| 1128 | case Intrinsic::ctpop: |
| 1129 | case Intrinsic::ctlz: |
| 1130 | case Intrinsic::cttz: { |
| 1131 | unsigned LowBits = Log2_32(BitWidth)+1; |
| 1132 | KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits); |
| 1133 | break; |
| 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | break; |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 1138 | } |
| 1139 | } |
| 1140 | |
Reid Spencer | e7816b5 | 2007-03-08 01:52:58 +0000 | [diff] [blame] | 1141 | /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use |
| 1142 | /// this predicate to simplify operations downstream. Mask is known to be zero |
| 1143 | /// for bits that V cannot have. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 1144 | bool InstCombiner::MaskedValueIsZero(Value *V, const APInt& Mask, |
| 1145 | unsigned Depth) { |
Zhou Sheng | edd089c | 2007-03-12 16:54:56 +0000 | [diff] [blame] | 1146 | APInt KnownZero(Mask.getBitWidth(), 0), KnownOne(Mask.getBitWidth(), 0); |
Reid Spencer | e7816b5 | 2007-03-08 01:52:58 +0000 | [diff] [blame] | 1147 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne, Depth); |
| 1148 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1149 | return (KnownZero & Mask) == Mask; |
| 1150 | } |
| 1151 | |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1152 | /// ShrinkDemandedConstant - Check to see if the specified operand of the |
| 1153 | /// specified instruction is a constant integer. If so, check to see if there |
| 1154 | /// are any bits set in the constant that are not demanded. If so, shrink the |
| 1155 | /// constant and return true. |
| 1156 | static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, |
Reid Spencer | 6b79e2d | 2007-03-12 17:15:10 +0000 | [diff] [blame] | 1157 | APInt Demanded) { |
| 1158 | assert(I && "No instruction?"); |
| 1159 | assert(OpNo < I->getNumOperands() && "Operand index too large"); |
| 1160 | |
| 1161 | // If the operand is not a constant integer, nothing to do. |
| 1162 | ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo)); |
| 1163 | if (!OpC) return false; |
| 1164 | |
| 1165 | // If there are no bits set that aren't demanded, nothing to do. |
| 1166 | Demanded.zextOrTrunc(OpC->getValue().getBitWidth()); |
| 1167 | if ((~Demanded & OpC->getValue()) == 0) |
| 1168 | return false; |
| 1169 | |
| 1170 | // This instruction is producing bits that are not demanded. Shrink the RHS. |
| 1171 | Demanded &= OpC->getValue(); |
| 1172 | I->setOperand(OpNo, ConstantInt::get(Demanded)); |
| 1173 | return true; |
| 1174 | } |
| 1175 | |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 1176 | // ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a |
| 1177 | // set of known zero and one bits, compute the maximum and minimum values that |
| 1178 | // could have the specified known zero and known one bits, returning them in |
| 1179 | // min/max. |
| 1180 | static void ComputeSignedMinMaxValuesFromKnownBits(const Type *Ty, |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 1181 | const APInt& KnownZero, |
| 1182 | const APInt& KnownOne, |
| 1183 | APInt& Min, APInt& Max) { |
| 1184 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
| 1185 | assert(KnownZero.getBitWidth() == BitWidth && |
| 1186 | KnownOne.getBitWidth() == BitWidth && |
| 1187 | Min.getBitWidth() == BitWidth && Max.getBitWidth() == BitWidth && |
| 1188 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1189 | APInt UnknownBits = ~(KnownZero|KnownOne); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 1190 | |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 1191 | // The minimum value is when all unknown bits are zeros, EXCEPT for the sign |
| 1192 | // bit if it is unknown. |
| 1193 | Min = KnownOne; |
| 1194 | Max = KnownOne|UnknownBits; |
| 1195 | |
Zhou Sheng | 4acf155 | 2007-03-28 05:15:57 +0000 | [diff] [blame] | 1196 | if (UnknownBits[BitWidth-1]) { // Sign bit is unknown |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 1197 | Min.set(BitWidth-1); |
| 1198 | Max.clear(BitWidth-1); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 1199 | } |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | // ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and |
| 1203 | // a set of known zero and one bits, compute the maximum and minimum values that |
| 1204 | // could have the specified known zero and known one bits, returning them in |
| 1205 | // min/max. |
| 1206 | static void ComputeUnsignedMinMaxValuesFromKnownBits(const Type *Ty, |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 1207 | const APInt &KnownZero, |
| 1208 | const APInt &KnownOne, |
| 1209 | APInt &Min, APInt &Max) { |
| 1210 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); BitWidth = BitWidth; |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 1211 | assert(KnownZero.getBitWidth() == BitWidth && |
| 1212 | KnownOne.getBitWidth() == BitWidth && |
| 1213 | Min.getBitWidth() == BitWidth && Max.getBitWidth() && |
| 1214 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1215 | APInt UnknownBits = ~(KnownZero|KnownOne); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 1216 | |
| 1217 | // The minimum value is when the unknown bits are all zeros. |
| 1218 | Min = KnownOne; |
| 1219 | // The maximum value is when the unknown bits are all ones. |
| 1220 | Max = KnownOne|UnknownBits; |
| 1221 | } |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1222 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1223 | /// SimplifyDemandedBits - This function attempts to replace V with a simpler |
| 1224 | /// value based on the demanded bits. When this function is called, it is known |
| 1225 | /// that only the bits set in DemandedMask of the result of V are ever used |
| 1226 | /// downstream. Consequently, depending on the mask and V, it may be possible |
| 1227 | /// to replace V with a constant or one of its operands. In such cases, this |
| 1228 | /// function does the replacement and returns true. In all other cases, it |
| 1229 | /// returns false after analyzing the expression and setting KnownOne and known |
| 1230 | /// to be one in the expression. KnownZero contains all the bits that are known |
| 1231 | /// to be zero in the expression. These are provided to potentially allow the |
| 1232 | /// caller (which might recursively be SimplifyDemandedBits itself) to simplify |
| 1233 | /// the expression. KnownOne and KnownZero always follow the invariant that |
| 1234 | /// KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that |
| 1235 | /// the bits in KnownOne and KnownZero may only be accurate for those bits set |
| 1236 | /// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero |
| 1237 | /// and KnownOne must all be the same. |
| 1238 | bool InstCombiner::SimplifyDemandedBits(Value *V, APInt DemandedMask, |
| 1239 | APInt& KnownZero, APInt& KnownOne, |
| 1240 | unsigned Depth) { |
| 1241 | assert(V != 0 && "Null pointer of Value???"); |
| 1242 | assert(Depth <= 6 && "Limit Search Depth"); |
| 1243 | uint32_t BitWidth = DemandedMask.getBitWidth(); |
| 1244 | const IntegerType *VTy = cast<IntegerType>(V->getType()); |
| 1245 | assert(VTy->getBitWidth() == BitWidth && |
| 1246 | KnownZero.getBitWidth() == BitWidth && |
| 1247 | KnownOne.getBitWidth() == BitWidth && |
| 1248 | "Value *V, DemandedMask, KnownZero and KnownOne \ |
| 1249 | must have same BitWidth"); |
| 1250 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 1251 | // We know all of the bits for a constant! |
| 1252 | KnownOne = CI->getValue() & DemandedMask; |
| 1253 | KnownZero = ~KnownOne & DemandedMask; |
| 1254 | return false; |
| 1255 | } |
| 1256 | |
Zhou Sheng | 9670445 | 2007-03-14 03:21:24 +0000 | [diff] [blame] | 1257 | KnownZero.clear(); |
| 1258 | KnownOne.clear(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1259 | if (!V->hasOneUse()) { // Other users may use these bits. |
| 1260 | if (Depth != 0) { // Not at the root. |
| 1261 | // Just compute the KnownZero/KnownOne bits to simplify things downstream. |
| 1262 | ComputeMaskedBits(V, DemandedMask, KnownZero, KnownOne, Depth); |
| 1263 | return false; |
| 1264 | } |
| 1265 | // If this is the root being simplified, allow it to have multiple uses, |
| 1266 | // just set the DemandedMask to all bits. |
| 1267 | DemandedMask = APInt::getAllOnesValue(BitWidth); |
| 1268 | } else if (DemandedMask == 0) { // Not demanding any bits from V. |
| 1269 | if (V != UndefValue::get(VTy)) |
| 1270 | return UpdateValueUsesWith(V, UndefValue::get(VTy)); |
| 1271 | return false; |
| 1272 | } else if (Depth == 6) { // Limit search depth. |
| 1273 | return false; |
| 1274 | } |
| 1275 | |
| 1276 | Instruction *I = dyn_cast<Instruction>(V); |
| 1277 | if (!I) return false; // Only analyze instructions. |
| 1278 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1279 | APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0); |
| 1280 | APInt &RHSKnownZero = KnownZero, &RHSKnownOne = KnownOne; |
| 1281 | switch (I->getOpcode()) { |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1282 | default: |
| 1283 | ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth); |
| 1284 | break; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1285 | case Instruction::And: |
| 1286 | // If either the LHS or the RHS are Zero, the result is zero. |
| 1287 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 1288 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1289 | return true; |
| 1290 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1291 | "Bits known to be one AND zero?"); |
| 1292 | |
| 1293 | // If something is known zero on the RHS, the bits aren't demanded on the |
| 1294 | // LHS. |
| 1295 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownZero, |
| 1296 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1297 | return true; |
| 1298 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 1299 | "Bits known to be one AND zero?"); |
| 1300 | |
| 1301 | // If all of the demanded bits are known 1 on one side, return the other. |
| 1302 | // These bits cannot contribute to the result of the 'and'. |
| 1303 | if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) == |
| 1304 | (DemandedMask & ~LHSKnownZero)) |
| 1305 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 1306 | if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) == |
| 1307 | (DemandedMask & ~RHSKnownZero)) |
| 1308 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 1309 | |
| 1310 | // If all of the demanded bits in the inputs are known zeros, return zero. |
| 1311 | if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask) |
| 1312 | return UpdateValueUsesWith(I, Constant::getNullValue(VTy)); |
| 1313 | |
| 1314 | // If the RHS is a constant, see if we can simplify it. |
| 1315 | if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero)) |
| 1316 | return UpdateValueUsesWith(I, I); |
| 1317 | |
| 1318 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 1319 | RHSKnownOne &= LHSKnownOne; |
| 1320 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 1321 | RHSKnownZero |= LHSKnownZero; |
| 1322 | break; |
| 1323 | case Instruction::Or: |
| 1324 | // If either the LHS or the RHS are One, the result is One. |
| 1325 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 1326 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1327 | return true; |
| 1328 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1329 | "Bits known to be one AND zero?"); |
| 1330 | // If something is known one on the RHS, the bits aren't demanded on the |
| 1331 | // LHS. |
| 1332 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownOne, |
| 1333 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1334 | return true; |
| 1335 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 1336 | "Bits known to be one AND zero?"); |
| 1337 | |
| 1338 | // If all of the demanded bits are known zero on one side, return the other. |
| 1339 | // These bits cannot contribute to the result of the 'or'. |
| 1340 | if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) == |
| 1341 | (DemandedMask & ~LHSKnownOne)) |
| 1342 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 1343 | if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) == |
| 1344 | (DemandedMask & ~RHSKnownOne)) |
| 1345 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 1346 | |
| 1347 | // If all of the potentially set bits on one side are known to be set on |
| 1348 | // the other side, just use the 'other' side. |
| 1349 | if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) == |
| 1350 | (DemandedMask & (~RHSKnownZero))) |
| 1351 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 1352 | if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) == |
| 1353 | (DemandedMask & (~LHSKnownZero))) |
| 1354 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 1355 | |
| 1356 | // If the RHS is a constant, see if we can simplify it. |
| 1357 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 1358 | return UpdateValueUsesWith(I, I); |
| 1359 | |
| 1360 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 1361 | RHSKnownZero &= LHSKnownZero; |
| 1362 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 1363 | RHSKnownOne |= LHSKnownOne; |
| 1364 | break; |
| 1365 | case Instruction::Xor: { |
| 1366 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 1367 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1368 | return true; |
| 1369 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1370 | "Bits known to be one AND zero?"); |
| 1371 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1372 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1373 | return true; |
| 1374 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 1375 | "Bits known to be one AND zero?"); |
| 1376 | |
| 1377 | // If all of the demanded bits are known zero on one side, return the other. |
| 1378 | // These bits cannot contribute to the result of the 'xor'. |
| 1379 | if ((DemandedMask & RHSKnownZero) == DemandedMask) |
| 1380 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 1381 | if ((DemandedMask & LHSKnownZero) == DemandedMask) |
| 1382 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 1383 | |
| 1384 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 1385 | APInt KnownZeroOut = (RHSKnownZero & LHSKnownZero) | |
| 1386 | (RHSKnownOne & LHSKnownOne); |
| 1387 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 1388 | APInt KnownOneOut = (RHSKnownZero & LHSKnownOne) | |
| 1389 | (RHSKnownOne & LHSKnownZero); |
| 1390 | |
| 1391 | // If all of the demanded bits are known to be zero on one side or the |
| 1392 | // other, turn this into an *inclusive* or. |
| 1393 | // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 |
| 1394 | if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) { |
| 1395 | Instruction *Or = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1396 | BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1), |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1397 | I->getName()); |
| 1398 | InsertNewInstBefore(Or, *I); |
| 1399 | return UpdateValueUsesWith(I, Or); |
| 1400 | } |
| 1401 | |
| 1402 | // If all of the demanded bits on one side are known, and all of the set |
| 1403 | // bits on that side are also known to be set on the other side, turn this |
| 1404 | // into an AND, as we know the bits will be cleared. |
| 1405 | // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 |
| 1406 | if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) { |
| 1407 | // all known |
| 1408 | if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) { |
| 1409 | Constant *AndC = ConstantInt::get(~RHSKnownOne & DemandedMask); |
| 1410 | Instruction *And = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1411 | BinaryOperator::CreateAnd(I->getOperand(0), AndC, "tmp"); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1412 | InsertNewInstBefore(And, *I); |
| 1413 | return UpdateValueUsesWith(I, And); |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | // If the RHS is a constant, see if we can simplify it. |
| 1418 | // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1. |
| 1419 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 1420 | return UpdateValueUsesWith(I, I); |
| 1421 | |
| 1422 | RHSKnownZero = KnownZeroOut; |
| 1423 | RHSKnownOne = KnownOneOut; |
| 1424 | break; |
| 1425 | } |
| 1426 | case Instruction::Select: |
| 1427 | if (SimplifyDemandedBits(I->getOperand(2), DemandedMask, |
| 1428 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1429 | return true; |
| 1430 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 1431 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1432 | return true; |
| 1433 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1434 | "Bits known to be one AND zero?"); |
| 1435 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 1436 | "Bits known to be one AND zero?"); |
| 1437 | |
| 1438 | // If the operands are constants, see if we can simplify them. |
| 1439 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 1440 | return UpdateValueUsesWith(I, I); |
| 1441 | if (ShrinkDemandedConstant(I, 2, DemandedMask)) |
| 1442 | return UpdateValueUsesWith(I, I); |
| 1443 | |
| 1444 | // Only known if known in both the LHS and RHS. |
| 1445 | RHSKnownOne &= LHSKnownOne; |
| 1446 | RHSKnownZero &= LHSKnownZero; |
| 1447 | break; |
| 1448 | case Instruction::Trunc: { |
| 1449 | uint32_t truncBf = |
| 1450 | cast<IntegerType>(I->getOperand(0)->getType())->getBitWidth(); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1451 | DemandedMask.zext(truncBf); |
| 1452 | RHSKnownZero.zext(truncBf); |
| 1453 | RHSKnownOne.zext(truncBf); |
| 1454 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1455 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1456 | return true; |
| 1457 | DemandedMask.trunc(BitWidth); |
| 1458 | RHSKnownZero.trunc(BitWidth); |
| 1459 | RHSKnownOne.trunc(BitWidth); |
| 1460 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1461 | "Bits known to be one AND zero?"); |
| 1462 | break; |
| 1463 | } |
| 1464 | case Instruction::BitCast: |
| 1465 | if (!I->getOperand(0)->getType()->isInteger()) |
| 1466 | return false; |
| 1467 | |
| 1468 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1469 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1470 | return true; |
| 1471 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1472 | "Bits known to be one AND zero?"); |
| 1473 | break; |
| 1474 | case Instruction::ZExt: { |
| 1475 | // Compute the bits in the result that are not present in the input. |
| 1476 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1477 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1478 | |
Zhou Sheng | d48653a | 2007-03-29 04:45:55 +0000 | [diff] [blame] | 1479 | DemandedMask.trunc(SrcBitWidth); |
| 1480 | RHSKnownZero.trunc(SrcBitWidth); |
| 1481 | RHSKnownOne.trunc(SrcBitWidth); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1482 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1483 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1484 | return true; |
| 1485 | DemandedMask.zext(BitWidth); |
| 1486 | RHSKnownZero.zext(BitWidth); |
| 1487 | RHSKnownOne.zext(BitWidth); |
| 1488 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1489 | "Bits known to be one AND zero?"); |
| 1490 | // The top bits are known to be zero. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1491 | RHSKnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1492 | break; |
| 1493 | } |
| 1494 | case Instruction::SExt: { |
| 1495 | // Compute the bits in the result that are not present in the input. |
| 1496 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1497 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1498 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1499 | APInt InputDemandedBits = DemandedMask & |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1500 | APInt::getLowBitsSet(BitWidth, SrcBitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1501 | |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1502 | APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1503 | // If any of the sign extended bits are demanded, we know that the sign |
| 1504 | // bit is demanded. |
| 1505 | if ((NewBits & DemandedMask) != 0) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 1506 | InputDemandedBits.set(SrcBitWidth-1); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1507 | |
Zhou Sheng | d48653a | 2007-03-29 04:45:55 +0000 | [diff] [blame] | 1508 | InputDemandedBits.trunc(SrcBitWidth); |
| 1509 | RHSKnownZero.trunc(SrcBitWidth); |
| 1510 | RHSKnownOne.trunc(SrcBitWidth); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1511 | if (SimplifyDemandedBits(I->getOperand(0), InputDemandedBits, |
| 1512 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1513 | return true; |
| 1514 | InputDemandedBits.zext(BitWidth); |
| 1515 | RHSKnownZero.zext(BitWidth); |
| 1516 | RHSKnownOne.zext(BitWidth); |
| 1517 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1518 | "Bits known to be one AND zero?"); |
| 1519 | |
| 1520 | // If the sign bit of the input is known set or clear, then we know the |
| 1521 | // top bits of the result. |
| 1522 | |
| 1523 | // If the input sign bit is known zero, or if the NewBits are not demanded |
| 1524 | // convert this into a zero extension. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1525 | if (RHSKnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1526 | { |
| 1527 | // Convert to ZExt cast |
| 1528 | CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName(), I); |
| 1529 | return UpdateValueUsesWith(I, NewCast); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1530 | } else if (RHSKnownOne[SrcBitWidth-1]) { // Input sign bit known set |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1531 | RHSKnownOne |= NewBits; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1532 | } |
| 1533 | break; |
| 1534 | } |
| 1535 | case Instruction::Add: { |
| 1536 | // Figure out what the input bits are. If the top bits of the and result |
| 1537 | // are not demanded, then the add doesn't demand them from its input |
| 1538 | // either. |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 1539 | uint32_t NLZ = DemandedMask.countLeadingZeros(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1540 | |
| 1541 | // If there is a constant on the RHS, there are a variety of xformations |
| 1542 | // we can do. |
| 1543 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1544 | // If null, this should be simplified elsewhere. Some of the xforms here |
| 1545 | // won't work if the RHS is zero. |
| 1546 | if (RHS->isZero()) |
| 1547 | break; |
| 1548 | |
| 1549 | // If the top bit of the output is demanded, demand everything from the |
| 1550 | // input. Otherwise, we demand all the input bits except NLZ top bits. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1551 | APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1552 | |
| 1553 | // Find information about known zero/one bits in the input. |
| 1554 | if (SimplifyDemandedBits(I->getOperand(0), InDemandedBits, |
| 1555 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1556 | return true; |
| 1557 | |
| 1558 | // If the RHS of the add has bits set that can't affect the input, reduce |
| 1559 | // the constant. |
| 1560 | if (ShrinkDemandedConstant(I, 1, InDemandedBits)) |
| 1561 | return UpdateValueUsesWith(I, I); |
| 1562 | |
| 1563 | // Avoid excess work. |
| 1564 | if (LHSKnownZero == 0 && LHSKnownOne == 0) |
| 1565 | break; |
| 1566 | |
| 1567 | // Turn it into OR if input bits are zero. |
| 1568 | if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) { |
| 1569 | Instruction *Or = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1570 | BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1), |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1571 | I->getName()); |
| 1572 | InsertNewInstBefore(Or, *I); |
| 1573 | return UpdateValueUsesWith(I, Or); |
| 1574 | } |
| 1575 | |
| 1576 | // We can say something about the output known-zero and known-one bits, |
| 1577 | // depending on potential carries from the input constant and the |
| 1578 | // unknowns. For example if the LHS is known to have at most the 0x0F0F0 |
| 1579 | // bits set and the RHS constant is 0x01001, then we know we have a known |
| 1580 | // one mask of 0x00001 and a known zero mask of 0xE0F0E. |
| 1581 | |
| 1582 | // To compute this, we first compute the potential carry bits. These are |
| 1583 | // the bits which may be modified. I'm not aware of a better way to do |
| 1584 | // this scan. |
Zhou Sheng | b9cb95f | 2007-03-31 02:38:39 +0000 | [diff] [blame] | 1585 | const APInt& RHSVal = RHS->getValue(); |
| 1586 | APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1587 | |
| 1588 | // Now that we know which bits have carries, compute the known-1/0 sets. |
| 1589 | |
| 1590 | // Bits are known one if they are known zero in one operand and one in the |
| 1591 | // other, and there is no input carry. |
| 1592 | RHSKnownOne = ((LHSKnownZero & RHSVal) | |
| 1593 | (LHSKnownOne & ~RHSVal)) & ~CarryBits; |
| 1594 | |
| 1595 | // Bits are known zero if they are known zero in both operands and there |
| 1596 | // is no input carry. |
| 1597 | RHSKnownZero = LHSKnownZero & ~RHSVal & ~CarryBits; |
| 1598 | } else { |
| 1599 | // If the high-bits of this ADD are not demanded, then it does not demand |
| 1600 | // the high bits of its LHS or RHS. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1601 | if (DemandedMask[BitWidth-1] == 0) { |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1602 | // Right fill the mask of bits for this ADD to demand the most |
| 1603 | // significant bit and all those below it. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1604 | APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1605 | if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps, |
| 1606 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1607 | return true; |
| 1608 | if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps, |
| 1609 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1610 | return true; |
| 1611 | } |
| 1612 | } |
| 1613 | break; |
| 1614 | } |
| 1615 | case Instruction::Sub: |
| 1616 | // If the high-bits of this SUB are not demanded, then it does not demand |
| 1617 | // the high bits of its LHS or RHS. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1618 | if (DemandedMask[BitWidth-1] == 0) { |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1619 | // Right fill the mask of bits for this SUB to demand the most |
| 1620 | // significant bit and all those below it. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 1621 | uint32_t NLZ = DemandedMask.countLeadingZeros(); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1622 | APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1623 | if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps, |
| 1624 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1625 | return true; |
| 1626 | if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps, |
| 1627 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1628 | return true; |
| 1629 | } |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1630 | // Otherwise just hand the sub off to ComputeMaskedBits to fill in |
| 1631 | // the known zeros and ones. |
| 1632 | ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1633 | break; |
| 1634 | case Instruction::Shl: |
| 1635 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 1636 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1637 | APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt)); |
| 1638 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1639 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1640 | return true; |
| 1641 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1642 | "Bits known to be one AND zero?"); |
| 1643 | RHSKnownZero <<= ShiftAmt; |
| 1644 | RHSKnownOne <<= ShiftAmt; |
| 1645 | // low bits known zero. |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1646 | if (ShiftAmt) |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 1647 | RHSKnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1648 | } |
| 1649 | break; |
| 1650 | case Instruction::LShr: |
| 1651 | // For a logical shift right |
| 1652 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 1653 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1654 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1655 | // Unsigned shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1656 | APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); |
| 1657 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1658 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1659 | return true; |
| 1660 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1661 | "Bits known to be one AND zero?"); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1662 | RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt); |
| 1663 | RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt); |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1664 | if (ShiftAmt) { |
| 1665 | // Compute the new bits that are at the top now. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1666 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1667 | RHSKnownZero |= HighBits; // high bits known zero. |
| 1668 | } |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1669 | } |
| 1670 | break; |
| 1671 | case Instruction::AShr: |
| 1672 | // If this is an arithmetic shift right and only the low-bit is set, we can |
| 1673 | // always convert this into a logical shr, even if the shift amount is |
| 1674 | // variable. The low bit of the shift cannot be an input sign bit unless |
| 1675 | // the shift amount is >= the size of the datatype, which is undefined. |
| 1676 | if (DemandedMask == 1) { |
| 1677 | // Perform the logical shift right. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1678 | Value *NewVal = BinaryOperator::CreateLShr( |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1679 | I->getOperand(0), I->getOperand(1), I->getName()); |
| 1680 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); |
| 1681 | return UpdateValueUsesWith(I, NewVal); |
| 1682 | } |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 1683 | |
| 1684 | // If the sign bit is the only bit demanded by this ashr, then there is no |
| 1685 | // need to do it, the shift doesn't change the high bit. |
| 1686 | if (DemandedMask.isSignBit()) |
| 1687 | return UpdateValueUsesWith(I, I->getOperand(0)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1688 | |
| 1689 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 1690 | uint32_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1691 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1692 | // Signed shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1693 | APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); |
Lauro Ramos Venancio | d0499af | 2007-06-06 17:08:48 +0000 | [diff] [blame] | 1694 | // If any of the "high bits" are demanded, we should set the sign bit as |
| 1695 | // demanded. |
| 1696 | if (DemandedMask.countLeadingZeros() <= ShiftAmt) |
| 1697 | DemandedMaskIn.set(BitWidth-1); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1698 | if (SimplifyDemandedBits(I->getOperand(0), |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1699 | DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1700 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1701 | return true; |
| 1702 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1703 | "Bits known to be one AND zero?"); |
| 1704 | // Compute the new bits that are at the top now. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1705 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1706 | RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt); |
| 1707 | RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt); |
| 1708 | |
| 1709 | // Handle the sign bits. |
| 1710 | APInt SignBit(APInt::getSignBit(BitWidth)); |
| 1711 | // Adjust to where it is now in the mask. |
| 1712 | SignBit = APIntOps::lshr(SignBit, ShiftAmt); |
| 1713 | |
| 1714 | // If the input sign bit is known to be zero, or if none of the top bits |
| 1715 | // are demanded, turn this into an unsigned shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1716 | if (RHSKnownZero[BitWidth-ShiftAmt-1] || |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1717 | (HighBits & ~DemandedMask) == HighBits) { |
| 1718 | // Perform the logical shift right. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1719 | Value *NewVal = BinaryOperator::CreateLShr( |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1720 | I->getOperand(0), SA, I->getName()); |
| 1721 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); |
| 1722 | return UpdateValueUsesWith(I, NewVal); |
| 1723 | } else if ((RHSKnownOne & SignBit) != 0) { // New bits are known one. |
| 1724 | RHSKnownOne |= HighBits; |
| 1725 | } |
| 1726 | } |
| 1727 | break; |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1728 | case Instruction::SRem: |
| 1729 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1730 | APInt RA = Rem->getValue(); |
| 1731 | if (RA.isPowerOf2() || (-RA).isPowerOf2()) { |
Dan Gohman | 23e1df8 | 2008-05-06 00:51:48 +0000 | [diff] [blame] | 1732 | APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA; |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1733 | APInt Mask2 = LowBits | APInt::getSignBit(BitWidth); |
| 1734 | if (SimplifyDemandedBits(I->getOperand(0), Mask2, |
| 1735 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1736 | return true; |
| 1737 | |
| 1738 | if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits)) |
| 1739 | LHSKnownZero |= ~LowBits; |
| 1740 | else if (LHSKnownOne[BitWidth-1]) |
| 1741 | LHSKnownOne |= ~LowBits; |
| 1742 | |
| 1743 | KnownZero |= LHSKnownZero & DemandedMask; |
| 1744 | KnownOne |= LHSKnownOne & DemandedMask; |
| 1745 | |
| 1746 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 1747 | } |
| 1748 | } |
| 1749 | break; |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1750 | case Instruction::URem: { |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1751 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1752 | APInt RA = Rem->getValue(); |
Dan Gohman | 23e1df8 | 2008-05-06 00:51:48 +0000 | [diff] [blame] | 1753 | if (RA.isPowerOf2()) { |
| 1754 | APInt LowBits = (RA - 1); |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1755 | APInt Mask2 = LowBits & DemandedMask; |
| 1756 | KnownZero |= ~LowBits & DemandedMask; |
| 1757 | if (SimplifyDemandedBits(I->getOperand(0), Mask2, |
| 1758 | KnownZero, KnownOne, Depth+1)) |
| 1759 | return true; |
| 1760 | |
| 1761 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1762 | break; |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1763 | } |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1764 | } |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1765 | |
| 1766 | APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0); |
| 1767 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); |
Dan Gohman | e85b758 | 2008-05-01 19:13:24 +0000 | [diff] [blame] | 1768 | if (SimplifyDemandedBits(I->getOperand(0), AllOnes, |
| 1769 | KnownZero2, KnownOne2, Depth+1)) |
| 1770 | return true; |
| 1771 | |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1772 | uint32_t Leaders = KnownZero2.countLeadingOnes(); |
Dan Gohman | e85b758 | 2008-05-01 19:13:24 +0000 | [diff] [blame] | 1773 | if (SimplifyDemandedBits(I->getOperand(1), AllOnes, |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1774 | KnownZero2, KnownOne2, Depth+1)) |
| 1775 | return true; |
| 1776 | |
| 1777 | Leaders = std::max(Leaders, |
| 1778 | KnownZero2.countLeadingOnes()); |
| 1779 | KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask; |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1780 | break; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1781 | } |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1782 | } |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1783 | |
| 1784 | // If the client is only demanding bits that we know, return the known |
| 1785 | // constant. |
| 1786 | if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) |
| 1787 | return UpdateValueUsesWith(I, ConstantInt::get(RHSKnownOne)); |
| 1788 | return false; |
| 1789 | } |
| 1790 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1791 | |
| 1792 | /// SimplifyDemandedVectorElts - The specified value producecs a vector with |
| 1793 | /// 64 or fewer elements. DemandedElts contains the set of elements that are |
| 1794 | /// actually used by the caller. This method analyzes which elements of the |
| 1795 | /// operand are undef and returns that information in UndefElts. |
| 1796 | /// |
| 1797 | /// If the information about demanded elements can be used to simplify the |
| 1798 | /// operation, the operation is simplified, then the resultant value is |
| 1799 | /// returned. This returns null if no change was made. |
| 1800 | Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, |
| 1801 | uint64_t &UndefElts, |
| 1802 | unsigned Depth) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1803 | unsigned VWidth = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1804 | assert(VWidth <= 64 && "Vector too wide to analyze!"); |
| 1805 | uint64_t EltMask = ~0ULL >> (64-VWidth); |
| 1806 | assert(DemandedElts != EltMask && (DemandedElts & ~EltMask) == 0 && |
| 1807 | "Invalid DemandedElts!"); |
| 1808 | |
| 1809 | if (isa<UndefValue>(V)) { |
| 1810 | // If the entire vector is undefined, just return this info. |
| 1811 | UndefElts = EltMask; |
| 1812 | return 0; |
| 1813 | } else if (DemandedElts == 0) { // If nothing is demanded, provide undef. |
| 1814 | UndefElts = EltMask; |
| 1815 | return UndefValue::get(V->getType()); |
| 1816 | } |
| 1817 | |
| 1818 | UndefElts = 0; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1819 | if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) { |
| 1820 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1821 | Constant *Undef = UndefValue::get(EltTy); |
| 1822 | |
| 1823 | std::vector<Constant*> Elts; |
| 1824 | for (unsigned i = 0; i != VWidth; ++i) |
| 1825 | if (!(DemandedElts & (1ULL << i))) { // If not demanded, set to undef. |
| 1826 | Elts.push_back(Undef); |
| 1827 | UndefElts |= (1ULL << i); |
| 1828 | } else if (isa<UndefValue>(CP->getOperand(i))) { // Already undef. |
| 1829 | Elts.push_back(Undef); |
| 1830 | UndefElts |= (1ULL << i); |
| 1831 | } else { // Otherwise, defined. |
| 1832 | Elts.push_back(CP->getOperand(i)); |
| 1833 | } |
| 1834 | |
| 1835 | // If we changed the constant, return it. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1836 | Constant *NewCP = ConstantVector::get(Elts); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1837 | return NewCP != CP ? NewCP : 0; |
| 1838 | } else if (isa<ConstantAggregateZero>(V)) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1839 | // Simplify the CAZ to a ConstantVector where the non-demanded elements are |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1840 | // set to undef. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1841 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1842 | Constant *Zero = Constant::getNullValue(EltTy); |
| 1843 | Constant *Undef = UndefValue::get(EltTy); |
| 1844 | std::vector<Constant*> Elts; |
| 1845 | for (unsigned i = 0; i != VWidth; ++i) |
| 1846 | Elts.push_back((DemandedElts & (1ULL << i)) ? Zero : Undef); |
| 1847 | UndefElts = DemandedElts ^ EltMask; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1848 | return ConstantVector::get(Elts); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
| 1851 | if (!V->hasOneUse()) { // Other users may use these bits. |
| 1852 | if (Depth != 0) { // Not at the root. |
| 1853 | // TODO: Just compute the UndefElts information recursively. |
| 1854 | return false; |
| 1855 | } |
| 1856 | return false; |
| 1857 | } else if (Depth == 10) { // Limit search depth. |
| 1858 | return false; |
| 1859 | } |
| 1860 | |
| 1861 | Instruction *I = dyn_cast<Instruction>(V); |
| 1862 | if (!I) return false; // Only analyze instructions. |
| 1863 | |
| 1864 | bool MadeChange = false; |
| 1865 | uint64_t UndefElts2; |
| 1866 | Value *TmpV; |
| 1867 | switch (I->getOpcode()) { |
| 1868 | default: break; |
| 1869 | |
| 1870 | case Instruction::InsertElement: { |
| 1871 | // If this is a variable index, we don't know which element it overwrites. |
| 1872 | // demand exactly the same input as we produce. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1873 | ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2)); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1874 | if (Idx == 0) { |
| 1875 | // Note that we can't propagate undef elt info, because we don't know |
| 1876 | // which elt is getting updated. |
| 1877 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1878 | UndefElts2, Depth+1); |
| 1879 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1880 | break; |
| 1881 | } |
| 1882 | |
| 1883 | // If this is inserting an element that isn't demanded, remove this |
| 1884 | // insertelement. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1885 | unsigned IdxNo = Idx->getZExtValue(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1886 | if (IdxNo >= VWidth || (DemandedElts & (1ULL << IdxNo)) == 0) |
| 1887 | return AddSoonDeadInstToWorklist(*I, 0); |
| 1888 | |
| 1889 | // Otherwise, the element inserted overwrites whatever was there, so the |
| 1890 | // input demanded set is simpler than the output set. |
| 1891 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), |
| 1892 | DemandedElts & ~(1ULL << IdxNo), |
| 1893 | UndefElts, Depth+1); |
| 1894 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1895 | |
| 1896 | // The inserted element is defined. |
| 1897 | UndefElts |= 1ULL << IdxNo; |
| 1898 | break; |
| 1899 | } |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1900 | case Instruction::BitCast: { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 1901 | // Vector->vector casts only. |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1902 | const VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType()); |
| 1903 | if (!VTy) break; |
| 1904 | unsigned InVWidth = VTy->getNumElements(); |
| 1905 | uint64_t InputDemandedElts = 0; |
| 1906 | unsigned Ratio; |
| 1907 | |
| 1908 | if (VWidth == InVWidth) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 1909 | // If we are converting from <4 x i32> -> <4 x f32>, we demand the same |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1910 | // elements as are demanded of us. |
| 1911 | Ratio = 1; |
| 1912 | InputDemandedElts = DemandedElts; |
| 1913 | } else if (VWidth > InVWidth) { |
| 1914 | // Untested so far. |
| 1915 | break; |
| 1916 | |
| 1917 | // If there are more elements in the result than there are in the source, |
| 1918 | // then an input element is live if any of the corresponding output |
| 1919 | // elements are live. |
| 1920 | Ratio = VWidth/InVWidth; |
| 1921 | for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) { |
| 1922 | if (DemandedElts & (1ULL << OutIdx)) |
| 1923 | InputDemandedElts |= 1ULL << (OutIdx/Ratio); |
| 1924 | } |
| 1925 | } else { |
| 1926 | // Untested so far. |
| 1927 | break; |
| 1928 | |
| 1929 | // If there are more elements in the source than there are in the result, |
| 1930 | // then an input element is live if the corresponding output element is |
| 1931 | // live. |
| 1932 | Ratio = InVWidth/VWidth; |
| 1933 | for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx) |
| 1934 | if (DemandedElts & (1ULL << InIdx/Ratio)) |
| 1935 | InputDemandedElts |= 1ULL << InIdx; |
| 1936 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1937 | |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1938 | // div/rem demand all inputs, because they don't want divide by zero. |
| 1939 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts, |
| 1940 | UndefElts2, Depth+1); |
| 1941 | if (TmpV) { |
| 1942 | I->setOperand(0, TmpV); |
| 1943 | MadeChange = true; |
| 1944 | } |
| 1945 | |
| 1946 | UndefElts = UndefElts2; |
| 1947 | if (VWidth > InVWidth) { |
| 1948 | assert(0 && "Unimp"); |
| 1949 | // If there are more elements in the result than there are in the source, |
| 1950 | // then an output element is undef if the corresponding input element is |
| 1951 | // undef. |
| 1952 | for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) |
| 1953 | if (UndefElts2 & (1ULL << (OutIdx/Ratio))) |
| 1954 | UndefElts |= 1ULL << OutIdx; |
| 1955 | } else if (VWidth < InVWidth) { |
| 1956 | assert(0 && "Unimp"); |
| 1957 | // If there are more elements in the source than there are in the result, |
| 1958 | // then a result element is undef if all of the corresponding input |
| 1959 | // elements are undef. |
| 1960 | UndefElts = ~0ULL >> (64-VWidth); // Start out all undef. |
| 1961 | for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx) |
| 1962 | if ((UndefElts2 & (1ULL << InIdx)) == 0) // Not undef? |
| 1963 | UndefElts &= ~(1ULL << (InIdx/Ratio)); // Clear undef bit. |
| 1964 | } |
| 1965 | break; |
| 1966 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1967 | case Instruction::And: |
| 1968 | case Instruction::Or: |
| 1969 | case Instruction::Xor: |
| 1970 | case Instruction::Add: |
| 1971 | case Instruction::Sub: |
| 1972 | case Instruction::Mul: |
| 1973 | // div/rem demand all inputs, because they don't want divide by zero. |
| 1974 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1975 | UndefElts, Depth+1); |
| 1976 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1977 | TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts, |
| 1978 | UndefElts2, Depth+1); |
| 1979 | if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; } |
| 1980 | |
| 1981 | // Output elements are undefined if both are undefined. Consider things |
| 1982 | // like undef&0. The result is known zero, not undef. |
| 1983 | UndefElts &= UndefElts2; |
| 1984 | break; |
| 1985 | |
| 1986 | case Instruction::Call: { |
| 1987 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(I); |
| 1988 | if (!II) break; |
| 1989 | switch (II->getIntrinsicID()) { |
| 1990 | default: break; |
| 1991 | |
| 1992 | // Binary vector operations that work column-wise. A dest element is a |
| 1993 | // function of the corresponding input elements from the two inputs. |
| 1994 | case Intrinsic::x86_sse_sub_ss: |
| 1995 | case Intrinsic::x86_sse_mul_ss: |
| 1996 | case Intrinsic::x86_sse_min_ss: |
| 1997 | case Intrinsic::x86_sse_max_ss: |
| 1998 | case Intrinsic::x86_sse2_sub_sd: |
| 1999 | case Intrinsic::x86_sse2_mul_sd: |
| 2000 | case Intrinsic::x86_sse2_min_sd: |
| 2001 | case Intrinsic::x86_sse2_max_sd: |
| 2002 | TmpV = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts, |
| 2003 | UndefElts, Depth+1); |
| 2004 | if (TmpV) { II->setOperand(1, TmpV); MadeChange = true; } |
| 2005 | TmpV = SimplifyDemandedVectorElts(II->getOperand(2), DemandedElts, |
| 2006 | UndefElts2, Depth+1); |
| 2007 | if (TmpV) { II->setOperand(2, TmpV); MadeChange = true; } |
| 2008 | |
| 2009 | // If only the low elt is demanded and this is a scalarizable intrinsic, |
| 2010 | // scalarize it now. |
| 2011 | if (DemandedElts == 1) { |
| 2012 | switch (II->getIntrinsicID()) { |
| 2013 | default: break; |
| 2014 | case Intrinsic::x86_sse_sub_ss: |
| 2015 | case Intrinsic::x86_sse_mul_ss: |
| 2016 | case Intrinsic::x86_sse2_sub_sd: |
| 2017 | case Intrinsic::x86_sse2_mul_sd: |
| 2018 | // TODO: Lower MIN/MAX/ABS/etc |
| 2019 | Value *LHS = II->getOperand(1); |
| 2020 | Value *RHS = II->getOperand(2); |
| 2021 | // Extract the element as scalars. |
| 2022 | LHS = InsertNewInstBefore(new ExtractElementInst(LHS, 0U,"tmp"), *II); |
| 2023 | RHS = InsertNewInstBefore(new ExtractElementInst(RHS, 0U,"tmp"), *II); |
| 2024 | |
| 2025 | switch (II->getIntrinsicID()) { |
| 2026 | default: assert(0 && "Case stmts out of sync!"); |
| 2027 | case Intrinsic::x86_sse_sub_ss: |
| 2028 | case Intrinsic::x86_sse2_sub_sd: |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2029 | TmpV = InsertNewInstBefore(BinaryOperator::CreateSub(LHS, RHS, |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 2030 | II->getName()), *II); |
| 2031 | break; |
| 2032 | case Intrinsic::x86_sse_mul_ss: |
| 2033 | case Intrinsic::x86_sse2_mul_sd: |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2034 | TmpV = InsertNewInstBefore(BinaryOperator::CreateMul(LHS, RHS, |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 2035 | II->getName()), *II); |
| 2036 | break; |
| 2037 | } |
| 2038 | |
| 2039 | Instruction *New = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2040 | InsertElementInst::Create(UndefValue::get(II->getType()), TmpV, 0U, |
| 2041 | II->getName()); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 2042 | InsertNewInstBefore(New, *II); |
| 2043 | AddSoonDeadInstToWorklist(*II, 0); |
| 2044 | return New; |
| 2045 | } |
| 2046 | } |
| 2047 | |
| 2048 | // Output elements are undefined if both are undefined. Consider things |
| 2049 | // like undef&0. The result is known zero, not undef. |
| 2050 | UndefElts &= UndefElts2; |
| 2051 | break; |
| 2052 | } |
| 2053 | break; |
| 2054 | } |
| 2055 | } |
| 2056 | return MadeChange ? I : 0; |
| 2057 | } |
| 2058 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2059 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
| 2060 | /// function is designed to check a chain of associative operators for a |
| 2061 | /// potential to apply a certain optimization. Since the optimization may be |
| 2062 | /// applicable if the expression was reassociated, this checks the chain, then |
| 2063 | /// reassociates the expression as necessary to expose the optimization |
| 2064 | /// opportunity. This makes use of a special Functor, which must define |
| 2065 | /// 'shouldApply' and 'apply' methods. |
| 2066 | /// |
| 2067 | template<typename Functor> |
| 2068 | Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { |
| 2069 | unsigned Opcode = Root.getOpcode(); |
| 2070 | Value *LHS = Root.getOperand(0); |
| 2071 | |
| 2072 | // Quick check, see if the immediate LHS matches... |
| 2073 | if (F.shouldApply(LHS)) |
| 2074 | return F.apply(Root); |
| 2075 | |
| 2076 | // Otherwise, if the LHS is not of the same opcode as the root, return. |
| 2077 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2078 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2079 | // Should we apply this transform to the RHS? |
| 2080 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); |
| 2081 | |
| 2082 | // If not to the RHS, check to see if we should apply to the LHS... |
| 2083 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { |
| 2084 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS |
| 2085 | ShouldApply = true; |
| 2086 | } |
| 2087 | |
| 2088 | // If the functor wants to apply the optimization to the RHS of LHSI, |
| 2089 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) |
| 2090 | if (ShouldApply) { |
| 2091 | BasicBlock *BB = Root.getParent(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2092 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2093 | // Now all of the instructions are in the current basic block, go ahead |
| 2094 | // and perform the reassociation. |
| 2095 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); |
| 2096 | |
| 2097 | // First move the selected RHS to the LHS of the root... |
| 2098 | Root.setOperand(0, LHSI->getOperand(1)); |
| 2099 | |
| 2100 | // Make what used to be the LHS of the root be the user of the root... |
| 2101 | Value *ExtraOperand = TmpLHSI->getOperand(1); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 2102 | if (&Root == TmpLHSI) { |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 2103 | Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); |
| 2104 | return 0; |
| 2105 | } |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 2106 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2107 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 2108 | TmpLHSI->getParent()->getInstList().remove(TmpLHSI); |
| 2109 | BasicBlock::iterator ARI = &Root; ++ARI; |
| 2110 | BB->getInstList().insert(ARI, TmpLHSI); // Move TmpLHSI to after Root |
| 2111 | ARI = Root; |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2112 | |
| 2113 | // Now propagate the ExtraOperand down the chain of instructions until we |
| 2114 | // get to LHSI. |
| 2115 | while (TmpLHSI != LHSI) { |
| 2116 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 2117 | // Move the instruction to immediately before the chain we are |
| 2118 | // constructing to avoid breaking dominance properties. |
| 2119 | NextLHSI->getParent()->getInstList().remove(NextLHSI); |
| 2120 | BB->getInstList().insert(ARI, NextLHSI); |
| 2121 | ARI = NextLHSI; |
| 2122 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2123 | Value *NextOp = NextLHSI->getOperand(1); |
| 2124 | NextLHSI->setOperand(1, ExtraOperand); |
| 2125 | TmpLHSI = NextLHSI; |
| 2126 | ExtraOperand = NextOp; |
| 2127 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2128 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2129 | // Now that the instructions are reassociated, have the functor perform |
| 2130 | // the transformation... |
| 2131 | return F.apply(Root); |
| 2132 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2133 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2134 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
| 2135 | } |
| 2136 | return 0; |
| 2137 | } |
| 2138 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 2139 | namespace { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2140 | |
| 2141 | // AddRHS - Implements: X + X --> X << 1 |
| 2142 | struct AddRHS { |
| 2143 | Value *RHS; |
| 2144 | AddRHS(Value *rhs) : RHS(rhs) {} |
| 2145 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 2146 | Instruction *apply(BinaryOperator &Add) const { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2147 | return BinaryOperator::CreateShl(Add.getOperand(0), |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2148 | ConstantInt::get(Add.getType(), 1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2149 | } |
| 2150 | }; |
| 2151 | |
| 2152 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) |
| 2153 | // iff C1&C2 == 0 |
| 2154 | struct AddMaskingAnd { |
| 2155 | Constant *C2; |
| 2156 | AddMaskingAnd(Constant *c) : C2(c) {} |
| 2157 | bool shouldApply(Value *LHS) const { |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2158 | ConstantInt *C1; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2159 | return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2160 | ConstantExpr::getAnd(C1, C2)->isNullValue(); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2161 | } |
| 2162 | Instruction *apply(BinaryOperator &Add) const { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2163 | return BinaryOperator::CreateOr(Add.getOperand(0), Add.getOperand(1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2164 | } |
| 2165 | }; |
| 2166 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2169 | static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2170 | InstCombiner *IC) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2171 | if (CastInst *CI = dyn_cast<CastInst>(&I)) { |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2172 | if (Constant *SOC = dyn_cast<Constant>(SO)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2173 | return ConstantExpr::getCast(CI->getOpcode(), SOC, I.getType()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2174 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2175 | return IC->InsertNewInstBefore(CastInst::Create( |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2176 | CI->getOpcode(), SO, I.getType(), SO->getName() + ".cast"), I); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2179 | // Figure out if the constant is the left or the right argument. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2180 | bool ConstIsRHS = isa<Constant>(I.getOperand(1)); |
| 2181 | Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2182 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2183 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
| 2184 | if (ConstIsRHS) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2185 | return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand); |
| 2186 | return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC); |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
| 2189 | Value *Op0 = SO, *Op1 = ConstOperand; |
| 2190 | if (!ConstIsRHS) |
| 2191 | std::swap(Op0, Op1); |
| 2192 | Instruction *New; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2193 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2194 | New = BinaryOperator::Create(BO->getOpcode(), Op0, Op1,SO->getName()+".op"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2195 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2196 | New = CmpInst::Create(CI->getOpcode(), CI->getPredicate(), Op0, Op1, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2197 | SO->getName()+".cmp"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 2198 | else { |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2199 | assert(0 && "Unknown binary instruction type!"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 2200 | abort(); |
| 2201 | } |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2202 | return IC->InsertNewInstBefore(New, I); |
| 2203 | } |
| 2204 | |
| 2205 | // FoldOpIntoSelect - Given an instruction with a select as one operand and a |
| 2206 | // constant as the other operand, try to fold the binary operator into the |
| 2207 | // select arguments. This also works for Cast instructions, which obviously do |
| 2208 | // not have a second operand. |
| 2209 | static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI, |
| 2210 | InstCombiner *IC) { |
| 2211 | // Don't modify shared select instructions |
| 2212 | if (!SI->hasOneUse()) return 0; |
| 2213 | Value *TV = SI->getOperand(1); |
| 2214 | Value *FV = SI->getOperand(2); |
| 2215 | |
| 2216 | if (isa<Constant>(TV) || isa<Constant>(FV)) { |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 2217 | // Bool selects with constant operands can be folded to logical ops. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2218 | if (SI->getType() == Type::Int1Ty) return 0; |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 2219 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2220 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC); |
| 2221 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC); |
| 2222 | |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2223 | return SelectInst::Create(SI->getCondition(), SelectTrueVal, |
| 2224 | SelectFalseVal); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2225 | } |
| 2226 | return 0; |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2229 | |
| 2230 | /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI |
| 2231 | /// node as operand #0, see if we can fold the instruction into the PHI (which |
| 2232 | /// is only possible if all operands to the PHI are constants). |
| 2233 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { |
| 2234 | PHINode *PN = cast<PHINode>(I.getOperand(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 2235 | unsigned NumPHIValues = PN->getNumIncomingValues(); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2236 | if (!PN->hasOneUse() || NumPHIValues == 0) return 0; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2237 | |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2238 | // Check to see if all of the operands of the PHI are constants. If there is |
| 2239 | // one non-constant value, remember the BB it is. If there is more than one |
Chris Lattner | b303668 | 2007-02-24 01:03:45 +0000 | [diff] [blame] | 2240 | // or if *it* is a PHI, bail out. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2241 | BasicBlock *NonConstBB = 0; |
| 2242 | for (unsigned i = 0; i != NumPHIValues; ++i) |
| 2243 | if (!isa<Constant>(PN->getIncomingValue(i))) { |
| 2244 | if (NonConstBB) return 0; // More than one non-const value. |
Chris Lattner | b303668 | 2007-02-24 01:03:45 +0000 | [diff] [blame] | 2245 | if (isa<PHINode>(PN->getIncomingValue(i))) return 0; // Itself a phi. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2246 | NonConstBB = PN->getIncomingBlock(i); |
| 2247 | |
| 2248 | // If the incoming non-constant value is in I's block, we have an infinite |
| 2249 | // loop. |
| 2250 | if (NonConstBB == I.getParent()) |
| 2251 | return 0; |
| 2252 | } |
| 2253 | |
| 2254 | // If there is exactly one non-constant value, we can insert a copy of the |
| 2255 | // operation in that block. However, if this is a critical edge, we would be |
| 2256 | // inserting the computation one some other paths (e.g. inside a loop). Only |
| 2257 | // do this if the pred block is unconditionally branching into the phi block. |
| 2258 | if (NonConstBB) { |
| 2259 | BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator()); |
| 2260 | if (!BI || !BI->isUnconditional()) return 0; |
| 2261 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2262 | |
| 2263 | // Okay, we can do the transformation: create the new PHI node. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2264 | PHINode *NewPN = PHINode::Create(I.getType(), ""); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 2265 | NewPN->reserveOperandSpace(PN->getNumOperands()/2); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2266 | InsertNewInstBefore(NewPN, *PN); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2267 | NewPN->takeName(PN); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2268 | |
| 2269 | // Next, add all of the operands to the PHI. |
| 2270 | if (I.getNumOperands() == 2) { |
| 2271 | Constant *C = cast<Constant>(I.getOperand(1)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 2272 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 2273 | Value *InV = 0; |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2274 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2275 | if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 2276 | InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C); |
| 2277 | else |
| 2278 | InV = ConstantExpr::get(I.getOpcode(), InC, C); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2279 | } else { |
| 2280 | assert(PN->getIncomingBlock(i) == NonConstBB); |
| 2281 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2282 | InV = BinaryOperator::Create(BO->getOpcode(), |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2283 | PN->getIncomingValue(i), C, "phitmp", |
| 2284 | NonConstBB->getTerminator()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2285 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2286 | InV = CmpInst::Create(CI->getOpcode(), |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2287 | CI->getPredicate(), |
| 2288 | PN->getIncomingValue(i), C, "phitmp", |
| 2289 | NonConstBB->getTerminator()); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2290 | else |
| 2291 | assert(0 && "Unknown binop!"); |
| 2292 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 2293 | AddToWorkList(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2294 | } |
| 2295 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2296 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2297 | } else { |
| 2298 | CastInst *CI = cast<CastInst>(&I); |
| 2299 | const Type *RetTy = CI->getType(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 2300 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2301 | Value *InV; |
| 2302 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2303 | InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2304 | } else { |
| 2305 | assert(PN->getIncomingBlock(i) == NonConstBB); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2306 | InV = CastInst::Create(CI->getOpcode(), PN->getIncomingValue(i), |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2307 | I.getType(), "phitmp", |
| 2308 | NonConstBB->getTerminator()); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 2309 | AddToWorkList(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2310 | } |
| 2311 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2312 | } |
| 2313 | } |
| 2314 | return ReplaceInstUsesWith(I, NewPN); |
| 2315 | } |
| 2316 | |
Chris Lattner | 2454a2e | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 2317 | |
| 2318 | /// CannotBeNegativeZero - Return true if we can prove that the specified FP |
| 2319 | /// value is never equal to -0.0. |
| 2320 | /// |
| 2321 | /// Note that this function will need to be revisited when we support nondefault |
| 2322 | /// rounding modes! |
| 2323 | /// |
| 2324 | static bool CannotBeNegativeZero(const Value *V) { |
| 2325 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) |
| 2326 | return !CFP->getValueAPF().isNegZero(); |
| 2327 | |
| 2328 | // (add x, 0.0) is guaranteed to return +0.0, not -0.0. |
| 2329 | if (const Instruction *I = dyn_cast<Instruction>(V)) { |
| 2330 | if (I->getOpcode() == Instruction::Add && |
| 2331 | isa<ConstantFP>(I->getOperand(1)) && |
| 2332 | cast<ConstantFP>(I->getOperand(1))->isNullValue()) |
| 2333 | return true; |
| 2334 | |
| 2335 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) |
| 2336 | if (II->getIntrinsicID() == Intrinsic::sqrt) |
| 2337 | return CannotBeNegativeZero(II->getOperand(1)); |
| 2338 | |
| 2339 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 2340 | if (const Function *F = CI->getCalledFunction()) { |
| 2341 | if (F->isDeclaration()) { |
| 2342 | switch (F->getNameLen()) { |
| 2343 | case 3: // abs(x) != -0.0 |
| 2344 | if (!strcmp(F->getNameStart(), "abs")) return true; |
| 2345 | break; |
| 2346 | case 4: // abs[lf](x) != -0.0 |
| 2347 | if (!strcmp(F->getNameStart(), "absf")) return true; |
| 2348 | if (!strcmp(F->getNameStart(), "absl")) return true; |
| 2349 | break; |
| 2350 | } |
| 2351 | } |
| 2352 | } |
| 2353 | } |
| 2354 | |
| 2355 | return false; |
| 2356 | } |
| 2357 | |
| 2358 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2359 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2360 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2361 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2362 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2363 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2364 | // X + undef -> undef |
| 2365 | if (isa<UndefValue>(RHS)) |
| 2366 | return ReplaceInstUsesWith(I, RHS); |
| 2367 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2368 | // X + 0 --> X |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2369 | if (!I.getType()->isFPOrFPVector()) { // NOTE: -0 + +0 = +0. |
Chris Lattner | 5e678e0 | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 2370 | if (RHSC->isNullValue()) |
| 2371 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 8532cf6 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 2372 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 2373 | if (CFP->isExactlyValue(ConstantFP::getNegativeZero |
| 2374 | (I.getType())->getValueAPF())) |
Chris Lattner | 8532cf6 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 2375 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 5e678e0 | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 2376 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2377 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2378 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 2379 | // X + (signbit) --> X ^ signbit |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2380 | const APInt& Val = CI->getValue(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2381 | uint32_t BitWidth = Val.getBitWidth(); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2382 | if (Val == APInt::getSignBit(BitWidth)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2383 | return BinaryOperator::CreateXor(LHS, RHS); |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 2384 | |
| 2385 | // See if SimplifyDemandedBits can simplify this. This handles stuff like |
| 2386 | // (X & 254)+1 -> (X&254)|1 |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2387 | if (!isa<VectorType>(I.getType())) { |
| 2388 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 2389 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 2390 | KnownZero, KnownOne)) |
| 2391 | return &I; |
| 2392 | } |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2393 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2394 | |
| 2395 | if (isa<PHINode>(LHS)) |
| 2396 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2397 | return NV; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2398 | |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2399 | ConstantInt *XorRHS = 0; |
| 2400 | Value *XorLHS = 0; |
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 2401 | if (isa<ConstantInt>(RHSC) && |
| 2402 | match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2403 | uint32_t TySizeBits = I.getType()->getPrimitiveSizeInBits(); |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2404 | const APInt& RHSVal = cast<ConstantInt>(RHSC)->getValue(); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2405 | |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2406 | uint32_t Size = TySizeBits / 2; |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2407 | APInt C0080Val(APInt(TySizeBits, 1ULL).shl(Size - 1)); |
| 2408 | APInt CFF80Val(-C0080Val); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2409 | do { |
| 2410 | if (TySizeBits > Size) { |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2411 | // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext. |
| 2412 | // If we have ADD(XOR(AND(X, 0xFF), 0xF..F80), 0x80), it's a sext. |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2413 | if ((RHSVal == CFF80Val && XorRHS->getValue() == C0080Val) || |
| 2414 | (RHSVal == C0080Val && XorRHS->getValue() == CFF80Val)) { |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2415 | // This is a sign extend if the top bits are known zero. |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 2416 | if (!MaskedValueIsZero(XorLHS, |
| 2417 | APInt::getHighBitsSet(TySizeBits, TySizeBits - Size))) |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2418 | Size = 0; // Not a sign ext, but can't be any others either. |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2419 | break; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2420 | } |
| 2421 | } |
| 2422 | Size >>= 1; |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2423 | C0080Val = APIntOps::lshr(C0080Val, Size); |
| 2424 | CFF80Val = APIntOps::ashr(CFF80Val, Size); |
| 2425 | } while (Size >= 1); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2426 | |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 2427 | // FIXME: This shouldn't be necessary. When the backends can handle types |
| 2428 | // with funny bit widths then this whole cascade of if statements should |
| 2429 | // be removed. It is just here to get the size of the "middle" type back |
| 2430 | // up to something that the back ends can handle. |
| 2431 | const Type *MiddleType = 0; |
| 2432 | switch (Size) { |
| 2433 | default: break; |
| 2434 | case 32: MiddleType = Type::Int32Ty; break; |
| 2435 | case 16: MiddleType = Type::Int16Ty; break; |
| 2436 | case 8: MiddleType = Type::Int8Ty; break; |
| 2437 | } |
| 2438 | if (MiddleType) { |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 2439 | Instruction *NewTrunc = new TruncInst(XorLHS, MiddleType, "sext"); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2440 | InsertNewInstBefore(NewTrunc, I); |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 2441 | return new SExtInst(NewTrunc, I.getType(), I.getName()); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2442 | } |
| 2443 | } |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2444 | } |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2445 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2446 | // X + X --> X << 1 |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2447 | if (I.getType()->isInteger() && I.getType() != Type::Int1Ty) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2448 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2449 | |
| 2450 | if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) { |
| 2451 | if (RHSI->getOpcode() == Instruction::Sub) |
| 2452 | if (LHS == RHSI->getOperand(1)) // A + (B - A) --> B |
| 2453 | return ReplaceInstUsesWith(I, RHSI->getOperand(0)); |
| 2454 | } |
| 2455 | if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) { |
| 2456 | if (LHSI->getOpcode() == Instruction::Sub) |
| 2457 | if (RHS == LHSI->getOperand(1)) // (B - A) + A --> B |
| 2458 | return ReplaceInstUsesWith(I, LHSI->getOperand(0)); |
| 2459 | } |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2460 | } |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2461 | |
Chris Lattner | 5c4afb9 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 2462 | // -A + B --> B - A |
Chris Lattner | dd12f96 | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 2463 | // -A + -B --> -(A + B) |
| 2464 | if (Value *LHSV = dyn_castNegVal(LHS)) { |
Chris Lattner | e10c0b9 | 2008-02-18 17:50:16 +0000 | [diff] [blame] | 2465 | if (LHS->getType()->isIntOrIntVector()) { |
| 2466 | if (Value *RHSV = dyn_castNegVal(RHS)) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2467 | Instruction *NewAdd = BinaryOperator::CreateAdd(LHSV, RHSV, "sum"); |
Chris Lattner | e10c0b9 | 2008-02-18 17:50:16 +0000 | [diff] [blame] | 2468 | InsertNewInstBefore(NewAdd, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2469 | return BinaryOperator::CreateNeg(NewAdd); |
Chris Lattner | e10c0b9 | 2008-02-18 17:50:16 +0000 | [diff] [blame] | 2470 | } |
Chris Lattner | dd12f96 | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2473 | return BinaryOperator::CreateSub(RHS, LHSV); |
Chris Lattner | dd12f96 | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 2474 | } |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2475 | |
| 2476 | // A + -B --> A - B |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2477 | if (!isa<Constant>(RHS)) |
| 2478 | if (Value *V = dyn_castNegVal(RHS)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2479 | return BinaryOperator::CreateSub(LHS, V); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2480 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2481 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2482 | ConstantInt *C2; |
| 2483 | if (Value *X = dyn_castFoldableMul(LHS, C2)) { |
| 2484 | if (X == RHS) // X*C + X --> X * (C+1) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2485 | return BinaryOperator::CreateMul(RHS, AddOne(C2)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2486 | |
| 2487 | // X*C1 + X*C2 --> X * (C1+C2) |
| 2488 | ConstantInt *C1; |
| 2489 | if (X == dyn_castFoldableMul(RHS, C1)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2490 | return BinaryOperator::CreateMul(X, Add(C1, C2)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2491 | } |
| 2492 | |
| 2493 | // X + X*C --> X * (C+1) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2494 | if (dyn_castFoldableMul(RHS, C2) == LHS) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2495 | return BinaryOperator::CreateMul(LHS, AddOne(C2)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2496 | |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2497 | // X + ~X --> -1 since ~X = -X-1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 2498 | if (dyn_castNotVal(LHS) == RHS || dyn_castNotVal(RHS) == LHS) |
| 2499 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2500 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2501 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2502 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2503 | if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2504 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) |
| 2505 | return R; |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 2506 | |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2507 | // W*X + Y*Z --> W * (X+Z) iff W == Y |
Nick Lewycky | 0c2c3f6 | 2008-02-03 08:19:11 +0000 | [diff] [blame] | 2508 | if (I.getType()->isIntOrIntVector()) { |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2509 | Value *W, *X, *Y, *Z; |
| 2510 | if (match(LHS, m_Mul(m_Value(W), m_Value(X))) && |
| 2511 | match(RHS, m_Mul(m_Value(Y), m_Value(Z)))) { |
| 2512 | if (W != Y) { |
| 2513 | if (W == Z) { |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 2514 | std::swap(Y, Z); |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2515 | } else if (Y == X) { |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 2516 | std::swap(W, X); |
| 2517 | } else if (X == Z) { |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2518 | std::swap(Y, Z); |
| 2519 | std::swap(W, X); |
| 2520 | } |
| 2521 | } |
| 2522 | |
| 2523 | if (W == Y) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2524 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::CreateAdd(X, Z, |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2525 | LHS->getName()), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2526 | return BinaryOperator::CreateMul(W, NewAdd); |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2527 | } |
| 2528 | } |
| 2529 | } |
| 2530 | |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 2531 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2532 | Value *X = 0; |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2533 | if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2534 | return BinaryOperator::CreateSub(SubOne(CRHS), X); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2535 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2536 | // (X & FF00) + xx00 -> (X+xx00) & FF00 |
| 2537 | if (LHS->hasOneUse() && match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) { |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2538 | Constant *Anded = And(CRHS, C2); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2539 | if (Anded == CRHS) { |
| 2540 | // See if all bits from the first bit set in the Add RHS up are included |
| 2541 | // in the mask. First, get the rightmost bit. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2542 | const APInt& AddRHSV = CRHS->getValue(); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2543 | |
| 2544 | // Form a mask of all bits from the lowest bit added through the top. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2545 | APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1)); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2546 | |
| 2547 | // See if the and mask includes all of these bits. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2548 | APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2549 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2550 | if (AddRHSHighBits == AddRHSHighBitsAnd) { |
| 2551 | // Okay, the xform is safe. Insert the new add pronto. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2552 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::CreateAdd(X, CRHS, |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2553 | LHS->getName()), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2554 | return BinaryOperator::CreateAnd(NewAdd, C2); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2555 | } |
| 2556 | } |
| 2557 | } |
| 2558 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2559 | // Try to fold constant add into select arguments. |
| 2560 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2561 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2562 | return R; |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 2563 | } |
| 2564 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2565 | // add (cast *A to intptrtype) B -> |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2566 | // cast (GEP (cast *A to sbyte*) B) --> intptrtype |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2567 | { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2568 | CastInst *CI = dyn_cast<CastInst>(LHS); |
| 2569 | Value *Other = RHS; |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2570 | if (!CI) { |
| 2571 | CI = dyn_cast<CastInst>(RHS); |
| 2572 | Other = LHS; |
| 2573 | } |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2574 | if (CI && CI->getType()->isSized() && |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 2575 | (CI->getType()->getPrimitiveSizeInBits() == |
| 2576 | TD->getIntPtrType()->getPrimitiveSizeInBits()) |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2577 | && isa<PointerType>(CI->getOperand(0)->getType())) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 2578 | unsigned AS = |
| 2579 | cast<PointerType>(CI->getOperand(0)->getType())->getAddressSpace(); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 2580 | Value *I2 = InsertBitCastBefore(CI->getOperand(0), |
| 2581 | PointerType::get(Type::Int8Ty, AS), I); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2582 | I2 = InsertNewInstBefore(GetElementPtrInst::Create(I2, Other, "ctg2"), I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2583 | return new PtrToIntInst(I2, CI->getType()); |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2584 | } |
| 2585 | } |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2586 | |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2587 | // add (select X 0 (sub n A)) A --> select X A n |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2588 | { |
| 2589 | SelectInst *SI = dyn_cast<SelectInst>(LHS); |
| 2590 | Value *Other = RHS; |
| 2591 | if (!SI) { |
| 2592 | SI = dyn_cast<SelectInst>(RHS); |
| 2593 | Other = LHS; |
| 2594 | } |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2595 | if (SI && SI->hasOneUse()) { |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2596 | Value *TV = SI->getTrueValue(); |
| 2597 | Value *FV = SI->getFalseValue(); |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2598 | Value *A, *N; |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2599 | |
| 2600 | // Can we fold the add into the argument of the select? |
| 2601 | // We check both true and false select arguments for a matching subtract. |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2602 | if (match(FV, m_Zero()) && match(TV, m_Sub(m_Value(N), m_Value(A))) && |
| 2603 | A == Other) // Fold the add into the true select value. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2604 | return SelectInst::Create(SI->getCondition(), N, A); |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2605 | if (match(TV, m_Zero()) && match(FV, m_Sub(m_Value(N), m_Value(A))) && |
| 2606 | A == Other) // Fold the add into the false select value. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2607 | return SelectInst::Create(SI->getCondition(), A, N); |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2608 | } |
| 2609 | } |
Chris Lattner | 2454a2e | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 2610 | |
| 2611 | // Check for X+0.0. Simplify it to X if we know X is not -0.0. |
| 2612 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) |
| 2613 | if (CFP->getValueAPF().isPosZero() && CannotBeNegativeZero(LHS)) |
| 2614 | return ReplaceInstUsesWith(I, LHS); |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2615 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2616 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2617 | } |
| 2618 | |
Chris Lattner | 1ba5bcd | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 2619 | // isSignBit - Return true if the value represented by the constant only has the |
| 2620 | // highest order bit set. |
| 2621 | static bool isSignBit(ConstantInt *CI) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2622 | uint32_t NumBits = CI->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 5a1e3e1 | 2007-03-19 20:58:18 +0000 | [diff] [blame] | 2623 | return CI->getValue() == APInt::getSignBit(NumBits); |
Chris Lattner | 1ba5bcd | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 2624 | } |
| 2625 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2626 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2627 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2628 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2629 | if (Op0 == Op1) // sub X, X -> 0 |
| 2630 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2631 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2632 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2633 | if (Value *V = dyn_castNegVal(Op1)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2634 | return BinaryOperator::CreateAdd(Op0, V); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2635 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2636 | if (isa<UndefValue>(Op0)) |
| 2637 | return ReplaceInstUsesWith(I, Op0); // undef - X -> undef |
| 2638 | if (isa<UndefValue>(Op1)) |
| 2639 | return ReplaceInstUsesWith(I, Op1); // X - undef -> undef |
| 2640 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2641 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
| 2642 | // Replace (-1 - A) with (~A)... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2643 | if (C->isAllOnesValue()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2644 | return BinaryOperator::CreateNot(Op1); |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2645 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2646 | // C - ~X == X + (1+C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2647 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2648 | if (match(Op1, m_Not(m_Value(X)))) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2649 | return BinaryOperator::CreateAdd(X, AddOne(C)); |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2650 | |
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 2651 | // -(X >>u 31) -> (X >>s 31) |
| 2652 | // -(X >>s 31) -> (X >>u 31) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 2653 | if (C->isZero()) { |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2654 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op1)) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2655 | if (SI->getOpcode() == Instruction::LShr) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2656 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 2657 | // Check to see if we are shifting out everything but the sign bit. |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 2658 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2659 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2660 | // Ok, the transformation is safe. Insert AShr. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2661 | return BinaryOperator::Create(Instruction::AShr, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2662 | SI->getOperand(0), CU, SI->getName()); |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 2663 | } |
| 2664 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2665 | } |
| 2666 | else if (SI->getOpcode() == Instruction::AShr) { |
| 2667 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
| 2668 | // Check to see if we are shifting out everything but the sign bit. |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 2669 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2670 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 2671 | // Ok, the transformation is safe. Insert LShr. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2672 | return BinaryOperator::CreateLShr( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2673 | SI->getOperand(0), CU, SI->getName()); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2674 | } |
| 2675 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2676 | } |
| 2677 | } |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 2678 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2679 | |
| 2680 | // Try to fold constant sub into select arguments. |
| 2681 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2682 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2683 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2684 | |
| 2685 | if (isa<PHINode>(Op0)) |
| 2686 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2687 | return NV; |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2688 | } |
| 2689 | |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2690 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
| 2691 | if (Op1I->getOpcode() == Instruction::Add && |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2692 | !Op0->getType()->isFPOrFPVector()) { |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2693 | if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2694 | return BinaryOperator::CreateNeg(Op1I->getOperand(1), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2695 | else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2696 | return BinaryOperator::CreateNeg(Op1I->getOperand(0), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2697 | else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) { |
| 2698 | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1))) |
| 2699 | // C1-(X+C2) --> (C1-C2)-X |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2700 | return BinaryOperator::CreateSub(Subtract(CI1, CI2), |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2701 | Op1I->getOperand(0)); |
| 2702 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2703 | } |
| 2704 | |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2705 | if (Op1I->hasOneUse()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2706 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 2707 | // is not used by anyone else... |
| 2708 | // |
Chris Lattner | 0517e72 | 2004-02-02 20:09:56 +0000 | [diff] [blame] | 2709 | if (Op1I->getOpcode() == Instruction::Sub && |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2710 | !Op1I->getType()->isFPOrFPVector()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2711 | // Swap the two operands of the subexpr... |
| 2712 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 2713 | Op1I->setOperand(0, IIOp1); |
| 2714 | Op1I->setOperand(1, IIOp0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2715 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2716 | // Create the new top level add instruction... |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2717 | return BinaryOperator::CreateAdd(Op0, Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2718 | } |
| 2719 | |
| 2720 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 2721 | // |
| 2722 | if (Op1I->getOpcode() == Instruction::And && |
| 2723 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 2724 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 2725 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 2726 | Value *NewNot = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2727 | InsertNewInstBefore(BinaryOperator::CreateNot(OtherOp, "B.not"), I); |
| 2728 | return BinaryOperator::CreateAnd(Op0, NewNot); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2729 | } |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2730 | |
Reid Spencer | ac5209e | 2006-10-16 23:08:08 +0000 | [diff] [blame] | 2731 | // 0 - (X sdiv C) -> (X sdiv -C) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2732 | if (Op1I->getOpcode() == Instruction::SDiv) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2733 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
Zhou Sheng | 843f0767 | 2007-04-19 05:39:12 +0000 | [diff] [blame] | 2734 | if (CSI->isZero()) |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2735 | if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2736 | return BinaryOperator::CreateSDiv(Op1I->getOperand(0), |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2737 | ConstantExpr::getNeg(DivRHS)); |
| 2738 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2739 | // X - X*C --> X * (1-C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2740 | ConstantInt *C2 = 0; |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2741 | if (dyn_castFoldableMul(Op1I, C2) == Op0) { |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2742 | Constant *CP1 = Subtract(ConstantInt::get(I.getType(), 1), C2); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2743 | return BinaryOperator::CreateMul(Op0, CP1); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2744 | } |
Dan Gohman | 5d066ff | 2007-09-17 17:31:57 +0000 | [diff] [blame] | 2745 | |
| 2746 | // X - ((X / Y) * Y) --> X % Y |
| 2747 | if (Op1I->getOpcode() == Instruction::Mul) |
| 2748 | if (Instruction *I = dyn_cast<Instruction>(Op1I->getOperand(0))) |
| 2749 | if (Op0 == I->getOperand(0) && |
| 2750 | Op1I->getOperand(1) == I->getOperand(1)) { |
| 2751 | if (I->getOpcode() == Instruction::SDiv) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2752 | return BinaryOperator::CreateSRem(Op0, Op1I->getOperand(1)); |
Dan Gohman | 5d066ff | 2007-09-17 17:31:57 +0000 | [diff] [blame] | 2753 | if (I->getOpcode() == Instruction::UDiv) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2754 | return BinaryOperator::CreateURem(Op0, Op1I->getOperand(1)); |
Dan Gohman | 5d066ff | 2007-09-17 17:31:57 +0000 | [diff] [blame] | 2755 | } |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2756 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2757 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2758 | |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2759 | if (!Op0->getType()->isFPOrFPVector()) |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2760 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2761 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2762 | if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X |
| 2763 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 2764 | else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X |
| 2765 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2766 | } else if (Op0I->getOpcode() == Instruction::Sub) { |
| 2767 | if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2768 | return BinaryOperator::CreateNeg(Op0I->getOperand(1), I.getName()); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2769 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2770 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2771 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2772 | ConstantInt *C1; |
| 2773 | if (Value *X = dyn_castFoldableMul(Op0, C1)) { |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2774 | if (X == Op1) // X*C - X --> X * (C-1) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2775 | return BinaryOperator::CreateMul(Op1, SubOne(C1)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2776 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2777 | ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2) |
| 2778 | if (X == dyn_castFoldableMul(Op1, C2)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2779 | return BinaryOperator::CreateMul(X, Subtract(C1, C2)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2780 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2781 | return 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2784 | /// isSignBitCheck - Given an exploded icmp instruction, return true if the |
| 2785 | /// comparison only checks the sign bit. If it only checks the sign bit, set |
| 2786 | /// TrueIfSigned if the result of the comparison is true when the input value is |
| 2787 | /// signed. |
| 2788 | static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS, |
| 2789 | bool &TrueIfSigned) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2790 | switch (pred) { |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2791 | case ICmpInst::ICMP_SLT: // True if LHS s< 0 |
| 2792 | TrueIfSigned = true; |
| 2793 | return RHS->isZero(); |
Chris Lattner | cb7122b | 2007-07-16 04:15:34 +0000 | [diff] [blame] | 2794 | case ICmpInst::ICMP_SLE: // True if LHS s<= RHS and RHS == -1 |
| 2795 | TrueIfSigned = true; |
| 2796 | return RHS->isAllOnesValue(); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2797 | case ICmpInst::ICMP_SGT: // True if LHS s> -1 |
| 2798 | TrueIfSigned = false; |
| 2799 | return RHS->isAllOnesValue(); |
Chris Lattner | cb7122b | 2007-07-16 04:15:34 +0000 | [diff] [blame] | 2800 | case ICmpInst::ICMP_UGT: |
| 2801 | // True if LHS u> RHS and RHS == high-bit-mask - 1 |
| 2802 | TrueIfSigned = true; |
| 2803 | return RHS->getValue() == |
| 2804 | APInt::getSignedMaxValue(RHS->getType()->getPrimitiveSizeInBits()); |
| 2805 | case ICmpInst::ICMP_UGE: |
| 2806 | // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc) |
| 2807 | TrueIfSigned = true; |
| 2808 | return RHS->getValue() == |
| 2809 | APInt::getSignBit(RHS->getType()->getPrimitiveSizeInBits()); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2810 | default: |
| 2811 | return false; |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2812 | } |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2813 | } |
| 2814 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2815 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2816 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2817 | Value *Op0 = I.getOperand(0); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2818 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2819 | if (isa<UndefValue>(I.getOperand(1))) // undef * X -> 0 |
| 2820 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2821 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2822 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2823 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 2824 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2825 | |
| 2826 | // ((X << C1)*C2) == (X * (C2 << C1)) |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2827 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op0)) |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2828 | if (SI->getOpcode() == Instruction::Shl) |
| 2829 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2830 | return BinaryOperator::CreateMul(SI->getOperand(0), |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2831 | ConstantExpr::getShl(CI, ShOp)); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2832 | |
Zhou Sheng | 843f0767 | 2007-04-19 05:39:12 +0000 | [diff] [blame] | 2833 | if (CI->isZero()) |
Chris Lattner | 515c97c | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 2834 | return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 |
| 2835 | if (CI->equalsInt(1)) // X * 1 == X |
| 2836 | return ReplaceInstUsesWith(I, Op0); |
| 2837 | if (CI->isAllOnesValue()) // X * -1 == 0 - X |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2838 | return BinaryOperator::CreateNeg(Op0, I.getName()); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2839 | |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 2840 | const APInt& Val = cast<ConstantInt>(CI)->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2841 | if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2842 | return BinaryOperator::CreateShl(Op0, |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2843 | ConstantInt::get(Op0->getType(), Val.logBase2())); |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 2844 | } |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2845 | } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2846 | if (Op1F->isNullValue()) |
| 2847 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2848 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2849 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 2850 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 2851 | // We need a better interface for long double here. |
| 2852 | if (Op1->getType() == Type::FloatTy || Op1->getType() == Type::DoubleTy) |
| 2853 | if (Op1F->isExactlyValue(1.0)) |
| 2854 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2855 | } |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2856 | |
| 2857 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 2858 | if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() && |
| 2859 | isa<ConstantInt>(Op0I->getOperand(1))) { |
| 2860 | // Canonicalize (X+C1)*C2 -> X*C2+C1*C2. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2861 | Instruction *Add = BinaryOperator::CreateMul(Op0I->getOperand(0), |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2862 | Op1, "tmp"); |
| 2863 | InsertNewInstBefore(Add, I); |
| 2864 | Value *C1C2 = ConstantExpr::getMul(Op1, |
| 2865 | cast<Constant>(Op0I->getOperand(1))); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2866 | return BinaryOperator::CreateAdd(Add, C1C2); |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2867 | |
| 2868 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2869 | |
| 2870 | // Try to fold constant mul into select arguments. |
| 2871 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2872 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2873 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2874 | |
| 2875 | if (isa<PHINode>(Op0)) |
| 2876 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2877 | return NV; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2878 | } |
| 2879 | |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2880 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
| 2881 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2882 | return BinaryOperator::CreateMul(Op0v, Op1v); |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2883 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2884 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 2885 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 2886 | // See if we can simplify things based on how the boolean was originally |
| 2887 | // formed. |
| 2888 | CastInst *BoolCast = 0; |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2889 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(0))) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2890 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2891 | BoolCast = CI; |
| 2892 | if (!BoolCast) |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2893 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(1))) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2894 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2895 | BoolCast = CI; |
| 2896 | if (BoolCast) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2897 | if (ICmpInst *SCI = dyn_cast<ICmpInst>(BoolCast->getOperand(0))) { |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2898 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); |
| 2899 | const Type *SCOpTy = SCIOp0->getType(); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2900 | bool TIS = false; |
| 2901 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2902 | // If the icmp is true iff the sign bit of X is set, then convert this |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2903 | // multiply into a shift/and combination. |
| 2904 | if (isa<ConstantInt>(SCIOp1) && |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2905 | isSignBitCheck(SCI->getPredicate(), cast<ConstantInt>(SCIOp1), TIS) && |
| 2906 | TIS) { |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2907 | // Shift the X value right to turn it into "all signbits". |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2908 | Constant *Amt = ConstantInt::get(SCIOp0->getType(), |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2909 | SCOpTy->getPrimitiveSizeInBits()-1); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2910 | Value *V = |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2911 | InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2912 | BinaryOperator::Create(Instruction::AShr, SCIOp0, Amt, |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2913 | BoolCast->getOperand(0)->getName()+ |
| 2914 | ".mask"), I); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2915 | |
| 2916 | // If the multiply type is not the same as the source type, sign extend |
| 2917 | // or truncate to the multiply type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2918 | if (I.getType() != V->getType()) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2919 | uint32_t SrcBits = V->getType()->getPrimitiveSizeInBits(); |
| 2920 | uint32_t DstBits = I.getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2921 | Instruction::CastOps opcode = |
| 2922 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2923 | (SrcBits < DstBits ? Instruction::SExt : Instruction::Trunc)); |
| 2924 | V = InsertCastBefore(opcode, V, I.getType(), I); |
| 2925 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2926 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2927 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2928 | return BinaryOperator::CreateAnd(V, OtherOp); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2929 | } |
| 2930 | } |
| 2931 | } |
| 2932 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2933 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2934 | } |
| 2935 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2936 | /// This function implements the transforms on div instructions that work |
| 2937 | /// regardless of the kind of div instruction it is (udiv, sdiv, or fdiv). It is |
| 2938 | /// used by the visitors to those instructions. |
| 2939 | /// @brief Transforms common to all three div instructions |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2940 | Instruction *InstCombiner::commonDivTransforms(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2941 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2942 | |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2943 | // undef / X -> 0 for integer. |
| 2944 | // undef / X -> undef for FP (the undef could be a snan). |
| 2945 | if (isa<UndefValue>(Op0)) { |
| 2946 | if (Op0->getType()->isFPOrFPVector()) |
| 2947 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2948 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2949 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2950 | |
| 2951 | // X / undef -> undef |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2952 | if (isa<UndefValue>(Op1)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2953 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2954 | |
Chris Lattner | 25feae5 | 2008-01-28 00:58:18 +0000 | [diff] [blame] | 2955 | // Handle cases involving: [su]div X, (select Cond, Y, Z) |
| 2956 | // This does not apply for fdiv. |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2957 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
Chris Lattner | 25feae5 | 2008-01-28 00:58:18 +0000 | [diff] [blame] | 2958 | // [su]div X, (Cond ? 0 : Y) -> div X, Y. If the div and the select are in |
| 2959 | // the same basic block, then we replace the select with Y, and the |
| 2960 | // condition of the select with false (if the cond value is in the same BB). |
| 2961 | // If the select has uses other than the div, this allows them to be |
| 2962 | // simplified also. Note that div X, Y is just as good as div X, 0 (undef) |
| 2963 | if (ConstantInt *ST = dyn_cast<ConstantInt>(SI->getOperand(1))) |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2964 | if (ST->isNullValue()) { |
| 2965 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2966 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2967 | UpdateValueUsesWith(CondI, ConstantInt::getFalse()); |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2968 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2969 | I.setOperand(1, SI->getOperand(2)); |
| 2970 | else |
| 2971 | UpdateValueUsesWith(SI, SI->getOperand(2)); |
| 2972 | return &I; |
| 2973 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2974 | |
Chris Lattner | 25feae5 | 2008-01-28 00:58:18 +0000 | [diff] [blame] | 2975 | // Likewise for: [su]div X, (Cond ? Y : 0) -> div X, Y |
| 2976 | if (ConstantInt *ST = dyn_cast<ConstantInt>(SI->getOperand(2))) |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2977 | if (ST->isNullValue()) { |
| 2978 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2979 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2980 | UpdateValueUsesWith(CondI, ConstantInt::getTrue()); |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2981 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2982 | I.setOperand(1, SI->getOperand(1)); |
| 2983 | else |
| 2984 | UpdateValueUsesWith(SI, SI->getOperand(1)); |
| 2985 | return &I; |
| 2986 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2987 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2988 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2989 | return 0; |
| 2990 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2991 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2992 | /// This function implements the transforms common to both integer division |
| 2993 | /// instructions (udiv and sdiv). It is called by the visitors to those integer |
| 2994 | /// division instructions. |
| 2995 | /// @brief Common integer divide transforms |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2996 | Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2997 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2998 | |
Chris Lattner | b2ae9e3 | 2008-05-16 02:59:42 +0000 | [diff] [blame] | 2999 | // (sdiv X, X) --> 1 (udiv X, X) --> 1 |
| 3000 | if (Op0 == Op1) |
| 3001 | return ReplaceInstUsesWith(I, ConstantInt::get(I.getType(), 1)); |
| 3002 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3003 | if (Instruction *Common = commonDivTransforms(I)) |
| 3004 | return Common; |
| 3005 | |
| 3006 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 3007 | // div X, 1 == X |
| 3008 | if (RHS->equalsInt(1)) |
| 3009 | return ReplaceInstUsesWith(I, Op0); |
| 3010 | |
| 3011 | // (X / C1) / C2 -> X / (C1*C2) |
| 3012 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) |
| 3013 | if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode()) |
| 3014 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { |
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 3015 | if (MultiplyOverflows(RHS, LHSRHS, I.getOpcode()==Instruction::SDiv)) |
| 3016 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3017 | else |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3018 | return BinaryOperator::Create(I.getOpcode(), LHS->getOperand(0), |
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 3019 | Multiply(RHS, LHSRHS)); |
Chris Lattner | bf70b83 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 3020 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3021 | |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3022 | if (!RHS->isZero()) { // avoid X udiv 0 |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3023 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 3024 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 3025 | return R; |
| 3026 | if (isa<PHINode>(Op0)) |
| 3027 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3028 | return NV; |
| 3029 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 3030 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3031 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3032 | // 0 / X == 0, we don't need to preserve faults! |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 3033 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3034 | if (LHS->equalsInt(0)) |
| 3035 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3036 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3037 | return 0; |
| 3038 | } |
| 3039 | |
| 3040 | Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { |
| 3041 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 3042 | |
| 3043 | // Handle the integer div common cases |
| 3044 | if (Instruction *Common = commonIDivTransforms(I)) |
| 3045 | return Common; |
| 3046 | |
| 3047 | // X udiv C^2 -> X >> C |
| 3048 | // Check to see if this is an unsigned division with an exact power of 2, |
| 3049 | // if so, convert to a right shift. |
| 3050 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) { |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3051 | if (C->getValue().isPowerOf2()) // 0 not included in isPowerOf2 |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3052 | return BinaryOperator::CreateLShr(Op0, |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 3053 | ConstantInt::get(Op0->getType(), C->getValue().logBase2())); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3054 | } |
| 3055 | |
| 3056 | // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3057 | if (BinaryOperator *RHSI = dyn_cast<BinaryOperator>(I.getOperand(1))) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3058 | if (RHSI->getOpcode() == Instruction::Shl && |
| 3059 | isa<ConstantInt>(RHSI->getOperand(0))) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3060 | const APInt& C1 = cast<ConstantInt>(RHSI->getOperand(0))->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3061 | if (C1.isPowerOf2()) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3062 | Value *N = RHSI->getOperand(1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3063 | const Type *NTy = N->getType(); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 3064 | if (uint32_t C2 = C1.logBase2()) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3065 | Constant *C2V = ConstantInt::get(NTy, C2); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3066 | N = InsertNewInstBefore(BinaryOperator::CreateAdd(N, C2V, "tmp"), I); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3067 | } |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3068 | return BinaryOperator::CreateLShr(Op0, N); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3069 | } |
| 3070 | } |
Chris Lattner | c812e5d | 2005-11-05 07:40:31 +0000 | [diff] [blame] | 3071 | } |
| 3072 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3073 | // udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2) |
| 3074 | // where C1&C2 are powers of two. |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3075 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3076 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3077 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3078 | const APInt &TVA = STO->getValue(), &FVA = SFO->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3079 | if (TVA.isPowerOf2() && FVA.isPowerOf2()) { |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3080 | // Compute the shift amounts |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3081 | uint32_t TSA = TVA.logBase2(), FSA = FVA.logBase2(); |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3082 | // Construct the "on true" case of the select |
| 3083 | Constant *TC = ConstantInt::get(Op0->getType(), TSA); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3084 | Instruction *TSI = BinaryOperator::CreateLShr( |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3085 | Op0, TC, SI->getName()+".t"); |
| 3086 | TSI = InsertNewInstBefore(TSI, I); |
| 3087 | |
| 3088 | // Construct the "on false" case of the select |
| 3089 | Constant *FC = ConstantInt::get(Op0->getType(), FSA); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3090 | Instruction *FSI = BinaryOperator::CreateLShr( |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3091 | Op0, FC, SI->getName()+".f"); |
| 3092 | FSI = InsertNewInstBefore(FSI, I); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3093 | |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3094 | // construct the select instruction and return it. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3095 | return SelectInst::Create(SI->getOperand(0), TSI, FSI, SI->getName()); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3096 | } |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3097 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3098 | return 0; |
| 3099 | } |
| 3100 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3101 | Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { |
| 3102 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 3103 | |
| 3104 | // Handle the integer div common cases |
| 3105 | if (Instruction *Common = commonIDivTransforms(I)) |
| 3106 | return Common; |
| 3107 | |
| 3108 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 3109 | // sdiv X, -1 == -X |
| 3110 | if (RHS->isAllOnesValue()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3111 | return BinaryOperator::CreateNeg(Op0); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3112 | |
| 3113 | // -X/C -> X/-C |
| 3114 | if (Value *LHSNeg = dyn_castNegVal(Op0)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3115 | return BinaryOperator::CreateSDiv(LHSNeg, ConstantExpr::getNeg(RHS)); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3116 | } |
| 3117 | |
| 3118 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| 3119 | // unsigned inputs), turn this into a udiv. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 3120 | if (I.getType()->isInteger()) { |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3121 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3122 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3123 | // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3124 | return BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3125 | } |
| 3126 | } |
| 3127 | |
| 3128 | return 0; |
| 3129 | } |
| 3130 | |
| 3131 | Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { |
| 3132 | return commonDivTransforms(I); |
| 3133 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3134 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3135 | /// This function implements the transforms on rem instructions that work |
| 3136 | /// regardless of the kind of rem instruction it is (urem, srem, or frem). It |
| 3137 | /// is used by the visitors to those instructions. |
| 3138 | /// @brief Transforms common to all three rem instructions |
| 3139 | Instruction *InstCombiner::commonRemTransforms(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 3140 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3141 | |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 3142 | // 0 % X == 0 for integer, we don't need to preserve faults! |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 3143 | if (Constant *LHS = dyn_cast<Constant>(Op0)) |
| 3144 | if (LHS->isNullValue()) |
| 3145 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3146 | |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 3147 | if (isa<UndefValue>(Op0)) { // undef % X -> 0 |
| 3148 | if (I.getType()->isFPOrFPVector()) |
| 3149 | return ReplaceInstUsesWith(I, Op0); // X % undef -> undef (could be SNaN) |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 3150 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 3151 | } |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 3152 | if (isa<UndefValue>(Op1)) |
| 3153 | return ReplaceInstUsesWith(I, Op1); // X % undef -> undef |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3154 | |
| 3155 | // Handle cases involving: rem X, (select Cond, Y, Z) |
| 3156 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 3157 | // rem X, (Cond ? 0 : Y) -> rem X, Y. If the rem and the select are in |
| 3158 | // the same basic block, then we replace the select with Y, and the |
| 3159 | // condition of the select with false (if the cond value is in the same |
| 3160 | // BB). If the select has uses other than the div, this allows them to be |
| 3161 | // simplified also. |
| 3162 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) |
| 3163 | if (ST->isNullValue()) { |
| 3164 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 3165 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3166 | UpdateValueUsesWith(CondI, ConstantInt::getFalse()); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3167 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 3168 | I.setOperand(1, SI->getOperand(2)); |
| 3169 | else |
| 3170 | UpdateValueUsesWith(SI, SI->getOperand(2)); |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 3171 | return &I; |
| 3172 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3173 | // Likewise for: rem X, (Cond ? Y : 0) -> rem X, Y |
| 3174 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) |
| 3175 | if (ST->isNullValue()) { |
| 3176 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 3177 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3178 | UpdateValueUsesWith(CondI, ConstantInt::getTrue()); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3179 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 3180 | I.setOperand(1, SI->getOperand(1)); |
| 3181 | else |
| 3182 | UpdateValueUsesWith(SI, SI->getOperand(1)); |
| 3183 | return &I; |
| 3184 | } |
Chris Lattner | 11a49f2 | 2005-11-05 07:28:37 +0000 | [diff] [blame] | 3185 | } |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 3186 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3187 | return 0; |
| 3188 | } |
| 3189 | |
| 3190 | /// This function implements the transforms common to both integer remainder |
| 3191 | /// instructions (urem and srem). It is called by the visitors to those integer |
| 3192 | /// remainder instructions. |
| 3193 | /// @brief Common integer remainder transforms |
| 3194 | Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { |
| 3195 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 3196 | |
| 3197 | if (Instruction *common = commonRemTransforms(I)) |
| 3198 | return common; |
| 3199 | |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 3200 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 3201 | // X % 0 == undef, we don't need to preserve faults! |
| 3202 | if (RHS->equalsInt(0)) |
| 3203 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); |
| 3204 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3205 | if (RHS->equalsInt(1)) // X % 1 == 0 |
| 3206 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3207 | |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 3208 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { |
| 3209 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { |
| 3210 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 3211 | return R; |
| 3212 | } else if (isa<PHINode>(Op0I)) { |
| 3213 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3214 | return NV; |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 3215 | } |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 3216 | |
| 3217 | // See if we can fold away this rem instruction. |
| 3218 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 3219 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 3220 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 3221 | KnownZero, KnownOne)) |
| 3222 | return &I; |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 3223 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3224 | } |
| 3225 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3226 | return 0; |
| 3227 | } |
| 3228 | |
| 3229 | Instruction *InstCombiner::visitURem(BinaryOperator &I) { |
| 3230 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 3231 | |
| 3232 | if (Instruction *common = commonIRemTransforms(I)) |
| 3233 | return common; |
| 3234 | |
| 3235 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 3236 | // X urem C^2 -> X and C |
| 3237 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 3238 | // if so, convert to a bitwise and. |
| 3239 | if (ConstantInt *C = dyn_cast<ConstantInt>(RHS)) |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3240 | if (C->getValue().isPowerOf2()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3241 | return BinaryOperator::CreateAnd(Op0, SubOne(C)); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3242 | } |
| 3243 | |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3244 | if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3245 | // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1) |
| 3246 | if (RHSI->getOpcode() == Instruction::Shl && |
| 3247 | isa<ConstantInt>(RHSI->getOperand(0))) { |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 3248 | if (cast<ConstantInt>(RHSI->getOperand(0))->getValue().isPowerOf2()) { |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3249 | Constant *N1 = ConstantInt::getAllOnesValue(I.getType()); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3250 | Value *Add = InsertNewInstBefore(BinaryOperator::CreateAdd(RHSI, N1, |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3251 | "tmp"), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3252 | return BinaryOperator::CreateAnd(Op0, Add); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3253 | } |
| 3254 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3255 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 3256 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3257 | // urem X, (select Cond, 2^C1, 2^C2) --> select Cond, (and X, C1), (and X, C2) |
| 3258 | // where C1&C2 are powers of two. |
| 3259 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 3260 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
| 3261 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
| 3262 | // STO == 0 and SFO == 0 handled above. |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3263 | if ((STO->getValue().isPowerOf2()) && |
| 3264 | (SFO->getValue().isPowerOf2())) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3265 | Value *TrueAnd = InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3266 | BinaryOperator::CreateAnd(Op0, SubOne(STO), SI->getName()+".t"), I); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3267 | Value *FalseAnd = InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3268 | BinaryOperator::CreateAnd(Op0, SubOne(SFO), SI->getName()+".f"), I); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3269 | return SelectInst::Create(SI->getOperand(0), TrueAnd, FalseAnd); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3270 | } |
| 3271 | } |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3272 | } |
| 3273 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3274 | return 0; |
| 3275 | } |
| 3276 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3277 | Instruction *InstCombiner::visitSRem(BinaryOperator &I) { |
| 3278 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 3279 | |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3280 | // Handle the integer rem common cases |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3281 | if (Instruction *common = commonIRemTransforms(I)) |
| 3282 | return common; |
| 3283 | |
| 3284 | if (Value *RHSNeg = dyn_castNegVal(Op1)) |
| 3285 | if (!isa<ConstantInt>(RHSNeg) || |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 3286 | cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive()) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3287 | // X % -Y -> X % Y |
| 3288 | AddUsesToWorkList(I); |
| 3289 | I.setOperand(1, RHSNeg); |
| 3290 | return &I; |
| 3291 | } |
| 3292 | |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3293 | // If the sign bits of both operands are zero (i.e. we can prove they are |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3294 | // unsigned inputs), turn this into a urem. |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3295 | if (I.getType()->isInteger()) { |
| 3296 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
| 3297 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
| 3298 | // X srem Y -> X urem Y, iff X and Y don't have sign bit set |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3299 | return BinaryOperator::CreateURem(Op0, Op1, I.getName()); |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3300 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3301 | } |
| 3302 | |
| 3303 | return 0; |
| 3304 | } |
| 3305 | |
| 3306 | Instruction *InstCombiner::visitFRem(BinaryOperator &I) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3307 | return commonRemTransforms(I); |
| 3308 | } |
| 3309 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3310 | // isMaxValueMinusOne - return true if this is Max-1 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3311 | static bool isMaxValueMinusOne(const ConstantInt *C, bool isSigned) { |
Reid Spencer | 3a2a9fb | 2007-03-19 21:10:28 +0000 | [diff] [blame] | 3312 | uint32_t TypeBits = C->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 3313 | if (!isSigned) |
| 3314 | return C->getValue() == APInt::getAllOnesValue(TypeBits) - 1; |
| 3315 | return C->getValue() == APInt::getSignedMaxValue(TypeBits)-1; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3316 | } |
| 3317 | |
| 3318 | // isMinValuePlusOne - return true if this is Min+1 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3319 | static bool isMinValuePlusOne(const ConstantInt *C, bool isSigned) { |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 3320 | if (!isSigned) |
| 3321 | return C->getValue() == 1; // unsigned |
| 3322 | |
| 3323 | // Calculate 1111111111000000000000 |
| 3324 | uint32_t TypeBits = C->getType()->getPrimitiveSizeInBits(); |
| 3325 | return C->getValue() == APInt::getSignedMinValue(TypeBits)+1; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3326 | } |
| 3327 | |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3328 | // isOneBitSet - Return true if there is exactly one bit set in the specified |
| 3329 | // constant. |
| 3330 | static bool isOneBitSet(const ConstantInt *CI) { |
Reid Spencer | 5f6a895 | 2007-03-20 00:16:52 +0000 | [diff] [blame] | 3331 | return CI->getValue().isPowerOf2(); |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3332 | } |
| 3333 | |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3334 | // isHighOnes - Return true if the constant is of the form 1+0+. |
| 3335 | // This is the same as lowones(~X). |
| 3336 | static bool isHighOnes(const ConstantInt *CI) { |
Zhou Sheng | 2cde46c | 2007-03-20 12:49:06 +0000 | [diff] [blame] | 3337 | return (~CI->getValue() + 1).isPowerOf2(); |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3338 | } |
| 3339 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3340 | /// getICmpCode - Encode a icmp predicate into a three bit mask. These bits |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3341 | /// are carefully arranged to allow folding of expressions such as: |
| 3342 | /// |
| 3343 | /// (A < B) | (A > B) --> (A != B) |
| 3344 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3345 | /// Note that this is only valid if the first and second predicates have the |
| 3346 | /// same sign. Is illegal to do: (A u< B) | (A s> B) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3347 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3348 | /// Three bits are used to represent the condition, as follows: |
| 3349 | /// 0 A > B |
| 3350 | /// 1 A == B |
| 3351 | /// 2 A < B |
| 3352 | /// |
| 3353 | /// <=> Value Definition |
| 3354 | /// 000 0 Always false |
| 3355 | /// 001 1 A > B |
| 3356 | /// 010 2 A == B |
| 3357 | /// 011 3 A >= B |
| 3358 | /// 100 4 A < B |
| 3359 | /// 101 5 A != B |
| 3360 | /// 110 6 A <= B |
| 3361 | /// 111 7 Always true |
| 3362 | /// |
| 3363 | static unsigned getICmpCode(const ICmpInst *ICI) { |
| 3364 | switch (ICI->getPredicate()) { |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3365 | // False -> 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3366 | case ICmpInst::ICMP_UGT: return 1; // 001 |
| 3367 | case ICmpInst::ICMP_SGT: return 1; // 001 |
| 3368 | case ICmpInst::ICMP_EQ: return 2; // 010 |
| 3369 | case ICmpInst::ICMP_UGE: return 3; // 011 |
| 3370 | case ICmpInst::ICMP_SGE: return 3; // 011 |
| 3371 | case ICmpInst::ICMP_ULT: return 4; // 100 |
| 3372 | case ICmpInst::ICMP_SLT: return 4; // 100 |
| 3373 | case ICmpInst::ICMP_NE: return 5; // 101 |
| 3374 | case ICmpInst::ICMP_ULE: return 6; // 110 |
| 3375 | case ICmpInst::ICMP_SLE: return 6; // 110 |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3376 | // True -> 7 |
| 3377 | default: |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3378 | assert(0 && "Invalid ICmp predicate!"); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3379 | return 0; |
| 3380 | } |
| 3381 | } |
| 3382 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3383 | /// getICmpValue - This is the complement of getICmpCode, which turns an |
| 3384 | /// opcode and two operands into either a constant true or false, or a brand |
Dan Gohman | 5d066ff | 2007-09-17 17:31:57 +0000 | [diff] [blame] | 3385 | /// new ICmp instruction. The sign is passed in to determine which kind |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3386 | /// of predicate to use in new icmp instructions. |
| 3387 | static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS) { |
| 3388 | switch (code) { |
| 3389 | default: assert(0 && "Illegal ICmp code!"); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3390 | case 0: return ConstantInt::getFalse(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3391 | case 1: |
| 3392 | if (sign) |
| 3393 | return new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS); |
| 3394 | else |
| 3395 | return new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS); |
| 3396 | case 2: return new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS); |
| 3397 | case 3: |
| 3398 | if (sign) |
| 3399 | return new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS); |
| 3400 | else |
| 3401 | return new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS); |
| 3402 | case 4: |
| 3403 | if (sign) |
| 3404 | return new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS); |
| 3405 | else |
| 3406 | return new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS); |
| 3407 | case 5: return new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS); |
| 3408 | case 6: |
| 3409 | if (sign) |
| 3410 | return new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS); |
| 3411 | else |
| 3412 | return new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3413 | case 7: return ConstantInt::getTrue(); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3414 | } |
| 3415 | } |
| 3416 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3417 | static bool PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) { |
| 3418 | return (ICmpInst::isSignedPredicate(p1) == ICmpInst::isSignedPredicate(p2)) || |
| 3419 | (ICmpInst::isSignedPredicate(p1) && |
| 3420 | (p2 == ICmpInst::ICMP_EQ || p2 == ICmpInst::ICMP_NE)) || |
| 3421 | (ICmpInst::isSignedPredicate(p2) && |
| 3422 | (p1 == ICmpInst::ICMP_EQ || p1 == ICmpInst::ICMP_NE)); |
| 3423 | } |
| 3424 | |
| 3425 | namespace { |
| 3426 | // FoldICmpLogical - Implements (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| 3427 | struct FoldICmpLogical { |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3428 | InstCombiner &IC; |
| 3429 | Value *LHS, *RHS; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3430 | ICmpInst::Predicate pred; |
| 3431 | FoldICmpLogical(InstCombiner &ic, ICmpInst *ICI) |
| 3432 | : IC(ic), LHS(ICI->getOperand(0)), RHS(ICI->getOperand(1)), |
| 3433 | pred(ICI->getPredicate()) {} |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3434 | bool shouldApply(Value *V) const { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3435 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(V)) |
| 3436 | if (PredicatesFoldable(pred, ICI->getPredicate())) |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3437 | return ((ICI->getOperand(0) == LHS && ICI->getOperand(1) == RHS) || |
| 3438 | (ICI->getOperand(0) == RHS && ICI->getOperand(1) == LHS)); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3439 | return false; |
| 3440 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3441 | Instruction *apply(Instruction &Log) const { |
| 3442 | ICmpInst *ICI = cast<ICmpInst>(Log.getOperand(0)); |
| 3443 | if (ICI->getOperand(0) != LHS) { |
| 3444 | assert(ICI->getOperand(1) == LHS); |
| 3445 | ICI->swapOperands(); // Swap the LHS and RHS of the ICmp |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3446 | } |
| 3447 | |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3448 | ICmpInst *RHSICI = cast<ICmpInst>(Log.getOperand(1)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3449 | unsigned LHSCode = getICmpCode(ICI); |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3450 | unsigned RHSCode = getICmpCode(RHSICI); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3451 | unsigned Code; |
| 3452 | switch (Log.getOpcode()) { |
| 3453 | case Instruction::And: Code = LHSCode & RHSCode; break; |
| 3454 | case Instruction::Or: Code = LHSCode | RHSCode; break; |
| 3455 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; |
Chris Lattner | 021c190 | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 3456 | default: assert(0 && "Illegal logical opcode!"); return 0; |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3457 | } |
| 3458 | |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3459 | bool isSigned = ICmpInst::isSignedPredicate(RHSICI->getPredicate()) || |
| 3460 | ICmpInst::isSignedPredicate(ICI->getPredicate()); |
| 3461 | |
| 3462 | Value *RV = getICmpValue(isSigned, Code, LHS, RHS); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3463 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 3464 | return I; |
| 3465 | // Otherwise, it's a constant boolean value... |
| 3466 | return IC.ReplaceInstUsesWith(Log, RV); |
| 3467 | } |
| 3468 | }; |
Chris Lattner | d23b5ba | 2006-11-15 04:53:24 +0000 | [diff] [blame] | 3469 | } // end anonymous namespace |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3470 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3471 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
| 3472 | // the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'. Op is |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3473 | // guaranteed to be a binary operator. |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3474 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3475 | ConstantInt *OpRHS, |
| 3476 | ConstantInt *AndRHS, |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3477 | BinaryOperator &TheAnd) { |
| 3478 | Value *X = Op->getOperand(0); |
Chris Lattner | 76f7fe2 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 3479 | Constant *Together = 0; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3480 | if (!Op->isShift()) |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 3481 | Together = And(AndRHS, OpRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3482 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3483 | switch (Op->getOpcode()) { |
| 3484 | case Instruction::Xor: |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3485 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3486 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3487 | Instruction *And = BinaryOperator::CreateAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3488 | InsertNewInstBefore(And, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3489 | And->takeName(Op); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3490 | return BinaryOperator::CreateXor(And, Together); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3491 | } |
| 3492 | break; |
| 3493 | case Instruction::Or: |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3494 | if (Together == AndRHS) // (X | C) & C --> C |
| 3495 | return ReplaceInstUsesWith(TheAnd, AndRHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3496 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3497 | if (Op->hasOneUse() && Together != OpRHS) { |
| 3498 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3499 | Instruction *Or = BinaryOperator::CreateOr(X, Together); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3500 | InsertNewInstBefore(Or, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3501 | Or->takeName(Op); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3502 | return BinaryOperator::CreateAnd(Or, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3503 | } |
| 3504 | break; |
| 3505 | case Instruction::Add: |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 3506 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3507 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 3508 | // of the bit. First thing to check is to see if this AND is with a |
| 3509 | // single bit constant. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3510 | const APInt& AndRHSV = cast<ConstantInt>(AndRHS)->getValue(); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3511 | |
| 3512 | // If there is only one bit set... |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3513 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3514 | // Ok, at this point, we know that we are masking the result of the |
| 3515 | // ADD down to exactly one bit. If the constant we are adding has |
| 3516 | // no bits set below this bit, then we can eliminate the ADD. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3517 | const APInt& AddRHS = cast<ConstantInt>(OpRHS)->getValue(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3518 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3519 | // Check to see if any bits below the one bit set in AndRHSV are set. |
| 3520 | if ((AddRHS & (AndRHSV-1)) == 0) { |
| 3521 | // If not, the only thing that can effect the output of the AND is |
| 3522 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 3523 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 3524 | // no effect. |
| 3525 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop |
| 3526 | TheAnd.setOperand(0, X); |
| 3527 | return &TheAnd; |
| 3528 | } else { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3529 | // Pull the XOR out of the AND. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3530 | Instruction *NewAnd = BinaryOperator::CreateAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3531 | InsertNewInstBefore(NewAnd, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3532 | NewAnd->takeName(Op); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3533 | return BinaryOperator::CreateXor(NewAnd, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3534 | } |
| 3535 | } |
| 3536 | } |
| 3537 | } |
| 3538 | break; |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3539 | |
| 3540 | case Instruction::Shl: { |
| 3541 | // We know that the AND will not produce any of the bits shifted in, so if |
| 3542 | // the anded constant includes them, clear them now! |
| 3543 | // |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3544 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3545 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3546 | APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal)); |
| 3547 | ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShlMask); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3548 | |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3549 | if (CI->getValue() == ShlMask) { |
| 3550 | // Masking out bits that the shift already masks |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3551 | return ReplaceInstUsesWith(TheAnd, Op); // No need for the and. |
| 3552 | } else if (CI != AndRHS) { // Reducing bits set in and. |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3553 | TheAnd.setOperand(1, CI); |
| 3554 | return &TheAnd; |
| 3555 | } |
| 3556 | break; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3557 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3558 | case Instruction::LShr: |
| 3559 | { |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3560 | // We know that the AND will not produce any of the bits shifted in, so if |
| 3561 | // the anded constant includes them, clear them now! This only applies to |
| 3562 | // unsigned shifts, because a signed shr may bring in set bits! |
| 3563 | // |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3564 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3565 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3566 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
| 3567 | ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShrMask); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3568 | |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3569 | if (CI->getValue() == ShrMask) { |
| 3570 | // Masking out bits that the shift already masks. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3571 | return ReplaceInstUsesWith(TheAnd, Op); |
| 3572 | } else if (CI != AndRHS) { |
| 3573 | TheAnd.setOperand(1, CI); // Reduce bits set in and cst. |
| 3574 | return &TheAnd; |
| 3575 | } |
| 3576 | break; |
| 3577 | } |
| 3578 | case Instruction::AShr: |
| 3579 | // Signed shr. |
| 3580 | // See if this is shifting in some sign extension, then masking it out |
| 3581 | // with an and. |
| 3582 | if (Op->hasOneUse()) { |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3583 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3584 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3585 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
| 3586 | Constant *C = ConstantInt::get(AndRHS->getValue() & ShrMask); |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 3587 | if (C == AndRHS) { // Masking out bits shifted in. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 3588 | // (Val ashr C1) & C2 -> (Val lshr C1) & C2 |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3589 | // Make the argument unsigned. |
| 3590 | Value *ShVal = Op->getOperand(0); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3591 | ShVal = InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3592 | BinaryOperator::CreateLShr(ShVal, OpRHS, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3593 | Op->getName()), TheAnd); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3594 | return BinaryOperator::CreateAnd(ShVal, AndRHS, TheAnd.getName()); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3595 | } |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3596 | } |
| 3597 | break; |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3598 | } |
| 3599 | return 0; |
| 3600 | } |
| 3601 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3602 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3603 | /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is |
| 3604 | /// true, otherwise (V < Lo || V >= Hi). In pratice, we emit the more efficient |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3605 | /// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. isSigned indicates |
| 3606 | /// whether to treat the V, Lo and HI as signed or not. IB is the location to |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3607 | /// insert new instructions. |
| 3608 | Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3609 | bool isSigned, bool Inside, |
| 3610 | Instruction &IB) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3611 | assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ? |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3612 | ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() && |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3613 | "Lo is not <= Hi in range emission code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3614 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3615 | if (Inside) { |
| 3616 | if (Lo == Hi) // Trivially false. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3617 | return new ICmpInst(ICmpInst::ICMP_NE, V, V); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3618 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3619 | // V >= Min && V < Hi --> V < Hi |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3620 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3621 | ICmpInst::Predicate pred = (isSigned ? |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3622 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT); |
| 3623 | return new ICmpInst(pred, V, Hi); |
| 3624 | } |
| 3625 | |
| 3626 | // Emit V-Lo <u Hi-Lo |
| 3627 | Constant *NegLo = ConstantExpr::getNeg(Lo); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3628 | Instruction *Add = BinaryOperator::CreateAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3629 | InsertNewInstBefore(Add, IB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3630 | Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi); |
| 3631 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, UpperBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3632 | } |
| 3633 | |
| 3634 | if (Lo == Hi) // Trivially true. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3635 | return new ICmpInst(ICmpInst::ICMP_EQ, V, V); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3636 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3637 | // V < Min || V >= Hi -> V > Hi-1 |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3638 | Hi = SubOne(cast<ConstantInt>(Hi)); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3639 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3640 | ICmpInst::Predicate pred = (isSigned ? |
| 3641 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT); |
| 3642 | return new ICmpInst(pred, V, Hi); |
| 3643 | } |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3644 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3645 | // Emit V-Lo >u Hi-1-Lo |
| 3646 | // Note that Hi has already had one subtracted from it, above. |
| 3647 | ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3648 | Instruction *Add = BinaryOperator::CreateAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3649 | InsertNewInstBefore(Add, IB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3650 | Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi); |
| 3651 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, LowerBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3652 | } |
| 3653 | |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3654 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 3655 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 3656 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 3657 | // not, since all 1s are not contiguous. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 3658 | static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3659 | const APInt& V = Val->getValue(); |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3660 | uint32_t BitWidth = Val->getType()->getBitWidth(); |
| 3661 | if (!APIntOps::isShiftedMask(BitWidth, V)) return false; |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3662 | |
| 3663 | // look for the first zero bit after the run of ones |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3664 | MB = BitWidth - ((V - 1) ^ V).countLeadingZeros(); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3665 | // look for the first non-zero bit |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3666 | ME = V.getActiveBits(); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3667 | return true; |
| 3668 | } |
| 3669 | |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3670 | /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask, |
| 3671 | /// where isSub determines whether the operator is a sub. If we can fold one of |
| 3672 | /// the following xforms: |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3673 | /// |
| 3674 | /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask |
| 3675 | /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 3676 | /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 3677 | /// |
| 3678 | /// return (A +/- B). |
| 3679 | /// |
| 3680 | Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3681 | ConstantInt *Mask, bool isSub, |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3682 | Instruction &I) { |
| 3683 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
| 3684 | if (!LHSI || LHSI->getNumOperands() != 2 || |
| 3685 | !isa<ConstantInt>(LHSI->getOperand(1))) return 0; |
| 3686 | |
| 3687 | ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1)); |
| 3688 | |
| 3689 | switch (LHSI->getOpcode()) { |
| 3690 | default: return 0; |
| 3691 | case Instruction::And: |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 3692 | if (And(N, Mask) == Mask) { |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3693 | // If the AndRHS is a power of two minus one (0+1+), this is simple. |
Zhou Sheng | 00f436c | 2007-03-24 15:34:37 +0000 | [diff] [blame] | 3694 | if ((Mask->getValue().countLeadingZeros() + |
| 3695 | Mask->getValue().countPopulation()) == |
| 3696 | Mask->getValue().getBitWidth()) |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3697 | break; |
| 3698 | |
| 3699 | // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+ |
| 3700 | // part, we don't need any explicit masks to take them out of A. If that |
| 3701 | // is all N is, ignore it. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 3702 | uint32_t MB = 0, ME = 0; |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3703 | if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 3704 | uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth(); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3705 | APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1)); |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3706 | if (MaskedValueIsZero(RHS, Mask)) |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3707 | break; |
| 3708 | } |
| 3709 | } |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3710 | return 0; |
| 3711 | case Instruction::Or: |
| 3712 | case Instruction::Xor: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3713 | // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0 |
Zhou Sheng | 00f436c | 2007-03-24 15:34:37 +0000 | [diff] [blame] | 3714 | if ((Mask->getValue().countLeadingZeros() + |
| 3715 | Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth() |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3716 | && And(N, Mask)->isZero()) |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3717 | break; |
| 3718 | return 0; |
| 3719 | } |
| 3720 | |
| 3721 | Instruction *New; |
| 3722 | if (isSub) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3723 | New = BinaryOperator::CreateSub(LHSI->getOperand(0), RHS, "fold"); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3724 | else |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3725 | New = BinaryOperator::CreateAdd(LHSI->getOperand(0), RHS, "fold"); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3726 | return InsertNewInstBefore(New, I); |
| 3727 | } |
| 3728 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3729 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3730 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3731 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3732 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3733 | if (isa<UndefValue>(Op1)) // X & undef -> 0 |
| 3734 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3735 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3736 | // and X, X = X |
| 3737 | if (Op0 == Op1) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3738 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3739 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3740 | // See if we can simplify any instructions used by the instruction whose sole |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 3741 | // purpose is to compute bits we don't care about. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3742 | if (!isa<VectorType>(I.getType())) { |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 3743 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 3744 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 3745 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3746 | KnownZero, KnownOne)) |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3747 | return &I; |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3748 | } else { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3749 | if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) { |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3750 | if (CP->isAllOnesValue()) // X & <-1,-1> -> X |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3751 | return ReplaceInstUsesWith(I, I.getOperand(0)); |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3752 | } else if (isa<ConstantAggregateZero>(Op1)) { |
| 3753 | return ReplaceInstUsesWith(I, Op1); // X & <0,0> -> <0,0> |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3754 | } |
| 3755 | } |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 3756 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3757 | if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3758 | const APInt& AndRHSMask = AndRHS->getValue(); |
| 3759 | APInt NotAndRHS(~AndRHSMask); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3760 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3761 | // Optimize a variety of ((val OP C1) & C2) combinations... |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3762 | if (isa<BinaryOperator>(Op0)) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3763 | Instruction *Op0I = cast<Instruction>(Op0); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3764 | Value *Op0LHS = Op0I->getOperand(0); |
| 3765 | Value *Op0RHS = Op0I->getOperand(1); |
| 3766 | switch (Op0I->getOpcode()) { |
| 3767 | case Instruction::Xor: |
| 3768 | case Instruction::Or: |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3769 | // If the mask is only needed on one incoming arm, push it up. |
| 3770 | if (Op0I->hasOneUse()) { |
| 3771 | if (MaskedValueIsZero(Op0LHS, NotAndRHS)) { |
| 3772 | // Not masking anything out for the LHS, move to RHS. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3773 | Instruction *NewRHS = BinaryOperator::CreateAnd(Op0RHS, AndRHS, |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3774 | Op0RHS->getName()+".masked"); |
| 3775 | InsertNewInstBefore(NewRHS, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3776 | return BinaryOperator::Create( |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3777 | cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3778 | } |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3779 | if (!isa<Constant>(Op0RHS) && |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3780 | MaskedValueIsZero(Op0RHS, NotAndRHS)) { |
| 3781 | // Not masking anything out for the RHS, move to LHS. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3782 | Instruction *NewLHS = BinaryOperator::CreateAnd(Op0LHS, AndRHS, |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3783 | Op0LHS->getName()+".masked"); |
| 3784 | InsertNewInstBefore(NewLHS, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3785 | return BinaryOperator::Create( |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3786 | cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS); |
| 3787 | } |
| 3788 | } |
| 3789 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3790 | break; |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3791 | case Instruction::Add: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3792 | // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS. |
| 3793 | // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 3794 | // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 3795 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3796 | return BinaryOperator::CreateAnd(V, AndRHS); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3797 | if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3798 | return BinaryOperator::CreateAnd(V, AndRHS); // Add commutes |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3799 | break; |
| 3800 | |
| 3801 | case Instruction::Sub: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3802 | // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS. |
| 3803 | // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 3804 | // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 3805 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3806 | return BinaryOperator::CreateAnd(V, AndRHS); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3807 | break; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3808 | } |
| 3809 | |
Chris Lattner | 5840326 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 3810 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3811 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3812 | return Res; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3813 | } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3814 | // If this is an integer truncation or change from signed-to-unsigned, and |
| 3815 | // if the source is an and/or with immediate, transform it. This |
| 3816 | // frequently occurs for bitfield accesses. |
| 3817 | if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3818 | if ((isa<TruncInst>(CI) || isa<BitCastInst>(CI)) && |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3819 | CastOp->getNumOperands() == 2) |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3820 | if (ConstantInt *AndCI = dyn_cast<ConstantInt>(CastOp->getOperand(1))) { |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3821 | if (CastOp->getOpcode() == Instruction::And) { |
| 3822 | // Change: and (cast (and X, C1) to T), C2 |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3823 | // into : and (cast X to T), trunc_or_bitcast(C1)&C2 |
| 3824 | // This will fold the two constants together, which may allow |
| 3825 | // other simplifications. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3826 | Instruction *NewCast = CastInst::CreateTruncOrBitCast( |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3827 | CastOp->getOperand(0), I.getType(), |
| 3828 | CastOp->getName()+".shrunk"); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3829 | NewCast = InsertNewInstBefore(NewCast, I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3830 | // trunc_or_bitcast(C1)&C2 |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3831 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3832 | C3 = ConstantExpr::getAnd(C3, AndRHS); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3833 | return BinaryOperator::CreateAnd(NewCast, C3); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3834 | } else if (CastOp->getOpcode() == Instruction::Or) { |
| 3835 | // Change: and (cast (or X, C1) to T), C2 |
| 3836 | // into : trunc(C1)&C2 iff trunc(C1)&C2 == C2 |
Chris Lattner | bb4e7b2 | 2006-12-12 19:11:20 +0000 | [diff] [blame] | 3837 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3838 | if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS) // trunc(C1)&C2 |
| 3839 | return ReplaceInstUsesWith(I, AndRHS); |
| 3840 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3841 | } |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3842 | } |
Chris Lattner | 06782f8 | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 3843 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3844 | |
| 3845 | // Try to fold constant and into select arguments. |
| 3846 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3847 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3848 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3849 | if (isa<PHINode>(Op0)) |
| 3850 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3851 | return NV; |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3852 | } |
| 3853 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3854 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 3855 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3856 | |
Chris Lattner | 5b62aa7 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 3857 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
| 3858 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3859 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 3860 | // (~A & ~B) == (~(A | B)) - De Morgan's Law |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3861 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3862 | Instruction *Or = BinaryOperator::CreateOr(Op0NotVal, Op1NotVal, |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3863 | I.getName()+".demorgan"); |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3864 | InsertNewInstBefore(Or, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3865 | return BinaryOperator::CreateNot(Or); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3866 | } |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3867 | |
| 3868 | { |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3869 | Value *A = 0, *B = 0, *C = 0, *D = 0; |
| 3870 | if (match(Op0, m_Or(m_Value(A), m_Value(B)))) { |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3871 | if (A == Op1 || B == Op1) // (A | ?) & A --> A |
| 3872 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3873 | |
| 3874 | // (A|B) & ~(A&B) -> A^B |
| 3875 | if (match(Op1, m_Not(m_And(m_Value(C), m_Value(D))))) { |
| 3876 | if ((A == C && B == D) || (A == D && B == C)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3877 | return BinaryOperator::CreateXor(A, B); |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3878 | } |
| 3879 | } |
| 3880 | |
| 3881 | if (match(Op1, m_Or(m_Value(A), m_Value(B)))) { |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3882 | if (A == Op0 || B == Op0) // A & (A | ?) --> A |
| 3883 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3884 | |
| 3885 | // ~(A&B) & (A|B) -> A^B |
| 3886 | if (match(Op0, m_Not(m_And(m_Value(C), m_Value(D))))) { |
| 3887 | if ((A == C && B == D) || (A == D && B == C)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3888 | return BinaryOperator::CreateXor(A, B); |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3889 | } |
| 3890 | } |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3891 | |
| 3892 | if (Op0->hasOneUse() && |
| 3893 | match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 3894 | if (A == Op1) { // (A^B)&A -> A&(A^B) |
| 3895 | I.swapOperands(); // Simplify below |
| 3896 | std::swap(Op0, Op1); |
| 3897 | } else if (B == Op1) { // (A^B)&B -> B&(B^A) |
| 3898 | cast<BinaryOperator>(Op0)->swapOperands(); |
| 3899 | I.swapOperands(); // Simplify below |
| 3900 | std::swap(Op0, Op1); |
| 3901 | } |
| 3902 | } |
| 3903 | if (Op1->hasOneUse() && |
| 3904 | match(Op1, m_Xor(m_Value(A), m_Value(B)))) { |
| 3905 | if (B == Op0) { // B&(A^B) -> B&(B^A) |
| 3906 | cast<BinaryOperator>(Op1)->swapOperands(); |
| 3907 | std::swap(A, B); |
| 3908 | } |
| 3909 | if (A == Op0) { // A&(A^B) -> A & ~B |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3910 | Instruction *NotB = BinaryOperator::CreateNot(B, "tmp"); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3911 | InsertNewInstBefore(NotB, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3912 | return BinaryOperator::CreateAnd(A, NotB); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3913 | } |
| 3914 | } |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3915 | } |
| 3916 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3917 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1)) { |
| 3918 | // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| 3919 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3920 | return R; |
| 3921 | |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3922 | Value *LHSVal, *RHSVal; |
| 3923 | ConstantInt *LHSCst, *RHSCst; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3924 | ICmpInst::Predicate LHSCC, RHSCC; |
| 3925 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 3926 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 3927 | if (LHSVal == RHSVal && // Found (X icmp C1) & (X icmp C2) |
| 3928 | // ICMP_[GL]E X, CST is folded to ICMP_[GL]T elsewhere. |
| 3929 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && |
| 3930 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && |
| 3931 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && |
Chris Lattner | eec8b9a | 2007-11-22 23:47:13 +0000 | [diff] [blame] | 3932 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE && |
| 3933 | |
| 3934 | // Don't try to fold ICMP_SLT + ICMP_ULT. |
| 3935 | (ICmpInst::isEquality(LHSCC) || ICmpInst::isEquality(RHSCC) || |
| 3936 | ICmpInst::isSignedPredicate(LHSCC) == |
| 3937 | ICmpInst::isSignedPredicate(RHSCC))) { |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3938 | // Ensure that the larger constant is on the RHS. |
Chris Lattner | ee2b7a4 | 2008-01-13 20:59:02 +0000 | [diff] [blame] | 3939 | ICmpInst::Predicate GT; |
| 3940 | if (ICmpInst::isSignedPredicate(LHSCC) || |
| 3941 | (ICmpInst::isEquality(LHSCC) && |
| 3942 | ICmpInst::isSignedPredicate(RHSCC))) |
| 3943 | GT = ICmpInst::ICMP_SGT; |
| 3944 | else |
| 3945 | GT = ICmpInst::ICMP_UGT; |
| 3946 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3947 | Constant *Cmp = ConstantExpr::getICmp(GT, LHSCst, RHSCst); |
| 3948 | ICmpInst *LHS = cast<ICmpInst>(Op0); |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3949 | if (cast<ConstantInt>(Cmp)->getZExtValue()) { |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3950 | std::swap(LHS, RHS); |
| 3951 | std::swap(LHSCst, RHSCst); |
| 3952 | std::swap(LHSCC, RHSCC); |
| 3953 | } |
| 3954 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3955 | // At this point, we know we have have two icmp instructions |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3956 | // comparing a value against two constants and and'ing the result |
| 3957 | // together. Because of the above check, we know that we only have |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3958 | // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know |
| 3959 | // (from the FoldICmpLogical check above), that the two constants |
| 3960 | // are not equal and that the larger constant is on the RHS |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3961 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 3962 | |
| 3963 | switch (LHSCC) { |
| 3964 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3965 | case ICmpInst::ICMP_EQ: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3966 | switch (RHSCC) { |
| 3967 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3968 | case ICmpInst::ICMP_EQ: // (X == 13 & X == 15) -> false |
| 3969 | case ICmpInst::ICMP_UGT: // (X == 13 & X > 15) -> false |
| 3970 | case ICmpInst::ICMP_SGT: // (X == 13 & X > 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3971 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3972 | case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13 |
| 3973 | case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13 |
| 3974 | case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3975 | return ReplaceInstUsesWith(I, LHS); |
| 3976 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3977 | case ICmpInst::ICMP_NE: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3978 | switch (RHSCC) { |
| 3979 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3980 | case ICmpInst::ICMP_ULT: |
| 3981 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13 |
| 3982 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSVal, LHSCst); |
| 3983 | break; // (X != 13 & X u< 15) -> no change |
| 3984 | case ICmpInst::ICMP_SLT: |
| 3985 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13 |
| 3986 | return new ICmpInst(ICmpInst::ICMP_SLT, LHSVal, LHSCst); |
| 3987 | break; // (X != 13 & X s< 15) -> no change |
| 3988 | case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15 |
| 3989 | case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15 |
| 3990 | case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3991 | return ReplaceInstUsesWith(I, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3992 | case ICmpInst::ICMP_NE: |
| 3993 | if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3994 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3995 | Instruction *Add = BinaryOperator::CreateAdd(LHSVal, AddCST, |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3996 | LHSVal->getName()+".off"); |
| 3997 | InsertNewInstBefore(Add, I); |
Chris Lattner | 424db02 | 2007-01-27 23:08:34 +0000 | [diff] [blame] | 3998 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, |
| 3999 | ConstantInt::get(Add->getType(), 1)); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 4000 | } |
| 4001 | break; // (X != 13 & X != 15) -> no change |
| 4002 | } |
| 4003 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4004 | case ICmpInst::ICMP_ULT: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 4005 | switch (RHSCC) { |
| 4006 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4007 | case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false |
| 4008 | case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4009 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4010 | case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change |
| 4011 | break; |
| 4012 | case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13 |
| 4013 | case ICmpInst::ICMP_ULT: // (X u< 13 & X u< 15) -> X u< 13 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 4014 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4015 | case ICmpInst::ICMP_SLT: // (X u< 13 & X s< 15) -> no change |
| 4016 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 4017 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4018 | break; |
| 4019 | case ICmpInst::ICMP_SLT: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 4020 | switch (RHSCC) { |
| 4021 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4022 | case ICmpInst::ICMP_EQ: // (X s< 13 & X == 15) -> false |
| 4023 | case ICmpInst::ICMP_SGT: // (X s< 13 & X s> 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4024 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4025 | case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change |
| 4026 | break; |
| 4027 | case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13 |
| 4028 | case ICmpInst::ICMP_SLT: // (X s< 13 & X s< 15) -> X < 13 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 4029 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4030 | case ICmpInst::ICMP_ULT: // (X s< 13 & X u< 15) -> no change |
| 4031 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 4032 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4033 | break; |
| 4034 | case ICmpInst::ICMP_UGT: |
| 4035 | switch (RHSCC) { |
| 4036 | default: assert(0 && "Unknown integer condition code!"); |
| 4037 | case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X > 13 |
| 4038 | return ReplaceInstUsesWith(I, LHS); |
| 4039 | case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15 |
| 4040 | return ReplaceInstUsesWith(I, RHS); |
| 4041 | case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change |
| 4042 | break; |
| 4043 | case ICmpInst::ICMP_NE: |
| 4044 | if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14 |
| 4045 | return new ICmpInst(LHSCC, LHSVal, RHSCst); |
| 4046 | break; // (X u> 13 & X != 15) -> no change |
| 4047 | case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) ->(X-14) <u 1 |
| 4048 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, false, |
| 4049 | true, I); |
| 4050 | case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change |
| 4051 | break; |
| 4052 | } |
| 4053 | break; |
| 4054 | case ICmpInst::ICMP_SGT: |
| 4055 | switch (RHSCC) { |
| 4056 | default: assert(0 && "Unknown integer condition code!"); |
Chris Lattner | a7d1ab0 | 2007-11-16 06:04:17 +0000 | [diff] [blame] | 4057 | case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X == 15 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4058 | case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15 |
| 4059 | return ReplaceInstUsesWith(I, RHS); |
| 4060 | case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change |
| 4061 | break; |
| 4062 | case ICmpInst::ICMP_NE: |
| 4063 | if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14 |
| 4064 | return new ICmpInst(LHSCC, LHSVal, RHSCst); |
| 4065 | break; // (X s> 13 & X != 15) -> no change |
| 4066 | case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) ->(X-14) s< 1 |
| 4067 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, true, |
| 4068 | true, I); |
| 4069 | case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change |
| 4070 | break; |
| 4071 | } |
| 4072 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 4073 | } |
| 4074 | } |
| 4075 | } |
| 4076 | |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4077 | // fold (and (cast A), (cast B)) -> (cast (and A, B)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4078 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) |
| 4079 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
| 4080 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind ? |
| 4081 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 4082 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4083 | // Only do this if the casts both really cause code to be generated. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4084 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 4085 | I.getType(), TD) && |
| 4086 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 4087 | I.getType(), TD)) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4088 | Instruction *NewOp = BinaryOperator::CreateAnd(Op0C->getOperand(0), |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4089 | Op1C->getOperand(0), |
| 4090 | I.getName()); |
| 4091 | InsertNewInstBefore(NewOp, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4092 | return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType()); |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4093 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4094 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4095 | |
| 4096 | // (X >> Z) & (Y >> Z) -> (X&Y) >> Z for all shifts. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4097 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 4098 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 4099 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4100 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 4101 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 4102 | Instruction *NewOp = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4103 | InsertNewInstBefore(BinaryOperator::CreateAnd(SI0->getOperand(0), |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4104 | SI1->getOperand(0), |
| 4105 | SI0->getName()), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4106 | return BinaryOperator::Create(SI1->getOpcode(), NewOp, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4107 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4108 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4109 | } |
| 4110 | |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4111 | // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y) |
| 4112 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) { |
| 4113 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) { |
| 4114 | if (LHS->getPredicate() == FCmpInst::FCMP_ORD && |
| 4115 | RHS->getPredicate() == FCmpInst::FCMP_ORD) |
| 4116 | if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) |
| 4117 | if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { |
| 4118 | // If either of the constants are nans, then the whole thing returns |
| 4119 | // false. |
Chris Lattner | be3e348 | 2007-10-24 18:54:45 +0000 | [diff] [blame] | 4120 | if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4121 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
| 4122 | return new FCmpInst(FCmpInst::FCMP_ORD, LHS->getOperand(0), |
| 4123 | RHS->getOperand(0)); |
| 4124 | } |
| 4125 | } |
| 4126 | } |
| 4127 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4128 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4129 | } |
| 4130 | |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4131 | /// CollectBSwapParts - Look to see if the specified value defines a single byte |
| 4132 | /// in the result. If it does, and if the specified byte hasn't been filled in |
| 4133 | /// yet, fill it in and return false. |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 4134 | static bool CollectBSwapParts(Value *V, SmallVector<Value*, 8> &ByteValues) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4135 | Instruction *I = dyn_cast<Instruction>(V); |
| 4136 | if (I == 0) return true; |
| 4137 | |
| 4138 | // If this is an or instruction, it is an inner node of the bswap. |
| 4139 | if (I->getOpcode() == Instruction::Or) |
| 4140 | return CollectBSwapParts(I->getOperand(0), ByteValues) || |
| 4141 | CollectBSwapParts(I->getOperand(1), ByteValues); |
| 4142 | |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 4143 | uint32_t BitWidth = I->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4144 | // If this is a shift by a constant int, and it is "24", then its operand |
| 4145 | // defines a byte. We only handle unsigned types here. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4146 | if (I->isShift() && isa<ConstantInt>(I->getOperand(1))) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4147 | // Not shifting the entire input by N-1 bytes? |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 4148 | if (cast<ConstantInt>(I->getOperand(1))->getLimitedValue(BitWidth) != |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4149 | 8*(ByteValues.size()-1)) |
| 4150 | return true; |
| 4151 | |
| 4152 | unsigned DestNo; |
| 4153 | if (I->getOpcode() == Instruction::Shl) { |
| 4154 | // X << 24 defines the top byte with the lowest of the input bytes. |
| 4155 | DestNo = ByteValues.size()-1; |
| 4156 | } else { |
| 4157 | // X >>u 24 defines the low byte with the highest of the input bytes. |
| 4158 | DestNo = 0; |
| 4159 | } |
| 4160 | |
| 4161 | // If the destination byte value is already defined, the values are or'd |
| 4162 | // together, which isn't a bswap (unless it's an or of the same bits). |
| 4163 | if (ByteValues[DestNo] && ByteValues[DestNo] != I->getOperand(0)) |
| 4164 | return true; |
| 4165 | ByteValues[DestNo] = I->getOperand(0); |
| 4166 | return false; |
| 4167 | } |
| 4168 | |
| 4169 | // Otherwise, we can only handle and(shift X, imm), imm). Bail out of if we |
| 4170 | // don't have this. |
| 4171 | Value *Shift = 0, *ShiftLHS = 0; |
| 4172 | ConstantInt *AndAmt = 0, *ShiftAmt = 0; |
| 4173 | if (!match(I, m_And(m_Value(Shift), m_ConstantInt(AndAmt))) || |
| 4174 | !match(Shift, m_Shift(m_Value(ShiftLHS), m_ConstantInt(ShiftAmt)))) |
| 4175 | return true; |
| 4176 | Instruction *SI = cast<Instruction>(Shift); |
| 4177 | |
| 4178 | // Make sure that the shift amount is by a multiple of 8 and isn't too big. |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 4179 | if (ShiftAmt->getLimitedValue(BitWidth) & 7 || |
| 4180 | ShiftAmt->getLimitedValue(BitWidth) > 8*ByteValues.size()) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4181 | return true; |
| 4182 | |
| 4183 | // Turn 0xFF -> 0, 0xFF00 -> 1, 0xFF0000 -> 2, etc. |
| 4184 | unsigned DestByte; |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 4185 | if (AndAmt->getValue().getActiveBits() > 64) |
| 4186 | return true; |
| 4187 | uint64_t AndAmtVal = AndAmt->getZExtValue(); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4188 | for (DestByte = 0; DestByte != ByteValues.size(); ++DestByte) |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 4189 | if (AndAmtVal == uint64_t(0xFF) << 8*DestByte) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4190 | break; |
| 4191 | // Unknown mask for bswap. |
| 4192 | if (DestByte == ByteValues.size()) return true; |
| 4193 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4194 | unsigned ShiftBytes = ShiftAmt->getZExtValue()/8; |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4195 | unsigned SrcByte; |
| 4196 | if (SI->getOpcode() == Instruction::Shl) |
| 4197 | SrcByte = DestByte - ShiftBytes; |
| 4198 | else |
| 4199 | SrcByte = DestByte + ShiftBytes; |
| 4200 | |
| 4201 | // If the SrcByte isn't a bswapped value from the DestByte, reject it. |
| 4202 | if (SrcByte != ByteValues.size()-DestByte-1) |
| 4203 | return true; |
| 4204 | |
| 4205 | // If the destination byte value is already defined, the values are or'd |
| 4206 | // together, which isn't a bswap (unless it's an or of the same bits). |
| 4207 | if (ByteValues[DestByte] && ByteValues[DestByte] != SI->getOperand(0)) |
| 4208 | return true; |
| 4209 | ByteValues[DestByte] = SI->getOperand(0); |
| 4210 | return false; |
| 4211 | } |
| 4212 | |
| 4213 | /// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom. |
| 4214 | /// If so, insert the new bswap intrinsic and return it. |
| 4215 | Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) { |
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 4216 | const IntegerType *ITy = dyn_cast<IntegerType>(I.getType()); |
| 4217 | if (!ITy || ITy->getBitWidth() % 16) |
| 4218 | return 0; // Can only bswap pairs of bytes. Can't do vectors. |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4219 | |
| 4220 | /// ByteValues - For each byte of the result, we keep track of which value |
| 4221 | /// defines each byte. |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 4222 | SmallVector<Value*, 8> ByteValues; |
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 4223 | ByteValues.resize(ITy->getBitWidth()/8); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4224 | |
| 4225 | // Try to find all the pieces corresponding to the bswap. |
| 4226 | if (CollectBSwapParts(I.getOperand(0), ByteValues) || |
| 4227 | CollectBSwapParts(I.getOperand(1), ByteValues)) |
| 4228 | return 0; |
| 4229 | |
| 4230 | // Check to see if all of the bytes come from the same value. |
| 4231 | Value *V = ByteValues[0]; |
| 4232 | if (V == 0) return 0; // Didn't find a byte? Must be zero. |
| 4233 | |
| 4234 | // Check to make sure that all of the bytes come from the same value. |
| 4235 | for (unsigned i = 1, e = ByteValues.size(); i != e; ++i) |
| 4236 | if (ByteValues[i] != V) |
| 4237 | return 0; |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 4238 | const Type *Tys[] = { ITy }; |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4239 | Module *M = I.getParent()->getParent()->getParent(); |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 4240 | Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 4241 | return CallInst::Create(F, V); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4242 | } |
| 4243 | |
| 4244 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4245 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 4246 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4247 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4248 | |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 4249 | if (isa<UndefValue>(Op1)) // X | undef -> -1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4250 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4251 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4252 | // or X, X = X |
| 4253 | if (Op0 == Op1) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 4254 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4255 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4256 | // See if we can simplify any instructions used by the instruction whose sole |
| 4257 | // purpose is to compute bits we don't care about. |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 4258 | if (!isa<VectorType>(I.getType())) { |
| 4259 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 4260 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 4261 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 4262 | KnownZero, KnownOne)) |
| 4263 | return &I; |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 4264 | } else if (isa<ConstantAggregateZero>(Op1)) { |
| 4265 | return ReplaceInstUsesWith(I, Op0); // X | <0,0> -> X |
| 4266 | } else if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) { |
| 4267 | if (CP->isAllOnesValue()) // X | <-1,-1> -> <-1,-1> |
| 4268 | return ReplaceInstUsesWith(I, I.getOperand(1)); |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 4269 | } |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 4270 | |
| 4271 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4272 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4273 | // or X, -1 == -1 |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4274 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 4275 | ConstantInt *C1 = 0; Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4276 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
| 4277 | if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4278 | Instruction *Or = BinaryOperator::CreateOr(X, RHS); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4279 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4280 | Or->takeName(Op0); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4281 | return BinaryOperator::CreateAnd(Or, |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4282 | ConstantInt::get(RHS->getValue() | C1->getValue())); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4283 | } |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 4284 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4285 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
| 4286 | if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4287 | Instruction *Or = BinaryOperator::CreateOr(X, RHS); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4288 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4289 | Or->takeName(Op0); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4290 | return BinaryOperator::CreateXor(Or, |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4291 | ConstantInt::get(C1->getValue() & ~RHS->getValue())); |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 4292 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4293 | |
| 4294 | // Try to fold constant and into select arguments. |
| 4295 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4296 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4297 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 4298 | if (isa<PHINode>(Op0)) |
| 4299 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 4300 | return NV; |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 4301 | } |
| 4302 | |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 4303 | Value *A = 0, *B = 0; |
| 4304 | ConstantInt *C1 = 0, *C2 = 0; |
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 4305 | |
| 4306 | if (match(Op0, m_And(m_Value(A), m_Value(B)))) |
| 4307 | if (A == Op1 || B == Op1) // (A & ?) | A --> A |
| 4308 | return ReplaceInstUsesWith(I, Op1); |
| 4309 | if (match(Op1, m_And(m_Value(A), m_Value(B)))) |
| 4310 | if (A == Op0 || B == Op0) // A | (A & ?) --> A |
| 4311 | return ReplaceInstUsesWith(I, Op0); |
| 4312 | |
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 4313 | // (A | B) | C and A | (B | C) -> bswap if possible. |
| 4314 | // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible. |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4315 | if (match(Op0, m_Or(m_Value(), m_Value())) || |
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 4316 | match(Op1, m_Or(m_Value(), m_Value())) || |
| 4317 | (match(Op0, m_Shift(m_Value(), m_Value())) && |
| 4318 | match(Op1, m_Shift(m_Value(), m_Value())))) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4319 | if (Instruction *BSwap = MatchBSwap(I)) |
| 4320 | return BSwap; |
| 4321 | } |
| 4322 | |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 4323 | // (X^C)|Y -> (X|Y)^C iff Y&C == 0 |
| 4324 | if (Op0->hasOneUse() && match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) && |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4325 | MaskedValueIsZero(Op1, C1->getValue())) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4326 | Instruction *NOr = BinaryOperator::CreateOr(A, Op1); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4327 | InsertNewInstBefore(NOr, I); |
| 4328 | NOr->takeName(Op0); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4329 | return BinaryOperator::CreateXor(NOr, C1); |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 4330 | } |
| 4331 | |
| 4332 | // Y|(X^C) -> (X|Y)^C iff Y&C == 0 |
| 4333 | if (Op1->hasOneUse() && match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) && |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4334 | MaskedValueIsZero(Op0, C1->getValue())) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4335 | Instruction *NOr = BinaryOperator::CreateOr(A, Op0); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4336 | InsertNewInstBefore(NOr, I); |
| 4337 | NOr->takeName(Op0); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4338 | return BinaryOperator::CreateXor(NOr, C1); |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 4339 | } |
| 4340 | |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4341 | // (A & C)|(B & D) |
Chris Lattner | 2384d7b | 2007-06-19 05:43:49 +0000 | [diff] [blame] | 4342 | Value *C = 0, *D = 0; |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4343 | if (match(Op0, m_And(m_Value(A), m_Value(C))) && |
| 4344 | match(Op1, m_And(m_Value(B), m_Value(D)))) { |
Chris Lattner | 6cae0e0 | 2007-04-08 07:55:22 +0000 | [diff] [blame] | 4345 | Value *V1 = 0, *V2 = 0, *V3 = 0; |
| 4346 | C1 = dyn_cast<ConstantInt>(C); |
| 4347 | C2 = dyn_cast<ConstantInt>(D); |
| 4348 | if (C1 && C2) { // (A & C1)|(B & C2) |
| 4349 | // If we have: ((V + N) & C1) | (V & C2) |
| 4350 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 4351 | // replace with V+N. |
| 4352 | if (C1->getValue() == ~C2->getValue()) { |
| 4353 | if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+ |
| 4354 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { |
| 4355 | // Add commutes, try both ways. |
| 4356 | if (V1 == B && MaskedValueIsZero(V2, C2->getValue())) |
| 4357 | return ReplaceInstUsesWith(I, A); |
| 4358 | if (V2 == B && MaskedValueIsZero(V1, C2->getValue())) |
| 4359 | return ReplaceInstUsesWith(I, A); |
| 4360 | } |
| 4361 | // Or commutes, try both ways. |
| 4362 | if ((C1->getValue() & (C1->getValue()+1)) == 0 && |
| 4363 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { |
| 4364 | // Add commutes, try both ways. |
| 4365 | if (V1 == A && MaskedValueIsZero(V2, C1->getValue())) |
| 4366 | return ReplaceInstUsesWith(I, B); |
| 4367 | if (V2 == A && MaskedValueIsZero(V1, C1->getValue())) |
| 4368 | return ReplaceInstUsesWith(I, B); |
| 4369 | } |
| 4370 | } |
Chris Lattner | 044e533 | 2007-04-08 08:01:49 +0000 | [diff] [blame] | 4371 | V1 = 0; V2 = 0; V3 = 0; |
Chris Lattner | 6cae0e0 | 2007-04-08 07:55:22 +0000 | [diff] [blame] | 4372 | } |
| 4373 | |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4374 | // Check to see if we have any common things being and'ed. If so, find the |
| 4375 | // terms for V1 & (V2|V3). |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4376 | if (isOnlyUse(Op0) || isOnlyUse(Op1)) { |
| 4377 | if (A == B) // (A & C)|(A & D) == A & (C|D) |
| 4378 | V1 = A, V2 = C, V3 = D; |
| 4379 | else if (A == D) // (A & C)|(B & A) == A & (B|C) |
| 4380 | V1 = A, V2 = B, V3 = C; |
| 4381 | else if (C == B) // (A & C)|(C & D) == C & (A|D) |
| 4382 | V1 = C, V2 = A, V3 = D; |
| 4383 | else if (C == D) // (A & C)|(B & C) == C & (A|B) |
| 4384 | V1 = C, V2 = A, V3 = B; |
| 4385 | |
| 4386 | if (V1) { |
| 4387 | Value *Or = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4388 | InsertNewInstBefore(BinaryOperator::CreateOr(V2, V3, "tmp"), I); |
| 4389 | return BinaryOperator::CreateAnd(V1, Or); |
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 4390 | } |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4391 | } |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 4392 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4393 | |
| 4394 | // (X >> Z) | (Y >> Z) -> (X|Y) >> Z for all shifts. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4395 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 4396 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 4397 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4398 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 4399 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 4400 | Instruction *NewOp = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4401 | InsertNewInstBefore(BinaryOperator::CreateOr(SI0->getOperand(0), |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4402 | SI1->getOperand(0), |
| 4403 | SI0->getName()), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4404 | return BinaryOperator::Create(SI1->getOpcode(), NewOp, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4405 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4406 | } |
| 4407 | } |
Chris Lattner | 67ca768 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 4408 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4409 | if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1 |
| 4410 | if (A == Op1) // ~A | A == -1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4411 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4412 | } else { |
| 4413 | A = 0; |
| 4414 | } |
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 4415 | // Note, A is still live here! |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4416 | if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B |
| 4417 | if (Op0 == B) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4418 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 4419 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 4420 | // (~A | ~B) == (~(A & B)) - De Morgan's Law |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4421 | if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4422 | Value *And = InsertNewInstBefore(BinaryOperator::CreateAnd(A, B, |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4423 | I.getName()+".demorgan"), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4424 | return BinaryOperator::CreateNot(And); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4425 | } |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 4426 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4427 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4428 | // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B) |
| 4429 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) { |
| 4430 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 4431 | return R; |
| 4432 | |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4433 | Value *LHSVal, *RHSVal; |
| 4434 | ConstantInt *LHSCst, *RHSCst; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4435 | ICmpInst::Predicate LHSCC, RHSCC; |
| 4436 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 4437 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 4438 | if (LHSVal == RHSVal && // Found (X icmp C1) | (X icmp C2) |
| 4439 | // icmp [us][gl]e x, cst is folded to icmp [us][gl]t elsewhere. |
| 4440 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && |
| 4441 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && |
| 4442 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4443 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE && |
| 4444 | // We can't fold (ugt x, C) | (sgt x, C2). |
| 4445 | PredicatesFoldable(LHSCC, RHSCC)) { |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4446 | // Ensure that the larger constant is on the RHS. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4447 | ICmpInst *LHS = cast<ICmpInst>(Op0); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4448 | bool NeedsSwap; |
| 4449 | if (ICmpInst::isSignedPredicate(LHSCC)) |
Chris Lattner | 3aea1bd | 2007-05-11 16:58:45 +0000 | [diff] [blame] | 4450 | NeedsSwap = LHSCst->getValue().sgt(RHSCst->getValue()); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4451 | else |
Chris Lattner | 3aea1bd | 2007-05-11 16:58:45 +0000 | [diff] [blame] | 4452 | NeedsSwap = LHSCst->getValue().ugt(RHSCst->getValue()); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4453 | |
| 4454 | if (NeedsSwap) { |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4455 | std::swap(LHS, RHS); |
| 4456 | std::swap(LHSCst, RHSCst); |
| 4457 | std::swap(LHSCC, RHSCC); |
| 4458 | } |
| 4459 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4460 | // At this point, we know we have have two icmp instructions |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4461 | // comparing a value against two constants and or'ing the result |
| 4462 | // together. Because of the above check, we know that we only have |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4463 | // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the |
| 4464 | // FoldICmpLogical check above), that the two constants are not |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4465 | // equal. |
| 4466 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 4467 | |
| 4468 | switch (LHSCC) { |
| 4469 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4470 | case ICmpInst::ICMP_EQ: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4471 | switch (RHSCC) { |
| 4472 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4473 | case ICmpInst::ICMP_EQ: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4474 | if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13 <u 2 |
| 4475 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4476 | Instruction *Add = BinaryOperator::CreateAdd(LHSVal, AddCST, |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4477 | LHSVal->getName()+".off"); |
| 4478 | InsertNewInstBefore(Add, I); |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4479 | AddCST = Subtract(AddOne(RHSCst), LHSCst); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4480 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, AddCST); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4481 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4482 | break; // (X == 13 | X == 15) -> no change |
| 4483 | case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change |
| 4484 | case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change |
Chris Lattner | 240d6f4 | 2005-04-19 06:04:18 +0000 | [diff] [blame] | 4485 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4486 | case ICmpInst::ICMP_NE: // (X == 13 | X != 15) -> X != 15 |
| 4487 | case ICmpInst::ICMP_ULT: // (X == 13 | X u< 15) -> X u< 15 |
| 4488 | case ICmpInst::ICMP_SLT: // (X == 13 | X s< 15) -> X s< 15 |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4489 | return ReplaceInstUsesWith(I, RHS); |
| 4490 | } |
| 4491 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4492 | case ICmpInst::ICMP_NE: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4493 | switch (RHSCC) { |
| 4494 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4495 | case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13 |
| 4496 | case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13 |
| 4497 | case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13 |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4498 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4499 | case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true |
| 4500 | case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true |
| 4501 | case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4502 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4503 | } |
| 4504 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4505 | case ICmpInst::ICMP_ULT: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4506 | switch (RHSCC) { |
| 4507 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4508 | case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4509 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4510 | case ICmpInst::ICMP_UGT: // (X u< 13 | X u> 15) ->(X-13) u> 2 |
Chris Lattner | 74e012a | 2007-11-01 02:18:41 +0000 | [diff] [blame] | 4511 | // If RHSCst is [us]MAXINT, it is always false. Not handling |
| 4512 | // this can cause overflow. |
| 4513 | if (RHSCst->isMaxValue(false)) |
| 4514 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4515 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false, |
| 4516 | false, I); |
| 4517 | case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change |
| 4518 | break; |
| 4519 | case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15 |
| 4520 | case ICmpInst::ICMP_ULT: // (X u< 13 | X u< 15) -> X u< 15 |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4521 | return ReplaceInstUsesWith(I, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4522 | case ICmpInst::ICMP_SLT: // (X u< 13 | X s< 15) -> no change |
| 4523 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4524 | } |
| 4525 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4526 | case ICmpInst::ICMP_SLT: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4527 | switch (RHSCC) { |
| 4528 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4529 | case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change |
| 4530 | break; |
| 4531 | case ICmpInst::ICMP_SGT: // (X s< 13 | X s> 15) ->(X-13) s> 2 |
Chris Lattner | 74e012a | 2007-11-01 02:18:41 +0000 | [diff] [blame] | 4532 | // If RHSCst is [us]MAXINT, it is always false. Not handling |
| 4533 | // this can cause overflow. |
| 4534 | if (RHSCst->isMaxValue(true)) |
| 4535 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4536 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), true, |
| 4537 | false, I); |
| 4538 | case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change |
| 4539 | break; |
| 4540 | case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15 |
| 4541 | case ICmpInst::ICMP_SLT: // (X s< 13 | X s< 15) -> X s< 15 |
| 4542 | return ReplaceInstUsesWith(I, RHS); |
| 4543 | case ICmpInst::ICMP_ULT: // (X s< 13 | X u< 15) -> no change |
| 4544 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4545 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4546 | break; |
| 4547 | case ICmpInst::ICMP_UGT: |
| 4548 | switch (RHSCC) { |
| 4549 | default: assert(0 && "Unknown integer condition code!"); |
| 4550 | case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13 |
| 4551 | case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13 |
| 4552 | return ReplaceInstUsesWith(I, LHS); |
| 4553 | case ICmpInst::ICMP_SGT: // (X u> 13 | X s> 15) -> no change |
| 4554 | break; |
| 4555 | case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true |
| 4556 | case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4557 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4558 | case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change |
| 4559 | break; |
| 4560 | } |
| 4561 | break; |
| 4562 | case ICmpInst::ICMP_SGT: |
| 4563 | switch (RHSCC) { |
| 4564 | default: assert(0 && "Unknown integer condition code!"); |
| 4565 | case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13 |
| 4566 | case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13 |
| 4567 | return ReplaceInstUsesWith(I, LHS); |
| 4568 | case ICmpInst::ICMP_UGT: // (X s> 13 | X u> 15) -> no change |
| 4569 | break; |
| 4570 | case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true |
| 4571 | case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4572 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4573 | case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change |
| 4574 | break; |
| 4575 | } |
| 4576 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4577 | } |
| 4578 | } |
| 4579 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4580 | |
| 4581 | // fold (or (cast A), (cast B)) -> (cast (or A, B)) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4582 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4583 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4584 | if (Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ? |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 4585 | if (!isa<ICmpInst>(Op0C->getOperand(0)) || |
| 4586 | !isa<ICmpInst>(Op1C->getOperand(0))) { |
| 4587 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
| 4588 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
| 4589 | // Only do this if the casts both really cause code to be |
| 4590 | // generated. |
| 4591 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 4592 | I.getType(), TD) && |
| 4593 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 4594 | I.getType(), TD)) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4595 | Instruction *NewOp = BinaryOperator::CreateOr(Op0C->getOperand(0), |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 4596 | Op1C->getOperand(0), |
| 4597 | I.getName()); |
| 4598 | InsertNewInstBefore(NewOp, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4599 | return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType()); |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 4600 | } |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4601 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4602 | } |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4603 | } |
| 4604 | |
| 4605 | |
| 4606 | // (fcmp uno x, c) | (fcmp uno y, c) -> (fcmp uno x, y) |
| 4607 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) { |
| 4608 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) { |
| 4609 | if (LHS->getPredicate() == FCmpInst::FCMP_UNO && |
Chris Lattner | 5ebd936 | 2008-02-29 06:09:11 +0000 | [diff] [blame] | 4610 | RHS->getPredicate() == FCmpInst::FCMP_UNO && |
| 4611 | LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4612 | if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) |
| 4613 | if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { |
| 4614 | // If either of the constants are nans, then the whole thing returns |
| 4615 | // true. |
Chris Lattner | be3e348 | 2007-10-24 18:54:45 +0000 | [diff] [blame] | 4616 | if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4617 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
| 4618 | |
| 4619 | // Otherwise, no need to compare the two constants, compare the |
| 4620 | // rest. |
| 4621 | return new FCmpInst(FCmpInst::FCMP_UNO, LHS->getOperand(0), |
| 4622 | RHS->getOperand(0)); |
| 4623 | } |
| 4624 | } |
| 4625 | } |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 4626 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4627 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4628 | } |
| 4629 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 4630 | namespace { |
| 4631 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4632 | // XorSelf - Implements: X ^ X --> 0 |
| 4633 | struct XorSelf { |
| 4634 | Value *RHS; |
| 4635 | XorSelf(Value *rhs) : RHS(rhs) {} |
| 4636 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 4637 | Instruction *apply(BinaryOperator &Xor) const { |
| 4638 | return &Xor; |
| 4639 | } |
| 4640 | }; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4641 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 4642 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4643 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4644 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 4645 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4646 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4647 | |
Evan Cheng | d34af78 | 2008-03-25 20:07:13 +0000 | [diff] [blame] | 4648 | if (isa<UndefValue>(Op1)) { |
| 4649 | if (isa<UndefValue>(Op0)) |
| 4650 | // Handle undef ^ undef -> 0 special case. This is a common |
| 4651 | // idiom (misuse). |
| 4652 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4653 | return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef |
Evan Cheng | d34af78 | 2008-03-25 20:07:13 +0000 | [diff] [blame] | 4654 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4655 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4656 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
| 4657 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 4658 | assert(Result == &I && "AssociativeOpt didn't work?"); Result=Result; |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 4659 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4660 | } |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4661 | |
| 4662 | // See if we can simplify any instructions used by the instruction whose sole |
| 4663 | // purpose is to compute bits we don't care about. |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4664 | if (!isa<VectorType>(I.getType())) { |
| 4665 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 4666 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 4667 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 4668 | KnownZero, KnownOne)) |
| 4669 | return &I; |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 4670 | } else if (isa<ConstantAggregateZero>(Op1)) { |
| 4671 | return ReplaceInstUsesWith(I, Op0); // X ^ <0,0> -> X |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4672 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4673 | |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4674 | // Is this a ~ operation? |
| 4675 | if (Value *NotOp = dyn_castNotVal(&I)) { |
| 4676 | // ~(~X & Y) --> (X | ~Y) - De Morgan's Law |
| 4677 | // ~(~X | Y) === (X & ~Y) - De Morgan's Law |
| 4678 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) { |
| 4679 | if (Op0I->getOpcode() == Instruction::And || |
| 4680 | Op0I->getOpcode() == Instruction::Or) { |
| 4681 | if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands(); |
| 4682 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { |
| 4683 | Instruction *NotY = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4684 | BinaryOperator::CreateNot(Op0I->getOperand(1), |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4685 | Op0I->getOperand(1)->getName()+".not"); |
| 4686 | InsertNewInstBefore(NotY, I); |
| 4687 | if (Op0I->getOpcode() == Instruction::And) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4688 | return BinaryOperator::CreateOr(Op0NotVal, NotY); |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4689 | else |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4690 | return BinaryOperator::CreateAnd(Op0NotVal, NotY); |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4691 | } |
| 4692 | } |
| 4693 | } |
| 4694 | } |
| 4695 | |
| 4696 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4697 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Nick Lewycky | f947b3e | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 4698 | // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B |
| 4699 | if (RHS == ConstantInt::getTrue() && Op0->hasOneUse()) { |
| 4700 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4701 | return new ICmpInst(ICI->getInversePredicate(), |
| 4702 | ICI->getOperand(0), ICI->getOperand(1)); |
Chris Lattner | ad5b4fb | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 4703 | |
Nick Lewycky | f947b3e | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 4704 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(Op0)) |
| 4705 | return new FCmpInst(FCI->getInversePredicate(), |
| 4706 | FCI->getOperand(0), FCI->getOperand(1)); |
| 4707 | } |
| 4708 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4709 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 4710 | // ~(c-X) == X-c-1 == X+(-c-1) |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4711 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
| 4712 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4713 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); |
| 4714 | Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4715 | ConstantInt::get(I.getType(), 1)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4716 | return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4717 | } |
Chris Lattner | 5c6e2db | 2007-04-02 05:36:22 +0000 | [diff] [blame] | 4718 | |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 4719 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) { |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4720 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 4721 | // ~(X-c) --> (-c-1)-X |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4722 | if (RHS->isAllOnesValue()) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4723 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4724 | return BinaryOperator::CreateSub( |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4725 | ConstantExpr::getSub(NegOp0CI, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4726 | ConstantInt::get(I.getType(), 1)), |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 4727 | Op0I->getOperand(0)); |
Chris Lattner | acf4e07 | 2007-04-02 05:42:22 +0000 | [diff] [blame] | 4728 | } else if (RHS->getValue().isSignBit()) { |
Chris Lattner | 5c6e2db | 2007-04-02 05:36:22 +0000 | [diff] [blame] | 4729 | // (X + C) ^ signbit -> (X + C + signbit) |
| 4730 | Constant *C = ConstantInt::get(RHS->getValue() + Op0CI->getValue()); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4731 | return BinaryOperator::CreateAdd(Op0I->getOperand(0), C); |
Chris Lattner | cd1d6d5 | 2007-04-02 05:48:58 +0000 | [diff] [blame] | 4732 | |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4733 | } |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4734 | } else if (Op0I->getOpcode() == Instruction::Or) { |
| 4735 | // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0 |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4736 | if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) { |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4737 | Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS); |
| 4738 | // Anything in both C1 and C2 is known to be zero, remove it from |
| 4739 | // NewRHS. |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4740 | Constant *CommonBits = And(Op0CI, RHS); |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4741 | NewRHS = ConstantExpr::getAnd(NewRHS, |
| 4742 | ConstantExpr::getNot(CommonBits)); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 4743 | AddToWorkList(Op0I); |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4744 | I.setOperand(0, Op0I->getOperand(0)); |
| 4745 | I.setOperand(1, NewRHS); |
| 4746 | return &I; |
| 4747 | } |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 4748 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 4749 | } |
Chris Lattner | 05bd1b2 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 4750 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4751 | |
| 4752 | // Try to fold constant and into select arguments. |
| 4753 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4754 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4755 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 4756 | if (isa<PHINode>(Op0)) |
| 4757 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 4758 | return NV; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4759 | } |
| 4760 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 4761 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4762 | if (X == Op1) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4763 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4764 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 4765 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4766 | if (X == Op0) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4767 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4768 | |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4769 | |
| 4770 | BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1); |
| 4771 | if (Op1I) { |
| 4772 | Value *A, *B; |
| 4773 | if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) { |
| 4774 | if (A == Op0) { // B^(B|A) == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4775 | Op1I->swapOperands(); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4776 | I.swapOperands(); |
| 4777 | std::swap(Op0, Op1); |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4778 | } else if (B == Op0) { // B^(A|B) == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4779 | I.swapOperands(); // Simplified below. |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4780 | std::swap(Op0, Op1); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4781 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4782 | } else if (match(Op1I, m_Xor(m_Value(A), m_Value(B)))) { |
| 4783 | if (Op0 == A) // A^(A^B) == B |
| 4784 | return ReplaceInstUsesWith(I, B); |
| 4785 | else if (Op0 == B) // A^(B^A) == B |
| 4786 | return ReplaceInstUsesWith(I, A); |
| 4787 | } else if (match(Op1I, m_And(m_Value(A), m_Value(B))) && Op1I->hasOneUse()){ |
Chris Lattner | 6abbdf9 | 2007-04-01 05:36:37 +0000 | [diff] [blame] | 4788 | if (A == Op0) { // A^(A&B) -> A^(B&A) |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4789 | Op1I->swapOperands(); |
Chris Lattner | 6abbdf9 | 2007-04-01 05:36:37 +0000 | [diff] [blame] | 4790 | std::swap(A, B); |
| 4791 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4792 | if (B == Op0) { // A^(B&A) -> (B&A)^A |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4793 | I.swapOperands(); // Simplified below. |
| 4794 | std::swap(Op0, Op1); |
| 4795 | } |
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 4796 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4797 | } |
| 4798 | |
| 4799 | BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0); |
| 4800 | if (Op0I) { |
| 4801 | Value *A, *B; |
| 4802 | if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && Op0I->hasOneUse()) { |
| 4803 | if (A == Op1) // (B|A)^B == (A|B)^B |
| 4804 | std::swap(A, B); |
| 4805 | if (B == Op1) { // (A|B)^B == A & ~B |
| 4806 | Instruction *NotB = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4807 | InsertNewInstBefore(BinaryOperator::CreateNot(Op1, "tmp"), I); |
| 4808 | return BinaryOperator::CreateAnd(A, NotB); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4809 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4810 | } else if (match(Op0I, m_Xor(m_Value(A), m_Value(B)))) { |
| 4811 | if (Op1 == A) // (A^B)^A == B |
| 4812 | return ReplaceInstUsesWith(I, B); |
| 4813 | else if (Op1 == B) // (B^A)^A == B |
| 4814 | return ReplaceInstUsesWith(I, A); |
| 4815 | } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) && Op0I->hasOneUse()){ |
| 4816 | if (A == Op1) // (A&B)^A -> (B&A)^A |
| 4817 | std::swap(A, B); |
| 4818 | if (B == Op1 && // (B&A)^A == ~B & A |
Chris Lattner | ae1ab39 | 2006-04-01 22:05:01 +0000 | [diff] [blame] | 4819 | !isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4820 | Instruction *N = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4821 | InsertNewInstBefore(BinaryOperator::CreateNot(A, "tmp"), I); |
| 4822 | return BinaryOperator::CreateAnd(N, Op1); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4823 | } |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4824 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4825 | } |
| 4826 | |
| 4827 | // (X >> Z) ^ (Y >> Z) -> (X^Y) >> Z for all shifts. |
| 4828 | if (Op0I && Op1I && Op0I->isShift() && |
| 4829 | Op0I->getOpcode() == Op1I->getOpcode() && |
| 4830 | Op0I->getOperand(1) == Op1I->getOperand(1) && |
| 4831 | (Op1I->hasOneUse() || Op1I->hasOneUse())) { |
| 4832 | Instruction *NewOp = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4833 | InsertNewInstBefore(BinaryOperator::CreateXor(Op0I->getOperand(0), |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4834 | Op1I->getOperand(0), |
| 4835 | Op0I->getName()), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4836 | return BinaryOperator::Create(Op1I->getOpcode(), NewOp, |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4837 | Op1I->getOperand(1)); |
| 4838 | } |
| 4839 | |
| 4840 | if (Op0I && Op1I) { |
| 4841 | Value *A, *B, *C, *D; |
| 4842 | // (A & B)^(A | B) -> A ^ B |
| 4843 | if (match(Op0I, m_And(m_Value(A), m_Value(B))) && |
| 4844 | match(Op1I, m_Or(m_Value(C), m_Value(D)))) { |
| 4845 | if ((A == C && B == D) || (A == D && B == C)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4846 | return BinaryOperator::CreateXor(A, B); |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4847 | } |
| 4848 | // (A | B)^(A & B) -> A ^ B |
| 4849 | if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && |
| 4850 | match(Op1I, m_And(m_Value(C), m_Value(D)))) { |
| 4851 | if ((A == C && B == D) || (A == D && B == C)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4852 | return BinaryOperator::CreateXor(A, B); |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4853 | } |
| 4854 | |
| 4855 | // (A & B)^(C & D) |
| 4856 | if ((Op0I->hasOneUse() || Op1I->hasOneUse()) && |
| 4857 | match(Op0I, m_And(m_Value(A), m_Value(B))) && |
| 4858 | match(Op1I, m_And(m_Value(C), m_Value(D)))) { |
| 4859 | // (X & Y)^(X & Y) -> (Y^Z) & X |
| 4860 | Value *X = 0, *Y = 0, *Z = 0; |
| 4861 | if (A == C) |
| 4862 | X = A, Y = B, Z = D; |
| 4863 | else if (A == D) |
| 4864 | X = A, Y = B, Z = C; |
| 4865 | else if (B == C) |
| 4866 | X = B, Y = A, Z = D; |
| 4867 | else if (B == D) |
| 4868 | X = B, Y = A, Z = C; |
| 4869 | |
| 4870 | if (X) { |
| 4871 | Instruction *NewOp = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4872 | InsertNewInstBefore(BinaryOperator::CreateXor(Y, Z, Op0->getName()), I); |
| 4873 | return BinaryOperator::CreateAnd(NewOp, X); |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4874 | } |
| 4875 | } |
| 4876 | } |
| 4877 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4878 | // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B) |
| 4879 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) |
| 4880 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 4881 | return R; |
| 4882 | |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4883 | // fold (xor (cast A), (cast B)) -> (cast (xor A, B)) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4884 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4885 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4886 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind? |
| 4887 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 4888 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4889 | // Only do this if the casts both really cause code to be generated. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4890 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 4891 | I.getType(), TD) && |
| 4892 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 4893 | I.getType(), TD)) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4894 | Instruction *NewOp = BinaryOperator::CreateXor(Op0C->getOperand(0), |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4895 | Op1C->getOperand(0), |
| 4896 | I.getName()); |
| 4897 | InsertNewInstBefore(NewOp, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4898 | return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType()); |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4899 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4900 | } |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4901 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4902 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4903 | } |
| 4904 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4905 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result |
| 4906 | /// overflowed for this type. |
| 4907 | static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 4908 | ConstantInt *In2, bool IsSigned = false) { |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4909 | Result = cast<ConstantInt>(Add(In1, In2)); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4910 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 4911 | if (IsSigned) |
| 4912 | if (In2->getValue().isNegative()) |
| 4913 | return Result->getValue().sgt(In1->getValue()); |
| 4914 | else |
| 4915 | return Result->getValue().slt(In1->getValue()); |
| 4916 | else |
| 4917 | return Result->getValue().ult(In1->getValue()); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4918 | } |
| 4919 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4920 | /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the |
| 4921 | /// code necessary to compute the offset from the base pointer (without adding |
| 4922 | /// in the base pointer). Return the result as a signed integer of intptr size. |
| 4923 | static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { |
| 4924 | TargetData &TD = IC.getTargetData(); |
| 4925 | gep_type_iterator GTI = gep_type_begin(GEP); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4926 | const Type *IntPtrTy = TD.getIntPtrType(); |
| 4927 | Value *Result = Constant::getNullValue(IntPtrTy); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4928 | |
| 4929 | // Build a mask for high order bits. |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 4930 | unsigned IntPtrWidth = TD.getPointerSizeInBits(); |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4931 | uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4932 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4933 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { |
| 4934 | Value *Op = GEP->getOperand(i); |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 4935 | uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()) & PtrSizeMask; |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4936 | if (ConstantInt *OpC = dyn_cast<ConstantInt>(Op)) { |
| 4937 | if (OpC->isZero()) continue; |
| 4938 | |
| 4939 | // Handle a struct index, which adds its field offset to the pointer. |
| 4940 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 4941 | Size = TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue()); |
| 4942 | |
| 4943 | if (ConstantInt *RC = dyn_cast<ConstantInt>(Result)) |
| 4944 | Result = ConstantInt::get(RC->getValue() + APInt(IntPtrWidth, Size)); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4945 | else |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4946 | Result = IC.InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4947 | BinaryOperator::CreateAdd(Result, |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4948 | ConstantInt::get(IntPtrTy, Size), |
| 4949 | GEP->getName()+".offs"), I); |
| 4950 | continue; |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4951 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4952 | |
| 4953 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); |
| 4954 | Constant *OC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/); |
| 4955 | Scale = ConstantExpr::getMul(OC, Scale); |
| 4956 | if (Constant *RC = dyn_cast<Constant>(Result)) |
| 4957 | Result = ConstantExpr::getAdd(RC, Scale); |
| 4958 | else { |
| 4959 | // Emit an add instruction. |
| 4960 | Result = IC.InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4961 | BinaryOperator::CreateAdd(Result, Scale, |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4962 | GEP->getName()+".offs"), I); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4963 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4964 | continue; |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4965 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4966 | // Convert to correct type. |
| 4967 | if (Op->getType() != IntPtrTy) { |
| 4968 | if (Constant *OpC = dyn_cast<Constant>(Op)) |
| 4969 | Op = ConstantExpr::getSExt(OpC, IntPtrTy); |
| 4970 | else |
| 4971 | Op = IC.InsertNewInstBefore(new SExtInst(Op, IntPtrTy, |
| 4972 | Op->getName()+".c"), I); |
| 4973 | } |
| 4974 | if (Size != 1) { |
| 4975 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); |
| 4976 | if (Constant *OpC = dyn_cast<Constant>(Op)) |
| 4977 | Op = ConstantExpr::getMul(OpC, Scale); |
| 4978 | else // We'll let instcombine(mul) convert this to a shl if possible. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4979 | Op = IC.InsertNewInstBefore(BinaryOperator::CreateMul(Op, Scale, |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4980 | GEP->getName()+".idx"), I); |
| 4981 | } |
| 4982 | |
| 4983 | // Emit an add instruction. |
| 4984 | if (isa<Constant>(Op) && isa<Constant>(Result)) |
| 4985 | Result = ConstantExpr::getAdd(cast<Constant>(Op), |
| 4986 | cast<Constant>(Result)); |
| 4987 | else |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4988 | Result = IC.InsertNewInstBefore(BinaryOperator::CreateAdd(Op, Result, |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4989 | GEP->getName()+".offs"), I); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4990 | } |
| 4991 | return Result; |
| 4992 | } |
| 4993 | |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 4994 | |
| 4995 | /// EvaluateGEPOffsetExpression - Return an value that can be used to compare of |
| 4996 | /// the *offset* implied by GEP to zero. For example, if we have &A[i], we want |
| 4997 | /// to return 'i' for "icmp ne i, 0". Note that, in general, indices can be |
| 4998 | /// complex, and scales are involved. The above expression would also be legal |
| 4999 | /// to codegen as "icmp ne (i*4), 0" (assuming A is a pointer to i32). This |
| 5000 | /// later form is less amenable to optimization though, and we are allowed to |
| 5001 | /// generate the first by knowing that pointer arithmetic doesn't overflow. |
| 5002 | /// |
| 5003 | /// If we can't emit an optimized form for this expression, this returns null. |
| 5004 | /// |
| 5005 | static Value *EvaluateGEPOffsetExpression(User *GEP, Instruction &I, |
| 5006 | InstCombiner &IC) { |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5007 | TargetData &TD = IC.getTargetData(); |
| 5008 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 5009 | |
| 5010 | // Check to see if this gep only has a single variable index. If so, and if |
| 5011 | // any constant indices are a multiple of its scale, then we can compute this |
| 5012 | // in terms of the scale of the variable index. For example, if the GEP |
| 5013 | // implies an offset of "12 + i*4", then we can codegen this as "3 + i", |
| 5014 | // because the expression will cross zero at the same point. |
| 5015 | unsigned i, e = GEP->getNumOperands(); |
| 5016 | int64_t Offset = 0; |
| 5017 | for (i = 1; i != e; ++i, ++GTI) { |
| 5018 | if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) { |
| 5019 | // Compute the aggregate offset of constant indices. |
| 5020 | if (CI->isZero()) continue; |
| 5021 | |
| 5022 | // Handle a struct index, which adds its field offset to the pointer. |
| 5023 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 5024 | Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue()); |
| 5025 | } else { |
| 5026 | uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()); |
| 5027 | Offset += Size*CI->getSExtValue(); |
| 5028 | } |
| 5029 | } else { |
| 5030 | // Found our variable index. |
| 5031 | break; |
| 5032 | } |
| 5033 | } |
| 5034 | |
| 5035 | // If there are no variable indices, we must have a constant offset, just |
| 5036 | // evaluate it the general way. |
| 5037 | if (i == e) return 0; |
| 5038 | |
| 5039 | Value *VariableIdx = GEP->getOperand(i); |
| 5040 | // Determine the scale factor of the variable element. For example, this is |
| 5041 | // 4 if the variable index is into an array of i32. |
| 5042 | uint64_t VariableScale = TD.getABITypeSize(GTI.getIndexedType()); |
| 5043 | |
| 5044 | // Verify that there are no other variable indices. If so, emit the hard way. |
| 5045 | for (++i, ++GTI; i != e; ++i, ++GTI) { |
| 5046 | ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i)); |
| 5047 | if (!CI) return 0; |
| 5048 | |
| 5049 | // Compute the aggregate offset of constant indices. |
| 5050 | if (CI->isZero()) continue; |
| 5051 | |
| 5052 | // Handle a struct index, which adds its field offset to the pointer. |
| 5053 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 5054 | Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue()); |
| 5055 | } else { |
| 5056 | uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()); |
| 5057 | Offset += Size*CI->getSExtValue(); |
| 5058 | } |
| 5059 | } |
| 5060 | |
| 5061 | // Okay, we know we have a single variable index, which must be a |
| 5062 | // pointer/array/vector index. If there is no offset, life is simple, return |
| 5063 | // the index. |
| 5064 | unsigned IntPtrWidth = TD.getPointerSizeInBits(); |
| 5065 | if (Offset == 0) { |
| 5066 | // Cast to intptrty in case a truncation occurs. If an extension is needed, |
| 5067 | // we don't need to bother extending: the extension won't affect where the |
| 5068 | // computation crosses zero. |
| 5069 | if (VariableIdx->getType()->getPrimitiveSizeInBits() > IntPtrWidth) |
| 5070 | VariableIdx = new TruncInst(VariableIdx, TD.getIntPtrType(), |
| 5071 | VariableIdx->getNameStart(), &I); |
| 5072 | return VariableIdx; |
| 5073 | } |
| 5074 | |
| 5075 | // Otherwise, there is an index. The computation we will do will be modulo |
| 5076 | // the pointer size, so get it. |
| 5077 | uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth); |
| 5078 | |
| 5079 | Offset &= PtrSizeMask; |
| 5080 | VariableScale &= PtrSizeMask; |
| 5081 | |
| 5082 | // To do this transformation, any constant index must be a multiple of the |
| 5083 | // variable scale factor. For example, we can evaluate "12 + 4*i" as "3 + i", |
| 5084 | // but we can't evaluate "10 + 3*i" in terms of i. Check that the offset is a |
| 5085 | // multiple of the variable scale. |
| 5086 | int64_t NewOffs = Offset / (int64_t)VariableScale; |
| 5087 | if (Offset != NewOffs*(int64_t)VariableScale) |
| 5088 | return 0; |
| 5089 | |
| 5090 | // Okay, we can do this evaluation. Start by converting the index to intptr. |
| 5091 | const Type *IntPtrTy = TD.getIntPtrType(); |
| 5092 | if (VariableIdx->getType() != IntPtrTy) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5093 | VariableIdx = CastInst::CreateIntegerCast(VariableIdx, IntPtrTy, |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5094 | true /*SExt*/, |
| 5095 | VariableIdx->getNameStart(), &I); |
| 5096 | Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5097 | return BinaryOperator::CreateAdd(VariableIdx, OffsetVal, "offset", &I); |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5098 | } |
| 5099 | |
| 5100 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5101 | /// FoldGEPICmp - Fold comparisons between a GEP instruction and something |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5102 | /// else. At this point we know that the GEP is on the LHS of the comparison. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5103 | Instruction *InstCombiner::FoldGEPICmp(User *GEPLHS, Value *RHS, |
| 5104 | ICmpInst::Predicate Cond, |
| 5105 | Instruction &I) { |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5106 | assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!"); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 5107 | |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5108 | // Look through bitcasts. |
| 5109 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(RHS)) |
| 5110 | RHS = BCI->getOperand(0); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 5111 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5112 | Value *PtrBase = GEPLHS->getOperand(0); |
| 5113 | if (PtrBase == RHS) { |
Chris Lattner | 7c95deb | 2008-02-05 04:45:32 +0000 | [diff] [blame] | 5114 | // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5115 | // This transformation (ignoring the base and scales) is valid because we |
| 5116 | // know pointers can't overflow. See if we can output an optimized form. |
| 5117 | Value *Offset = EvaluateGEPOffsetExpression(GEPLHS, I, *this); |
| 5118 | |
| 5119 | // If not, synthesize the offset the hard way. |
| 5120 | if (Offset == 0) |
| 5121 | Offset = EmitGEPOffset(GEPLHS, I, *this); |
Chris Lattner | 7c95deb | 2008-02-05 04:45:32 +0000 | [diff] [blame] | 5122 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset, |
| 5123 | Constant::getNullValue(Offset->getType())); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5124 | } else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) { |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 5125 | // If the base pointers are different, but the indices are the same, just |
| 5126 | // compare the base pointer. |
| 5127 | if (PtrBase != GEPRHS->getOperand(0)) { |
| 5128 | bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands(); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 5129 | IndicesTheSame &= GEPLHS->getOperand(0)->getType() == |
Chris Lattner | 93b94a6 | 2005-04-26 14:40:41 +0000 | [diff] [blame] | 5130 | GEPRHS->getOperand(0)->getType(); |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 5131 | if (IndicesTheSame) |
| 5132 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 5133 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 5134 | IndicesTheSame = false; |
| 5135 | break; |
| 5136 | } |
| 5137 | |
| 5138 | // If all indices are the same, just compare the base pointers. |
| 5139 | if (IndicesTheSame) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5140 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), |
| 5141 | GEPLHS->getOperand(0), GEPRHS->getOperand(0)); |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 5142 | |
| 5143 | // Otherwise, the base pointers are different and the indices are |
| 5144 | // different, bail out. |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5145 | return 0; |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 5146 | } |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5147 | |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 5148 | // If one of the GEPs has all zero indices, recurse. |
| 5149 | bool AllZeros = true; |
| 5150 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 5151 | if (!isa<Constant>(GEPLHS->getOperand(i)) || |
| 5152 | !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) { |
| 5153 | AllZeros = false; |
| 5154 | break; |
| 5155 | } |
| 5156 | if (AllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5157 | return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0), |
| 5158 | ICmpInst::getSwappedPredicate(Cond), I); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 5159 | |
| 5160 | // If the other GEP has all zero indices, recurse. |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 5161 | AllZeros = true; |
| 5162 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 5163 | if (!isa<Constant>(GEPRHS->getOperand(i)) || |
| 5164 | !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) { |
| 5165 | AllZeros = false; |
| 5166 | break; |
| 5167 | } |
| 5168 | if (AllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5169 | return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 5170 | |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 5171 | if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) { |
| 5172 | // If the GEPs only differ by one index, compare it. |
| 5173 | unsigned NumDifferences = 0; // Keep track of # differences. |
| 5174 | unsigned DiffOperand = 0; // The operand that differs. |
| 5175 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 5176 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5177 | if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() != |
| 5178 | GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 5179 | // Irreconcilable differences. |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 5180 | NumDifferences = 2; |
| 5181 | break; |
| 5182 | } else { |
| 5183 | if (NumDifferences++) break; |
| 5184 | DiffOperand = i; |
| 5185 | } |
| 5186 | } |
| 5187 | |
| 5188 | if (NumDifferences == 0) // SAME GEP? |
| 5189 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 5190 | ConstantInt::get(Type::Int1Ty, |
Nick Lewycky | fc1efbb | 2008-05-17 07:33:39 +0000 | [diff] [blame^] | 5191 | ICmpInst::isTrueWhenEqual(Cond))); |
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 5192 | |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 5193 | else if (NumDifferences == 1) { |
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 5194 | Value *LHSV = GEPLHS->getOperand(DiffOperand); |
| 5195 | Value *RHSV = GEPRHS->getOperand(DiffOperand); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5196 | // Make sure we do a signed comparison here. |
| 5197 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 5198 | } |
| 5199 | } |
| 5200 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5201 | // Only lower this if the icmp is the only user of the GEP or if we expect |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5202 | // the result to fold to a constant! |
| 5203 | if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) && |
| 5204 | (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) { |
| 5205 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) |
| 5206 | Value *L = EmitGEPOffset(GEPLHS, I, *this); |
| 5207 | Value *R = EmitGEPOffset(GEPRHS, I, *this); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5208 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5209 | } |
| 5210 | } |
| 5211 | return 0; |
| 5212 | } |
| 5213 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5214 | Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { |
| 5215 | bool Changed = SimplifyCompare(I); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5216 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5217 | |
Chris Lattner | 58e9746 | 2007-01-14 19:42:17 +0000 | [diff] [blame] | 5218 | // Fold trivial predicates. |
| 5219 | if (I.getPredicate() == FCmpInst::FCMP_FALSE) |
| 5220 | return ReplaceInstUsesWith(I, Constant::getNullValue(Type::Int1Ty)); |
| 5221 | if (I.getPredicate() == FCmpInst::FCMP_TRUE) |
| 5222 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
| 5223 | |
| 5224 | // Simplify 'fcmp pred X, X' |
| 5225 | if (Op0 == Op1) { |
| 5226 | switch (I.getPredicate()) { |
| 5227 | default: assert(0 && "Unknown predicate!"); |
| 5228 | case FCmpInst::FCMP_UEQ: // True if unordered or equal |
| 5229 | case FCmpInst::FCMP_UGE: // True if unordered, greater than, or equal |
| 5230 | case FCmpInst::FCMP_ULE: // True if unordered, less than, or equal |
| 5231 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
| 5232 | case FCmpInst::FCMP_OGT: // True if ordered and greater than |
| 5233 | case FCmpInst::FCMP_OLT: // True if ordered and less than |
| 5234 | case FCmpInst::FCMP_ONE: // True if ordered and operands are unequal |
| 5235 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0)); |
| 5236 | |
| 5237 | case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y) |
| 5238 | case FCmpInst::FCMP_ULT: // True if unordered or less than |
| 5239 | case FCmpInst::FCMP_UGT: // True if unordered or greater than |
| 5240 | case FCmpInst::FCMP_UNE: // True if unordered or not equal |
| 5241 | // Canonicalize these to be 'fcmp uno %X, 0.0'. |
| 5242 | I.setPredicate(FCmpInst::FCMP_UNO); |
| 5243 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 5244 | return &I; |
| 5245 | |
| 5246 | case FCmpInst::FCMP_ORD: // True if ordered (no nans) |
| 5247 | case FCmpInst::FCMP_OEQ: // True if ordered and equal |
| 5248 | case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal |
| 5249 | case FCmpInst::FCMP_OLE: // True if ordered and less than or equal |
| 5250 | // Canonicalize these to be 'fcmp ord %X, 0.0'. |
| 5251 | I.setPredicate(FCmpInst::FCMP_ORD); |
| 5252 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 5253 | return &I; |
| 5254 | } |
| 5255 | } |
| 5256 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5257 | if (isa<UndefValue>(Op1)) // fcmp pred X, undef -> undef |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5258 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5259 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5260 | // Handle fcmp with constant RHS |
| 5261 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 5262 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 5263 | switch (LHSI->getOpcode()) { |
| 5264 | case Instruction::PHI: |
| 5265 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 5266 | return NV; |
| 5267 | break; |
| 5268 | case Instruction::Select: |
| 5269 | // If either operand of the select is a constant, we can fold the |
| 5270 | // comparison into the select arms, which will cause one to be |
| 5271 | // constant folded and the select turned into a bitwise or. |
| 5272 | Value *Op1 = 0, *Op2 = 0; |
| 5273 | if (LHSI->hasOneUse()) { |
| 5274 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 5275 | // Fold the known value into the constant operand. |
| 5276 | Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); |
| 5277 | // Insert a new FCmp of the other select operand. |
| 5278 | Op2 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), |
| 5279 | LHSI->getOperand(2), RHSC, |
| 5280 | I.getName()), I); |
| 5281 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 5282 | // Fold the known value into the constant operand. |
| 5283 | Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); |
| 5284 | // Insert a new FCmp of the other select operand. |
| 5285 | Op1 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), |
| 5286 | LHSI->getOperand(1), RHSC, |
| 5287 | I.getName()), I); |
| 5288 | } |
| 5289 | } |
| 5290 | |
| 5291 | if (Op1) |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 5292 | return SelectInst::Create(LHSI->getOperand(0), Op1, Op2); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5293 | break; |
| 5294 | } |
| 5295 | } |
| 5296 | |
| 5297 | return Changed ? &I : 0; |
| 5298 | } |
| 5299 | |
| 5300 | Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { |
| 5301 | bool Changed = SimplifyCompare(I); |
| 5302 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 5303 | const Type *Ty = Op0->getType(); |
| 5304 | |
| 5305 | // icmp X, X |
| 5306 | if (Op0 == Op1) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5307 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
Nick Lewycky | fc1efbb | 2008-05-17 07:33:39 +0000 | [diff] [blame^] | 5308 | I.isTrueWhenEqual())); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5309 | |
| 5310 | if (isa<UndefValue>(Op1)) // X icmp undef -> undef |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5311 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Christopher Lamb | 7a0678c | 2007-12-18 21:32:20 +0000 | [diff] [blame] | 5312 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5313 | // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value |
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 5314 | // addresses never equal each other! We already know that Op0 != Op1. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5315 | if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) || |
| 5316 | isa<ConstantPointerNull>(Op0)) && |
| 5317 | (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) || |
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 5318 | isa<ConstantPointerNull>(Op1))) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5319 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
Nick Lewycky | fc1efbb | 2008-05-17 07:33:39 +0000 | [diff] [blame^] | 5320 | !I.isTrueWhenEqual())); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5321 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5322 | // icmp's with boolean values can always be turned into bitwise operations |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5323 | if (Ty == Type::Int1Ty) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5324 | switch (I.getPredicate()) { |
| 5325 | default: assert(0 && "Invalid icmp instruction!"); |
| 5326 | case ICmpInst::ICMP_EQ: { // icmp eq bool %A, %B -> ~(A^B) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5327 | Instruction *Xor = BinaryOperator::CreateXor(Op0, Op1, I.getName()+"tmp"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5328 | InsertNewInstBefore(Xor, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5329 | return BinaryOperator::CreateNot(Xor); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5330 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5331 | case ICmpInst::ICMP_NE: // icmp eq bool %A, %B -> A^B |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5332 | return BinaryOperator::CreateXor(Op0, Op1); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5333 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5334 | case ICmpInst::ICMP_UGT: |
| 5335 | case ICmpInst::ICMP_SGT: |
| 5336 | std::swap(Op0, Op1); // Change icmp gt -> icmp lt |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5337 | // FALL THROUGH |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5338 | case ICmpInst::ICMP_ULT: |
| 5339 | case ICmpInst::ICMP_SLT: { // icmp lt bool A, B -> ~X & Y |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5340 | Instruction *Not = BinaryOperator::CreateNot(Op0, I.getName()+"tmp"); |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5341 | InsertNewInstBefore(Not, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5342 | return BinaryOperator::CreateAnd(Not, Op1); |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5343 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5344 | case ICmpInst::ICMP_UGE: |
| 5345 | case ICmpInst::ICMP_SGE: |
| 5346 | std::swap(Op0, Op1); // Change icmp ge -> icmp le |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5347 | // FALL THROUGH |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5348 | case ICmpInst::ICMP_ULE: |
| 5349 | case ICmpInst::ICMP_SLE: { // icmp le bool %A, %B -> ~A | B |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5350 | Instruction *Not = BinaryOperator::CreateNot(Op0, I.getName()+"tmp"); |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5351 | InsertNewInstBefore(Not, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5352 | return BinaryOperator::CreateOr(Not, Op1); |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5353 | } |
| 5354 | } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5355 | } |
| 5356 | |
Chris Lattner | 2be51ae | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 5357 | // See if we are doing a comparison between a constant and an instruction that |
| 5358 | // can be folded into the comparison. |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5359 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Christopher Lamb | 103e1a3 | 2007-12-20 07:21:11 +0000 | [diff] [blame] | 5360 | Value *A, *B; |
| 5361 | |
Chris Lattner | b656601 | 2008-01-05 01:18:20 +0000 | [diff] [blame] | 5362 | // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B) |
| 5363 | if (I.isEquality() && CI->isNullValue() && |
| 5364 | match(Op0, m_Sub(m_Value(A), m_Value(B)))) { |
| 5365 | // (icmp cond A B) if cond is equality |
| 5366 | return new ICmpInst(I.getPredicate(), A, B); |
Owen Anderson | f5783f8 | 2007-12-28 07:42:12 +0000 | [diff] [blame] | 5367 | } |
Christopher Lamb | 103e1a3 | 2007-12-20 07:21:11 +0000 | [diff] [blame] | 5368 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5369 | switch (I.getPredicate()) { |
| 5370 | default: break; |
| 5371 | case ICmpInst::ICMP_ULT: // A <u MIN -> FALSE |
| 5372 | if (CI->isMinValue(false)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5373 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5374 | if (CI->isMaxValue(false)) // A <u MAX -> A != MAX |
| 5375 | return new ICmpInst(ICmpInst::ICMP_NE, Op0,Op1); |
| 5376 | if (isMinValuePlusOne(CI,false)) // A <u MIN+1 -> A == MIN |
| 5377 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 5378 | // (x <u 2147483648) -> (x >s -1) -> true if sign bit clear |
| 5379 | if (CI->isMinValue(true)) |
| 5380 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, |
| 5381 | ConstantInt::getAllOnesValue(Op0->getType())); |
| 5382 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5383 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5384 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5385 | case ICmpInst::ICMP_SLT: |
| 5386 | if (CI->isMinValue(true)) // A <s MIN -> FALSE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5387 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5388 | if (CI->isMaxValue(true)) // A <s MAX -> A != MAX |
| 5389 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 5390 | if (isMinValuePlusOne(CI,true)) // A <s MIN+1 -> A == MIN |
| 5391 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); |
| 5392 | break; |
| 5393 | |
| 5394 | case ICmpInst::ICMP_UGT: |
| 5395 | if (CI->isMaxValue(false)) // A >u MAX -> FALSE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5396 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5397 | if (CI->isMinValue(false)) // A >u MIN -> A != MIN |
| 5398 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 5399 | if (isMaxValueMinusOne(CI, false)) // A >u MAX-1 -> A == MAX |
| 5400 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 5401 | |
| 5402 | // (x >u 2147483647) -> (x <s 0) -> true if sign bit set |
| 5403 | if (CI->isMaxValue(true)) |
| 5404 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, |
| 5405 | ConstantInt::getNullValue(Op0->getType())); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5406 | break; |
| 5407 | |
| 5408 | case ICmpInst::ICMP_SGT: |
| 5409 | if (CI->isMaxValue(true)) // A >s MAX -> FALSE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5410 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5411 | if (CI->isMinValue(true)) // A >s MIN -> A != MIN |
| 5412 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 5413 | if (isMaxValueMinusOne(CI, true)) // A >s MAX-1 -> A == MAX |
| 5414 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); |
| 5415 | break; |
| 5416 | |
| 5417 | case ICmpInst::ICMP_ULE: |
| 5418 | if (CI->isMaxValue(false)) // A <=u MAX -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5419 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5420 | if (CI->isMinValue(false)) // A <=u MIN -> A == MIN |
| 5421 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 5422 | if (isMaxValueMinusOne(CI,false)) // A <=u MAX-1 -> A != MAX |
| 5423 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI)); |
| 5424 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5425 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5426 | case ICmpInst::ICMP_SLE: |
| 5427 | if (CI->isMaxValue(true)) // A <=s MAX -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5428 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5429 | if (CI->isMinValue(true)) // A <=s MIN -> A == MIN |
| 5430 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 5431 | if (isMaxValueMinusOne(CI,true)) // A <=s MAX-1 -> A != MAX |
| 5432 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI)); |
| 5433 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5434 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5435 | case ICmpInst::ICMP_UGE: |
| 5436 | if (CI->isMinValue(false)) // A >=u MIN -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5437 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5438 | if (CI->isMaxValue(false)) // A >=u MAX -> A == MAX |
| 5439 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 5440 | if (isMinValuePlusOne(CI,false)) // A >=u MIN-1 -> A != MIN |
| 5441 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI)); |
| 5442 | break; |
| 5443 | |
| 5444 | case ICmpInst::ICMP_SGE: |
| 5445 | if (CI->isMinValue(true)) // A >=s MIN -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5446 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5447 | if (CI->isMaxValue(true)) // A >=s MAX -> A == MAX |
| 5448 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 5449 | if (isMinValuePlusOne(CI,true)) // A >=s MIN-1 -> A != MIN |
| 5450 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI)); |
| 5451 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5452 | } |
| 5453 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5454 | // If we still have a icmp le or icmp ge instruction, turn it into the |
| 5455 | // appropriate icmp lt or icmp gt instruction. Since the border cases have |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5456 | // already been handled above, this requires little checking. |
| 5457 | // |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 5458 | switch (I.getPredicate()) { |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 5459 | default: break; |
| 5460 | case ICmpInst::ICMP_ULE: |
| 5461 | return new ICmpInst(ICmpInst::ICMP_ULT, Op0, AddOne(CI)); |
| 5462 | case ICmpInst::ICMP_SLE: |
| 5463 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, AddOne(CI)); |
| 5464 | case ICmpInst::ICMP_UGE: |
| 5465 | return new ICmpInst( ICmpInst::ICMP_UGT, Op0, SubOne(CI)); |
| 5466 | case ICmpInst::ICMP_SGE: |
| 5467 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, SubOne(CI)); |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 5468 | } |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 5469 | |
| 5470 | // See if we can fold the comparison based on bits known to be zero or one |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 5471 | // in the input. If this comparison is a normal comparison, it demands all |
| 5472 | // bits, if it is a sign bit comparison, it only demands the sign bit. |
| 5473 | |
| 5474 | bool UnusedBit; |
| 5475 | bool isSignBit = isSignBitCheck(I.getPredicate(), CI, UnusedBit); |
| 5476 | |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5477 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
| 5478 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 5479 | if (SimplifyDemandedBits(Op0, |
| 5480 | isSignBit ? APInt::getSignBit(BitWidth) |
| 5481 | : APInt::getAllOnesValue(BitWidth), |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 5482 | KnownZero, KnownOne, 0)) |
| 5483 | return &I; |
| 5484 | |
| 5485 | // Given the known and unknown bits, compute a range that the LHS could be |
| 5486 | // in. |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5487 | if ((KnownOne | KnownZero) != 0) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5488 | // Compute the Min, Max and RHS values based on the known bits. For the |
| 5489 | // EQ and NE we use unsigned values. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 5490 | APInt Min(BitWidth, 0), Max(BitWidth, 0); |
| 5491 | const APInt& RHSVal = CI->getValue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5492 | if (ICmpInst::isSignedPredicate(I.getPredicate())) { |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5493 | ComputeSignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, Min, |
| 5494 | Max); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5495 | } else { |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5496 | ComputeUnsignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, Min, |
| 5497 | Max); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5498 | } |
| 5499 | switch (I.getPredicate()) { // LE/GE have been folded already. |
| 5500 | default: assert(0 && "Unknown icmp opcode!"); |
| 5501 | case ICmpInst::ICMP_EQ: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5502 | if (Max.ult(RHSVal) || Min.ugt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5503 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5504 | break; |
| 5505 | case ICmpInst::ICMP_NE: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5506 | if (Max.ult(RHSVal) || Min.ugt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5507 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5508 | break; |
| 5509 | case ICmpInst::ICMP_ULT: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5510 | if (Max.ult(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5511 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 81973ef | 2007-04-09 23:52:13 +0000 | [diff] [blame] | 5512 | if (Min.uge(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5513 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5514 | break; |
| 5515 | case ICmpInst::ICMP_UGT: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5516 | if (Min.ugt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5517 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 81973ef | 2007-04-09 23:52:13 +0000 | [diff] [blame] | 5518 | if (Max.ule(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5519 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5520 | break; |
| 5521 | case ICmpInst::ICMP_SLT: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5522 | if (Max.slt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5523 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5524 | if (Min.sgt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5525 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5526 | break; |
| 5527 | case ICmpInst::ICMP_SGT: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5528 | if (Min.sgt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5529 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 81973ef | 2007-04-09 23:52:13 +0000 | [diff] [blame] | 5530 | if (Max.sle(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5531 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5532 | break; |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 5533 | } |
| 5534 | } |
| 5535 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5536 | // Since the RHS is a ConstantInt (CI), if the left hand side is an |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 5537 | // instruction, see if that instruction also has constants so that the |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5538 | // instruction can be folded into the icmp |
Chris Lattner | 3c6a0d4 | 2004-05-25 06:32:08 +0000 | [diff] [blame] | 5539 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5540 | if (Instruction *Res = visitICmpInstWithInstAndIntCst(I, LHSI, CI)) |
| 5541 | return Res; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5542 | } |
| 5543 | |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5544 | // Handle icmp with constant (but not simple integer constant) RHS |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5545 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 5546 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 5547 | switch (LHSI->getOpcode()) { |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5548 | case Instruction::GetElementPtr: |
| 5549 | if (RHSC->isNullValue()) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5550 | // icmp pred GEP (P, int 0, int 0, int 0), null -> icmp pred P, null |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5551 | bool isAllZeros = true; |
| 5552 | for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i) |
| 5553 | if (!isa<Constant>(LHSI->getOperand(i)) || |
| 5554 | !cast<Constant>(LHSI->getOperand(i))->isNullValue()) { |
| 5555 | isAllZeros = false; |
| 5556 | break; |
| 5557 | } |
| 5558 | if (isAllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5559 | return new ICmpInst(I.getPredicate(), LHSI->getOperand(0), |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5560 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
| 5561 | } |
| 5562 | break; |
| 5563 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5564 | case Instruction::PHI: |
| 5565 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 5566 | return NV; |
| 5567 | break; |
Chris Lattner | 4802d90 | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 5568 | case Instruction::Select: { |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5569 | // If either operand of the select is a constant, we can fold the |
| 5570 | // comparison into the select arms, which will cause one to be |
| 5571 | // constant folded and the select turned into a bitwise or. |
| 5572 | Value *Op1 = 0, *Op2 = 0; |
| 5573 | if (LHSI->hasOneUse()) { |
| 5574 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 5575 | // Fold the known value into the constant operand. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5576 | Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 5577 | // Insert a new ICmp of the other select operand. |
| 5578 | Op2 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), |
| 5579 | LHSI->getOperand(2), RHSC, |
| 5580 | I.getName()), I); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5581 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 5582 | // Fold the known value into the constant operand. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5583 | Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 5584 | // Insert a new ICmp of the other select operand. |
| 5585 | Op1 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), |
| 5586 | LHSI->getOperand(1), RHSC, |
| 5587 | I.getName()), I); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5588 | } |
| 5589 | } |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 5590 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5591 | if (Op1) |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 5592 | return SelectInst::Create(LHSI->getOperand(0), Op1, Op2); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5593 | break; |
| 5594 | } |
Chris Lattner | 4802d90 | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 5595 | case Instruction::Malloc: |
| 5596 | // If we have (malloc != null), and if the malloc has a single use, we |
| 5597 | // can assume it is successful and remove the malloc. |
| 5598 | if (LHSI->hasOneUse() && isa<ConstantPointerNull>(RHSC)) { |
| 5599 | AddToWorkList(LHSI); |
| 5600 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
Nick Lewycky | fc1efbb | 2008-05-17 07:33:39 +0000 | [diff] [blame^] | 5601 | !I.isTrueWhenEqual())); |
Chris Lattner | 4802d90 | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 5602 | } |
| 5603 | break; |
| 5604 | } |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5605 | } |
| 5606 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5607 | // If we can optimize a 'icmp GEP, P' or 'icmp P, GEP', do so now. |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5608 | if (User *GEP = dyn_castGetElementPtr(Op0)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5609 | if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I)) |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5610 | return NI; |
| 5611 | if (User *GEP = dyn_castGetElementPtr(Op1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5612 | if (Instruction *NI = FoldGEPICmp(GEP, Op0, |
| 5613 | ICmpInst::getSwappedPredicate(I.getPredicate()), I)) |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5614 | return NI; |
| 5615 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5616 | // Test to see if the operands of the icmp are casted versions of other |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5617 | // values. If the ptr->ptr cast can be stripped off both arguments, we do so |
| 5618 | // now. |
| 5619 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) { |
| 5620 | if (isa<PointerType>(Op0->getType()) && |
| 5621 | (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) { |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5622 | // We keep moving the cast from the left operand over to the right |
| 5623 | // operand, where it can often be eliminated completely. |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5624 | Op0 = CI->getOperand(0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5625 | |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5626 | // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast |
| 5627 | // so eliminate it as well. |
| 5628 | if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1)) |
| 5629 | Op1 = CI2->getOperand(0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5630 | |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5631 | // If Op1 is a constant, we can fold the cast into the constant. |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 5632 | if (Op0->getType() != Op1->getType()) { |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5633 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 5634 | Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType()); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5635 | } else { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5636 | // Otherwise, cast the RHS right before the icmp |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 5637 | Op1 = InsertBitCastBefore(Op1, Op0->getType(), I); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5638 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 5639 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5640 | return new ICmpInst(I.getPredicate(), Op0, Op1); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5641 | } |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5642 | } |
| 5643 | |
| 5644 | if (isa<CastInst>(Op0)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5645 | // Handle the special case of: icmp (cast bool to X), <cst> |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 5646 | // This comes up when you have code like |
| 5647 | // int X = A < B; |
| 5648 | // if (X) ... |
| 5649 | // For generality, we handle any zero-extension of any operand comparison |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5650 | // with a constant or another cast from the same type. |
| 5651 | if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5652 | if (Instruction *R = visitICmpInstWithCastAndCast(I)) |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5653 | return R; |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 5654 | } |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5655 | |
Chris Lattner | 7d2cbd2 | 2008-05-09 05:19:28 +0000 | [diff] [blame] | 5656 | // ~x < ~y --> y < x |
| 5657 | { Value *A, *B; |
| 5658 | if (match(Op0, m_Not(m_Value(A))) && |
| 5659 | match(Op1, m_Not(m_Value(B)))) |
| 5660 | return new ICmpInst(I.getPredicate(), B, A); |
| 5661 | } |
| 5662 | |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 5663 | if (I.isEquality()) { |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5664 | Value *A, *B, *C, *D; |
Chris Lattner | 7d2cbd2 | 2008-05-09 05:19:28 +0000 | [diff] [blame] | 5665 | |
| 5666 | // -x == -y --> x == y |
| 5667 | if (match(Op0, m_Neg(m_Value(A))) && |
| 5668 | match(Op1, m_Neg(m_Value(B)))) |
| 5669 | return new ICmpInst(I.getPredicate(), A, B); |
| 5670 | |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5671 | if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 5672 | if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 |
| 5673 | Value *OtherVal = A == Op1 ? B : A; |
| 5674 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 5675 | Constant::getNullValue(A->getType())); |
| 5676 | } |
| 5677 | |
| 5678 | if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) { |
| 5679 | // A^c1 == C^c2 --> A == C^(c1^c2) |
| 5680 | if (ConstantInt *C1 = dyn_cast<ConstantInt>(B)) |
| 5681 | if (ConstantInt *C2 = dyn_cast<ConstantInt>(D)) |
| 5682 | if (Op1->hasOneUse()) { |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 5683 | Constant *NC = ConstantInt::get(C1->getValue() ^ C2->getValue()); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5684 | Instruction *Xor = BinaryOperator::CreateXor(C, NC, "tmp"); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5685 | return new ICmpInst(I.getPredicate(), A, |
| 5686 | InsertNewInstBefore(Xor, I)); |
| 5687 | } |
| 5688 | |
| 5689 | // A^B == A^D -> B == D |
| 5690 | if (A == C) return new ICmpInst(I.getPredicate(), B, D); |
| 5691 | if (A == D) return new ICmpInst(I.getPredicate(), B, C); |
| 5692 | if (B == C) return new ICmpInst(I.getPredicate(), A, D); |
| 5693 | if (B == D) return new ICmpInst(I.getPredicate(), A, C); |
| 5694 | } |
| 5695 | } |
| 5696 | |
| 5697 | if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && |
| 5698 | (A == Op0 || B == Op0)) { |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5699 | // A == (A^B) -> B == 0 |
| 5700 | Value *OtherVal = A == Op0 ? B : A; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5701 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 5702 | Constant::getNullValue(A->getType())); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5703 | } |
| 5704 | if (match(Op0, m_Sub(m_Value(A), m_Value(B))) && A == Op1) { |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5705 | // (A-B) == A -> B == 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5706 | return new ICmpInst(I.getPredicate(), B, |
| 5707 | Constant::getNullValue(B->getType())); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5708 | } |
| 5709 | if (match(Op1, m_Sub(m_Value(A), m_Value(B))) && A == Op0) { |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5710 | // A == (A-B) -> B == 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5711 | return new ICmpInst(I.getPredicate(), B, |
| 5712 | Constant::getNullValue(B->getType())); |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5713 | } |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5714 | |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5715 | // (X&Z) == (Y&Z) -> (X^Y) & Z == 0 |
| 5716 | if (Op0->hasOneUse() && Op1->hasOneUse() && |
| 5717 | match(Op0, m_And(m_Value(A), m_Value(B))) && |
| 5718 | match(Op1, m_And(m_Value(C), m_Value(D)))) { |
| 5719 | Value *X = 0, *Y = 0, *Z = 0; |
| 5720 | |
| 5721 | if (A == C) { |
| 5722 | X = B; Y = D; Z = A; |
| 5723 | } else if (A == D) { |
| 5724 | X = B; Y = C; Z = A; |
| 5725 | } else if (B == C) { |
| 5726 | X = A; Y = D; Z = B; |
| 5727 | } else if (B == D) { |
| 5728 | X = A; Y = C; Z = B; |
| 5729 | } |
| 5730 | |
| 5731 | if (X) { // Build (X^Y) & Z |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5732 | Op1 = InsertNewInstBefore(BinaryOperator::CreateXor(X, Y, "tmp"), I); |
| 5733 | Op1 = InsertNewInstBefore(BinaryOperator::CreateAnd(Op1, Z, "tmp"), I); |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5734 | I.setOperand(0, Op1); |
| 5735 | I.setOperand(1, Constant::getNullValue(Op1->getType())); |
| 5736 | return &I; |
| 5737 | } |
| 5738 | } |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5739 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5740 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5741 | } |
| 5742 | |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5743 | |
| 5744 | /// FoldICmpDivCst - Fold "icmp pred, ([su]div X, DivRHS), CmpRHS" where DivRHS |
| 5745 | /// and CmpRHS are both known to be integer constants. |
| 5746 | Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, |
| 5747 | ConstantInt *DivRHS) { |
| 5748 | ConstantInt *CmpRHS = cast<ConstantInt>(ICI.getOperand(1)); |
| 5749 | const APInt &CmpRHSV = CmpRHS->getValue(); |
| 5750 | |
| 5751 | // FIXME: If the operand types don't match the type of the divide |
| 5752 | // then don't attempt this transform. The code below doesn't have the |
| 5753 | // logic to deal with a signed divide and an unsigned compare (and |
| 5754 | // vice versa). This is because (x /s C1) <s C2 produces different |
| 5755 | // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even |
| 5756 | // (x /u C1) <u C2. Simply casting the operands and result won't |
| 5757 | // work. :( The if statement below tests that condition and bails |
| 5758 | // if it finds it. |
| 5759 | bool DivIsSigned = DivI->getOpcode() == Instruction::SDiv; |
| 5760 | if (!ICI.isEquality() && DivIsSigned != ICI.isSignedPredicate()) |
| 5761 | return 0; |
| 5762 | if (DivRHS->isZero()) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5763 | return 0; // The ProdOV computation fails on divide by zero. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5764 | |
| 5765 | // Compute Prod = CI * DivRHS. We are essentially solving an equation |
| 5766 | // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and |
| 5767 | // C2 (CI). By solving for X we can turn this into a range check |
| 5768 | // instead of computing a divide. |
| 5769 | ConstantInt *Prod = Multiply(CmpRHS, DivRHS); |
| 5770 | |
| 5771 | // Determine if the product overflows by seeing if the product is |
| 5772 | // not equal to the divide. Make sure we do the same kind of divide |
| 5773 | // as in the LHS instruction that we're folding. |
| 5774 | bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) : |
| 5775 | ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS; |
| 5776 | |
| 5777 | // Get the ICmp opcode |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5778 | ICmpInst::Predicate Pred = ICI.getPredicate(); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5779 | |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5780 | // Figure out the interval that is being checked. For example, a comparison |
| 5781 | // like "X /u 5 == 0" is really checking that X is in the interval [0, 5). |
| 5782 | // Compute this interval based on the constants involved and the signedness of |
| 5783 | // the compare/divide. This computes a half-open interval, keeping track of |
| 5784 | // whether either value in the interval overflows. After analysis each |
| 5785 | // overflow variable is set to 0 if it's corresponding bound variable is valid |
| 5786 | // -1 if overflowed off the bottom end, or +1 if overflowed off the top end. |
| 5787 | int LoOverflow = 0, HiOverflow = 0; |
| 5788 | ConstantInt *LoBound = 0, *HiBound = 0; |
| 5789 | |
| 5790 | |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5791 | if (!DivIsSigned) { // udiv |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5792 | // e.g. X/5 op 3 --> [15, 20) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5793 | LoBound = Prod; |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5794 | HiOverflow = LoOverflow = ProdOV; |
| 5795 | if (!HiOverflow) |
| 5796 | HiOverflow = AddWithOverflow(HiBound, LoBound, DivRHS, false); |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5797 | } else if (DivRHS->getValue().isStrictlyPositive()) { // Divisor is > 0. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5798 | if (CmpRHSV == 0) { // (X / pos) op 0 |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5799 | // Can't overflow. e.g. X/2 op 0 --> [-1, 2) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5800 | LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS))); |
| 5801 | HiBound = DivRHS; |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5802 | } else if (CmpRHSV.isStrictlyPositive()) { // (X / pos) op pos |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5803 | LoBound = Prod; // e.g. X/5 op 3 --> [15, 20) |
| 5804 | HiOverflow = LoOverflow = ProdOV; |
| 5805 | if (!HiOverflow) |
| 5806 | HiOverflow = AddWithOverflow(HiBound, Prod, DivRHS, true); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5807 | } else { // (X / pos) op neg |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5808 | // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5809 | Constant *DivRHSH = ConstantExpr::getNeg(SubOne(DivRHS)); |
| 5810 | LoOverflow = AddWithOverflow(LoBound, Prod, |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5811 | cast<ConstantInt>(DivRHSH), true) ? -1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5812 | HiBound = AddOne(Prod); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5813 | HiOverflow = ProdOV ? -1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5814 | } |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5815 | } else if (DivRHS->getValue().isNegative()) { // Divisor is < 0. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5816 | if (CmpRHSV == 0) { // (X / neg) op 0 |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5817 | // e.g. X/-5 op 0 --> [-4, 5) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5818 | LoBound = AddOne(DivRHS); |
| 5819 | HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS)); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5820 | if (HiBound == DivRHS) { // -INTMIN = INTMIN |
| 5821 | HiOverflow = 1; // [INTMIN+1, overflow) |
| 5822 | HiBound = 0; // e.g. X/INTMIN = 0 --> X > INTMIN |
| 5823 | } |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5824 | } else if (CmpRHSV.isStrictlyPositive()) { // (X / neg) op pos |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5825 | // e.g. X/-5 op 3 --> [-19, -14) |
| 5826 | HiOverflow = LoOverflow = ProdOV ? -1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5827 | if (!LoOverflow) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5828 | LoOverflow = AddWithOverflow(LoBound, Prod, AddOne(DivRHS), true) ?-1:0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5829 | HiBound = AddOne(Prod); |
| 5830 | } else { // (X / neg) op neg |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5831 | // e.g. X/-5 op -3 --> [15, 20) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5832 | LoBound = Prod; |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5833 | LoOverflow = HiOverflow = ProdOV ? 1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5834 | HiBound = Subtract(Prod, DivRHS); |
| 5835 | } |
| 5836 | |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5837 | // Dividing by a negative swaps the condition. LT <-> GT |
| 5838 | Pred = ICmpInst::getSwappedPredicate(Pred); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5839 | } |
| 5840 | |
| 5841 | Value *X = DivI->getOperand(0); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5842 | switch (Pred) { |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5843 | default: assert(0 && "Unhandled icmp opcode!"); |
| 5844 | case ICmpInst::ICMP_EQ: |
| 5845 | if (LoOverflow && HiOverflow) |
| 5846 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
| 5847 | else if (HiOverflow) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5848 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5849 | ICmpInst::ICMP_UGE, X, LoBound); |
| 5850 | else if (LoOverflow) |
| 5851 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
| 5852 | ICmpInst::ICMP_ULT, X, HiBound); |
| 5853 | else |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5854 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, true, ICI); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5855 | case ICmpInst::ICMP_NE: |
| 5856 | if (LoOverflow && HiOverflow) |
| 5857 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5858 | else if (HiOverflow) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5859 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5860 | ICmpInst::ICMP_ULT, X, LoBound); |
| 5861 | else if (LoOverflow) |
| 5862 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
| 5863 | ICmpInst::ICMP_UGE, X, HiBound); |
| 5864 | else |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5865 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, false, ICI); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5866 | case ICmpInst::ICMP_ULT: |
| 5867 | case ICmpInst::ICMP_SLT: |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5868 | if (LoOverflow == +1) // Low bound is greater than input range. |
| 5869 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5870 | if (LoOverflow == -1) // Low bound is less than input range. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5871 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5872 | return new ICmpInst(Pred, X, LoBound); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5873 | case ICmpInst::ICMP_UGT: |
| 5874 | case ICmpInst::ICMP_SGT: |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5875 | if (HiOverflow == +1) // High bound greater than input range. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5876 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5877 | else if (HiOverflow == -1) // High bound less than input range. |
| 5878 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5879 | if (Pred == ICmpInst::ICMP_UGT) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5880 | return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound); |
| 5881 | else |
| 5882 | return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound); |
| 5883 | } |
| 5884 | } |
| 5885 | |
| 5886 | |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5887 | /// visitICmpInstWithInstAndIntCst - Handle "icmp (instr, intcst)". |
| 5888 | /// |
| 5889 | Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, |
| 5890 | Instruction *LHSI, |
| 5891 | ConstantInt *RHS) { |
| 5892 | const APInt &RHSV = RHS->getValue(); |
| 5893 | |
| 5894 | switch (LHSI->getOpcode()) { |
Duncan Sands | 0091bf2 | 2007-04-04 06:42:45 +0000 | [diff] [blame] | 5895 | case Instruction::Xor: // (icmp pred (xor X, XorCST), CI) |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5896 | if (ConstantInt *XorCST = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
| 5897 | // If this is a comparison that tests the signbit (X < 0) or (x > -1), |
| 5898 | // fold the xor. |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 5899 | if ((ICI.getPredicate() == ICmpInst::ICMP_SLT && RHSV == 0) || |
| 5900 | (ICI.getPredicate() == ICmpInst::ICMP_SGT && RHSV.isAllOnesValue())) { |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5901 | Value *CompareVal = LHSI->getOperand(0); |
| 5902 | |
| 5903 | // If the sign bit of the XorCST is not set, there is no change to |
| 5904 | // the operation, just stop using the Xor. |
| 5905 | if (!XorCST->getValue().isNegative()) { |
| 5906 | ICI.setOperand(0, CompareVal); |
| 5907 | AddToWorkList(LHSI); |
| 5908 | return &ICI; |
| 5909 | } |
| 5910 | |
| 5911 | // Was the old condition true if the operand is positive? |
| 5912 | bool isTrueIfPositive = ICI.getPredicate() == ICmpInst::ICMP_SGT; |
| 5913 | |
| 5914 | // If so, the new one isn't. |
| 5915 | isTrueIfPositive ^= true; |
| 5916 | |
| 5917 | if (isTrueIfPositive) |
| 5918 | return new ICmpInst(ICmpInst::ICMP_SGT, CompareVal, SubOne(RHS)); |
| 5919 | else |
| 5920 | return new ICmpInst(ICmpInst::ICMP_SLT, CompareVal, AddOne(RHS)); |
| 5921 | } |
| 5922 | } |
| 5923 | break; |
| 5924 | case Instruction::And: // (icmp pred (and X, AndCST), RHS) |
| 5925 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && |
| 5926 | LHSI->getOperand(0)->hasOneUse()) { |
| 5927 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); |
| 5928 | |
| 5929 | // If the LHS is an AND of a truncating cast, we can widen the |
| 5930 | // and/compare to be the input width without changing the value |
| 5931 | // produced, eliminating a cast. |
| 5932 | if (TruncInst *Cast = dyn_cast<TruncInst>(LHSI->getOperand(0))) { |
| 5933 | // We can do this transformation if either the AND constant does not |
| 5934 | // have its sign bit set or if it is an equality comparison. |
| 5935 | // Extending a relational comparison when we're checking the sign |
| 5936 | // bit would not work. |
| 5937 | if (Cast->hasOneUse() && |
Anton Korobeynikov | 4aefd6b | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 5938 | (ICI.isEquality() || |
| 5939 | (AndCST->getValue().isNonNegative() && RHSV.isNonNegative()))) { |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5940 | uint32_t BitWidth = |
| 5941 | cast<IntegerType>(Cast->getOperand(0)->getType())->getBitWidth(); |
| 5942 | APInt NewCST = AndCST->getValue(); |
| 5943 | NewCST.zext(BitWidth); |
| 5944 | APInt NewCI = RHSV; |
| 5945 | NewCI.zext(BitWidth); |
| 5946 | Instruction *NewAnd = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5947 | BinaryOperator::CreateAnd(Cast->getOperand(0), |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5948 | ConstantInt::get(NewCST),LHSI->getName()); |
| 5949 | InsertNewInstBefore(NewAnd, ICI); |
| 5950 | return new ICmpInst(ICI.getPredicate(), NewAnd, |
| 5951 | ConstantInt::get(NewCI)); |
| 5952 | } |
| 5953 | } |
| 5954 | |
| 5955 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare |
| 5956 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This |
| 5957 | // happens a LOT in code produced by the C front-end, for bitfield |
| 5958 | // access. |
| 5959 | BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0)); |
| 5960 | if (Shift && !Shift->isShift()) |
| 5961 | Shift = 0; |
| 5962 | |
| 5963 | ConstantInt *ShAmt; |
| 5964 | ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : 0; |
| 5965 | const Type *Ty = Shift ? Shift->getType() : 0; // Type of the shift. |
| 5966 | const Type *AndTy = AndCST->getType(); // Type of the and. |
| 5967 | |
| 5968 | // We can fold this as long as we can't shift unknown bits |
| 5969 | // into the mask. This can only happen with signed shift |
| 5970 | // rights, as they sign-extend. |
| 5971 | if (ShAmt) { |
| 5972 | bool CanFold = Shift->isLogicalShift(); |
| 5973 | if (!CanFold) { |
| 5974 | // To test for the bad case of the signed shr, see if any |
| 5975 | // of the bits shifted in could be tested after the mask. |
| 5976 | uint32_t TyBits = Ty->getPrimitiveSizeInBits(); |
| 5977 | int ShAmtVal = TyBits - ShAmt->getLimitedValue(TyBits); |
| 5978 | |
| 5979 | uint32_t BitWidth = AndTy->getPrimitiveSizeInBits(); |
| 5980 | if ((APInt::getHighBitsSet(BitWidth, BitWidth-ShAmtVal) & |
| 5981 | AndCST->getValue()) == 0) |
| 5982 | CanFold = true; |
| 5983 | } |
| 5984 | |
| 5985 | if (CanFold) { |
| 5986 | Constant *NewCst; |
| 5987 | if (Shift->getOpcode() == Instruction::Shl) |
| 5988 | NewCst = ConstantExpr::getLShr(RHS, ShAmt); |
| 5989 | else |
| 5990 | NewCst = ConstantExpr::getShl(RHS, ShAmt); |
| 5991 | |
| 5992 | // Check to see if we are shifting out any of the bits being |
| 5993 | // compared. |
| 5994 | if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != RHS) { |
| 5995 | // If we shifted bits out, the fold is not going to work out. |
| 5996 | // As a special case, check to see if this means that the |
| 5997 | // result is always true or false now. |
| 5998 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
| 5999 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
| 6000 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
| 6001 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 6002 | } else { |
| 6003 | ICI.setOperand(1, NewCst); |
| 6004 | Constant *NewAndCST; |
| 6005 | if (Shift->getOpcode() == Instruction::Shl) |
| 6006 | NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt); |
| 6007 | else |
| 6008 | NewAndCST = ConstantExpr::getShl(AndCST, ShAmt); |
| 6009 | LHSI->setOperand(1, NewAndCST); |
| 6010 | LHSI->setOperand(0, Shift->getOperand(0)); |
| 6011 | AddToWorkList(Shift); // Shift is dead. |
| 6012 | AddUsesToWorkList(ICI); |
| 6013 | return &ICI; |
| 6014 | } |
| 6015 | } |
| 6016 | } |
| 6017 | |
| 6018 | // Turn ((X >> Y) & C) == 0 into (X & (C << Y)) == 0. The later is |
| 6019 | // preferable because it allows the C<<Y expression to be hoisted out |
| 6020 | // of a loop if Y is invariant and X is not. |
| 6021 | if (Shift && Shift->hasOneUse() && RHSV == 0 && |
| 6022 | ICI.isEquality() && !Shift->isArithmeticShift() && |
| 6023 | isa<Instruction>(Shift->getOperand(0))) { |
| 6024 | // Compute C << Y. |
| 6025 | Value *NS; |
| 6026 | if (Shift->getOpcode() == Instruction::LShr) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6027 | NS = BinaryOperator::CreateShl(AndCST, |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6028 | Shift->getOperand(1), "tmp"); |
| 6029 | } else { |
| 6030 | // Insert a logical shift. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6031 | NS = BinaryOperator::CreateLShr(AndCST, |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6032 | Shift->getOperand(1), "tmp"); |
| 6033 | } |
| 6034 | InsertNewInstBefore(cast<Instruction>(NS), ICI); |
| 6035 | |
| 6036 | // Compute X & (C << Y). |
| 6037 | Instruction *NewAnd = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6038 | BinaryOperator::CreateAnd(Shift->getOperand(0), NS, LHSI->getName()); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6039 | InsertNewInstBefore(NewAnd, ICI); |
| 6040 | |
| 6041 | ICI.setOperand(0, NewAnd); |
| 6042 | return &ICI; |
| 6043 | } |
| 6044 | } |
| 6045 | break; |
| 6046 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6047 | case Instruction::Shl: { // (icmp pred (shl X, ShAmt), CI) |
| 6048 | ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
| 6049 | if (!ShAmt) break; |
| 6050 | |
| 6051 | uint32_t TypeBits = RHSV.getBitWidth(); |
| 6052 | |
| 6053 | // Check that the shift amount is in range. If not, don't perform |
| 6054 | // undefined shifts. When the shift is visited it will be |
| 6055 | // simplified. |
| 6056 | if (ShAmt->uge(TypeBits)) |
| 6057 | break; |
| 6058 | |
| 6059 | if (ICI.isEquality()) { |
| 6060 | // If we are comparing against bits always shifted out, the |
| 6061 | // comparison cannot succeed. |
| 6062 | Constant *Comp = |
| 6063 | ConstantExpr::getShl(ConstantExpr::getLShr(RHS, ShAmt), ShAmt); |
| 6064 | if (Comp != RHS) {// Comparing against a bit that we know is zero. |
| 6065 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 6066 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); |
| 6067 | return ReplaceInstUsesWith(ICI, Cst); |
| 6068 | } |
| 6069 | |
| 6070 | if (LHSI->hasOneUse()) { |
| 6071 | // Otherwise strength reduce the shift into an and. |
| 6072 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); |
| 6073 | Constant *Mask = |
| 6074 | ConstantInt::get(APInt::getLowBitsSet(TypeBits, TypeBits-ShAmtVal)); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6075 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6076 | Instruction *AndI = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6077 | BinaryOperator::CreateAnd(LHSI->getOperand(0), |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6078 | Mask, LHSI->getName()+".mask"); |
| 6079 | Value *And = InsertNewInstBefore(AndI, ICI); |
| 6080 | return new ICmpInst(ICI.getPredicate(), And, |
| 6081 | ConstantInt::get(RHSV.lshr(ShAmtVal))); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6082 | } |
| 6083 | } |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6084 | |
| 6085 | // Otherwise, if this is a comparison of the sign bit, simplify to and/test. |
| 6086 | bool TrueIfSigned = false; |
| 6087 | if (LHSI->hasOneUse() && |
| 6088 | isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) { |
| 6089 | // (X << 31) <s 0 --> (X&1) != 0 |
| 6090 | Constant *Mask = ConstantInt::get(APInt(TypeBits, 1) << |
| 6091 | (TypeBits-ShAmt->getZExtValue()-1)); |
| 6092 | Instruction *AndI = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6093 | BinaryOperator::CreateAnd(LHSI->getOperand(0), |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6094 | Mask, LHSI->getName()+".mask"); |
| 6095 | Value *And = InsertNewInstBefore(AndI, ICI); |
| 6096 | |
| 6097 | return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ, |
| 6098 | And, Constant::getNullValue(And->getType())); |
| 6099 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6100 | break; |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6101 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6102 | |
| 6103 | case Instruction::LShr: // (icmp pred (shr X, ShAmt), CI) |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6104 | case Instruction::AShr: { |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6105 | // Only handle equality comparisons of shift-by-constant. |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6106 | ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6107 | if (!ShAmt || !ICI.isEquality()) break; |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6108 | |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6109 | // Check that the shift amount is in range. If not, don't perform |
| 6110 | // undefined shifts. When the shift is visited it will be |
| 6111 | // simplified. |
| 6112 | uint32_t TypeBits = RHSV.getBitWidth(); |
| 6113 | if (ShAmt->uge(TypeBits)) |
| 6114 | break; |
| 6115 | |
| 6116 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6117 | |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6118 | // If we are comparing against bits always shifted out, the |
| 6119 | // comparison cannot succeed. |
| 6120 | APInt Comp = RHSV << ShAmtVal; |
| 6121 | if (LHSI->getOpcode() == Instruction::LShr) |
| 6122 | Comp = Comp.lshr(ShAmtVal); |
| 6123 | else |
| 6124 | Comp = Comp.ashr(ShAmtVal); |
| 6125 | |
| 6126 | if (Comp != RHSV) { // Comparing against a bit that we know is zero. |
| 6127 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 6128 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); |
| 6129 | return ReplaceInstUsesWith(ICI, Cst); |
| 6130 | } |
| 6131 | |
| 6132 | // Otherwise, check to see if the bits shifted out are known to be zero. |
| 6133 | // If so, we can compare against the unshifted value: |
| 6134 | // (X & 4) >> 1 == 2 --> (X & 4) == 4. |
Evan Cheng | f30752c | 2008-04-23 00:38:06 +0000 | [diff] [blame] | 6135 | if (LHSI->hasOneUse() && |
| 6136 | MaskedValueIsZero(LHSI->getOperand(0), |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6137 | APInt::getLowBitsSet(Comp.getBitWidth(), ShAmtVal))) { |
| 6138 | return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0), |
| 6139 | ConstantExpr::getShl(RHS, ShAmt)); |
| 6140 | } |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6141 | |
Evan Cheng | f30752c | 2008-04-23 00:38:06 +0000 | [diff] [blame] | 6142 | if (LHSI->hasOneUse()) { |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6143 | // Otherwise strength reduce the shift into an and. |
| 6144 | APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal)); |
| 6145 | Constant *Mask = ConstantInt::get(Val); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6146 | |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6147 | Instruction *AndI = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6148 | BinaryOperator::CreateAnd(LHSI->getOperand(0), |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6149 | Mask, LHSI->getName()+".mask"); |
| 6150 | Value *And = InsertNewInstBefore(AndI, ICI); |
| 6151 | return new ICmpInst(ICI.getPredicate(), And, |
| 6152 | ConstantExpr::getShl(RHS, ShAmt)); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6153 | } |
| 6154 | break; |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6155 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6156 | |
| 6157 | case Instruction::SDiv: |
| 6158 | case Instruction::UDiv: |
| 6159 | // Fold: icmp pred ([us]div X, C1), C2 -> range test |
| 6160 | // Fold this div into the comparison, producing a range check. |
| 6161 | // Determine, based on the divide type, what the range is being |
| 6162 | // checked. If there is an overflow on the low or high side, remember |
| 6163 | // it, otherwise compute the range [low, hi) bounding the new value. |
| 6164 | // See: InsertRangeTest above for the kinds of replacements possible. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6165 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) |
| 6166 | if (Instruction *R = FoldICmpDivCst(ICI, cast<BinaryOperator>(LHSI), |
| 6167 | DivRHS)) |
| 6168 | return R; |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6169 | break; |
Nick Lewycky | 5be2920 | 2008-02-03 16:33:09 +0000 | [diff] [blame] | 6170 | |
| 6171 | case Instruction::Add: |
| 6172 | // Fold: icmp pred (add, X, C1), C2 |
| 6173 | |
| 6174 | if (!ICI.isEquality()) { |
| 6175 | ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
| 6176 | if (!LHSC) break; |
| 6177 | const APInt &LHSV = LHSC->getValue(); |
| 6178 | |
| 6179 | ConstantRange CR = ICI.makeConstantRange(ICI.getPredicate(), RHSV) |
| 6180 | .subtract(LHSV); |
| 6181 | |
| 6182 | if (ICI.isSignedPredicate()) { |
| 6183 | if (CR.getLower().isSignBit()) { |
| 6184 | return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0), |
| 6185 | ConstantInt::get(CR.getUpper())); |
| 6186 | } else if (CR.getUpper().isSignBit()) { |
| 6187 | return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0), |
| 6188 | ConstantInt::get(CR.getLower())); |
| 6189 | } |
| 6190 | } else { |
| 6191 | if (CR.getLower().isMinValue()) { |
| 6192 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0), |
| 6193 | ConstantInt::get(CR.getUpper())); |
| 6194 | } else if (CR.getUpper().isMinValue()) { |
| 6195 | return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0), |
| 6196 | ConstantInt::get(CR.getLower())); |
| 6197 | } |
| 6198 | } |
| 6199 | } |
| 6200 | break; |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6201 | } |
| 6202 | |
| 6203 | // Simplify icmp_eq and icmp_ne instructions with integer constant RHS. |
| 6204 | if (ICI.isEquality()) { |
| 6205 | bool isICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 6206 | |
| 6207 | // If the first operand is (add|sub|and|or|xor|rem) with a constant, and |
| 6208 | // the second operand is a constant, simplify a bit. |
| 6209 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(LHSI)) { |
| 6210 | switch (BO->getOpcode()) { |
| 6211 | case Instruction::SRem: |
| 6212 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. |
| 6213 | if (RHSV == 0 && isa<ConstantInt>(BO->getOperand(1)) &&BO->hasOneUse()){ |
| 6214 | const APInt &V = cast<ConstantInt>(BO->getOperand(1))->getValue(); |
| 6215 | if (V.sgt(APInt(V.getBitWidth(), 1)) && V.isPowerOf2()) { |
| 6216 | Instruction *NewRem = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6217 | BinaryOperator::CreateURem(BO->getOperand(0), BO->getOperand(1), |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6218 | BO->getName()); |
| 6219 | InsertNewInstBefore(NewRem, ICI); |
| 6220 | return new ICmpInst(ICI.getPredicate(), NewRem, |
| 6221 | Constant::getNullValue(BO->getType())); |
| 6222 | } |
| 6223 | } |
| 6224 | break; |
| 6225 | case Instruction::Add: |
| 6226 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. |
| 6227 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 6228 | if (BO->hasOneUse()) |
| 6229 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 6230 | Subtract(RHS, BOp1C)); |
| 6231 | } else if (RHSV == 0) { |
| 6232 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 6233 | // efficiently invertible, or if the add has just this one use. |
| 6234 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
| 6235 | |
| 6236 | if (Value *NegVal = dyn_castNegVal(BOp1)) |
| 6237 | return new ICmpInst(ICI.getPredicate(), BOp0, NegVal); |
| 6238 | else if (Value *NegVal = dyn_castNegVal(BOp0)) |
| 6239 | return new ICmpInst(ICI.getPredicate(), NegVal, BOp1); |
| 6240 | else if (BO->hasOneUse()) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6241 | Instruction *Neg = BinaryOperator::CreateNeg(BOp1); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6242 | InsertNewInstBefore(Neg, ICI); |
| 6243 | Neg->takeName(BO); |
| 6244 | return new ICmpInst(ICI.getPredicate(), BOp0, Neg); |
| 6245 | } |
| 6246 | } |
| 6247 | break; |
| 6248 | case Instruction::Xor: |
| 6249 | // For the xor case, we can xor two constants together, eliminating |
| 6250 | // the explicit xor. |
| 6251 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) |
| 6252 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 6253 | ConstantExpr::getXor(RHS, BOC)); |
| 6254 | |
| 6255 | // FALLTHROUGH |
| 6256 | case Instruction::Sub: |
| 6257 | // Replace (([sub|xor] A, B) != 0) with (A != B) |
| 6258 | if (RHSV == 0) |
| 6259 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 6260 | BO->getOperand(1)); |
| 6261 | break; |
| 6262 | |
| 6263 | case Instruction::Or: |
| 6264 | // If bits are being or'd in that are not present in the constant we |
| 6265 | // are comparing against, then the comparison could never succeed! |
| 6266 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { |
| 6267 | Constant *NotCI = ConstantExpr::getNot(RHS); |
| 6268 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) |
| 6269 | return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty, |
| 6270 | isICMP_NE)); |
| 6271 | } |
| 6272 | break; |
| 6273 | |
| 6274 | case Instruction::And: |
| 6275 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 6276 | // If bits are being compared against that are and'd out, then the |
| 6277 | // comparison can never succeed! |
| 6278 | if ((RHSV & ~BOC->getValue()) != 0) |
| 6279 | return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty, |
| 6280 | isICMP_NE)); |
| 6281 | |
| 6282 | // If we have ((X & C) == C), turn it into ((X & C) != 0). |
| 6283 | if (RHS == BOC && RHSV.isPowerOf2()) |
| 6284 | return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ : |
| 6285 | ICmpInst::ICMP_NE, LHSI, |
| 6286 | Constant::getNullValue(RHS->getType())); |
| 6287 | |
| 6288 | // Replace (and X, (1 << size(X)-1) != 0) with x s< 0 |
| 6289 | if (isSignBit(BOC)) { |
| 6290 | Value *X = BO->getOperand(0); |
| 6291 | Constant *Zero = Constant::getNullValue(X->getType()); |
| 6292 | ICmpInst::Predicate pred = isICMP_NE ? |
| 6293 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE; |
| 6294 | return new ICmpInst(pred, X, Zero); |
| 6295 | } |
| 6296 | |
| 6297 | // ((X & ~7) == 0) --> X < 8 |
| 6298 | if (RHSV == 0 && isHighOnes(BOC)) { |
| 6299 | Value *X = BO->getOperand(0); |
| 6300 | Constant *NegX = ConstantExpr::getNeg(BOC); |
| 6301 | ICmpInst::Predicate pred = isICMP_NE ? |
| 6302 | ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT; |
| 6303 | return new ICmpInst(pred, X, NegX); |
| 6304 | } |
| 6305 | } |
| 6306 | default: break; |
| 6307 | } |
| 6308 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI)) { |
| 6309 | // Handle icmp {eq|ne} <intrinsic>, intcst. |
| 6310 | if (II->getIntrinsicID() == Intrinsic::bswap) { |
| 6311 | AddToWorkList(II); |
| 6312 | ICI.setOperand(0, II->getOperand(1)); |
| 6313 | ICI.setOperand(1, ConstantInt::get(RHSV.byteSwap())); |
| 6314 | return &ICI; |
| 6315 | } |
| 6316 | } |
| 6317 | } else { // Not a ICMP_EQ/ICMP_NE |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 6318 | // If the LHS is a cast from an integral value of the same size, |
| 6319 | // then since we know the RHS is a constant, try to simlify. |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6320 | if (CastInst *Cast = dyn_cast<CastInst>(LHSI)) { |
| 6321 | Value *CastOp = Cast->getOperand(0); |
| 6322 | const Type *SrcTy = CastOp->getType(); |
| 6323 | uint32_t SrcTySize = SrcTy->getPrimitiveSizeInBits(); |
| 6324 | if (SrcTy->isInteger() && |
| 6325 | SrcTySize == Cast->getType()->getPrimitiveSizeInBits()) { |
| 6326 | // If this is an unsigned comparison, try to make the comparison use |
| 6327 | // smaller constant values. |
| 6328 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT && RHSV.isSignBit()) { |
| 6329 | // X u< 128 => X s> -1 |
| 6330 | return new ICmpInst(ICmpInst::ICMP_SGT, CastOp, |
| 6331 | ConstantInt::get(APInt::getAllOnesValue(SrcTySize))); |
| 6332 | } else if (ICI.getPredicate() == ICmpInst::ICMP_UGT && |
| 6333 | RHSV == APInt::getSignedMaxValue(SrcTySize)) { |
| 6334 | // X u> 127 => X s< 0 |
| 6335 | return new ICmpInst(ICmpInst::ICMP_SLT, CastOp, |
| 6336 | Constant::getNullValue(SrcTy)); |
| 6337 | } |
| 6338 | } |
| 6339 | } |
| 6340 | } |
| 6341 | return 0; |
| 6342 | } |
| 6343 | |
| 6344 | /// visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst). |
| 6345 | /// We only handle extending casts so far. |
| 6346 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6347 | Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) { |
| 6348 | const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6349 | Value *LHSCIOp = LHSCI->getOperand(0); |
| 6350 | const Type *SrcTy = LHSCIOp->getType(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6351 | const Type *DestTy = LHSCI->getType(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6352 | Value *RHSCIOp; |
| 6353 | |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 6354 | // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the |
| 6355 | // integer type is the same size as the pointer type. |
| 6356 | if (LHSCI->getOpcode() == Instruction::PtrToInt && |
| 6357 | getTargetData().getPointerSizeInBits() == |
| 6358 | cast<IntegerType>(DestTy)->getBitWidth()) { |
| 6359 | Value *RHSOp = 0; |
| 6360 | if (Constant *RHSC = dyn_cast<Constant>(ICI.getOperand(1))) { |
Chris Lattner | 6f6f512 | 2007-05-06 07:24:03 +0000 | [diff] [blame] | 6361 | RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy); |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 6362 | } else if (PtrToIntInst *RHSC = dyn_cast<PtrToIntInst>(ICI.getOperand(1))) { |
| 6363 | RHSOp = RHSC->getOperand(0); |
| 6364 | // If the pointer types don't match, insert a bitcast. |
| 6365 | if (LHSCIOp->getType() != RHSOp->getType()) |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 6366 | RHSOp = InsertBitCastBefore(RHSOp, LHSCIOp->getType(), ICI); |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 6367 | } |
| 6368 | |
| 6369 | if (RHSOp) |
| 6370 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSOp); |
| 6371 | } |
| 6372 | |
| 6373 | // The code below only handles extension cast instructions, so far. |
| 6374 | // Enforce this. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6375 | if (LHSCI->getOpcode() != Instruction::ZExt && |
| 6376 | LHSCI->getOpcode() != Instruction::SExt) |
Chris Lattner | b352fa5 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 6377 | return 0; |
| 6378 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6379 | bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt; |
| 6380 | bool isSignedCmp = ICI.isSignedPredicate(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6381 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6382 | if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6383 | // Not an extension from the same type? |
| 6384 | RHSCIOp = CI->getOperand(0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6385 | if (RHSCIOp->getType() != LHSCIOp->getType()) |
| 6386 | return 0; |
Chris Lattner | a5c5e77 | 2007-01-13 23:11:38 +0000 | [diff] [blame] | 6387 | |
Nick Lewycky | 4189a53 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 6388 | // If the signedness of the two casts doesn't agree (i.e. one is a sext |
Chris Lattner | a5c5e77 | 2007-01-13 23:11:38 +0000 | [diff] [blame] | 6389 | // and the other is a zext), then we can't handle this. |
| 6390 | if (CI->getOpcode() != LHSCI->getOpcode()) |
| 6391 | return 0; |
| 6392 | |
Nick Lewycky | 4189a53 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 6393 | // Deal with equality cases early. |
| 6394 | if (ICI.isEquality()) |
| 6395 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); |
| 6396 | |
| 6397 | // A signed comparison of sign extended values simplifies into a |
| 6398 | // signed comparison. |
| 6399 | if (isSignedCmp && isSignedExt) |
| 6400 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); |
| 6401 | |
| 6402 | // The other three cases all fold into an unsigned comparison. |
| 6403 | return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp); |
Reid Spencer | 6731d5c | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 6404 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6405 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6406 | // If we aren't dealing with a constant on the RHS, exit early |
| 6407 | ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1)); |
| 6408 | if (!CI) |
| 6409 | return 0; |
| 6410 | |
| 6411 | // Compute the constant that would happen if we truncated to SrcTy then |
| 6412 | // reextended to DestTy. |
| 6413 | Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy); |
| 6414 | Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(), Res1, DestTy); |
| 6415 | |
| 6416 | // If the re-extended constant didn't change... |
| 6417 | if (Res2 == CI) { |
| 6418 | // Make sure that sign of the Cmp and the sign of the Cast are the same. |
| 6419 | // For example, we might have: |
| 6420 | // %A = sext short %X to uint |
| 6421 | // %B = icmp ugt uint %A, 1330 |
| 6422 | // It is incorrect to transform this into |
| 6423 | // %B = icmp ugt short %X, 1330 |
| 6424 | // because %A may have negative value. |
| 6425 | // |
| 6426 | // However, it is OK if SrcTy is bool (See cast-set.ll testcase) |
| 6427 | // OR operation is EQ/NE. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 6428 | if (isSignedExt == isSignedCmp || SrcTy == Type::Int1Ty || ICI.isEquality()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6429 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1); |
| 6430 | else |
| 6431 | return 0; |
| 6432 | } |
| 6433 | |
| 6434 | // The re-extended constant changed so the constant cannot be represented |
| 6435 | // in the shorter type. Consequently, we cannot emit a simple comparison. |
| 6436 | |
| 6437 | // First, handle some easy cases. We know the result cannot be equal at this |
| 6438 | // point so handle the ICI.isEquality() cases |
| 6439 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6440 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6441 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6442 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6443 | |
| 6444 | // Evaluate the comparison for LT (we invert for GT below). LE and GE cases |
| 6445 | // should have been folded away previously and not enter in here. |
| 6446 | Value *Result; |
| 6447 | if (isSignedCmp) { |
| 6448 | // We're performing a signed comparison. |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 6449 | if (cast<ConstantInt>(CI)->getValue().isNegative()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6450 | Result = ConstantInt::getFalse(); // X < (small) --> false |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6451 | else |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6452 | Result = ConstantInt::getTrue(); // X < (large) --> true |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6453 | } else { |
| 6454 | // We're performing an unsigned comparison. |
| 6455 | if (isSignedExt) { |
| 6456 | // We're performing an unsigned comp with a sign extended value. |
| 6457 | // This is true if the input is >= 0. [aka >s -1] |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6458 | Constant *NegOne = ConstantInt::getAllOnesValue(SrcTy); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6459 | Result = InsertNewInstBefore(new ICmpInst(ICmpInst::ICMP_SGT, LHSCIOp, |
| 6460 | NegOne, ICI.getName()), ICI); |
| 6461 | } else { |
| 6462 | // Unsigned extend & unsigned compare -> always true. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6463 | Result = ConstantInt::getTrue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6464 | } |
| 6465 | } |
| 6466 | |
| 6467 | // Finally, return the value computed. |
| 6468 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT || |
| 6469 | ICI.getPredicate() == ICmpInst::ICMP_SLT) { |
| 6470 | return ReplaceInstUsesWith(ICI, Result); |
| 6471 | } else { |
| 6472 | assert((ICI.getPredicate()==ICmpInst::ICMP_UGT || |
| 6473 | ICI.getPredicate()==ICmpInst::ICMP_SGT) && |
| 6474 | "ICmp should be folded!"); |
| 6475 | if (Constant *CI = dyn_cast<Constant>(Result)) |
| 6476 | return ReplaceInstUsesWith(ICI, ConstantExpr::getNot(CI)); |
| 6477 | else |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6478 | return BinaryOperator::CreateNot(Result); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6479 | } |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6480 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6481 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6482 | Instruction *InstCombiner::visitShl(BinaryOperator &I) { |
| 6483 | return commonShiftTransforms(I); |
| 6484 | } |
| 6485 | |
| 6486 | Instruction *InstCombiner::visitLShr(BinaryOperator &I) { |
| 6487 | return commonShiftTransforms(I); |
| 6488 | } |
| 6489 | |
| 6490 | Instruction *InstCombiner::visitAShr(BinaryOperator &I) { |
Chris Lattner | 348f665 | 2007-12-06 01:59:46 +0000 | [diff] [blame] | 6491 | if (Instruction *R = commonShiftTransforms(I)) |
| 6492 | return R; |
| 6493 | |
| 6494 | Value *Op0 = I.getOperand(0); |
| 6495 | |
| 6496 | // ashr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 6497 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
| 6498 | if (CSI->isAllOnesValue()) |
| 6499 | return ReplaceInstUsesWith(I, CSI); |
| 6500 | |
| 6501 | // See if we can turn a signed shr into an unsigned shr. |
| 6502 | if (MaskedValueIsZero(Op0, |
| 6503 | APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()))) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6504 | return BinaryOperator::CreateLShr(Op0, I.getOperand(1)); |
Chris Lattner | 348f665 | 2007-12-06 01:59:46 +0000 | [diff] [blame] | 6505 | |
| 6506 | return 0; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6507 | } |
| 6508 | |
| 6509 | Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) { |
| 6510 | assert(I.getOperand(1)->getType() == I.getOperand(0)->getType()); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 6511 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6512 | |
| 6513 | // shl X, 0 == X and shr X, 0 == X |
| 6514 | // shl 0, X == 0 and shr 0, X == 0 |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6515 | if (Op1 == Constant::getNullValue(Op1->getType()) || |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 6516 | Op0 == Constant::getNullValue(Op0->getType())) |
| 6517 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 6518 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6519 | if (isa<UndefValue>(Op0)) { |
| 6520 | if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef |
Chris Lattner | 79a564c | 2004-10-16 23:28:04 +0000 | [diff] [blame] | 6521 | return ReplaceInstUsesWith(I, Op0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6522 | else // undef << X -> 0, undef >>u X -> 0 |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6523 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 6524 | } |
| 6525 | if (isa<UndefValue>(Op1)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6526 | if (I.getOpcode() == Instruction::AShr) // X >>s undef -> X |
| 6527 | return ReplaceInstUsesWith(I, Op0); |
| 6528 | else // X << undef, X >>u undef -> 0 |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6529 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6530 | } |
| 6531 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6532 | // Try to fold constant and into select arguments. |
| 6533 | if (isa<Constant>(Op0)) |
| 6534 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 6535 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6536 | return R; |
| 6537 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6538 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1)) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6539 | if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I)) |
| 6540 | return Res; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6541 | return 0; |
| 6542 | } |
| 6543 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6544 | Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6545 | BinaryOperator &I) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6546 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6547 | |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 6548 | // See if we can simplify any instructions used by the instruction whose sole |
| 6549 | // purpose is to compute bits we don't care about. |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6550 | uint32_t TypeBits = Op0->getType()->getPrimitiveSizeInBits(); |
| 6551 | APInt KnownZero(TypeBits, 0), KnownOne(TypeBits, 0); |
| 6552 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(TypeBits), |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 6553 | KnownZero, KnownOne)) |
| 6554 | return &I; |
| 6555 | |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6556 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr |
| 6557 | // of a signed value. |
| 6558 | // |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 6559 | if (Op1->uge(TypeBits)) { |
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 6560 | if (I.getOpcode() != Instruction::AShr) |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6561 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
| 6562 | else { |
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 6563 | I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1)); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6564 | return &I; |
Chris Lattner | 8adac75 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 6565 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6566 | } |
| 6567 | |
| 6568 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 6569 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 6570 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 6571 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6572 | return BinaryOperator::CreateMul(BO->getOperand(0), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6573 | ConstantExpr::getShl(BOOp, Op1)); |
| 6574 | |
| 6575 | // Try to fold constant and into select arguments. |
| 6576 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 6577 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 6578 | return R; |
| 6579 | if (isa<PHINode>(Op0)) |
| 6580 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 6581 | return NV; |
| 6582 | |
Chris Lattner | 8999dd3 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 6583 | // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2)) |
| 6584 | if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) { |
| 6585 | Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0)); |
| 6586 | // If 'shift2' is an ashr, we would have to get the sign bit into a funny |
| 6587 | // place. Don't try to do this transformation in this case. Also, we |
| 6588 | // require that the input operand is a shift-by-constant so that we have |
| 6589 | // confidence that the shifts will get folded together. We could do this |
| 6590 | // xform in more cases, but it is unlikely to be profitable. |
| 6591 | if (TrOp && I.isLogicalShift() && TrOp->isShift() && |
| 6592 | isa<ConstantInt>(TrOp->getOperand(1))) { |
| 6593 | // Okay, we'll do this xform. Make the shift of shift. |
| 6594 | Constant *ShAmt = ConstantExpr::getZExt(Op1, TrOp->getType()); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6595 | Instruction *NSh = BinaryOperator::Create(I.getOpcode(), TrOp, ShAmt, |
Chris Lattner | 8999dd3 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 6596 | I.getName()); |
| 6597 | InsertNewInstBefore(NSh, I); // (shift2 (shift1 & 0x00FF), c2) |
| 6598 | |
| 6599 | // For logical shifts, the truncation has the effect of making the high |
| 6600 | // part of the register be zeros. Emulate this by inserting an AND to |
| 6601 | // clear the top bits as needed. This 'and' will usually be zapped by |
| 6602 | // other xforms later if dead. |
| 6603 | unsigned SrcSize = TrOp->getType()->getPrimitiveSizeInBits(); |
| 6604 | unsigned DstSize = TI->getType()->getPrimitiveSizeInBits(); |
| 6605 | APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize)); |
| 6606 | |
| 6607 | // The mask we constructed says what the trunc would do if occurring |
| 6608 | // between the shifts. We want to know the effect *after* the second |
| 6609 | // shift. We know that it is a logical shift by a constant, so adjust the |
| 6610 | // mask as appropriate. |
| 6611 | if (I.getOpcode() == Instruction::Shl) |
| 6612 | MaskV <<= Op1->getZExtValue(); |
| 6613 | else { |
| 6614 | assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift"); |
| 6615 | MaskV = MaskV.lshr(Op1->getZExtValue()); |
| 6616 | } |
| 6617 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6618 | Instruction *And = BinaryOperator::CreateAnd(NSh, ConstantInt::get(MaskV), |
Chris Lattner | 8999dd3 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 6619 | TI->getName()); |
| 6620 | InsertNewInstBefore(And, I); // shift1 & 0x00FF |
| 6621 | |
| 6622 | // Return the value truncated to the interesting size. |
| 6623 | return new TruncInst(And, I.getType()); |
| 6624 | } |
| 6625 | } |
| 6626 | |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6627 | if (Op0->hasOneUse()) { |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6628 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) { |
| 6629 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
| 6630 | Value *V1, *V2; |
| 6631 | ConstantInt *CC; |
| 6632 | switch (Op0BO->getOpcode()) { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6633 | default: break; |
| 6634 | case Instruction::Add: |
| 6635 | case Instruction::And: |
| 6636 | case Instruction::Or: |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6637 | case Instruction::Xor: { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6638 | // These operators commute. |
| 6639 | // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6640 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
| 6641 | match(Op0BO->getOperand(1), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6642 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6643 | Instruction *YS = BinaryOperator::CreateShl( |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6644 | Op0BO->getOperand(0), Op1, |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6645 | Op0BO->getName()); |
| 6646 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6647 | Instruction *X = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6648 | BinaryOperator::Create(Op0BO->getOpcode(), YS, V1, |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6649 | Op0BO->getOperand(1)->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6650 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6651 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6652 | return BinaryOperator::CreateAnd(X, ConstantInt::get( |
Zhou Sheng | 90b9681 | 2007-03-30 05:45:18 +0000 | [diff] [blame] | 6653 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6654 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6655 | |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6656 | // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C)) |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6657 | Value *Op0BOOp1 = Op0BO->getOperand(1); |
Chris Lattner | 3c69849 | 2007-03-05 00:11:19 +0000 | [diff] [blame] | 6658 | if (isLeftShift && Op0BOOp1->hasOneUse() && |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6659 | match(Op0BOOp1, |
| 6660 | m_And(m_Shr(m_Value(V1), m_Value(V2)),m_ConstantInt(CC))) && |
Chris Lattner | 3c69849 | 2007-03-05 00:11:19 +0000 | [diff] [blame] | 6661 | cast<BinaryOperator>(Op0BOOp1)->getOperand(0)->hasOneUse() && |
| 6662 | V2 == Op1) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6663 | Instruction *YS = BinaryOperator::CreateShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6664 | Op0BO->getOperand(0), Op1, |
| 6665 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6666 | InsertNewInstBefore(YS, I); // (Y << C) |
| 6667 | Instruction *XM = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6668 | BinaryOperator::CreateAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6669 | V1->getName()+".mask"); |
| 6670 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 6671 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6672 | return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6673 | } |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6674 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6675 | |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6676 | // FALL THROUGH. |
| 6677 | case Instruction::Sub: { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6678 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6679 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 6680 | match(Op0BO->getOperand(0), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6681 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6682 | Instruction *YS = BinaryOperator::CreateShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6683 | Op0BO->getOperand(1), Op1, |
| 6684 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6685 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6686 | Instruction *X = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6687 | BinaryOperator::Create(Op0BO->getOpcode(), V1, YS, |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6688 | Op0BO->getOperand(0)->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6689 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6690 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6691 | return BinaryOperator::CreateAnd(X, ConstantInt::get( |
Zhou Sheng | 90b9681 | 2007-03-30 05:45:18 +0000 | [diff] [blame] | 6692 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6693 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6694 | |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 6695 | // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6696 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 6697 | match(Op0BO->getOperand(0), |
| 6698 | m_And(m_Shr(m_Value(V1), m_Value(V2)), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6699 | m_ConstantInt(CC))) && V2 == Op1 && |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6700 | cast<BinaryOperator>(Op0BO->getOperand(0)) |
| 6701 | ->getOperand(0)->hasOneUse()) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6702 | Instruction *YS = BinaryOperator::CreateShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6703 | Op0BO->getOperand(1), Op1, |
| 6704 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6705 | InsertNewInstBefore(YS, I); // (Y << C) |
| 6706 | Instruction *XM = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6707 | BinaryOperator::CreateAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6708 | V1->getName()+".mask"); |
| 6709 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 6710 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6711 | return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6712 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6713 | |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6714 | break; |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6715 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6716 | } |
| 6717 | |
| 6718 | |
| 6719 | // If the operand is an bitwise operator with a constant RHS, and the |
| 6720 | // shift is the only use, we can pull it out of the shift. |
| 6721 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 6722 | bool isValid = true; // Valid only for And, Or, Xor |
| 6723 | bool highBitSet = false; // Transform if high bit of constant set? |
| 6724 | |
| 6725 | switch (Op0BO->getOpcode()) { |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 6726 | default: isValid = false; break; // Do not perform transform! |
Chris Lattner | 1f7e160 | 2004-10-08 03:46:20 +0000 | [diff] [blame] | 6727 | case Instruction::Add: |
| 6728 | isValid = isLeftShift; |
| 6729 | break; |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 6730 | case Instruction::Or: |
| 6731 | case Instruction::Xor: |
| 6732 | highBitSet = false; |
| 6733 | break; |
| 6734 | case Instruction::And: |
| 6735 | highBitSet = true; |
| 6736 | break; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6737 | } |
| 6738 | |
| 6739 | // If this is a signed shift right, and the high bit is modified |
| 6740 | // by the logical operation, do not perform the transformation. |
| 6741 | // The highBitSet boolean indicates the value of the high bit of |
| 6742 | // the constant which would cause it to be modified for this |
| 6743 | // operation. |
| 6744 | // |
Chris Lattner | c95ba44 | 2007-12-06 06:25:04 +0000 | [diff] [blame] | 6745 | if (isValid && I.getOpcode() == Instruction::AShr) |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6746 | isValid = Op0C->getValue()[TypeBits-1] == highBitSet; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6747 | |
| 6748 | if (isValid) { |
| 6749 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1); |
| 6750 | |
| 6751 | Instruction *NewShift = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6752 | BinaryOperator::Create(I.getOpcode(), Op0BO->getOperand(0), Op1); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6753 | InsertNewInstBefore(NewShift, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6754 | NewShift->takeName(Op0BO); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6755 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6756 | return BinaryOperator::Create(Op0BO->getOpcode(), NewShift, |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6757 | NewRHS); |
| 6758 | } |
| 6759 | } |
| 6760 | } |
| 6761 | } |
| 6762 | |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6763 | // Find out if this is a shift of a shift by a constant. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6764 | BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0); |
| 6765 | if (ShiftOp && !ShiftOp->isShift()) |
| 6766 | ShiftOp = 0; |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6767 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6768 | if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6769 | ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1)); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 6770 | uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits); |
| 6771 | uint32_t ShiftAmt2 = Op1->getLimitedValue(TypeBits); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6772 | assert(ShiftAmt2 != 0 && "Should have been simplified earlier"); |
| 6773 | if (ShiftAmt1 == 0) return 0; // Will be simplified in the future. |
| 6774 | Value *X = ShiftOp->getOperand(0); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6775 | |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6776 | uint32_t AmtSum = ShiftAmt1+ShiftAmt2; // Fold into one big shift. |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6777 | if (AmtSum > TypeBits) |
| 6778 | AmtSum = TypeBits; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6779 | |
| 6780 | const IntegerType *Ty = cast<IntegerType>(I.getType()); |
| 6781 | |
| 6782 | // Check for (X << c1) << c2 and (X >> c1) >> c2 |
Chris Lattner | 7f3da2d | 2007-02-03 23:28:07 +0000 | [diff] [blame] | 6783 | if (I.getOpcode() == ShiftOp->getOpcode()) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6784 | return BinaryOperator::Create(I.getOpcode(), X, |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6785 | ConstantInt::get(Ty, AmtSum)); |
| 6786 | } else if (ShiftOp->getOpcode() == Instruction::LShr && |
| 6787 | I.getOpcode() == Instruction::AShr) { |
| 6788 | // ((X >>u C1) >>s C2) -> (X >>u (C1+C2)) since C1 != 0. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6789 | return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, AmtSum)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6790 | } else if (ShiftOp->getOpcode() == Instruction::AShr && |
| 6791 | I.getOpcode() == Instruction::LShr) { |
| 6792 | // ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0. |
| 6793 | Instruction *Shift = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6794 | BinaryOperator::CreateAShr(X, ConstantInt::get(Ty, AmtSum)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6795 | InsertNewInstBefore(Shift, I); |
| 6796 | |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6797 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6798 | return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6799 | } |
| 6800 | |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6801 | // Okay, if we get here, one shift must be left, and the other shift must be |
| 6802 | // right. See if the amounts are equal. |
| 6803 | if (ShiftAmt1 == ShiftAmt2) { |
| 6804 | // If we have ((X >>? C) << C), turn this into X & (-1 << C). |
| 6805 | if (I.getOpcode() == Instruction::Shl) { |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6806 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6807 | return BinaryOperator::CreateAnd(X, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6808 | } |
| 6809 | // If we have ((X << C) >>u C), turn this into X & (-1 >>u C). |
| 6810 | if (I.getOpcode() == Instruction::LShr) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 6811 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6812 | return BinaryOperator::CreateAnd(X, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6813 | } |
| 6814 | // We can simplify ((X << C) >>s C) into a trunc + sext. |
| 6815 | // NOTE: we could do this for any C, but that would make 'unusual' integer |
| 6816 | // types. For now, just stick to ones well-supported by the code |
| 6817 | // generators. |
| 6818 | const Type *SExtType = 0; |
| 6819 | switch (Ty->getBitWidth() - ShiftAmt1) { |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6820 | case 1 : |
| 6821 | case 8 : |
| 6822 | case 16 : |
| 6823 | case 32 : |
| 6824 | case 64 : |
| 6825 | case 128: |
| 6826 | SExtType = IntegerType::get(Ty->getBitWidth() - ShiftAmt1); |
| 6827 | break; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6828 | default: break; |
| 6829 | } |
| 6830 | if (SExtType) { |
| 6831 | Instruction *NewTrunc = new TruncInst(X, SExtType, "sext"); |
| 6832 | InsertNewInstBefore(NewTrunc, I); |
| 6833 | return new SExtInst(NewTrunc, Ty); |
| 6834 | } |
| 6835 | // Otherwise, we can't handle it yet. |
| 6836 | } else if (ShiftAmt1 < ShiftAmt2) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6837 | uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1; |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6838 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6839 | // (X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6840 | if (I.getOpcode() == Instruction::Shl) { |
| 6841 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 6842 | ShiftOp->getOpcode() == Instruction::AShr); |
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 6843 | Instruction *Shift = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6844 | BinaryOperator::CreateShl(X, ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 6845 | InsertNewInstBefore(Shift, I); |
| 6846 | |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6847 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6848 | return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6849 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6850 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6851 | // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6852 | if (I.getOpcode() == Instruction::LShr) { |
| 6853 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
| 6854 | Instruction *Shift = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6855 | BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6856 | InsertNewInstBefore(Shift, I); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6857 | |
Reid Spencer | d5e30f0 | 2007-03-26 17:18:58 +0000 | [diff] [blame] | 6858 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6859 | return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6860 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6861 | |
| 6862 | // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. |
| 6863 | } else { |
| 6864 | assert(ShiftAmt2 < ShiftAmt1); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6865 | uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6866 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6867 | // (X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6868 | if (I.getOpcode() == Instruction::Shl) { |
| 6869 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 6870 | ShiftOp->getOpcode() == Instruction::AShr); |
| 6871 | Instruction *Shift = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6872 | BinaryOperator::Create(ShiftOp->getOpcode(), X, |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6873 | ConstantInt::get(Ty, ShiftDiff)); |
| 6874 | InsertNewInstBefore(Shift, I); |
| 6875 | |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6876 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6877 | return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6878 | } |
| 6879 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6880 | // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6881 | if (I.getOpcode() == Instruction::LShr) { |
| 6882 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
| 6883 | Instruction *Shift = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6884 | BinaryOperator::CreateShl(X, ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6885 | InsertNewInstBefore(Shift, I); |
| 6886 | |
Reid Spencer | 68d27cf | 2007-03-26 23:45:51 +0000 | [diff] [blame] | 6887 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6888 | return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6889 | } |
| 6890 | |
| 6891 | // We can't handle (X << C1) >>a C2, it shifts arbitrary bits in. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 6892 | } |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6893 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6894 | return 0; |
| 6895 | } |
| 6896 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 6897 | |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6898 | /// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear |
| 6899 | /// expression. If so, decompose it, returning some value X, such that Val is |
| 6900 | /// X*Scale+Offset. |
| 6901 | /// |
| 6902 | static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale, |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6903 | int &Offset) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6904 | assert(Val->getType() == Type::Int32Ty && "Unexpected allocation size type!"); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6905 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6906 | Offset = CI->getZExtValue(); |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 6907 | Scale = 0; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6908 | return ConstantInt::get(Type::Int32Ty, 0); |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 6909 | } else if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) { |
| 6910 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 6911 | if (I->getOpcode() == Instruction::Shl) { |
| 6912 | // This is a value scaled by '1 << the shift amt'. |
| 6913 | Scale = 1U << RHS->getZExtValue(); |
| 6914 | Offset = 0; |
| 6915 | return I->getOperand(0); |
| 6916 | } else if (I->getOpcode() == Instruction::Mul) { |
| 6917 | // This value is scaled by 'RHS'. |
| 6918 | Scale = RHS->getZExtValue(); |
| 6919 | Offset = 0; |
| 6920 | return I->getOperand(0); |
| 6921 | } else if (I->getOpcode() == Instruction::Add) { |
| 6922 | // We have X+C. Check to see if we really have (X*C2)+C1, |
| 6923 | // where C1 is divisible by C2. |
| 6924 | unsigned SubScale; |
| 6925 | Value *SubVal = |
| 6926 | DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset); |
| 6927 | Offset += RHS->getZExtValue(); |
| 6928 | Scale = SubScale; |
| 6929 | return SubVal; |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6930 | } |
| 6931 | } |
| 6932 | } |
| 6933 | |
| 6934 | // Otherwise, we can't look past this. |
| 6935 | Scale = 1; |
| 6936 | Offset = 0; |
| 6937 | return Val; |
| 6938 | } |
| 6939 | |
| 6940 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6941 | /// PromoteCastOfAllocation - If we find a cast of an allocation instruction, |
| 6942 | /// try to eliminate the cast by moving the type information into the alloc. |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6943 | Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI, |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6944 | AllocationInst &AI) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6945 | const PointerType *PTy = cast<PointerType>(CI.getType()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6946 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6947 | // Remove any uses of AI that are dead. |
| 6948 | assert(!CI.use_empty() && "Dead instructions should be removed earlier!"); |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 6949 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6950 | for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) { |
| 6951 | Instruction *User = cast<Instruction>(*UI++); |
| 6952 | if (isInstructionTriviallyDead(User)) { |
| 6953 | while (UI != E && *UI == User) |
| 6954 | ++UI; // If this instruction uses AI more than once, don't break UI. |
| 6955 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6956 | ++NumDeadInst; |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 6957 | DOUT << "IC: DCE: " << *User; |
Chris Lattner | f22a5c6 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 6958 | EraseInstFromFunction(*User); |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6959 | } |
| 6960 | } |
| 6961 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6962 | // Get the type really allocated and the type casted to. |
| 6963 | const Type *AllocElTy = AI.getAllocatedType(); |
| 6964 | const Type *CastElTy = PTy->getElementType(); |
| 6965 | if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0; |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6966 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 6967 | unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy); |
| 6968 | unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy); |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6969 | if (CastElTyAlign < AllocElTyAlign) return 0; |
| 6970 | |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6971 | // If the allocation has multiple uses, only promote it if we are strictly |
| 6972 | // increasing the alignment of the resultant allocation. If we keep it the |
| 6973 | // same, we open the door to infinite loops of various kinds. |
| 6974 | if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0; |
| 6975 | |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 6976 | uint64_t AllocElTySize = TD->getABITypeSize(AllocElTy); |
| 6977 | uint64_t CastElTySize = TD->getABITypeSize(CastElTy); |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 6978 | if (CastElTySize == 0 || AllocElTySize == 0) return 0; |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6979 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6980 | // See if we can satisfy the modulus by pulling a scale out of the array |
| 6981 | // size argument. |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6982 | unsigned ArraySizeScale; |
| 6983 | int ArrayOffset; |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6984 | Value *NumElements = // See if the array size is a decomposable linear expr. |
| 6985 | DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset); |
| 6986 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6987 | // If we can now satisfy the modulus, by using a non-1 scale, we really can |
| 6988 | // do the xform. |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6989 | if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 || |
| 6990 | (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0; |
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 6991 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6992 | unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize; |
| 6993 | Value *Amt = 0; |
| 6994 | if (Scale == 1) { |
| 6995 | Amt = NumElements; |
| 6996 | } else { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6997 | // If the allocation size is constant, form a constant mul expression |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6998 | Amt = ConstantInt::get(Type::Int32Ty, Scale); |
| 6999 | if (isa<ConstantInt>(NumElements)) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 7000 | Amt = Multiply(cast<ConstantInt>(NumElements), cast<ConstantInt>(Amt)); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7001 | // otherwise multiply the amount and the number of elements |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 7002 | else if (Scale != 1) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7003 | Instruction *Tmp = BinaryOperator::CreateMul(Amt, NumElements, "tmp"); |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 7004 | Amt = InsertNewInstBefore(Tmp, AI); |
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 7005 | } |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 7006 | } |
| 7007 | |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 7008 | if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { |
| 7009 | Value *Off = ConstantInt::get(Type::Int32Ty, Offset, true); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7010 | Instruction *Tmp = BinaryOperator::CreateAdd(Amt, Off, "tmp"); |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 7011 | Amt = InsertNewInstBefore(Tmp, AI); |
| 7012 | } |
| 7013 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 7014 | AllocationInst *New; |
| 7015 | if (isa<MallocInst>(AI)) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7016 | New = new MallocInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 7017 | else |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7018 | New = new AllocaInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 7019 | InsertNewInstBefore(New, AI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7020 | New->takeName(&AI); |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 7021 | |
| 7022 | // If the allocation has multiple uses, insert a cast and change all things |
| 7023 | // that used it to use the new cast. This will also hack on CI, but it will |
| 7024 | // die soon. |
| 7025 | if (!AI.hasOneUse()) { |
| 7026 | AddUsesToWorkList(AI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7027 | // New is the allocation instruction, pointer typed. AI is the original |
| 7028 | // allocation instruction, also pointer typed. Thus, cast to use is BitCast. |
| 7029 | CastInst *NewCast = new BitCastInst(New, AI.getType(), "tmpcast"); |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 7030 | InsertNewInstBefore(NewCast, AI); |
| 7031 | AI.replaceAllUsesWith(NewCast); |
| 7032 | } |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 7033 | return ReplaceInstUsesWith(CI, New); |
| 7034 | } |
| 7035 | |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7036 | /// CanEvaluateInDifferentType - Return true if we can take the specified value |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7037 | /// and return it as type Ty without inserting any new casts and without |
| 7038 | /// changing the computed value. This is used by code that tries to decide |
| 7039 | /// whether promoting or shrinking integer operations to wider or smaller types |
| 7040 | /// will allow us to eliminate a truncate or extend. |
| 7041 | /// |
| 7042 | /// This is a truncation operation if Ty is smaller than V->getType(), or an |
| 7043 | /// extension operation if Ty is larger. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 7044 | bool InstCombiner::CanEvaluateInDifferentType(Value *V, const IntegerType *Ty, |
| 7045 | unsigned CastOpc, |
| 7046 | int &NumCastsRemoved) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7047 | // We can always evaluate constants in another type. |
| 7048 | if (isa<ConstantInt>(V)) |
| 7049 | return true; |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7050 | |
| 7051 | Instruction *I = dyn_cast<Instruction>(V); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7052 | if (!I) return false; |
| 7053 | |
| 7054 | const IntegerType *OrigTy = cast<IntegerType>(V->getType()); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7055 | |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7056 | // If this is an extension or truncate, we can often eliminate it. |
| 7057 | if (isa<TruncInst>(I) || isa<ZExtInst>(I) || isa<SExtInst>(I)) { |
| 7058 | // If this is a cast from the destination type, we can trivially eliminate |
| 7059 | // it, and this will remove a cast overall. |
| 7060 | if (I->getOperand(0)->getType() == Ty) { |
| 7061 | // If the first operand is itself a cast, and is eliminable, do not count |
| 7062 | // this as an eliminable cast. We would prefer to eliminate those two |
| 7063 | // casts first. |
| 7064 | if (!isa<CastInst>(I->getOperand(0))) |
| 7065 | ++NumCastsRemoved; |
| 7066 | return true; |
| 7067 | } |
| 7068 | } |
| 7069 | |
| 7070 | // We can't extend or shrink something that has multiple uses: doing so would |
| 7071 | // require duplicating the instruction in general, which isn't profitable. |
| 7072 | if (!I->hasOneUse()) return false; |
| 7073 | |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7074 | switch (I->getOpcode()) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7075 | case Instruction::Add: |
| 7076 | case Instruction::Sub: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7077 | case Instruction::And: |
| 7078 | case Instruction::Or: |
| 7079 | case Instruction::Xor: |
| 7080 | // These operators can all arbitrarily be extended or truncated. |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7081 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 7082 | NumCastsRemoved) && |
| 7083 | CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc, |
| 7084 | NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7085 | |
Nick Lewycky | e6b0c00 | 2008-01-22 05:08:48 +0000 | [diff] [blame] | 7086 | case Instruction::Mul: |
Nick Lewycky | e6b0c00 | 2008-01-22 05:08:48 +0000 | [diff] [blame] | 7087 | // A multiply can be truncated by truncating its operands. |
| 7088 | return Ty->getBitWidth() < OrigTy->getBitWidth() && |
| 7089 | CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 7090 | NumCastsRemoved) && |
| 7091 | CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc, |
| 7092 | NumCastsRemoved); |
| 7093 | |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 7094 | case Instruction::Shl: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7095 | // If we are truncating the result of this SHL, and if it's a shift of a |
| 7096 | // constant amount, we can always perform a SHL in a smaller type. |
| 7097 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7098 | uint32_t BitWidth = Ty->getBitWidth(); |
| 7099 | if (BitWidth < OrigTy->getBitWidth() && |
| 7100 | CI->getLimitedValue(BitWidth) < BitWidth) |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7101 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 7102 | NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7103 | } |
| 7104 | break; |
| 7105 | case Instruction::LShr: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7106 | // If this is a truncate of a logical shr, we can truncate it to a smaller |
| 7107 | // lshr iff we know that the bits we would otherwise be shifting in are |
| 7108 | // already zeros. |
| 7109 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7110 | uint32_t OrigBitWidth = OrigTy->getBitWidth(); |
| 7111 | uint32_t BitWidth = Ty->getBitWidth(); |
| 7112 | if (BitWidth < OrigBitWidth && |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7113 | MaskedValueIsZero(I->getOperand(0), |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7114 | APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) && |
| 7115 | CI->getLimitedValue(BitWidth) < BitWidth) { |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7116 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 7117 | NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7118 | } |
| 7119 | } |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 7120 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7121 | case Instruction::ZExt: |
| 7122 | case Instruction::SExt: |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7123 | case Instruction::Trunc: |
| 7124 | // If this is the same kind of case as our original (e.g. zext+zext), we |
Chris Lattner | 5543a85 | 2007-08-02 17:23:38 +0000 | [diff] [blame] | 7125 | // can safely replace it. Note that replacing it does not reduce the number |
| 7126 | // of casts in the input. |
| 7127 | if (I->getOpcode() == CastOpc) |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7128 | return true; |
Chris Lattner | 50d9d77 | 2007-09-10 23:46:29 +0000 | [diff] [blame] | 7129 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7130 | break; |
| 7131 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7132 | // TODO: Can handle more cases here. |
| 7133 | break; |
| 7134 | } |
| 7135 | |
| 7136 | return false; |
| 7137 | } |
| 7138 | |
| 7139 | /// EvaluateInDifferentType - Given an expression that |
| 7140 | /// CanEvaluateInDifferentType returns true for, actually insert the code to |
| 7141 | /// evaluate the expression. |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 7142 | Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty, |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7143 | bool isSigned) { |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7144 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 7145 | return ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7146 | |
| 7147 | // Otherwise, it must be an instruction. |
| 7148 | Instruction *I = cast<Instruction>(V); |
Chris Lattner | 01859e8 | 2006-05-20 23:14:03 +0000 | [diff] [blame] | 7149 | Instruction *Res = 0; |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7150 | switch (I->getOpcode()) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7151 | case Instruction::Add: |
| 7152 | case Instruction::Sub: |
Nick Lewycky | e6b0c00 | 2008-01-22 05:08:48 +0000 | [diff] [blame] | 7153 | case Instruction::Mul: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7154 | case Instruction::And: |
| 7155 | case Instruction::Or: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7156 | case Instruction::Xor: |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 7157 | case Instruction::AShr: |
| 7158 | case Instruction::LShr: |
| 7159 | case Instruction::Shl: { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 7160 | Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7161 | Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7162 | Res = BinaryOperator::Create((Instruction::BinaryOps)I->getOpcode(), |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7163 | LHS, RHS, I->getName()); |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 7164 | break; |
| 7165 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7166 | case Instruction::Trunc: |
| 7167 | case Instruction::ZExt: |
| 7168 | case Instruction::SExt: |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7169 | // If the source type of the cast is the type we're trying for then we can |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7170 | // just return the source. There's no need to insert it because it is not |
| 7171 | // new. |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7172 | if (I->getOperand(0)->getType() == Ty) |
| 7173 | return I->getOperand(0); |
| 7174 | |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7175 | // Otherwise, must be the same type of case, so just reinsert a new one. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7176 | Res = CastInst::Create(cast<CastInst>(I)->getOpcode(), I->getOperand(0), |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7177 | Ty, I->getName()); |
| 7178 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7179 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7180 | // TODO: Can handle more cases here. |
| 7181 | assert(0 && "Unreachable!"); |
| 7182 | break; |
| 7183 | } |
| 7184 | |
| 7185 | return InsertNewInstBefore(Res, *I); |
| 7186 | } |
| 7187 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7188 | /// @brief Implement the transforms common to all CastInst visitors. |
| 7189 | Instruction *InstCombiner::commonCastTransforms(CastInst &CI) { |
Chris Lattner | 79d35b3 | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 7190 | Value *Src = CI.getOperand(0); |
| 7191 | |
Dan Gohman | 23d9d27 | 2007-05-11 21:10:54 +0000 | [diff] [blame] | 7192 | // Many cases of "cast of a cast" are eliminable. If it's eliminable we just |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7193 | // eliminate it now. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 7194 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7195 | if (Instruction::CastOps opc = |
| 7196 | isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) { |
| 7197 | // The first cast (CSrc) is eliminable so we need to fix up or replace |
| 7198 | // the second cast (CI). CSrc will then have a good chance of being dead. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7199 | return CastInst::Create(opc, CSrc->getOperand(0), CI.getType()); |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 7200 | } |
| 7201 | } |
Chris Lattner | a710ddc | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 7202 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7203 | // If we are casting a select then fold the cast into the select |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 7204 | if (SelectInst *SI = dyn_cast<SelectInst>(Src)) |
| 7205 | if (Instruction *NV = FoldOpIntoSelect(CI, SI, this)) |
| 7206 | return NV; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7207 | |
| 7208 | // If we are casting a PHI then fold the cast into the PHI |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 7209 | if (isa<PHINode>(Src)) |
| 7210 | if (Instruction *NV = FoldOpIntoPhi(CI)) |
| 7211 | return NV; |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 7212 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7213 | return 0; |
| 7214 | } |
| 7215 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7216 | /// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint) |
| 7217 | Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) { |
| 7218 | Value *Src = CI.getOperand(0); |
| 7219 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7220 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7221 | // If casting the result of a getelementptr instruction with no offset, turn |
| 7222 | // this into a cast of the original pointer! |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7223 | if (GEP->hasAllZeroIndices()) { |
| 7224 | // Changing the cast operand is usually not a good idea but it is safe |
| 7225 | // here because the pointer operand is being replaced with another |
| 7226 | // pointer operand so the opcode doesn't need to change. |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7227 | AddToWorkList(GEP); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7228 | CI.setOperand(0, GEP->getOperand(0)); |
| 7229 | return &CI; |
| 7230 | } |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7231 | |
| 7232 | // If the GEP has a single use, and the base pointer is a bitcast, and the |
| 7233 | // GEP computes a constant offset, see if we can convert these three |
| 7234 | // instructions into fewer. This typically happens with unions and other |
| 7235 | // non-type-safe code. |
| 7236 | if (GEP->hasOneUse() && isa<BitCastInst>(GEP->getOperand(0))) { |
| 7237 | if (GEP->hasAllConstantIndices()) { |
| 7238 | // We are guaranteed to get a constant from EmitGEPOffset. |
| 7239 | ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(GEP, CI, *this)); |
| 7240 | int64_t Offset = OffsetV->getSExtValue(); |
| 7241 | |
| 7242 | // Get the base pointer input of the bitcast, and the type it points to. |
| 7243 | Value *OrigBase = cast<BitCastInst>(GEP->getOperand(0))->getOperand(0); |
| 7244 | const Type *GEPIdxTy = |
| 7245 | cast<PointerType>(OrigBase->getType())->getElementType(); |
| 7246 | if (GEPIdxTy->isSized()) { |
| 7247 | SmallVector<Value*, 8> NewIndices; |
| 7248 | |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 7249 | // Start with the index over the outer type. Note that the type size |
| 7250 | // might be zero (even if the offset isn't zero) if the indexed type |
| 7251 | // is something like [0 x {int, int}] |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7252 | const Type *IntPtrTy = TD->getIntPtrType(); |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 7253 | int64_t FirstIdx = 0; |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 7254 | if (int64_t TySize = TD->getABITypeSize(GEPIdxTy)) { |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 7255 | FirstIdx = Offset/TySize; |
| 7256 | Offset %= TySize; |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7257 | |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 7258 | // Handle silly modulus not returning values values [0..TySize). |
| 7259 | if (Offset < 0) { |
| 7260 | --FirstIdx; |
| 7261 | Offset += TySize; |
| 7262 | assert(Offset >= 0); |
| 7263 | } |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 7264 | assert((uint64_t)Offset < (uint64_t)TySize &&"Out of range offset"); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7265 | } |
| 7266 | |
| 7267 | NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx)); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7268 | |
| 7269 | // Index into the types. If we fail, set OrigBase to null. |
| 7270 | while (Offset) { |
| 7271 | if (const StructType *STy = dyn_cast<StructType>(GEPIdxTy)) { |
| 7272 | const StructLayout *SL = TD->getStructLayout(STy); |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 7273 | if (Offset < (int64_t)SL->getSizeInBytes()) { |
| 7274 | unsigned Elt = SL->getElementContainingOffset(Offset); |
| 7275 | NewIndices.push_back(ConstantInt::get(Type::Int32Ty, Elt)); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7276 | |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 7277 | Offset -= SL->getElementOffset(Elt); |
| 7278 | GEPIdxTy = STy->getElementType(Elt); |
| 7279 | } else { |
| 7280 | // Otherwise, we can't index into this, bail out. |
| 7281 | Offset = 0; |
| 7282 | OrigBase = 0; |
| 7283 | } |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7284 | } else if (isa<ArrayType>(GEPIdxTy) || isa<VectorType>(GEPIdxTy)) { |
| 7285 | const SequentialType *STy = cast<SequentialType>(GEPIdxTy); |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 7286 | if (uint64_t EltSize = TD->getABITypeSize(STy->getElementType())){ |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 7287 | NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize)); |
| 7288 | Offset %= EltSize; |
| 7289 | } else { |
| 7290 | NewIndices.push_back(ConstantInt::get(IntPtrTy, 0)); |
| 7291 | } |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7292 | GEPIdxTy = STy->getElementType(); |
| 7293 | } else { |
| 7294 | // Otherwise, we can't index into this, bail out. |
| 7295 | Offset = 0; |
| 7296 | OrigBase = 0; |
| 7297 | } |
| 7298 | } |
| 7299 | if (OrigBase) { |
| 7300 | // If we were able to index down into an element, create the GEP |
| 7301 | // and bitcast the result. This eliminates one bitcast, potentially |
| 7302 | // two. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7303 | Instruction *NGEP = GetElementPtrInst::Create(OrigBase, |
| 7304 | NewIndices.begin(), |
| 7305 | NewIndices.end(), ""); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7306 | InsertNewInstBefore(NGEP, CI); |
| 7307 | NGEP->takeName(GEP); |
| 7308 | |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7309 | if (isa<BitCastInst>(CI)) |
| 7310 | return new BitCastInst(NGEP, CI.getType()); |
| 7311 | assert(isa<PtrToIntInst>(CI)); |
| 7312 | return new PtrToIntInst(NGEP, CI.getType()); |
| 7313 | } |
| 7314 | } |
| 7315 | } |
| 7316 | } |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7317 | } |
| 7318 | |
| 7319 | return commonCastTransforms(CI); |
| 7320 | } |
| 7321 | |
| 7322 | |
| 7323 | |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7324 | /// Only the TRUNC, ZEXT, SEXT, and BITCAST can both operand and result as |
| 7325 | /// integer types. This function implements the common transforms for all those |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7326 | /// cases. |
| 7327 | /// @brief Implement the transforms common to CastInst with integer operands |
| 7328 | Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) { |
| 7329 | if (Instruction *Result = commonCastTransforms(CI)) |
| 7330 | return Result; |
| 7331 | |
| 7332 | Value *Src = CI.getOperand(0); |
| 7333 | const Type *SrcTy = Src->getType(); |
| 7334 | const Type *DestTy = CI.getType(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7335 | uint32_t SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 7336 | uint32_t DestBitSize = DestTy->getPrimitiveSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7337 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7338 | // See if we can simplify any instructions used by the LHS whose sole |
| 7339 | // purpose is to compute bits we don't care about. |
Reid Spencer | ad6676e | 2007-03-22 20:56:53 +0000 | [diff] [blame] | 7340 | APInt KnownZero(DestBitSize, 0), KnownOne(DestBitSize, 0); |
| 7341 | if (SimplifyDemandedBits(&CI, APInt::getAllOnesValue(DestBitSize), |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7342 | KnownZero, KnownOne)) |
| 7343 | return &CI; |
| 7344 | |
| 7345 | // If the source isn't an instruction or has more than one use then we |
| 7346 | // can't do anything more. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7347 | Instruction *SrcI = dyn_cast<Instruction>(Src); |
| 7348 | if (!SrcI || !Src->hasOneUse()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7349 | return 0; |
| 7350 | |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7351 | // Attempt to propagate the cast into the instruction for int->int casts. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7352 | int NumCastsRemoved = 0; |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7353 | if (!isa<BitCastInst>(CI) && |
| 7354 | CanEvaluateInDifferentType(SrcI, cast<IntegerType>(DestTy), |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7355 | CI.getOpcode(), NumCastsRemoved)) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7356 | // If this cast is a truncate, evaluting in a different type always |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7357 | // eliminates the cast, so it is always a win. If this is a zero-extension, |
| 7358 | // we need to do an AND to maintain the clear top-part of the computation, |
| 7359 | // so we require that the input have eliminated at least one cast. If this |
| 7360 | // is a sign extension, we insert two new casts (to do the extension) so we |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7361 | // require that two casts have been eliminated. |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7362 | bool DoXForm; |
| 7363 | switch (CI.getOpcode()) { |
| 7364 | default: |
| 7365 | // All the others use floating point so we shouldn't actually |
| 7366 | // get here because of the check above. |
| 7367 | assert(0 && "Unknown cast type"); |
| 7368 | case Instruction::Trunc: |
| 7369 | DoXForm = true; |
| 7370 | break; |
| 7371 | case Instruction::ZExt: |
| 7372 | DoXForm = NumCastsRemoved >= 1; |
| 7373 | break; |
| 7374 | case Instruction::SExt: |
| 7375 | DoXForm = NumCastsRemoved >= 2; |
| 7376 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7377 | } |
| 7378 | |
| 7379 | if (DoXForm) { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 7380 | Value *Res = EvaluateInDifferentType(SrcI, DestTy, |
| 7381 | CI.getOpcode() == Instruction::SExt); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7382 | assert(Res->getType() == DestTy); |
| 7383 | switch (CI.getOpcode()) { |
| 7384 | default: assert(0 && "Unknown cast type!"); |
| 7385 | case Instruction::Trunc: |
| 7386 | case Instruction::BitCast: |
| 7387 | // Just replace this cast with the result. |
| 7388 | return ReplaceInstUsesWith(CI, Res); |
| 7389 | case Instruction::ZExt: { |
| 7390 | // We need to emit an AND to clear the high bits. |
| 7391 | assert(SrcBitSize < DestBitSize && "Not a zext?"); |
Chris Lattner | cd1d6d5 | 2007-04-02 05:48:58 +0000 | [diff] [blame] | 7392 | Constant *C = ConstantInt::get(APInt::getLowBitsSet(DestBitSize, |
| 7393 | SrcBitSize)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7394 | return BinaryOperator::CreateAnd(Res, C); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7395 | } |
| 7396 | case Instruction::SExt: |
| 7397 | // We need to emit a cast to truncate, then a cast to sext. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7398 | return CastInst::Create(Instruction::SExt, |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7399 | InsertCastBefore(Instruction::Trunc, Res, Src->getType(), |
| 7400 | CI), DestTy); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7401 | } |
| 7402 | } |
| 7403 | } |
| 7404 | |
| 7405 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; |
| 7406 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; |
| 7407 | |
| 7408 | switch (SrcI->getOpcode()) { |
| 7409 | case Instruction::Add: |
| 7410 | case Instruction::Mul: |
| 7411 | case Instruction::And: |
| 7412 | case Instruction::Or: |
| 7413 | case Instruction::Xor: |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 7414 | // If we are discarding information, rewrite. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7415 | if (DestBitSize <= SrcBitSize && DestBitSize != 1) { |
| 7416 | // Don't insert two casts if they cannot be eliminated. We allow |
| 7417 | // two casts to be inserted if the sizes are the same. This could |
| 7418 | // only be converting signedness, which is a noop. |
| 7419 | if (DestBitSize == SrcBitSize || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7420 | !ValueRequiresCast(CI.getOpcode(), Op1, DestTy,TD) || |
| 7421 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 7422 | Instruction::CastOps opcode = CI.getOpcode(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7423 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); |
| 7424 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7425 | return BinaryOperator::Create( |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7426 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7427 | } |
| 7428 | } |
| 7429 | |
| 7430 | // cast (xor bool X, true) to int --> xor (cast bool X to int), 1 |
| 7431 | if (isa<ZExtInst>(CI) && SrcBitSize == 1 && |
| 7432 | SrcI->getOpcode() == Instruction::Xor && |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7433 | Op1 == ConstantInt::getTrue() && |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7434 | (!Op0->hasOneUse() || !isa<CmpInst>(Op0))) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7435 | Value *New = InsertOperandCastBefore(Instruction::ZExt, Op0, DestTy, &CI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7436 | return BinaryOperator::CreateXor(New, ConstantInt::get(CI.getType(), 1)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7437 | } |
| 7438 | break; |
| 7439 | case Instruction::SDiv: |
| 7440 | case Instruction::UDiv: |
| 7441 | case Instruction::SRem: |
| 7442 | case Instruction::URem: |
| 7443 | // If we are just changing the sign, rewrite. |
| 7444 | if (DestBitSize == SrcBitSize) { |
| 7445 | // Don't insert two casts if they cannot be eliminated. We allow |
| 7446 | // two casts to be inserted if the sizes are the same. This could |
| 7447 | // only be converting signedness, which is a noop. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7448 | if (!ValueRequiresCast(CI.getOpcode(), Op1, DestTy, TD) || |
| 7449 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7450 | Value *Op0c = InsertOperandCastBefore(Instruction::BitCast, |
| 7451 | Op0, DestTy, SrcI); |
| 7452 | Value *Op1c = InsertOperandCastBefore(Instruction::BitCast, |
| 7453 | Op1, DestTy, SrcI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7454 | return BinaryOperator::Create( |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7455 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
| 7456 | } |
| 7457 | } |
| 7458 | break; |
| 7459 | |
| 7460 | case Instruction::Shl: |
| 7461 | // Allow changing the sign of the source operand. Do not allow |
| 7462 | // changing the size of the shift, UNLESS the shift amount is a |
| 7463 | // constant. We must not change variable sized shifts to a smaller |
| 7464 | // size, because it is undefined to shift more bits out than exist |
| 7465 | // in the value. |
| 7466 | if (DestBitSize == SrcBitSize || |
| 7467 | (DestBitSize < SrcBitSize && isa<Constant>(Op1))) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7468 | Instruction::CastOps opcode = (DestBitSize == SrcBitSize ? |
| 7469 | Instruction::BitCast : Instruction::Trunc); |
| 7470 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7471 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7472 | return BinaryOperator::CreateShl(Op0c, Op1c); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7473 | } |
| 7474 | break; |
| 7475 | case Instruction::AShr: |
| 7476 | // If this is a signed shr, and if all bits shifted in are about to be |
| 7477 | // truncated off, turn it into an unsigned shr to allow greater |
| 7478 | // simplifications. |
| 7479 | if (DestBitSize < SrcBitSize && |
| 7480 | isa<ConstantInt>(Op1)) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7481 | uint32_t ShiftAmt = cast<ConstantInt>(Op1)->getLimitedValue(SrcBitSize); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7482 | if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) { |
| 7483 | // Insert the new logical shift right. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7484 | return BinaryOperator::CreateLShr(Op0, Op1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7485 | } |
| 7486 | } |
| 7487 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7488 | } |
| 7489 | return 0; |
| 7490 | } |
| 7491 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7492 | Instruction *InstCombiner::visitTrunc(TruncInst &CI) { |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7493 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 7494 | return Result; |
| 7495 | |
| 7496 | Value *Src = CI.getOperand(0); |
| 7497 | const Type *Ty = CI.getType(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7498 | uint32_t DestBitWidth = Ty->getPrimitiveSizeInBits(); |
| 7499 | uint32_t SrcBitWidth = cast<IntegerType>(Src->getType())->getBitWidth(); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7500 | |
| 7501 | if (Instruction *SrcI = dyn_cast<Instruction>(Src)) { |
| 7502 | switch (SrcI->getOpcode()) { |
| 7503 | default: break; |
| 7504 | case Instruction::LShr: |
| 7505 | // We can shrink lshr to something smaller if we know the bits shifted in |
| 7506 | // are already zeros. |
| 7507 | if (ConstantInt *ShAmtV = dyn_cast<ConstantInt>(SrcI->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7508 | uint32_t ShAmt = ShAmtV->getLimitedValue(SrcBitWidth); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7509 | |
| 7510 | // Get a mask for the bits shifting in. |
Zhou Sheng | e82fca0 | 2007-03-28 09:19:01 +0000 | [diff] [blame] | 7511 | APInt Mask(APInt::getLowBitsSet(SrcBitWidth, ShAmt).shl(DestBitWidth)); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7512 | Value* SrcIOp0 = SrcI->getOperand(0); |
| 7513 | if (SrcI->hasOneUse() && MaskedValueIsZero(SrcIOp0, Mask)) { |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7514 | if (ShAmt >= DestBitWidth) // All zeros. |
| 7515 | return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty)); |
| 7516 | |
| 7517 | // Okay, we can shrink this. Truncate the input, then return a new |
| 7518 | // shift. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7519 | Value *V1 = InsertCastBefore(Instruction::Trunc, SrcIOp0, Ty, CI); |
| 7520 | Value *V2 = InsertCastBefore(Instruction::Trunc, SrcI->getOperand(1), |
| 7521 | Ty, CI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7522 | return BinaryOperator::CreateLShr(V1, V2); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7523 | } |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7524 | } else { // This is a variable shr. |
| 7525 | |
| 7526 | // Turn 'trunc (lshr X, Y) to bool' into '(X & (1 << Y)) != 0'. This is |
| 7527 | // more LLVM instructions, but allows '1 << Y' to be hoisted if |
| 7528 | // loop-invariant and CSE'd. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 7529 | if (CI.getType() == Type::Int1Ty && SrcI->hasOneUse()) { |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7530 | Value *One = ConstantInt::get(SrcI->getType(), 1); |
| 7531 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7532 | Value *V = InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7533 | BinaryOperator::CreateShl(One, SrcI->getOperand(1), |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7534 | "tmp"), CI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7535 | V = InsertNewInstBefore(BinaryOperator::CreateAnd(V, |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7536 | SrcI->getOperand(0), |
| 7537 | "tmp"), CI); |
| 7538 | Value *Zero = Constant::getNullValue(V->getType()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7539 | return new ICmpInst(ICmpInst::ICMP_NE, V, Zero); |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7540 | } |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7541 | } |
| 7542 | break; |
| 7543 | } |
| 7544 | } |
| 7545 | |
| 7546 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7547 | } |
| 7548 | |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7549 | /// transformZExtICmp - Transform (zext icmp) to bitwise / integer operations |
| 7550 | /// in order to eliminate the icmp. |
| 7551 | Instruction *InstCombiner::transformZExtICmp(ICmpInst *ICI, Instruction &CI, |
| 7552 | bool DoXform) { |
| 7553 | // If we are just checking for a icmp eq of a single bit and zext'ing it |
| 7554 | // to an integer, then shift the bit to the appropriate place and then |
| 7555 | // cast to integer to avoid the comparison. |
| 7556 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) { |
| 7557 | const APInt &Op1CV = Op1C->getValue(); |
| 7558 | |
| 7559 | // zext (x <s 0) to i32 --> x>>u31 true if signbit set. |
| 7560 | // zext (x >s -1) to i32 --> (x>>u31)^1 true if signbit clear. |
| 7561 | if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) || |
| 7562 | (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())) { |
| 7563 | if (!DoXform) return ICI; |
| 7564 | |
| 7565 | Value *In = ICI->getOperand(0); |
| 7566 | Value *Sh = ConstantInt::get(In->getType(), |
| 7567 | In->getType()->getPrimitiveSizeInBits()-1); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7568 | In = InsertNewInstBefore(BinaryOperator::CreateLShr(In, Sh, |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7569 | In->getName()+".lobit"), |
| 7570 | CI); |
| 7571 | if (In->getType() != CI.getType()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7572 | In = CastInst::CreateIntegerCast(In, CI.getType(), |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7573 | false/*ZExt*/, "tmp", &CI); |
| 7574 | |
| 7575 | if (ICI->getPredicate() == ICmpInst::ICMP_SGT) { |
| 7576 | Constant *One = ConstantInt::get(In->getType(), 1); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7577 | In = InsertNewInstBefore(BinaryOperator::CreateXor(In, One, |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7578 | In->getName()+".not"), |
| 7579 | CI); |
| 7580 | } |
| 7581 | |
| 7582 | return ReplaceInstUsesWith(CI, In); |
| 7583 | } |
| 7584 | |
| 7585 | |
| 7586 | |
| 7587 | // zext (X == 0) to i32 --> X^1 iff X has only the low bit set. |
| 7588 | // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. |
| 7589 | // zext (X == 1) to i32 --> X iff X has only the low bit set. |
| 7590 | // zext (X == 2) to i32 --> X>>1 iff X has only the 2nd bit set. |
| 7591 | // zext (X != 0) to i32 --> X iff X has only the low bit set. |
| 7592 | // zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set. |
| 7593 | // zext (X != 1) to i32 --> X^1 iff X has only the low bit set. |
| 7594 | // zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. |
| 7595 | if ((Op1CV == 0 || Op1CV.isPowerOf2()) && |
| 7596 | // This only works for EQ and NE |
| 7597 | ICI->isEquality()) { |
| 7598 | // If Op1C some other power of two, convert: |
| 7599 | uint32_t BitWidth = Op1C->getType()->getBitWidth(); |
| 7600 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 7601 | APInt TypeMask(APInt::getAllOnesValue(BitWidth)); |
| 7602 | ComputeMaskedBits(ICI->getOperand(0), TypeMask, KnownZero, KnownOne); |
| 7603 | |
| 7604 | APInt KnownZeroMask(~KnownZero); |
| 7605 | if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1? |
| 7606 | if (!DoXform) return ICI; |
| 7607 | |
| 7608 | bool isNE = ICI->getPredicate() == ICmpInst::ICMP_NE; |
| 7609 | if (Op1CV != 0 && (Op1CV != KnownZeroMask)) { |
| 7610 | // (X&4) == 2 --> false |
| 7611 | // (X&4) != 2 --> true |
| 7612 | Constant *Res = ConstantInt::get(Type::Int1Ty, isNE); |
| 7613 | Res = ConstantExpr::getZExt(Res, CI.getType()); |
| 7614 | return ReplaceInstUsesWith(CI, Res); |
| 7615 | } |
| 7616 | |
| 7617 | uint32_t ShiftAmt = KnownZeroMask.logBase2(); |
| 7618 | Value *In = ICI->getOperand(0); |
| 7619 | if (ShiftAmt) { |
| 7620 | // Perform a logical shr by shiftamt. |
| 7621 | // Insert the shift to put the result in the low bit. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7622 | In = InsertNewInstBefore(BinaryOperator::CreateLShr(In, |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7623 | ConstantInt::get(In->getType(), ShiftAmt), |
| 7624 | In->getName()+".lobit"), CI); |
| 7625 | } |
| 7626 | |
| 7627 | if ((Op1CV != 0) == isNE) { // Toggle the low bit. |
| 7628 | Constant *One = ConstantInt::get(In->getType(), 1); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7629 | In = BinaryOperator::CreateXor(In, One, "tmp"); |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7630 | InsertNewInstBefore(cast<Instruction>(In), CI); |
| 7631 | } |
| 7632 | |
| 7633 | if (CI.getType() == In->getType()) |
| 7634 | return ReplaceInstUsesWith(CI, In); |
| 7635 | else |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7636 | return CastInst::CreateIntegerCast(In, CI.getType(), false/*ZExt*/); |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7637 | } |
| 7638 | } |
| 7639 | } |
| 7640 | |
| 7641 | return 0; |
| 7642 | } |
| 7643 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7644 | Instruction *InstCombiner::visitZExt(ZExtInst &CI) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7645 | // If one of the common conversion will work .. |
| 7646 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 7647 | return Result; |
| 7648 | |
| 7649 | Value *Src = CI.getOperand(0); |
| 7650 | |
| 7651 | // If this is a cast of a cast |
| 7652 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7653 | // If this is a TRUNC followed by a ZEXT then we are dealing with integral |
| 7654 | // types and if the sizes are just right we can convert this into a logical |
| 7655 | // 'and' which will be much cheaper than the pair of casts. |
| 7656 | if (isa<TruncInst>(CSrc)) { |
| 7657 | // Get the sizes of the types involved |
| 7658 | Value *A = CSrc->getOperand(0); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7659 | uint32_t SrcSize = A->getType()->getPrimitiveSizeInBits(); |
| 7660 | uint32_t MidSize = CSrc->getType()->getPrimitiveSizeInBits(); |
| 7661 | uint32_t DstSize = CI.getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7662 | // If we're actually extending zero bits and the trunc is a no-op |
| 7663 | if (MidSize < DstSize && SrcSize == DstSize) { |
| 7664 | // Replace both of the casts with an And of the type mask. |
Zhou Sheng | e82fca0 | 2007-03-28 09:19:01 +0000 | [diff] [blame] | 7665 | APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); |
Reid Spencer | ad6676e | 2007-03-22 20:56:53 +0000 | [diff] [blame] | 7666 | Constant *AndConst = ConstantInt::get(AndValue); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7667 | Instruction *And = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7668 | BinaryOperator::CreateAnd(CSrc->getOperand(0), AndConst); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7669 | // Unfortunately, if the type changed, we need to cast it back. |
| 7670 | if (And->getType() != CI.getType()) { |
| 7671 | And->setName(CSrc->getName()+".mask"); |
| 7672 | InsertNewInstBefore(And, CI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7673 | And = CastInst::CreateIntegerCast(And, CI.getType(), false/*ZExt*/); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7674 | } |
| 7675 | return And; |
| 7676 | } |
| 7677 | } |
| 7678 | } |
| 7679 | |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7680 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) |
| 7681 | return transformZExtICmp(ICI, CI); |
Chris Lattner | a2e2c9b | 2007-04-11 06:53:04 +0000 | [diff] [blame] | 7682 | |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7683 | BinaryOperator *SrcI = dyn_cast<BinaryOperator>(Src); |
| 7684 | if (SrcI && SrcI->getOpcode() == Instruction::Or) { |
| 7685 | // zext (or icmp, icmp) --> or (zext icmp), (zext icmp) if at least one |
| 7686 | // of the (zext icmp) will be transformed. |
| 7687 | ICmpInst *LHS = dyn_cast<ICmpInst>(SrcI->getOperand(0)); |
| 7688 | ICmpInst *RHS = dyn_cast<ICmpInst>(SrcI->getOperand(1)); |
| 7689 | if (LHS && RHS && LHS->hasOneUse() && RHS->hasOneUse() && |
| 7690 | (transformZExtICmp(LHS, CI, false) || |
| 7691 | transformZExtICmp(RHS, CI, false))) { |
| 7692 | Value *LCast = InsertCastBefore(Instruction::ZExt, LHS, CI.getType(), CI); |
| 7693 | Value *RCast = InsertCastBefore(Instruction::ZExt, RHS, CI.getType(), CI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7694 | return BinaryOperator::Create(Instruction::Or, LCast, RCast); |
Chris Lattner | 66bc325 | 2007-04-11 05:45:39 +0000 | [diff] [blame] | 7695 | } |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7696 | } |
| 7697 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7698 | return 0; |
| 7699 | } |
| 7700 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7701 | Instruction *InstCombiner::visitSExt(SExtInst &CI) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7702 | if (Instruction *I = commonIntCastTransforms(CI)) |
| 7703 | return I; |
| 7704 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7705 | Value *Src = CI.getOperand(0); |
| 7706 | |
| 7707 | // sext (x <s 0) -> ashr x, 31 -> all ones if signed |
| 7708 | // sext (x >s -1) -> ashr x, 31 -> all ones if not signed |
| 7709 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) { |
| 7710 | // If we are just checking for a icmp eq of a single bit and zext'ing it |
| 7711 | // to an integer, then shift the bit to the appropriate place and then |
| 7712 | // cast to integer to avoid the comparison. |
| 7713 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) { |
| 7714 | const APInt &Op1CV = Op1C->getValue(); |
| 7715 | |
| 7716 | // sext (x <s 0) to i32 --> x>>s31 true if signbit set. |
| 7717 | // sext (x >s -1) to i32 --> (x>>s31)^-1 true if signbit clear. |
| 7718 | if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) || |
| 7719 | (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())){ |
| 7720 | Value *In = ICI->getOperand(0); |
| 7721 | Value *Sh = ConstantInt::get(In->getType(), |
| 7722 | In->getType()->getPrimitiveSizeInBits()-1); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7723 | In = InsertNewInstBefore(BinaryOperator::CreateAShr(In, Sh, |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 7724 | In->getName()+".lobit"), |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7725 | CI); |
| 7726 | if (In->getType() != CI.getType()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7727 | In = CastInst::CreateIntegerCast(In, CI.getType(), |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7728 | true/*SExt*/, "tmp", &CI); |
| 7729 | |
| 7730 | if (ICI->getPredicate() == ICmpInst::ICMP_SGT) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7731 | In = InsertNewInstBefore(BinaryOperator::CreateNot(In, |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7732 | In->getName()+".not"), CI); |
| 7733 | |
| 7734 | return ReplaceInstUsesWith(CI, In); |
| 7735 | } |
| 7736 | } |
| 7737 | } |
| 7738 | |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7739 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7740 | } |
| 7741 | |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7742 | /// FitsInFPType - Return a Constant* for the specified FP constant if it fits |
| 7743 | /// in the specified FP type without changing its value. |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 7744 | static Constant *FitsInFPType(ConstantFP *CFP, const fltSemantics &Sem) { |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7745 | APFloat F = CFP->getValueAPF(); |
| 7746 | if (F.convert(Sem, APFloat::rmNearestTiesToEven) == APFloat::opOK) |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 7747 | return ConstantFP::get(F); |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7748 | return 0; |
| 7749 | } |
| 7750 | |
| 7751 | /// LookThroughFPExtensions - If this is an fp extension instruction, look |
| 7752 | /// through it until we get the source value. |
| 7753 | static Value *LookThroughFPExtensions(Value *V) { |
| 7754 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 7755 | if (I->getOpcode() == Instruction::FPExt) |
| 7756 | return LookThroughFPExtensions(I->getOperand(0)); |
| 7757 | |
| 7758 | // If this value is a constant, return the constant in the smallest FP type |
| 7759 | // that can accurately represent it. This allows us to turn |
| 7760 | // (float)((double)X+2.0) into x+2.0f. |
| 7761 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { |
| 7762 | if (CFP->getType() == Type::PPC_FP128Ty) |
| 7763 | return V; // No constant folding of this. |
| 7764 | // See if the value can be truncated to float and then reextended. |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 7765 | if (Value *V = FitsInFPType(CFP, APFloat::IEEEsingle)) |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7766 | return V; |
| 7767 | if (CFP->getType() == Type::DoubleTy) |
| 7768 | return V; // Won't shrink. |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 7769 | if (Value *V = FitsInFPType(CFP, APFloat::IEEEdouble)) |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7770 | return V; |
| 7771 | // Don't try to shrink to various long double types. |
| 7772 | } |
| 7773 | |
| 7774 | return V; |
| 7775 | } |
| 7776 | |
| 7777 | Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) { |
| 7778 | if (Instruction *I = commonCastTransforms(CI)) |
| 7779 | return I; |
| 7780 | |
| 7781 | // If we have fptrunc(add (fpextend x), (fpextend y)), where x and y are |
| 7782 | // smaller than the destination type, we can eliminate the truncate by doing |
| 7783 | // the add as the smaller type. This applies to add/sub/mul/div as well as |
| 7784 | // many builtins (sqrt, etc). |
| 7785 | BinaryOperator *OpI = dyn_cast<BinaryOperator>(CI.getOperand(0)); |
| 7786 | if (OpI && OpI->hasOneUse()) { |
| 7787 | switch (OpI->getOpcode()) { |
| 7788 | default: break; |
| 7789 | case Instruction::Add: |
| 7790 | case Instruction::Sub: |
| 7791 | case Instruction::Mul: |
| 7792 | case Instruction::FDiv: |
| 7793 | case Instruction::FRem: |
| 7794 | const Type *SrcTy = OpI->getType(); |
| 7795 | Value *LHSTrunc = LookThroughFPExtensions(OpI->getOperand(0)); |
| 7796 | Value *RHSTrunc = LookThroughFPExtensions(OpI->getOperand(1)); |
| 7797 | if (LHSTrunc->getType() != SrcTy && |
| 7798 | RHSTrunc->getType() != SrcTy) { |
| 7799 | unsigned DstSize = CI.getType()->getPrimitiveSizeInBits(); |
| 7800 | // If the source types were both smaller than the destination type of |
| 7801 | // the cast, do this xform. |
| 7802 | if (LHSTrunc->getType()->getPrimitiveSizeInBits() <= DstSize && |
| 7803 | RHSTrunc->getType()->getPrimitiveSizeInBits() <= DstSize) { |
| 7804 | LHSTrunc = InsertCastBefore(Instruction::FPExt, LHSTrunc, |
| 7805 | CI.getType(), CI); |
| 7806 | RHSTrunc = InsertCastBefore(Instruction::FPExt, RHSTrunc, |
| 7807 | CI.getType(), CI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7808 | return BinaryOperator::Create(OpI->getOpcode(), LHSTrunc, RHSTrunc); |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7809 | } |
| 7810 | } |
| 7811 | break; |
| 7812 | } |
| 7813 | } |
| 7814 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7815 | } |
| 7816 | |
| 7817 | Instruction *InstCombiner::visitFPExt(CastInst &CI) { |
| 7818 | return commonCastTransforms(CI); |
| 7819 | } |
| 7820 | |
| 7821 | Instruction *InstCombiner::visitFPToUI(CastInst &CI) { |
Reid Spencer | 44c030a | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 7822 | return commonCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7823 | } |
| 7824 | |
| 7825 | Instruction *InstCombiner::visitFPToSI(CastInst &CI) { |
Reid Spencer | 44c030a | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 7826 | return commonCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7827 | } |
| 7828 | |
| 7829 | Instruction *InstCombiner::visitUIToFP(CastInst &CI) { |
| 7830 | return commonCastTransforms(CI); |
| 7831 | } |
| 7832 | |
| 7833 | Instruction *InstCombiner::visitSIToFP(CastInst &CI) { |
| 7834 | return commonCastTransforms(CI); |
| 7835 | } |
| 7836 | |
| 7837 | Instruction *InstCombiner::visitPtrToInt(CastInst &CI) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7838 | return commonPointerCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7839 | } |
| 7840 | |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7841 | Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) { |
| 7842 | if (Instruction *I = commonCastTransforms(CI)) |
| 7843 | return I; |
| 7844 | |
| 7845 | const Type *DestPointee = cast<PointerType>(CI.getType())->getElementType(); |
| 7846 | if (!DestPointee->isSized()) return 0; |
| 7847 | |
| 7848 | // If this is inttoptr(add (ptrtoint x), cst), try to turn this into a GEP. |
| 7849 | ConstantInt *Cst; |
| 7850 | Value *X; |
| 7851 | if (match(CI.getOperand(0), m_Add(m_Cast<PtrToIntInst>(m_Value(X)), |
| 7852 | m_ConstantInt(Cst)))) { |
| 7853 | // If the source and destination operands have the same type, see if this |
| 7854 | // is a single-index GEP. |
| 7855 | if (X->getType() == CI.getType()) { |
| 7856 | // Get the size of the pointee type. |
Bill Wendling | b9d4f8d | 2008-03-14 05:12:19 +0000 | [diff] [blame] | 7857 | uint64_t Size = TD->getABITypeSize(DestPointee); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7858 | |
| 7859 | // Convert the constant to intptr type. |
| 7860 | APInt Offset = Cst->getValue(); |
| 7861 | Offset.sextOrTrunc(TD->getPointerSizeInBits()); |
| 7862 | |
| 7863 | // If Offset is evenly divisible by Size, we can do this xform. |
| 7864 | if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){ |
| 7865 | Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size)); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7866 | return GetElementPtrInst::Create(X, ConstantInt::get(Offset)); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7867 | } |
| 7868 | } |
| 7869 | // TODO: Could handle other cases, e.g. where add is indexing into field of |
| 7870 | // struct etc. |
| 7871 | } else if (CI.getOperand(0)->hasOneUse() && |
| 7872 | match(CI.getOperand(0), m_Add(m_Value(X), m_ConstantInt(Cst)))) { |
| 7873 | // Otherwise, if this is inttoptr(add x, cst), try to turn this into an |
| 7874 | // "inttoptr+GEP" instead of "add+intptr". |
| 7875 | |
| 7876 | // Get the size of the pointee type. |
| 7877 | uint64_t Size = TD->getABITypeSize(DestPointee); |
| 7878 | |
| 7879 | // Convert the constant to intptr type. |
| 7880 | APInt Offset = Cst->getValue(); |
| 7881 | Offset.sextOrTrunc(TD->getPointerSizeInBits()); |
| 7882 | |
| 7883 | // If Offset is evenly divisible by Size, we can do this xform. |
| 7884 | if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){ |
| 7885 | Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size)); |
| 7886 | |
| 7887 | Instruction *P = InsertNewInstBefore(new IntToPtrInst(X, CI.getType(), |
| 7888 | "tmp"), CI); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7889 | return GetElementPtrInst::Create(P, ConstantInt::get(Offset), "tmp"); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7890 | } |
| 7891 | } |
| 7892 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7893 | } |
| 7894 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7895 | Instruction *InstCombiner::visitBitCast(BitCastInst &CI) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7896 | // If the operands are integer typed then apply the integer transforms, |
| 7897 | // otherwise just apply the common ones. |
| 7898 | Value *Src = CI.getOperand(0); |
| 7899 | const Type *SrcTy = Src->getType(); |
| 7900 | const Type *DestTy = CI.getType(); |
| 7901 | |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 7902 | if (SrcTy->isInteger() && DestTy->isInteger()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7903 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 7904 | return Result; |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7905 | } else if (isa<PointerType>(SrcTy)) { |
| 7906 | if (Instruction *I = commonPointerCastTransforms(CI)) |
| 7907 | return I; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7908 | } else { |
| 7909 | if (Instruction *Result = commonCastTransforms(CI)) |
| 7910 | return Result; |
| 7911 | } |
| 7912 | |
| 7913 | |
| 7914 | // Get rid of casts from one type to the same type. These are useless and can |
| 7915 | // be replaced by the operand. |
| 7916 | if (DestTy == Src->getType()) |
| 7917 | return ReplaceInstUsesWith(CI, Src); |
| 7918 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7919 | if (const PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7920 | const PointerType *SrcPTy = cast<PointerType>(SrcTy); |
| 7921 | const Type *DstElTy = DstPTy->getElementType(); |
| 7922 | const Type *SrcElTy = SrcPTy->getElementType(); |
| 7923 | |
Nate Begeman | 83ad90a | 2008-03-31 00:22:16 +0000 | [diff] [blame] | 7924 | // If the address spaces don't match, don't eliminate the bitcast, which is |
| 7925 | // required for changing types. |
| 7926 | if (SrcPTy->getAddressSpace() != DstPTy->getAddressSpace()) |
| 7927 | return 0; |
| 7928 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7929 | // If we are casting a malloc or alloca to a pointer to a type of the same |
| 7930 | // size, rewrite the allocation instruction to allocate the "right" type. |
| 7931 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) |
| 7932 | if (Instruction *V = PromoteCastOfAllocation(CI, *AI)) |
| 7933 | return V; |
| 7934 | |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 7935 | // If the source and destination are pointers, and this cast is equivalent |
| 7936 | // to a getelementptr X, 0, 0, 0... turn it into the appropriate gep. |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7937 | // This can enhance SROA and other transforms that want type-safe pointers. |
| 7938 | Constant *ZeroUInt = Constant::getNullValue(Type::Int32Ty); |
| 7939 | unsigned NumZeros = 0; |
| 7940 | while (SrcElTy != DstElTy && |
| 7941 | isa<CompositeType>(SrcElTy) && !isa<PointerType>(SrcElTy) && |
| 7942 | SrcElTy->getNumContainedTypes() /* not "{}" */) { |
| 7943 | SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt); |
| 7944 | ++NumZeros; |
| 7945 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 7946 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7947 | // If we found a path from the src to dest, create the getelementptr now. |
| 7948 | if (SrcElTy == DstElTy) { |
| 7949 | SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7950 | return GetElementPtrInst::Create(Src, Idxs.begin(), Idxs.end(), "", |
| 7951 | ((Instruction*) NULL)); |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 7952 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7953 | } |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 7954 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7955 | if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) { |
| 7956 | if (SVI->hasOneUse()) { |
| 7957 | // Okay, we have (bitconvert (shuffle ..)). Check to see if this is |
| 7958 | // a bitconvert to a vector with the same # elts. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 7959 | if (isa<VectorType>(DestTy) && |
| 7960 | cast<VectorType>(DestTy)->getNumElements() == |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7961 | SVI->getType()->getNumElements()) { |
| 7962 | CastInst *Tmp; |
| 7963 | // If either of the operands is a cast from CI.getType(), then |
| 7964 | // evaluating the shuffle in the casted destination's type will allow |
| 7965 | // us to eliminate at least one cast. |
| 7966 | if (((Tmp = dyn_cast<CastInst>(SVI->getOperand(0))) && |
| 7967 | Tmp->getOperand(0)->getType() == DestTy) || |
| 7968 | ((Tmp = dyn_cast<CastInst>(SVI->getOperand(1))) && |
| 7969 | Tmp->getOperand(0)->getType() == DestTy)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7970 | Value *LHS = InsertOperandCastBefore(Instruction::BitCast, |
| 7971 | SVI->getOperand(0), DestTy, &CI); |
| 7972 | Value *RHS = InsertOperandCastBefore(Instruction::BitCast, |
| 7973 | SVI->getOperand(1), DestTy, &CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7974 | // Return a new shuffle vector. Use the same element ID's, as we |
| 7975 | // know the vector types match #elts. |
| 7976 | return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2)); |
Chris Lattner | 01575b7 | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 7977 | } |
| 7978 | } |
| 7979 | } |
| 7980 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 7981 | return 0; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 7982 | } |
| 7983 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7984 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
| 7985 | /// %C = or %A, %B |
| 7986 | /// %D = select %cond, %C, %A |
| 7987 | /// into: |
| 7988 | /// %C = select %cond, %B, 0 |
| 7989 | /// %D = or %A, %C |
| 7990 | /// |
| 7991 | /// Assuming that the specified instruction is an operand to the select, return |
| 7992 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 7993 | /// equal the other incoming value of the select. |
| 7994 | /// |
| 7995 | static unsigned GetSelectFoldableOperands(Instruction *I) { |
| 7996 | switch (I->getOpcode()) { |
| 7997 | case Instruction::Add: |
| 7998 | case Instruction::Mul: |
| 7999 | case Instruction::And: |
| 8000 | case Instruction::Or: |
| 8001 | case Instruction::Xor: |
| 8002 | return 3; // Can fold through either operand. |
| 8003 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 8004 | case Instruction::Shl: // Can only fold on the shift amount. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 8005 | case Instruction::LShr: |
| 8006 | case Instruction::AShr: |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8007 | return 1; |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8008 | default: |
| 8009 | return 0; // Cannot fold |
| 8010 | } |
| 8011 | } |
| 8012 | |
| 8013 | /// GetSelectFoldableConstant - For the same transformation as the previous |
| 8014 | /// function, return the identity constant that goes into the select. |
| 8015 | static Constant *GetSelectFoldableConstant(Instruction *I) { |
| 8016 | switch (I->getOpcode()) { |
| 8017 | default: assert(0 && "This cannot happen!"); abort(); |
| 8018 | case Instruction::Add: |
| 8019 | case Instruction::Sub: |
| 8020 | case Instruction::Or: |
| 8021 | case Instruction::Xor: |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8022 | case Instruction::Shl: |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 8023 | case Instruction::LShr: |
| 8024 | case Instruction::AShr: |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8025 | return Constant::getNullValue(I->getType()); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8026 | case Instruction::And: |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 8027 | return Constant::getAllOnesValue(I->getType()); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8028 | case Instruction::Mul: |
| 8029 | return ConstantInt::get(I->getType(), 1); |
| 8030 | } |
| 8031 | } |
| 8032 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8033 | /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI |
| 8034 | /// have the same opcode and only one use each. Try to simplify this. |
| 8035 | Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 8036 | Instruction *FI) { |
| 8037 | if (TI->getNumOperands() == 1) { |
| 8038 | // If this is a non-volatile load or a cast from the same type, |
| 8039 | // merge. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8040 | if (TI->isCast()) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8041 | if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType()) |
| 8042 | return 0; |
| 8043 | } else { |
| 8044 | return 0; // unknown unary op. |
| 8045 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8046 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8047 | // Fold this by inserting a select from the input values. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8048 | SelectInst *NewSI = SelectInst::Create(SI.getCondition(), TI->getOperand(0), |
| 8049 | FI->getOperand(0), SI.getName()+".v"); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8050 | InsertNewInstBefore(NewSI, SI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8051 | return CastInst::Create(Instruction::CastOps(TI->getOpcode()), NewSI, |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8052 | TI->getType()); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8053 | } |
| 8054 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8055 | // Only handle binary operators here. |
| 8056 | if (!isa<BinaryOperator>(TI)) |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8057 | return 0; |
| 8058 | |
| 8059 | // Figure out if the operations have any operands in common. |
| 8060 | Value *MatchOp, *OtherOpT, *OtherOpF; |
| 8061 | bool MatchIsOpZero; |
| 8062 | if (TI->getOperand(0) == FI->getOperand(0)) { |
| 8063 | MatchOp = TI->getOperand(0); |
| 8064 | OtherOpT = TI->getOperand(1); |
| 8065 | OtherOpF = FI->getOperand(1); |
| 8066 | MatchIsOpZero = true; |
| 8067 | } else if (TI->getOperand(1) == FI->getOperand(1)) { |
| 8068 | MatchOp = TI->getOperand(1); |
| 8069 | OtherOpT = TI->getOperand(0); |
| 8070 | OtherOpF = FI->getOperand(0); |
| 8071 | MatchIsOpZero = false; |
| 8072 | } else if (!TI->isCommutative()) { |
| 8073 | return 0; |
| 8074 | } else if (TI->getOperand(0) == FI->getOperand(1)) { |
| 8075 | MatchOp = TI->getOperand(0); |
| 8076 | OtherOpT = TI->getOperand(1); |
| 8077 | OtherOpF = FI->getOperand(0); |
| 8078 | MatchIsOpZero = true; |
| 8079 | } else if (TI->getOperand(1) == FI->getOperand(0)) { |
| 8080 | MatchOp = TI->getOperand(1); |
| 8081 | OtherOpT = TI->getOperand(0); |
| 8082 | OtherOpF = FI->getOperand(1); |
| 8083 | MatchIsOpZero = true; |
| 8084 | } else { |
| 8085 | return 0; |
| 8086 | } |
| 8087 | |
| 8088 | // If we reach here, they do have operations in common. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8089 | SelectInst *NewSI = SelectInst::Create(SI.getCondition(), OtherOpT, |
| 8090 | OtherOpF, SI.getName()+".v"); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8091 | InsertNewInstBefore(NewSI, SI); |
| 8092 | |
| 8093 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) { |
| 8094 | if (MatchIsOpZero) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8095 | return BinaryOperator::Create(BO->getOpcode(), MatchOp, NewSI); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8096 | else |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8097 | return BinaryOperator::Create(BO->getOpcode(), NewSI, MatchOp); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8098 | } |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 8099 | assert(0 && "Shouldn't get here"); |
| 8100 | return 0; |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8101 | } |
| 8102 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 8103 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 8104 | Value *CondVal = SI.getCondition(); |
| 8105 | Value *TrueVal = SI.getTrueValue(); |
| 8106 | Value *FalseVal = SI.getFalseValue(); |
| 8107 | |
| 8108 | // select true, X, Y -> X |
| 8109 | // select false, X, Y -> Y |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8110 | if (ConstantInt *C = dyn_cast<ConstantInt>(CondVal)) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 8111 | return ReplaceInstUsesWith(SI, C->getZExtValue() ? TrueVal : FalseVal); |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 8112 | |
| 8113 | // select C, X, X -> X |
| 8114 | if (TrueVal == FalseVal) |
| 8115 | return ReplaceInstUsesWith(SI, TrueVal); |
| 8116 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8117 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 8118 | return ReplaceInstUsesWith(SI, FalseVal); |
| 8119 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 8120 | return ReplaceInstUsesWith(SI, TrueVal); |
| 8121 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 8122 | if (isa<Constant>(TrueVal)) |
| 8123 | return ReplaceInstUsesWith(SI, TrueVal); |
| 8124 | else |
| 8125 | return ReplaceInstUsesWith(SI, FalseVal); |
| 8126 | } |
| 8127 | |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 8128 | if (SI.getType() == Type::Int1Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 8129 | if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 8130 | if (C->getZExtValue()) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8131 | // Change: A = select B, true, C --> A = or B, C |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8132 | return BinaryOperator::CreateOr(CondVal, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8133 | } else { |
| 8134 | // Change: A = select B, false, C --> A = and !B, C |
| 8135 | Value *NotCond = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8136 | InsertNewInstBefore(BinaryOperator::CreateNot(CondVal, |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8137 | "not."+CondVal->getName()), SI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8138 | return BinaryOperator::CreateAnd(NotCond, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8139 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 8140 | } else if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 8141 | if (C->getZExtValue() == false) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8142 | // Change: A = select B, C, false --> A = and B, C |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8143 | return BinaryOperator::CreateAnd(CondVal, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8144 | } else { |
| 8145 | // Change: A = select B, C, true --> A = or !B, C |
| 8146 | Value *NotCond = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8147 | InsertNewInstBefore(BinaryOperator::CreateNot(CondVal, |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8148 | "not."+CondVal->getName()), SI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8149 | return BinaryOperator::CreateOr(NotCond, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8150 | } |
| 8151 | } |
Chris Lattner | cfa5975 | 2007-11-25 21:27:53 +0000 | [diff] [blame] | 8152 | |
| 8153 | // select a, b, a -> a&b |
| 8154 | // select a, a, b -> a|b |
| 8155 | if (CondVal == TrueVal) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8156 | return BinaryOperator::CreateOr(CondVal, FalseVal); |
Chris Lattner | cfa5975 | 2007-11-25 21:27:53 +0000 | [diff] [blame] | 8157 | else if (CondVal == FalseVal) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8158 | return BinaryOperator::CreateAnd(CondVal, TrueVal); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8159 | } |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8160 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 8161 | // Selecting between two integer constants? |
| 8162 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) |
| 8163 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 8164 | // select C, 1, 0 -> zext C to int |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8165 | if (FalseValC->isZero() && TrueValC->getValue() == 1) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8166 | return CastInst::Create(Instruction::ZExt, CondVal, SI.getType()); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8167 | } else if (TrueValC->isZero() && FalseValC->getValue() == 1) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 8168 | // select C, 0, 1 -> zext !C to int |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 8169 | Value *NotCond = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8170 | InsertNewInstBefore(BinaryOperator::CreateNot(CondVal, |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 8171 | "not."+CondVal->getName()), SI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8172 | return CastInst::Create(Instruction::ZExt, NotCond, SI.getType()); |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 8173 | } |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 8174 | |
| 8175 | // FIXME: Turn select 0/-1 and -1/0 into sext from condition! |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8176 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8177 | if (ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition())) { |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8178 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8179 | // (x <s 0) ? -1 : 0 -> ashr x, 31 |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8180 | if (TrueValC->isAllOnesValue() && FalseValC->isZero()) |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8181 | if (ConstantInt *CmpCst = dyn_cast<ConstantInt>(IC->getOperand(1))) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 8182 | if (IC->getPredicate() == ICmpInst::ICMP_SLT && CmpCst->isZero()) { |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8183 | // The comparison constant and the result are not neccessarily the |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8184 | // same width. Make an all-ones value by inserting a AShr. |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8185 | Value *X = IC->getOperand(0); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 8186 | uint32_t Bits = X->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8187 | Constant *ShAmt = ConstantInt::get(X->getType(), Bits-1); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8188 | Instruction *SRA = BinaryOperator::Create(Instruction::AShr, X, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8189 | ShAmt, "ones"); |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8190 | InsertNewInstBefore(SRA, SI); |
| 8191 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8192 | // Finally, convert to the type of the select RHS. We figure out |
| 8193 | // if this requires a SExt, Trunc or BitCast based on the sizes. |
| 8194 | Instruction::CastOps opc = Instruction::BitCast; |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 8195 | uint32_t SRASize = SRA->getType()->getPrimitiveSizeInBits(); |
| 8196 | uint32_t SISize = SI.getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8197 | if (SRASize < SISize) |
| 8198 | opc = Instruction::SExt; |
| 8199 | else if (SRASize > SISize) |
| 8200 | opc = Instruction::Trunc; |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8201 | return CastInst::Create(opc, SRA, SI.getType()); |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8202 | } |
| 8203 | } |
| 8204 | |
| 8205 | |
| 8206 | // If one of the constants is zero (we know they can't both be) and we |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 8207 | // have an icmp instruction with zero, and we have an 'and' with the |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8208 | // non-constant value, eliminate this whole mess. This corresponds to |
| 8209 | // cases like this: ((X & 27) ? 27 : 0) |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8210 | if (TrueValC->isZero() || FalseValC->isZero()) |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 8211 | if (IC->isEquality() && isa<ConstantInt>(IC->getOperand(1)) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8212 | cast<Constant>(IC->getOperand(1))->isNullValue()) |
| 8213 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) |
| 8214 | if (ICA->getOpcode() == Instruction::And && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8215 | isa<ConstantInt>(ICA->getOperand(1)) && |
| 8216 | (ICA->getOperand(1) == TrueValC || |
| 8217 | ICA->getOperand(1) == FalseValC) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8218 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { |
| 8219 | // Okay, now we know that everything is set up, we just don't |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8220 | // know whether we have a icmp_ne or icmp_eq and whether the |
| 8221 | // true or false val is the zero. |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8222 | bool ShouldNotVal = !TrueValC->isZero(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8223 | ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE; |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8224 | Value *V = ICA; |
| 8225 | if (ShouldNotVal) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8226 | V = InsertNewInstBefore(BinaryOperator::Create( |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8227 | Instruction::Xor, V, ICA->getOperand(1)), SI); |
| 8228 | return ReplaceInstUsesWith(SI, V); |
| 8229 | } |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8230 | } |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 8231 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8232 | |
| 8233 | // See if we are selecting two values based on a comparison of the two values. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8234 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) { |
| 8235 | if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) { |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8236 | // Transform (X == Y) ? X : Y -> Y |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 8237 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
| 8238 | // This is not safe in general for floating point: |
| 8239 | // consider X== -0, Y== +0. |
| 8240 | // It becomes safe if either operand is a nonzero constant. |
| 8241 | ConstantFP *CFPt, *CFPf; |
| 8242 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 8243 | !CFPt->getValueAPF().isZero()) || |
| 8244 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 8245 | !CFPf->getValueAPF().isZero())) |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8246 | return ReplaceInstUsesWith(SI, FalseVal); |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 8247 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8248 | // Transform (X != Y) ? X : Y -> X |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8249 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8250 | return ReplaceInstUsesWith(SI, TrueVal); |
| 8251 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 8252 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8253 | } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){ |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8254 | // Transform (X == Y) ? Y : X -> X |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 8255 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
| 8256 | // This is not safe in general for floating point: |
| 8257 | // consider X== -0, Y== +0. |
| 8258 | // It becomes safe if either operand is a nonzero constant. |
| 8259 | ConstantFP *CFPt, *CFPf; |
| 8260 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 8261 | !CFPt->getValueAPF().isZero()) || |
| 8262 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 8263 | !CFPf->getValueAPF().isZero())) |
| 8264 | return ReplaceInstUsesWith(SI, FalseVal); |
| 8265 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8266 | // Transform (X != Y) ? Y : X -> Y |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8267 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
| 8268 | return ReplaceInstUsesWith(SI, TrueVal); |
| 8269 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 8270 | } |
| 8271 | } |
| 8272 | |
| 8273 | // See if we are selecting two values based on a comparison of the two values. |
| 8274 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) { |
| 8275 | if (ICI->getOperand(0) == TrueVal && ICI->getOperand(1) == FalseVal) { |
| 8276 | // Transform (X == Y) ? X : Y -> Y |
| 8277 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) |
| 8278 | return ReplaceInstUsesWith(SI, FalseVal); |
| 8279 | // Transform (X != Y) ? X : Y -> X |
| 8280 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) |
| 8281 | return ReplaceInstUsesWith(SI, TrueVal); |
| 8282 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 8283 | |
| 8284 | } else if (ICI->getOperand(0) == FalseVal && ICI->getOperand(1) == TrueVal){ |
| 8285 | // Transform (X == Y) ? Y : X -> X |
| 8286 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) |
| 8287 | return ReplaceInstUsesWith(SI, FalseVal); |
| 8288 | // Transform (X != Y) ? Y : X -> Y |
| 8289 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) |
Chris Lattner | fbede52 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 8290 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8291 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 8292 | } |
| 8293 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8294 | |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8295 | if (Instruction *TI = dyn_cast<Instruction>(TrueVal)) |
| 8296 | if (Instruction *FI = dyn_cast<Instruction>(FalseVal)) |
| 8297 | if (TI->hasOneUse() && FI->hasOneUse()) { |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8298 | Instruction *AddOp = 0, *SubOp = 0; |
| 8299 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8300 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) |
| 8301 | if (TI->getOpcode() == FI->getOpcode()) |
| 8302 | if (Instruction *IV = FoldSelectOpOp(SI, TI, FI)) |
| 8303 | return IV; |
| 8304 | |
| 8305 | // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is |
| 8306 | // even legal for FP. |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8307 | if (TI->getOpcode() == Instruction::Sub && |
| 8308 | FI->getOpcode() == Instruction::Add) { |
| 8309 | AddOp = FI; SubOp = TI; |
| 8310 | } else if (FI->getOpcode() == Instruction::Sub && |
| 8311 | TI->getOpcode() == Instruction::Add) { |
| 8312 | AddOp = TI; SubOp = FI; |
| 8313 | } |
| 8314 | |
| 8315 | if (AddOp) { |
| 8316 | Value *OtherAddOp = 0; |
| 8317 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { |
| 8318 | OtherAddOp = AddOp->getOperand(1); |
| 8319 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { |
| 8320 | OtherAddOp = AddOp->getOperand(0); |
| 8321 | } |
| 8322 | |
| 8323 | if (OtherAddOp) { |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 8324 | // So at this point we know we have (Y -> OtherAddOp): |
| 8325 | // select C, (add X, Y), (sub X, Z) |
| 8326 | Value *NegVal; // Compute -Z |
| 8327 | if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) { |
| 8328 | NegVal = ConstantExpr::getNeg(C); |
| 8329 | } else { |
| 8330 | NegVal = InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8331 | BinaryOperator::CreateNeg(SubOp->getOperand(1), "tmp"), SI); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8332 | } |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 8333 | |
| 8334 | Value *NewTrueOp = OtherAddOp; |
| 8335 | Value *NewFalseOp = NegVal; |
| 8336 | if (AddOp != TI) |
| 8337 | std::swap(NewTrueOp, NewFalseOp); |
| 8338 | Instruction *NewSel = |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 8339 | SelectInst::Create(CondVal, NewTrueOp, |
| 8340 | NewFalseOp, SI.getName() + ".p"); |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 8341 | |
| 8342 | NewSel = InsertNewInstBefore(NewSel, SI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8343 | return BinaryOperator::CreateAdd(SubOp->getOperand(0), NewSel); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8344 | } |
| 8345 | } |
| 8346 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8347 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8348 | // See if we can fold the select into one of our operands. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 8349 | if (SI.getType()->isInteger()) { |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8350 | // See the comment above GetSelectFoldableOperands for a description of the |
| 8351 | // transformation we are doing here. |
| 8352 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) |
| 8353 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && |
| 8354 | !isa<Constant>(FalseVal)) |
| 8355 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { |
| 8356 | unsigned OpToFold = 0; |
| 8357 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 8358 | OpToFold = 1; |
| 8359 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
| 8360 | OpToFold = 2; |
| 8361 | } |
| 8362 | |
| 8363 | if (OpToFold) { |
| 8364 | Constant *C = GetSelectFoldableConstant(TVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8365 | Instruction *NewSel = |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 8366 | SelectInst::Create(SI.getCondition(), |
| 8367 | TVI->getOperand(2-OpToFold), C); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8368 | InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8369 | NewSel->takeName(TVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8370 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8371 | return BinaryOperator::Create(BO->getOpcode(), FalseVal, NewSel); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8372 | else { |
| 8373 | assert(0 && "Unknown instruction!!"); |
| 8374 | } |
| 8375 | } |
| 8376 | } |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 8377 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8378 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) |
| 8379 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && |
| 8380 | !isa<Constant>(TrueVal)) |
| 8381 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { |
| 8382 | unsigned OpToFold = 0; |
| 8383 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 8384 | OpToFold = 1; |
| 8385 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
| 8386 | OpToFold = 2; |
| 8387 | } |
| 8388 | |
| 8389 | if (OpToFold) { |
| 8390 | Constant *C = GetSelectFoldableConstant(FVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8391 | Instruction *NewSel = |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 8392 | SelectInst::Create(SI.getCondition(), C, |
| 8393 | FVI->getOperand(2-OpToFold)); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8394 | InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8395 | NewSel->takeName(FVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8396 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8397 | return BinaryOperator::Create(BO->getOpcode(), TrueVal, NewSel); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8398 | else |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8399 | assert(0 && "Unknown instruction!!"); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8400 | } |
| 8401 | } |
| 8402 | } |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 8403 | |
| 8404 | if (BinaryOperator::isNot(CondVal)) { |
| 8405 | SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); |
| 8406 | SI.setOperand(1, FalseVal); |
| 8407 | SI.setOperand(2, TrueVal); |
| 8408 | return &SI; |
| 8409 | } |
| 8410 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 8411 | return 0; |
| 8412 | } |
| 8413 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8414 | /// EnforceKnownAlignment - If the specified pointer points to an object that |
| 8415 | /// we control, modify the object's alignment to PrefAlign. This isn't |
| 8416 | /// often possible though. If alignment is important, a more reliable approach |
| 8417 | /// is to simply align all global variables and allocation instructions to |
| 8418 | /// their preferred alignment from the beginning. |
| 8419 | /// |
| 8420 | static unsigned EnforceKnownAlignment(Value *V, |
| 8421 | unsigned Align, unsigned PrefAlign) { |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8422 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8423 | User *U = dyn_cast<User>(V); |
| 8424 | if (!U) return Align; |
| 8425 | |
| 8426 | switch (getOpcode(U)) { |
| 8427 | default: break; |
| 8428 | case Instruction::BitCast: |
| 8429 | return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); |
| 8430 | case Instruction::GetElementPtr: { |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8431 | // If all indexes are zero, it is just the alignment of the base pointer. |
| 8432 | bool AllZeroOperands = true; |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8433 | for (unsigned i = 1, e = U->getNumOperands(); i != e; ++i) |
| 8434 | if (!isa<Constant>(U->getOperand(i)) || |
| 8435 | !cast<Constant>(U->getOperand(i))->isNullValue()) { |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8436 | AllZeroOperands = false; |
| 8437 | break; |
| 8438 | } |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8439 | |
| 8440 | if (AllZeroOperands) { |
| 8441 | // Treat this like a bitcast. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8442 | return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8443 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8444 | break; |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8445 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8446 | } |
| 8447 | |
| 8448 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 8449 | // If there is a large requested alignment and we can, bump up the alignment |
| 8450 | // of the global. |
| 8451 | if (!GV->isDeclaration()) { |
| 8452 | GV->setAlignment(PrefAlign); |
| 8453 | Align = PrefAlign; |
| 8454 | } |
| 8455 | } else if (AllocationInst *AI = dyn_cast<AllocationInst>(V)) { |
| 8456 | // If there is a requested alignment and if this is an alloca, round up. We |
| 8457 | // don't do this for malloc, because some systems can't respect the request. |
| 8458 | if (isa<AllocaInst>(AI)) { |
| 8459 | AI->setAlignment(PrefAlign); |
| 8460 | Align = PrefAlign; |
| 8461 | } |
| 8462 | } |
| 8463 | |
| 8464 | return Align; |
| 8465 | } |
| 8466 | |
| 8467 | /// GetOrEnforceKnownAlignment - If the specified pointer has an alignment that |
| 8468 | /// we can determine, return it, otherwise return 0. If PrefAlign is specified, |
| 8469 | /// and it is more than the alignment of the ultimate object, see if we can |
| 8470 | /// increase the alignment of the ultimate object, making this check succeed. |
| 8471 | unsigned InstCombiner::GetOrEnforceKnownAlignment(Value *V, |
| 8472 | unsigned PrefAlign) { |
| 8473 | unsigned BitWidth = TD ? TD->getTypeSizeInBits(V->getType()) : |
| 8474 | sizeof(PrefAlign) * CHAR_BIT; |
| 8475 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 8476 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 8477 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne); |
| 8478 | unsigned TrailZ = KnownZero.countTrailingOnes(); |
| 8479 | unsigned Align = 1u << std::min(BitWidth - 1, TrailZ); |
| 8480 | |
| 8481 | if (PrefAlign > Align) |
| 8482 | Align = EnforceKnownAlignment(V, Align, PrefAlign); |
| 8483 | |
| 8484 | // We don't need to make any adjustment. |
| 8485 | return Align; |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8486 | } |
| 8487 | |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8488 | Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) { |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8489 | unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(1)); |
| 8490 | unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(2)); |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8491 | unsigned MinAlign = std::min(DstAlign, SrcAlign); |
| 8492 | unsigned CopyAlign = MI->getAlignment()->getZExtValue(); |
| 8493 | |
| 8494 | if (CopyAlign < MinAlign) { |
| 8495 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, MinAlign)); |
| 8496 | return MI; |
| 8497 | } |
| 8498 | |
| 8499 | // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with |
| 8500 | // load/store. |
| 8501 | ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getOperand(3)); |
| 8502 | if (MemOpLength == 0) return 0; |
| 8503 | |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8504 | // Source and destination pointer types are always "i8*" for intrinsic. See |
| 8505 | // if the size is something we can handle with a single primitive load/store. |
| 8506 | // A single load+store correctly handles overlapping memory in the memmove |
| 8507 | // case. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8508 | unsigned Size = MemOpLength->getZExtValue(); |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 8509 | if (Size == 0) return MI; // Delete this mem transfer. |
| 8510 | |
| 8511 | if (Size > 8 || (Size&(Size-1))) |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8512 | return 0; // If not 1/2/4/8 bytes, exit. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8513 | |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8514 | // Use an integer load+store unless we can find something better. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8515 | Type *NewPtrTy = PointerType::getUnqual(IntegerType::get(Size<<3)); |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8516 | |
| 8517 | // Memcpy forces the use of i8* for the source and destination. That means |
| 8518 | // that if you're using memcpy to move one double around, you'll get a cast |
| 8519 | // from double* to i8*. We'd much rather use a double load+store rather than |
| 8520 | // an i64 load+store, here because this improves the odds that the source or |
| 8521 | // dest address will be promotable. See if we can find a better type than the |
| 8522 | // integer datatype. |
| 8523 | if (Value *Op = getBitCastOperand(MI->getOperand(1))) { |
| 8524 | const Type *SrcETy = cast<PointerType>(Op->getType())->getElementType(); |
| 8525 | if (SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) { |
| 8526 | // The SrcETy might be something like {{{double}}} or [1 x double]. Rip |
| 8527 | // down through these levels if so. |
| 8528 | while (!SrcETy->isFirstClassType()) { |
| 8529 | if (const StructType *STy = dyn_cast<StructType>(SrcETy)) { |
| 8530 | if (STy->getNumElements() == 1) |
| 8531 | SrcETy = STy->getElementType(0); |
| 8532 | else |
| 8533 | break; |
| 8534 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcETy)) { |
| 8535 | if (ATy->getNumElements() == 1) |
| 8536 | SrcETy = ATy->getElementType(); |
| 8537 | else |
| 8538 | break; |
| 8539 | } else |
| 8540 | break; |
| 8541 | } |
| 8542 | |
| 8543 | if (SrcETy->isFirstClassType()) |
| 8544 | NewPtrTy = PointerType::getUnqual(SrcETy); |
| 8545 | } |
| 8546 | } |
| 8547 | |
| 8548 | |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8549 | // If the memcpy/memmove provides better alignment info than we can |
| 8550 | // infer, use it. |
| 8551 | SrcAlign = std::max(SrcAlign, CopyAlign); |
| 8552 | DstAlign = std::max(DstAlign, CopyAlign); |
| 8553 | |
| 8554 | Value *Src = InsertBitCastBefore(MI->getOperand(2), NewPtrTy, *MI); |
| 8555 | Value *Dest = InsertBitCastBefore(MI->getOperand(1), NewPtrTy, *MI); |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8556 | Instruction *L = new LoadInst(Src, "tmp", false, SrcAlign); |
| 8557 | InsertNewInstBefore(L, *MI); |
| 8558 | InsertNewInstBefore(new StoreInst(L, Dest, false, DstAlign), *MI); |
| 8559 | |
| 8560 | // Set the size of the copy to 0, it will be deleted on the next iteration. |
| 8561 | MI->setOperand(3, Constant::getNullValue(MemOpLength->getType())); |
| 8562 | return MI; |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8563 | } |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 8564 | |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 8565 | Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) { |
| 8566 | unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest()); |
| 8567 | if (MI->getAlignment()->getZExtValue() < Alignment) { |
| 8568 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, Alignment)); |
| 8569 | return MI; |
| 8570 | } |
| 8571 | |
| 8572 | // Extract the length and alignment and fill if they are constant. |
| 8573 | ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength()); |
| 8574 | ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue()); |
| 8575 | if (!LenC || !FillC || FillC->getType() != Type::Int8Ty) |
| 8576 | return 0; |
| 8577 | uint64_t Len = LenC->getZExtValue(); |
| 8578 | Alignment = MI->getAlignment()->getZExtValue(); |
| 8579 | |
| 8580 | // If the length is zero, this is a no-op |
| 8581 | if (Len == 0) return MI; // memset(d,c,0,a) -> noop |
| 8582 | |
| 8583 | // memset(s,c,n) -> store s, c (for n=1,2,4,8) |
| 8584 | if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) { |
| 8585 | const Type *ITy = IntegerType::get(Len*8); // n=1 -> i8. |
| 8586 | |
| 8587 | Value *Dest = MI->getDest(); |
| 8588 | Dest = InsertBitCastBefore(Dest, PointerType::getUnqual(ITy), *MI); |
| 8589 | |
| 8590 | // Alignment 0 is identity for alignment 1 for memset, but not store. |
| 8591 | if (Alignment == 0) Alignment = 1; |
| 8592 | |
| 8593 | // Extract the fill value and store. |
| 8594 | uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL; |
| 8595 | InsertNewInstBefore(new StoreInst(ConstantInt::get(ITy, Fill), Dest, false, |
| 8596 | Alignment), *MI); |
| 8597 | |
| 8598 | // Set the size of the copy to 0, it will be deleted on the next iteration. |
| 8599 | MI->setLength(Constant::getNullValue(LenC->getType())); |
| 8600 | return MI; |
| 8601 | } |
| 8602 | |
| 8603 | return 0; |
| 8604 | } |
| 8605 | |
| 8606 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8607 | /// visitCallInst - CallInst simplification. This mostly only handles folding |
| 8608 | /// of intrinsic instructions. For normal calls, it allows visitCallSite to do |
| 8609 | /// the heavy lifting. |
| 8610 | /// |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8611 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8612 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI); |
| 8613 | if (!II) return visitCallSite(&CI); |
| 8614 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8615 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 8616 | // visitCallSite. |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8617 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8618 | bool Changed = false; |
| 8619 | |
| 8620 | // memmove/cpy/set of zero bytes is a noop. |
| 8621 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { |
| 8622 | if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); |
| 8623 | |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8624 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8625 | if (CI->getZExtValue() == 1) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8626 | // Replace the instruction with just byte operations. We would |
| 8627 | // transform other cases to loads/stores, but we don't know if |
| 8628 | // alignment is sufficient. |
| 8629 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8630 | } |
| 8631 | |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8632 | // If we have a memmove and the source operation is a constant global, |
| 8633 | // then the source and dest pointers can't alias, so we can change this |
| 8634 | // into a call to memcpy. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8635 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8636 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
| 8637 | if (GVSrc->isConstant()) { |
| 8638 | Module *M = CI.getParent()->getParent()->getParent(); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8639 | Intrinsic::ID MemCpyID; |
| 8640 | if (CI.getOperand(3)->getType() == Type::Int32Ty) |
| 8641 | MemCpyID = Intrinsic::memcpy_i32; |
Chris Lattner | 2195939 | 2006-03-03 01:34:17 +0000 | [diff] [blame] | 8642 | else |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8643 | MemCpyID = Intrinsic::memcpy_i64; |
| 8644 | CI.setOperand(0, Intrinsic::getDeclaration(M, MemCpyID)); |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8645 | Changed = true; |
| 8646 | } |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8647 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8648 | |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8649 | // If we can determine a pointer alignment that is bigger than currently |
| 8650 | // set, update the alignment. |
| 8651 | if (isa<MemCpyInst>(MI) || isa<MemMoveInst>(MI)) { |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8652 | if (Instruction *I = SimplifyMemTransfer(MI)) |
| 8653 | return I; |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 8654 | } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) { |
| 8655 | if (Instruction *I = SimplifyMemSet(MSI)) |
| 8656 | return I; |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8657 | } |
| 8658 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8659 | if (Changed) return II; |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8660 | } else { |
| 8661 | switch (II->getIntrinsicID()) { |
| 8662 | default: break; |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 8663 | case Intrinsic::ppc_altivec_lvx: |
| 8664 | case Intrinsic::ppc_altivec_lvxl: |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 8665 | case Intrinsic::x86_sse_loadu_ps: |
| 8666 | case Intrinsic::x86_sse2_loadu_pd: |
| 8667 | case Intrinsic::x86_sse2_loadu_dq: |
| 8668 | // Turn PPC lvx -> load if the pointer is known aligned. |
| 8669 | // Turn X86 loadups -> load if the pointer is known aligned. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8670 | if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) { |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8671 | Value *Ptr = InsertBitCastBefore(II->getOperand(1), |
| 8672 | PointerType::getUnqual(II->getType()), |
| 8673 | CI); |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 8674 | return new LoadInst(Ptr); |
| 8675 | } |
| 8676 | break; |
| 8677 | case Intrinsic::ppc_altivec_stvx: |
| 8678 | case Intrinsic::ppc_altivec_stvxl: |
| 8679 | // Turn stvx -> store if the pointer is known aligned. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8680 | if (GetOrEnforceKnownAlignment(II->getOperand(2), 16) >= 16) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8681 | const Type *OpPtrTy = |
| 8682 | PointerType::getUnqual(II->getOperand(1)->getType()); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8683 | Value *Ptr = InsertBitCastBefore(II->getOperand(2), OpPtrTy, CI); |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 8684 | return new StoreInst(II->getOperand(1), Ptr); |
| 8685 | } |
| 8686 | break; |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 8687 | case Intrinsic::x86_sse_storeu_ps: |
| 8688 | case Intrinsic::x86_sse2_storeu_pd: |
| 8689 | case Intrinsic::x86_sse2_storeu_dq: |
| 8690 | case Intrinsic::x86_sse2_storel_dq: |
| 8691 | // Turn X86 storeu -> store if the pointer is known aligned. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8692 | if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8693 | const Type *OpPtrTy = |
| 8694 | PointerType::getUnqual(II->getOperand(2)->getType()); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8695 | Value *Ptr = InsertBitCastBefore(II->getOperand(1), OpPtrTy, CI); |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 8696 | return new StoreInst(II->getOperand(2), Ptr); |
| 8697 | } |
| 8698 | break; |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8699 | |
| 8700 | case Intrinsic::x86_sse_cvttss2si: { |
| 8701 | // These intrinsics only demands the 0th element of its input vector. If |
| 8702 | // we can simplify the input based on that, do so now. |
| 8703 | uint64_t UndefElts; |
| 8704 | if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), 1, |
| 8705 | UndefElts)) { |
| 8706 | II->setOperand(1, V); |
| 8707 | return II; |
| 8708 | } |
| 8709 | break; |
| 8710 | } |
| 8711 | |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8712 | case Intrinsic::ppc_altivec_vperm: |
| 8713 | // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8714 | if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(3))) { |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8715 | assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!"); |
| 8716 | |
| 8717 | // Check that all of the elements are integer constants or undefs. |
| 8718 | bool AllEltsOk = true; |
| 8719 | for (unsigned i = 0; i != 16; ++i) { |
| 8720 | if (!isa<ConstantInt>(Mask->getOperand(i)) && |
| 8721 | !isa<UndefValue>(Mask->getOperand(i))) { |
| 8722 | AllEltsOk = false; |
| 8723 | break; |
| 8724 | } |
| 8725 | } |
| 8726 | |
| 8727 | if (AllEltsOk) { |
| 8728 | // Cast the input vectors to byte vectors. |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8729 | Value *Op0 =InsertBitCastBefore(II->getOperand(1),Mask->getType(),CI); |
| 8730 | Value *Op1 =InsertBitCastBefore(II->getOperand(2),Mask->getType(),CI); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8731 | Value *Result = UndefValue::get(Op0->getType()); |
| 8732 | |
| 8733 | // Only extract each element once. |
| 8734 | Value *ExtractedElts[32]; |
| 8735 | memset(ExtractedElts, 0, sizeof(ExtractedElts)); |
| 8736 | |
| 8737 | for (unsigned i = 0; i != 16; ++i) { |
| 8738 | if (isa<UndefValue>(Mask->getOperand(i))) |
| 8739 | continue; |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 8740 | unsigned Idx=cast<ConstantInt>(Mask->getOperand(i))->getZExtValue(); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8741 | Idx &= 31; // Match the hardware behavior. |
| 8742 | |
| 8743 | if (ExtractedElts[Idx] == 0) { |
| 8744 | Instruction *Elt = |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8745 | new ExtractElementInst(Idx < 16 ? Op0 : Op1, Idx&15, "tmp"); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8746 | InsertNewInstBefore(Elt, CI); |
| 8747 | ExtractedElts[Idx] = Elt; |
| 8748 | } |
| 8749 | |
| 8750 | // Insert this value into the result vector. |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 8751 | Result = InsertElementInst::Create(Result, ExtractedElts[Idx], |
| 8752 | i, "tmp"); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8753 | InsertNewInstBefore(cast<Instruction>(Result), CI); |
| 8754 | } |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8755 | return CastInst::Create(Instruction::BitCast, Result, CI.getType()); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8756 | } |
| 8757 | } |
| 8758 | break; |
| 8759 | |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8760 | case Intrinsic::stackrestore: { |
| 8761 | // If the save is right next to the restore, remove the restore. This can |
| 8762 | // happen when variable allocas are DCE'd. |
| 8763 | if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) { |
| 8764 | if (SS->getIntrinsicID() == Intrinsic::stacksave) { |
| 8765 | BasicBlock::iterator BI = SS; |
| 8766 | if (&*++BI == II) |
| 8767 | return EraseInstFromFunction(CI); |
| 8768 | } |
| 8769 | } |
| 8770 | |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8771 | // Scan down this block to see if there is another stack restore in the |
| 8772 | // same block without an intervening call/alloca. |
| 8773 | BasicBlock::iterator BI = II; |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8774 | TerminatorInst *TI = II->getParent()->getTerminator(); |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8775 | bool CannotRemove = false; |
| 8776 | for (++BI; &*BI != TI; ++BI) { |
| 8777 | if (isa<AllocaInst>(BI)) { |
| 8778 | CannotRemove = true; |
| 8779 | break; |
| 8780 | } |
| 8781 | if (isa<CallInst>(BI)) { |
| 8782 | if (!isa<IntrinsicInst>(BI)) { |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8783 | CannotRemove = true; |
| 8784 | break; |
| 8785 | } |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8786 | // If there is a stackrestore below this one, remove this one. |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8787 | return EraseInstFromFunction(CI); |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8788 | } |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8789 | } |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8790 | |
| 8791 | // If the stack restore is in a return/unwind block and if there are no |
| 8792 | // allocas or calls between the restore and the return, nuke the restore. |
| 8793 | if (!CannotRemove && (isa<ReturnInst>(TI) || isa<UnwindInst>(TI))) |
| 8794 | return EraseInstFromFunction(CI); |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8795 | break; |
| 8796 | } |
| 8797 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8798 | } |
| 8799 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8800 | return visitCallSite(II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8801 | } |
| 8802 | |
| 8803 | // InvokeInst simplification |
| 8804 | // |
| 8805 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8806 | return visitCallSite(&II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8807 | } |
| 8808 | |
Dale Johannesen | da30ccb | 2008-04-25 21:16:07 +0000 | [diff] [blame] | 8809 | /// isSafeToEliminateVarargsCast - If this cast does not affect the value |
| 8810 | /// passed through the varargs area, we can eliminate the use of the cast. |
Dale Johannesen | 1f530a5 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 8811 | static bool isSafeToEliminateVarargsCast(const CallSite CS, |
| 8812 | const CastInst * const CI, |
| 8813 | const TargetData * const TD, |
| 8814 | const int ix) { |
| 8815 | if (!CI->isLosslessCast()) |
| 8816 | return false; |
| 8817 | |
| 8818 | // The size of ByVal arguments is derived from the type, so we |
| 8819 | // can't change to a type with a different size. If the size were |
| 8820 | // passed explicitly we could avoid this check. |
| 8821 | if (!CS.paramHasAttr(ix, ParamAttr::ByVal)) |
| 8822 | return true; |
| 8823 | |
| 8824 | const Type* SrcTy = |
| 8825 | cast<PointerType>(CI->getOperand(0)->getType())->getElementType(); |
| 8826 | const Type* DstTy = cast<PointerType>(CI->getType())->getElementType(); |
| 8827 | if (!SrcTy->isSized() || !DstTy->isSized()) |
| 8828 | return false; |
| 8829 | if (TD->getABITypeSize(SrcTy) != TD->getABITypeSize(DstTy)) |
| 8830 | return false; |
| 8831 | return true; |
| 8832 | } |
| 8833 | |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8834 | // visitCallSite - Improvements for call and invoke instructions. |
| 8835 | // |
| 8836 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8837 | bool Changed = false; |
| 8838 | |
| 8839 | // If the callee is a constexpr cast of a function, attempt to move the cast |
| 8840 | // to the arguments of the call/invoke. |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8841 | if (transformConstExprCastCall(CS)) return 0; |
| 8842 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8843 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8844 | |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 8845 | if (Function *CalleeF = dyn_cast<Function>(Callee)) |
| 8846 | if (CalleeF->getCallingConv() != CS.getCallingConv()) { |
| 8847 | Instruction *OldCall = CS.getInstruction(); |
| 8848 | // If the call and callee calling conventions don't match, this call must |
| 8849 | // be unreachable, as the call is undefined. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8850 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8851 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), |
| 8852 | OldCall); |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 8853 | if (!OldCall->use_empty()) |
| 8854 | OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); |
| 8855 | if (isa<CallInst>(OldCall)) // Not worth removing an invoke here. |
| 8856 | return EraseInstFromFunction(*OldCall); |
| 8857 | return 0; |
| 8858 | } |
| 8859 | |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8860 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
| 8861 | // This instruction is not reachable, just remove it. We insert a store to |
| 8862 | // undef so that we know that this code is not reachable, despite the fact |
| 8863 | // that we can't modify the CFG here. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8864 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8865 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8866 | CS.getInstruction()); |
| 8867 | |
| 8868 | if (!CS.getInstruction()->use_empty()) |
| 8869 | CS.getInstruction()-> |
| 8870 | replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); |
| 8871 | |
| 8872 | if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { |
| 8873 | // Don't break the CFG, insert a dummy cond branch. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8874 | BranchInst::Create(II->getNormalDest(), II->getUnwindDest(), |
| 8875 | ConstantInt::getTrue(), II); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8876 | } |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8877 | return EraseInstFromFunction(*CS.getInstruction()); |
| 8878 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8879 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8880 | if (BitCastInst *BC = dyn_cast<BitCastInst>(Callee)) |
| 8881 | if (IntrinsicInst *In = dyn_cast<IntrinsicInst>(BC->getOperand(0))) |
| 8882 | if (In->getIntrinsicID() == Intrinsic::init_trampoline) |
| 8883 | return transformCallThroughTrampoline(CS); |
| 8884 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8885 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 8886 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 8887 | if (FTy->isVarArg()) { |
Dale Johannesen | 63e7eb4 | 2008-04-23 01:03:05 +0000 | [diff] [blame] | 8888 | int ix = FTy->getNumParams() + (isa<InvokeInst>(Callee) ? 3 : 1); |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8889 | // See if we can optimize any arguments passed through the varargs area of |
| 8890 | // the call. |
| 8891 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
Dale Johannesen | 1f530a5 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 8892 | E = CS.arg_end(); I != E; ++I, ++ix) { |
| 8893 | CastInst *CI = dyn_cast<CastInst>(*I); |
| 8894 | if (CI && isSafeToEliminateVarargsCast(CS, CI, TD, ix)) { |
| 8895 | *I = CI->getOperand(0); |
| 8896 | Changed = true; |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8897 | } |
Dale Johannesen | 1f530a5 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 8898 | } |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8899 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8900 | |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 8901 | if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) { |
Duncan Sands | ece2c04 | 2007-12-16 15:51:49 +0000 | [diff] [blame] | 8902 | // Inline asm calls cannot throw - mark them 'nounwind'. |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 8903 | CS.setDoesNotThrow(); |
Duncan Sands | ece2c04 | 2007-12-16 15:51:49 +0000 | [diff] [blame] | 8904 | Changed = true; |
| 8905 | } |
| 8906 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8907 | return Changed ? CS.getInstruction() : 0; |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8908 | } |
| 8909 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8910 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 8911 | // attempt to move the cast to the arguments of the call/invoke. |
| 8912 | // |
| 8913 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 8914 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 8915 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8916 | if (CE->getOpcode() != Instruction::BitCast || |
| 8917 | !isa<Function>(CE->getOperand(0))) |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8918 | return false; |
Reid Spencer | 8863f18 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 8919 | Function *Callee = cast<Function>(CE->getOperand(0)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8920 | Instruction *Caller = CS.getInstruction(); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8921 | const PAListPtr &CallerPAL = CS.getParamAttrs(); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8922 | |
| 8923 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 8924 | // would cause a type conversion of one of our arguments, change this call to |
| 8925 | // be a direct call with arguments casted to the appropriate types. |
| 8926 | // |
| 8927 | const FunctionType *FT = Callee->getFunctionType(); |
| 8928 | const Type *OldRetTy = Caller->getType(); |
| 8929 | |
Devang Patel | 75e6f02 | 2008-03-11 18:04:06 +0000 | [diff] [blame] | 8930 | if (isa<StructType>(FT->getReturnType())) |
| 8931 | return false; // TODO: Handle multiple return values. |
| 8932 | |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8933 | // Check to see if we are changing the return type... |
| 8934 | if (OldRetTy != FT->getReturnType()) { |
Bill Wendling | a6c3112 | 2008-05-14 22:45:20 +0000 | [diff] [blame] | 8935 | if (Callee->isDeclaration() && |
Chris Lattner | 46013f4 | 2007-01-06 19:53:32 +0000 | [diff] [blame] | 8936 | // Conversion is ok if changing from pointer to int of same size. |
| 8937 | !(isa<PointerType>(FT->getReturnType()) && |
| 8938 | TD->getIntPtrType() == OldRetTy)) |
Chris Lattner | ec47992 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 8939 | return false; // Cannot transform this return value. |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8940 | |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8941 | if (!Caller->use_empty() && |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8942 | // void -> non-void is handled specially |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8943 | FT->getReturnType() != Type::VoidTy && |
| 8944 | !CastInst::isCastable(FT->getReturnType(), OldRetTy)) |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8945 | return false; // Cannot transform this return value. |
| 8946 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8947 | if (!CallerPAL.isEmpty() && !Caller->use_empty()) { |
| 8948 | ParameterAttributes RAttrs = CallerPAL.getParamAttrs(0); |
Duncan Sands | 6c3470e | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 8949 | if (RAttrs & ParamAttr::typeIncompatible(FT->getReturnType())) |
| 8950 | return false; // Attribute not compatible with transformed value. |
| 8951 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8952 | |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8953 | // If the callsite is an invoke instruction, and the return value is used by |
| 8954 | // a PHI node in a successor, we cannot change the return type of the call |
| 8955 | // because there is no place to put the cast instruction (without breaking |
| 8956 | // the critical edge). Bail out in this case. |
| 8957 | if (!Caller->use_empty()) |
| 8958 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 8959 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 8960 | UI != E; ++UI) |
| 8961 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 8962 | if (PN->getParent() == II->getNormalDest() || |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 8963 | PN->getParent() == II->getUnwindDest()) |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8964 | return false; |
| 8965 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8966 | |
| 8967 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 8968 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8969 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8970 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 8971 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 8972 | const Type *ParamTy = FT->getParamType(i); |
Andrew Lenharth | b8e604c | 2006-06-28 01:01:52 +0000 | [diff] [blame] | 8973 | const Type *ActTy = (*AI)->getType(); |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8974 | |
| 8975 | if (!CastInst::isCastable(ActTy, ParamTy)) |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8976 | return false; // Cannot transform this parameter value. |
| 8977 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8978 | if (CallerPAL.getParamAttrs(i + 1) & ParamAttr::typeIncompatible(ParamTy)) |
| 8979 | return false; // Attribute not compatible with transformed value. |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8980 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8981 | ConstantInt *c = dyn_cast<ConstantInt>(*AI); |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8982 | // Some conversions are safe even if we do not have a body. |
| 8983 | // Either we can cast directly, or we can upconvert the argument |
Chris Lattner | ec47992 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 8984 | bool isConvertible = ActTy == ParamTy || |
Chris Lattner | 46013f4 | 2007-01-06 19:53:32 +0000 | [diff] [blame] | 8985 | (isa<PointerType>(ParamTy) && isa<PointerType>(ActTy)) || |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 8986 | (ParamTy->isInteger() && ActTy->isInteger() && |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 8987 | ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits()) || |
| 8988 | (c && ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits() |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 8989 | && c->getValue().isStrictlyPositive()); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8990 | if (Callee->isDeclaration() && !isConvertible) return false; |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8991 | } |
| 8992 | |
| 8993 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8994 | Callee->isDeclaration()) |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8995 | return false; // Do not delete arguments unless we have a function body. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8996 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8997 | if (FT->getNumParams() < NumActualArgs && FT->isVarArg() && |
| 8998 | !CallerPAL.isEmpty()) |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8999 | // In this case we have more arguments than the new function type, but we |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 9000 | // won't be dropping them. Check that these extra arguments have attributes |
| 9001 | // that are compatible with being a vararg call argument. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9002 | for (unsigned i = CallerPAL.getNumSlots(); i; --i) { |
| 9003 | if (CallerPAL.getSlot(i - 1).Index <= FT->getNumParams()) |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 9004 | break; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9005 | ParameterAttributes PAttrs = CallerPAL.getSlot(i - 1).Attrs; |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 9006 | if (PAttrs & ParamAttr::VarArgsIncompatible) |
| 9007 | return false; |
| 9008 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9009 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9010 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 9011 | // inserting cast instructions as necessary... |
| 9012 | std::vector<Value*> Args; |
| 9013 | Args.reserve(NumActualArgs); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9014 | SmallVector<ParamAttrsWithIndex, 8> attrVec; |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9015 | attrVec.reserve(NumCommonArgs); |
| 9016 | |
| 9017 | // Get any return attributes. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9018 | ParameterAttributes RAttrs = CallerPAL.getParamAttrs(0); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9019 | |
| 9020 | // If the return value is not being used, the type may not be compatible |
| 9021 | // with the existing attributes. Wipe out any problematic attributes. |
Duncan Sands | 6c3470e | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 9022 | RAttrs &= ~ParamAttr::typeIncompatible(FT->getReturnType()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9023 | |
| 9024 | // Add the new return attributes. |
| 9025 | if (RAttrs) |
| 9026 | attrVec.push_back(ParamAttrsWithIndex::get(0, RAttrs)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9027 | |
| 9028 | AI = CS.arg_begin(); |
| 9029 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 9030 | const Type *ParamTy = FT->getParamType(i); |
| 9031 | if ((*AI)->getType() == ParamTy) { |
| 9032 | Args.push_back(*AI); |
| 9033 | } else { |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 9034 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9035 | false, ParamTy, false); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9036 | CastInst *NewCast = CastInst::Create(opcode, *AI, ParamTy, "tmp"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9037 | Args.push_back(InsertNewInstBefore(NewCast, *Caller)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9038 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9039 | |
| 9040 | // Add any parameter attributes. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9041 | if (ParameterAttributes PAttrs = CallerPAL.getParamAttrs(i + 1)) |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9042 | attrVec.push_back(ParamAttrsWithIndex::get(i + 1, PAttrs)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9043 | } |
| 9044 | |
| 9045 | // If the function takes more arguments than the call was taking, add them |
| 9046 | // now... |
| 9047 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 9048 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 9049 | |
| 9050 | // If we are removing arguments to the function, emit an obnoxious warning... |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9051 | if (FT->getNumParams() < NumActualArgs) { |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9052 | if (!FT->isVarArg()) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 9053 | cerr << "WARNING: While resolving call to function '" |
| 9054 | << Callee->getName() << "' arguments were dropped!\n"; |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9055 | } else { |
| 9056 | // Add all of the arguments in their promoted form to the arg list... |
| 9057 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 9058 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 9059 | if (PTy != (*AI)->getType()) { |
| 9060 | // Must promote to pass through va_arg area! |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9061 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, false, |
| 9062 | PTy, false); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9063 | Instruction *Cast = CastInst::Create(opcode, *AI, PTy, "tmp"); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9064 | InsertNewInstBefore(Cast, *Caller); |
| 9065 | Args.push_back(Cast); |
| 9066 | } else { |
| 9067 | Args.push_back(*AI); |
| 9068 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9069 | |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 9070 | // Add any parameter attributes. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9071 | if (ParameterAttributes PAttrs = CallerPAL.getParamAttrs(i + 1)) |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 9072 | attrVec.push_back(ParamAttrsWithIndex::get(i + 1, PAttrs)); |
| 9073 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9074 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9075 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9076 | |
| 9077 | if (FT->getReturnType() == Type::VoidTy) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 9078 | Caller->setName(""); // Void type should not have a name. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9079 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9080 | const PAListPtr &NewCallerPAL = PAListPtr::get(attrVec.begin(),attrVec.end()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9081 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9082 | Instruction *NC; |
| 9083 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9084 | NC = InvokeInst::Create(Callee, II->getNormalDest(), II->getUnwindDest(), |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 9085 | Args.begin(), Args.end(), |
| 9086 | Caller->getName(), Caller); |
Reid Spencer | ed3fa85 | 2007-07-30 19:53:57 +0000 | [diff] [blame] | 9087 | cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9088 | cast<InvokeInst>(NC)->setParamAttrs(NewCallerPAL); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9089 | } else { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9090 | NC = CallInst::Create(Callee, Args.begin(), Args.end(), |
| 9091 | Caller->getName(), Caller); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9092 | CallInst *CI = cast<CallInst>(Caller); |
| 9093 | if (CI->isTailCall()) |
Chris Lattner | a9e9211 | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 9094 | cast<CallInst>(NC)->setTailCall(); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9095 | cast<CallInst>(NC)->setCallingConv(CI->getCallingConv()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9096 | cast<CallInst>(NC)->setParamAttrs(NewCallerPAL); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9097 | } |
| 9098 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 9099 | // Insert a cast of the return type as necessary. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9100 | Value *NV = NC; |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 9101 | if (OldRetTy != NV->getType() && !Caller->use_empty()) { |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9102 | if (NV->getType() != Type::VoidTy) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9103 | Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false, |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 9104 | OldRetTy, false); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9105 | NV = NC = CastInst::Create(opcode, NC, OldRetTy, "tmp"); |
Chris Lattner | bb60904 | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 9106 | |
| 9107 | // If this is an invoke instruction, we should insert it after the first |
| 9108 | // non-phi, instruction in the normal successor block. |
| 9109 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 9110 | BasicBlock::iterator I = II->getNormalDest()->begin(); |
| 9111 | while (isa<PHINode>(I)) ++I; |
| 9112 | InsertNewInstBefore(NC, *I); |
| 9113 | } else { |
| 9114 | // Otherwise, it's a call, just insert cast right after the call instr |
| 9115 | InsertNewInstBefore(NC, *Caller); |
| 9116 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 9117 | AddUsersToWorkList(*Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9118 | } else { |
Chris Lattner | c30bda7 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 9119 | NV = UndefValue::get(Caller->getType()); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9120 | } |
| 9121 | } |
| 9122 | |
| 9123 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 9124 | Caller->replaceAllUsesWith(NV); |
Chris Lattner | f22a5c6 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 9125 | Caller->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9126 | RemoveFromWorkList(Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9127 | return true; |
| 9128 | } |
| 9129 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9130 | // transformCallThroughTrampoline - Turn a call to a function created by the |
| 9131 | // init_trampoline intrinsic into a direct call to the underlying function. |
| 9132 | // |
| 9133 | Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) { |
| 9134 | Value *Callee = CS.getCalledValue(); |
| 9135 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 9136 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9137 | const PAListPtr &Attrs = CS.getParamAttrs(); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9138 | |
| 9139 | // If the call already has the 'nest' attribute somewhere then give up - |
| 9140 | // otherwise 'nest' would occur twice after splicing in the chain. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9141 | if (Attrs.hasAttrSomewhere(ParamAttr::Nest)) |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9142 | return 0; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9143 | |
| 9144 | IntrinsicInst *Tramp = |
| 9145 | cast<IntrinsicInst>(cast<BitCastInst>(Callee)->getOperand(0)); |
| 9146 | |
Anton Korobeynikov | 0b12ecf | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 9147 | Function *NestF = cast<Function>(Tramp->getOperand(2)->stripPointerCasts()); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9148 | const PointerType *NestFPTy = cast<PointerType>(NestF->getType()); |
| 9149 | const FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType()); |
| 9150 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9151 | const PAListPtr &NestAttrs = NestF->getParamAttrs(); |
| 9152 | if (!NestAttrs.isEmpty()) { |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9153 | unsigned NestIdx = 1; |
| 9154 | const Type *NestTy = 0; |
Dale Johannesen | 0d51e7e | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 9155 | ParameterAttributes NestAttr = ParamAttr::None; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9156 | |
| 9157 | // Look for a parameter marked with the 'nest' attribute. |
| 9158 | for (FunctionType::param_iterator I = NestFTy->param_begin(), |
| 9159 | E = NestFTy->param_end(); I != E; ++NestIdx, ++I) |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9160 | if (NestAttrs.paramHasAttr(NestIdx, ParamAttr::Nest)) { |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9161 | // Record the parameter type and any other attributes. |
| 9162 | NestTy = *I; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9163 | NestAttr = NestAttrs.getParamAttrs(NestIdx); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9164 | break; |
| 9165 | } |
| 9166 | |
| 9167 | if (NestTy) { |
| 9168 | Instruction *Caller = CS.getInstruction(); |
| 9169 | std::vector<Value*> NewArgs; |
| 9170 | NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1); |
| 9171 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9172 | SmallVector<ParamAttrsWithIndex, 8> NewAttrs; |
| 9173 | NewAttrs.reserve(Attrs.getNumSlots() + 1); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9174 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9175 | // Insert the nest argument into the call argument list, which may |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9176 | // mean appending it. Likewise for attributes. |
| 9177 | |
| 9178 | // Add any function result attributes. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9179 | if (ParameterAttributes Attr = Attrs.getParamAttrs(0)) |
| 9180 | NewAttrs.push_back(ParamAttrsWithIndex::get(0, Attr)); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9181 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9182 | { |
| 9183 | unsigned Idx = 1; |
| 9184 | CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); |
| 9185 | do { |
| 9186 | if (Idx == NestIdx) { |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9187 | // Add the chain argument and attributes. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9188 | Value *NestVal = Tramp->getOperand(3); |
| 9189 | if (NestVal->getType() != NestTy) |
| 9190 | NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller); |
| 9191 | NewArgs.push_back(NestVal); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9192 | NewAttrs.push_back(ParamAttrsWithIndex::get(NestIdx, NestAttr)); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9193 | } |
| 9194 | |
| 9195 | if (I == E) |
| 9196 | break; |
| 9197 | |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9198 | // Add the original argument and attributes. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9199 | NewArgs.push_back(*I); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9200 | if (ParameterAttributes Attr = Attrs.getParamAttrs(Idx)) |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9201 | NewAttrs.push_back |
| 9202 | (ParamAttrsWithIndex::get(Idx + (Idx >= NestIdx), Attr)); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9203 | |
| 9204 | ++Idx, ++I; |
| 9205 | } while (1); |
| 9206 | } |
| 9207 | |
| 9208 | // The trampoline may have been bitcast to a bogus type (FTy). |
| 9209 | // Handle this by synthesizing a new function type, equal to FTy |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9210 | // with the chain parameter inserted. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9211 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9212 | std::vector<const Type*> NewTypes; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9213 | NewTypes.reserve(FTy->getNumParams()+1); |
| 9214 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9215 | // Insert the chain's type into the list of parameter types, which may |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9216 | // mean appending it. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9217 | { |
| 9218 | unsigned Idx = 1; |
| 9219 | FunctionType::param_iterator I = FTy->param_begin(), |
| 9220 | E = FTy->param_end(); |
| 9221 | |
| 9222 | do { |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9223 | if (Idx == NestIdx) |
| 9224 | // Add the chain's type. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9225 | NewTypes.push_back(NestTy); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9226 | |
| 9227 | if (I == E) |
| 9228 | break; |
| 9229 | |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9230 | // Add the original type. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9231 | NewTypes.push_back(*I); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9232 | |
| 9233 | ++Idx, ++I; |
| 9234 | } while (1); |
| 9235 | } |
| 9236 | |
| 9237 | // Replace the trampoline call with a direct call. Let the generic |
| 9238 | // code sort out any function type mismatches. |
| 9239 | FunctionType *NewFTy = |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9240 | FunctionType::get(FTy->getReturnType(), NewTypes, FTy->isVarArg()); |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 9241 | Constant *NewCallee = NestF->getType() == PointerType::getUnqual(NewFTy) ? |
| 9242 | NestF : ConstantExpr::getBitCast(NestF, PointerType::getUnqual(NewFTy)); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9243 | const PAListPtr &NewPAL = PAListPtr::get(NewAttrs.begin(),NewAttrs.end()); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9244 | |
| 9245 | Instruction *NewCaller; |
| 9246 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9247 | NewCaller = InvokeInst::Create(NewCallee, |
| 9248 | II->getNormalDest(), II->getUnwindDest(), |
| 9249 | NewArgs.begin(), NewArgs.end(), |
| 9250 | Caller->getName(), Caller); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9251 | cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9252 | cast<InvokeInst>(NewCaller)->setParamAttrs(NewPAL); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9253 | } else { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9254 | NewCaller = CallInst::Create(NewCallee, NewArgs.begin(), NewArgs.end(), |
| 9255 | Caller->getName(), Caller); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9256 | if (cast<CallInst>(Caller)->isTailCall()) |
| 9257 | cast<CallInst>(NewCaller)->setTailCall(); |
| 9258 | cast<CallInst>(NewCaller)-> |
| 9259 | setCallingConv(cast<CallInst>(Caller)->getCallingConv()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9260 | cast<CallInst>(NewCaller)->setParamAttrs(NewPAL); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9261 | } |
| 9262 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 9263 | Caller->replaceAllUsesWith(NewCaller); |
| 9264 | Caller->eraseFromParent(); |
| 9265 | RemoveFromWorkList(Caller); |
| 9266 | return 0; |
| 9267 | } |
| 9268 | } |
| 9269 | |
| 9270 | // Replace the trampoline call with a direct call. Since there is no 'nest' |
| 9271 | // parameter, there is no need to adjust the argument list. Let the generic |
| 9272 | // code sort out any function type mismatches. |
| 9273 | Constant *NewCallee = |
| 9274 | NestF->getType() == PTy ? NestF : ConstantExpr::getBitCast(NestF, PTy); |
| 9275 | CS.setCalledFunction(NewCallee); |
| 9276 | return CS.getInstruction(); |
| 9277 | } |
| 9278 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9279 | /// FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(c,d)] |
| 9280 | /// and if a/b/c/d and the add's all have a single use, turn this into two phi's |
| 9281 | /// and a single binop. |
| 9282 | Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI(PHINode &PN) { |
| 9283 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 9284 | assert(isa<BinaryOperator>(FirstInst) || isa<GetElementPtrInst>(FirstInst) || |
| 9285 | isa<CmpInst>(FirstInst)); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9286 | unsigned Opc = FirstInst->getOpcode(); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9287 | Value *LHSVal = FirstInst->getOperand(0); |
| 9288 | Value *RHSVal = FirstInst->getOperand(1); |
| 9289 | |
| 9290 | const Type *LHSType = LHSVal->getType(); |
| 9291 | const Type *RHSType = RHSVal->getType(); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9292 | |
| 9293 | // Scan to see if all operands are the same opcode, all have one use, and all |
| 9294 | // kill their operands (i.e. the operands have one use). |
Chris Lattner | a90a24c | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 9295 | for (unsigned i = 0; i != PN.getNumIncomingValues(); ++i) { |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9296 | Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i)); |
Chris Lattner | a90a24c | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 9297 | if (!I || I->getOpcode() != Opc || !I->hasOneUse() || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9298 | // Verify type of the LHS matches so we don't fold cmp's of different |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9299 | // types or GEP's with different index types. |
| 9300 | I->getOperand(0)->getType() != LHSType || |
| 9301 | I->getOperand(1)->getType() != RHSType) |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9302 | return 0; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9303 | |
| 9304 | // If they are CmpInst instructions, check their predicates |
| 9305 | if (Opc == Instruction::ICmp || Opc == Instruction::FCmp) |
| 9306 | if (cast<CmpInst>(I)->getPredicate() != |
| 9307 | cast<CmpInst>(FirstInst)->getPredicate()) |
| 9308 | return 0; |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9309 | |
| 9310 | // Keep track of which operand needs a phi node. |
| 9311 | if (I->getOperand(0) != LHSVal) LHSVal = 0; |
| 9312 | if (I->getOperand(1) != RHSVal) RHSVal = 0; |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9313 | } |
| 9314 | |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 9315 | // Otherwise, this is safe to transform, determine if it is profitable. |
| 9316 | |
| 9317 | // If this is a GEP, and if the index (not the pointer) needs a PHI, bail out. |
| 9318 | // Indexes are often folded into load/store instructions, so we don't want to |
| 9319 | // hide them behind a phi. |
| 9320 | if (isa<GetElementPtrInst>(FirstInst) && RHSVal == 0) |
| 9321 | return 0; |
| 9322 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9323 | Value *InLHS = FirstInst->getOperand(0); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9324 | Value *InRHS = FirstInst->getOperand(1); |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 9325 | PHINode *NewLHS = 0, *NewRHS = 0; |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9326 | if (LHSVal == 0) { |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 9327 | NewLHS = PHINode::Create(LHSType, |
| 9328 | FirstInst->getOperand(0)->getName() + ".pn"); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9329 | NewLHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 9330 | NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0)); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9331 | InsertNewInstBefore(NewLHS, PN); |
| 9332 | LHSVal = NewLHS; |
| 9333 | } |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9334 | |
| 9335 | if (RHSVal == 0) { |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 9336 | NewRHS = PHINode::Create(RHSType, |
| 9337 | FirstInst->getOperand(1)->getName() + ".pn"); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9338 | NewRHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 9339 | NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0)); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9340 | InsertNewInstBefore(NewRHS, PN); |
| 9341 | RHSVal = NewRHS; |
| 9342 | } |
| 9343 | |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9344 | // Add all operands to the new PHIs. |
| 9345 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 9346 | if (NewLHS) { |
| 9347 | Value *NewInLHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 9348 | NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i)); |
| 9349 | } |
| 9350 | if (NewRHS) { |
| 9351 | Value *NewInRHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(1); |
| 9352 | NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i)); |
| 9353 | } |
| 9354 | } |
| 9355 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9356 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9357 | return BinaryOperator::Create(BinOp->getOpcode(), LHSVal, RHSVal); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9358 | else if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9359 | return CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(), LHSVal, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9360 | RHSVal); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9361 | else { |
| 9362 | assert(isa<GetElementPtrInst>(FirstInst)); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9363 | return GetElementPtrInst::Create(LHSVal, RHSVal); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9364 | } |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9365 | } |
| 9366 | |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9367 | /// isSafeToSinkLoad - Return true if we know that it is safe sink the load out |
| 9368 | /// of the block that defines it. This means that it must be obvious the value |
| 9369 | /// of the load is not changed from the point of the load to the end of the |
| 9370 | /// block it is in. |
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 9371 | /// |
| 9372 | /// Finally, it is safe, but not profitable, to sink a load targetting a |
| 9373 | /// non-address-taken alloca. Doing so will cause us to not promote the alloca |
| 9374 | /// to a register. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9375 | static bool isSafeToSinkLoad(LoadInst *L) { |
| 9376 | BasicBlock::iterator BBI = L, E = L->getParent()->end(); |
| 9377 | |
| 9378 | for (++BBI; BBI != E; ++BBI) |
| 9379 | if (BBI->mayWriteToMemory()) |
| 9380 | return false; |
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 9381 | |
| 9382 | // Check for non-address taken alloca. If not address-taken already, it isn't |
| 9383 | // profitable to do this xform. |
| 9384 | if (AllocaInst *AI = dyn_cast<AllocaInst>(L->getOperand(0))) { |
| 9385 | bool isAddressTaken = false; |
| 9386 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); |
| 9387 | UI != E; ++UI) { |
| 9388 | if (isa<LoadInst>(UI)) continue; |
| 9389 | if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { |
| 9390 | // If storing TO the alloca, then the address isn't taken. |
| 9391 | if (SI->getOperand(1) == AI) continue; |
| 9392 | } |
| 9393 | isAddressTaken = true; |
| 9394 | break; |
| 9395 | } |
| 9396 | |
| 9397 | if (!isAddressTaken) |
| 9398 | return false; |
| 9399 | } |
| 9400 | |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9401 | return true; |
| 9402 | } |
| 9403 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9404 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9405 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 9406 | // operator and they all are only used by the PHI, PHI together their |
| 9407 | // inputs, and do the operation once, to the result of the PHI. |
| 9408 | Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { |
| 9409 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
| 9410 | |
| 9411 | // Scan the instruction, looking for input operations that can be folded away. |
| 9412 | // If all input operands to the phi are the same instruction (e.g. a cast from |
| 9413 | // the same type or "+42") we can pull the operation through the PHI, reducing |
| 9414 | // code size and simplifying code. |
| 9415 | Constant *ConstantOp = 0; |
| 9416 | const Type *CastSrcTy = 0; |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9417 | bool isVolatile = false; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9418 | if (isa<CastInst>(FirstInst)) { |
| 9419 | CastSrcTy = FirstInst->getOperand(0)->getType(); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 9420 | } else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9421 | // Can fold binop, compare or shift here if the RHS is a constant, |
| 9422 | // otherwise call FoldPHIArgBinOpIntoPHI. |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9423 | ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1)); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9424 | if (ConstantOp == 0) |
| 9425 | return FoldPHIArgBinOpIntoPHI(PN); |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9426 | } else if (LoadInst *LI = dyn_cast<LoadInst>(FirstInst)) { |
| 9427 | isVolatile = LI->isVolatile(); |
| 9428 | // We can't sink the load if the loaded value could be modified between the |
| 9429 | // load and the PHI. |
| 9430 | if (LI->getParent() != PN.getIncomingBlock(0) || |
| 9431 | !isSafeToSinkLoad(LI)) |
| 9432 | return 0; |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9433 | } else if (isa<GetElementPtrInst>(FirstInst)) { |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 9434 | if (FirstInst->getNumOperands() == 2) |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9435 | return FoldPHIArgBinOpIntoPHI(PN); |
| 9436 | // Can't handle general GEPs yet. |
| 9437 | return 0; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9438 | } else { |
| 9439 | return 0; // Cannot fold this operation. |
| 9440 | } |
| 9441 | |
| 9442 | // Check to see if all arguments are the same operation. |
| 9443 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 9444 | if (!isa<Instruction>(PN.getIncomingValue(i))) return 0; |
| 9445 | Instruction *I = cast<Instruction>(PN.getIncomingValue(i)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9446 | if (!I->hasOneUse() || !I->isSameOperationAs(FirstInst)) |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9447 | return 0; |
| 9448 | if (CastSrcTy) { |
| 9449 | if (I->getOperand(0)->getType() != CastSrcTy) |
| 9450 | return 0; // Cast operation must match. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9451 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9452 | // We can't sink the load if the loaded value could be modified between |
| 9453 | // the load and the PHI. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9454 | if (LI->isVolatile() != isVolatile || |
| 9455 | LI->getParent() != PN.getIncomingBlock(i) || |
| 9456 | !isSafeToSinkLoad(LI)) |
| 9457 | return 0; |
Chris Lattner | 40700fe | 2008-04-29 17:28:22 +0000 | [diff] [blame] | 9458 | |
| 9459 | // If the PHI is volatile and its block has multiple successors, sinking |
| 9460 | // it would remove a load of the volatile value from the path through the |
| 9461 | // other successor. |
| 9462 | if (isVolatile && |
| 9463 | LI->getParent()->getTerminator()->getNumSuccessors() != 1) |
| 9464 | return 0; |
| 9465 | |
| 9466 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9467 | } else if (I->getOperand(1) != ConstantOp) { |
| 9468 | return 0; |
| 9469 | } |
| 9470 | } |
| 9471 | |
| 9472 | // Okay, they are all the same operation. Create a new PHI node of the |
| 9473 | // correct type, and PHI together all of the LHS's of the instructions. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9474 | PHINode *NewPN = PHINode::Create(FirstInst->getOperand(0)->getType(), |
| 9475 | PN.getName()+".in"); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 9476 | NewPN->reserveOperandSpace(PN.getNumOperands()/2); |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 9477 | |
| 9478 | Value *InVal = FirstInst->getOperand(0); |
| 9479 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9480 | |
| 9481 | // Add all operands to the new PHI. |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 9482 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 9483 | Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 9484 | if (NewInVal != InVal) |
| 9485 | InVal = 0; |
| 9486 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); |
| 9487 | } |
| 9488 | |
| 9489 | Value *PhiVal; |
| 9490 | if (InVal) { |
| 9491 | // The new PHI unions all of the same values together. This is really |
| 9492 | // common, so we handle it intelligently here for compile-time speed. |
| 9493 | PhiVal = InVal; |
| 9494 | delete NewPN; |
| 9495 | } else { |
| 9496 | InsertNewInstBefore(NewPN, PN); |
| 9497 | PhiVal = NewPN; |
| 9498 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9499 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9500 | // Insert and return the new operation. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9501 | if (CastInst* FirstCI = dyn_cast<CastInst>(FirstInst)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9502 | return CastInst::Create(FirstCI->getOpcode(), PhiVal, PN.getType()); |
Chris Lattner | 54545ac | 2008-04-29 17:13:43 +0000 | [diff] [blame] | 9503 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9504 | return BinaryOperator::Create(BinOp->getOpcode(), PhiVal, ConstantOp); |
Chris Lattner | 54545ac | 2008-04-29 17:13:43 +0000 | [diff] [blame] | 9505 | if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9506 | return CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(), |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9507 | PhiVal, ConstantOp); |
Chris Lattner | 54545ac | 2008-04-29 17:13:43 +0000 | [diff] [blame] | 9508 | assert(isa<LoadInst>(FirstInst) && "Unknown operation"); |
| 9509 | |
| 9510 | // If this was a volatile load that we are merging, make sure to loop through |
| 9511 | // and mark all the input loads as non-volatile. If we don't do this, we will |
| 9512 | // insert a new volatile load and the old ones will not be deletable. |
| 9513 | if (isVolatile) |
| 9514 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 9515 | cast<LoadInst>(PN.getIncomingValue(i))->setVolatile(false); |
| 9516 | |
| 9517 | return new LoadInst(PhiVal, "", isVolatile); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9518 | } |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 9519 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9520 | /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle |
| 9521 | /// that is dead. |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 9522 | static bool DeadPHICycle(PHINode *PN, |
| 9523 | SmallPtrSet<PHINode*, 16> &PotentiallyDeadPHIs) { |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9524 | if (PN->use_empty()) return true; |
| 9525 | if (!PN->hasOneUse()) return false; |
| 9526 | |
| 9527 | // Remember this node, and if we find the cycle, return. |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 9528 | if (!PotentiallyDeadPHIs.insert(PN)) |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9529 | return true; |
Chris Lattner | 92103de | 2007-08-28 04:23:55 +0000 | [diff] [blame] | 9530 | |
| 9531 | // Don't scan crazily complex things. |
| 9532 | if (PotentiallyDeadPHIs.size() == 16) |
| 9533 | return false; |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9534 | |
| 9535 | if (PHINode *PU = dyn_cast<PHINode>(PN->use_back())) |
| 9536 | return DeadPHICycle(PU, PotentiallyDeadPHIs); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9537 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9538 | return false; |
| 9539 | } |
| 9540 | |
Chris Lattner | cf5008a | 2007-11-06 21:52:06 +0000 | [diff] [blame] | 9541 | /// PHIsEqualValue - Return true if this phi node is always equal to |
| 9542 | /// NonPhiInVal. This happens with mutually cyclic phi nodes like: |
| 9543 | /// z = some value; x = phi (y, z); y = phi (x, z) |
| 9544 | static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal, |
| 9545 | SmallPtrSet<PHINode*, 16> &ValueEqualPHIs) { |
| 9546 | // See if we already saw this PHI node. |
| 9547 | if (!ValueEqualPHIs.insert(PN)) |
| 9548 | return true; |
| 9549 | |
| 9550 | // Don't scan crazily complex things. |
| 9551 | if (ValueEqualPHIs.size() == 16) |
| 9552 | return false; |
| 9553 | |
| 9554 | // Scan the operands to see if they are either phi nodes or are equal to |
| 9555 | // the value. |
| 9556 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 9557 | Value *Op = PN->getIncomingValue(i); |
| 9558 | if (PHINode *OpPN = dyn_cast<PHINode>(Op)) { |
| 9559 | if (!PHIsEqualValue(OpPN, NonPhiInVal, ValueEqualPHIs)) |
| 9560 | return false; |
| 9561 | } else if (Op != NonPhiInVal) |
| 9562 | return false; |
| 9563 | } |
| 9564 | |
| 9565 | return true; |
| 9566 | } |
| 9567 | |
| 9568 | |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 9569 | // PHINode simplification |
| 9570 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9571 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Owen Anderson | b64ab87 | 2006-07-10 22:15:25 +0000 | [diff] [blame] | 9572 | // If LCSSA is around, don't mess with Phi nodes |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 9573 | if (MustPreserveLCSSA) return 0; |
Owen Anderson | d1b78a1 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 9574 | |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9575 | if (Value *V = PN.hasConstantValue()) |
| 9576 | return ReplaceInstUsesWith(PN, V); |
| 9577 | |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9578 | // If all PHI operands are the same operation, pull them through the PHI, |
| 9579 | // reducing code size. |
| 9580 | if (isa<Instruction>(PN.getIncomingValue(0)) && |
| 9581 | PN.getIncomingValue(0)->hasOneUse()) |
| 9582 | if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) |
| 9583 | return Result; |
| 9584 | |
| 9585 | // If this is a trivial cycle in the PHI node graph, remove it. Basically, if |
| 9586 | // this PHI only has a single use (a PHI), and if that PHI only has one use (a |
| 9587 | // PHI)... break the cycle. |
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 9588 | if (PN.hasOneUse()) { |
| 9589 | Instruction *PHIUser = cast<Instruction>(PN.use_back()); |
| 9590 | if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) { |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 9591 | SmallPtrSet<PHINode*, 16> PotentiallyDeadPHIs; |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9592 | PotentiallyDeadPHIs.insert(&PN); |
| 9593 | if (DeadPHICycle(PU, PotentiallyDeadPHIs)) |
| 9594 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 9595 | } |
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 9596 | |
| 9597 | // If this phi has a single use, and if that use just computes a value for |
| 9598 | // the next iteration of a loop, delete the phi. This occurs with unused |
| 9599 | // induction variables, e.g. "for (int j = 0; ; ++j);". Detecting this |
| 9600 | // common case here is good because the only other things that catch this |
| 9601 | // are induction variable analysis (sometimes) and ADCE, which is only run |
| 9602 | // late. |
| 9603 | if (PHIUser->hasOneUse() && |
| 9604 | (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) && |
| 9605 | PHIUser->use_back() == &PN) { |
| 9606 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 9607 | } |
| 9608 | } |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9609 | |
Chris Lattner | cf5008a | 2007-11-06 21:52:06 +0000 | [diff] [blame] | 9610 | // We sometimes end up with phi cycles that non-obviously end up being the |
| 9611 | // same value, for example: |
| 9612 | // z = some value; x = phi (y, z); y = phi (x, z) |
| 9613 | // where the phi nodes don't necessarily need to be in the same block. Do a |
| 9614 | // quick check to see if the PHI node only contains a single non-phi value, if |
| 9615 | // so, scan to see if the phi cycle is actually equal to that value. |
| 9616 | { |
| 9617 | unsigned InValNo = 0, NumOperandVals = PN.getNumIncomingValues(); |
| 9618 | // Scan for the first non-phi operand. |
| 9619 | while (InValNo != NumOperandVals && |
| 9620 | isa<PHINode>(PN.getIncomingValue(InValNo))) |
| 9621 | ++InValNo; |
| 9622 | |
| 9623 | if (InValNo != NumOperandVals) { |
| 9624 | Value *NonPhiInVal = PN.getOperand(InValNo); |
| 9625 | |
| 9626 | // Scan the rest of the operands to see if there are any conflicts, if so |
| 9627 | // there is no need to recursively scan other phis. |
| 9628 | for (++InValNo; InValNo != NumOperandVals; ++InValNo) { |
| 9629 | Value *OpVal = PN.getIncomingValue(InValNo); |
| 9630 | if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal)) |
| 9631 | break; |
| 9632 | } |
| 9633 | |
| 9634 | // If we scanned over all operands, then we have one unique value plus |
| 9635 | // phi values. Scan PHI nodes to see if they all merge in each other or |
| 9636 | // the value. |
| 9637 | if (InValNo == NumOperandVals) { |
| 9638 | SmallPtrSet<PHINode*, 16> ValueEqualPHIs; |
| 9639 | if (PHIsEqualValue(&PN, NonPhiInVal, ValueEqualPHIs)) |
| 9640 | return ReplaceInstUsesWith(PN, NonPhiInVal); |
| 9641 | } |
| 9642 | } |
| 9643 | } |
Chris Lattner | 60921c9 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 9644 | return 0; |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 9645 | } |
| 9646 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9647 | static Value *InsertCastToIntPtrTy(Value *V, const Type *DTy, |
| 9648 | Instruction *InsertPoint, |
| 9649 | InstCombiner *IC) { |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 9650 | unsigned PtrSize = DTy->getPrimitiveSizeInBits(); |
| 9651 | unsigned VTySize = V->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9652 | // We must cast correctly to the pointer type. Ensure that we |
| 9653 | // sign extend the integer value if it is smaller as this is |
| 9654 | // used for address computation. |
| 9655 | Instruction::CastOps opcode = |
| 9656 | (VTySize < PtrSize ? Instruction::SExt : |
| 9657 | (VTySize == PtrSize ? Instruction::BitCast : Instruction::Trunc)); |
| 9658 | return IC->InsertCastBefore(opcode, V, DTy, *InsertPoint); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9659 | } |
| 9660 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 9661 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9662 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9663 | Value *PtrOp = GEP.getOperand(0); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 9664 | // Is it 'getelementptr %P, i32 0' or 'getelementptr %P' |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9665 | // If so, eliminate the noop. |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9666 | if (GEP.getNumOperands() == 1) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9667 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9668 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9669 | if (isa<UndefValue>(GEP.getOperand(0))) |
| 9670 | return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType())); |
| 9671 | |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9672 | bool HasZeroPointerIndex = false; |
| 9673 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) |
| 9674 | HasZeroPointerIndex = C->isNullValue(); |
| 9675 | |
| 9676 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9677 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 9678 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9679 | // Eliminate unneeded casts for indices. |
| 9680 | bool MadeChange = false; |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9681 | |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9682 | gep_type_iterator GTI = gep_type_begin(GEP); |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9683 | for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI) { |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9684 | if (isa<SequentialType>(*GTI)) { |
| 9685 | if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) { |
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 9686 | if (CI->getOpcode() == Instruction::ZExt || |
| 9687 | CI->getOpcode() == Instruction::SExt) { |
| 9688 | const Type *SrcTy = CI->getOperand(0)->getType(); |
| 9689 | // We can eliminate a cast from i32 to i64 iff the target |
| 9690 | // is a 32-bit pointer target. |
| 9691 | if (SrcTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()) { |
| 9692 | MadeChange = true; |
| 9693 | GEP.setOperand(i, CI->getOperand(0)); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9694 | } |
| 9695 | } |
| 9696 | } |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9697 | // If we are using a wider index than needed for this platform, shrink it |
| 9698 | // to what we need. If the incoming value needs a cast instruction, |
| 9699 | // insert it. This explicit cast can make subsequent optimizations more |
| 9700 | // obvious. |
| 9701 | Value *Op = GEP.getOperand(i); |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9702 | if (TD->getTypeSizeInBits(Op->getType()) > TD->getPointerSizeInBits()) { |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 9703 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9704 | GEP.setOperand(i, ConstantExpr::getTrunc(C, TD->getIntPtrType())); |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 9705 | MadeChange = true; |
| 9706 | } else { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9707 | Op = InsertCastBefore(Instruction::Trunc, Op, TD->getIntPtrType(), |
| 9708 | GEP); |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9709 | GEP.setOperand(i, Op); |
| 9710 | MadeChange = true; |
| 9711 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9712 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9713 | } |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9714 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9715 | if (MadeChange) return &GEP; |
| 9716 | |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9717 | // If this GEP instruction doesn't move the pointer, and if the input operand |
| 9718 | // is a bitcast of another pointer, just replace the GEP with a bitcast of the |
| 9719 | // real input to the dest type. |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 9720 | if (GEP.hasAllZeroIndices()) { |
| 9721 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(GEP.getOperand(0))) { |
| 9722 | // If the bitcast is of an allocation, and the allocation will be |
| 9723 | // converted to match the type of the cast, don't touch this. |
| 9724 | if (isa<AllocationInst>(BCI->getOperand(0))) { |
| 9725 | // See if the bitcast simplifies, if so, don't nuke this GEP yet. |
Chris Lattner | a79dd43 | 2007-10-12 18:05:47 +0000 | [diff] [blame] | 9726 | if (Instruction *I = visitBitCast(*BCI)) { |
| 9727 | if (I != BCI) { |
| 9728 | I->takeName(BCI); |
| 9729 | BCI->getParent()->getInstList().insert(BCI, I); |
| 9730 | ReplaceInstUsesWith(*BCI, I); |
| 9731 | } |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 9732 | return &GEP; |
Chris Lattner | a79dd43 | 2007-10-12 18:05:47 +0000 | [diff] [blame] | 9733 | } |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 9734 | } |
| 9735 | return new BitCastInst(BCI->getOperand(0), GEP.getType()); |
| 9736 | } |
| 9737 | } |
| 9738 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9739 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 9740 | // is a getelementptr instruction, combine the indices of the two |
| 9741 | // getelementptr instructions into a single instruction. |
| 9742 | // |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 9743 | SmallVector<Value*, 8> SrcGEPOperands; |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 9744 | if (User *Src = dyn_castGetElementPtr(PtrOp)) |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 9745 | SrcGEPOperands.append(Src->op_begin(), Src->op_end()); |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 9746 | |
| 9747 | if (!SrcGEPOperands.empty()) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9748 | // Note that if our source is a gep chain itself that we wait for that |
| 9749 | // chain to be resolved before we perform this transformation. This |
| 9750 | // avoids us creating a TON of code in some cases. |
| 9751 | // |
| 9752 | if (isa<GetElementPtrInst>(SrcGEPOperands[0]) && |
| 9753 | cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2) |
| 9754 | return 0; // Wait until our source is folded to completion. |
| 9755 | |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 9756 | SmallVector<Value*, 8> Indices; |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9757 | |
| 9758 | // Find out whether the last index in the source GEP is a sequential idx. |
| 9759 | bool EndsWithSequential = false; |
| 9760 | for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)), |
| 9761 | E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I) |
Chris Lattner | be97b4e | 2004-05-08 22:41:42 +0000 | [diff] [blame] | 9762 | EndsWithSequential = !isa<StructType>(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9763 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9764 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9765 | if (EndsWithSequential) { |
Chris Lattner | decd081 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 9766 | // Replace: gep (gep %P, long B), long A, ... |
| 9767 | // With: T = long A+B; gep %P, T, ... |
| 9768 | // |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9769 | Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9770 | if (SO1 == Constant::getNullValue(SO1->getType())) { |
| 9771 | Sum = GO1; |
| 9772 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { |
| 9773 | Sum = SO1; |
| 9774 | } else { |
| 9775 | // If they aren't the same type, convert both to an integer of the |
| 9776 | // target's pointer size. |
| 9777 | if (SO1->getType() != GO1->getType()) { |
| 9778 | if (Constant *SO1C = dyn_cast<Constant>(SO1)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9779 | SO1 = ConstantExpr::getIntegerCast(SO1C, GO1->getType(), true); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9780 | } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9781 | GO1 = ConstantExpr::getIntegerCast(GO1C, SO1->getType(), true); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9782 | } else { |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9783 | unsigned PS = TD->getPointerSizeInBits(); |
| 9784 | if (TD->getTypeSizeInBits(SO1->getType()) == PS) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9785 | // Convert GO1 to SO1's type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9786 | GO1 = InsertCastToIntPtrTy(GO1, SO1->getType(), &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9787 | |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9788 | } else if (TD->getTypeSizeInBits(GO1->getType()) == PS) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9789 | // Convert SO1 to GO1's type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9790 | SO1 = InsertCastToIntPtrTy(SO1, GO1->getType(), &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9791 | } else { |
| 9792 | const Type *PT = TD->getIntPtrType(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9793 | SO1 = InsertCastToIntPtrTy(SO1, PT, &GEP, this); |
| 9794 | GO1 = InsertCastToIntPtrTy(GO1, PT, &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9795 | } |
| 9796 | } |
| 9797 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9798 | if (isa<Constant>(SO1) && isa<Constant>(GO1)) |
| 9799 | Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1)); |
| 9800 | else { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9801 | Sum = BinaryOperator::CreateAdd(SO1, GO1, PtrOp->getName()+".sum"); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 9802 | InsertNewInstBefore(cast<Instruction>(Sum), GEP); |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9803 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9804 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9805 | |
| 9806 | // Recycle the GEP we already have if possible. |
| 9807 | if (SrcGEPOperands.size() == 2) { |
| 9808 | GEP.setOperand(0, SrcGEPOperands[0]); |
| 9809 | GEP.setOperand(1, Sum); |
| 9810 | return &GEP; |
| 9811 | } else { |
| 9812 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 9813 | SrcGEPOperands.end()-1); |
| 9814 | Indices.push_back(Sum); |
| 9815 | Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end()); |
| 9816 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9817 | } else if (isa<Constant>(*GEP.idx_begin()) && |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9818 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9819 | SrcGEPOperands.size() != 1) { |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9820 | // Otherwise we can do the fold if the first index of the GEP is a zero |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 9821 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 9822 | SrcGEPOperands.end()); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9823 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
| 9824 | } |
| 9825 | |
| 9826 | if (!Indices.empty()) |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9827 | return GetElementPtrInst::Create(SrcGEPOperands[0], Indices.begin(), |
| 9828 | Indices.end(), GEP.getName()); |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9829 | |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9830 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) { |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9831 | // GEP of global variable. If all of the indices for this GEP are |
| 9832 | // constants, we can promote this to a constexpr instead of an instruction. |
| 9833 | |
| 9834 | // Scan for nonconstants... |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 9835 | SmallVector<Constant*, 8> Indices; |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9836 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
| 9837 | for (; I != E && isa<Constant>(*I); ++I) |
| 9838 | Indices.push_back(cast<Constant>(*I)); |
| 9839 | |
| 9840 | if (I == E) { // If they are all constants... |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 9841 | Constant *CE = ConstantExpr::getGetElementPtr(GV, |
| 9842 | &Indices[0],Indices.size()); |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9843 | |
| 9844 | // Replace all uses of the GEP with the new constexpr... |
| 9845 | return ReplaceInstUsesWith(GEP, CE); |
| 9846 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9847 | } else if (Value *X = getBitCastOperand(PtrOp)) { // Is the operand a cast? |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9848 | if (!isa<PointerType>(X->getType())) { |
| 9849 | // Not interesting. Source pointer must be a cast from pointer. |
| 9850 | } else if (HasZeroPointerIndex) { |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9851 | // transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... |
| 9852 | // into : GEP [10 x i8]* X, i32 0, ... |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9853 | // |
| 9854 | // This occurs when the program declares an array extern like "int X[];" |
| 9855 | // |
| 9856 | const PointerType *CPTy = cast<PointerType>(PtrOp->getType()); |
| 9857 | const PointerType *XTy = cast<PointerType>(X->getType()); |
| 9858 | if (const ArrayType *XATy = |
| 9859 | dyn_cast<ArrayType>(XTy->getElementType())) |
| 9860 | if (const ArrayType *CATy = |
| 9861 | dyn_cast<ArrayType>(CPTy->getElementType())) |
| 9862 | if (CATy->getElementType() == XATy->getElementType()) { |
| 9863 | // At this point, we know that the cast source type is a pointer |
| 9864 | // to an array of the same type as the destination pointer |
| 9865 | // array. Because the array type is never stepped over (there |
| 9866 | // is a leading zero) we can fold the cast into this GEP. |
| 9867 | GEP.setOperand(0, X); |
| 9868 | return &GEP; |
| 9869 | } |
| 9870 | } else if (GEP.getNumOperands() == 2) { |
| 9871 | // Transform things like: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9872 | // %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V |
| 9873 | // into: %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9874 | const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); |
| 9875 | const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType(); |
| 9876 | if (isa<ArrayType>(SrcElTy) && |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9877 | TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType()) == |
| 9878 | TD->getABITypeSize(ResElTy)) { |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9879 | Value *Idx[2]; |
| 9880 | Idx[0] = Constant::getNullValue(Type::Int32Ty); |
| 9881 | Idx[1] = GEP.getOperand(1); |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9882 | Value *V = InsertNewInstBefore( |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9883 | GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName()), GEP); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9884 | // V and GEP are both pointer types --> BitCast |
| 9885 | return new BitCastInst(V, GEP.getType()); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9886 | } |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9887 | |
| 9888 | // Transform things like: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9889 | // getelementptr i8* bitcast ([100 x double]* X to i8*), i32 %tmp |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9890 | // (where tmp = 8*tmp2) into: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9891 | // getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9892 | |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9893 | if (isa<ArrayType>(SrcElTy) && ResElTy == Type::Int8Ty) { |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9894 | uint64_t ArrayEltSize = |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9895 | TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType()); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9896 | |
| 9897 | // Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We |
| 9898 | // allow either a mul, shift, or constant here. |
| 9899 | Value *NewIdx = 0; |
| 9900 | ConstantInt *Scale = 0; |
| 9901 | if (ArrayEltSize == 1) { |
| 9902 | NewIdx = GEP.getOperand(1); |
| 9903 | Scale = ConstantInt::get(NewIdx->getType(), 1); |
| 9904 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) { |
Chris Lattner | 6e2f843 | 2005-09-14 17:32:56 +0000 | [diff] [blame] | 9905 | NewIdx = ConstantInt::get(CI->getType(), 1); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9906 | Scale = CI; |
| 9907 | } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){ |
| 9908 | if (Inst->getOpcode() == Instruction::Shl && |
| 9909 | isa<ConstantInt>(Inst->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 9910 | ConstantInt *ShAmt = cast<ConstantInt>(Inst->getOperand(1)); |
| 9911 | uint32_t ShAmtVal = ShAmt->getLimitedValue(64); |
| 9912 | Scale = ConstantInt::get(Inst->getType(), 1ULL << ShAmtVal); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9913 | NewIdx = Inst->getOperand(0); |
| 9914 | } else if (Inst->getOpcode() == Instruction::Mul && |
| 9915 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 9916 | Scale = cast<ConstantInt>(Inst->getOperand(1)); |
| 9917 | NewIdx = Inst->getOperand(0); |
| 9918 | } |
| 9919 | } |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9920 | |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9921 | // If the index will be to exactly the right offset with the scale taken |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9922 | // out, perform the transformation. Note, we don't know whether Scale is |
| 9923 | // signed or not. We'll use unsigned version of division/modulo |
| 9924 | // operation after making sure Scale doesn't have the sign bit set. |
| 9925 | if (Scale && Scale->getSExtValue() >= 0LL && |
| 9926 | Scale->getZExtValue() % ArrayEltSize == 0) { |
| 9927 | Scale = ConstantInt::get(Scale->getType(), |
| 9928 | Scale->getZExtValue() / ArrayEltSize); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9929 | if (Scale->getZExtValue() != 1) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9930 | Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(), |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9931 | false /*ZExt*/); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9932 | Instruction *Sc = BinaryOperator::CreateMul(NewIdx, C, "idxscale"); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9933 | NewIdx = InsertNewInstBefore(Sc, GEP); |
| 9934 | } |
| 9935 | |
| 9936 | // Insert the new GEP instruction. |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9937 | Value *Idx[2]; |
| 9938 | Idx[0] = Constant::getNullValue(Type::Int32Ty); |
| 9939 | Idx[1] = NewIdx; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9940 | Instruction *NewGEP = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9941 | GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9942 | NewGEP = InsertNewInstBefore(NewGEP, GEP); |
| 9943 | // The NewGEP must be pointer typed, so must the old one -> BitCast |
| 9944 | return new BitCastInst(NewGEP, GEP.getType()); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9945 | } |
| 9946 | } |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9947 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9948 | } |
| 9949 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9950 | return 0; |
| 9951 | } |
| 9952 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9953 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
| 9954 | // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1 |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9955 | if (AI.isArrayAllocation()) { // Check C != 1 |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9956 | if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) { |
| 9957 | const Type *NewTy = |
| 9958 | ArrayType::get(AI.getAllocatedType(), C->getZExtValue()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 9959 | AllocationInst *New = 0; |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9960 | |
| 9961 | // Create and insert the replacement instruction... |
| 9962 | if (isa<MallocInst>(AI)) |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 9963 | New = new MallocInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 9964 | else { |
| 9965 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 9966 | New = new AllocaInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 9967 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9968 | |
| 9969 | InsertNewInstBefore(New, AI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9970 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9971 | // Scan to the end of the allocation instructions, to skip over a block of |
| 9972 | // allocas if possible... |
| 9973 | // |
| 9974 | BasicBlock::iterator It = New; |
| 9975 | while (isa<AllocationInst>(*It)) ++It; |
| 9976 | |
| 9977 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 9978 | // insert our getelementptr instruction... |
| 9979 | // |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9980 | Value *NullIdx = Constant::getNullValue(Type::Int32Ty); |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9981 | Value *Idx[2]; |
| 9982 | Idx[0] = NullIdx; |
| 9983 | Idx[1] = NullIdx; |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9984 | Value *V = GetElementPtrInst::Create(New, Idx, Idx + 2, |
| 9985 | New->getName()+".sub", It); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9986 | |
| 9987 | // Now make everything use the getelementptr instead of the original |
| 9988 | // allocation. |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9989 | return ReplaceInstUsesWith(AI, V); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9990 | } else if (isa<UndefValue>(AI.getArraySize())) { |
| 9991 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9992 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9993 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9994 | |
| 9995 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. |
| 9996 | // Note that we only do this for alloca's, because malloc should allocate and |
| 9997 | // return a unique pointer, even for a zero byte allocation. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9998 | if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() && |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9999 | TD->getABITypeSize(AI.getAllocatedType()) == 0) |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 10000 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
| 10001 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 10002 | return 0; |
| 10003 | } |
| 10004 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 10005 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { |
| 10006 | Value *Op = FI.getOperand(0); |
| 10007 | |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 10008 | // free undef -> unreachable. |
| 10009 | if (isa<UndefValue>(Op)) { |
| 10010 | // Insert a new store to null because we cannot modify the CFG here. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 10011 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 10012 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), &FI); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 10013 | return EraseInstFromFunction(FI); |
| 10014 | } |
Chris Lattner | 6fe5541 | 2007-04-14 00:20:02 +0000 | [diff] [blame] | 10015 | |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 10016 | // If we have 'free null' delete the instruction. This can happen in stl code |
| 10017 | // when lots of inlining happens. |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 10018 | if (isa<ConstantPointerNull>(Op)) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 10019 | return EraseInstFromFunction(FI); |
Chris Lattner | 6fe5541 | 2007-04-14 00:20:02 +0000 | [diff] [blame] | 10020 | |
| 10021 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X |
| 10022 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op)) { |
| 10023 | FI.setOperand(0, CI->getOperand(0)); |
| 10024 | return &FI; |
| 10025 | } |
| 10026 | |
| 10027 | // Change free (gep X, 0,0,0,0) into free(X) |
| 10028 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { |
| 10029 | if (GEPI->hasAllZeroIndices()) { |
| 10030 | AddToWorkList(GEPI); |
| 10031 | FI.setOperand(0, GEPI->getOperand(0)); |
| 10032 | return &FI; |
| 10033 | } |
| 10034 | } |
| 10035 | |
| 10036 | // Change free(malloc) into nothing, if the malloc has a single use. |
| 10037 | if (MallocInst *MI = dyn_cast<MallocInst>(Op)) |
| 10038 | if (MI->hasOneUse()) { |
| 10039 | EraseInstFromFunction(FI); |
| 10040 | return EraseInstFromFunction(*MI); |
| 10041 | } |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 10042 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 10043 | return 0; |
| 10044 | } |
| 10045 | |
| 10046 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10047 | /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible. |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 10048 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI, |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 10049 | const TargetData *TD) { |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 10050 | User *CI = cast<User>(LI.getOperand(0)); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10051 | Value *CastOp = CI->getOperand(0); |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 10052 | |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 10053 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(CI)) { |
| 10054 | // Instead of loading constant c string, use corresponding integer value |
| 10055 | // directly if string length is small enough. |
| 10056 | const std::string &Str = CE->getOperand(0)->getStringValue(); |
| 10057 | if (!Str.empty()) { |
| 10058 | unsigned len = Str.length(); |
| 10059 | const Type *Ty = cast<PointerType>(CE->getType())->getElementType(); |
| 10060 | unsigned numBits = Ty->getPrimitiveSizeInBits(); |
| 10061 | // Replace LI with immediate integer store. |
| 10062 | if ((numBits >> 3) == len + 1) { |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 10063 | APInt StrVal(numBits, 0); |
| 10064 | APInt SingleChar(numBits, 0); |
| 10065 | if (TD->isLittleEndian()) { |
| 10066 | for (signed i = len-1; i >= 0; i--) { |
| 10067 | SingleChar = (uint64_t) Str[i]; |
| 10068 | StrVal = (StrVal << 8) | SingleChar; |
| 10069 | } |
| 10070 | } else { |
| 10071 | for (unsigned i = 0; i < len; i++) { |
| 10072 | SingleChar = (uint64_t) Str[i]; |
| 10073 | StrVal = (StrVal << 8) | SingleChar; |
| 10074 | } |
| 10075 | // Append NULL at the end. |
| 10076 | SingleChar = 0; |
| 10077 | StrVal = (StrVal << 8) | SingleChar; |
| 10078 | } |
| 10079 | Value *NL = ConstantInt::get(StrVal); |
| 10080 | return IC.ReplaceInstUsesWith(LI, NL); |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 10081 | } |
| 10082 | } |
| 10083 | } |
| 10084 | |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 10085 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10086 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 10087 | const Type *SrcPTy = SrcTy->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10088 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 10089 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10090 | isa<VectorType>(DestPTy)) { |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10091 | // If the source is an array, the code below will not succeed. Check to |
| 10092 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 10093 | // constants. |
| 10094 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 10095 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 10096 | if (ASrcTy->getNumElements() != 0) { |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 10097 | Value *Idxs[2]; |
| 10098 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); |
| 10099 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10100 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 10101 | SrcPTy = SrcTy->getElementType(); |
| 10102 | } |
| 10103 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 10104 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10105 | isa<VectorType>(SrcPTy)) && |
Chris Lattner | b1515fe | 2005-03-29 06:37:47 +0000 | [diff] [blame] | 10106 | // Do not allow turning this into a load of an integer, which is then |
| 10107 | // casted to a pointer, this pessimizes pointer analysis a lot. |
| 10108 | (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) && |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 10109 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == |
| 10110 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10111 | |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10112 | // Okay, we are casting from one integer or pointer type to another of |
| 10113 | // the same size. Instead of casting the pointer before the load, cast |
| 10114 | // the result of the loaded value. |
| 10115 | Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp, |
| 10116 | CI->getName(), |
| 10117 | LI.isVolatile()),LI); |
| 10118 | // Now cast the result of the load. |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 10119 | return new BitCastInst(NewLoad, LI.getType()); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10120 | } |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 10121 | } |
| 10122 | } |
| 10123 | return 0; |
| 10124 | } |
| 10125 | |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10126 | /// isSafeToLoadUnconditionally - Return true if we know that executing a load |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10127 | /// from this value cannot trap. If it is not obviously safe to load from the |
| 10128 | /// specified pointer, we do a quick local scan of the basic block containing |
| 10129 | /// ScanFrom, to determine if the address is already accessed. |
| 10130 | static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) { |
Duncan Sands | 892c7e4 | 2007-09-19 10:10:31 +0000 | [diff] [blame] | 10131 | // If it is an alloca it is always safe to load from. |
| 10132 | if (isa<AllocaInst>(V)) return true; |
| 10133 | |
Duncan Sands | 46318cd | 2007-09-19 10:25:38 +0000 | [diff] [blame] | 10134 | // If it is a global variable it is mostly safe to load from. |
Duncan Sands | 892c7e4 | 2007-09-19 10:10:31 +0000 | [diff] [blame] | 10135 | if (const GlobalValue *GV = dyn_cast<GlobalVariable>(V)) |
Duncan Sands | 46318cd | 2007-09-19 10:25:38 +0000 | [diff] [blame] | 10136 | // Don't try to evaluate aliases. External weak GV can be null. |
Duncan Sands | 892c7e4 | 2007-09-19 10:10:31 +0000 | [diff] [blame] | 10137 | return !isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage(); |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10138 | |
| 10139 | // Otherwise, be a little bit agressive by scanning the local block where we |
| 10140 | // want to check to see if the pointer is already being loaded or stored |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 10141 | // from/to. If so, the previous load or store would have already trapped, |
| 10142 | // so there is no harm doing an extra load (also, CSE will later eliminate |
| 10143 | // the load entirely). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10144 | BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); |
| 10145 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 10146 | while (BBI != E) { |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10147 | --BBI; |
| 10148 | |
| 10149 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 10150 | if (LI->getOperand(0) == V) return true; |
| 10151 | } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 10152 | if (SI->getOperand(1) == V) return true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10153 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 10154 | } |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10155 | return false; |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10156 | } |
| 10157 | |
Chris Lattner | 8d2e888 | 2007-08-11 18:48:48 +0000 | [diff] [blame] | 10158 | /// GetUnderlyingObject - Trace through a series of getelementptrs and bitcasts |
| 10159 | /// until we find the underlying object a pointer is referring to or something |
| 10160 | /// we don't understand. Note that the returned pointer may be offset from the |
| 10161 | /// input, because we ignore GEP indices. |
| 10162 | static Value *GetUnderlyingObject(Value *Ptr) { |
| 10163 | while (1) { |
| 10164 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) { |
| 10165 | if (CE->getOpcode() == Instruction::BitCast || |
| 10166 | CE->getOpcode() == Instruction::GetElementPtr) |
| 10167 | Ptr = CE->getOperand(0); |
| 10168 | else |
| 10169 | return Ptr; |
| 10170 | } else if (BitCastInst *BCI = dyn_cast<BitCastInst>(Ptr)) { |
| 10171 | Ptr = BCI->getOperand(0); |
| 10172 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) { |
| 10173 | Ptr = GEP->getOperand(0); |
| 10174 | } else { |
| 10175 | return Ptr; |
| 10176 | } |
| 10177 | } |
| 10178 | } |
| 10179 | |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 10180 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 10181 | Value *Op = LI.getOperand(0); |
Chris Lattner | 5f16a13 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 10182 | |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 10183 | // Attempt to improve the alignment. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 10184 | unsigned KnownAlign = GetOrEnforceKnownAlignment(Op); |
| 10185 | if (KnownAlign > |
| 10186 | (LI.getAlignment() == 0 ? TD->getABITypeAlignment(LI.getType()) : |
| 10187 | LI.getAlignment())) |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 10188 | LI.setAlignment(KnownAlign); |
| 10189 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10190 | // load (cast X) --> cast (load X) iff safe |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 10191 | if (isa<CastInst>(Op)) |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 10192 | if (Instruction *Res = InstCombineLoadCast(*this, LI, TD)) |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10193 | return Res; |
| 10194 | |
| 10195 | // None of the following transforms are legal for volatile loads. |
| 10196 | if (LI.isVolatile()) return 0; |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 10197 | |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 10198 | if (&LI.getParent()->front() != &LI) { |
| 10199 | BasicBlock::iterator BBI = &LI; --BBI; |
Chris Lattner | 9c1f0fd | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 10200 | // If the instruction immediately before this is a store to the same |
| 10201 | // address, do a simple form of store->load forwarding. |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 10202 | if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 10203 | if (SI->getOperand(1) == LI.getOperand(0)) |
| 10204 | return ReplaceInstUsesWith(LI, SI->getOperand(0)); |
Chris Lattner | 9c1f0fd | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 10205 | if (LoadInst *LIB = dyn_cast<LoadInst>(BBI)) |
| 10206 | if (LIB->getOperand(0) == LI.getOperand(0)) |
| 10207 | return ReplaceInstUsesWith(LI, LIB); |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 10208 | } |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10209 | |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 10210 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { |
| 10211 | const Value *GEPI0 = GEPI->getOperand(0); |
| 10212 | // TODO: Consider a target hook for valid address spaces for this xform. |
| 10213 | if (isa<ConstantPointerNull>(GEPI0) && |
| 10214 | cast<PointerType>(GEPI0->getType())->getAddressSpace() == 0) { |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10215 | // Insert a new store to null instruction before the load to indicate |
| 10216 | // that this code is not reachable. We do this instead of inserting |
| 10217 | // an unreachable instruction directly because we cannot modify the |
| 10218 | // CFG. |
| 10219 | new StoreInst(UndefValue::get(LI.getType()), |
| 10220 | Constant::getNullValue(Op->getType()), &LI); |
| 10221 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 10222 | } |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 10223 | } |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10224 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10225 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10226 | // load null/undef -> undef |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 10227 | // TODO: Consider a target hook for valid address spaces for this xform. |
| 10228 | if (isa<UndefValue>(C) || (C->isNullValue() && |
| 10229 | cast<PointerType>(Op->getType())->getAddressSpace() == 0)) { |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 10230 | // Insert a new store to null instruction before the load to indicate that |
| 10231 | // this code is not reachable. We do this instead of inserting an |
| 10232 | // unreachable instruction directly because we cannot modify the CFG. |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10233 | new StoreInst(UndefValue::get(LI.getType()), |
| 10234 | Constant::getNullValue(Op->getType()), &LI); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10235 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 10236 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 10237 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10238 | // Instcombine load (constant global) into the value loaded. |
| 10239 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 10240 | if (GV->isConstant() && !GV->isDeclaration()) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10241 | return ReplaceInstUsesWith(LI, GV->getInitializer()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10242 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10243 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded. |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 10244 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) { |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10245 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 10246 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 10247 | if (GV->isConstant() && !GV->isDeclaration()) |
Chris Lattner | 363f2a2 | 2005-09-26 05:28:06 +0000 | [diff] [blame] | 10248 | if (Constant *V = |
| 10249 | ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10250 | return ReplaceInstUsesWith(LI, V); |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10251 | if (CE->getOperand(0)->isNullValue()) { |
| 10252 | // Insert a new store to null instruction before the load to indicate |
| 10253 | // that this code is not reachable. We do this instead of inserting |
| 10254 | // an unreachable instruction directly because we cannot modify the |
| 10255 | // CFG. |
| 10256 | new StoreInst(UndefValue::get(LI.getType()), |
| 10257 | Constant::getNullValue(Op->getType()), &LI); |
| 10258 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 10259 | } |
| 10260 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 10261 | } else if (CE->isCast()) { |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 10262 | if (Instruction *Res = InstCombineLoadCast(*this, LI, TD)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10263 | return Res; |
| 10264 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 10265 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10266 | } |
Chris Lattner | 8d2e888 | 2007-08-11 18:48:48 +0000 | [diff] [blame] | 10267 | |
| 10268 | // If this load comes from anywhere in a constant global, and if the global |
| 10269 | // is all undef or zero, we know what it loads. |
| 10270 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GetUnderlyingObject(Op))) { |
| 10271 | if (GV->isConstant() && GV->hasInitializer()) { |
| 10272 | if (GV->getInitializer()->isNullValue()) |
| 10273 | return ReplaceInstUsesWith(LI, Constant::getNullValue(LI.getType())); |
| 10274 | else if (isa<UndefValue>(GV->getInitializer())) |
| 10275 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 10276 | } |
| 10277 | } |
Chris Lattner | f499eac | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 10278 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10279 | if (Op->hasOneUse()) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10280 | // Change select and PHI nodes to select values instead of addresses: this |
| 10281 | // helps alias analysis out a lot, allows many others simplifications, and |
| 10282 | // exposes redundancy in the code. |
| 10283 | // |
| 10284 | // Note that we cannot do the transformation unless we know that the |
| 10285 | // introduced loads cannot trap! Something like this is valid as long as |
| 10286 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 10287 | // but it would not be valid if we transformed it to load from null |
| 10288 | // unconditionally. |
| 10289 | // |
| 10290 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 10291 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10292 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && |
| 10293 | isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10294 | Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 10295 | SI->getOperand(1)->getName()+".val"), LI); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10296 | Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 10297 | SI->getOperand(2)->getName()+".val"), LI); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10298 | return SelectInst::Create(SI->getCondition(), V1, V2); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10299 | } |
| 10300 | |
Chris Lattner | 684fe21 | 2004-09-23 15:46:00 +0000 | [diff] [blame] | 10301 | // load (select (cond, null, P)) -> load P |
| 10302 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) |
| 10303 | if (C->isNullValue()) { |
| 10304 | LI.setOperand(0, SI->getOperand(2)); |
| 10305 | return &LI; |
| 10306 | } |
| 10307 | |
| 10308 | // load (select (cond, P, null)) -> load P |
| 10309 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) |
| 10310 | if (C->isNullValue()) { |
| 10311 | LI.setOperand(0, SI->getOperand(1)); |
| 10312 | return &LI; |
| 10313 | } |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10314 | } |
| 10315 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 10316 | return 0; |
| 10317 | } |
| 10318 | |
Reid Spencer | 55af2b5 | 2007-01-19 21:20:31 +0000 | [diff] [blame] | 10319 | /// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10320 | /// when possible. |
| 10321 | static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { |
| 10322 | User *CI = cast<User>(SI.getOperand(1)); |
| 10323 | Value *CastOp = CI->getOperand(0); |
| 10324 | |
| 10325 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
| 10326 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
| 10327 | const Type *SrcPTy = SrcTy->getElementType(); |
| 10328 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 10329 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) { |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10330 | // If the source is an array, the code below will not succeed. Check to |
| 10331 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 10332 | // constants. |
| 10333 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 10334 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 10335 | if (ASrcTy->getNumElements() != 0) { |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 10336 | Value* Idxs[2]; |
| 10337 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); |
| 10338 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10339 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 10340 | SrcPTy = SrcTy->getElementType(); |
| 10341 | } |
| 10342 | |
Reid Spencer | 67f827c | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 10343 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
| 10344 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == |
| 10345 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10346 | |
| 10347 | // Okay, we are casting from one integer or pointer type to another of |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 10348 | // the same size. Instead of casting the pointer before |
| 10349 | // the store, cast the value to be stored. |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10350 | Value *NewCast; |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 10351 | Value *SIOp0 = SI.getOperand(0); |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 10352 | Instruction::CastOps opcode = Instruction::BitCast; |
| 10353 | const Type* CastSrcTy = SIOp0->getType(); |
| 10354 | const Type* CastDstTy = SrcPTy; |
| 10355 | if (isa<PointerType>(CastDstTy)) { |
| 10356 | if (CastSrcTy->isInteger()) |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 10357 | opcode = Instruction::IntToPtr; |
Reid Spencer | 67f827c | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 10358 | } else if (isa<IntegerType>(CastDstTy)) { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 10359 | if (isa<PointerType>(SIOp0->getType())) |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 10360 | opcode = Instruction::PtrToInt; |
| 10361 | } |
| 10362 | if (Constant *C = dyn_cast<Constant>(SIOp0)) |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 10363 | NewCast = ConstantExpr::getCast(opcode, C, CastDstTy); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10364 | else |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 10365 | NewCast = IC.InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 10366 | CastInst::Create(opcode, SIOp0, CastDstTy, SIOp0->getName()+".c"), |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 10367 | SI); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10368 | return new StoreInst(NewCast, CastOp); |
| 10369 | } |
| 10370 | } |
| 10371 | } |
| 10372 | return 0; |
| 10373 | } |
| 10374 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10375 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
| 10376 | Value *Val = SI.getOperand(0); |
| 10377 | Value *Ptr = SI.getOperand(1); |
| 10378 | |
| 10379 | if (isa<UndefValue>(Ptr)) { // store X, undef -> noop (even if volatile) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10380 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10381 | ++NumCombined; |
| 10382 | return 0; |
| 10383 | } |
Chris Lattner | 836692d | 2007-01-15 06:51:56 +0000 | [diff] [blame] | 10384 | |
| 10385 | // If the RHS is an alloca with a single use, zapify the store, making the |
| 10386 | // alloca dead. |
Chris Lattner | cea1fdd | 2008-04-29 04:58:38 +0000 | [diff] [blame] | 10387 | if (Ptr->hasOneUse() && !SI.isVolatile()) { |
Chris Lattner | 836692d | 2007-01-15 06:51:56 +0000 | [diff] [blame] | 10388 | if (isa<AllocaInst>(Ptr)) { |
| 10389 | EraseInstFromFunction(SI); |
| 10390 | ++NumCombined; |
| 10391 | return 0; |
| 10392 | } |
| 10393 | |
| 10394 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) |
| 10395 | if (isa<AllocaInst>(GEP->getOperand(0)) && |
| 10396 | GEP->getOperand(0)->hasOneUse()) { |
| 10397 | EraseInstFromFunction(SI); |
| 10398 | ++NumCombined; |
| 10399 | return 0; |
| 10400 | } |
| 10401 | } |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10402 | |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 10403 | // Attempt to improve the alignment. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 10404 | unsigned KnownAlign = GetOrEnforceKnownAlignment(Ptr); |
| 10405 | if (KnownAlign > |
| 10406 | (SI.getAlignment() == 0 ? TD->getABITypeAlignment(Val->getType()) : |
| 10407 | SI.getAlignment())) |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 10408 | SI.setAlignment(KnownAlign); |
| 10409 | |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10410 | // Do really simple DSE, to catch cases where there are several consequtive |
| 10411 | // stores to the same location, separated by a few arithmetic operations. This |
| 10412 | // situation often occurs with bitfield accesses. |
| 10413 | BasicBlock::iterator BBI = &SI; |
| 10414 | for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts; |
| 10415 | --ScanInsts) { |
| 10416 | --BBI; |
| 10417 | |
| 10418 | if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) { |
| 10419 | // Prev store isn't volatile, and stores to the same location? |
| 10420 | if (!PrevSI->isVolatile() && PrevSI->getOperand(1) == SI.getOperand(1)) { |
| 10421 | ++NumDeadStore; |
| 10422 | ++BBI; |
| 10423 | EraseInstFromFunction(*PrevSI); |
| 10424 | continue; |
| 10425 | } |
| 10426 | break; |
| 10427 | } |
| 10428 | |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 10429 | // If this is a load, we have to stop. However, if the loaded value is from |
| 10430 | // the pointer we're loading and is producing the pointer we're storing, |
| 10431 | // then *this* store is dead (X = load P; store X -> P). |
| 10432 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
Chris Lattner | a54c7eb | 2007-09-07 05:33:03 +0000 | [diff] [blame] | 10433 | if (LI == Val && LI->getOperand(0) == Ptr && !SI.isVolatile()) { |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 10434 | EraseInstFromFunction(SI); |
| 10435 | ++NumCombined; |
| 10436 | return 0; |
| 10437 | } |
| 10438 | // Otherwise, this is a load from some other location. Stores before it |
| 10439 | // may not be dead. |
| 10440 | break; |
| 10441 | } |
| 10442 | |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10443 | // Don't skip over loads or things that can modify memory. |
Chris Lattner | 0ef546e | 2008-05-08 17:20:30 +0000 | [diff] [blame] | 10444 | if (BBI->mayWriteToMemory() || BBI->mayReadFromMemory()) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10445 | break; |
| 10446 | } |
| 10447 | |
| 10448 | |
| 10449 | if (SI.isVolatile()) return 0; // Don't hack volatile stores. |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10450 | |
| 10451 | // store X, null -> turns into 'unreachable' in SimplifyCFG |
| 10452 | if (isa<ConstantPointerNull>(Ptr)) { |
| 10453 | if (!isa<UndefValue>(Val)) { |
| 10454 | SI.setOperand(0, UndefValue::get(Val->getType())); |
| 10455 | if (Instruction *U = dyn_cast<Instruction>(Val)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10456 | AddToWorkList(U); // Dropped a use. |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10457 | ++NumCombined; |
| 10458 | } |
| 10459 | return 0; // Do not modify these! |
| 10460 | } |
| 10461 | |
| 10462 | // store undef, Ptr -> noop |
| 10463 | if (isa<UndefValue>(Val)) { |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10464 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10465 | ++NumCombined; |
| 10466 | return 0; |
| 10467 | } |
| 10468 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10469 | // If the pointer destination is a cast, see if we can fold the cast into the |
| 10470 | // source instead. |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 10471 | if (isa<CastInst>(Ptr)) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10472 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 10473 | return Res; |
| 10474 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 10475 | if (CE->isCast()) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10476 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 10477 | return Res; |
| 10478 | |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 10479 | |
| 10480 | // If this store is the last instruction in the basic block, and if the block |
| 10481 | // ends with an unconditional branch, try to move it to the successor block. |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10482 | BBI = &SI; ++BBI; |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 10483 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10484 | if (BI->isUnconditional()) |
| 10485 | if (SimplifyStoreAtEndOfBlock(SI)) |
| 10486 | return 0; // xform done! |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 10487 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10488 | return 0; |
| 10489 | } |
| 10490 | |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10491 | /// SimplifyStoreAtEndOfBlock - Turn things like: |
| 10492 | /// if () { *P = v1; } else { *P = v2 } |
| 10493 | /// into a phi node with a store in the successor. |
| 10494 | /// |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10495 | /// Simplify things like: |
| 10496 | /// *P = v1; if () { *P = v2; } |
| 10497 | /// into a phi node with a store in the successor. |
| 10498 | /// |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10499 | bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) { |
| 10500 | BasicBlock *StoreBB = SI.getParent(); |
| 10501 | |
| 10502 | // Check to see if the successor block has exactly two incoming edges. If |
| 10503 | // so, see if the other predecessor contains a store to the same location. |
| 10504 | // if so, insert a PHI node (if needed) and move the stores down. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10505 | BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10506 | |
| 10507 | // Determine whether Dest has exactly two predecessors and, if so, compute |
| 10508 | // the other predecessor. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10509 | pred_iterator PI = pred_begin(DestBB); |
| 10510 | BasicBlock *OtherBB = 0; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10511 | if (*PI != StoreBB) |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10512 | OtherBB = *PI; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10513 | ++PI; |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10514 | if (PI == pred_end(DestBB)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10515 | return false; |
| 10516 | |
| 10517 | if (*PI != StoreBB) { |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10518 | if (OtherBB) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10519 | return false; |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10520 | OtherBB = *PI; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10521 | } |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10522 | if (++PI != pred_end(DestBB)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10523 | return false; |
| 10524 | |
| 10525 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10526 | // Verify that the other block ends in a branch and is not otherwise empty. |
| 10527 | BasicBlock::iterator BBI = OtherBB->getTerminator(); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10528 | BranchInst *OtherBr = dyn_cast<BranchInst>(BBI); |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10529 | if (!OtherBr || BBI == OtherBB->begin()) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10530 | return false; |
| 10531 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10532 | // If the other block ends in an unconditional branch, check for the 'if then |
| 10533 | // else' case. there is an instruction before the branch. |
| 10534 | StoreInst *OtherStore = 0; |
| 10535 | if (OtherBr->isUnconditional()) { |
| 10536 | // If this isn't a store, or isn't a store to the same location, bail out. |
| 10537 | --BBI; |
| 10538 | OtherStore = dyn_cast<StoreInst>(BBI); |
| 10539 | if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1)) |
| 10540 | return false; |
| 10541 | } else { |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 10542 | // Otherwise, the other block ended with a conditional branch. If one of the |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10543 | // destinations is StoreBB, then we have the if/then case. |
| 10544 | if (OtherBr->getSuccessor(0) != StoreBB && |
| 10545 | OtherBr->getSuccessor(1) != StoreBB) |
| 10546 | return false; |
| 10547 | |
| 10548 | // Okay, we know that OtherBr now goes to Dest and StoreBB, so this is an |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 10549 | // if/then triangle. See if there is a store to the same ptr as SI that |
| 10550 | // lives in OtherBB. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10551 | for (;; --BBI) { |
| 10552 | // Check to see if we find the matching store. |
| 10553 | if ((OtherStore = dyn_cast<StoreInst>(BBI))) { |
| 10554 | if (OtherStore->getOperand(1) != SI.getOperand(1)) |
| 10555 | return false; |
| 10556 | break; |
| 10557 | } |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 10558 | // If we find something that may be using the stored value, or if we run |
| 10559 | // out of instructions, we can't do the xform. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10560 | if (isa<LoadInst>(BBI) || BBI->mayWriteToMemory() || |
| 10561 | BBI == OtherBB->begin()) |
| 10562 | return false; |
| 10563 | } |
| 10564 | |
| 10565 | // In order to eliminate the store in OtherBr, we have to |
| 10566 | // make sure nothing reads the stored value in StoreBB. |
| 10567 | for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) { |
| 10568 | // FIXME: This should really be AA driven. |
| 10569 | if (isa<LoadInst>(I) || I->mayWriteToMemory()) |
| 10570 | return false; |
| 10571 | } |
| 10572 | } |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10573 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10574 | // Insert a PHI node now if we need it. |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10575 | Value *MergedVal = OtherStore->getOperand(0); |
| 10576 | if (MergedVal != SI.getOperand(0)) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10577 | PHINode *PN = PHINode::Create(MergedVal->getType(), "storemerge"); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10578 | PN->reserveOperandSpace(2); |
| 10579 | PN->addIncoming(SI.getOperand(0), SI.getParent()); |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10580 | PN->addIncoming(OtherStore->getOperand(0), OtherBB); |
| 10581 | MergedVal = InsertNewInstBefore(PN, DestBB->front()); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10582 | } |
| 10583 | |
| 10584 | // Advance to a place where it is safe to insert the new store and |
| 10585 | // insert it. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10586 | BBI = DestBB->begin(); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10587 | while (isa<PHINode>(BBI)) ++BBI; |
| 10588 | InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1), |
| 10589 | OtherStore->isVolatile()), *BBI); |
| 10590 | |
| 10591 | // Nuke the old stores. |
| 10592 | EraseInstFromFunction(SI); |
| 10593 | EraseInstFromFunction(*OtherStore); |
| 10594 | ++NumCombined; |
| 10595 | return true; |
| 10596 | } |
| 10597 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10598 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 10599 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 10600 | // Change br (not X), label True, label False to: br X, label False, True |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 10601 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 10602 | BasicBlock *TrueDest; |
| 10603 | BasicBlock *FalseDest; |
| 10604 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && |
| 10605 | !isa<Constant>(X)) { |
| 10606 | // Swap Destinations and condition... |
| 10607 | BI.setCondition(X); |
| 10608 | BI.setSuccessor(0, FalseDest); |
| 10609 | BI.setSuccessor(1, TrueDest); |
| 10610 | return &BI; |
| 10611 | } |
| 10612 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10613 | // Cannonicalize fcmp_one -> fcmp_oeq |
| 10614 | FCmpInst::Predicate FPred; Value *Y; |
| 10615 | if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)), |
| 10616 | TrueDest, FalseDest))) |
| 10617 | if ((FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE || |
| 10618 | FPred == FCmpInst::FCMP_OGE) && BI.getCondition()->hasOneUse()) { |
| 10619 | FCmpInst *I = cast<FCmpInst>(BI.getCondition()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10620 | FCmpInst::Predicate NewPred = FCmpInst::getInversePredicate(FPred); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10621 | Instruction *NewSCC = new FCmpInst(NewPred, X, Y, "", I); |
| 10622 | NewSCC->takeName(I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10623 | // Swap Destinations and condition... |
| 10624 | BI.setCondition(NewSCC); |
| 10625 | BI.setSuccessor(0, FalseDest); |
| 10626 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10627 | RemoveFromWorkList(I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10628 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10629 | AddToWorkList(NewSCC); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10630 | return &BI; |
| 10631 | } |
| 10632 | |
| 10633 | // Cannonicalize icmp_ne -> icmp_eq |
| 10634 | ICmpInst::Predicate IPred; |
| 10635 | if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)), |
| 10636 | TrueDest, FalseDest))) |
| 10637 | if ((IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE || |
| 10638 | IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE || |
| 10639 | IPred == ICmpInst::ICMP_SGE) && BI.getCondition()->hasOneUse()) { |
| 10640 | ICmpInst *I = cast<ICmpInst>(BI.getCondition()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10641 | ICmpInst::Predicate NewPred = ICmpInst::getInversePredicate(IPred); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10642 | Instruction *NewSCC = new ICmpInst(NewPred, X, Y, "", I); |
| 10643 | NewSCC->takeName(I); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 10644 | // Swap Destinations and condition... |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 10645 | BI.setCondition(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 10646 | BI.setSuccessor(0, FalseDest); |
| 10647 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10648 | RemoveFromWorkList(I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10649 | I->eraseFromParent();; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10650 | AddToWorkList(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 10651 | return &BI; |
| 10652 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10653 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 10654 | return 0; |
| 10655 | } |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 10656 | |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 10657 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
| 10658 | Value *Cond = SI.getCondition(); |
| 10659 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { |
| 10660 | if (I->getOpcode() == Instruction::Add) |
| 10661 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 10662 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' |
| 10663 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10664 | SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 10665 | AddRHS)); |
| 10666 | SI.setOperand(0, I->getOperand(0)); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10667 | AddToWorkList(I); |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 10668 | return &SI; |
| 10669 | } |
| 10670 | } |
| 10671 | return 0; |
| 10672 | } |
| 10673 | |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10674 | /// CheapToScalarize - Return true if the value is cheaper to scalarize than it |
| 10675 | /// is to leave as a vector operation. |
| 10676 | static bool CheapToScalarize(Value *V, bool isConstant) { |
| 10677 | if (isa<ConstantAggregateZero>(V)) |
| 10678 | return true; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10679 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) { |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10680 | if (isConstant) return true; |
| 10681 | // If all elts are the same, we can extract. |
| 10682 | Constant *Op0 = C->getOperand(0); |
| 10683 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
| 10684 | if (C->getOperand(i) != Op0) |
| 10685 | return false; |
| 10686 | return true; |
| 10687 | } |
| 10688 | Instruction *I = dyn_cast<Instruction>(V); |
| 10689 | if (!I) return false; |
| 10690 | |
| 10691 | // Insert element gets simplified to the inserted element or is deleted if |
| 10692 | // this is constant idx extract element and its a constant idx insertelt. |
| 10693 | if (I->getOpcode() == Instruction::InsertElement && isConstant && |
| 10694 | isa<ConstantInt>(I->getOperand(2))) |
| 10695 | return true; |
| 10696 | if (I->getOpcode() == Instruction::Load && I->hasOneUse()) |
| 10697 | return true; |
| 10698 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) |
| 10699 | if (BO->hasOneUse() && |
| 10700 | (CheapToScalarize(BO->getOperand(0), isConstant) || |
| 10701 | CheapToScalarize(BO->getOperand(1), isConstant))) |
| 10702 | return true; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10703 | if (CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 10704 | if (CI->hasOneUse() && |
| 10705 | (CheapToScalarize(CI->getOperand(0), isConstant) || |
| 10706 | CheapToScalarize(CI->getOperand(1), isConstant))) |
| 10707 | return true; |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10708 | |
| 10709 | return false; |
| 10710 | } |
| 10711 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 10712 | /// Read and decode a shufflevector mask. |
| 10713 | /// |
| 10714 | /// It turns undef elements into values that are larger than the number of |
| 10715 | /// elements in the input. |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10716 | static std::vector<unsigned> getShuffleMask(const ShuffleVectorInst *SVI) { |
| 10717 | unsigned NElts = SVI->getType()->getNumElements(); |
| 10718 | if (isa<ConstantAggregateZero>(SVI->getOperand(2))) |
| 10719 | return std::vector<unsigned>(NElts, 0); |
| 10720 | if (isa<UndefValue>(SVI->getOperand(2))) |
| 10721 | return std::vector<unsigned>(NElts, 2*NElts); |
| 10722 | |
| 10723 | std::vector<unsigned> Result; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10724 | const ConstantVector *CP = cast<ConstantVector>(SVI->getOperand(2)); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10725 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 10726 | if (isa<UndefValue>(CP->getOperand(i))) |
| 10727 | Result.push_back(NElts*2); // undef -> 8 |
| 10728 | else |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10729 | Result.push_back(cast<ConstantInt>(CP->getOperand(i))->getZExtValue()); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10730 | return Result; |
| 10731 | } |
| 10732 | |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10733 | /// FindScalarElement - Given a vector and an element number, see if the scalar |
| 10734 | /// value is already around as a register, for example if it were inserted then |
| 10735 | /// extracted from the vector. |
| 10736 | static Value *FindScalarElement(Value *V, unsigned EltNo) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10737 | assert(isa<VectorType>(V->getType()) && "Not looking at a vector?"); |
| 10738 | const VectorType *PTy = cast<VectorType>(V->getType()); |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 10739 | unsigned Width = PTy->getNumElements(); |
| 10740 | if (EltNo >= Width) // Out of range access. |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10741 | return UndefValue::get(PTy->getElementType()); |
| 10742 | |
| 10743 | if (isa<UndefValue>(V)) |
| 10744 | return UndefValue::get(PTy->getElementType()); |
| 10745 | else if (isa<ConstantAggregateZero>(V)) |
| 10746 | return Constant::getNullValue(PTy->getElementType()); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10747 | else if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10748 | return CP->getOperand(EltNo); |
| 10749 | else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) { |
| 10750 | // If this is an insert to a variable element, we don't know what it is. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10751 | if (!isa<ConstantInt>(III->getOperand(2))) |
| 10752 | return 0; |
| 10753 | unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue(); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10754 | |
| 10755 | // If this is an insert to the element we are looking for, return the |
| 10756 | // inserted value. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10757 | if (EltNo == IIElt) |
| 10758 | return III->getOperand(1); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10759 | |
| 10760 | // Otherwise, the insertelement doesn't modify the value, recurse on its |
| 10761 | // vector input. |
| 10762 | return FindScalarElement(III->getOperand(0), EltNo); |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 10763 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) { |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10764 | unsigned InEl = getShuffleMask(SVI)[EltNo]; |
| 10765 | if (InEl < Width) |
| 10766 | return FindScalarElement(SVI->getOperand(0), InEl); |
| 10767 | else if (InEl < Width*2) |
| 10768 | return FindScalarElement(SVI->getOperand(1), InEl - Width); |
| 10769 | else |
| 10770 | return UndefValue::get(PTy->getElementType()); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10771 | } |
| 10772 | |
| 10773 | // Otherwise, we don't know. |
| 10774 | return 0; |
| 10775 | } |
| 10776 | |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10777 | Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10778 | |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 10779 | // If vector val is undef, replace extract with scalar undef. |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 10780 | if (isa<UndefValue>(EI.getOperand(0))) |
| 10781 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
| 10782 | |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 10783 | // If vector val is constant 0, replace extract with scalar 0. |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 10784 | if (isa<ConstantAggregateZero>(EI.getOperand(0))) |
| 10785 | return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType())); |
| 10786 | |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10787 | if (ConstantVector *C = dyn_cast<ConstantVector>(EI.getOperand(0))) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 10788 | // If vector val is constant with uniform operands, replace EI |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10789 | // with that operand |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10790 | Constant *op0 = C->getOperand(0); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10791 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10792 | if (C->getOperand(i) != op0) { |
| 10793 | op0 = 0; |
| 10794 | break; |
| 10795 | } |
| 10796 | if (op0) |
| 10797 | return ReplaceInstUsesWith(EI, op0); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10798 | } |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10799 | |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10800 | // If extracting a specified index from the vector, see if we can recursively |
| 10801 | // find a previously computed scalar that was inserted into the vector. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10802 | if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
Chris Lattner | 8546409 | 2007-04-09 01:37:55 +0000 | [diff] [blame] | 10803 | unsigned IndexVal = IdxC->getZExtValue(); |
| 10804 | unsigned VectorWidth = |
| 10805 | cast<VectorType>(EI.getOperand(0)->getType())->getNumElements(); |
| 10806 | |
| 10807 | // If this is extracting an invalid index, turn this into undef, to avoid |
| 10808 | // crashing the code below. |
| 10809 | if (IndexVal >= VectorWidth) |
| 10810 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
| 10811 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10812 | // This instruction only demands the single element from the input vector. |
| 10813 | // If the input vector has a single use, simplify it based on this use |
| 10814 | // property. |
Chris Lattner | 8546409 | 2007-04-09 01:37:55 +0000 | [diff] [blame] | 10815 | if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) { |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10816 | uint64_t UndefElts; |
| 10817 | if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10818 | 1 << IndexVal, |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10819 | UndefElts)) { |
| 10820 | EI.setOperand(0, V); |
| 10821 | return &EI; |
| 10822 | } |
| 10823 | } |
| 10824 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10825 | if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10826 | return ReplaceInstUsesWith(EI, Elt); |
Chris Lattner | b7300fa | 2007-04-14 23:02:14 +0000 | [diff] [blame] | 10827 | |
| 10828 | // If the this extractelement is directly using a bitcast from a vector of |
| 10829 | // the same number of elements, see if we can find the source element from |
| 10830 | // it. In this case, we will end up needing to bitcast the scalars. |
| 10831 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) { |
| 10832 | if (const VectorType *VT = |
| 10833 | dyn_cast<VectorType>(BCI->getOperand(0)->getType())) |
| 10834 | if (VT->getNumElements() == VectorWidth) |
| 10835 | if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal)) |
| 10836 | return new BitCastInst(Elt, EI.getType()); |
| 10837 | } |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 10838 | } |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10839 | |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 10840 | if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) { |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10841 | if (I->hasOneUse()) { |
| 10842 | // Push extractelement into predecessor operation if legal and |
| 10843 | // profitable to do so |
| 10844 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10845 | bool isConstantElt = isa<ConstantInt>(EI.getOperand(1)); |
| 10846 | if (CheapToScalarize(BO, isConstantElt)) { |
| 10847 | ExtractElementInst *newEI0 = |
| 10848 | new ExtractElementInst(BO->getOperand(0), EI.getOperand(1), |
| 10849 | EI.getName()+".lhs"); |
| 10850 | ExtractElementInst *newEI1 = |
| 10851 | new ExtractElementInst(BO->getOperand(1), EI.getOperand(1), |
| 10852 | EI.getName()+".rhs"); |
| 10853 | InsertNewInstBefore(newEI0, EI); |
| 10854 | InsertNewInstBefore(newEI1, EI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 10855 | return BinaryOperator::Create(BO->getOpcode(), newEI0, newEI1); |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10856 | } |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 10857 | } else if (isa<LoadInst>(I)) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 10858 | unsigned AS = |
| 10859 | cast<PointerType>(I->getOperand(0)->getType())->getAddressSpace(); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 10860 | Value *Ptr = InsertBitCastBefore(I->getOperand(0), |
| 10861 | PointerType::get(EI.getType(), AS),EI); |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 10862 | GetElementPtrInst *GEP = |
| 10863 | GetElementPtrInst::Create(Ptr, EI.getOperand(1), I->getName()+".gep"); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10864 | InsertNewInstBefore(GEP, EI); |
| 10865 | return new LoadInst(GEP); |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 10866 | } |
| 10867 | } |
| 10868 | if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) { |
| 10869 | // Extracting the inserted element? |
| 10870 | if (IE->getOperand(2) == EI.getOperand(1)) |
| 10871 | return ReplaceInstUsesWith(EI, IE->getOperand(1)); |
| 10872 | // If the inserted and extracted elements are constants, they must not |
| 10873 | // be the same value, extract from the pre-inserted value instead. |
| 10874 | if (isa<Constant>(IE->getOperand(2)) && |
| 10875 | isa<Constant>(EI.getOperand(1))) { |
| 10876 | AddUsesToWorkList(EI); |
| 10877 | EI.setOperand(0, IE->getOperand(0)); |
| 10878 | return &EI; |
| 10879 | } |
| 10880 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) { |
| 10881 | // If this is extracting an element from a shufflevector, figure out where |
| 10882 | // it came from and extract from the appropriate input element instead. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10883 | if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
| 10884 | unsigned SrcIdx = getShuffleMask(SVI)[Elt->getZExtValue()]; |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10885 | Value *Src; |
| 10886 | if (SrcIdx < SVI->getType()->getNumElements()) |
| 10887 | Src = SVI->getOperand(0); |
| 10888 | else if (SrcIdx < SVI->getType()->getNumElements()*2) { |
| 10889 | SrcIdx -= SVI->getType()->getNumElements(); |
| 10890 | Src = SVI->getOperand(1); |
| 10891 | } else { |
| 10892 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
Chris Lattner | df084ff | 2006-03-30 22:02:40 +0000 | [diff] [blame] | 10893 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10894 | return new ExtractElementInst(Src, SrcIdx); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10895 | } |
| 10896 | } |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 10897 | } |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10898 | return 0; |
| 10899 | } |
| 10900 | |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10901 | /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns |
| 10902 | /// elements from either LHS or RHS, return the shuffle mask and true. |
| 10903 | /// Otherwise, return false. |
| 10904 | static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, |
| 10905 | std::vector<Constant*> &Mask) { |
| 10906 | assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() && |
| 10907 | "Invalid CollectSingleShuffleElements"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10908 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10909 | |
| 10910 | if (isa<UndefValue>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10911 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10912 | return true; |
| 10913 | } else if (V == LHS) { |
| 10914 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10915 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10916 | return true; |
| 10917 | } else if (V == RHS) { |
| 10918 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10919 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i+NumElts)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10920 | return true; |
| 10921 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 10922 | // If this is an insert of an extract from some other vector, include it. |
| 10923 | Value *VecOp = IEI->getOperand(0); |
| 10924 | Value *ScalarOp = IEI->getOperand(1); |
| 10925 | Value *IdxOp = IEI->getOperand(2); |
| 10926 | |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 10927 | if (!isa<ConstantInt>(IdxOp)) |
| 10928 | return false; |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10929 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 10930 | |
| 10931 | if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector. |
| 10932 | // Okay, we can handle this if the vector we are insertinting into is |
| 10933 | // transitively ok. |
| 10934 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 10935 | // If so, update the mask to reflect the inserted undef. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10936 | Mask[InsertedIdx] = UndefValue::get(Type::Int32Ty); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 10937 | return true; |
| 10938 | } |
| 10939 | } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){ |
| 10940 | if (isa<ConstantInt>(EI->getOperand(1)) && |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10941 | EI->getOperand(0)->getType() == V->getType()) { |
| 10942 | unsigned ExtractedIdx = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10943 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10944 | |
| 10945 | // This must be extracting from either LHS or RHS. |
| 10946 | if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) { |
| 10947 | // Okay, we can handle this if the vector we are insertinting into is |
| 10948 | // transitively ok. |
| 10949 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 10950 | // If so, update the mask to reflect the inserted value. |
| 10951 | if (EI->getOperand(0) == LHS) { |
| 10952 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10953 | ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10954 | } else { |
| 10955 | assert(EI->getOperand(0) == RHS); |
| 10956 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10957 | ConstantInt::get(Type::Int32Ty, ExtractedIdx+NumElts); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10958 | |
| 10959 | } |
| 10960 | return true; |
| 10961 | } |
| 10962 | } |
| 10963 | } |
| 10964 | } |
| 10965 | } |
| 10966 | // TODO: Handle shufflevector here! |
| 10967 | |
| 10968 | return false; |
| 10969 | } |
| 10970 | |
| 10971 | /// CollectShuffleElements - We are building a shuffle of V, using RHS as the |
| 10972 | /// RHS of the shuffle instruction, if it is not null. Return a shuffle mask |
| 10973 | /// that computes V and the LHS value of the shuffle. |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10974 | static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask, |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10975 | Value *&RHS) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10976 | assert(isa<VectorType>(V->getType()) && |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10977 | (RHS == 0 || V->getType() == RHS->getType()) && |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10978 | "Invalid shuffle!"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10979 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10980 | |
| 10981 | if (isa<UndefValue>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10982 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10983 | return V; |
| 10984 | } else if (isa<ConstantAggregateZero>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10985 | Mask.assign(NumElts, ConstantInt::get(Type::Int32Ty, 0)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10986 | return V; |
| 10987 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 10988 | // If this is an insert of an extract from some other vector, include it. |
| 10989 | Value *VecOp = IEI->getOperand(0); |
| 10990 | Value *ScalarOp = IEI->getOperand(1); |
| 10991 | Value *IdxOp = IEI->getOperand(2); |
| 10992 | |
| 10993 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 10994 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 10995 | EI->getOperand(0)->getType() == V->getType()) { |
| 10996 | unsigned ExtractedIdx = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10997 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
| 10998 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10999 | |
| 11000 | // Either the extracted from or inserted into vector must be RHSVec, |
| 11001 | // otherwise we'd end up with a shuffle of three inputs. |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11002 | if (EI->getOperand(0) == RHS || RHS == 0) { |
| 11003 | RHS = EI->getOperand(0); |
| 11004 | Value *V = CollectShuffleElements(VecOp, Mask, RHS); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11005 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11006 | ConstantInt::get(Type::Int32Ty, NumElts+ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11007 | return V; |
| 11008 | } |
| 11009 | |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11010 | if (VecOp == RHS) { |
| 11011 | Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11012 | // Everything but the extracted element is replaced with the RHS. |
| 11013 | for (unsigned i = 0; i != NumElts; ++i) { |
| 11014 | if (i != InsertedIdx) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11015 | Mask[i] = ConstantInt::get(Type::Int32Ty, NumElts+i); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11016 | } |
| 11017 | return V; |
| 11018 | } |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11019 | |
| 11020 | // If this insertelement is a chain that comes from exactly these two |
| 11021 | // vectors, return the vector and the effective shuffle. |
| 11022 | if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask)) |
| 11023 | return EI->getOperand(0); |
| 11024 | |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11025 | } |
| 11026 | } |
| 11027 | } |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11028 | // TODO: Handle shufflevector here! |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11029 | |
| 11030 | // Otherwise, can't do anything fancy. Return an identity vector. |
| 11031 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11032 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11033 | return V; |
| 11034 | } |
| 11035 | |
| 11036 | Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { |
| 11037 | Value *VecOp = IE.getOperand(0); |
| 11038 | Value *ScalarOp = IE.getOperand(1); |
| 11039 | Value *IdxOp = IE.getOperand(2); |
| 11040 | |
Chris Lattner | 599ded1 | 2007-04-09 01:11:16 +0000 | [diff] [blame] | 11041 | // Inserting an undef or into an undefined place, remove this. |
| 11042 | if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp)) |
| 11043 | ReplaceInstUsesWith(IE, VecOp); |
| 11044 | |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11045 | // If the inserted element was extracted from some other vector, and if the |
| 11046 | // indexes are constant, try to turn this into a shufflevector operation. |
| 11047 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 11048 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 11049 | EI->getOperand(0)->getType() == IE.getType()) { |
| 11050 | unsigned NumVectorElts = IE.getType()->getNumElements(); |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 11051 | unsigned ExtractedIdx = |
| 11052 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 11053 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11054 | |
| 11055 | if (ExtractedIdx >= NumVectorElts) // Out of range extract. |
| 11056 | return ReplaceInstUsesWith(IE, VecOp); |
| 11057 | |
| 11058 | if (InsertedIdx >= NumVectorElts) // Out of range insert. |
| 11059 | return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType())); |
| 11060 | |
| 11061 | // If we are extracting a value from a vector, then inserting it right |
| 11062 | // back into the same place, just use the input vector. |
| 11063 | if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx) |
| 11064 | return ReplaceInstUsesWith(IE, VecOp); |
| 11065 | |
| 11066 | // We could theoretically do this for ANY input. However, doing so could |
| 11067 | // turn chains of insertelement instructions into a chain of shufflevector |
| 11068 | // instructions, and right now we do not merge shufflevectors. As such, |
| 11069 | // only do this in a situation where it is clear that there is benefit. |
| 11070 | if (isa<UndefValue>(VecOp) || isa<ConstantAggregateZero>(VecOp)) { |
| 11071 | // Turn this into shuffle(EIOp0, VecOp, Mask). The result has all of |
| 11072 | // the values of VecOp, except then one read from EIOp0. |
| 11073 | // Build a new shuffle mask. |
| 11074 | std::vector<Constant*> Mask; |
| 11075 | if (isa<UndefValue>(VecOp)) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11076 | Mask.assign(NumVectorElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11077 | else { |
| 11078 | assert(isa<ConstantAggregateZero>(VecOp) && "Unknown thing"); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11079 | Mask.assign(NumVectorElts, ConstantInt::get(Type::Int32Ty, |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11080 | NumVectorElts)); |
| 11081 | } |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11082 | Mask[InsertedIdx] = ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11083 | return new ShuffleVectorInst(EI->getOperand(0), VecOp, |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11084 | ConstantVector::get(Mask)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11085 | } |
| 11086 | |
| 11087 | // If this insertelement isn't used by some other insertelement, turn it |
| 11088 | // (and any insertelements it points to), into one big shuffle. |
| 11089 | if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) { |
| 11090 | std::vector<Constant*> Mask; |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11091 | Value *RHS = 0; |
| 11092 | Value *LHS = CollectShuffleElements(&IE, Mask, RHS); |
| 11093 | if (RHS == 0) RHS = UndefValue::get(LHS->getType()); |
| 11094 | // We now have a shuffle of LHS, RHS, Mask. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11095 | return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Mask)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11096 | } |
| 11097 | } |
| 11098 | } |
| 11099 | |
| 11100 | return 0; |
| 11101 | } |
| 11102 | |
| 11103 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11104 | Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { |
| 11105 | Value *LHS = SVI.getOperand(0); |
| 11106 | Value *RHS = SVI.getOperand(1); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11107 | std::vector<unsigned> Mask = getShuffleMask(&SVI); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11108 | |
| 11109 | bool MadeChange = false; |
| 11110 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 11111 | // Undefined shuffle mask -> undefined value. |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11112 | if (isa<UndefValue>(SVI.getOperand(2))) |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11113 | return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType())); |
| 11114 | |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 11115 | // If we have shuffle(x, undef, mask) and any elements of mask refer to |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11116 | // the undef, change them to undefs. |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 11117 | if (isa<UndefValue>(SVI.getOperand(1))) { |
| 11118 | // Scan to see if there are any references to the RHS. If so, replace them |
| 11119 | // with undef element refs and set MadeChange to true. |
| 11120 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 11121 | if (Mask[i] >= e && Mask[i] != 2*e) { |
| 11122 | Mask[i] = 2*e; |
| 11123 | MadeChange = true; |
| 11124 | } |
| 11125 | } |
| 11126 | |
| 11127 | if (MadeChange) { |
| 11128 | // Remap any references to RHS to use LHS. |
| 11129 | std::vector<Constant*> Elts; |
| 11130 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 11131 | if (Mask[i] == 2*e) |
| 11132 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
| 11133 | else |
| 11134 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); |
| 11135 | } |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11136 | SVI.setOperand(2, ConstantVector::get(Elts)); |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 11137 | } |
| 11138 | } |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11139 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11140 | // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask') |
| 11141 | // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask'). |
| 11142 | if (LHS == RHS || isa<UndefValue>(LHS)) { |
| 11143 | if (isa<UndefValue>(LHS) && LHS == RHS) { |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11144 | // shuffle(undef,undef,mask) -> undef. |
| 11145 | return ReplaceInstUsesWith(SVI, LHS); |
| 11146 | } |
| 11147 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11148 | // Remap any references to RHS to use LHS. |
| 11149 | std::vector<Constant*> Elts; |
| 11150 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11151 | if (Mask[i] >= 2*e) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11152 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11153 | else { |
| 11154 | if ((Mask[i] >= e && isa<UndefValue>(RHS)) || |
| 11155 | (Mask[i] < e && isa<UndefValue>(LHS))) |
| 11156 | Mask[i] = 2*e; // Turn into undef. |
| 11157 | else |
| 11158 | Mask[i] &= (e-1); // Force to LHS. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11159 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11160 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11161 | } |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11162 | SVI.setOperand(0, SVI.getOperand(1)); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11163 | SVI.setOperand(1, UndefValue::get(RHS->getType())); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11164 | SVI.setOperand(2, ConstantVector::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11165 | LHS = SVI.getOperand(0); |
| 11166 | RHS = SVI.getOperand(1); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11167 | MadeChange = true; |
| 11168 | } |
| 11169 | |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11170 | // Analyze the shuffle, are the LHS or RHS and identity shuffles? |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11171 | bool isLHSID = true, isRHSID = true; |
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 11172 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11173 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 11174 | if (Mask[i] >= e*2) continue; // Ignore undef values. |
| 11175 | // Is this an identity shuffle of the LHS value? |
| 11176 | isLHSID &= (Mask[i] == i); |
| 11177 | |
| 11178 | // Is this an identity shuffle of the RHS value? |
| 11179 | isRHSID &= (Mask[i]-e == i); |
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 11180 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11181 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11182 | // Eliminate identity shuffles. |
| 11183 | if (isLHSID) return ReplaceInstUsesWith(SVI, LHS); |
| 11184 | if (isRHSID) return ReplaceInstUsesWith(SVI, RHS); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11185 | |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11186 | // If the LHS is a shufflevector itself, see if we can combine it with this |
| 11187 | // one without producing an unusual shuffle. Here we are really conservative: |
| 11188 | // we are absolutely afraid of producing a shuffle mask not in the input |
| 11189 | // program, because the code gen may not be smart enough to turn a merged |
| 11190 | // shuffle into two specific shuffles: it may produce worse code. As such, |
| 11191 | // we only merge two shuffles if the result is one of the two input shuffle |
| 11192 | // masks. In this case, merging the shuffles just removes one instruction, |
| 11193 | // which we know is safe. This is good for things like turning: |
| 11194 | // (splat(splat)) -> splat. |
| 11195 | if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) { |
| 11196 | if (isa<UndefValue>(RHS)) { |
| 11197 | std::vector<unsigned> LHSMask = getShuffleMask(LHSSVI); |
| 11198 | |
| 11199 | std::vector<unsigned> NewMask; |
| 11200 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) |
| 11201 | if (Mask[i] >= 2*e) |
| 11202 | NewMask.push_back(2*e); |
| 11203 | else |
| 11204 | NewMask.push_back(LHSMask[Mask[i]]); |
| 11205 | |
| 11206 | // If the result mask is equal to the src shuffle or this shuffle mask, do |
| 11207 | // the replacement. |
| 11208 | if (NewMask == LHSMask || NewMask == Mask) { |
| 11209 | std::vector<Constant*> Elts; |
| 11210 | for (unsigned i = 0, e = NewMask.size(); i != e; ++i) { |
| 11211 | if (NewMask[i] >= e*2) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11212 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11213 | } else { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11214 | Elts.push_back(ConstantInt::get(Type::Int32Ty, NewMask[i])); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11215 | } |
| 11216 | } |
| 11217 | return new ShuffleVectorInst(LHSSVI->getOperand(0), |
| 11218 | LHSSVI->getOperand(1), |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11219 | ConstantVector::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11220 | } |
| 11221 | } |
| 11222 | } |
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 11223 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11224 | return MadeChange ? &SVI : 0; |
| 11225 | } |
| 11226 | |
| 11227 | |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 11228 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11229 | |
| 11230 | /// TryToSinkInstruction - Try to move the specified instruction from its |
| 11231 | /// current block into the beginning of DestBlock, which can only happen if it's |
| 11232 | /// safe to move the instruction past all of the instructions between it and the |
| 11233 | /// end of its block. |
| 11234 | static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { |
| 11235 | assert(I->hasOneUse() && "Invariants didn't hold!"); |
| 11236 | |
Chris Lattner | 108e902 | 2005-10-27 17:13:11 +0000 | [diff] [blame] | 11237 | // Cannot move control-flow-involving, volatile loads, vaarg, etc. |
Chris Lattner | bfc538c | 2008-05-09 15:07:33 +0000 | [diff] [blame] | 11238 | if (isa<PHINode>(I) || I->mayWriteToMemory() || isa<TerminatorInst>(I)) |
| 11239 | return false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 11240 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11241 | // Do not sink alloca instructions out of the entry block. |
Dan Gohman | ecb7a77 | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 11242 | if (isa<AllocaInst>(I) && I->getParent() == |
| 11243 | &DestBlock->getParent()->getEntryBlock()) |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11244 | return false; |
| 11245 | |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 11246 | // We can only sink load instructions if there is nothing between the load and |
| 11247 | // the end of block that could change the value. |
Chris Lattner | 2539e33 | 2008-05-08 17:37:37 +0000 | [diff] [blame] | 11248 | if (I->mayReadFromMemory()) { |
| 11249 | for (BasicBlock::iterator Scan = I, E = I->getParent()->end(); |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 11250 | Scan != E; ++Scan) |
| 11251 | if (Scan->mayWriteToMemory()) |
| 11252 | return false; |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 11253 | } |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11254 | |
| 11255 | BasicBlock::iterator InsertPos = DestBlock->begin(); |
| 11256 | while (isa<PHINode>(InsertPos)) ++InsertPos; |
| 11257 | |
Chris Lattner | 4bc5f80 | 2005-08-08 19:11:57 +0000 | [diff] [blame] | 11258 | I->moveBefore(InsertPos); |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11259 | ++NumSunkInst; |
| 11260 | return true; |
| 11261 | } |
| 11262 | |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11263 | |
| 11264 | /// AddReachableCodeToWorklist - Walk the function in depth-first order, adding |
| 11265 | /// all reachable code to the worklist. |
| 11266 | /// |
| 11267 | /// This has a couple of tricks to make the code faster and more powerful. In |
| 11268 | /// particular, we constant fold and DCE instructions as we go, to avoid adding |
| 11269 | /// them to the worklist (this significantly speeds up instcombine on code where |
| 11270 | /// many instructions are dead or constant). Additionally, if we find a branch |
| 11271 | /// whose condition is a known constant, we only visit the reachable successors. |
| 11272 | /// |
| 11273 | static void AddReachableCodeToWorklist(BasicBlock *BB, |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 11274 | SmallPtrSet<BasicBlock*, 64> &Visited, |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11275 | InstCombiner &IC, |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11276 | const TargetData *TD) { |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11277 | std::vector<BasicBlock*> Worklist; |
| 11278 | Worklist.push_back(BB); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11279 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11280 | while (!Worklist.empty()) { |
| 11281 | BB = Worklist.back(); |
| 11282 | Worklist.pop_back(); |
| 11283 | |
| 11284 | // We have now visited this block! If we've already been here, ignore it. |
| 11285 | if (!Visited.insert(BB)) continue; |
| 11286 | |
| 11287 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { |
| 11288 | Instruction *Inst = BBI++; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11289 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11290 | // DCE instruction if trivially dead. |
| 11291 | if (isInstructionTriviallyDead(Inst)) { |
| 11292 | ++NumDeadInst; |
| 11293 | DOUT << "IC: DCE: " << *Inst; |
| 11294 | Inst->eraseFromParent(); |
| 11295 | continue; |
| 11296 | } |
| 11297 | |
| 11298 | // ConstantProp instruction if trivially constant. |
| 11299 | if (Constant *C = ConstantFoldInstruction(Inst, TD)) { |
| 11300 | DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst; |
| 11301 | Inst->replaceAllUsesWith(C); |
| 11302 | ++NumConstProp; |
| 11303 | Inst->eraseFromParent(); |
| 11304 | continue; |
| 11305 | } |
Chris Lattner | 3ccc6bc | 2007-07-20 22:06:41 +0000 | [diff] [blame] | 11306 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11307 | IC.AddToWorkList(Inst); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11308 | } |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11309 | |
| 11310 | // Recursively visit successors. If this is a branch or switch on a |
| 11311 | // constant, only visit the reachable successor. |
| 11312 | TerminatorInst *TI = BB->getTerminator(); |
| 11313 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
| 11314 | if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) { |
| 11315 | bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue(); |
Nick Lewycky | 9143699 | 2008-03-09 08:50:23 +0000 | [diff] [blame] | 11316 | BasicBlock *ReachableBB = BI->getSuccessor(!CondVal); |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 11317 | Worklist.push_back(ReachableBB); |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11318 | continue; |
| 11319 | } |
| 11320 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 11321 | if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) { |
| 11322 | // See if this is an explicit destination. |
| 11323 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) |
| 11324 | if (SI->getCaseValue(i) == Cond) { |
Nick Lewycky | 9143699 | 2008-03-09 08:50:23 +0000 | [diff] [blame] | 11325 | BasicBlock *ReachableBB = SI->getSuccessor(i); |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 11326 | Worklist.push_back(ReachableBB); |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11327 | continue; |
| 11328 | } |
| 11329 | |
| 11330 | // Otherwise it is the default destination. |
| 11331 | Worklist.push_back(SI->getSuccessor(0)); |
| 11332 | continue; |
| 11333 | } |
| 11334 | } |
| 11335 | |
| 11336 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| 11337 | Worklist.push_back(TI->getSuccessor(i)); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11338 | } |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11339 | } |
| 11340 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11341 | bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11342 | bool Changed = false; |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 11343 | TD = &getAnalysis<TargetData>(); |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11344 | |
| 11345 | DEBUG(DOUT << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on " |
| 11346 | << F.getNameStr() << "\n"); |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11347 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 11348 | { |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11349 | // Do a depth-first traversal of the function, populate the worklist with |
| 11350 | // the reachable instructions. Ignore blocks that are not reachable. Keep |
| 11351 | // track of which blocks we visit. |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 11352 | SmallPtrSet<BasicBlock*, 64> Visited; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11353 | AddReachableCodeToWorklist(F.begin(), Visited, *this, TD); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 11354 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 11355 | // Do a quick scan over the function. If we find any blocks that are |
| 11356 | // unreachable, remove any instructions inside of them. This prevents |
| 11357 | // the instcombine code from having to deal with some bad special cases. |
| 11358 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 11359 | if (!Visited.count(BB)) { |
| 11360 | Instruction *Term = BB->getTerminator(); |
| 11361 | while (Term != BB->begin()) { // Remove instrs bottom-up |
| 11362 | BasicBlock::iterator I = Term; --I; |
Chris Lattner | 6ffe551 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 11363 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 11364 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 11365 | ++NumDeadInst; |
| 11366 | |
| 11367 | if (!I->use_empty()) |
| 11368 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 11369 | I->eraseFromParent(); |
| 11370 | } |
| 11371 | } |
| 11372 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11373 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11374 | while (!Worklist.empty()) { |
| 11375 | Instruction *I = RemoveOneFromWorkList(); |
| 11376 | if (I == 0) continue; // skip null values. |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11377 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11378 | // Check to see if we can DCE the instruction. |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11379 | if (isInstructionTriviallyDead(I)) { |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11380 | // Add operands to the worklist. |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11381 | if (I->getNumOperands() < 4) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 11382 | AddUsesToWorkList(*I); |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11383 | ++NumDeadInst; |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11384 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 11385 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 11386 | |
| 11387 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11388 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11389 | continue; |
| 11390 | } |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11391 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11392 | // Instruction isn't dead, see if we can constant propagate it. |
Chris Lattner | 0a19ffa | 2007-01-30 23:16:15 +0000 | [diff] [blame] | 11393 | if (Constant *C = ConstantFoldInstruction(I, TD)) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 11394 | DOUT << "IC: ConstFold to: " << *C << " from: " << *I; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 11395 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11396 | // Add operands to the worklist. |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 11397 | AddUsesToWorkList(*I); |
Chris Lattner | c736d56 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 11398 | ReplaceInstUsesWith(*I, C); |
| 11399 | |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11400 | ++NumConstProp; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11401 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11402 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11403 | continue; |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11404 | } |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11405 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11406 | // See if we can trivially sink this instruction to a successor basic block. |
Chris Lattner | 2539e33 | 2008-05-08 17:37:37 +0000 | [diff] [blame] | 11407 | // FIXME: Remove GetResultInst test when first class support for aggregates |
| 11408 | // is implemented. |
Devang Patel | f944c9a | 2008-05-03 00:36:30 +0000 | [diff] [blame] | 11409 | if (I->hasOneUse() && !isa<GetResultInst>(I)) { |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11410 | BasicBlock *BB = I->getParent(); |
| 11411 | BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent(); |
| 11412 | if (UserParent != BB) { |
| 11413 | bool UserIsSuccessor = false; |
| 11414 | // See if the user is one of our successors. |
| 11415 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) |
| 11416 | if (*SI == UserParent) { |
| 11417 | UserIsSuccessor = true; |
| 11418 | break; |
| 11419 | } |
| 11420 | |
| 11421 | // If the user is one of our immediate successors, and if that successor |
| 11422 | // only has us as a predecessors (we'd have to split the critical edge |
| 11423 | // otherwise), we can keep going. |
| 11424 | if (UserIsSuccessor && !isa<PHINode>(I->use_back()) && |
| 11425 | next(pred_begin(UserParent)) == pred_end(UserParent)) |
| 11426 | // Okay, the CFG is simple enough, try to sink this instruction. |
| 11427 | Changed |= TryToSinkInstruction(I, UserParent); |
| 11428 | } |
| 11429 | } |
| 11430 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11431 | // Now that we have an instruction, try combining it to simplify it... |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 11432 | #ifndef NDEBUG |
| 11433 | std::string OrigI; |
| 11434 | #endif |
| 11435 | DEBUG(std::ostringstream SS; I->print(SS); OrigI = SS.str();); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 11436 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 11437 | ++NumCombined; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11438 | // Should we replace the old instruction with a new one? |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 11439 | if (Result != I) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 11440 | DOUT << "IC: Old = " << *I |
| 11441 | << " New = " << *Result; |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 11442 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 11443 | // Everything uses the new instruction now. |
| 11444 | I->replaceAllUsesWith(Result); |
| 11445 | |
| 11446 | // Push the new instruction and any users onto the worklist. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11447 | AddToWorkList(Result); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 11448 | AddUsersToWorkList(*Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11449 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 11450 | // Move the name to the new instruction first. |
| 11451 | Result->takeName(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11452 | |
| 11453 | // Insert the new instruction into the basic block... |
| 11454 | BasicBlock *InstParent = I->getParent(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 11455 | BasicBlock::iterator InsertPos = I; |
| 11456 | |
| 11457 | if (!isa<PHINode>(Result)) // If combining a PHI, don't insert |
| 11458 | while (isa<PHINode>(InsertPos)) // middle of a block of PHIs. |
| 11459 | ++InsertPos; |
| 11460 | |
| 11461 | InstParent->getInstList().insert(InsertPos, Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11462 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 11463 | // Make sure that we reprocess all operands now that we reduced their |
| 11464 | // use counts. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11465 | AddUsesToWorkList(*I); |
Chris Lattner | 216d4d8 | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 11466 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 11467 | // Instructions can end up on the worklist more than once. Make sure |
| 11468 | // we do not process an instruction that has been deleted. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11469 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11470 | |
| 11471 | // Erase the old instruction. |
| 11472 | InstParent->getInstList().erase(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 11473 | } else { |
Evan Cheng | c7baf68 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 11474 | #ifndef NDEBUG |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 11475 | DOUT << "IC: Mod = " << OrigI |
| 11476 | << " New = " << *I; |
Evan Cheng | c7baf68 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 11477 | #endif |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 11478 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 11479 | // If the instruction was modified, it's possible that it is now dead. |
| 11480 | // if so, remove it. |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 11481 | if (isInstructionTriviallyDead(I)) { |
| 11482 | // Make sure we process all operands now that we are reducing their |
| 11483 | // use counts. |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11484 | AddUsesToWorkList(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 11485 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 11486 | // Instructions may end up in the worklist more than once. Erase all |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 11487 | // occurrences of this instruction. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11488 | RemoveFromWorkList(I); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 11489 | I->eraseFromParent(); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 11490 | } else { |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11491 | AddToWorkList(I); |
| 11492 | AddUsersToWorkList(*I); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 11493 | } |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 11494 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11495 | Changed = true; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11496 | } |
| 11497 | } |
| 11498 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11499 | assert(WorklistMap.empty() && "Worklist empty, but map not?"); |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 11500 | |
| 11501 | // Do an explicit clear, this shrinks the map if needed. |
| 11502 | WorklistMap.clear(); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11503 | return Changed; |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 11504 | } |
| 11505 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11506 | |
| 11507 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 11508 | MustPreserveLCSSA = mustPreserveAnalysisID(LCSSAID); |
| 11509 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11510 | bool EverMadeChange = false; |
| 11511 | |
| 11512 | // Iterate while there is work to do. |
| 11513 | unsigned Iteration = 0; |
Bill Wendling | a6c3112 | 2008-05-14 22:45:20 +0000 | [diff] [blame] | 11514 | while (DoOneIteration(F, Iteration++)) |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11515 | EverMadeChange = true; |
| 11516 | return EverMadeChange; |
| 11517 | } |
| 11518 | |
Brian Gaeke | 96d4bf7 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 11519 | FunctionPass *llvm::createInstructionCombiningPass() { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11520 | return new InstCombiner(); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 11521 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 11522 | |